blob: 83067d1e897d1943b4e80ab1528038bc1ae34fe1 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001USING THE IJG JPEG LIBRARY
2
3Copyright (C) 1994, Thomas G. Lane.
4This file is part of the Independent JPEG Group's software.
5For conditions of distribution and use, see the accompanying README file.
6
7
8This file describes how to use the IJG JPEG library within an application
9program. Read it if you want to write a program that uses the library.
10
11The file example.c provides heavily commented skeleton code for calling the
12JPEG library. Also see jpeglib.h (the include file to be used by application
13programs) for full details about data structures and function parameter lists.
14The library source code, of course, is the ultimate reference.
15
16Note that there have been *major* changes from the application interface
17presented by IJG version 4 and earlier versions. The old design had several
18inherent limitations, and it had accumulated a lot of cruft as we added
19features while trying to minimize application-interface changes. We have
20sacrificed backward compatibility in the version 5 rewrite, but we think the
21improvements justify this.
22
23
24TABLE OF CONTENTS
25-----------------
26
27Overview:
28 Functions provided by the library
29 Outline of typical usage
30Basic library usage:
31 Data formats
32 Compression details
33 Decompression details
34 Mechanics of usage: include files, linking, etc
35Advanced features:
36 Compression parameter selection
37 Decompression parameter selection
38 Special color spaces
39 Error handling
40 Compressed data handling (source and destination managers)
41 I/O suspension
42 Abbreviated datastreams and multiple images
43 Special markers
44 Raw (downsampled) image data
45 Progress monitoring
46 Memory management
47 Library compile-time options
48 Portability considerations
49 Notes for MS-DOS implementors
50
51You should read at least the overview and basic usage sections before trying
52to program with the library. The sections on advanced features can be read
53if and when you need them.
54
55
56OVERVIEW
57========
58
59Functions provided by the library
60---------------------------------
61
62The IJG JPEG library provides C code to read and write JPEG-compressed image
63files. The surrounding application program receives or supplies image data a
64scanline at a time, using a straightforward uncompressed image format. All
65details of color conversion and other preprocessing/postprocessing can be
66handled by the library.
67
68The library includes a substantial amount of code that is not covered by the
69JPEG standard but is necessary for typical applications of JPEG. These
70functions preprocess the image before JPEG compression or postprocess it after
71decompression. They include colorspace conversion, downsampling/upsampling,
72and color quantization. The application indirectly selects use of this code
73by specifying the format in which it wishes to supply or receive image data.
74For example, if colormapped output is requested, then the decompression
75library automatically invokes color quantization.
76
77A wide range of quality vs. speed tradeoffs are possible in JPEG processing,
78and even more so in decompression postprocessing. The decompression library
79provides multiple implementations that cover most of the useful tradeoffs,
80ranging from very-high-quality down to fast-preview operation. On the
81compression side we have generally not provided low-quality choices, since
82compression is normally less time-critical. It should be understood that the
83low-quality modes may not meet the JPEG standard's accuracy requirements;
84nonetheless, they are useful for viewers.
85
86A word about functions *not* provided by the library. We handle a subset of
87the ISO JPEG standard; most baseline and extended-sequential JPEG processes
88are supported. (Our subset includes all features now in common use.)
89Unsupported ISO options include:
90 * Progressive storage (may be supported in future versions)
91 * Hierarchical storage
92 * Lossless JPEG
93 * Arithmetic entropy coding (unsupported for legal reasons)
94 * DNL marker
95 * Nonintegral subsampling ratios
96We support both 8- and 12-bit data precision, but this is a compile-time
97choice rather than a run-time choice; hence it is difficult to use both
98precisions in a single application.
99
100By itself, the library handles only interchange JPEG datastreams --- in
101particular the widely used JFIF file format. The library can be used by
102surrounding code to process interchange or abbreviated JPEG datastreams that
103are embedded in more complex file formats. (For example, we anticipate that
104Sam Leffler's LIBTIFF library will use this code to support the revised TIFF
105JPEG format.)
106
107
108Outline of typical usage
109------------------------
110
111The rough outline of a JPEG compression operation is:
112
113 Allocate and initialize a JPEG compression object
114 Specify the destination for the compressed data (eg, a file)
115 Set parameters for compression, including image size & colorspace
116 jpeg_start_compress(...);
117 while (scan lines remain to be written)
118 jpeg_write_scanlines(...);
119 jpeg_finish_compress(...);
120 Release the JPEG compression object
121
122A JPEG compression object holds parameters and working state for the JPEG
123library. We make creation/destruction of the object separate from starting
124or finishing compression of an image; the same object can be re-used for a
125series of image compression operations. This makes it easy to re-use the
126same parameter settings for a sequence of images. Re-use of a JPEG object
127also has important implications for processing abbreviated JPEG datastreams,
128as discussed later.
129
130The image data to be compressed is supplied to jpeg_write_scanlines() from
131in-memory buffers. If the application is doing file-to-file compression,
132reading image data from the source file is the application's responsibility.
133The library emits compressed data by calling a "data destination manager",
134which typically will write the data into a file; but the application can
135provide its own destination manager to do something else.
136
137Similarly, the rough outline of a JPEG decompression operation is:
138
139 Allocate and initialize a JPEG decompression object
140 Specify the source of the compressed data (eg, a file)
141 Call jpeg_read_header() to obtain image info
142 Set parameters for decompression
143 jpeg_start_decompress(...);
144 while (scan lines remain to be read)
145 jpeg_read_scanlines(...);
146 jpeg_finish_decompress(...);
147 Release the JPEG decompression object
148
149This is comparable to the compression outline except that reading the
150datastream header is a separate step. This is helpful because information
151about the image's size, colorspace, etc is available when the application
152selects decompression parameters. For example, the application can choose an
153output scaling ratio that will fit the image into the available screen size.
154
155The decompression library obtains compressed data by calling a data source
156manager, which typically will read the data from a file; but other behaviors
157can be obtained with a custom source manager. Decompressed data is delivered
158into in-memory buffers passed to jpeg_read_scanlines().
159
160It is possible to abort an incomplete compression or decompression operation
161by calling jpeg_abort(); or, if you do not need to retain the JPEG object,
162simply release it by calling jpeg_destroy().
163
164JPEG compression and decompression objects are two separate struct types.
165However, they share some common fields, and certain routines such as
166jpeg_destroy() can work on either type of object.
167
168The JPEG library has no static variables: all state is in the compression
169or decompression object. Therefore it is possible to process multiple
170compression and decompression operations concurrently, using multiple JPEG
171objects.
172
173Both compression and decompression can be done in an incremental memory-to-
174memory fashion, if suitable source/destination managers are used. However,
175there are some restrictions on the processing that can be done in this mode.
176See the section on "I/O suspension" for more details.
177
178
179BASIC LIBRARY USAGE
180===================
181
182Data formats
183------------
184
185Before diving into procedural details, it is helpful to understand the
186image data format that the JPEG library expects or returns.
187
188The standard input image format is a rectangular array of pixels, with each
189pixel having the same number of "component" values (color channels). You
190must specify how many components there are and the colorspace interpretation
191of the components. Most applications will use RGB data (three components
192per pixel) or grayscale data (one component per pixel).
193
194Note that there is no provision for colormapped input. You can feed in a
195colormapped image by expanding it to full-color format. However JPEG often
196doesn't work very well with colormapped source data, because of dithering
197noise. This is discussed in more detail in the JPEG FAQ and the other
198references mentioned in the README file.
199
200Pixels are stored by scanlines, with each scanline running from left to
201right. The component values for each pixel are adjacent in the row; for
202example, R,G,B,R,G,B,R,G,B,... for 24-bit RGB color. Each scanline is an
203array of data type JSAMPLE --- which is typically "unsigned char", unless
204you've changed jmorecfg.h. (You can also change the RGB pixel layout, say
205to B,G,R order, by modifying jmorecfg.h. But see the restrictions listed in
206that file before doing so.)
207
208A 2-D array of pixels is formed by making a list of pointers to the starts of
209scanlines; so the scanlines need not be physically adjacent in memory. Even
210if you process just one scanline at a time, you must make a one-element
211pointer array to serve this purpose. Pointers to JSAMPLE rows are of type
212JSAMPROW, and the pointer to the pointer array is of type JSAMPARRAY.
213
214The library accepts or supplies one or more complete scanlines per call.
215It is not possible to process part of a row at a time. Scanlines are always
216processed top-to-bottom. You can process an entire image in one call if you
217have it all in memory, but usually it's simplest to process one scanline at
218a time.
219
220For best results, source data values should have the precision specified by
221BITS_IN_JSAMPLE (normally 8 bits). For instance, if you choose to compress
222data that's only 6 bits/channel, you should left-justify each value in a
223byte before passing it to the compressor. If you need to compress data
224that has more than 8 bits/channel, compile with BITS_IN_JSAMPLE = 12.
225(See "Library compile-time options", later.)
226
227The data format returned by the decompressor is the same in all details,
228except that colormapped data is supported. If you request colormapped
229output then the returned data array contains a single JSAMPLE per pixel;
230its value is an index into a color map. The color map is represented as
231a 2-D JSAMPARRAY in which each row holds the values of one color component,
232that is, colormap[i][j] is the value of the i'th color component for pixel
233value (map index) j. Note that since the colormap indexes are stored in
234JSAMPLEs, the maximum number of colors is limited by the size of JSAMPLE
235(ie, at most 256 colors for an 8-bit JPEG library).
236
237
238Compression details
239-------------------
240
241Here we revisit the JPEG compression outline given in the overview.
242
2431. Allocate and initialize a JPEG compression object.
244
245A JPEG compression object is a "struct jpeg_compress_struct" (plus a bunch of
246subsidiary structures which are allocated via malloc(), but the application
247doesn't control those directly). This struct can be just a local variable in
248the calling routine, if a single routine is going to execute the whole JPEG
249compression sequence. Otherwise it can be static or allocated from malloc().
250
251You will also need a structure representing a JPEG error handler. The part
252of this that the library cares about is a "struct jpeg_error_mgr". If you
253are providing your own error handler, you'll typically want to embed the
254jpeg_error_mgr struct in a larger structure; this is discussed later under
255"Error handling". For now we'll assume you are just using the default error
256handler. The default error handler will print JPEG error/warning messages
257on stderr, and it will call exit() if a fatal error occurs.
258
259You must initialize the error handler structure, store a pointer to it into
260the JPEG object's "err" field, and then call jpeg_create_compress() to
261initialize the rest of the JPEG object.
262
263Typical code for this step, if you are using the default error handler, is
264
265 struct jpeg_compress_struct cinfo;
266 struct jpeg_error_mgr jerr;
267 ...
268 cinfo.err = jpeg_std_error(&jerr);
269 jpeg_create_compress(&cinfo);
270
271jpeg_create_compress allocates a small amount of memory, so it could fail
272if you are out of memory. In that case it will exit via the error handler;
273that's why the error handler must be initialized first.
274
275
2762. Specify the destination for the compressed data (eg, a file).
277
278As previously mentioned, the JPEG library delivers compressed data to a
279"data destination" module. The library includes one data destination
280module which knows how to write to a stdio stream. You can use your own
281destination module if you want to do something else, as discussed later.
282
283If you use the standard destination module, you must open the target stdio
284stream beforehand. Typical code for this step looks like:
285
286 FILE * outfile;
287 ...
288 if ((outfile = fopen(filename, "wb")) == NULL) {
289 fprintf(stderr, "can't open %s\n", filename);
290 exit(1);
291 }
292 jpeg_stdio_dest(&cinfo, outfile);
293
294where the last line invokes the standard destination module.
295
296WARNING: it is critical that the binary compressed data be delivered to the
297output file unchanged. On non-Unix systems the stdio library may perform
298newline translation or otherwise corrupt binary data. To suppress this
299behavior, you may need to use a "b" option to fopen (as shown above), or use
300setmode() or another routine to put the stdio stream in binary mode. See
301cjpeg.c and djpeg.c for code that has been found to work on many systems.
302
303You can select the data destination after setting other parameters (step 3),
304if that's more convenient. You may not change the destination between
305calling jpeg_start_compress() and jpeg_finish_compress().
306
307
3083. Set parameters for compression, including image size & colorspace.
309
310You must supply information about the source image by setting the following
311fields in the JPEG object (cinfo structure):
312
313 image_width Width of image, in pixels
314 image_height Height of image, in pixels
315 input_components Number of color channels (samples per pixel)
316 in_color_space Color space of source image
317
318The image dimensions are, hopefully, obvious. JPEG supports image dimensions
319of 1 to 64K pixels in either direction. The input color space is typically
320RGB or grayscale, and input_components is 3 or 1 accordingly. (See "Special
321color spaces", later, for more info.) The in_color_space field must be
322assigned one of the J_COLOR_SPACE enum constants, typically JCS_RGB or
323JCS_GRAYSCALE.
324
325JPEG has a large number of compression parameters that determine how the
326image is encoded. Most applications don't need or want to know about all
327these parameters. You can set all the parameters to reasonable defaults by
328calling jpeg_set_defaults(); then, if there are particular values you want
329to change, you can do so after that. The "Compression parameter selection"
330section tells about all the parameters.
331
332You must set in_color_space correctly before calling jpeg_set_defaults(),
333because the defaults depend on the source image colorspace. However the
334other three source image parameters need not be valid until you call
335jpeg_start_compress(). There's no harm in calling jpeg_set_defaults() more
336than once, if that happens to be convenient.
337
338Typical code for a 24-bit RGB source image is
339
340 cinfo.image_width = Width; /* image width and height, in pixels */
341 cinfo.image_height = Height;
342 cinfo.input_components = 3; /* # of color components per pixel */
343 cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
344
345 jpeg_set_defaults(&cinfo);
346 /* Make optional parameter settings here */
347
348
3494. jpeg_start_compress(...);
350
351After you have established the data destination and set all the necessary
352source image info and other parameters, call jpeg_start_compress() to begin
353a compression cycle. This will initialize internal state, allocate working
354storage, and emit the first few bytes of the JPEG datastream header.
355
356Typical code:
357
358 jpeg_start_compress(&cinfo, TRUE);
359
360The "TRUE" parameter ensures that a complete JPEG interchange datastream
361will be written. This is appropriate in most cases. If you think you might
362want to use an abbreviated datastream, read the section on abbreviated
363datastreams, below.
364
365Once you have called jpeg_start_compress(), you may not alter any JPEG
366parameters or other fields of the JPEG object until you have completed
367the compression cycle.
368
369
3705. while (scan lines remain to be written)
371 jpeg_write_scanlines(...);
372
373Now write all the required image data by calling jpeg_write_scanlines()
374one or more times. You can pass one or more scanlines in each call, up
375to the total image height. In most applications it is convenient to pass
376just one or a few scanlines at a time. The expected format for the passed
377data is discussed under "Data formats", above.
378
379Image data should be written in top-to-bottom scanline order. The JPEG spec
380contains some weasel wording about how top and bottom are application-defined
381terms (a curious interpretation of the English language...) but if you want
382your files to be compatible with everyone else's, you WILL use top-to-bottom
383order. If the source data must be read in bottom-to-top order, you can use
384the JPEG library's virtual array mechanism to invert the data efficiently.
385Examples of this can be found in the sample application cjpeg.
386
387The library maintains a count of the number of scanlines written so far
388in the next_scanline field of the JPEG object. Usually you can just use
389this variable as the loop counter, so that the loop test looks like
390"while (cinfo.next_scanline < cinfo.image_height)".
391
392Code for this step depends heavily on the way that you store the source data.
393example.c shows the following code for the case of a full-size 2-D source
394array containing 3-byte RGB pixels:
395
396 JSAMPROW row_pointer[1]; /* pointer to a single row */
397 int row_stride; /* physical row width in buffer */
398
399 row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
400
401 while (cinfo.next_scanline < cinfo.image_height) {
402 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
403 jpeg_write_scanlines(&cinfo, row_pointer, 1);
404 }
405
406jpeg_write_scanlines() returns the number of scanlines actually written.
407This will normally be equal to the number passed in, so you can usually
408ignore the return value. It is different in just two cases:
409 * If you try to write more scanlines than the declared image height,
410 the additional scanlines are ignored.
411 * If you use a suspending data destination manager, output buffer overrun
412 will cause the compressor to return before accepting all the passed lines.
413 This feature is discussed under "I/O suspension", below. The normal
414 stdio destination manager will NOT cause this to happen.
415In any case, the return value is the same as the change in the value of
416next_scanline.
417
418
4196. jpeg_finish_compress(...);
420
421After all the image data has been written, call jpeg_finish_compress() to
422complete the compression cycle. This step is ESSENTIAL to ensure that the
423last bufferload of data is written to the data destination.
424jpeg_finish_compress() also releases working memory associated with the JPEG
425object.
426
427Typical code:
428
429 jpeg_finish_compress(&cinfo);
430
431If using the stdio destination manager, don't forget to close the output
432stdio stream if necessary.
433
434If you have requested a multi-pass operating mode, such as Huffman code
435optimization, jpeg_finish_compress() will perform the additional passes using
436data buffered by the first pass. In this case jpeg_finish_compress() may take
437quite a while to complete. With the default compression parameters, this will
438not happen.
439
440It is an error to call jpeg_finish_compress() before writing the necessary
441total number of scanlines. If you wish to abort compression, call
442jpeg_abort() as discussed below.
443
444After completing a compression cycle, you may dispose of the JPEG object
445as discussed next, or you may use it to compress another image. In that case
446return to step 2, 3, or 4 as appropriate. If you do not change the
447destination manager, the new datastream will be written to the same target.
448If you do not change any JPEG parameters, the new datastream will be written
449with the same parameters as before. Note that you can change the input image
450dimensions freely between cycles, but if you change the input colorspace, you
451should call jpeg_set_defaults() to adjust for the new colorspace; and then
452you'll need to repeat all of step 3.
453
454
4557. Release the JPEG compression object.
456
457When you are done with a JPEG compression object, destroy it by calling
458jpeg_destroy_compress(). This will free all subsidiary memory. Or you can
459call jpeg_destroy() which works for either compression or decompression
460objects --- this may be more convenient if you are sharing code between
461compression and decompression cases. (Actually, these routines are equivalent
462except for the declared type of the passed pointer. To avoid gripes from
463ANSI C compilers, pass a j_common_ptr to jpeg_destroy().)
464
465If you allocated the jpeg_compress_struct structure from malloc(), freeing
466it is your responsibility --- jpeg_destroy() won't. Ditto for the error
467handler structure.
468
469Typical code:
470
471 jpeg_destroy_compress(&cinfo);
472
473
4748. Aborting.
475
476If you decide to abort a compression cycle before finishing, you can clean up
477in either of two ways:
478
479* If you don't need the JPEG object any more, just call
480 jpeg_destroy_compress() or jpeg_destroy() to release memory. This is
481 legitimate at any point after calling jpeg_create_compress() --- in fact,
482 it's safe even if jpeg_create_compress() fails.
483
484* If you want to re-use the JPEG object, call jpeg_abort_compress(), or
485 jpeg_abort() which works on both compression and decompression objects.
486 This will return the object to an idle state, releasing any working memory.
487 jpeg_abort() is allowed at any time after successful object creation.
488
489Note that cleaning up the data destination, if required, is your
490responsibility.
491
492
493Decompression details
494---------------------
495
496Here we revisit the JPEG decompression outline given in the overview.
497
4981. Allocate and initialize a JPEG decompression object.
499
500This is just like initialization for compression, as discussed above,
501except that the object is a "struct jpeg_decompress_struct" and you
502call jpeg_create_decompress(). Error handling is exactly the same.
503
504Typical code:
505
506 struct jpeg_decompress_struct cinfo;
507 struct jpeg_error_mgr jerr;
508 ...
509 cinfo.err = jpeg_std_error(&jerr);
510 jpeg_create_decompress(&cinfo);
511
512(Both here and in the IJG code, we usually use variable name "cinfo" for
513both compression and decompression objects.)
514
515
5162. Specify the source of the compressed data (eg, a file).
517
518As previously mentioned, the JPEG library reads compressed data from a "data
519source" module. The library includes one data source module which knows how
520to read from a stdio stream. You can use your own source module if you want
521to do something else, as discussed later.
522
523If you use the standard source module, you must open the source stdio stream
524beforehand. Typical code for this step looks like:
525
526 FILE * infile;
527 ...
528 if ((infile = fopen(filename, "rb")) == NULL) {
529 fprintf(stderr, "can't open %s\n", filename);
530 exit(1);
531 }
532 jpeg_stdio_src(&cinfo, infile);
533
534where the last line invokes the standard source module.
535
536WARNING: it is critical that the binary compressed data be read unchanged.
537On non-Unix systems the stdio library may perform newline translation or
538otherwise corrupt binary data. To suppress this behavior, you may need to use
539a "b" option to fopen (as shown above), or use setmode() or another routine to
540put the stdio stream in binary mode. See cjpeg.c and djpeg.c for code that
541has been found to work on many systems.
542
543You may not change the data source between calling jpeg_read_header() and
544jpeg_finish_decompress(). If you wish to read a series of JPEG images from
545a single source file, you should repeat the jpeg_read_header() to
546jpeg_finish_decompress() sequence without reinitializing either the JPEG
547object or the data source module; this prevents buffered input data from
548being discarded.
549
550
5513. Call jpeg_read_header() to obtain image info.
552
553Typical code for this step is just
554
555 jpeg_read_header(&cinfo, TRUE);
556
557This will read the source datastream header markers, up to the beginning
558of the compressed data proper. On return, the image dimensions and other
559info have been stored in the JPEG object. The application may wish to
560consult this information before selecting decompression parameters.
561
562More complex code is necessary if
563 * A suspending data source is used --- in that case jpeg_read_header()
564 may return before it has read all the header data. See "I/O suspension",
565 below. The normal stdio source manager will NOT cause this to happen.
566 * Abbreviated JPEG files are to be processed --- see the section on
567 abbreviated datastreams. Standard applications that deal only in
568 interchange JPEG files need not be concerned with this case either.
569
570It is permissible to stop at this point if you just wanted to find out the
571image dimensions and other header info for a JPEG file. In that case,
572call jpeg_destroy() when you are done with the JPEG object, or call
573jpeg_abort() to return it to an idle state before selecting a new data
574source and reading another header.
575
576
5774. Set parameters for decompression.
578
579jpeg_read_header() sets appropriate default decompression parameters based on
580the properties of the image (in particular, its colorspace). However, you
581may well want to alter these defaults before beginning the decompression.
582For example, the default is to produce full color output from a color file.
583If you want colormapped output you must ask for it. Other options allow the
584returned image to be scaled and allow various speed/quality tradeoffs to be
585selected. "Decompression parameter selection", below, gives details.
586
587If the defaults are appropriate, nothing need be done at this step.
588
589Note that all default values are set by each call to jpeg_read_header().
590If you reuse a decompression object, you cannot expect your parameter
591settings to be preserved across cycles, as you can for compression.
592You must adjust parameter values each time.
593
594
5955. jpeg_start_decompress(...);
596
597Once the parameter values are satisfactory, call jpeg_start_decompress() to
598begin decompression. This will initialize internal state, allocate working
599memory, and prepare for returning data.
600
601Typical code is just
602
603 jpeg_start_decompress(&cinfo);
604
605If you have requested a multi-pass operating mode, such as 2-pass color
606quantization, jpeg_start_decompress() will do everything needed before data
607output can begin. In this case jpeg_start_decompress() may take quite a while
608to complete. With a single-scan (fully interleaved) JPEG file and default
609decompression parameters, this will not happen; jpeg_start_decompress() will
610return quickly.
611
612After this call, the final output image dimensions, including any requested
613scaling, are available in the JPEG object; so is the selected colormap, if
614colormapped output has been requested. Useful fields include
615
616 output_width image width and height, as scaled
617 output_height
618 out_color_components # of color components in out_color_space
619 output_components # of color components returned per pixel
620 colormap the selected colormap, if any
621 actual_number_of_colors number of entries in colormap
622
623output_components is 1 (a colormap index) when quantizing colors; otherwise it
624equals out_color_components. It is the number of JSAMPLE values that will be
625emitted per pixel in the output arrays.
626
627Typically you will need to allocate data buffers to hold the incoming image.
628You will need output_width * output_components JSAMPLEs per scanline in your
629output buffer, and a total of output_height scanlines will be returned.
630
631Note: if you are using the JPEG library's internal memory manager to allocate
632data buffers (as djpeg does), then the manager's protocol requires that you
633request large buffers *before* calling jpeg_start_decompress(). This is a
634little tricky since the output_XXX fields are not normally valid then. You
635can make them valid by calling jpeg_calc_output_dimensions() after setting the
636relevant parameters (scaling, output color space, and quantization flag).
637
638
6396. while (scan lines remain to be read)
640 jpeg_read_scanlines(...);
641
642Now you can read the decompressed image data by calling jpeg_read_scanlines()
643one or more times. At each call, you pass in the maximum number of scanlines
644to be read (ie, the height of your working buffer); jpeg_read_scanlines()
645will return up to that many lines. The return value is the number of lines
646actually read. The format of the returned data is discussed under "Data
647formats", above.
648
649Image data is returned in top-to-bottom scanline order. If you must write
650out the image in bottom-to-top order, you can use the JPEG library's virtual
651array mechanism to invert the data efficiently. Examples of this can be
652found in the sample application djpeg.
653
654The library maintains a count of the number of scanlines returned so far
655in the output_scanline field of the JPEG object. Usually you can just use
656this variable as the loop counter, so that the loop test looks like
657"while (cinfo.output_scanline < cinfo.output_height)". (Note that the test
658should NOT be against image_height, unless you never use scaling. The
659image_height field is the height of the original unscaled image.)
660
661If you don't use a suspending data source, it is safe to assume that
662jpeg_read_scanlines() reads at least one scanline per call, until the
663bottom of the image has been reached. If you use a buffer larger than one
664scanline, it is NOT safe to assume that jpeg_read_scanlines() fills it.
665In any case, the return value is the same as the change in the value of
666output_scanline.
667
668
6697. jpeg_finish_decompress(...);
670
671After all the image data has been read, call jpeg_finish_decompress() to
672complete the decompression cycle. This causes working memory associated
673with the JPEG object to be released.
674
675Typical code:
676
677 jpeg_finish_decompress(&cinfo);
678
679If using the stdio source manager, don't forget to close the source stdio
680stream if necessary.
681
682It is an error to call jpeg_finish_decompress() before reading the correct
683total number of scanlines. If you wish to abort compression, call
684jpeg_abort() as discussed below.
685
686After completing a decompression cycle, you may dispose of the JPEG object as
687discussed next, or you may use it to decompress another image. In that case
688return to step 2 or 3 as appropriate. If you do not change the source
689manager, the next image will be read from the same source.
690
691
6928. Release the JPEG decompression object.
693
694When you are done with a JPEG decompression object, destroy it by calling
695jpeg_destroy_decompress() or jpeg_destroy(). The previous discussion of
696destroying compression objects applies here too.
697
698Typical code:
699
700 jpeg_destroy_decompress(&cinfo);
701
702
7039. Aborting.
704
705You can abort a decompression cycle by calling jpeg_destroy_decompress() or
706jpeg_destroy() if you don't need the JPEG object any more, or
707jpeg_abort_decompress() or jpeg_abort() if you want to reuse the object.
708The previous discussion of aborting compression cycles applies here too.
709
710
711Mechanics of usage: include files, linking, etc
712-----------------------------------------------
713
714Applications using the JPEG library should include the header file jpeglib.h
715to obtain declarations of data types and routines. Before including
716jpeglib.h, include system headers that define at least the typedefs FILE and
717size_t. On ANSI-conforming systems, including <stdio.h> is sufficient; on
718older Unix systems, you may need <sys/types.h> to define size_t.
719
720If the application needs to refer to individual JPEG library error codes, also
721include jerror.h to define those symbols.
722
723jpeglib.h indirectly includes the files jconfig.h and jmorecfg.h. If you are
724installing the JPEG header files in a system directory, you will want to
725install all four files: jpeglib.h, jerror.h, jconfig.h, jmorecfg.h.
726
727The most convenient way to include the JPEG code into your executable program
728is to prepare a library file ("libjpeg.a", or a corresponding name on non-Unix
729machines) and reference it at your link step. If you use only half of the
730library (only compression or only decompression), only that much code will be
731included from the library, unless your linker is hopelessly brain-damaged.
732The supplied makefiles build libjpeg.a automatically (see install.doc).
733
734On some systems your application may need to set up a signal handler to ensure
735that temporary files are deleted if the program is interrupted. This is most
736critical if you are on MS-DOS and use the jmemdos.c memory manager back end;
737it will try to grab extended memory for temp files, and that space will NOT be
738freed automatically. See cjpeg.c or djpeg.c for an example signal handler.
739
740It may be worth pointing out that the core JPEG library does not actually
741require the stdio library: only the default source/destination managers and
742error handler need it. You can use the library in a stdio-less environment
743if you replace those modules and use jmemnobs.c (or another memory manager of
744your own devising). More info about the minimum system library requirements
745may be found in jinclude.h.
746
747
748ADVANCED FEATURES
749=================
750
751Compression parameter selection
752-------------------------------
753
754This section describes all the optional parameters you can set for JPEG
755compression, as well as the "helper" routines provided to assist in this
756task. Proper setting of some parameters requires detailed understanding
757of the JPEG standard; if you don't know what a parameter is for, it's best
758not to mess with it! See REFERENCES in the README file for pointers to
759more info about JPEG.
760
761It's a good idea to call jpeg_set_defaults() first, even if you plan to set
762all the parameters; that way your code is more likely to work with future JPEG
763libraries that have additional parameters. For the same reason, we recommend
764you use a helper routine where one is provided, in preference to twiddling
765cinfo fields directly.
766
767The helper routines are:
768
769jpeg_set_defaults (j_compress_ptr cinfo)
770 This routine sets all JPEG parameters to reasonable defaults, using
771 only the input image's color space (field in_color_space, which must
772 already be set in cinfo). Many applications will only need to use
773 this routine and perhaps jpeg_set_quality().
774
775jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
776 Sets the JPEG file's colorspace (field jpeg_color_space) as specified,
777 and sets other color-space-dependent parameters appropriately. See
778 "Special color spaces", below, before using this. A large number of
779 parameters, including all per-component parameters, are set by this
780 routine; if you want to twiddle individual parameters you should call
781 jpeg_set_colorspace() before rather than after.
782
783jpeg_default_colorspace (j_compress_ptr cinfo)
784 Selects an appropriate JPEG colorspace based on cinfo->in_color_space,
785 and calls jpeg_set_colorspace(). This is actually a subroutine of
786 jpeg_set_defaults(). It's broken out in case you want to change
787 just the colorspace-dependent JPEG parameters.
788
789jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
790 Constructs JPEG quantization tables appropriate for the indicated
791 quality setting. The quality value is expressed on the 0..100 scale
792 recommended by IJG (cjpeg's "-quality" switch uses this routine).
793 Note that the exact mapping from quality values to tables may change
794 in future IJG releases as more is learned about DCT quantization.
795 If the force_baseline parameter is TRUE, then the quantization table
796 entries are constrained to the range 1..255 for full JPEG baseline
797 compatibility. In the current implementation, this only makes a
798 difference for quality settings below 25, and it effectively prevents
799 very small/low quality files from being generated. The IJG decoder
800 is capable of reading the non-baseline files generated at low quality
801 settings when force_baseline is FALSE, but other decoders may not be.
802
803jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
804 boolean force_baseline)
805 Same as jpeg_set_quality() except that the generated tables are the
806 sample tables given in the JPEC spec section K.1, multiplied by the
807 specified scale factor (which is expressed as a percentage; thus
808 scale_factor = 100 reproduces the spec's tables). Note that larger
809 scale factors give lower quality. This entry point is useful for
810 conforming to the Adobe PostScript DCT conventions, but we do not
811 recommend linear scaling as a user-visible quality scale otherwise.
812 force_baseline again constrains the computed table entries to 1..255.
813
814int jpeg_quality_scaling (int quality)
815 Converts a value on the IJG-recommended quality scale to a linear
816 scaling percentage. Note that this routine may change or go away
817 in future releases --- IJG may choose to adopt a scaling method that
818 can't be expressed as a simple scalar multiplier, in which case the
819 premise of this routine collapses. Caveat user.
820
821jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
822 const unsigned int *basic_table,
823 int scale_factor, boolean force_baseline));
824 Allows an arbitrary quantization table to be created. which_tbl
825 indicates which table slot to fill. basic_table points to an array
826 of 64 unsigned ints given in JPEG zigzag order. These values are
827 multiplied by scale_factor/100 and then clamped to the range 1..65535
828 (or to 1..255 if force_baseline is TRUE).
829
830
831Compression parameters (cinfo fields) include:
832
833boolean optimize_coding
834 TRUE causes the compressor to compute optimal Huffman coding tables
835 for the image. This requires an extra pass over the data and
836 therefore costs a good deal of space and time. The default is
837 FALSE, which tells the compressor to use the supplied or default
838 Huffman tables. In most cases optimal tables save only a few percent
839 of file size compared to the default tables. Note that when this is
840 TRUE, you need not supply Huffman tables at all, and any you do
841 supply will be overwritten.
842
843int smoothing_factor
844 If non-zero, the input image is smoothed; the value should be 1 for
845 minimal smoothing to 100 for maximum smoothing. Consult jcsample.c
846 for details of the smoothing algorithm. The default is zero.
847
848J_DCT_METHOD dct_method
849 Selects the algorithm used for the DCT step. Choices are:
850 JDCT_ISLOW: slow but accurate integer algorithm
851 JDCT_IFAST: faster, less accurate integer method
852 JDCT_FLOAT: floating-point method
853 JDCT_DEFAULT: default method (normally JDCT_ISLOW)
854 JDCT_FASTEST: fastest method (normally JDCT_IFAST)
855 The floating-point method is the most accurate, but may give slightly
856 different results on different machines due to varying roundoff
857 behavior. The integer methods should give the same results on all
858 machines. On machines with sufficiently fast FP hardware, the
859 floating-point method may also be the fastest. The IFAST method is
860 considerably less accurate than the other two; its use is not
861 recommended if high quality is a concern. JDCT_DEFAULT and
862 JDCT_FASTEST are macros configurable by each installation.
863
864unsigned int restart_interval
865int restart_in_rows
866 To emit restart markers in the JPEG file, set one of these nonzero.
867 Set restart_interval to specify the exact interval in MCU blocks.
868 Set restart_in_rows to specify the interval in MCU rows. (If
869 restart_in_rows is not 0, then restart_interval is set after the
870 image width in MCUs is computed.) Defaults are zero (no restarts).
871
872J_COLOR_SPACE jpeg_color_space
873int num_components
874 The JPEG color space and corresponding number of components; see
875 "Special color spaces", below, for more info. We recommend using
876 jpeg_set_color_space() if you want to change these.
877
878boolean write_JFIF_header
879 If TRUE, a JFIF APP0 marker is emitted. jpeg_set_defaults() and
880 jpeg_set_colorspace() set this TRUE if a JFIF-legal JPEG color space
881 (ie, YCbCr or grayscale) is selected, otherwise FALSE.
882
883UINT8 density_unit
884UINT16 X_density
885UINT16 Y_density
886 The resolution information to be written into the JFIF marker;
887 not used otherwise. density_unit may be 0 for unknown,
888 1 for dots/inch, or 2 for dots/cm. The default values are 0,1,1
889 indicating square pixels of unknown size.
890
891boolean write_Adobe_marker
892 If TRUE, an Adobe APP14 marker is emitted. jpeg_set_defaults() and
893 jpeg_set_colorspace() set this TRUE if JPEG color space RGB, CMYK,
894 or YCCK is selected, otherwise FALSE. It is generally a bad idea
895 to set both write_JFIF_header and write_Adobe_marker. In fact,
896 you probably shouldn't change the default settings at all --- the
897 default behavior ensures that the JPEG file's color space can be
898 recognized by the decoder.
899
900JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS]
901 Pointers to coefficient quantization tables, one per table slot,
902 or NULL if no table is defined for a slot. Usually these should
903 be set via one of the above helper routines; jpeg_add_quant_table()
904 is general enough to define any quantization table. The other
905 routines will set up table slot 0 for luminance quality and table
906 slot 1 for chrominance.
907
908JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS]
909JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS]
910 Pointers to Huffman coding tables, one per table slot, or NULL if
911 no table is defined for a slot. Slots 0 and 1 are filled with the
912 JPEG sample tables by jpeg_set_defaults(). If you need to allocate
913 more table structures, jpeg_alloc_huff_table() may be used.
914 Note that optimal Huffman tables can be computed for an image
915 by setting optimize_coding, as discussed above; there's seldom
916 any need to mess with providing your own Huffman tables.
917
918There are some additional cinfo fields which are not documented here
919because you currently can't change them; for example, you can't set
920arith_code TRUE because arithmetic coding is unsupported.
921
922
923Per-component parameters are stored in the struct cinfo.comp_info[i] for
924component number i. Note that components here refer to components of the
925JPEG color space, *not* the source image color space. A suitably large
926comp_info[] array is allocated by jpeg_set_defaults(); if you choose not
927to use that routine, it's up to you to allocate the array.
928
929int component_id
930 The one-byte identifier code to be recorded in the JPEG file for
931 this component. For the standard color spaces, we recommend you
932 leave the default values alone.
933
934int h_samp_factor
935int v_samp_factor
936 Horizontal and vertical sampling factors for the component; must
937 be 1..4 according to the JPEG standard. Note that larger sampling
938 factors indicate a higher-resolution component; many people find
939 this behavior quite unintuitive. The default values are 2,2 for
940 luminance components and 1,1 for chrominance components, except
941 for grayscale where 1,1 is used.
942
943int quant_tbl_no
944 Quantization table number for component. The default value is
945 0 for luminance components and 1 for chrominance components.
946
947int dc_tbl_no
948int ac_tbl_no
949 DC and AC entropy coding table numbers. The default values are
950 0 for luminance components and 1 for chrominance components.
951
952int component_index
953 Must equal the component's index in comp_info[].
954
955
956Decompression parameter selection
957---------------------------------
958
959Decompression parameter selection is somewhat simpler than compression
960parameter selection, since all of the JPEG internal parameters are
961recorded in the source file and need not be supplied by the application.
962(Unless you are working with abbreviated files, in which case see
963"Abbreviated datastreams", below.) Decompression parameters control
964the postprocessing done on the image to deliver it in a format suitable
965for the application's use. Many of the parameters control speed/quality
966tradeoffs, in which faster decompression may be obtained at the price of
967a poorer-quality image. The defaults select the highest quality (slowest)
968processing.
969
970The following fields in the JPEG object are set by jpeg_read_header() and
971may be useful to the application in choosing decompression parameters:
972
973JDIMENSION image_width Width and height of image
974JDIMENSION image_height
975int num_components Number of color components
976J_COLOR_SPACE jpeg_color_space Colorspace of image
977boolean saw_JFIF_marker TRUE if a JFIF APP0 marker was seen
978 UINT8 density_unit Resolution data from JFIF marker
979 UINT16 X_density
980 UINT16 Y_density
981boolean saw_Adobe_marker TRUE if an Adobe APP14 marker was seen
982 UINT8 Adobe_transform Color transform code from Adobe marker
983
984The JPEG color space, unfortunately, is something of a guess since the JPEG
985standard proper does not provide a way to record it. In practice most files
986adhere to the JFIF or Adobe conventions, and the decoder will recognize these
987correctly. See "Special color spaces", below, for more info.
988
989
990The decompression parameters that determine the basic properties of the
991returned image are:
992
993J_COLOR_SPACE out_color_space
994 Output color space. jpeg_read_header() sets an appropriate default
995 based on jpeg_color_space; typically it will be RGB or grayscale.
996 The application can change this field to request output in a different
997 colorspace. For example, set it to JCS_GRAYSCALE to get grayscale
998 output from a color file. (This is useful for previewing: grayscale
999 output is faster than full color since the color components need not
1000 be processed.) Note that not all possible color space transforms are
1001 currently implemented; you may need to extend jdcolor.c if you want an
1002 unusual conversion.
1003
1004unsigned int scale_num, scale_denom
1005 Scale the image by the fraction scale_num/scale_denom. Default is
1006 1/1, or no scaling. Currently, the only supported scaling ratios
1007 are 1/1, 1/2, 1/4, and 1/8. (The library design allows for arbitrary
1008 scaling ratios but this is not likely to be implemented any time soon.)
1009 Smaller scaling ratios permit significantly faster decoding since
1010 fewer pixels need be processed and a simpler IDCT method can be used.
1011
1012boolean quantize_colors
1013 If set TRUE, colormapped output will be delivered. Default is FALSE,
1014 meaning that full-color output will be delivered.
1015
1016The next three parameters are relevant only if quantize_colors is TRUE.
1017
1018int desired_number_of_colors
1019 Maximum number of colors to use in generating a library-supplied color
1020 map (the actual number of colors is returned in a different field).
1021 Default 256. Ignored when the application supplies its own color map.
1022
1023boolean two_pass_quantize
1024 If TRUE, an extra pass over the image is made to select a custom color
1025 map for the image. This usually looks a lot better than the one-size-
1026 fits-all colormap that is used otherwise. Default is TRUE. Ignored
1027 when the application supplies its own color map.
1028
1029J_DITHER_MODE dither_mode
1030 Selects color dithering method. Supported values are:
1031 JDITHER_NONE no dithering: fast, very low quality
1032 JDITHER_ORDERED ordered dither: moderate speed and quality
1033 JDITHER_FS Floyd-Steinberg dither: slow, high quality
1034 Default is JDITHER_FS. (At present, ordered dither is implemented
1035 only in the single-pass, standard-colormap case. If you ask for
1036 ordered dither when two_pass_quantize is TRUE or when you supply
1037 an external color map, you'll get F-S dithering.)
1038
1039When quantize_colors is TRUE, the target color map is described by the next
1040two fields. colormap is set to NULL by jpeg_read_header(). The application
1041can supply a color map by setting colormap non-NULL and setting
1042actual_number_of_colors to the map size. Otherwise, jpeg_start_decompress()
1043selects a suitable color map and sets these two fields itself.
1044[Implementation restriction: at present, an externally supplied colormap is
1045only accepted for 3-component output color spaces.]
1046
1047JSAMPARRAY colormap
1048 The color map, represented as a 2-D pixel array of out_color_components
1049 rows and actual_number_of_colors columns. Ignored if not quantizing.
1050
1051int actual_number_of_colors
1052 The number of colors in the color map.
1053
1054Additional decompression parameters that the application may set include:
1055
1056J_DCT_METHOD dct_method
1057 Selects the algorithm used for the DCT step. Choices are the same
1058 as described above for compression.
1059
1060boolean do_fancy_upsampling
1061 If TRUE, do careful upsampling of chroma components. If FALSE,
1062 a faster but sloppier method is used. Default is TRUE. The visual
1063 impact of the sloppier method is often very small.
1064
1065
1066The output image dimensions are given by the following fields. These are
1067computed from the source image dimensions and the decompression parameters
1068by jpeg_start_decompress(). You can also call jpeg_calc_output_dimensions()
1069to obtain the values that will result from the current parameter settings.
1070This can be useful if you are trying to pick a scaling ratio that will get
1071close to a desired target size. It's also important if you are using the
1072JPEG library's memory manager to allocate output buffer space, because you
1073are supposed to request such buffers *before* jpeg_start_decompress().
1074
1075JDIMENSION output_width Actual dimensions of output image.
1076JDIMENSION output_height
1077int out_color_components Number of color components in out_color_space.
1078int output_components Number of color components returned.
1079int rec_outbuf_height Recommended height of scanline buffer.
1080
1081When quantizing colors, output_components is 1, indicating a single color map
1082index per pixel. Otherwise it equals out_color_components. The output arrays
1083are required to be output_width * output_components JSAMPLEs wide.
1084
1085rec_outbuf_height is the recommended minimum height (in scanlines) of the
1086buffer passed to jpeg_read_scanlines(). If the buffer is smaller, the
1087library will still work, but time will be wasted due to unnecessary data
1088copying. In high-quality modes, rec_outbuf_height is always 1, but some
1089faster, lower-quality modes set it to larger values (typically 2 to 4).
1090If you are going to ask for a high-speed processing mode, you may as well
1091go to the trouble of honoring rec_outbuf_height so as to avoid data copying.
1092
1093
1094Special color spaces
1095--------------------
1096
1097The JPEG standard itself is "color blind" and doesn't specify any particular
1098color space. It is customary to convert color data to a luminance/chrominance
1099color space before compressing, since this permits greater compression. The
1100existing de-facto JPEG file format standards specify YCbCr or grayscale data
1101(JFIF), or grayscale, RGB, YCbCr, CMYK, or YCCK (Adobe). For special
1102applications such as multispectral images, other color spaces can be used,
1103but it must be understood that such files will be unportable.
1104
1105The JPEG library can handle the most common colorspace conversions (namely
1106RGB <=> YCbCr and CMYK <=> YCCK). It can also deal with data of an unknown
1107color space, passing it through without conversion. If you deal extensively
1108with an unusual color space, you can easily extend the library to understand
1109additional color spaces and perform appropriate conversions.
1110
1111For compression, the source data's color space is specified by field
1112in_color_space. This is transformed to the JPEG file's color space given
1113by jpeg_color_space. jpeg_set_defaults() chooses a reasonable JPEG color
1114space depending on in_color_space, but you can override this by calling
1115jpeg_set_colorspace(). Of course you must select a supported transformation.
1116jccolor.c currently supports the following transformations:
1117 RGB => YCbCr
1118 RGB => GRAYSCALE
1119 YCbCr => GRAYSCALE
1120 CMYK => YCCK
1121plus the null transforms: GRAYSCALE => GRAYSCALE, RGB => RGB,
1122YCbCr => YCbCr, CMYK => CMYK, YCCK => YCCK, and UNKNOWN => UNKNOWN.
1123
1124The de-facto file format standards (JFIF and Adobe) specify APPn markers that
1125indicate the color space of the JPEG file. It is important to ensure that
1126these are written correctly, or omitted if the JPEG file's color space is not
1127one of the ones supported by the de-facto standards. jpeg_set_colorspace()
1128will set the compression parameters to include or omit the APPn markers
1129properly, so long as it is told the truth about the JPEG color space.
1130For example, if you are writing some random 3-component color space without
1131conversion, don't try to fake out the library by setting in_color_space and
1132jpeg_color_space to JCS_YCbCr; use JCS_UNKNOWN. You may want to write an
1133APPn marker of your own devising to identify the colorspace --- see "Special
1134markers", below.
1135
1136When told that the color space is UNKNOWN, the library will default to using
1137luminance-quality compression parameters for all color components. You may
1138well want to change these parameters. See the source code for
1139jpeg_set_colorspace(), in jcparam.c, for details.
1140
1141For decompression, the JPEG file's color space is given in jpeg_color_space,
1142and this is transformed to the output color space out_color_space.
1143jpeg_read_header's setting of jpeg_color_space can be relied on if the file
1144conforms to JFIF or Adobe conventions, but otherwise it is no better than a
1145guess. If you know the JPEG file's color space for certain, you can override
1146jpeg_read_header's guess by setting jpeg_color_space. jpeg_read_header also
1147selects a default output color space based on (its guess of) jpeg_color_space;
1148set out_color_space to override this. Again, you must select a supported
1149transformation. jdcolor.c currently supports
1150 YCbCr => GRAYSCALE
1151 YCbCr => RGB
1152 YCCK => CMYK
1153as well as the null transforms.
1154
1155The two-pass color quantizer, jquant2.c, is specialized to handle RGB data
1156(it weights distances appropriately for RGB colors). You'll need to modify
1157the code if you want to use it for non-RGB output color spaces. Note that
1158jquant2.c is used to map to an application-supplied colormap as well as for
1159the normal two-pass colormap selection process.
1160
1161CAUTION: it appears that Adobe Photoshop writes inverted data in CMYK JPEG
1162files: 0 represents 100% ink coverage, rather than 0% ink as you'd expect.
1163This is arguably a bug in Photoshop, but if you need to work with Photoshop
1164CMYK files, you will have to deal with it in your application. We cannot
1165"fix" this in the library by inverting the data during the CMYK<=>YCCK
1166transform, because that would break other applications, notably Ghostscript.
1167Photoshop versions prior to 3.0 write EPS files containing JPEG-encoded CMYK
1168data in the same inverted-YCCK representation used in bare JPEG files, but
1169the surrounding PostScript code performs an inversion using the PS image
1170operator. I am told that Photoshop 3.0 will write uninverted YCCK in
1171EPS/JPEG files, and will omit the PS-level inversion. (But the data
1172polarity used in bare JPEG files will not change in 3.0.) In either case,
1173the JPEG library must not invert the data itself, or else Ghostscript would
1174read these EPS files incorrectly.
1175
1176
1177Error handling
1178--------------
1179
1180When the default error handler is used, any error detected inside the JPEG
1181routines will cause a message to be printed on stderr, followed by exit().
1182You can supply your own error handling routines to override this behavior
1183and to control the treatment of nonfatal warnings and trace/debug messages.
1184The file example.c illustrates the most common case, which is to have the
1185application regain control after an error rather than exiting.
1186
1187The JPEG library never writes any message directly; it always goes through
1188the error handling routines. Three classes of messages are recognized:
1189 * Fatal errors: the library cannot continue.
1190 * Warnings: the library can continue, but the data is corrupt, and a
1191 damaged output image is likely to result.
1192 * Trace/informational messages. These come with a trace level indicating
1193 the importance of the message; you can control the verbosity of the
1194 program by adjusting the maximum trace level that will be displayed.
1195
1196You may, if you wish, simply replace the entire JPEG error handling module
1197(jerror.c) with your own code. However, you can avoid code duplication by
1198only replacing some of the routines depending on the behavior you need.
1199This is accomplished by calling jpeg_std_error() as usual, but then overriding
1200some of the method pointers in the jpeg_error_mgr struct, as illustrated by
1201example.c.
1202
1203All of the error handling routines will receive a pointer to the JPEG object
1204(a j_common_ptr which points to either a jpeg_compress_struct or a
1205jpeg_decompress_struct; if you need to tell which, test the is_decompressor
1206field). This struct includes a pointer to the error manager struct in its
1207"err" field. Frequently, custom error handler routines will need to access
1208additional data which is not known to the JPEG library or the standard error
1209handler. The most convenient way to do this is to embed either the JPEG
1210object or the jpeg_error_mgr struct in a larger structure that contains
1211additional fields; then casting the passed pointer provides access to the
1212additional fields. Again, see example.c for one way to do it.
1213
1214The individual methods that you might wish to override are:
1215
1216error_exit (j_common_ptr cinfo)
1217 Receives control for a fatal error. Information sufficient to
1218 generate the error message has been stored in cinfo->err; call
1219 output_message to display it. Control must NOT return to the caller;
1220 generally this routine will exit() or longjmp() somewhere.
1221 Typically you would override this routine to get rid of the exit()
1222 default behavior. Note that if you continue processing, you should
1223 clean up the JPEG object with jpeg_abort() or jpeg_destroy().
1224
1225output_message (j_common_ptr cinfo)
1226 Actual output of any JPEG message. Override this to send messages
1227 somewhere other than stderr. Note that this method does not know
1228 how to generate a message, only where to send it.
1229
1230format_message (j_common_ptr cinfo, char * buffer)
1231 Constructs a readable error message string based on the error info
1232 stored in cinfo->err. This method is called by output_message. Few
1233 applications should need to override this method. One possible
1234 reason for doing so is to implement dynamic switching of error message
1235 language.
1236
1237emit_message (j_common_ptr cinfo, int msg_level)
1238 Decide whether or not to emit a warning or trace message; if so,
1239 calls output_message. The main reason for overriding this method
1240 would be to abort on warnings. msg_level is -1 for warnings,
1241 0 and up for trace messages.
1242
1243Only error_exit() and emit_message() are called from the rest of the JPEG
1244library; the other two are internal to the error handler.
1245
1246The actual message texts are stored in an array of strings which is pointed to
1247by the field err->jpeg_message_table. The messages are numbered from 0 to
1248err->last_jpeg_message, and it is these code numbers that are used in the
1249JPEG library code. You could replace the message texts (for instance, with
1250messages in French or German) by changing the message table pointer. See
1251jerror.h for the default texts. CAUTION: this table will almost certainly
1252change or grow from one library version to the next.
1253
1254It may be useful for an application to add its own message texts that are
1255handled by the same mechanism. The error handler supports a second "add-on"
1256message table for this purpose. To define an addon table, set the pointer
1257err->addon_message_table and the message numbers err->first_addon_message and
1258err->last_addon_message. If you number the addon messages beginning at 1000
1259or so, you won't have to worry about conflicts with the library's built-in
1260messages. See the sample applications cjpeg/djpeg for an example of using
1261addon messages (the addon messages are defined in cderror.h).
1262
1263Actual invocation of the error handler is done via macros defined in jerror.h:
1264 ERREXITn(...) for fatal errors
1265 WARNMSn(...) for corrupt-data warnings
1266 TRACEMSn(...) for trace and informational messages.
1267These macros store the message code and any additional parameters into the
1268error handler struct, then invoke the error_exit() or emit_message() method.
1269The variants of each macro are for varying numbers of additional parameters.
1270The additional parameters are inserted into the generated message using
1271standard printf() format codes.
1272
1273See jerror.h and jerror.c for further details.
1274
1275
1276Compressed data handling (source and destination managers)
1277----------------------------------------------------------
1278
1279The JPEG compression library sends its compressed data to a "destination
1280manager" module. The default destination manager just writes the data to a
1281stdio stream, but you can provide your own manager to do something else.
1282Similarly, the decompression library calls a "source manager" to obtain the
1283compressed data; you can provide your own source manager if you want the data
1284to come from somewhere other than a stdio stream.
1285
1286In both cases, compressed data is processed a bufferload at a time: the
1287destination or source manager provides a work buffer, and the library invokes
1288the manager only when the buffer is filled or emptied. (You could define a
1289one-character buffer to force the manager to be invoked for each byte, but
1290that would be rather inefficient.) The buffer's size and location are
1291controlled by the manager, not by the library. For example, if you desired to
1292decompress a JPEG datastream that was all in memory, you could just make the
1293buffer pointer and length point to the original data in memory. Then the
1294buffer-reload procedure would be invoked only if the decompressor ran off the
1295end of the datastream, which would indicate an erroneous datastream.
1296
1297The work buffer is defined as an array of datatype JOCTET, which is generally
1298"char" or "unsigned char". On a machine where char is not exactly 8 bits
1299wide, you must define JOCTET as a wider data type and then modify the data
1300source and destination modules to transcribe the work arrays into 8-bit units
1301on external storage.
1302
1303A data destination manager struct contains a pointer and count defining the
1304next byte to write in the work buffer and the remaining free space:
1305
1306 JOCTET * next_output_byte; /* => next byte to write in buffer */
1307 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
1308
1309The library increments the pointer and decrements the count until the buffer
1310is filled. The manager's empty_output_buffer method must reset the pointer
1311and count. The manager is expected to remember the buffer's starting address
1312and total size in private fields not visible to the library.
1313
1314A data destination manager provides three methods:
1315
1316init_destination (j_compress_ptr cinfo)
1317 Initialize destination. This is called by jpeg_start_compress()
1318 before any data is actually written. It must initialize
1319 next_output_byte and free_in_buffer. free_in_buffer must be
1320 initialized to a positive value.
1321
1322empty_output_buffer (j_compress_ptr cinfo)
1323 This is called whenever the buffer has filled (free_in_buffer
1324 reaches zero). In typical applications, it should write out the
1325 *entire* buffer (use the saved start address and buffer length;
1326 ignore the current state of next_output_byte and free_in_buffer).
1327 Then reset the pointer & count to the start of the buffer, and
1328 return TRUE indicating that the buffer has been dumped.
1329 free_in_buffer must be set to a positive value when TRUE is
1330 returned. A FALSE return should only be used when I/O suspension is
1331 desired (this operating mode is discussed in the next section).
1332
1333term_destination (j_compress_ptr cinfo)
1334 Terminate destination --- called by jpeg_finish_compress() after all
1335 data has been written. In most applications, this must flush any
1336 data remaining in the buffer. Use either next_output_byte or
1337 free_in_buffer to determine how much data is in the buffer.
1338
1339term_destination() is NOT called by jpeg_abort() or jpeg_destroy(). If you
1340want the destination manager to be cleaned up during an abort, you must do it
1341yourself.
1342
1343You will also need code to create a jpeg_destination_mgr struct, fill in its
1344method pointers, and insert a pointer to the struct into the "dest" field of
1345the JPEG compression object. This can be done in-line in your setup code if
1346you like, but it's probably cleaner to provide a separate routine similar to
1347the jpeg_stdio_dest() routine of the supplied destination manager.
1348
1349Decompression source managers follow a parallel design, but with some
1350additional frammishes. The source manager struct contains a pointer and count
1351defining the next byte to read from the work buffer and the number of bytes
1352remaining:
1353
1354 const JOCTET * next_input_byte; /* => next byte to read from buffer */
1355 size_t bytes_in_buffer; /* # of bytes remaining in buffer */
1356
1357The library increments the pointer and decrements the count until the buffer
1358is emptied. The manager's fill_input_buffer method must reset the pointer and
1359count. In most applications, the manager must remember the buffer's starting
1360address and total size in private fields not visible to the library.
1361
1362A data source manager provides five methods:
1363
1364init_source (j_decompress_ptr cinfo)
1365 Initialize source. This is called by jpeg_read_header() before any
1366 data is actually read. Unlike init_destination(), it may leave
1367 bytes_in_buffer set to 0 (in which case a fill_input_buffer() call
1368 will occur immediately).
1369
1370fill_input_buffer (j_decompress_ptr cinfo)
1371 This is called whenever bytes_in_buffer has reached zero and more
1372 data is wanted. In typical applications, it should read fresh data
1373 into the buffer (ignoring the current state of next_input_byte and
1374 bytes_in_buffer), reset the pointer & count to the start of the
1375 buffer, and return TRUE indicating that the buffer has been reloaded.
1376 It is not necessary to fill the buffer entirely, only to obtain at
1377 least one more byte. bytes_in_buffer MUST be set to a positive value
1378 if TRUE is returned. A FALSE return should only be used when I/O
1379 suspension is desired (this mode is discussed in the next section).
1380
1381skip_input_data (j_decompress_ptr cinfo, long num_bytes)
1382 Skip num_bytes worth of data. The buffer pointer and count should
1383 be advanced over num_bytes input bytes, refilling the buffer as
1384 needed. This is used to skip over a potentially large amount of
1385 uninteresting data (such as an APPn marker). In some applications
1386 it may be possible to optimize away the reading of the skipped data,
1387 but it's not clear that being smart is worth much trouble; large
1388 skips are uncommon. bytes_in_buffer may be zero on return.
1389 A zero or negative skip count should be treated as a no-op.
1390
1391resync_to_restart (j_decompress_ptr cinfo)
1392 This routine is called only when the decompressor has failed to find
1393 a restart (RSTn) marker where one is expected. Its mission is to
1394 find a suitable point for resuming decompression. For most
1395 applications, we recommend that you just use the default resync
1396 procedure, jpeg_resync_to_restart(). However, if you are able to back
1397 up in the input data stream, or if you have a-priori knowledge about
1398 the likely location of restart markers, you may be able to do better.
1399 Read the read_restart_marker() and jpeg_resync_to_restart() routines
1400 in jdmarker.c if you think you'd like to implement your own resync
1401 procedure.
1402
1403term_source (j_decompress_ptr cinfo)
1404 Terminate source --- called by jpeg_finish_decompress() after all
1405 data has been read. Often a no-op.
1406
1407For both fill_input_buffer() and skip_input_data(), there is no such thing
1408as an EOF return. If the end of the file has been reached, the routine has
1409a choice of exiting via ERREXIT() or inserting fake data into the buffer.
1410In most cases, generating a warning message and inserting a fake EOI marker
1411is the best course of action --- this will allow the decompressor to output
1412however much of the image is there. In pathological cases, the decompressor
1413may swallow the EOI and again demand data ... just keep feeding it fake EOIs.
1414jdatasrc.c illustrates the recommended error recovery behavior.
1415
1416term_source() is NOT called by jpeg_abort() or jpeg_destroy(). If you want
1417the source manager to be cleaned up during an abort, you must do it yourself.
1418
1419You will also need code to create a jpeg_source_mgr struct, fill in its method
1420pointers, and insert a pointer to the struct into the "src" field of the JPEG
1421decompression object. This can be done in-line in your setup code if you
1422like, but it's probably cleaner to provide a separate routine similar to the
1423jpeg_stdio_src() routine of the supplied source manager.
1424
1425For more information, consult the stdio source and destination managers
1426in jdatasrc.c and jdatadst.c.
1427
1428
1429I/O suspension
1430--------------
1431
1432Some applications need to use the JPEG library as an incremental memory-to-
1433memory filter: when the compressed data buffer is filled or emptied, they want
1434control to return to the outer loop, rather than expecting that the buffer can
1435be flushed or reloaded within the data source/destination manager subroutine.
1436The library supports this need by providing an "I/O suspension" mode, which we
1437describe in this section.
1438
1439The I/O suspension mode is a limited solution: it works only in the simplest
1440operating modes (namely single-pass processing of single-scan JPEG files), and
1441it has several other restrictions which are documented below. Furthermore,
1442nothing is guaranteed about the maximum amount of time spent in any one call
1443to the library, so a single-threaded application may still have response-time
1444problems. If you need multi-pass processing or guaranteed response time, we
1445suggest you "bite the bullet" and implement a real multi-tasking capability.
1446
1447To use I/O suspension, cooperation is needed between the calling application
1448and the data source or destination manager; you will always need a custom
1449source/destination manager. (Please read the previous section if you haven't
1450already.) The basic idea is that the empty_output_buffer() or
1451fill_input_buffer() routine is a no-op, merely returning FALSE to indicate
1452that it has done nothing. Upon seeing this, the JPEG library suspends
1453operation and returns to its caller. The surrounding application is
1454responsible for emptying or refilling the work buffer before calling the JPEG
1455library again.
1456
1457Compression suspension:
1458
1459For compression suspension, use an empty_output_buffer() routine that
1460returns FALSE; typically it will not do anything else. This will cause the
1461compressor to return to the caller of jpeg_write_scanlines(), with the
1462return value indicating that not all the supplied scanlines have been
1463accepted. The application must make more room in the output buffer, adjust
1464the buffer pointer/count appropriately, and then call jpeg_write_scanlines()
1465again, pointing to the first unconsumed scanline.
1466
1467When forced to suspend, the compressor will backtrack to a convenient stopping
1468point (usually the start of the current MCU); it will regenerate some output
1469data when restarted. Therefore, although empty_output_buffer() is only called
1470when the buffer is filled, you should NOT dump out the entire buffer, only the
1471data up to the current position of next_output_byte/free_in_buffer. The data
1472beyond that point will be regenerated after resumption.
1473
1474Because of the backtracking behavior, a good-size output buffer is essential
1475for efficiency; you don't want the compressor to suspend often. (In fact, an
1476overly small buffer could lead to infinite looping, if a single MCU required
1477more data than would fit in the buffer.) We recommend a buffer of at least
1478several Kbytes. You may want to insert explicit code to ensure that you don't
1479call jpeg_write_scanlines() unless there is a reasonable amount of space in
1480the output buffer; in other words, flush the buffer before trying to compress
1481more data.
1482
1483The JPEG compressor does not support suspension while it is trying to write
1484JPEG markers at the beginning and end of the file. This means that
1485 * At the beginning of a compression operation, there must be enough free
1486 space in the output buffer to hold the header markers (typically 600 or
1487 so bytes). The recommended buffer size is bigger than this anyway, so
1488 this is not a problem as long as you start with an empty buffer. However,
1489 this restriction might catch you if you insert large special markers, such
1490 as a JFIF thumbnail image.
1491 * When you call jpeg_finish_compress(), there must be enough space in the
1492 output buffer to emit any buffered data and the final EOI marker. In the
1493 current implementation, half a dozen bytes should suffice for this, but
1494 for safety's sake we recommend ensuring that at least 100 bytes are free
1495 before calling jpeg_finish_compress().
1496Furthermore, since jpeg_finish_compress() cannot suspend, you cannot request
1497multi-pass operating modes such as Huffman code optimization or multiple-scan
1498output. That would imply that a large amount of data would be written inside
1499jpeg_finish_compress(), which would certainly trigger a buffer overrun.
1500
1501Decompression suspension:
1502
1503For decompression suspension, use a fill_input_buffer() routine that simply
1504returns FALSE (except perhaps during error recovery, as discussed below).
1505This will cause the decompressor to return to its caller with an indication
1506that suspension has occurred. This can happen at three places:
1507 * jpeg_read_header(): will return JPEG_SUSPENDED.
1508 * jpeg_read_scanlines(): will return the number of scanlines already
1509 completed (possibly 0).
1510 * jpeg_finish_decompress(): will return FALSE, rather than its usual TRUE.
1511The surrounding application must recognize these cases, load more data into
1512the input buffer, and repeat the call. In the case of jpeg_read_scanlines(),
1513adjust the passed pointers to reflect any scanlines successfully read.
1514
1515Just as with compression, the decompressor will typically backtrack to a
1516convenient restart point before suspending. The data beyond the current
1517position of next_input_byte/bytes_in_buffer must NOT be discarded; it will
1518be re-read upon resumption. In most implementations, you'll need to shift
1519this data down to the start of your work buffer and then load more data
1520after it. Again, this behavior means that a several-Kbyte work buffer is
1521essential for decent performance; furthermore, you should load a reasonable
1522amount of new data before resuming decompression. (If you loaded, say,
1523only one new byte each time around, you could waste a LOT of cycles.)
1524
1525The skip_input_data() source manager routine requires special care in a
1526suspension scenario. This routine is NOT granted the ability to suspend the
1527decompressor; it can decrement bytes_in_buffer to zero, but no more. If the
1528requested skip distance exceeds the amount of data currently in the input
1529buffer, then skip_input_data() must set bytes_in_buffer to zero and record the
1530additional skip distance somewhere else. The decompressor will immediately
1531call fill_input_buffer(), which will return FALSE, which will cause a
1532suspension return. The surrounding application must then arrange to discard
1533the right number of bytes before it resumes loading the input buffer. (Yes,
1534this design is rather baroque, but it avoids complexity in the far more common
1535case where a non-suspending source manager is used.)
1536
1537If the input data has been exhausted, we recommend that you emit a warning
1538and insert dummy EOI markers just as a non-suspending data source manager
1539would do. This can be handled either in the surrounding application logic or
1540within fill_input_buffer(); the latter is probably more efficient. If
1541fill_input_buffer() knows that no more data is available, it can set the
1542pointer/count to point to a dummy EOI marker and then return TRUE just as
1543though it had read more data in a non-suspending situation.
1544
1545The decompressor does not support suspension within jpeg_start_decompress().
1546This means that you cannot use suspension with any multi-pass processing mode
1547(eg, two-pass color quantization or multiple-scan JPEG files). In single-pass
1548modes, jpeg_start_decompress() reads no data and thus need never suspend.
1549
1550The decompressor does not attempt to suspend within any JPEG marker; it will
1551backtrack to the start of the marker. Hence the input buffer must be large
1552enough to hold the longest marker in the file. We recommend at least a 2K
1553buffer. The buffer would need to be 64K to allow for arbitrary COM or APPn
1554markers, but the decompressor does not actually try to read these; it just
1555skips them by calling skip_input_data(). If you provide a special marker
1556handling routine that does look at such markers, coping with buffer overflow
1557is your problem. Ordinary JPEG markers should normally not exceed a few
1558hundred bytes each (DHT tables are typically the longest). For robustness
1559against damaged marker length counts, you may wish to insert a test in your
1560application for the case that the input buffer is completely full and yet the
1561decoder has suspended without consuming any data --- otherwise, if this
1562situation did occur, it would lead to an endless loop.
1563
1564
1565Abbreviated datastreams and multiple images
1566-------------------------------------------
1567
1568A JPEG compression or decompression object can be reused to process multiple
1569images. This saves a small amount of time per image by eliminating the
1570"create" and "destroy" operations, but that isn't the real purpose of the
1571feature. Rather, reuse of an object provides support for abbreviated JPEG
1572datastreams. Object reuse can also simplify processing a series of images in
1573a single input or output file. This section explains these features.
1574
1575A JPEG file normally contains several hundred bytes worth of quantization
1576and Huffman tables. In a situation where many images will be stored or
1577transmitted with identical tables, this may represent an annoying overhead.
1578The JPEG standard therefore permits tables to be omitted. The standard
1579defines three classes of JPEG datastreams:
1580 * "Interchange" datastreams contain an image and all tables needed to decode
1581 the image. These are the usual kind of JPEG file.
1582 * "Abbreviated image" datastreams contain an image, but are missing some or
1583 all of the tables needed to decode that image.
1584 * "Abbreviated table specification" (henceforth "tables-only") datastreams
1585 contain only table specifications.
1586To decode an abbreviated image, it is necessary to load the missing table(s)
1587into the decoder beforehand. This can be accomplished by reading a separate
1588tables-only file. A variant scheme uses a series of images in which the first
1589image is an interchange (complete) datastream, while subsequent ones are
1590abbreviated and rely on the tables loaded by the first image. It is assumed
1591that once the decoder has read a table, it will remember that table until a
1592new definition for the same table number is encountered.
1593
1594It is the application designer's responsibility to figure out how to associate
1595the correct tables with an abbreviated image. While abbreviated datastreams
1596can be useful in a closed environment, their use is strongly discouraged in
1597any situation where data exchange with other applications might be needed.
1598Caveat designer.
1599
1600The JPEG library provides support for reading and writing any combination of
1601tables-only datastreams and abbreviated images. In both compression and
1602decompression objects, a quantization or Huffman table will be retained for
1603the lifetime of the object, unless it is overwritten by a new table definition.
1604
1605
1606To create abbreviated image datastreams, it is only necessary to tell the
1607compressor not to emit some or all of the tables it is using. Each
1608quantization and Huffman table struct contains a boolean field "sent_table",
1609which normally is initialized to FALSE. For each table used by the image, the
1610header-writing process emits the table and sets sent_table = TRUE unless it is
1611already TRUE. (In normal usage, this prevents outputting the same table
1612definition multiple times, as would otherwise occur because the chroma
1613components typically share tables.) Thus, setting this field to TRUE before
1614calling jpeg_start_compress() will prevent the table from being written at
1615all.
1616
1617If you want to create a "pure" abbreviated image file containing no tables,
1618just call "jpeg_suppress_tables(&cinfo, TRUE)" after constructing all the
1619tables. If you want to emit some but not all tables, you'll need to set the
1620individual sent_table fields directly.
1621
1622To create an abbreviated image, you must also call jpeg_start_compress()
1623with a second parameter of FALSE, not TRUE. Otherwise jpeg_start_compress()
1624will force all the sent_table fields to FALSE. (This is a safety feature to
1625prevent abbreviated images from being created accidentally.)
1626
1627To create a tables-only file, perform the same parameter setup that you
1628normally would, but instead of calling jpeg_start_compress() and so on, call
1629jpeg_write_tables(&cinfo). This will write an abbreviated datastream
1630containing only SOI, DQT and/or DHT markers, and EOI. All the quantization
1631and Huffman tables that are currently defined in the compression object will
1632be emitted unless their sent_tables flag is already TRUE, and then all the
1633sent_tables flags will be set TRUE.
1634
1635A sure-fire way to create matching tables-only and abbreviated image files
1636is to proceed as follows:
1637
1638 create JPEG compression object
1639 set JPEG parameters
1640 set destination to tables-only file
1641 jpeg_write_tables(&cinfo);
1642 set destination to image file
1643 jpeg_start_compress(&cinfo, FALSE);
1644 write data...
1645 jpeg_finish_compress(&cinfo);
1646
1647Since the JPEG parameters are not altered between writing the table file and
1648the abbreviated image file, the same tables are sure to be used. Of course,
1649you can repeat the jpeg_start_compress() ... jpeg_finish_compress() sequence
1650many times to produce many abbreviated image files matching the table file.
1651
1652You cannot suppress output of the computed Huffman tables when Huffman
1653optimization is selected. (If you could, there'd be no way to decode the
1654image...) Generally, you don't want to set optimize_coding = TRUE when
1655you are trying to produce abbreviated files.
1656
1657In some cases you might want to compress an image using tables which are
1658not stored in the application, but are defined in an interchange or
1659tables-only file readable by the application. This can be done by setting up
1660a JPEG decompression object to read the specification file, then copying the
1661tables into your compression object.
1662
1663
1664To read abbreviated image files, you simply need to load the proper tables
1665into the decompression object before trying to read the abbreviated image.
1666If the proper tables are stored in the application program, you can just
1667allocate the table structs and fill in their contents directly. More commonly
1668you'd want to read the tables from a tables-only file. The jpeg_read_header()
1669call is sufficient to read a tables-only file. You must pass a second
1670parameter of FALSE to indicate that you do not require an image to be present.
1671Thus, the typical scenario is
1672
1673 create JPEG decompression object
1674 set source to tables-only file
1675 jpeg_read_header(&cinfo, FALSE);
1676 set source to abbreviated image file
1677 jpeg_read_header(&cinfo, TRUE);
1678 set decompression parameters
1679 jpeg_start_decompress(&cinfo);
1680 read data...
1681 jpeg_finish_decompress(&cinfo);
1682
1683In some cases, you may want to read a file without knowing whether it contains
1684an image or just tables. In that case, pass FALSE and check the return value
1685from jpeg_read_header(): it will be JPEG_HEADER_OK if an image was found,
1686JPEG_HEADER_TABLES_ONLY if only tables were found. (A third return value,
1687JPEG_SUSPENDED, is possible when using a suspending data source manager.)
1688Note that jpeg_read_header() will not complain if you read an abbreviated
1689image for which you haven't loaded the missing tables; the missing-table check
1690occurs in jpeg_start_decompress().
1691
1692
1693It is possible to read a series of images from a single source file by
1694repeating the jpeg_read_header() ... jpeg_finish_decompress() sequence,
1695without releasing/recreating the JPEG object or the data source module.
1696(If you did reinitialize, any partial bufferload left in the data source
1697buffer at the end of one image would be discarded, causing you to lose the
1698start of the next image.) When you use this method, stored tables are
1699automatically carried forward, so some of the images can be abbreviated images
1700that depend on tables from earlier images.
1701
1702If you intend to write a series of images into a single destination file,
1703you might want to make a specialized data destination module that doesn't
1704flush the output buffer at term_destination() time. This would speed things
1705up by some trifling amount. Of course, you'd need to remember to flush the
1706buffer after the last image. You can make the later images be abbreviated
1707ones by passing FALSE to jpeg_start_compress().
1708
1709
1710Special markers
1711---------------
1712
1713Some applications may need to insert or extract special data in the JPEG
1714datastream. The JPEG standard provides marker types "COM" (comment) and
1715"APP0" through "APP15" (application) to hold application-specific data.
1716Unfortunately, the use of these markers is not specified by the standard.
1717COM markers are fairly widely used to hold user-supplied text. The JFIF file
1718format spec uses APP0 markers with specified initial strings to hold certain
1719data. Adobe applications use APP14 markers beginning with the string "Adobe"
1720for miscellaneous data. Other APPn markers are rarely seen, but might
1721contain almost anything.
1722
1723If you wish to store user-supplied text, we recommend you use COM markers
1724and place readable 7-bit ASCII text in them. Newline conventions are not
1725standardized --- expect to find LF (Unix style), CR/LF (DOS style), or CR
1726(Mac style). A robust COM reader should be able to cope with random binary
1727garbage, including nulls, since some applications generate COM markers
1728containing non-ASCII junk. (But yours should not be one of them.)
1729
1730For program-supplied data, use an APPn marker, and be sure to begin it with an
1731identifying string so that you can tell whether the marker is actually yours.
1732It's probably best to avoid using APP0 or APP14 for any private markers.
1733
1734Keep in mind that at most 65533 bytes can be put into one marker, but you
1735can have as many markers as you like.
1736
1737By default, the JPEG compression library will write a JFIF APP0 marker if the
1738selected JPEG colorspace is grayscale or YCbCr, or an Adobe APP14 marker if
1739the selected colorspace is RGB, CMYK, or YCCK. You can disable this, but
1740we don't recommend it. The decompression library will recognize JFIF and
1741Adobe markers and will set the JPEG colorspace properly when one is found.
1742
1743You can write special markers immediately following the datastream header by
1744calling jpeg_write_marker() after jpeg_start_compress() and before the first
1745call to jpeg_write_scanlines(). When you do this, the markers appear after
1746the SOI and the JFIF APP0 and Adobe APP14 markers (if written), but before
1747all else. Write the marker type parameter as "JPEG_COM" for COM or
1748"JPEG_APP0 + n" for APPn. (Actually, jpeg_write_marker will let you write
1749any marker type, but we don't recommend writing any other kinds of marker.)
1750For example, to write a user comment string pointed to by comment_text:
1751 jpeg_write_marker(cinfo, JPEG_COM, comment_text, strlen(comment_text));
1752Or if you prefer to synthesize the marker byte sequence yourself, you can
1753just cram it straight into the data destination module.
1754
1755For decompression, you can supply your own routine to process COM or APPn
1756markers by calling jpeg_set_marker_processor(). Usually you'd call this
1757after creating a decompression object and before calling jpeg_read_header(),
1758because the markers of interest will normally be scanned by jpeg_read_header.
1759Once you've supplied a routine, it will be used for the life of that
1760decompression object. A separate routine may be registered for COM and for
1761each APPn marker code.
1762
1763A marker processor routine must have the signature
1764 boolean jpeg_marker_parser_method (j_decompress_ptr cinfo)
1765Although the marker code is not explicitly passed, the routine can find it
1766in cinfo->unread_marker. At the time of call, the marker proper has been
1767read from the data source module. The processor routine is responsible for
1768reading the marker length word and the remaining parameter bytes, if any.
1769Return TRUE to indicate success. (FALSE should be returned only if you are
1770using a suspending data source and it tells you to suspend. See the standard
1771marker processors in jdmarker.c for appropriate coding methods if you need to
1772use a suspending data source.)
1773
1774If you override the default APP0 or APP14 processors, it is up to you to
1775recognize JFIF and Adobe markers if you want colorspace recognition to occur
1776properly. We recommend copying and extending the default processors if you
1777want to do that.
1778
1779A simple example of an external COM processor can be found in djpeg.c.
1780
1781
1782Raw (downsampled) image data
1783----------------------------
1784
1785Some applications need to supply already-downsampled image data to the JPEG
1786compressor, or to receive raw downsampled data from the decompressor. The
1787library supports this requirement by allowing the application to write or
1788read raw data, bypassing the normal preprocessing or postprocessing steps.
1789The interface is different from the standard one and is somewhat harder to
1790use. If your interest is merely in bypassing color conversion, we recommend
1791that you use the standard interface and simply set jpeg_color_space =
1792in_color_space (or jpeg_color_space = out_color_space for decompression).
1793The mechanism described in this section is necessary only to supply or
1794receive downsampled image data, in which not all components have the same
1795dimensions.
1796
1797
1798To compress raw data, you must supply the data in the colorspace to be used
1799in the JPEG file (please read the earlier section on Special color spaces)
1800and downsampled to the sampling factors specified in the JPEG parameters.
1801You must supply the data in the format used internally by the JPEG library,
1802namely a JSAMPIMAGE array. This is an array of pointers to two-dimensional
1803arrays, each of type JSAMPARRAY. Each 2-D array holds the values for one
1804color component. This structure is necessary since the components are of
1805different sizes. If the image dimensions are not a multiple of the MCU size,
1806you must also pad the data correctly (usually, this is done by replicating
1807the last column and/or row). The data must be padded to a multiple of a DCT
1808block in each component: that is, each downsampled row must contain a
1809multiple of 8 valid samples, and there must be a multiple of 8 sample rows
1810for each component. (For applications such as conversion of digital TV
1811images, the standard image size is usually a multiple of the DCT block size,
1812so that no padding need actually be done.)
1813
1814The procedure for compression of raw data is basically the same as normal
1815compression, except that you call jpeg_write_raw_data() in place of
1816jpeg_write_scanlines(). Before calling jpeg_start_compress(), you must do
1817the following:
1818 * Set cinfo->raw_data_in to TRUE. (It is set FALSE by jpeg_set_defaults().)
1819 This notifies the library that you will be supplying raw data.
1820 * Ensure jpeg_color_space is correct --- an explicit jpeg_set_colorspace()
1821 call is a good idea. Note that since color conversion is bypassed,
1822 in_color_space is ignored, except that jpeg_set_defaults() uses it to
1823 choose the default jpeg_color_space setting.
1824 * Ensure the sampling factors, cinfo->comp_info[i].h_samp_factor and
1825 cinfo->comp_info[i].v_samp_factor, are correct. Since these indicate the
1826 dimensions of the data you are supplying, it's wise to set them
1827 explicitly, rather than assuming the library's defaults are what you want.
1828
1829To pass raw data to the library, call jpeg_write_raw_data() in place of
1830jpeg_write_scanlines(). The two routines work similarly except that
1831jpeg_write_raw_data takes a JSAMPIMAGE data array rather than JSAMPARRAY.
1832The scanlines count passed to and returned from jpeg_write_raw_data is
1833measured in terms of the component with the largest v_samp_factor.
1834
1835jpeg_write_raw_data() processes one MCU row per call, which is to say
1836v_samp_factor*DCTSIZE sample rows of each component. The passed num_lines
1837value must be at least max_v_samp_factor*DCTSIZE, and the return value will
1838be exactly that amount (or possibly some multiple of that amount, in future
1839library versions). This is true even on the last call at the bottom of the
1840image; don't forget to pad your data as necessary.
1841
1842The required dimensions of the supplied data can be computed for each
1843component as
1844 cinfo->comp_info[i].width_in_blocks*DCTSIZE samples per row
1845 cinfo->comp_info[i].height_in_blocks*DCTSIZE rows in image
1846after jpeg_start_compress() has initialized those fields. If the valid data
1847is smaller than this, it must be padded appropriately. For some sampling
1848factors and image sizes, additional dummy DCT blocks are inserted to make
1849the image a multiple of the MCU dimensions. The library creates such dummy
1850blocks itself; it does not read them from your supplied data. Therefore you
1851need never pad by more than DCTSIZE samples. An example may help here.
1852Assume 2h2v downsampling of YCbCr data, that is
1853 cinfo->comp_info[0].h_samp_factor = 2 for Y
1854 cinfo->comp_info[0].v_samp_factor = 2
1855 cinfo->comp_info[1].h_samp_factor = 1 for Cb
1856 cinfo->comp_info[1].v_samp_factor = 1
1857 cinfo->comp_info[2].h_samp_factor = 1 for Cr
1858 cinfo->comp_info[2].v_samp_factor = 1
1859and suppose that the nominal image dimensions (cinfo->image_width and
1860cinfo->image_height) are 101x101 pixels. Then jpeg_start_compress() will
1861compute downsampled_width = 101 and width_in_blocks = 13 for Y,
1862downsampled_width = 51 and width_in_blocks = 7 for Cb and Cr (and the same
1863for the height fields). You must pad the Y data to at least 13*8 = 104
1864columns and rows, the Cb/Cr data to at least 7*8 = 56 columns and rows. The
1865MCU height is max_v_samp_factor = 2 DCT rows so you must pass at least 16
1866scanlines on each call to jpeg_write_raw_data(), which is to say 16 actual
1867sample rows of Y and 8 each of Cb and Cr. A total of 7 MCU rows are needed,
1868so you must pass a total of 7*16 = 112 "scanlines". The last DCT block row
1869of Y data is dummy, so it doesn't matter what you pass for it in the data
1870arrays, but the scanlines count must total up to 112 so that all of the Cb
1871and Cr data gets passed.
1872
1873Currently, output suspension is not supported with raw data output: an error
1874will result if the data destination module tries to suspend.
1875
1876
1877Decompression with raw data output implies bypassing all postprocessing:
1878you cannot ask for color quantization, for instance. More seriously, you must
1879deal with the color space and sampling factors present in the incoming file.
1880If your application only handles, say, 2h1v YCbCr data, you must check for
1881and fail on other color spaces or other sampling factors.
1882
1883To obtain raw data output, set cinfo->raw_data_out = TRUE before
1884jpeg_start_decompress() (it is set FALSE by jpeg_read_header()). Be sure to
1885verify that the color space and sampling factors are ones you can handle.
1886Then call jpeg_read_raw_data() in place of jpeg_read_scanlines(). The
1887decompression process is otherwise the same as usual.
1888
1889jpeg_read_raw_data() returns one MCU row per call, and thus you must pass a
1890buffer of at least max_v_samp_factor*DCTSIZE scanlines (scanline counting is
1891the same as for raw-data compression). The buffer you pass must be large
1892enough to hold the actual data plus padding to DCT-block boundaries. As with
1893compression, any entirely dummy DCT blocks are not processed so you need not
1894allocate space for them, but the total scanline count includes them. The
1895above example of computing buffer dimensions for raw-data compression is
1896equally valid for decompression.
1897
1898Input suspension is supported with raw-data decompression: if the data source
1899module suspends, jpeg_read_raw_data() will return 0.
1900
1901
1902Progress monitoring
1903-------------------
1904
1905Some applications may need to regain control from the JPEG library every so
1906often. The typical use of this feature is to produce a percent-done bar or
1907other progress display. (For a simple example, see cjpeg.c or djpeg.c.)
1908Although you do get control back frequently during the data-transferring pass
1909(the jpeg_read_scanlines or jpeg_write_scanlines loop), any additional passes
1910will occur inside jpeg_finish_compress or jpeg_start_decompress; those
1911routines may take a long time to execute, and you don't get control back
1912until they are done.
1913
1914You can define a progress-monitor routine which will be called periodically
1915by the library. No guarantees are made about how often this call will occur,
1916so we don't recommend you use it for mouse tracking or anything like that.
1917At present, a call will occur once per MCU row, scanline, or sample row
1918group, whichever unit is convenient for the current processing mode; so the
1919wider the image, the longer the time between calls. (During the data
1920transferring pass, only one call occurs per call of jpeg_read_scanlines or
1921jpeg_write_scanlines, so don't pass a large number of scanlines at once if
1922you want fine resolution in the progress count.)
1923
1924To establish a progress-monitor callback, create a struct jpeg_progress_mgr,
1925fill in its progress_monitor field with a pointer to your callback routine,
1926and set cinfo->progress to point to the struct. The callback will be called
1927whenever cinfo->progress is non-NULL. (This pointer is set to NULL by
1928jpeg_create_compress or jpeg_create_decompress; the library will not change
1929it thereafter. So if you allocate dynamic storage for the progress struct,
1930make sure it will live as long as the JPEG object does. Allocating from the
1931JPEG memory manager with lifetime JPOOL_PERMANENT will work nicely.) You
1932can use the same callback routine for both compression and decompression.
1933
1934The jpeg_progress_mgr struct contains four fields which are set by the library:
1935 long pass_counter; /* work units completed in this pass */
1936 long pass_limit; /* total number of work units in this pass */
1937 int completed_passes; /* passes completed so far */
1938 int total_passes; /* total number of passes expected */
1939During any one pass, pass_counter increases from 0 up to (not including)
1940pass_limit; the step size is not necessarily 1. Both the step size and the
1941limit may differ from one pass to another. The expected total number of
1942passes is in total_passes, and the number of passes already completed is in
1943completed_passes. Thus the fraction of work completed may be estimated as
1944 completed_passes + (pass_counter/pass_limit)
1945 --------------------------------------------
1946 total_passes
1947ignoring the fact that the passes may not be equal amounts of work.
1948
1949When decompressing, the total_passes value is not trustworthy, because it
1950depends on the number of scans in the JPEG file, which isn't always known in
1951advance. In the current implementation, completed_passes may jump by more
1952than one when dealing with a multiple-scan input file. About all that is
1953really safe to assume is that when completed_passes = total_passes - 1, the
1954current pass will be the last one.
1955
1956If you really need to use the callback mechanism for time-critical tasks
1957like mouse tracking, you could insert additional calls inside some of the
1958library's inner loops.
1959
1960
1961Memory management
1962-----------------
1963
1964This section covers some key facts about the JPEG library's built-in memory
1965manager. For more info, please read structure.doc's section about the memory
1966manager, and consult the source code if necessary.
1967
1968All memory and temporary file allocation within the library is done via the
1969memory manager. If necessary, you can replace the "back end" of the memory
1970manager to control allocation yourself (for example, if you don't want the
1971library to use malloc() and free() for some reason).
1972
1973Some data is allocated "permanently" and will not be freed until the JPEG
1974object is destroyed. Most data is allocated "per image" and is freed by
1975jpeg_finish_compress, jpeg_finish_decompress, or jpeg_abort. You can call the
1976memory manager yourself to allocate structures that will automatically be
1977freed at these times. Typical code for this is
1978 ptr = (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, size);
1979Use JPOOL_PERMANENT to get storage that lasts as long as the JPEG object.
1980Use alloc_large instead of alloc_small for anything bigger than a few Kbytes.
1981There are also alloc_sarray and alloc_barray routines that automatically
1982build 2-D sample or block arrays.
1983
1984The library's minimum space requirements to process an image depend on the
1985image's width, but not on its height, because the library ordinarily works
1986with "strip" buffers that are as wide as the image but just a few rows high.
1987Some operating modes (eg, two-pass color quantization) require full-image
1988buffers. Such buffers are treated as "virtual arrays": only the current strip
1989need be in memory, and the rest can be swapped out to a temporary file.
1990
1991If you use the simplest memory manager back end (jmemnobs.c), then no
1992temporary files are used; virtual arrays are simply malloc()'d. Images bigger
1993than memory can be processed only if your system supports virtual memory.
1994The other memory manager back ends support temporary files of various flavors
1995and thus work in machines without virtual memory. They may also be useful on
1996Unix machines if you need to process images that exceed available swap space.
1997
1998When using temporary files, the library will make the in-memory buffers for
1999its virtual arrays just big enough to stay within a "maximum memory" setting.
2000Your application can set this limit by setting cinfo->mem->max_memory_to_use
2001after creating the JPEG object. (Of course, there is still a minimum size for
2002the buffers, so the max-memory setting is effective only if it is bigger than
2003the minimum space needed.) If you allocate any large structures yourself, you
2004must allocate them before jpeg_start_compress() or jpeg_start_decompress() in
2005order to have them counted against the max memory limit. Also keep in mind
2006that space allocated with alloc_small() is ignored, on the assumption that
2007it's too small to be worth worrying about.
2008
2009If you use the jmemname.c or jmemdos.c memory manager back end, it is
2010important to clean up the JPEG object properly to ensure that the temporary
2011files get deleted. (This is especially crucial with jmemdos.c, where the
2012"temporary files" may be extended-memory segments; if they are not freed,
2013DOS will require a reboot to recover the memory.) Thus, with these memory
2014managers, it's a good idea to provide a signal handler that will trap any
2015early exit from your program. The handler should call either jpeg_abort()
2016or jpeg_destroy() for any active JPEG objects. A handler is not needed with
2017jmemnobs.c, and shouldn't be necessary with jmemansi.c either, since the C
2018library is supposed to take care of deleting files made with tmpfile().
2019
2020
2021Library compile-time options
2022----------------------------
2023
2024A number of compile-time options are available by modifying jmorecfg.h.
2025
2026The JPEG standard provides for both the baseline 8-bit DCT process and
2027a 12-bit DCT process. 12-bit lossy JPEG is supported if you define
2028BITS_IN_JSAMPLE as 12 rather than 8. Note that this causes JSAMPLE to be
2029larger than a char, so it affects the surrounding application's image data.
2030At present, a 12-bit library can handle *only* 12-bit images, not both
2031precisions. (If you need to include both 8- and 12-bit libraries in a
2032single application, you could probably do it by defining
2033NEED_SHORT_EXTERNAL_NAMES for just one of the copies. You'd have to access
2034the 8-bit and 12-bit copies from separate application source files. This is
2035untested ... if you try it, we'd like to hear whether it works!)
2036
2037The maximum number of components (color channels) in the image is determined
2038by MAX_COMPONENTS. The JPEG standard allows up to 255 components, but we
2039expect that few applications will need more than four or so.
2040
2041On machines with unusual data type sizes, you may be able to improve
2042performance or reduce memory space by tweaking the various typedefs in
2043jmorecfg.h. In particular, on some RISC CPUs, access to arrays of "short"s
2044is quite slow; consider trading memory for speed by making JCOEF, INT16, and
2045UINT16 be "int" or "unsigned int". UINT8 is also a candidate to become int.
2046You probably don't want to make JSAMPLE be int unless you have lots of memory
2047to burn.
2048
2049You can reduce the size of the library by compiling out various optional
2050functions. To do this, undefine xxx_SUPPORTED symbols as necessary.
2051
2052
2053Portability considerations
2054--------------------------
2055
2056The JPEG library has been written to be extremely portable; the sample
2057applications cjpeg and djpeg are slightly less so. This section summarizes
2058the design goals in this area. (If you encounter any bugs that cause the
2059library to be less portable than is claimed here, we'd appreciate hearing
2060about them.)
2061
2062The code works fine on both ANSI and pre-ANSI C compilers, using any of the
2063popular system include file setups, and some not-so-popular ones too. See
2064install.doc for configuration procedures.
2065
2066The code is not dependent on the exact sizes of the C data types. As
2067distributed, we make the assumptions that
2068 char is at least 8 bits wide
2069 short is at least 16 bits wide
2070 int is at least 16 bits wide
2071 long is at least 32 bits wide
2072(These are the minimum requirements of the ANSI C standard.) Wider types will
2073work fine, although memory may be used inefficiently if char is much larger
2074than 8 bits or short is much bigger than 16 bits. The code should work
2075equally well with 16- or 32-bit ints.
2076
2077In a system where these assumptions are not met, you may be able to make the
2078code work by modifying the typedefs in jmorecfg.h. However, you will probably
2079have difficulty if int is less than 16 bits wide, since references to plain
2080int abound in the code.
2081
2082char can be either signed or unsigned, although the code runs faster if an
2083unsigned char type is available. If char is wider than 8 bits, you will need
2084to redefine JOCTET and/or provide custom data source/destination managers so
2085that JOCTET represents exactly 8 bits of data on external storage.
2086
2087The JPEG library proper does not assume ASCII representation of characters.
2088But some of the image file I/O modules in cjpeg/djpeg do have ASCII
2089dependencies in file-header manipulation; so does cjpeg's select_file_type()
2090routine.
2091
2092The JPEG library does not rely heavily on the C library. In particular, C
2093stdio is used only by the data source/destination modules and the error
2094handler, all of which are application-replaceable. (cjpeg/djpeg are more
2095heavily dependent on stdio.) malloc and free are called only from the memory
2096manager "back end" module, so you can use a different memory allocator by
2097replacing that one file.
2098
2099The code generally assumes that C names must be unique in the first 15
2100characters. However, global function names can be made unique in the
2101first 6 characters by defining NEED_SHORT_EXTERNAL_NAMES.
2102
2103More info about porting the code may be gleaned by reading jconfig.doc,
2104jmorecfg.h, and jinclude.h.
2105
2106
2107Notes for MS-DOS implementors
2108-----------------------------
2109
2110The IJG code is designed to work efficiently in 80x86 "small" or "medium"
2111memory models (i.e., data pointers are 16 bits unless explicitly declared
2112"far"; code pointers can be either size). You may be able to use small
2113model to compile cjpeg or djpeg by itself, but you will probably have to use
2114medium model for any larger application. This won't make much difference in
2115performance. You *will* take a noticeable performance hit if you use a
2116large-data memory model (perhaps 10%-25%), and you should avoid "huge" model
2117if at all possible.
2118
2119The JPEG library typically needs 2Kb-3Kb of stack space. It will also
2120malloc about 20K-30K of near heap space while executing (and lots of far
2121heap, but that doesn't count in this calculation). This figure will vary
2122depending on selected operating mode, and to a lesser extent on image size.
2123Thus you have perhaps 25K available for static data and other modules' near
2124heap space before you need to go to a larger memory model. The C library's
2125static data will account for several K of this, but that still leaves a good
2126deal for your needs. (If you are tight on space, you could reduce the sizes
2127of the I/O buffers allocated by jdatasrc.c and jdatadst.c, say from 4K to
21281K.)
2129
2130About 2K of the near heap space is "permanent" memory that will not be
2131released until you destroy the JPEG object. This is only an issue if you
2132save a JPEG object between compression or decompression operations.
2133
2134Far data space may also be a tight resource when you are dealing with large
2135images. The most memory-intensive case is decompression with two-pass color
2136quantization, or single-pass quantization to an externally supplied color
2137map. This requires a 128Kb color lookup table plus strip buffers amounting
2138to about 50 bytes per column for typical sampling ratios (eg, about 32000
2139bytes for a 640-pixel-wide image). You may not be able to process wide
2140images if you have large data structures of your own.
2141
2142Of course, all of these concerns vanish if you use a 32-bit flat-memory-model
2143compiler, such as DJGPP or Watcom C. We highly recommend flat model if you
2144can use it; the JPEG library is significantly faster in flat model.