wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / wrlib / jpeg.c
blob2339f516f54128d54ca6035e37b69bff2880bdb0
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 #ifdef USE_JPEG
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
34 #include <jpeglib.h>
36 #include "wraster.h"
37 #include "imgformat.h"
40 * <setjmp.h> is used for the optional error recovery mechanism shown in
41 * the second part of the example.
44 #include <setjmp.h>
47 * ERROR HANDLING:
49 * The JPEG library's standard error handler (jerror.c) is divided into
50 * several "methods" which you can override individually. This lets you
51 * adjust the behavior without duplicating a lot of code, which you might
52 * have to update with each future release.
54 * Our example here shows how to override the "error_exit" method so that
55 * control is returned to the library's caller when a fatal error occurs,
56 * rather than calling exit() as the standard error_exit method does.
58 * We use C's setjmp/longjmp facility to return control. This means that the
59 * routine which calls the JPEG library must first execute a setjmp() call to
60 * establish the return point. We want the replacement error_exit to do a
61 * longjmp(). But we need to make the setjmp buffer accessible to the
62 * error_exit routine. To do this, we make a private extension of the
63 * standard JPEG error handler object. (If we were using C++, we'd say we
64 * were making a subclass of the regular error handler.)
66 * Here's the extended error handler struct:
69 struct my_error_mgr {
70 struct jpeg_error_mgr pub; /* "public" fields */
72 jmp_buf setjmp_buffer; /* for return to caller */
75 typedef struct my_error_mgr *my_error_ptr;
78 * Here's the routine that will replace the standard error_exit method:
81 static void my_error_exit(j_common_ptr cinfo)
83 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
84 my_error_ptr myerr = (my_error_ptr) cinfo->err;
86 /* Always display the message. */
87 /* We could postpone this until after returning, if we chose. */
88 (*cinfo->err->output_message) (cinfo);
90 /* Return control to the setjmp point */
91 longjmp(myerr->setjmp_buffer, 1);
94 RImage *RLoadJPEG(RContext * context, const char *file_name)
96 RImage *image = NULL;
97 struct jpeg_decompress_struct cinfo;
98 int i;
99 unsigned char *ptr;
100 JSAMPROW buffer[1], bptr;
101 FILE *file;
102 /* We use our private extension JPEG error handler.
103 * Note that this struct must live as long as the main JPEG parameter
104 * struct, to avoid dangling-pointer problems.
106 struct my_error_mgr jerr;
108 file = fopen(file_name, "rb");
109 if (!file) {
110 RErrorCode = RERR_OPEN;
111 return NULL;
114 cinfo.err = jpeg_std_error(&jerr.pub);
115 jerr.pub.error_exit = my_error_exit;
116 /* Establish the setjmp return context for my_error_exit to use. */
117 if (setjmp(jerr.setjmp_buffer)) {
118 /* If we get here, the JPEG code has signaled an error.
119 * We need to clean up the JPEG object, close the input file, and return.
121 jpeg_destroy_decompress(&cinfo);
122 fclose(file);
123 return NULL;
126 jpeg_create_decompress(&cinfo);
128 jpeg_stdio_src(&cinfo, file);
130 jpeg_read_header(&cinfo, TRUE);
132 if (cinfo.image_width < 1 || cinfo.image_height < 1) {
133 RErrorCode = RERR_BADIMAGEFILE;
134 goto bye;
137 bptr = buffer[0] = (JSAMPROW) malloc(cinfo.image_width * cinfo.num_components);
138 if (!buffer[0]) {
139 RErrorCode = RERR_NOMEMORY;
140 goto bye;
143 if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
144 cinfo.out_color_space = JCS_GRAYSCALE;
145 } else
146 cinfo.out_color_space = JCS_RGB;
147 cinfo.quantize_colors = FALSE;
148 cinfo.do_fancy_upsampling = FALSE;
149 cinfo.do_block_smoothing = FALSE;
150 jpeg_calc_output_dimensions(&cinfo);
152 if (context->flags.optimize_for_speed)
153 image = RCreateImage(cinfo.image_width, cinfo.image_height, True);
154 else
155 image = RCreateImage(cinfo.image_width, cinfo.image_height, False);
157 if (!image) {
158 RErrorCode = RERR_NOMEMORY;
159 goto bye;
161 jpeg_start_decompress(&cinfo);
163 ptr = image->data;
165 if (cinfo.out_color_space == JCS_RGB) {
166 if (context->flags.optimize_for_speed) {
167 while (cinfo.output_scanline < cinfo.output_height) {
168 jpeg_read_scanlines(&cinfo, buffer, (JDIMENSION) 1);
169 bptr = buffer[0];
170 for (i = 0; i < cinfo.image_width; i++) {
171 *ptr++ = *bptr++;
172 *ptr++ = *bptr++;
173 *ptr++ = *bptr++;
174 ptr++; /* skip alpha channel */
177 } else {
178 while (cinfo.output_scanline < cinfo.output_height) {
179 jpeg_read_scanlines(&cinfo, buffer, (JDIMENSION) 1);
180 bptr = buffer[0];
181 memcpy(ptr, bptr, cinfo.image_width * 3);
182 ptr += cinfo.image_width * 3;
185 } else {
186 while (cinfo.output_scanline < cinfo.output_height) {
187 jpeg_read_scanlines(&cinfo, buffer, (JDIMENSION) 1);
188 bptr = buffer[0];
189 for (i = 0; i < cinfo.image_width; i++) {
190 *ptr++ = *bptr;
191 *ptr++ = *bptr;
192 *ptr++ = *bptr++;
197 jpeg_finish_decompress(&cinfo);
199 bye:
200 jpeg_destroy_decompress(&cinfo);
202 fclose(file);
204 if (buffer[0])
205 free(buffer[0]);
207 return image;
210 #endif /* USE_JPEG */