wmiv: align version with wm
[wmaker-crm.git] / wrlib / load_jpeg.c
blob9c2ef6be909d279405589c3e8bf8080f4f6da6d4
1 /* jpeg.c - load JPEG image from file
3 * Raster graphics library
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include <config.h>
25 /* Avoid a compiler warning */
26 #undef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include <jpeglib.h>
34 #ifdef HAVE_STDNORETURN
35 #include <stdnoreturn.h>
36 #endif
38 #include "wraster.h"
39 #include "imgformat.h"
40 #include "wr_i18n.h"
44 * <setjmp.h> is used for the optional error recovery mechanism shown in
45 * the second part of the example.
48 #include <setjmp.h>
51 * ERROR HANDLING:
53 * The JPEG library's standard error handler (jerror.c) is divided into
54 * several "methods" which you can override individually. This lets you
55 * adjust the behavior without duplicating a lot of code, which you might
56 * have to update with each future release.
58 * Our example here shows how to override the "error_exit" method so that
59 * control is returned to the library's caller when a fatal error occurs,
60 * rather than calling exit() as the standard error_exit method does.
62 * We use C's setjmp/longjmp facility to return control. This means that the
63 * routine which calls the JPEG library must first execute a setjmp() call to
64 * establish the return point. We want the replacement error_exit to do a
65 * longjmp(). But we need to make the setjmp buffer accessible to the
66 * error_exit routine. To do this, we make a private extension of the
67 * standard JPEG error handler object. (If we were using C++, we'd say we
68 * were making a subclass of the regular error handler.)
70 * Here's the extended error handler struct:
73 struct my_error_mgr {
74 struct jpeg_error_mgr pub; /* "public" fields */
76 jmp_buf setjmp_buffer; /* for return to caller */
79 typedef struct my_error_mgr *my_error_ptr;
82 * Here's the routine that will replace the standard error_exit method:
85 static noreturn void my_error_exit(j_common_ptr cinfo)
87 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
88 my_error_ptr myerr = (my_error_ptr) cinfo->err;
90 /* Always display the message. */
91 /* We could postpone this until after returning, if we chose. */
92 (*cinfo->err->output_message) (cinfo);
94 /* Return control to the setjmp point */
95 longjmp(myerr->setjmp_buffer, 1);
98 static RImage *do_read_jpeg_file(struct jpeg_decompress_struct *cinfo, const char *file_name)
100 RImage *image = NULL;
101 int i;
102 unsigned char *ptr;
103 JSAMPROW buffer[1], bptr;
104 FILE *file;
106 file = fopen(file_name, "rb");
107 if (!file) {
108 RErrorCode = RERR_OPEN;
109 return NULL;
112 jpeg_create_decompress(cinfo);
113 jpeg_stdio_src(cinfo, file);
114 jpeg_read_header(cinfo, TRUE);
116 if (cinfo->image_width < 1 || cinfo->image_height < 1) {
117 buffer[0] = NULL; /* Initialize pointer to avoid spurious free in cleanup code */
118 RErrorCode = RERR_BADIMAGEFILE;
119 goto abort_and_release_resources;
122 buffer[0] = (JSAMPROW) malloc(cinfo->image_width * cinfo->num_components);
123 if (!buffer[0]) {
124 RErrorCode = RERR_NOMEMORY;
125 goto abort_and_release_resources;
128 if (cinfo->jpeg_color_space == JCS_GRAYSCALE)
129 cinfo->out_color_space = JCS_GRAYSCALE;
130 else
131 cinfo->out_color_space = JCS_RGB;
133 cinfo->quantize_colors = FALSE;
134 cinfo->do_fancy_upsampling = FALSE;
135 cinfo->do_block_smoothing = FALSE;
136 jpeg_calc_output_dimensions(cinfo);
137 image = RCreateImage(cinfo->image_width, cinfo->image_height, False);
138 if (!image) {
139 RErrorCode = RERR_NOMEMORY;
140 goto abort_and_release_resources;
143 jpeg_start_decompress(cinfo);
144 ptr = image->data;
145 if (cinfo->out_color_space == JCS_RGB) {
146 while (cinfo->output_scanline < cinfo->output_height) {
147 jpeg_read_scanlines(cinfo, buffer, (JDIMENSION) 1);
148 bptr = buffer[0];
149 memcpy(ptr, bptr, cinfo->image_width * 3);
150 ptr += cinfo->image_width * 3;
152 } else {
153 while (cinfo->output_scanline < cinfo->output_height) {
154 jpeg_read_scanlines(cinfo, buffer, (JDIMENSION) 1);
155 bptr = buffer[0];
156 for (i = 0; i < cinfo->image_width; i++) {
157 *ptr++ = *bptr;
158 *ptr++ = *bptr;
159 *ptr++ = *bptr++;
164 jpeg_finish_decompress(cinfo);
166 abort_and_release_resources:
167 jpeg_destroy_decompress(cinfo);
168 fclose(file);
169 if (buffer[0])
170 free(buffer[0]);
172 return image;
175 RImage *RLoadJPEG(const char *file_name)
177 struct jpeg_decompress_struct cinfo;
178 /* We use our private extension JPEG error handler.
179 * Note that this struct must live as long as the main
180 * JPEG parameter struct, to avoid dangling-pointer problems.
182 struct my_error_mgr jerr;
184 cinfo.err = jpeg_std_error(&jerr.pub);
185 jerr.pub.error_exit = my_error_exit;
186 /* Establish the setjmp return context for my_error_exit to use. */
187 if (setjmp(jerr.setjmp_buffer)) {
189 * If we get here, the JPEG code has signaled an error.
190 * We need to clean up the JPEG object, close the input file, and return.
192 jpeg_destroy_decompress(&cinfo);
193 return NULL;
195 return do_read_jpeg_file(&cinfo, file_name);