Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / jpeg.c
1 /* jpeg.c - load JPEG image from file
2  *
3  * Raster graphics library
4  *
5  * Copyright (c) 1997-2003 Alfredo K. Kojima
6  *
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.
11  *
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.
16  *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <config.h>
23
24 /* Avoid a compiler warning */
25 #undef HAVE_STDLIB_H
26
27 #ifdef USE_JPEG
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include <jpeglib.h>
34
35 #include "wraster.h"
36
37 /*
38  * <setjmp.h> is used for the optional error recovery mechanism shown in
39  * the second part of the example.
40  */
41
42 #include <setjmp.h>
43
44 /*
45  * ERROR HANDLING:
46  *
47  * The JPEG library's standard error handler (jerror.c) is divided into
48  * several "methods" which you can override individually.  This lets you
49  * adjust the behavior without duplicating a lot of code, which you might
50  * have to update with each future release.
51  *
52  * Our example here shows how to override the "error_exit" method so that
53  * control is returned to the library's caller when a fatal error occurs,
54  * rather than calling exit() as the standard error_exit method does.
55  *
56  * We use C's setjmp/longjmp facility to return control.  This means that the
57  * routine which calls the JPEG library must first execute a setjmp() call to
58  * establish the return point.  We want the replacement error_exit to do a
59  * longjmp().  But we need to make the setjmp buffer accessible to the
60  * error_exit routine.  To do this, we make a private extension of the
61  * standard JPEG error handler object.  (If we were using C++, we'd say we
62  * were making a subclass of the regular error handler.)
63  *
64  * Here's the extended error handler struct:
65  */
66
67 struct my_error_mgr {
68         struct jpeg_error_mgr pub;      /* "public" fields */
69
70         jmp_buf setjmp_buffer;  /* for return to caller */
71 };
72
73 typedef struct my_error_mgr *my_error_ptr;
74
75 /*
76  * Here's the routine that will replace the standard error_exit method:
77  */
78
79 static void my_error_exit(j_common_ptr cinfo)
80 {
81         /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
82         my_error_ptr myerr = (my_error_ptr) cinfo->err;
83
84         /* Always display the message. */
85         /* We could postpone this until after returning, if we chose. */
86         (*cinfo->err->output_message) (cinfo);
87
88         /* Return control to the setjmp point */
89         longjmp(myerr->setjmp_buffer, 1);
90 }
91
92 RImage *RLoadJPEG(RContext * context, char *file_name, int index)
93 {
94         RImage *image = NULL;
95         struct jpeg_decompress_struct cinfo;
96         int i;
97         unsigned char *ptr;
98         JSAMPROW buffer[1], bptr;
99         FILE *file;
100         /* We use our private extension JPEG error handler.
101          * Note that this struct must live as long as the main JPEG parameter
102          * struct, to avoid dangling-pointer problems.
103          */
104         struct my_error_mgr jerr;
105
106         file = fopen(file_name, "rb");
107         if (!file) {
108                 RErrorCode = RERR_OPEN;
109                 return NULL;
110         }
111
112         cinfo.err = jpeg_std_error(&jerr.pub);
113         jerr.pub.error_exit = my_error_exit;
114         /* Establish the setjmp return context for my_error_exit to use. */
115         if (setjmp(jerr.setjmp_buffer)) {
116                 /* If we get here, the JPEG code has signaled an error.
117                  * We need to clean up the JPEG object, close the input file, and return.
118                  */
119                 jpeg_destroy_decompress(&cinfo);
120                 fclose(file);
121                 return NULL;
122         }
123
124         jpeg_create_decompress(&cinfo);
125
126         jpeg_stdio_src(&cinfo, file);
127
128         jpeg_read_header(&cinfo, TRUE);
129
130         if (cinfo.image_width < 1 || cinfo.image_height < 1) {
131                 RErrorCode = RERR_BADIMAGEFILE;
132                 goto bye;
133         }
134
135         bptr = buffer[0] = (JSAMPROW) malloc(cinfo.image_width * cinfo.num_components);
136         if (!buffer[0]) {
137                 RErrorCode = RERR_NOMEMORY;
138                 goto bye;
139         }
140
141         if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
142                 cinfo.out_color_space = JCS_GRAYSCALE;
143         } else
144                 cinfo.out_color_space = JCS_RGB;
145         cinfo.quantize_colors = FALSE;
146         cinfo.do_fancy_upsampling = FALSE;
147         cinfo.do_block_smoothing = FALSE;
148         jpeg_calc_output_dimensions(&cinfo);
149
150         if (context->flags.optimize_for_speed)
151                 image = RCreateImage(cinfo.image_width, cinfo.image_height, True);
152         else
153                 image = RCreateImage(cinfo.image_width, cinfo.image_height, False);
154
155         if (!image) {
156                 RErrorCode = RERR_NOMEMORY;
157                 goto bye;
158         }
159         jpeg_start_decompress(&cinfo);
160
161         ptr = image->data;
162
163         if (cinfo.out_color_space == JCS_RGB) {
164                 if (context->flags.optimize_for_speed) {
165                         while (cinfo.output_scanline < cinfo.output_height) {
166                                 jpeg_read_scanlines(&cinfo, buffer, (JDIMENSION) 1);
167                                 bptr = buffer[0];
168                                 for (i = 0; i < cinfo.image_width; i++) {
169                                         *ptr++ = *bptr++;
170                                         *ptr++ = *bptr++;
171                                         *ptr++ = *bptr++;
172                                         ptr++;  /* skip alpha channel */
173                                 }
174                         }
175                 } else {
176                         while (cinfo.output_scanline < cinfo.output_height) {
177                                 jpeg_read_scanlines(&cinfo, buffer, (JDIMENSION) 1);
178                                 bptr = buffer[0];
179                                 memcpy(ptr, bptr, cinfo.image_width * 3);
180                                 ptr += cinfo.image_width * 3;
181                         }
182                 }
183         } else {
184                 while (cinfo.output_scanline < cinfo.output_height) {
185                         jpeg_read_scanlines(&cinfo, buffer, (JDIMENSION) 1);
186                         bptr = buffer[0];
187                         for (i = 0; i < cinfo.image_width; i++) {
188                                 *ptr++ = *bptr;
189                                 *ptr++ = *bptr;
190                                 *ptr++ = *bptr++;
191                         }
192                 }
193         }
194
195         jpeg_finish_decompress(&cinfo);
196
197  bye:
198         jpeg_destroy_decompress(&cinfo);
199
200         fclose(file);
201
202         if (buffer[0])
203                 free(buffer[0]);
204
205         return image;
206 }
207
208 #endif                          /* USE_JPEG */