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,
25 /* Avoid a compiler warning */
39 * <setjmp.h> is used for the optional error recovery mechanism shown in
40 * the second part of the example.
48 * The JPEG library's standard error handler (jerror.c) is divided into
49 * several "methods" which you can override individually. This lets you
50 * adjust the behavior without duplicating a lot of code, which you might
51 * have to update with each future release.
53 * Our example here shows how to override the "error_exit" method so that
54 * control is returned to the library's caller when a fatal error occurs,
55 * rather than calling exit() as the standard error_exit method does.
57 * We use C's setjmp/longjmp facility to return control. This means that the
58 * routine which calls the JPEG library must first execute a setjmp() call to
59 * establish the return point. We want the replacement error_exit to do a
60 * longjmp(). But we need to make the setjmp buffer accessible to the
61 * error_exit routine. To do this, we make a private extension of the
62 * standard JPEG error handler object. (If we were using C++, we'd say we
63 * were making a subclass of the regular error handler.)
65 * Here's the extended error handler struct:
69 struct jpeg_error_mgr pub
; /* "public" fields */
71 jmp_buf setjmp_buffer
; /* for return to caller */
74 typedef struct my_error_mgr
*my_error_ptr
;
77 * Here's the routine that will replace the standard error_exit method:
80 static void my_error_exit(j_common_ptr cinfo
)
82 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
83 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
85 /* Always display the message. */
86 /* We could postpone this until after returning, if we chose. */
87 (*cinfo
->err
->output_message
) (cinfo
);
89 /* Return control to the setjmp point */
90 longjmp(myerr
->setjmp_buffer
, 1);
93 RImage
*RLoadJPEG(RContext
* context
, char *file_name
)
96 struct jpeg_decompress_struct cinfo
;
99 JSAMPROW buffer
[1], bptr
;
101 /* We use our private extension JPEG error handler.
102 * Note that this struct must live as long as the main JPEG parameter
103 * struct, to avoid dangling-pointer problems.
105 struct my_error_mgr jerr
;
107 file
= fopen(file_name
, "rb");
109 RErrorCode
= RERR_OPEN
;
113 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
114 jerr
.pub
.error_exit
= my_error_exit
;
115 /* Establish the setjmp return context for my_error_exit to use. */
116 if (setjmp(jerr
.setjmp_buffer
)) {
117 /* If we get here, the JPEG code has signaled an error.
118 * We need to clean up the JPEG object, close the input file, and return.
120 jpeg_destroy_decompress(&cinfo
);
125 jpeg_create_decompress(&cinfo
);
127 jpeg_stdio_src(&cinfo
, file
);
129 jpeg_read_header(&cinfo
, TRUE
);
131 if (cinfo
.image_width
< 1 || cinfo
.image_height
< 1) {
132 RErrorCode
= RERR_BADIMAGEFILE
;
136 bptr
= buffer
[0] = (JSAMPROW
) malloc(cinfo
.image_width
* cinfo
.num_components
);
138 RErrorCode
= RERR_NOMEMORY
;
142 if (cinfo
.jpeg_color_space
== JCS_GRAYSCALE
) {
143 cinfo
.out_color_space
= JCS_GRAYSCALE
;
145 cinfo
.out_color_space
= JCS_RGB
;
146 cinfo
.quantize_colors
= FALSE
;
147 cinfo
.do_fancy_upsampling
= FALSE
;
148 cinfo
.do_block_smoothing
= FALSE
;
149 jpeg_calc_output_dimensions(&cinfo
);
151 if (context
->flags
.optimize_for_speed
)
152 image
= RCreateImage(cinfo
.image_width
, cinfo
.image_height
, True
);
154 image
= RCreateImage(cinfo
.image_width
, cinfo
.image_height
, False
);
157 RErrorCode
= RERR_NOMEMORY
;
160 jpeg_start_decompress(&cinfo
);
164 if (cinfo
.out_color_space
== JCS_RGB
) {
165 if (context
->flags
.optimize_for_speed
) {
166 while (cinfo
.output_scanline
< cinfo
.output_height
) {
167 jpeg_read_scanlines(&cinfo
, buffer
, (JDIMENSION
) 1);
169 for (i
= 0; i
< cinfo
.image_width
; i
++) {
173 ptr
++; /* skip alpha channel */
177 while (cinfo
.output_scanline
< cinfo
.output_height
) {
178 jpeg_read_scanlines(&cinfo
, buffer
, (JDIMENSION
) 1);
180 memcpy(ptr
, bptr
, cinfo
.image_width
* 3);
181 ptr
+= cinfo
.image_width
* 3;
185 while (cinfo
.output_scanline
< cinfo
.output_height
) {
186 jpeg_read_scanlines(&cinfo
, buffer
, (JDIMENSION
) 1);
188 for (i
= 0; i
< cinfo
.image_width
; i
++) {
196 jpeg_finish_decompress(&cinfo
);
199 jpeg_destroy_decompress(&cinfo
);
209 #endif /* USE_JPEG */