xref: /plan9/sys/src/cmd/gs/jpeg/example.c (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1*7dd7cddfSDavid du Colombier /*
2*7dd7cddfSDavid du Colombier  * example.c
3*7dd7cddfSDavid du Colombier  *
4*7dd7cddfSDavid du Colombier  * This file illustrates how to use the IJG code as a subroutine library
5*7dd7cddfSDavid du Colombier  * to read or write JPEG image files.  You should look at this code in
6*7dd7cddfSDavid du Colombier  * conjunction with the documentation file libjpeg.doc.
7*7dd7cddfSDavid du Colombier  *
8*7dd7cddfSDavid du Colombier  * This code will not do anything useful as-is, but it may be helpful as a
9*7dd7cddfSDavid du Colombier  * skeleton for constructing routines that call the JPEG library.
10*7dd7cddfSDavid du Colombier  *
11*7dd7cddfSDavid du Colombier  * We present these routines in the same coding style used in the JPEG code
12*7dd7cddfSDavid du Colombier  * (ANSI function definitions, etc); but you are of course free to code your
13*7dd7cddfSDavid du Colombier  * routines in a different style if you prefer.
14*7dd7cddfSDavid du Colombier  */
15*7dd7cddfSDavid du Colombier 
16*7dd7cddfSDavid du Colombier #include <stdio.h>
17*7dd7cddfSDavid du Colombier 
18*7dd7cddfSDavid du Colombier /*
19*7dd7cddfSDavid du Colombier  * Include file for users of JPEG library.
20*7dd7cddfSDavid du Colombier  * You will need to have included system headers that define at least
21*7dd7cddfSDavid du Colombier  * the typedefs FILE and size_t before you can include jpeglib.h.
22*7dd7cddfSDavid du Colombier  * (stdio.h is sufficient on ANSI-conforming systems.)
23*7dd7cddfSDavid du Colombier  * You may also wish to include "jerror.h".
24*7dd7cddfSDavid du Colombier  */
25*7dd7cddfSDavid du Colombier 
26*7dd7cddfSDavid du Colombier #include "jpeglib.h"
27*7dd7cddfSDavid du Colombier 
28*7dd7cddfSDavid du Colombier /*
29*7dd7cddfSDavid du Colombier  * <setjmp.h> is used for the optional error recovery mechanism shown in
30*7dd7cddfSDavid du Colombier  * the second part of the example.
31*7dd7cddfSDavid du Colombier  */
32*7dd7cddfSDavid du Colombier 
33*7dd7cddfSDavid du Colombier #include <setjmp.h>
34*7dd7cddfSDavid du Colombier 
35*7dd7cddfSDavid du Colombier 
36*7dd7cddfSDavid du Colombier 
37*7dd7cddfSDavid du Colombier /******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
38*7dd7cddfSDavid du Colombier 
39*7dd7cddfSDavid du Colombier /* This half of the example shows how to feed data into the JPEG compressor.
40*7dd7cddfSDavid du Colombier  * We present a minimal version that does not worry about refinements such
41*7dd7cddfSDavid du Colombier  * as error recovery (the JPEG code will just exit() if it gets an error).
42*7dd7cddfSDavid du Colombier  */
43*7dd7cddfSDavid du Colombier 
44*7dd7cddfSDavid du Colombier 
45*7dd7cddfSDavid du Colombier /*
46*7dd7cddfSDavid du Colombier  * IMAGE DATA FORMATS:
47*7dd7cddfSDavid du Colombier  *
48*7dd7cddfSDavid du Colombier  * The standard input image format is a rectangular array of pixels, with
49*7dd7cddfSDavid du Colombier  * each pixel having the same number of "component" values (color channels).
50*7dd7cddfSDavid du Colombier  * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars).
51*7dd7cddfSDavid du Colombier  * If you are working with color data, then the color values for each pixel
52*7dd7cddfSDavid du Colombier  * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
53*7dd7cddfSDavid du Colombier  * RGB color.
54*7dd7cddfSDavid du Colombier  *
55*7dd7cddfSDavid du Colombier  * For this example, we'll assume that this data structure matches the way
56*7dd7cddfSDavid du Colombier  * our application has stored the image in memory, so we can just pass a
57*7dd7cddfSDavid du Colombier  * pointer to our image buffer.  In particular, let's say that the image is
58*7dd7cddfSDavid du Colombier  * RGB color and is described by:
59*7dd7cddfSDavid du Colombier  */
60*7dd7cddfSDavid du Colombier 
61*7dd7cddfSDavid du Colombier extern JSAMPLE * image_buffer;	/* Points to large array of R,G,B-order data */
62*7dd7cddfSDavid du Colombier extern int image_height;	/* Number of rows in image */
63*7dd7cddfSDavid du Colombier extern int image_width;		/* Number of columns in image */
64*7dd7cddfSDavid du Colombier 
65*7dd7cddfSDavid du Colombier 
66*7dd7cddfSDavid du Colombier /*
67*7dd7cddfSDavid du Colombier  * Sample routine for JPEG compression.  We assume that the target file name
68*7dd7cddfSDavid du Colombier  * and a compression quality factor are passed in.
69*7dd7cddfSDavid du Colombier  */
70*7dd7cddfSDavid du Colombier 
71*7dd7cddfSDavid du Colombier GLOBAL(void)
write_JPEG_file(char * filename,int quality)72*7dd7cddfSDavid du Colombier write_JPEG_file (char * filename, int quality)
73*7dd7cddfSDavid du Colombier {
74*7dd7cddfSDavid du Colombier   /* This struct contains the JPEG compression parameters and pointers to
75*7dd7cddfSDavid du Colombier    * working space (which is allocated as needed by the JPEG library).
76*7dd7cddfSDavid du Colombier    * It is possible to have several such structures, representing multiple
77*7dd7cddfSDavid du Colombier    * compression/decompression processes, in existence at once.  We refer
78*7dd7cddfSDavid du Colombier    * to any one struct (and its associated working data) as a "JPEG object".
79*7dd7cddfSDavid du Colombier    */
80*7dd7cddfSDavid du Colombier   struct jpeg_compress_struct cinfo;
81*7dd7cddfSDavid du Colombier   /* This struct represents a JPEG error handler.  It is declared separately
82*7dd7cddfSDavid du Colombier    * because applications often want to supply a specialized error handler
83*7dd7cddfSDavid du Colombier    * (see the second half of this file for an example).  But here we just
84*7dd7cddfSDavid du Colombier    * take the easy way out and use the standard error handler, which will
85*7dd7cddfSDavid du Colombier    * print a message on stderr and call exit() if compression fails.
86*7dd7cddfSDavid du Colombier    * Note that this struct must live as long as the main JPEG parameter
87*7dd7cddfSDavid du Colombier    * struct, to avoid dangling-pointer problems.
88*7dd7cddfSDavid du Colombier    */
89*7dd7cddfSDavid du Colombier   struct jpeg_error_mgr jerr;
90*7dd7cddfSDavid du Colombier   /* More stuff */
91*7dd7cddfSDavid du Colombier   FILE * outfile;		/* target file */
92*7dd7cddfSDavid du Colombier   JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
93*7dd7cddfSDavid du Colombier   int row_stride;		/* physical row width in image buffer */
94*7dd7cddfSDavid du Colombier 
95*7dd7cddfSDavid du Colombier   /* Step 1: allocate and initialize JPEG compression object */
96*7dd7cddfSDavid du Colombier 
97*7dd7cddfSDavid du Colombier   /* We have to set up the error handler first, in case the initialization
98*7dd7cddfSDavid du Colombier    * step fails.  (Unlikely, but it could happen if you are out of memory.)
99*7dd7cddfSDavid du Colombier    * This routine fills in the contents of struct jerr, and returns jerr's
100*7dd7cddfSDavid du Colombier    * address which we place into the link field in cinfo.
101*7dd7cddfSDavid du Colombier    */
102*7dd7cddfSDavid du Colombier   cinfo.err = jpeg_std_error(&jerr);
103*7dd7cddfSDavid du Colombier   /* Now we can initialize the JPEG compression object. */
104*7dd7cddfSDavid du Colombier   jpeg_create_compress(&cinfo);
105*7dd7cddfSDavid du Colombier 
106*7dd7cddfSDavid du Colombier   /* Step 2: specify data destination (eg, a file) */
107*7dd7cddfSDavid du Colombier   /* Note: steps 2 and 3 can be done in either order. */
108*7dd7cddfSDavid du Colombier 
109*7dd7cddfSDavid du Colombier   /* Here we use the library-supplied code to send compressed data to a
110*7dd7cddfSDavid du Colombier    * stdio stream.  You can also write your own code to do something else.
111*7dd7cddfSDavid du Colombier    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
112*7dd7cddfSDavid du Colombier    * requires it in order to write binary files.
113*7dd7cddfSDavid du Colombier    */
114*7dd7cddfSDavid du Colombier   if ((outfile = fopen(filename, "wb")) == NULL) {
115*7dd7cddfSDavid du Colombier     fprintf(stderr, "can't open %s\n", filename);
116*7dd7cddfSDavid du Colombier     exit(1);
117*7dd7cddfSDavid du Colombier   }
118*7dd7cddfSDavid du Colombier   jpeg_stdio_dest(&cinfo, outfile);
119*7dd7cddfSDavid du Colombier 
120*7dd7cddfSDavid du Colombier   /* Step 3: set parameters for compression */
121*7dd7cddfSDavid du Colombier 
122*7dd7cddfSDavid du Colombier   /* First we supply a description of the input image.
123*7dd7cddfSDavid du Colombier    * Four fields of the cinfo struct must be filled in:
124*7dd7cddfSDavid du Colombier    */
125*7dd7cddfSDavid du Colombier   cinfo.image_width = image_width; 	/* image width and height, in pixels */
126*7dd7cddfSDavid du Colombier   cinfo.image_height = image_height;
127*7dd7cddfSDavid du Colombier   cinfo.input_components = 3;		/* # of color components per pixel */
128*7dd7cddfSDavid du Colombier   cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
129*7dd7cddfSDavid du Colombier   /* Now use the library's routine to set default compression parameters.
130*7dd7cddfSDavid du Colombier    * (You must set at least cinfo.in_color_space before calling this,
131*7dd7cddfSDavid du Colombier    * since the defaults depend on the source color space.)
132*7dd7cddfSDavid du Colombier    */
133*7dd7cddfSDavid du Colombier   jpeg_set_defaults(&cinfo);
134*7dd7cddfSDavid du Colombier   /* Now you can set any non-default parameters you wish to.
135*7dd7cddfSDavid du Colombier    * Here we just illustrate the use of quality (quantization table) scaling:
136*7dd7cddfSDavid du Colombier    */
137*7dd7cddfSDavid du Colombier   jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
138*7dd7cddfSDavid du Colombier 
139*7dd7cddfSDavid du Colombier   /* Step 4: Start compressor */
140*7dd7cddfSDavid du Colombier 
141*7dd7cddfSDavid du Colombier   /* TRUE ensures that we will write a complete interchange-JPEG file.
142*7dd7cddfSDavid du Colombier    * Pass TRUE unless you are very sure of what you're doing.
143*7dd7cddfSDavid du Colombier    */
144*7dd7cddfSDavid du Colombier   jpeg_start_compress(&cinfo, TRUE);
145*7dd7cddfSDavid du Colombier 
146*7dd7cddfSDavid du Colombier   /* Step 5: while (scan lines remain to be written) */
147*7dd7cddfSDavid du Colombier   /*           jpeg_write_scanlines(...); */
148*7dd7cddfSDavid du Colombier 
149*7dd7cddfSDavid du Colombier   /* Here we use the library's state variable cinfo.next_scanline as the
150*7dd7cddfSDavid du Colombier    * loop counter, so that we don't have to keep track ourselves.
151*7dd7cddfSDavid du Colombier    * To keep things simple, we pass one scanline per call; you can pass
152*7dd7cddfSDavid du Colombier    * more if you wish, though.
153*7dd7cddfSDavid du Colombier    */
154*7dd7cddfSDavid du Colombier   row_stride = image_width * 3;	/* JSAMPLEs per row in image_buffer */
155*7dd7cddfSDavid du Colombier 
156*7dd7cddfSDavid du Colombier   while (cinfo.next_scanline < cinfo.image_height) {
157*7dd7cddfSDavid du Colombier     /* jpeg_write_scanlines expects an array of pointers to scanlines.
158*7dd7cddfSDavid du Colombier      * Here the array is only one element long, but you could pass
159*7dd7cddfSDavid du Colombier      * more than one scanline at a time if that's more convenient.
160*7dd7cddfSDavid du Colombier      */
161*7dd7cddfSDavid du Colombier     row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
162*7dd7cddfSDavid du Colombier     (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
163*7dd7cddfSDavid du Colombier   }
164*7dd7cddfSDavid du Colombier 
165*7dd7cddfSDavid du Colombier   /* Step 6: Finish compression */
166*7dd7cddfSDavid du Colombier 
167*7dd7cddfSDavid du Colombier   jpeg_finish_compress(&cinfo);
168*7dd7cddfSDavid du Colombier   /* After finish_compress, we can close the output file. */
169*7dd7cddfSDavid du Colombier   fclose(outfile);
170*7dd7cddfSDavid du Colombier 
171*7dd7cddfSDavid du Colombier   /* Step 7: release JPEG compression object */
172*7dd7cddfSDavid du Colombier 
173*7dd7cddfSDavid du Colombier   /* This is an important step since it will release a good deal of memory. */
174*7dd7cddfSDavid du Colombier   jpeg_destroy_compress(&cinfo);
175*7dd7cddfSDavid du Colombier 
176*7dd7cddfSDavid du Colombier   /* And we're done! */
177*7dd7cddfSDavid du Colombier }
178*7dd7cddfSDavid du Colombier 
179*7dd7cddfSDavid du Colombier 
180*7dd7cddfSDavid du Colombier /*
181*7dd7cddfSDavid du Colombier  * SOME FINE POINTS:
182*7dd7cddfSDavid du Colombier  *
183*7dd7cddfSDavid du Colombier  * In the above loop, we ignored the return value of jpeg_write_scanlines,
184*7dd7cddfSDavid du Colombier  * which is the number of scanlines actually written.  We could get away
185*7dd7cddfSDavid du Colombier  * with this because we were only relying on the value of cinfo.next_scanline,
186*7dd7cddfSDavid du Colombier  * which will be incremented correctly.  If you maintain additional loop
187*7dd7cddfSDavid du Colombier  * variables then you should be careful to increment them properly.
188*7dd7cddfSDavid du Colombier  * Actually, for output to a stdio stream you needn't worry, because
189*7dd7cddfSDavid du Colombier  * then jpeg_write_scanlines will write all the lines passed (or else exit
190*7dd7cddfSDavid du Colombier  * with a fatal error).  Partial writes can only occur if you use a data
191*7dd7cddfSDavid du Colombier  * destination module that can demand suspension of the compressor.
192*7dd7cddfSDavid du Colombier  * (If you don't know what that's for, you don't need it.)
193*7dd7cddfSDavid du Colombier  *
194*7dd7cddfSDavid du Colombier  * If the compressor requires full-image buffers (for entropy-coding
195*7dd7cddfSDavid du Colombier  * optimization or a multi-scan JPEG file), it will create temporary
196*7dd7cddfSDavid du Colombier  * files for anything that doesn't fit within the maximum-memory setting.
197*7dd7cddfSDavid du Colombier  * (Note that temp files are NOT needed if you use the default parameters.)
198*7dd7cddfSDavid du Colombier  * On some systems you may need to set up a signal handler to ensure that
199*7dd7cddfSDavid du Colombier  * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
200*7dd7cddfSDavid du Colombier  *
201*7dd7cddfSDavid du Colombier  * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG
202*7dd7cddfSDavid du Colombier  * files to be compatible with everyone else's.  If you cannot readily read
203*7dd7cddfSDavid du Colombier  * your data in that order, you'll need an intermediate array to hold the
204*7dd7cddfSDavid du Colombier  * image.  See rdtarga.c or rdbmp.c for examples of handling bottom-to-top
205*7dd7cddfSDavid du Colombier  * source data using the JPEG code's internal virtual-array mechanisms.
206*7dd7cddfSDavid du Colombier  */
207*7dd7cddfSDavid du Colombier 
208*7dd7cddfSDavid du Colombier 
209*7dd7cddfSDavid du Colombier 
210*7dd7cddfSDavid du Colombier /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
211*7dd7cddfSDavid du Colombier 
212*7dd7cddfSDavid du Colombier /* This half of the example shows how to read data from the JPEG decompressor.
213*7dd7cddfSDavid du Colombier  * It's a bit more refined than the above, in that we show:
214*7dd7cddfSDavid du Colombier  *   (a) how to modify the JPEG library's standard error-reporting behavior;
215*7dd7cddfSDavid du Colombier  *   (b) how to allocate workspace using the library's memory manager.
216*7dd7cddfSDavid du Colombier  *
217*7dd7cddfSDavid du Colombier  * Just to make this example a little different from the first one, we'll
218*7dd7cddfSDavid du Colombier  * assume that we do not intend to put the whole image into an in-memory
219*7dd7cddfSDavid du Colombier  * buffer, but to send it line-by-line someplace else.  We need a one-
220*7dd7cddfSDavid du Colombier  * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
221*7dd7cddfSDavid du Colombier  * memory manager allocate it for us.  This approach is actually quite useful
222*7dd7cddfSDavid du Colombier  * because we don't need to remember to deallocate the buffer separately: it
223*7dd7cddfSDavid du Colombier  * will go away automatically when the JPEG object is cleaned up.
224*7dd7cddfSDavid du Colombier  */
225*7dd7cddfSDavid du Colombier 
226*7dd7cddfSDavid du Colombier 
227*7dd7cddfSDavid du Colombier /*
228*7dd7cddfSDavid du Colombier  * ERROR HANDLING:
229*7dd7cddfSDavid du Colombier  *
230*7dd7cddfSDavid du Colombier  * The JPEG library's standard error handler (jerror.c) is divided into
231*7dd7cddfSDavid du Colombier  * several "methods" which you can override individually.  This lets you
232*7dd7cddfSDavid du Colombier  * adjust the behavior without duplicating a lot of code, which you might
233*7dd7cddfSDavid du Colombier  * have to update with each future release.
234*7dd7cddfSDavid du Colombier  *
235*7dd7cddfSDavid du Colombier  * Our example here shows how to override the "error_exit" method so that
236*7dd7cddfSDavid du Colombier  * control is returned to the library's caller when a fatal error occurs,
237*7dd7cddfSDavid du Colombier  * rather than calling exit() as the standard error_exit method does.
238*7dd7cddfSDavid du Colombier  *
239*7dd7cddfSDavid du Colombier  * We use C's setjmp/longjmp facility to return control.  This means that the
240*7dd7cddfSDavid du Colombier  * routine which calls the JPEG library must first execute a setjmp() call to
241*7dd7cddfSDavid du Colombier  * establish the return point.  We want the replacement error_exit to do a
242*7dd7cddfSDavid du Colombier  * longjmp().  But we need to make the setjmp buffer accessible to the
243*7dd7cddfSDavid du Colombier  * error_exit routine.  To do this, we make a private extension of the
244*7dd7cddfSDavid du Colombier  * standard JPEG error handler object.  (If we were using C++, we'd say we
245*7dd7cddfSDavid du Colombier  * were making a subclass of the regular error handler.)
246*7dd7cddfSDavid du Colombier  *
247*7dd7cddfSDavid du Colombier  * Here's the extended error handler struct:
248*7dd7cddfSDavid du Colombier  */
249*7dd7cddfSDavid du Colombier 
250*7dd7cddfSDavid du Colombier struct my_error_mgr {
251*7dd7cddfSDavid du Colombier   struct jpeg_error_mgr pub;	/* "public" fields */
252*7dd7cddfSDavid du Colombier 
253*7dd7cddfSDavid du Colombier   jmp_buf setjmp_buffer;	/* for return to caller */
254*7dd7cddfSDavid du Colombier };
255*7dd7cddfSDavid du Colombier 
256*7dd7cddfSDavid du Colombier typedef struct my_error_mgr * my_error_ptr;
257*7dd7cddfSDavid du Colombier 
258*7dd7cddfSDavid du Colombier /*
259*7dd7cddfSDavid du Colombier  * Here's the routine that will replace the standard error_exit method:
260*7dd7cddfSDavid du Colombier  */
261*7dd7cddfSDavid du Colombier 
262*7dd7cddfSDavid du Colombier METHODDEF(void)
my_error_exit(j_common_ptr cinfo)263*7dd7cddfSDavid du Colombier my_error_exit (j_common_ptr cinfo)
264*7dd7cddfSDavid du Colombier {
265*7dd7cddfSDavid du Colombier   /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
266*7dd7cddfSDavid du Colombier   my_error_ptr myerr = (my_error_ptr) cinfo->err;
267*7dd7cddfSDavid du Colombier 
268*7dd7cddfSDavid du Colombier   /* Always display the message. */
269*7dd7cddfSDavid du Colombier   /* We could postpone this until after returning, if we chose. */
270*7dd7cddfSDavid du Colombier   (*cinfo->err->output_message) (cinfo);
271*7dd7cddfSDavid du Colombier 
272*7dd7cddfSDavid du Colombier   /* Return control to the setjmp point */
273*7dd7cddfSDavid du Colombier   longjmp(myerr->setjmp_buffer, 1);
274*7dd7cddfSDavid du Colombier }
275*7dd7cddfSDavid du Colombier 
276*7dd7cddfSDavid du Colombier 
277*7dd7cddfSDavid du Colombier /*
278*7dd7cddfSDavid du Colombier  * Sample routine for JPEG decompression.  We assume that the source file name
279*7dd7cddfSDavid du Colombier  * is passed in.  We want to return 1 on success, 0 on error.
280*7dd7cddfSDavid du Colombier  */
281*7dd7cddfSDavid du Colombier 
282*7dd7cddfSDavid du Colombier 
283*7dd7cddfSDavid du Colombier GLOBAL(int)
read_JPEG_file(char * filename)284*7dd7cddfSDavid du Colombier read_JPEG_file (char * filename)
285*7dd7cddfSDavid du Colombier {
286*7dd7cddfSDavid du Colombier   /* This struct contains the JPEG decompression parameters and pointers to
287*7dd7cddfSDavid du Colombier    * working space (which is allocated as needed by the JPEG library).
288*7dd7cddfSDavid du Colombier    */
289*7dd7cddfSDavid du Colombier   struct jpeg_decompress_struct cinfo;
290*7dd7cddfSDavid du Colombier   /* We use our private extension JPEG error handler.
291*7dd7cddfSDavid du Colombier    * Note that this struct must live as long as the main JPEG parameter
292*7dd7cddfSDavid du Colombier    * struct, to avoid dangling-pointer problems.
293*7dd7cddfSDavid du Colombier    */
294*7dd7cddfSDavid du Colombier   struct my_error_mgr jerr;
295*7dd7cddfSDavid du Colombier   /* More stuff */
296*7dd7cddfSDavid du Colombier   FILE * infile;		/* source file */
297*7dd7cddfSDavid du Colombier   JSAMPARRAY buffer;		/* Output row buffer */
298*7dd7cddfSDavid du Colombier   int row_stride;		/* physical row width in output buffer */
299*7dd7cddfSDavid du Colombier 
300*7dd7cddfSDavid du Colombier   /* In this example we want to open the input file before doing anything else,
301*7dd7cddfSDavid du Colombier    * so that the setjmp() error recovery below can assume the file is open.
302*7dd7cddfSDavid du Colombier    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
303*7dd7cddfSDavid du Colombier    * requires it in order to read binary files.
304*7dd7cddfSDavid du Colombier    */
305*7dd7cddfSDavid du Colombier 
306*7dd7cddfSDavid du Colombier   if ((infile = fopen(filename, "rb")) == NULL) {
307*7dd7cddfSDavid du Colombier     fprintf(stderr, "can't open %s\n", filename);
308*7dd7cddfSDavid du Colombier     return 0;
309*7dd7cddfSDavid du Colombier   }
310*7dd7cddfSDavid du Colombier 
311*7dd7cddfSDavid du Colombier   /* Step 1: allocate and initialize JPEG decompression object */
312*7dd7cddfSDavid du Colombier 
313*7dd7cddfSDavid du Colombier   /* We set up the normal JPEG error routines, then override error_exit. */
314*7dd7cddfSDavid du Colombier   cinfo.err = jpeg_std_error(&jerr.pub);
315*7dd7cddfSDavid du Colombier   jerr.pub.error_exit = my_error_exit;
316*7dd7cddfSDavid du Colombier   /* Establish the setjmp return context for my_error_exit to use. */
317*7dd7cddfSDavid du Colombier   if (setjmp(jerr.setjmp_buffer)) {
318*7dd7cddfSDavid du Colombier     /* If we get here, the JPEG code has signaled an error.
319*7dd7cddfSDavid du Colombier      * We need to clean up the JPEG object, close the input file, and return.
320*7dd7cddfSDavid du Colombier      */
321*7dd7cddfSDavid du Colombier     jpeg_destroy_decompress(&cinfo);
322*7dd7cddfSDavid du Colombier     fclose(infile);
323*7dd7cddfSDavid du Colombier     return 0;
324*7dd7cddfSDavid du Colombier   }
325*7dd7cddfSDavid du Colombier   /* Now we can initialize the JPEG decompression object. */
326*7dd7cddfSDavid du Colombier   jpeg_create_decompress(&cinfo);
327*7dd7cddfSDavid du Colombier 
328*7dd7cddfSDavid du Colombier   /* Step 2: specify data source (eg, a file) */
329*7dd7cddfSDavid du Colombier 
330*7dd7cddfSDavid du Colombier   jpeg_stdio_src(&cinfo, infile);
331*7dd7cddfSDavid du Colombier 
332*7dd7cddfSDavid du Colombier   /* Step 3: read file parameters with jpeg_read_header() */
333*7dd7cddfSDavid du Colombier 
334*7dd7cddfSDavid du Colombier   (void) jpeg_read_header(&cinfo, TRUE);
335*7dd7cddfSDavid du Colombier   /* We can ignore the return value from jpeg_read_header since
336*7dd7cddfSDavid du Colombier    *   (a) suspension is not possible with the stdio data source, and
337*7dd7cddfSDavid du Colombier    *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
338*7dd7cddfSDavid du Colombier    * See libjpeg.doc for more info.
339*7dd7cddfSDavid du Colombier    */
340*7dd7cddfSDavid du Colombier 
341*7dd7cddfSDavid du Colombier   /* Step 4: set parameters for decompression */
342*7dd7cddfSDavid du Colombier 
343*7dd7cddfSDavid du Colombier   /* In this example, we don't need to change any of the defaults set by
344*7dd7cddfSDavid du Colombier    * jpeg_read_header(), so we do nothing here.
345*7dd7cddfSDavid du Colombier    */
346*7dd7cddfSDavid du Colombier 
347*7dd7cddfSDavid du Colombier   /* Step 5: Start decompressor */
348*7dd7cddfSDavid du Colombier 
349*7dd7cddfSDavid du Colombier   (void) jpeg_start_decompress(&cinfo);
350*7dd7cddfSDavid du Colombier   /* We can ignore the return value since suspension is not possible
351*7dd7cddfSDavid du Colombier    * with the stdio data source.
352*7dd7cddfSDavid du Colombier    */
353*7dd7cddfSDavid du Colombier 
354*7dd7cddfSDavid du Colombier   /* We may need to do some setup of our own at this point before reading
355*7dd7cddfSDavid du Colombier    * the data.  After jpeg_start_decompress() we have the correct scaled
356*7dd7cddfSDavid du Colombier    * output image dimensions available, as well as the output colormap
357*7dd7cddfSDavid du Colombier    * if we asked for color quantization.
358*7dd7cddfSDavid du Colombier    * In this example, we need to make an output work buffer of the right size.
359*7dd7cddfSDavid du Colombier    */
360*7dd7cddfSDavid du Colombier   /* JSAMPLEs per row in output buffer */
361*7dd7cddfSDavid du Colombier   row_stride = cinfo.output_width * cinfo.output_components;
362*7dd7cddfSDavid du Colombier   /* Make a one-row-high sample array that will go away when done with image */
363*7dd7cddfSDavid du Colombier   buffer = (*cinfo.mem->alloc_sarray)
364*7dd7cddfSDavid du Colombier 		((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
365*7dd7cddfSDavid du Colombier 
366*7dd7cddfSDavid du Colombier   /* Step 6: while (scan lines remain to be read) */
367*7dd7cddfSDavid du Colombier   /*           jpeg_read_scanlines(...); */
368*7dd7cddfSDavid du Colombier 
369*7dd7cddfSDavid du Colombier   /* Here we use the library's state variable cinfo.output_scanline as the
370*7dd7cddfSDavid du Colombier    * loop counter, so that we don't have to keep track ourselves.
371*7dd7cddfSDavid du Colombier    */
372*7dd7cddfSDavid du Colombier   while (cinfo.output_scanline < cinfo.output_height) {
373*7dd7cddfSDavid du Colombier     /* jpeg_read_scanlines expects an array of pointers to scanlines.
374*7dd7cddfSDavid du Colombier      * Here the array is only one element long, but you could ask for
375*7dd7cddfSDavid du Colombier      * more than one scanline at a time if that's more convenient.
376*7dd7cddfSDavid du Colombier      */
377*7dd7cddfSDavid du Colombier     (void) jpeg_read_scanlines(&cinfo, buffer, 1);
378*7dd7cddfSDavid du Colombier     /* Assume put_scanline_someplace wants a pointer and sample count. */
379*7dd7cddfSDavid du Colombier     put_scanline_someplace(buffer[0], row_stride);
380*7dd7cddfSDavid du Colombier   }
381*7dd7cddfSDavid du Colombier 
382*7dd7cddfSDavid du Colombier   /* Step 7: Finish decompression */
383*7dd7cddfSDavid du Colombier 
384*7dd7cddfSDavid du Colombier   (void) jpeg_finish_decompress(&cinfo);
385*7dd7cddfSDavid du Colombier   /* We can ignore the return value since suspension is not possible
386*7dd7cddfSDavid du Colombier    * with the stdio data source.
387*7dd7cddfSDavid du Colombier    */
388*7dd7cddfSDavid du Colombier 
389*7dd7cddfSDavid du Colombier   /* Step 8: Release JPEG decompression object */
390*7dd7cddfSDavid du Colombier 
391*7dd7cddfSDavid du Colombier   /* This is an important step since it will release a good deal of memory. */
392*7dd7cddfSDavid du Colombier   jpeg_destroy_decompress(&cinfo);
393*7dd7cddfSDavid du Colombier 
394*7dd7cddfSDavid du Colombier   /* After finish_decompress, we can close the input file.
395*7dd7cddfSDavid du Colombier    * Here we postpone it until after no more JPEG errors are possible,
396*7dd7cddfSDavid du Colombier    * so as to simplify the setjmp error logic above.  (Actually, I don't
397*7dd7cddfSDavid du Colombier    * think that jpeg_destroy can do an error exit, but why assume anything...)
398*7dd7cddfSDavid du Colombier    */
399*7dd7cddfSDavid du Colombier   fclose(infile);
400*7dd7cddfSDavid du Colombier 
401*7dd7cddfSDavid du Colombier   /* At this point you may want to check to see whether any corrupt-data
402*7dd7cddfSDavid du Colombier    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
403*7dd7cddfSDavid du Colombier    */
404*7dd7cddfSDavid du Colombier 
405*7dd7cddfSDavid du Colombier   /* And we're done! */
406*7dd7cddfSDavid du Colombier   return 1;
407*7dd7cddfSDavid du Colombier }
408*7dd7cddfSDavid du Colombier 
409*7dd7cddfSDavid du Colombier 
410*7dd7cddfSDavid du Colombier /*
411*7dd7cddfSDavid du Colombier  * SOME FINE POINTS:
412*7dd7cddfSDavid du Colombier  *
413*7dd7cddfSDavid du Colombier  * In the above code, we ignored the return value of jpeg_read_scanlines,
414*7dd7cddfSDavid du Colombier  * which is the number of scanlines actually read.  We could get away with
415*7dd7cddfSDavid du Colombier  * this because we asked for only one line at a time and we weren't using
416*7dd7cddfSDavid du Colombier  * a suspending data source.  See libjpeg.doc for more info.
417*7dd7cddfSDavid du Colombier  *
418*7dd7cddfSDavid du Colombier  * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
419*7dd7cddfSDavid du Colombier  * we should have done it beforehand to ensure that the space would be
420*7dd7cddfSDavid du Colombier  * counted against the JPEG max_memory setting.  In some systems the above
421*7dd7cddfSDavid du Colombier  * code would risk an out-of-memory error.  However, in general we don't
422*7dd7cddfSDavid du Colombier  * know the output image dimensions before jpeg_start_decompress(), unless we
423*7dd7cddfSDavid du Colombier  * call jpeg_calc_output_dimensions().  See libjpeg.doc for more about this.
424*7dd7cddfSDavid du Colombier  *
425*7dd7cddfSDavid du Colombier  * Scanlines are returned in the same order as they appear in the JPEG file,
426*7dd7cddfSDavid du Colombier  * which is standardly top-to-bottom.  If you must emit data bottom-to-top,
427*7dd7cddfSDavid du Colombier  * you can use one of the virtual arrays provided by the JPEG memory manager
428*7dd7cddfSDavid du Colombier  * to invert the data.  See wrbmp.c for an example.
429*7dd7cddfSDavid du Colombier  *
430*7dd7cddfSDavid du Colombier  * As with compression, some operating modes may require temporary files.
431*7dd7cddfSDavid du Colombier  * On some systems you may need to set up a signal handler to ensure that
432*7dd7cddfSDavid du Colombier  * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
433*7dd7cddfSDavid du Colombier  */
434