home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume44 / jpeg / part04 / libjpeg.doc.A < prev   
Encoding:
Text File  |  1994-09-26  |  48.2 KB  |  1,074 lines

  1. USING THE IJG JPEG LIBRARY
  2.  
  3. Copyright (C) 1994, Thomas G. Lane.
  4. This file is part of the Independent JPEG Group's software.
  5. For conditions of distribution and use, see the accompanying README file.
  6.  
  7.  
  8. This file describes how to use the IJG JPEG library within an application
  9. program.  Read it if you want to write a program that uses the library.
  10.  
  11. The file example.c provides heavily commented skeleton code for calling the
  12. JPEG library.  Also see jpeglib.h (the include file to be used by application
  13. programs) for full details about data structures and function parameter lists.
  14. The library source code, of course, is the ultimate reference.
  15.  
  16. Note that there have been *major* changes from the application interface
  17. presented by IJG version 4 and earlier versions.  The old design had several
  18. inherent limitations, and it had accumulated a lot of cruft as we added
  19. features while trying to minimize application-interface changes.  We have
  20. sacrificed backward compatibility in the version 5 rewrite, but we think the
  21. improvements justify this.
  22.  
  23.  
  24. TABLE OF CONTENTS
  25. -----------------
  26.  
  27. Overview:
  28.     Functions provided by the library
  29.     Outline of typical usage
  30. Basic library usage:
  31.     Data formats
  32.     Compression details
  33.     Decompression details
  34.     Mechanics of usage: include files, linking, etc
  35. Advanced 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.  
  51. You should read at least the overview and basic usage sections before trying
  52. to program with the library.  The sections on advanced features can be read
  53. if and when you need them.
  54.  
  55.  
  56. OVERVIEW
  57. ========
  58.  
  59. Functions provided by the library
  60. ---------------------------------
  61.  
  62. The IJG JPEG library provides C code to read and write JPEG-compressed image
  63. files.  The surrounding application program receives or supplies image data a
  64. scanline at a time, using a straightforward uncompressed image format.  All
  65. details of color conversion and other preprocessing/postprocessing can be
  66. handled by the library.
  67.  
  68. The library includes a substantial amount of code that is not covered by the
  69. JPEG standard but is necessary for typical applications of JPEG.  These
  70. functions preprocess the image before JPEG compression or postprocess it after
  71. decompression.  They include colorspace conversion, downsampling/upsampling,
  72. and color quantization.  The application indirectly selects use of this code
  73. by specifying the format in which it wishes to supply or receive image data.
  74. For example, if colormapped output is requested, then the decompression
  75. library automatically invokes color quantization.
  76.  
  77. A wide range of quality vs. speed tradeoffs are possible in JPEG processing,
  78. and even more so in decompression postprocessing.  The decompression library
  79. provides multiple implementations that cover most of the useful tradeoffs,
  80. ranging from very-high-quality down to fast-preview operation.  On the
  81. compression side we have generally not provided low-quality choices, since
  82. compression is normally less time-critical.  It should be understood that the
  83. low-quality modes may not meet the JPEG standard's accuracy requirements;
  84. nonetheless, they are useful for viewers.
  85.  
  86. A word about functions *not* provided by the library.  We handle a subset of
  87. the ISO JPEG standard; most baseline and extended-sequential JPEG processes
  88. are supported.  (Our subset includes all features now in common use.)
  89. Unsupported 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
  96. We support both 8- and 12-bit data precision, but this is a compile-time
  97. choice rather than a run-time choice; hence it is difficult to use both
  98. precisions in a single application.
  99.  
  100. By itself, the library handles only interchange JPEG datastreams --- in
  101. particular the widely used JFIF file format.  The library can be used by
  102. surrounding code to process interchange or abbreviated JPEG datastreams that
  103. are embedded in more complex file formats.  (For example, we anticipate that
  104. Sam Leffler's LIBTIFF library will use this code to support the revised TIFF
  105. JPEG format.)
  106.  
  107.  
  108. Outline of typical usage
  109. ------------------------
  110.  
  111. The 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.  
  122. A JPEG compression object holds parameters and working state for the JPEG
  123. library.  We make creation/destruction of the object separate from starting
  124. or finishing compression of an image; the same object can be re-used for a
  125. series of image compression operations.  This makes it easy to re-use the
  126. same parameter settings for a sequence of images.  Re-use of a JPEG object
  127. also has important implications for processing abbreviated JPEG datastreams,
  128. as discussed later.
  129.  
  130. The image data to be compressed is supplied to jpeg_write_scanlines() from
  131. in-memory buffers.  If the application is doing file-to-file compression,
  132. reading image data from the source file is the application's responsibility.
  133. The library emits compressed data by calling a "data destination manager",
  134. which typically will write the data into a file; but the application can
  135. provide its own destination manager to do something else.
  136.  
  137. Similarly, 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.  
  149. This is comparable to the compression outline except that reading the
  150. datastream header is a separate step.  This is helpful because information
  151. about the image's size, colorspace, etc is available when the application
  152. selects decompression parameters.  For example, the application can choose an
  153. output scaling ratio that will fit the image into the available screen size.
  154.  
  155. The decompression library obtains compressed data by calling a data source
  156. manager, which typically will read the data from a file; but other behaviors
  157. can be obtained with a custom source manager.  Decompressed data is delivered
  158. into in-memory buffers passed to jpeg_read_scanlines().
  159.  
  160. It is possible to abort an incomplete compression or decompression operation
  161. by calling jpeg_abort(); or, if you do not need to retain the JPEG object,
  162. simply release it by calling jpeg_destroy().
  163.  
  164. JPEG compression and decompression objects are two separate struct types.
  165. However, they share some common fields, and certain routines such as
  166. jpeg_destroy() can work on either type of object.
  167.  
  168. The JPEG library has no static variables: all state is in the compression
  169. or decompression object.  Therefore it is possible to process multiple
  170. compression and decompression operations concurrently, using multiple JPEG
  171. objects.
  172.  
  173. Both compression and decompression can be done in an incremental memory-to-
  174. memory fashion, if suitable source/destination managers are used.  However,
  175. there are some restrictions on the processing that can be done in this mode.
  176. See the section on "I/O suspension" for more details.
  177.  
  178.  
  179. BASIC LIBRARY USAGE
  180. ===================
  181.  
  182. Data formats
  183. ------------
  184.  
  185. Before diving into procedural details, it is helpful to understand the
  186. image data format that the JPEG library expects or returns.
  187.  
  188. The standard input image format is a rectangular array of pixels, with each
  189. pixel having the same number of "component" values (color channels).  You
  190. must specify how many components there are and the colorspace interpretation
  191. of the components.  Most applications will use RGB data (three components
  192. per pixel) or grayscale data (one component per pixel).
  193.  
  194. Note that there is no provision for colormapped input.  You can feed in a
  195. colormapped image by expanding it to full-color format.  However JPEG often
  196. doesn't work very well with colormapped source data, because of dithering
  197. noise.  This is discussed in more detail in the JPEG FAQ and the other
  198. references mentioned in the README file.
  199.  
  200. Pixels are stored by scanlines, with each scanline running from left to
  201. right.  The component values for each pixel are adjacent in the row; for
  202. example, R,G,B,R,G,B,R,G,B,... for 24-bit RGB color.  Each scanline is an
  203. array of data type JSAMPLE --- which is typically "unsigned char", unless
  204. you've changed jmorecfg.h.  (You can also change the RGB pixel layout, say
  205. to B,G,R order, by modifying jmorecfg.h.  But see the restrictions listed in
  206. that file before doing so.)
  207.  
  208. A 2-D array of pixels is formed by making a list of pointers to the starts of
  209. scanlines; so the scanlines need not be physically adjacent in memory.  Even
  210. if you process just one scanline at a time, you must make a one-element
  211. pointer array to serve this purpose.  Pointers to JSAMPLE rows are of type
  212. JSAMPROW, and the pointer to the pointer array is of type JSAMPARRAY.
  213.  
  214. The library accepts or supplies one or more complete scanlines per call.
  215. It is not possible to process part of a row at a time.  Scanlines are always
  216. processed top-to-bottom.  You can process an entire image in one call if you
  217. have it all in memory, but usually it's simplest to process one scanline at
  218. a time.
  219.  
  220. For best results, source data values should have the precision specified by
  221. BITS_IN_JSAMPLE (normally 8 bits).  For instance, if you choose to compress
  222. data that's only 6 bits/channel, you should left-justify each value in a
  223. byte before passing it to the compressor.  If you need to compress data
  224. that has more than 8 bits/channel, compile with BITS_IN_JSAMPLE = 12.
  225. (See "Library compile-time options", later.)
  226.  
  227. The data format returned by the decompressor is the same in all details,
  228. except that colormapped data is supported.  If you request colormapped
  229. output then the returned data array contains a single JSAMPLE per pixel;
  230. its value is an index into a color map.  The color map is represented as
  231. a 2-D JSAMPARRAY in which each row holds the values of one color component,
  232. that is, colormap[i][j] is the value of the i'th color component for pixel
  233. value (map index) j.  Note that since the colormap indexes are stored in
  234. JSAMPLEs, 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.  
  238. Compression details
  239. -------------------
  240.  
  241. Here we revisit the JPEG compression outline given in the overview.
  242.  
  243. 1. Allocate and initialize a JPEG compression object.
  244.  
  245. A JPEG compression object is a "struct jpeg_compress_struct" (plus a bunch of
  246. subsidiary structures which are allocated via malloc(), but the application
  247. doesn't control those directly).  This struct can be just a local variable in
  248. the calling routine, if a single routine is going to execute the whole JPEG
  249. compression sequence.  Otherwise it can be static or allocated from malloc().
  250.  
  251. You will also need a structure representing a JPEG error handler.  The part
  252. of this that the library cares about is a "struct jpeg_error_mgr".  If you
  253. are providing your own error handler, you'll typically want to embed the
  254. jpeg_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
  256. handler.  The default error handler will print JPEG error/warning messages
  257. on stderr, and it will call exit() if a fatal error occurs.
  258.  
  259. You must initialize the error handler structure, store a pointer to it into
  260. the JPEG object's "err" field, and then call jpeg_create_compress() to
  261. initialize the rest of the JPEG object.
  262.  
  263. Typical 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.  
  271. jpeg_create_compress allocates a small amount of memory, so it could fail
  272. if you are out of memory.  In that case it will exit via the error handler;
  273. that's why the error handler must be initialized first.
  274.  
  275.  
  276. 2. Specify the destination for the compressed data (eg, a file).
  277.  
  278. As previously mentioned, the JPEG library delivers compressed data to a
  279. "data destination" module.  The library includes one data destination
  280. module which knows how to write to a stdio stream.  You can use your own
  281. destination module if you want to do something else, as discussed later.
  282.  
  283. If you use the standard destination module, you must open the target stdio
  284. stream 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.  
  294. where the last line invokes the standard destination module.
  295.  
  296. WARNING: it is critical that the binary compressed data be delivered to the
  297. output file unchanged.  On non-Unix systems the stdio library may perform
  298. newline translation or otherwise corrupt binary data.  To suppress this
  299. behavior, you may need to use a "b" option to fopen (as shown above), or use
  300. setmode() or another routine to put the stdio stream in binary mode.  See
  301. cjpeg.c and djpeg.c for code that has been found to work on many systems.
  302.  
  303. You can select the data destination after setting other parameters (step 3),
  304. if that's more convenient.  You may not change the destination between
  305. calling jpeg_start_compress() and jpeg_finish_compress().
  306.  
  307.  
  308. 3. Set parameters for compression, including image size & colorspace.
  309.  
  310. You must supply information about the source image by setting the following
  311. fields 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.  
  318. The image dimensions are, hopefully, obvious.  JPEG supports image dimensions
  319. of 1 to 64K pixels in either direction.  The input color space is typically
  320. RGB or grayscale, and input_components is 3 or 1 accordingly.  (See "Special
  321. color spaces", later, for more info.)  The in_color_space field must be
  322. assigned one of the J_COLOR_SPACE enum constants, typically JCS_RGB or
  323. JCS_GRAYSCALE.
  324.  
  325. JPEG has a large number of compression parameters that determine how the
  326. image is encoded.  Most applications don't need or want to know about all
  327. these parameters.  You can set all the parameters to reasonable defaults by
  328. calling jpeg_set_defaults(); then, if there are particular values you want
  329. to change, you can do so after that.  The "Compression parameter selection"
  330. section tells about all the parameters.
  331.  
  332. You must set in_color_space correctly before calling jpeg_set_defaults(),
  333. because the defaults depend on the source image colorspace.  However the
  334. other three source image parameters need not be valid until you call
  335. jpeg_start_compress().  There's no harm in calling jpeg_set_defaults() more
  336. than once, if that happens to be convenient.
  337.  
  338. Typical 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.  
  349. 4. jpeg_start_compress(...);
  350.  
  351. After you have established the data destination and set all the necessary
  352. source image info and other parameters, call jpeg_start_compress() to begin
  353. a compression cycle.  This will initialize internal state, allocate working
  354. storage, and emit the first few bytes of the JPEG datastream header.
  355.  
  356. Typical code:
  357.  
  358.     jpeg_start_compress(&cinfo, TRUE);
  359.  
  360. The "TRUE" parameter ensures that a complete JPEG interchange datastream
  361. will be written.  This is appropriate in most cases.  If you think you might
  362. want to use an abbreviated datastream, read the section on abbreviated
  363. datastreams, below.
  364.  
  365. Once you have called jpeg_start_compress(), you may not alter any JPEG
  366. parameters or other fields of the JPEG object until you have completed
  367. the compression cycle.
  368.  
  369.  
  370. 5. while (scan lines remain to be written)
  371.     jpeg_write_scanlines(...);
  372.  
  373. Now write all the required image data by calling jpeg_write_scanlines()
  374. one or more times.  You can pass one or more scanlines in each call, up
  375. to the total image height.  In most applications it is convenient to pass
  376. just one or a few scanlines at a time.  The expected format for the passed
  377. data is discussed under "Data formats", above.
  378.  
  379. Image data should be written in top-to-bottom scanline order.  The JPEG spec
  380. contains some weasel wording about how top and bottom are application-defined
  381. terms (a curious interpretation of the English language...) but if you want
  382. your files to be compatible with everyone else's, you WILL use top-to-bottom
  383. order.  If the source data must be read in bottom-to-top order, you can use
  384. the JPEG library's virtual array mechanism to invert the data efficiently.
  385. Examples of this can be found in the sample application cjpeg.
  386.  
  387. The library maintains a count of the number of scanlines written so far
  388. in the next_scanline field of the JPEG object.  Usually you can just use
  389. this variable as the loop counter, so that the loop test looks like
  390. "while (cinfo.next_scanline < cinfo.image_height)".
  391.  
  392. Code for this step depends heavily on the way that you store the source data.
  393. example.c shows the following code for the case of a full-size 2-D source
  394. array 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.  
  406. jpeg_write_scanlines() returns the number of scanlines actually written.
  407. This will normally be equal to the number passed in, so you can usually
  408. ignore 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.
  415. In any case, the return value is the same as the change in the value of
  416. next_scanline.
  417.  
  418.  
  419. 6. jpeg_finish_compress(...);
  420.  
  421. After all the image data has been written, call jpeg_finish_compress() to
  422. complete the compression cycle.  This step is ESSENTIAL to ensure that the
  423. last bufferload of data is written to the data destination.
  424. jpeg_finish_compress() also releases working memory associated with the JPEG
  425. object.
  426.  
  427. Typical code:
  428.  
  429.     jpeg_finish_compress(&cinfo);
  430.  
  431. If using the stdio destination manager, don't forget to close the output
  432. stdio stream if necessary.
  433.  
  434. If you have requested a multi-pass operating mode, such as Huffman code
  435. optimization, jpeg_finish_compress() will perform the additional passes using
  436. data buffered by the first pass.  In this case jpeg_finish_compress() may take
  437. quite a while to complete.  With the default compression parameters, this will
  438. not happen.
  439.  
  440. It is an error to call jpeg_finish_compress() before writing the necessary
  441. total number of scanlines.  If you wish to abort compression, call
  442. jpeg_abort() as discussed below.
  443.  
  444. After completing a compression cycle, you may dispose of the JPEG object
  445. as discussed next, or you may use it to compress another image.  In that case
  446. return to step 2, 3, or 4 as appropriate.  If you do not change the
  447. destination manager, the new datastream will be written to the same target.
  448. If you do not change any JPEG parameters, the new datastream will be written
  449. with the same parameters as before.  Note that you can change the input image
  450. dimensions freely between cycles, but if you change the input colorspace, you
  451. should call jpeg_set_defaults() to adjust for the new colorspace; and then
  452. you'll need to repeat all of step 3.
  453.  
  454.  
  455. 7. Release the JPEG compression object.
  456.  
  457. When you are done with a JPEG compression object, destroy it by calling
  458. jpeg_destroy_compress().  This will free all subsidiary memory.  Or you can
  459. call jpeg_destroy() which works for either compression or decompression
  460. objects --- this may be more convenient if you are sharing code between
  461. compression and decompression cases.  (Actually, these routines are equivalent
  462. except for the declared type of the passed pointer.  To avoid gripes from
  463. ANSI C compilers, pass a j_common_ptr to jpeg_destroy().)
  464.  
  465. If you allocated the jpeg_compress_struct structure from malloc(), freeing
  466. it is your responsibility --- jpeg_destroy() won't.  Ditto for the error
  467. handler structure.
  468.  
  469. Typical code:
  470.  
  471.     jpeg_destroy_compress(&cinfo);
  472.  
  473.  
  474. 8. Aborting.
  475.  
  476. If you decide to abort a compression cycle before finishing, you can clean up
  477. in 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.  
  489. Note that cleaning up the data destination, if required, is your
  490. responsibility.
  491.  
  492.  
  493. Decompression details
  494. ---------------------
  495.  
  496. Here we revisit the JPEG decompression outline given in the overview.
  497.  
  498. 1. Allocate and initialize a JPEG decompression object.
  499.  
  500. This is just like initialization for compression, as discussed above,
  501. except that the object is a "struct jpeg_decompress_struct" and you
  502. call jpeg_create_decompress().  Error handling is exactly the same.
  503.  
  504. Typical 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
  513. both compression and decompression objects.)
  514.  
  515.  
  516. 2. Specify the source of the compressed data (eg, a file).
  517.  
  518. As previously mentioned, the JPEG library reads compressed data from a "data
  519. source" module.  The library includes one data source module which knows how
  520. to read from a stdio stream.  You can use your own source module if you want
  521. to do something else, as discussed later.
  522.  
  523. If you use the standard source module, you must open the source stdio stream
  524. beforehand.  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.  
  534. where the last line invokes the standard source module.
  535.  
  536. WARNING: it is critical that the binary compressed data be read unchanged.
  537. On non-Unix systems the stdio library may perform newline translation or
  538. otherwise corrupt binary data.  To suppress this behavior, you may need to use
  539. a "b" option to fopen (as shown above), or use setmode() or another routine to
  540. put the stdio stream in binary mode.  See cjpeg.c and djpeg.c for code that
  541. has been found to work on many systems.
  542.  
  543. You may not change the data source between calling jpeg_read_header() and
  544. jpeg_finish_decompress().  If you wish to read a series of JPEG images from
  545. a single source file, you should repeat the jpeg_read_header() to
  546. jpeg_finish_decompress() sequence without reinitializing either the JPEG
  547. object or the data source module; this prevents buffered input data from
  548. being discarded.
  549.  
  550.  
  551. 3. Call jpeg_read_header() to obtain image info.
  552.  
  553. Typical code for this step is just
  554.  
  555.     jpeg_read_header(&cinfo, TRUE);
  556.  
  557. This will read the source datastream header markers, up to the beginning
  558. of the compressed data proper.  On return, the image dimensions and other
  559. info have been stored in the JPEG object.  The application may wish to
  560. consult this information before selecting decompression parameters.
  561.  
  562. More 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.  
  570. It is permissible to stop at this point if you just wanted to find out the
  571. image dimensions and other header info for a JPEG file.  In that case,
  572. call jpeg_destroy() when you are done with the JPEG object, or call
  573. jpeg_abort() to return it to an idle state before selecting a new data
  574. source and reading another header.
  575.  
  576.  
  577. 4. Set parameters for decompression.
  578.  
  579. jpeg_read_header() sets appropriate default decompression parameters based on
  580. the properties of the image (in particular, its colorspace).  However, you
  581. may well want to alter these defaults before beginning the decompression.
  582. For example, the default is to produce full color output from a color file.
  583. If you want colormapped output you must ask for it.  Other options allow the
  584. returned image to be scaled and allow various speed/quality tradeoffs to be
  585. selected.  "Decompression parameter selection", below, gives details.
  586.  
  587. If the defaults are appropriate, nothing need be done at this step.
  588.  
  589. Note that all default values are set by each call to jpeg_read_header().
  590. If you reuse a decompression object, you cannot expect your parameter
  591. settings to be preserved across cycles, as you can for compression.
  592. You must adjust parameter values each time.
  593.  
  594.  
  595. 5. jpeg_start_decompress(...);
  596.  
  597. Once the parameter values are satisfactory, call jpeg_start_decompress() to
  598. begin decompression.  This will initialize internal state, allocate working
  599. memory, and prepare for returning data.
  600.  
  601. Typical code is just
  602.  
  603.     jpeg_start_decompress(&cinfo);
  604.  
  605. If you have requested a multi-pass operating mode, such as 2-pass color
  606. quantization, jpeg_start_decompress() will do everything needed before data
  607. output can begin.  In this case jpeg_start_decompress() may take quite a while
  608. to complete.  With a single-scan (fully interleaved) JPEG file and default
  609. decompression parameters, this will not happen; jpeg_start_decompress() will
  610. return quickly.
  611.  
  612. After this call, the final output image dimensions, including any requested
  613. scaling, are available in the JPEG object; so is the selected colormap, if
  614. colormapped 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.  
  623. output_components is 1 (a colormap index) when quantizing colors; otherwise it
  624. equals out_color_components.  It is the number of JSAMPLE values that will be
  625. emitted per pixel in the output arrays.
  626.  
  627. Typically you will need to allocate data buffers to hold the incoming image.
  628. You will need output_width * output_components JSAMPLEs per scanline in your
  629. output buffer, and a total of output_height scanlines will be returned.
  630.  
  631. Note: if you are using the JPEG library's internal memory manager to allocate
  632. data buffers (as djpeg does), then the manager's protocol requires that you
  633. request large buffers *before* calling jpeg_start_decompress().  This is a
  634. little tricky since the output_XXX fields are not normally valid then.  You
  635. can make them valid by calling jpeg_calc_output_dimensions() after setting the
  636. relevant parameters (scaling, output color space, and quantization flag).
  637.  
  638.  
  639. 6. while (scan lines remain to be read)
  640.     jpeg_read_scanlines(...);
  641.  
  642. Now you can read the decompressed image data by calling jpeg_read_scanlines()
  643. one or more times.  At each call, you pass in the maximum number of scanlines
  644. to be read (ie, the height of your working buffer); jpeg_read_scanlines()
  645. will return up to that many lines.  The return value is the number of lines
  646. actually read.  The format of the returned data is discussed under "Data
  647. formats", above.
  648.  
  649. Image data is returned in top-to-bottom scanline order.  If you must write
  650. out the image in bottom-to-top order, you can use the JPEG library's virtual
  651. array mechanism to invert the data efficiently.  Examples of this can be
  652. found in the sample application djpeg.
  653.  
  654. The library maintains a count of the number of scanlines returned so far
  655. in the output_scanline field of the JPEG object.  Usually you can just use
  656. this variable as the loop counter, so that the loop test looks like
  657. "while (cinfo.output_scanline < cinfo.output_height)".  (Note that the test
  658. should NOT be against image_height, unless you never use scaling.  The
  659. image_height field is the height of the original unscaled image.)
  660.  
  661. If you don't use a suspending data source, it is safe to assume that
  662. jpeg_read_scanlines() reads at least one scanline per call, until the
  663. bottom of the image has been reached.  If you use a buffer larger than one
  664. scanline, it is NOT safe to assume that jpeg_read_scanlines() fills it.
  665. In any case, the return value is the same as the change in the value of
  666. output_scanline.
  667.  
  668.  
  669. 7. jpeg_finish_decompress(...);
  670.  
  671. After all the image data has been read, call jpeg_finish_decompress() to
  672. complete the decompression cycle.  This causes working memory associated
  673. with the JPEG object to be released.
  674.  
  675. Typical code:
  676.  
  677.     jpeg_finish_decompress(&cinfo);
  678.  
  679. If using the stdio source manager, don't forget to close the source stdio
  680. stream if necessary.
  681.  
  682. It is an error to call jpeg_finish_decompress() before reading the correct
  683. total number of scanlines.  If you wish to abort compression, call
  684. jpeg_abort() as discussed below.
  685.  
  686. After completing a decompression cycle, you may dispose of the JPEG object as
  687. discussed next, or you may use it to decompress another image.  In that case
  688. return to step 2 or 3 as appropriate.  If you do not change the source
  689. manager, the next image will be read from the same source.
  690.  
  691.  
  692. 8. Release the JPEG decompression object.
  693.  
  694. When you are done with a JPEG decompression object, destroy it by calling
  695. jpeg_destroy_decompress() or jpeg_destroy().  The previous discussion of
  696. destroying compression objects applies here too.
  697.  
  698. Typical code:
  699.  
  700.     jpeg_destroy_decompress(&cinfo);
  701.  
  702.  
  703. 9. Aborting.
  704.  
  705. You can abort a decompression cycle by calling jpeg_destroy_decompress() or
  706. jpeg_destroy() if you don't need the JPEG object any more, or
  707. jpeg_abort_decompress() or jpeg_abort() if you want to reuse the object.
  708. The previous discussion of aborting compression cycles applies here too.
  709.  
  710.  
  711. Mechanics of usage: include files, linking, etc
  712. -----------------------------------------------
  713.  
  714. Applications using the JPEG library should include the header file jpeglib.h
  715. to obtain declarations of data types and routines.  Before including
  716. jpeglib.h, include system headers that define at least the typedefs FILE and
  717. size_t.  On ANSI-conforming systems, including <stdio.h> is sufficient; on
  718. older Unix systems, you may need <sys/types.h> to define size_t.
  719.  
  720. If the application needs to refer to individual JPEG library error codes, also
  721. include jerror.h to define those symbols.
  722.  
  723. jpeglib.h indirectly includes the files jconfig.h and jmorecfg.h.  If you are
  724. installing the JPEG header files in a system directory, you will want to
  725. install all four files: jpeglib.h, jerror.h, jconfig.h, jmorecfg.h.
  726.  
  727. The most convenient way to include the JPEG code into your executable program
  728. is to prepare a library file ("libjpeg.a", or a corresponding name on non-Unix
  729. machines) and reference it at your link step.  If you use only half of the
  730. library (only compression or only decompression), only that much code will be
  731. included from the library, unless your linker is hopelessly brain-damaged.
  732. The supplied makefiles build libjpeg.a automatically (see install.doc).
  733.  
  734. On some systems your application may need to set up a signal handler to ensure
  735. that temporary files are deleted if the program is interrupted.  This is most
  736. critical if you are on MS-DOS and use the jmemdos.c memory manager back end;
  737. it will try to grab extended memory for temp files, and that space will NOT be
  738. freed automatically.  See cjpeg.c or djpeg.c for an example signal handler.
  739.  
  740. It may be worth pointing out that the core JPEG library does not actually
  741. require the stdio library: only the default source/destination managers and
  742. error handler need it.  You can use the library in a stdio-less environment
  743. if you replace those modules and use jmemnobs.c (or another memory manager of
  744. your own devising).  More info about the minimum system library requirements
  745. may be found in jinclude.h.
  746.  
  747.  
  748. ADVANCED FEATURES
  749. =================
  750.  
  751. Compression parameter selection
  752. -------------------------------
  753.  
  754. This section describes all the optional parameters you can set for JPEG
  755. compression, as well as the "helper" routines provided to assist in this
  756. task.  Proper setting of some parameters requires detailed understanding
  757. of the JPEG standard; if you don't know what a parameter is for, it's best
  758. not to mess with it!  See REFERENCES in the README file for pointers to
  759. more info about JPEG.
  760.  
  761. It's a good idea to call jpeg_set_defaults() first, even if you plan to set
  762. all the parameters; that way your code is more likely to work with future JPEG
  763. libraries that have additional parameters.  For the same reason, we recommend
  764. you use a helper routine where one is provided, in preference to twiddling
  765. cinfo fields directly.
  766.  
  767. The helper routines are:
  768.  
  769. jpeg_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.  
  775. jpeg_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.  
  783. jpeg_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.  
  789. jpeg_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.  
  803. jpeg_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.  
  814. int 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.  
  821. jpeg_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.  
  831. Compression parameters (cinfo fields) include:
  832.  
  833. boolean 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.  
  843. int 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.  
  848. J_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.  
  864. unsigned int restart_interval
  865. int 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.  
  872. J_COLOR_SPACE jpeg_color_space
  873. int 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.  
  878. boolean 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.  
  883. UINT8 density_unit
  884. UINT16 X_density
  885. UINT16 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.  
  891. boolean 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.  
  900. JQUANT_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.  
  908. JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS]
  909. JHUFF_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.  
  918. There are some additional cinfo fields which are not documented here
  919. because you currently can't change them; for example, you can't set
  920. arith_code TRUE because arithmetic coding is unsupported.
  921.  
  922.  
  923. Per-component parameters are stored in the struct cinfo.comp_info[i] for
  924. component number i.  Note that components here refer to components of the
  925. JPEG color space, *not* the source image color space.  A suitably large
  926. comp_info[] array is allocated by jpeg_set_defaults(); if you choose not
  927. to use that routine, it's up to you to allocate the array.
  928.  
  929. int 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.  
  934. int h_samp_factor
  935. int 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.  
  943. int quant_tbl_no
  944.     Quantization table number for component.  The default value is
  945.     0 for luminance components and 1 for chrominance components.
  946.  
  947. int dc_tbl_no
  948. int 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.  
  952. int component_index
  953.     Must equal the component's index in comp_info[].
  954.  
  955.  
  956. Decompression parameter selection
  957. ---------------------------------
  958.  
  959. Decompression parameter selection is somewhat simpler than compression
  960. parameter selection, since all of the JPEG internal parameters are
  961. recorded 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
  964. the postprocessing done on the image to deliver it in a format suitable
  965. for the application's use.  Many of the parameters control speed/quality
  966. tradeoffs, in which faster decompression may be obtained at the price of
  967. a poorer-quality image.  The defaults select the highest quality (slowest)
  968. processing.
  969.  
  970. The following fields in the JPEG object are set by jpeg_read_header() and
  971. may be useful to the application in choosing decompression parameters:
  972.  
  973. JDIMENSION image_width            Width and height of image
  974. JDIMENSION image_height
  975. int num_components            Number of color components
  976. J_COLOR_SPACE jpeg_color_space        Colorspace of image
  977. boolean 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
  981. boolean saw_Adobe_marker        TRUE if an Adobe APP14 marker was seen
  982.   UINT8 Adobe_transform            Color transform code from Adobe marker
  983.  
  984. The JPEG color space, unfortunately, is something of a guess since the JPEG
  985. standard proper does not provide a way to record it.  In practice most files
  986. adhere to the JFIF or Adobe conventions, and the decoder will recognize these
  987. correctly.  See "Special color spaces", below, for more info.
  988.  
  989.  
  990. The decompression parameters that determine the basic properties of the
  991. returned image are:
  992.  
  993. J_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.  
  1004. unsigned 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.  
  1012. boolean quantize_colors
  1013.     If set TRUE, colormapped output will be delivered.  Default is FALSE,
  1014.     meaning that full-color output will be delivered.
  1015.  
  1016. The next three parameters are relevant only if quantize_colors is TRUE.
  1017.  
  1018. int 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.  
  1023. boolean 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.  
  1029. J_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.  
  1039. When quantize_colors is TRUE, the target color map is described by the next
  1040. two fields.  colormap is set to NULL by jpeg_read_header().  The application
  1041. can supply a color map by setting colormap non-NULL and setting
  1042. actual_number_of_colors to the map size.  Otherwise, jpeg_start_decompress()
  1043. selects a suitable color map and sets these two fields itself.
  1044. [Implementation restriction: at present, an externally supplied colormap is
  1045. only accepted for 3-component output color spaces.]
  1046.  
  1047. JSAMPARRAY 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.  
  1051. int actual_number_of_colors
  1052.     The number of colors in the color map.
  1053.  
  1054. Additional decompression parameters that the application may set include:
  1055.  
  1056. J_DCT_METHOD dct_method
  1057.     Selects the algorithm used for the DCT step.  Choices are the same
  1058.     as described above for compression.
  1059.  
  1060. boolean 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.  
  1066. The output image dimensions are given by the following fields.  These are
  1067. computed from the source image dimensions and the decompression parameters
  1068. by jpeg_start_decompress().  You can also call jpeg_calc_output_dimensions()
  1069. to obtain the values that will result from the current parameter settings.
  1070. This can be useful if you are trying to pick a scaling ratio that will get
  1071. close to a desired target size.  It's also important if you are using the
  1072. JPEG library's memory manager to allocate output buffer space, because you
  1073. are supposed to request such buffers *before* jpeg_start_decompress().
  1074.