wrlib: Fix typo in macro containing ImageMagick version
[wmaker-crm.git] / wrlib / load_jpeg.c
blobd3938e5c0fad0df25eb6ccb80750c6d7ea033f3d
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"
42 * <setjmp.h> is used for the optional error recovery mechanism shown in
43 * the second part of the example.
46 #include <setjmp.h>
49 * ERROR HANDLING:
51 * The JPEG library's standard error handler (jerror.c) is divided into
52 * several "methods" which you can override individually. This lets you
53 * adjust the behavior without duplicating a lot of code, which you might
54 * have to update with each future release.
56 * Our example here shows how to override the "error_exit" method so that
57 * control is returned to the library's caller when a fatal error occurs,
58 * rather than calling exit() as the standard error_exit method does.
60 * We use C's setjmp/longjmp facility to return control. This means that the
61 * routine which calls the JPEG library must first execute a setjmp() call to
62 * establish the return point. We want the replacement error_exit to do a
63 * longjmp(). But we need to make the setjmp buffer accessible to the
64 * error_exit routine. To do this, we make a private extension of the
65 * standard JPEG error handler object. (If we were using C++, we'd say we
66 * were making a subclass of the regular error handler.)
68 * Here's the extended error handler struct:
71 struct my_error_mgr {
72 struct jpeg_error_mgr pub; /* "public" fields */
74 jmp_buf setjmp_buffer; /* for return to caller */
77 typedef struct my_error_mgr *my_error_ptr;
80 * Here's the routine that will replace the standard error_exit method:
83 static noreturn void my_error_exit(j_common_ptr cinfo)
85 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
86 my_error_ptr myerr = (my_error_ptr) cinfo->err;
88 /* Always display the message. */
89 /* We could postpone this until after returning, if we chose. */
90 (*cinfo->err->output_message) (cinfo);
92 /* Return control to the setjmp point */
93 longjmp(myerr->setjmp_buffer, 1);
96 RImage *RLoadJPEG(const char *file_name)
98 RImage *image = NULL;
99 struct jpeg_decompress_struct cinfo;
100 int i;
101 unsigned char *ptr;
102 JSAMPROW buffer[1], bptr;
103 FILE *file;
104 /* We use our private extension JPEG error handler.
105 * Note that this struct must live as long as the main JPEG parameter
106 * struct, to avoid dangling-pointer problems.
108 struct my_error_mgr jerr;
110 file = fopen(file_name, "rb");
111 if (!file) {
112 RErrorCode = RERR_OPEN;
113 return NULL;
116 cinfo.err = jpeg_std_error(&jerr.pub);
117 jerr.pub.error_exit = my_error_exit;
118 /* Establish the setjmp return context for my_error_exit to use. */
119 if (setjmp(jerr.setjmp_buffer)) {
121 * If we get here, the JPEG code has signaled an error.
122 * We need to clean up the JPEG object, close the input file, and return.
124 jpeg_destroy_decompress(&cinfo);
125 fclose(file);
126 return NULL;
129 jpeg_create_decompress(&cinfo);
130 jpeg_stdio_src(&cinfo, file);
131 jpeg_read_header(&cinfo, TRUE);
132 if (cinfo.image_width < 1 || cinfo.image_height < 1) {
133 RErrorCode = RERR_BADIMAGEFILE;
134 jpeg_destroy_decompress(&cinfo);
135 fclose(file);
136 return NULL;
139 buffer[0] = (JSAMPROW) malloc(cinfo.image_width * cinfo.num_components);
140 if (!buffer[0]) {
141 RErrorCode = RERR_NOMEMORY;
142 jpeg_destroy_decompress(&cinfo);
143 fclose(file);
144 return NULL;
147 if (cinfo.jpeg_color_space == JCS_GRAYSCALE)
148 cinfo.out_color_space = JCS_GRAYSCALE;
149 else
150 cinfo.out_color_space = JCS_RGB;
152 cinfo.quantize_colors = FALSE;
153 cinfo.do_fancy_upsampling = FALSE;
154 cinfo.do_block_smoothing = FALSE;
155 jpeg_calc_output_dimensions(&cinfo);
156 image = RCreateImage(cinfo.image_width, cinfo.image_height, False);
157 if (!image) {
158 RErrorCode = RERR_NOMEMORY;
159 jpeg_destroy_decompress(&cinfo);
160 fclose(file);
161 if (buffer[0])
162 free(buffer[0]);
164 return NULL;
167 jpeg_start_decompress(&cinfo);
168 ptr = image->data;
169 if (cinfo.out_color_space == JCS_RGB) {
170 while (cinfo.output_scanline < cinfo.output_height) {
171 jpeg_read_scanlines(&cinfo, buffer, (JDIMENSION) 1);
172 bptr = buffer[0];
173 memcpy(ptr, bptr, cinfo.image_width * 3);
174 ptr += cinfo.image_width * 3;
176 } else {
177 while (cinfo.output_scanline < cinfo.output_height) {
178 jpeg_read_scanlines(&cinfo, buffer, (JDIMENSION) 1);
179 bptr = buffer[0];
180 for (i = 0; i < cinfo.image_width; i++) {
181 *ptr++ = *bptr;
182 *ptr++ = *bptr;
183 *ptr++ = *bptr++;
188 jpeg_finish_decompress(&cinfo);
189 jpeg_destroy_decompress(&cinfo);
190 fclose(file);
191 if (buffer[0])
192 free(buffer[0]);
194 return image;