blob: 577f061f49f31ec80ab186c30c104e50eac718cd [file] [log] [blame]
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001/*
2 * example.c
3 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00004 * 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.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00007 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00008 * 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.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000010 *
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.
14 */
15
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016#include <stdio.h>
17
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000018/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000019 * Include file for users of JPEG library.
20 * You will need to have included system headers that define at least
21 * the typedefs FILE and size_t before you can include jpeglib.h.
22 * (stdio.h is sufficient on ANSI-conforming systems.)
23 * You may also wish to include "jerror.h".
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000024 */
25
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000026#include "jpeglib.h"
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000027
28/*
29 * <setjmp.h> is used for the optional error recovery mechanism shown in
30 * the second part of the example.
31 */
32
33#include <setjmp.h>
34
35
36
37/******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
38
39/* This half of the example shows how to feed data into the JPEG compressor.
40 * We present a minimal version that does not worry about refinements such
41 * as error recovery (the JPEG code will just exit() if it gets an error).
42 */
43
44
45/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000046 * IMAGE DATA FORMATS:
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000047 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000048 * The standard input image format is a rectangular array of pixels, with
49 * each pixel having the same number of "component" values (color channels).
50 * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars).
51 * If you are working with color data, then the color values for each pixel
52 * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
53 * RGB color.
54 *
55 * For this example, we'll assume that this data structure matches the way
56 * our application has stored the image in memory, so we can just pass a
57 * pointer to our image buffer. In particular, let's say that the image is
58 * RGB color and is described by:
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000059 */
60
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000061extern JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
62extern int image_height; /* Number of rows in image */
63extern int image_width; /* Number of columns in image */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000064
65
66/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000067 * Sample routine for JPEG compression. We assume that the target file name
68 * and a compression quality factor are passed in.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000069 */
70
71GLOBAL void
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072write_JPEG_file (char * filename, int quality)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000073{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074 /* This struct contains the JPEG compression parameters and pointers to
75 * working space (which is allocated as needed by the JPEG library).
76 * It is possible to have several such structures, representing multiple
77 * compression/decompression processes, in existence at once. We refer
78 * to any one struct (and its associated working data) as a "JPEG object".
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000079 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080 struct jpeg_compress_struct cinfo;
81 /* This struct represents a JPEG error handler. It is declared separately
82 * because applications often want to supply a specialized error handler
83 * (see the second half of this file for an example). But here we just
84 * take the easy way out and use the standard error handler, which will
85 * print a message on stderr and call exit() if compression fails.
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000086 * Note that this struct must live as long as the main JPEG parameter
87 * struct, to avoid dangling-pointer problems.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000088 */
89 struct jpeg_error_mgr jerr;
90 /* More stuff */
91 FILE * outfile; /* target file */
92 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
93 int row_stride; /* physical row width in image buffer */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000094
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095 /* Step 1: allocate and initialize JPEG compression object */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000096
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000097 /* We have to set up the error handler first, in case the initialization
98 * step fails. (Unlikely, but it could happen if you are out of memory.)
99 * This routine fills in the contents of struct jerr, and returns jerr's
100 * address which we place into the link field in cinfo.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000101 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000102 cinfo.err = jpeg_std_error(&jerr);
103 /* Now we can initialize the JPEG compression object. */
104 jpeg_create_compress(&cinfo);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000105
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000106 /* Step 2: specify data destination (eg, a file) */
107 /* Note: steps 2 and 3 can be done in either order. */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000108
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000109 /* Here we use the library-supplied code to send compressed data to a
110 * stdio stream. You can also write your own code to do something else.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000111 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
112 * requires it in order to write binary files.
113 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000114 if ((outfile = fopen(filename, "wb")) == NULL) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000115 fprintf(stderr, "can't open %s\n", filename);
116 exit(1);
117 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000118 jpeg_stdio_dest(&cinfo, outfile);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000119
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120 /* Step 3: set parameters for compression */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000121
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000122 /* First we supply a description of the input image.
123 * Four fields of the cinfo struct must be filled in:
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000124 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000125 cinfo.image_width = image_width; /* image width and height, in pixels */
126 cinfo.image_height = image_height;
127 cinfo.input_components = 3; /* # of color components per pixel */
128 cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
129 /* Now use the library's routine to set default compression parameters.
130 * (You must set at least cinfo.in_color_space before calling this,
131 * since the defaults depend on the source color space.)
132 */
133 jpeg_set_defaults(&cinfo);
134 /* Now you can set any non-default parameters you wish to.
135 * Here we just illustrate the use of quality (quantization table) scaling:
136 */
137 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
138
139 /* Step 4: Start compressor */
140
141 /* TRUE ensures that we will write a complete interchange-JPEG file.
142 * Pass TRUE unless you are very sure of what you're doing.
143 */
144 jpeg_start_compress(&cinfo, TRUE);
145
146 /* Step 5: while (scan lines remain to be written) */
147 /* jpeg_write_scanlines(...); */
148
149 /* Here we use the library's state variable cinfo.next_scanline as the
150 * loop counter, so that we don't have to keep track ourselves.
151 * To keep things simple, we pass one scanline per call; you can pass
152 * more if you wish, though.
153 */
154 row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
155
156 while (cinfo.next_scanline < cinfo.image_height) {
157 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
158 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
159 }
160
161 /* Step 6: Finish compression */
162
163 jpeg_finish_compress(&cinfo);
164 /* After finish_compress, we can close the output file. */
165 fclose(outfile);
166
167 /* Step 7: release JPEG compression object */
168
169 /* This is an important step since it will release a good deal of memory. */
170 jpeg_destroy_compress(&cinfo);
171
172 /* And we're done! */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000173}
174
175
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000176/*
177 * SOME FINE POINTS:
178 *
179 * In the above loop, we ignored the return value of jpeg_write_scanlines,
180 * which is the number of scanlines actually written. We could get away
181 * with this because we were only relying on the value of cinfo.next_scanline,
182 * which will be incremented correctly. If you maintain additional loop
183 * variables then you should be careful to increment them properly.
184 * Actually, for output to a stdio stream you needn't worry, because
185 * then jpeg_write_scanlines will write all the lines passed (or else exit
186 * with a fatal error). Partial writes can only occur if you use a data
187 * destination module that can demand suspension of the compressor.
188 * (If you don't know what that's for, you don't need it.)
189 *
190 * If the compressor requires full-image buffers (for entropy-coding
191 * optimization or a noninterleaved JPEG file), it will create temporary
192 * files for anything that doesn't fit within the maximum-memory setting.
193 * (Note that temp files are NOT needed if you use the default parameters.)
194 * On some systems you may need to set up a signal handler to ensure that
195 * temporary files are deleted if the program is interrupted. See libjpeg.doc.
196 *
197 * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG
198 * files to be compatible with everyone else's. If you cannot readily read
199 * your data in that order, you'll need an intermediate array to hold the
200 * image. See rdtarga.c or rdbmp.c for examples of handling bottom-to-top
201 * source data using the JPEG code's internal virtual-array mechanisms.
202 */
203
204
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000205
206/******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
207
208/* This half of the example shows how to read data from the JPEG decompressor.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000209 * It's a bit more refined than the above, in that we show:
210 * (a) how to modify the JPEG library's standard error-reporting behavior;
211 * (b) how to allocate workspace using the library's memory manager.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000212 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000213 * Just to make this example a little different from the first one, we'll
214 * assume that we do not intend to put the whole image into an in-memory
215 * buffer, but to send it line-by-line someplace else. We need a one-
216 * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
217 * memory manager allocate it for us. This approach is actually quite useful
218 * because we don't need to remember to deallocate the buffer separately: it
219 * will go away automatically when the JPEG object is cleaned up.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000220 */
221
222
223/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000224 * ERROR HANDLING:
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000225 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 * The JPEG library's standard error handler (jerror.c) is divided into
227 * several "methods" which you can override individually. This lets you
228 * adjust the behavior without duplicating a lot of code, which you might
229 * have to update with each future release.
230 *
231 * Our example here shows how to override the "error_exit" method so that
232 * control is returned to the library's caller when a fatal error occurs,
233 * rather than calling exit() as the standard error_exit method does.
234 *
235 * We use C's setjmp/longjmp facility to return control. This means that the
236 * routine which calls the JPEG library must first execute a setjmp() call to
237 * establish the return point. We want the replacement error_exit to do a
238 * longjmp(). But we need to make the setjmp buffer accessible to the
239 * error_exit routine. To do this, we make a private extension of the
240 * standard JPEG error handler object. (If we were using C++, we'd say we
241 * were making a subclass of the regular error handler.)
242 *
243 * Here's the extended error handler struct:
244 */
245
246struct my_error_mgr {
247 struct jpeg_error_mgr pub; /* "public" fields */
248
249 jmp_buf setjmp_buffer; /* for return to caller */
250};
251
252typedef struct my_error_mgr * my_error_ptr;
253
254/*
255 * Here's the routine that will replace the standard error_exit method:
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000256 */
257
258METHODDEF void
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000259my_error_exit (j_common_ptr cinfo)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000260{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000261 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
262 my_error_ptr myerr = (my_error_ptr) cinfo->err;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000263
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000264 /* Always display the message. */
265 /* We could postpone this until after returning, if we chose. */
266 (*cinfo->err->output_message) (cinfo);
267
268 /* Return control to the setjmp point */
269 longjmp(myerr->setjmp_buffer, 1);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000270}
271
272
273/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000274 * Sample routine for JPEG decompression. We assume that the source file name
275 * is passed in. We want to return 1 on success, 0 on error.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000276 */
277
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000278
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000279GLOBAL int
280read_JPEG_file (char * filename)
281{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000282 /* This struct contains the JPEG decompression parameters and pointers to
283 * working space (which is allocated as needed by the JPEG library).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000284 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000285 struct jpeg_decompress_struct cinfo;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000286 /* We use our private extension JPEG error handler.
287 * Note that this struct must live as long as the main JPEG parameter
288 * struct, to avoid dangling-pointer problems.
289 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000290 struct my_error_mgr jerr;
291 /* More stuff */
292 FILE * infile; /* source file */
293 JSAMPARRAY buffer; /* Output row buffer */
294 int row_stride; /* physical row width in output buffer */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000295
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000296 /* In this example we want to open the input file before doing anything else,
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000297 * so that the setjmp() error recovery below can assume the file is open.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000298 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
299 * requires it in order to read binary files.
300 */
301
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000302 if ((infile = fopen(filename, "rb")) == NULL) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000303 fprintf(stderr, "can't open %s\n", filename);
304 return 0;
305 }
306
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000307 /* Step 1: allocate and initialize JPEG decompression object */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000308
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000309 /* We set up the normal JPEG error routines, then override error_exit. */
310 cinfo.err = jpeg_std_error(&jerr.pub);
311 jerr.pub.error_exit = my_error_exit;
312 /* Establish the setjmp return context for my_error_exit to use. */
313 if (setjmp(jerr.setjmp_buffer)) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000314 /* If we get here, the JPEG code has signaled an error.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315 * We need to clean up the JPEG object, close the input file, and return.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000316 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000317 jpeg_destroy_decompress(&cinfo);
318 fclose(infile);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000319 return 0;
320 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000321 /* Now we can initialize the JPEG decompression object. */
322 jpeg_create_decompress(&cinfo);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000323
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000324 /* Step 2: specify data source (eg, a file) */
325
326 jpeg_stdio_src(&cinfo, infile);
327
328 /* Step 3: read file parameters with jpeg_read_header() */
329
330 (void) jpeg_read_header(&cinfo, TRUE);
331 /* We can ignore the return value from jpeg_read_header since
332 * (a) suspension is not possible with the stdio data source, and
333 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
334 * See libjpeg.doc for more info.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000335 */
336
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000337 /* Step 4: set parameters for decompression */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000338
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000339 /* In this example, we don't need to change any of the defaults set by
340 * jpeg_read_header(), so we do nothing here.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000341 */
342
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000343 /* Step 5: Start decompressor */
344
345 jpeg_start_decompress(&cinfo);
346
347 /* We may need to do some setup of our own at this point before reading
348 * the data. After jpeg_start_decompress() we have the correct scaled
349 * output image dimensions available, as well as the output colormap
350 * if we asked for color quantization.
351 * In this example, we need to make an output work buffer of the right size.
352 */
353 /* JSAMPLEs per row in output buffer */
354 row_stride = cinfo.output_width * cinfo.output_components;
355 /* Make a one-row-high sample array that will go away when done with image */
356 buffer = (*cinfo.mem->alloc_sarray)
357 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
358
359 /* Step 6: while (scan lines remain to be read) */
360 /* jpeg_read_scanlines(...); */
361
362 /* Here we use the library's state variable cinfo.output_scanline as the
363 * loop counter, so that we don't have to keep track ourselves.
364 */
365 while (cinfo.output_scanline < cinfo.output_height) {
366 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
367 /* Assume put_scanline_someplace wants a pointer and sample count. */
368 put_scanline_someplace(buffer[0], row_stride);
369 }
370
371 /* Step 7: Finish decompression */
372
373 (void) jpeg_finish_decompress(&cinfo);
374 /* We can ignore the return value since suspension is not possible
375 * with the stdio data source.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000376 */
377
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000378 /* Step 8: Release JPEG decompression object */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000379
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000380 /* This is an important step since it will release a good deal of memory. */
381 jpeg_destroy_decompress(&cinfo);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000382
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000383 /* After finish_decompress, we can close the input file.
384 * Here we postpone it until after no more JPEG errors are possible,
385 * so as to simplify the setjmp error logic above. (Actually, I don't
386 * think that jpeg_destroy can do an error exit, but why assume anything...)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000387 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000388 fclose(infile);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000389
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000390 /* At this point you may want to check to see whether any corrupt-data
391 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000392 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000393
394 /* And we're done! */
395 return 1;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000396}
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000397
398
399/*
400 * SOME FINE POINTS:
401 *
402 * In the above code, we ignored the return value of jpeg_read_scanlines,
403 * which is the number of scanlines actually read. We could get away with
404 * this because we asked for only one line at a time and we weren't using
405 * a suspending data source. See libjpeg.doc for more info.
406 *
407 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
408 * we should have done it beforehand to ensure that the space would be
409 * counted against the JPEG max_memory setting. In some systems the above
410 * code would risk an out-of-memory error. However, in general we don't
411 * know the output image dimensions before jpeg_start_decompress(), unless we
412 * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this.
413 *
414 * Scanlines are returned in the same order as they appear in the JPEG file,
415 * which is standardly top-to-bottom. If you must emit data bottom-to-top,
416 * you can use one of the virtual arrays provided by the JPEG memory manager
417 * to invert the data. See wrbmp.c for an example.
418 *
419 * As with compression, some operating modes may require temporary files.
420 * On some systems you may need to set up a signal handler to ensure that
421 * temporary files are deleted if the program is interrupted. See libjpeg.doc.
422 */