4 * This file illustrates how to use the IJG code as a subroutine library
5 * to read or write JPEG image files. You should look at this code in
6 * conjunction with the documentation file libjpeg.doc.
8 * This code will not do anything useful as-is, but it may be helpful as a
9 * skeleton for constructing routines that call the JPEG library.
11 * We present these routines in the same coding style used in the JPEG code
12 * (ANSI function definitions, etc); but you are of course free to code your
13 * routines in a different style if you prefer.
22 * Include file for users of JPEG library.
23 * You will need to have included system headers that define at least
24 * the typedefs FILE and size_t before you can include jpeglib.h.
25 * (stdio.h is sufficient on ANSI-conforming systems.)
26 * You may also wish to include "jerror.h".
31 * <setjmp.h> is used for the optional error recovery mechanism shown in
32 * the second part of the example.
39 /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
41 /* This half of the example shows how to read data from the JPEG decompressor.
42 * It's a bit more refined than the above, in that we show:
43 * (a) how to modify the JPEG library's standard error-reporting behavior;
44 * (b) how to allocate workspace using the library's memory manager.
46 * Just to make this example a little different from the first one, we'll
47 * assume that we do not intend to put the whole image into an in-memory
48 * buffer, but to send it line-by-line someplace else. We need a one-
49 * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
50 * memory manager allocate it for us. This approach is actually quite useful
51 * because we don't need to remember to deallocate the buffer separately: it
52 * will go away automatically when the JPEG object is cleaned up.
59 * The JPEG library's standard error handler (jerror.c) is divided into
60 * several "methods" which you can override individually. This lets you
61 * adjust the behavior without duplicating a lot of code, which you might
62 * have to update with each future release.
64 * Our example here shows how to override the "error_exit" method so that
65 * control is returned to the library's caller when a fatal error occurs,
66 * rather than calling exit() as the standard error_exit method does.
68 * We use C's setjmp/longjmp facility to return control. This means that the
69 * routine which calls the JPEG library must first execute a setjmp() call to
70 * establish the return point. We want the replacement error_exit to do a
71 * longjmp(). But we need to make the setjmp buffer accessible to the
72 * error_exit routine. To do this, we make a private extension of the
73 * standard JPEG error handler object. (If we were using C++, we'd say we
74 * were making a subclass of the regular error handler.)
76 * Here's the extended error handler struct:
80 struct jpeg_error_mgr pub
; /* "public" fields */
82 jmp_buf setjmp_buffer
; /* for return to caller */
85 typedef struct my_error_mgr
* my_error_ptr
;
88 * Here's the routine that will replace the standard error_exit method:
91 my_error_exit (j_common_ptr cinfo
)
93 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
94 my_error_ptr myerr
= (my_error_ptr
) cinfo
->err
;
96 /* Always display the message. */
97 /* We could postpone this until after returning, if we chose. */
98 (*cinfo
->err
->output_message
) (cinfo
);
100 /* Return control to the setjmp point */
101 longjmp(myerr
->setjmp_buffer
, 1);
106 * Sample routine for JPEG decompression. We assume that the source file name
107 * is passed in. We want to return 1 on success, 0 on error.
115 void put_jpegdata(unsigned char* b
,int l
)
123 im_coding((unsigned int)b
[i
],(unsigned int) b
[i
],(unsigned int) b
[i
],(char**) &mybuf
);
126 im_coding((unsigned int)b
[i
],(unsigned int) b
[i
+1],(unsigned int) b
[i
+2],(char**) &mybuf
);
127 mybuf
= buf
+ line_w
;
130 struct jpeg_decompress_struct cinfo
;
131 /* We use our private extension JPEG error handler.
132 * Note that this struct must live as long as the main JPEG parameter
133 * struct, to avoid dangling-pointer problems.
135 struct my_error_mgr jerr
;
137 FILE * infile
; /* source file */
138 JSAMPARRAY buffer
; /* Output row buffer */
139 int row_stride
; /* physical row width in output buffer */
141 /* In this example we want to open the input file before doing anything else,
142 * so that the setjmp() error recovery below can assume the file is open.
143 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
144 * requires it in order to read binary files.
148 int LoadJFIF(char * filename
,PICINFO
* pc
)
150 /* This struct contains the JPEG decompression parameters and pointers to
151 * working space (which is allocated as needed by the JPEG library).
154 if ((infile
= fopen(filename
, "rb")) == NULL
) {
155 fprintf(stderr
, "can't open %s\n", filename
);
159 /* Step 1: allocate and initialize JPEG decompression object */
161 /* We set up the normal JPEG error routines, then override error_exit. */
162 cinfo
.err
= jpeg_std_error(&jerr
.pub
);
163 jerr
.pub
.error_exit
= my_error_exit
;
164 /* Establish the setjmp return context for my_error_exit to use. */
165 if (setjmp(jerr
.setjmp_buffer
)) {
166 /* If we get here, the JPEG code has signaled an error.
167 * We need to clean up the JPEG object, close the input file, and return.
169 jpeg_destroy_decompress(&cinfo
);
173 /* Now we can initialize the JPEG decompression object. */
174 jpeg_create_decompress(&cinfo
);
176 /* Step 2: specify data source (eg, a file) */
178 jpeg_stdio_src(&cinfo
, infile
);
180 /* Step 3: read file parameters with jpeg_read_header() */
182 (void) jpeg_read_header(&cinfo
, TRUE
);
183 /* We can ignore the return value from jpeg_read_header since
184 * (a) suspension is not possible with the stdio data source, and
185 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
186 * See libjpeg.doc for more info.
189 if(cinfo
.jpeg_color_space
==JCS_GRAYSCALE
) jgray
=1; else jgray
=0;
190 pc
->w
=cinfo
.image_width
;
191 pc
->h
=cinfo
.image_height
;
192 pc
->pic
=(byte
*) im_alloc_true(pc
->w
,pc
->h
);
194 mybuf
=(char*) pc
->pic
;
195 line_w
= im_get_linew_true(pc
->w
);
197 sprintf(pc
->fullInfo
,"%d x %d JPEG image (Grayscale format)",pc
->w
,pc
->h
);
199 sprintf(pc
->fullInfo
,"%d x %d JPEG image (RGB format)",pc
->w
,pc
->h
);
201 /* Step 4: set parameters for decompression */
203 /* In this example, we don't need to change any of the defaults set by
204 * jpeg_read_header(), so we do nothing here.
207 /* Step 5: Start decompressor */
209 (void) jpeg_start_decompress(&cinfo
);
210 /* We can ignore the return value since suspension is not possible
211 * with the stdio data source.
214 /* We may need to do some setup of our own at this point before reading
215 * the data. After jpeg_start_decompress() we have the correct scaled
216 * output image dimensions available, as well as the output colormap
217 * if we asked for color quantization.
218 * In this example, we need to make an output work buffer of the right size.
220 /* JSAMPLEs per row in output buffer */
221 row_stride
= cinfo
.output_width
* cinfo
.output_components
;
222 /* Make a one-row-high sample array that will go away when done with image */
223 buffer
= (*cinfo
.mem
->alloc_sarray
)
224 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, row_stride
, 1);
226 /* Step 6: while (scan lines remain to be read) */
227 /* jpeg_read_scanlines(...); */
229 /* Here we use the library's state variable cinfo.output_scanline as the
230 * loop counter, so that we don't have to keep track ourselves.
232 while (cinfo
.output_scanline
< cinfo
.output_height
) {
233 /* jpeg_read_scanlines expects an array of pointers to scanlines.
234 * Here the array is only one element long, but you could ask for
235 * more than one scanline at a time if that's more convenient.
237 (void) jpeg_read_scanlines(&cinfo
, buffer
, 1);
238 /* Assume put_scanline_someplace wants a pointer and sample count. */
239 put_jpegdata((unsigned char*) buffer
[0], row_stride
);
242 /* Step 7: Finish decompression */
244 (void) jpeg_finish_decompress(&cinfo
);
245 /* We can ignore the return value since suspension is not possible
246 * with the stdio data source.
249 /* Step 8: Release JPEG decompression object */
251 /* This is an important step since it will release a good deal of memory. */
252 jpeg_destroy_decompress(&cinfo
);
254 /* After finish_decompress, we can close the input file.
255 * Here we postpone it until after no more JPEG errors are possible,
256 * so as to simplify the setjmp error logic above. (Actually, I don't
257 * think that jpeg_destroy can do an error exit, but why assume anything...)
261 /* At this point you may want to check to see whether any corrupt-data
262 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
265 /* And we're done! */
273 * In the above code, we ignored the return value of jpeg_read_scanlines,
274 * which is the number of scanlines actually read. We could get away with
275 * this because we asked for only one line at a time and we weren't using
276 * a suspending data source. See libjpeg.doc for more info.
278 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
279 * we should have done it beforehand to ensure that the space would be
280 * counted against the JPEG max_memory setting. In some systems the above
281 * code would risk an out-of-memory error. However, in general we don't
282 * know the output image dimensions before jpeg_start_decompress(), unless we
283 * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this.
285 * Scanlines are returned in the same order as they appear in the JPEG file,
286 * which is standardly top-to-bottom. If you must emit data bottom-to-top,
287 * you can use one of the virtual arrays provided by the JPEG memory manager
288 * to invert the data. See wrbmp.c for an example.
290 * As with compression, some operating modes may require temporary files.
291 * On some systems you may need to set up a signal handler to ensure that
292 * temporary files are deleted if the program is interrupted. See libjpeg.doc.
295 void main(int argc,char **argv)
298 LoadJFIF(argv[1],&ppp);