170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * example.c
370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This file illustrates how to use the IJG code as a subroutine library
570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * to read or write JPEG image files.  You should look at this code in
670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * conjunction with the documentation file libjpeg.doc.
770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This code will not do anything useful as-is, but it may be helpful as a
970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * skeleton for constructing routines that call the JPEG library.
1070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
1170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We present these routines in the same coding style used in the JPEG code
1270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * (ANSI function definitions, etc); but you are of course free to code your
1370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * routines in a different style if you prefer.
1470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
1570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
1670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include <stdio.h>
1770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
1870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
1970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Include file for users of JPEG library.
2070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * You will need to have included system headers that define at least
2170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * the typedefs FILE and size_t before you can include jpeglib.h.
2270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * (stdio.h is sufficient on ANSI-conforming systems.)
2370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * You may also wish to include "jerror.h".
2470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
2570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
2670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include "jpeglib.h"
2770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
2870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
2970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * <setjmp.h> is used for the optional error recovery mechanism shown in
3070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * the second part of the example.
3170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
3270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include <setjmp.h>
3470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
3870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* This half of the example shows how to feed data into the JPEG compressor.
4070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We present a minimal version that does not worry about refinements such
4170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * as error recovery (the JPEG code will just exit() if it gets an error).
4270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
4370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
4470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
4570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
4670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * IMAGE DATA FORMATS:
4770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
4870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The standard input image format is a rectangular array of pixels, with
4970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * each pixel having the same number of "component" values (color channels).
5070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars).
5170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * If you are working with color data, then the color values for each pixel
5270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
5370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * RGB color.
5470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
5570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * For this example, we'll assume that this data structure matches the way
5670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * our application has stored the image in memory, so we can just pass a
5770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * pointer to our image buffer.  In particular, let's say that the image is
5870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * RGB color and is described by:
5970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
6070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineextern JSAMPLE * image_buffer;	/* Points to large array of R,G,B-order data */
6270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineextern int image_height;	/* Number of rows in image */
6370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineextern int image_width;		/* Number of columns in image */
6470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
6770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Sample routine for JPEG compression.  We assume that the target file name
6870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * and a compression quality factor are passed in.
6970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
7070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
7170a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
7270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinewrite_JPEG_file (char * filename, int quality)
7370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
7470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* This struct contains the JPEG compression parameters and pointers to
7570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * working space (which is allocated as needed by the JPEG library).
7670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * It is possible to have several such structures, representing multiple
7770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * compression/decompression processes, in existence at once.  We refer
7870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * to any one struct (and its associated working data) as a "JPEG object".
7970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
8070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  struct jpeg_compress_struct cinfo;
8170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* This struct represents a JPEG error handler.  It is declared separately
8270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * because applications often want to supply a specialized error handler
8370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * (see the second half of this file for an example).  But here we just
8470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * take the easy way out and use the standard error handler, which will
8570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * print a message on stderr and call exit() if compression fails.
8670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Note that this struct must live as long as the main JPEG parameter
8770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * struct, to avoid dangling-pointer problems.
8870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
8970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  struct jpeg_error_mgr jerr;
9070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* More stuff */
9170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  FILE * outfile;		/* target file */
9270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
9370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int row_stride;		/* physical row width in image buffer */
9470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 1: allocate and initialize JPEG compression object */
9670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* We have to set up the error handler first, in case the initialization
9870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * step fails.  (Unlikely, but it could happen if you are out of memory.)
9970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * This routine fills in the contents of struct jerr, and returns jerr's
10070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * address which we place into the link field in cinfo.
10170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
10270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo.err = jpeg_std_error(&jerr);
10370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Now we can initialize the JPEG compression object. */
10470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_create_compress(&cinfo);
10570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
10670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 2: specify data destination (eg, a file) */
10770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Note: steps 2 and 3 can be done in either order. */
10870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
10970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Here we use the library-supplied code to send compressed data to a
11070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * stdio stream.  You can also write your own code to do something else.
11170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
11270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * requires it in order to write binary files.
11370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
11470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if ((outfile = fopen(filename, "wb")) == NULL) {
11570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    fprintf(stderr, "can't open %s\n", filename);
11670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    exit(1);
11770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
11870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_stdio_dest(&cinfo, outfile);
11970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
12070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 3: set parameters for compression */
12170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
12270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* First we supply a description of the input image.
12370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Four fields of the cinfo struct must be filled in:
12470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
12570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo.image_width = image_width; 	/* image width and height, in pixels */
12670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo.image_height = image_height;
12770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo.input_components = 3;		/* # of color components per pixel */
12870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
12970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Now use the library's routine to set default compression parameters.
13070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * (You must set at least cinfo.in_color_space before calling this,
13170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * since the defaults depend on the source color space.)
13270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
13370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_set_defaults(&cinfo);
13470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Now you can set any non-default parameters you wish to.
13570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Here we just illustrate the use of quality (quantization table) scaling:
13670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
13770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
13870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
13970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 4: Start compressor */
14070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
14170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* TRUE ensures that we will write a complete interchange-JPEG file.
14270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Pass TRUE unless you are very sure of what you're doing.
14370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
14470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_start_compress(&cinfo, TRUE);
14570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
14670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 5: while (scan lines remain to be written) */
14770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /*           jpeg_write_scanlines(...); */
14870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
14970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Here we use the library's state variable cinfo.next_scanline as the
15070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * loop counter, so that we don't have to keep track ourselves.
15170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * To keep things simple, we pass one scanline per call; you can pass
15270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * more if you wish, though.
15370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
15470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  row_stride = image_width * 3;	/* JSAMPLEs per row in image_buffer */
15570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
15670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  while (cinfo.next_scanline < cinfo.image_height) {
15770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* jpeg_write_scanlines expects an array of pointers to scanlines.
15870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     * Here the array is only one element long, but you could pass
15970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     * more than one scanline at a time if that's more convenient.
16070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     */
16170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
16270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
16370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
16470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
16570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 6: Finish compression */
16670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
16770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_finish_compress(&cinfo);
16870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* After finish_compress, we can close the output file. */
16970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  fclose(outfile);
17070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
17170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 7: release JPEG compression object */
17270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
17370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* This is an important step since it will release a good deal of memory. */
17470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_destroy_compress(&cinfo);
17570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
17670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* And we're done! */
17770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
17870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
17970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
18070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
18170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * SOME FINE POINTS:
18270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
18370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * In the above loop, we ignored the return value of jpeg_write_scanlines,
18470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * which is the number of scanlines actually written.  We could get away
18570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * with this because we were only relying on the value of cinfo.next_scanline,
18670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * which will be incremented correctly.  If you maintain additional loop
18770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * variables then you should be careful to increment them properly.
18870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Actually, for output to a stdio stream you needn't worry, because
18970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * then jpeg_write_scanlines will write all the lines passed (or else exit
19070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * with a fatal error).  Partial writes can only occur if you use a data
19170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * destination module that can demand suspension of the compressor.
19270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * (If you don't know what that's for, you don't need it.)
19370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
19470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * If the compressor requires full-image buffers (for entropy-coding
19570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * optimization or a multi-scan JPEG file), it will create temporary
19670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * files for anything that doesn't fit within the maximum-memory setting.
19770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * (Note that temp files are NOT needed if you use the default parameters.)
19870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * On some systems you may need to set up a signal handler to ensure that
19970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
20070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
20170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG
20270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * files to be compatible with everyone else's.  If you cannot readily read
20370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * your data in that order, you'll need an intermediate array to hold the
20470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * image.  See rdtarga.c or rdbmp.c for examples of handling bottom-to-top
20570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * source data using the JPEG code's internal virtual-array mechanisms.
20670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
20770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
20870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
20970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
21070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
21170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
21270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* This half of the example shows how to read data from the JPEG decompressor.
21370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * It's a bit more refined than the above, in that we show:
21470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *   (a) how to modify the JPEG library's standard error-reporting behavior;
21570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *   (b) how to allocate workspace using the library's memory manager.
21670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
21770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Just to make this example a little different from the first one, we'll
21870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * assume that we do not intend to put the whole image into an in-memory
21970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * buffer, but to send it line-by-line someplace else.  We need a one-
22070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
22170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * memory manager allocate it for us.  This approach is actually quite useful
22270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * because we don't need to remember to deallocate the buffer separately: it
22370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * will go away automatically when the JPEG object is cleaned up.
22470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
22570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
22670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
22770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
22870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * ERROR HANDLING:
22970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
23070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The JPEG library's standard error handler (jerror.c) is divided into
23170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * several "methods" which you can override individually.  This lets you
23270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * adjust the behavior without duplicating a lot of code, which you might
23370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * have to update with each future release.
23470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
23570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Our example here shows how to override the "error_exit" method so that
23670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * control is returned to the library's caller when a fatal error occurs,
23770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * rather than calling exit() as the standard error_exit method does.
23870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
23970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We use C's setjmp/longjmp facility to return control.  This means that the
24070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * routine which calls the JPEG library must first execute a setjmp() call to
24170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * establish the return point.  We want the replacement error_exit to do a
24270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * longjmp().  But we need to make the setjmp buffer accessible to the
24370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * error_exit routine.  To do this, we make a private extension of the
24470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * standard JPEG error handler object.  (If we were using C++, we'd say we
24570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * were making a subclass of the regular error handler.)
24670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
24770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Here's the extended error handler struct:
24870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
24970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
25070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinestruct my_error_mgr {
25170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  struct jpeg_error_mgr pub;	/* "public" fields */
25270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
25370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jmp_buf setjmp_buffer;	/* for return to caller */
25470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine};
25570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
25670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef struct my_error_mgr * my_error_ptr;
25770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
25870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
25970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Here's the routine that will replace the standard error_exit method:
26070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
26170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
26270a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void)
26370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinemy_error_exit (j_common_ptr cinfo)
26470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
26570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
26670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  my_error_ptr myerr = (my_error_ptr) cinfo->err;
26770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
26870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Always display the message. */
26970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* We could postpone this until after returning, if we chose. */
27070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (*cinfo->err->output_message) (cinfo);
27170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
27270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Return control to the setjmp point */
27370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  longjmp(myerr->setjmp_buffer, 1);
27470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
27570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
27670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
27770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
27870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Sample routine for JPEG decompression.  We assume that the source file name
27970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * is passed in.  We want to return 1 on success, 0 on error.
28070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
28170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
28270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
28370a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(int)
28470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineread_JPEG_file (char * filename)
28570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
28670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* This struct contains the JPEG decompression parameters and pointers to
28770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * working space (which is allocated as needed by the JPEG library).
28870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
28970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  struct jpeg_decompress_struct cinfo;
29070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* We use our private extension JPEG error handler.
29170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Note that this struct must live as long as the main JPEG parameter
29270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * struct, to avoid dangling-pointer problems.
29370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
29470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  struct my_error_mgr jerr;
29570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* More stuff */
29670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  FILE * infile;		/* source file */
29770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JSAMPARRAY buffer;		/* Output row buffer */
29870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int row_stride;		/* physical row width in output buffer */
29970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
30070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* In this example we want to open the input file before doing anything else,
30170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * so that the setjmp() error recovery below can assume the file is open.
30270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
30370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * requires it in order to read binary files.
30470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
30570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
30670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if ((infile = fopen(filename, "rb")) == NULL) {
30770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    fprintf(stderr, "can't open %s\n", filename);
30870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    return 0;
30970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
31070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
31170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 1: allocate and initialize JPEG decompression object */
31270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
31370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* We set up the normal JPEG error routines, then override error_exit. */
31470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo.err = jpeg_std_error(&jerr.pub);
31570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jerr.pub.error_exit = my_error_exit;
31670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Establish the setjmp return context for my_error_exit to use. */
31770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (setjmp(jerr.setjmp_buffer)) {
31870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* If we get here, the JPEG code has signaled an error.
31970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     * We need to clean up the JPEG object, close the input file, and return.
32070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     */
32170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    jpeg_destroy_decompress(&cinfo);
32270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    fclose(infile);
32370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    return 0;
32470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
32570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Now we can initialize the JPEG decompression object. */
32670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_create_decompress(&cinfo);
32770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
32870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 2: specify data source (eg, a file) */
32970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
33070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_stdio_src(&cinfo, infile);
33170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
33270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 3: read file parameters with jpeg_read_header() */
33370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
33470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (void) jpeg_read_header(&cinfo, TRUE);
33570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* We can ignore the return value from jpeg_read_header since
33670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   *   (a) suspension is not possible with the stdio data source, and
33770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
33870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * See libjpeg.doc for more info.
33970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
34070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
34170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 4: set parameters for decompression */
34270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
34370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* In this example, we don't need to change any of the defaults set by
34470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * jpeg_read_header(), so we do nothing here.
34570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
34670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
34770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 5: Start decompressor */
34870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
34970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (void) jpeg_start_decompress(&cinfo);
35070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* We can ignore the return value since suspension is not possible
35170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * with the stdio data source.
35270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
35370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
35470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* We may need to do some setup of our own at this point before reading
35570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * the data.  After jpeg_start_decompress() we have the correct scaled
35670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * output image dimensions available, as well as the output colormap
35770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * if we asked for color quantization.
35870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * In this example, we need to make an output work buffer of the right size.
35970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
36070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* JSAMPLEs per row in output buffer */
36170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  row_stride = cinfo.output_width * cinfo.output_components;
36270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Make a one-row-high sample array that will go away when done with image */
36370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  buffer = (*cinfo.mem->alloc_sarray)
36470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine		((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
36570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
36670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 6: while (scan lines remain to be read) */
36770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /*           jpeg_read_scanlines(...); */
36870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
36970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Here we use the library's state variable cinfo.output_scanline as the
37070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * loop counter, so that we don't have to keep track ourselves.
37170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
37270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  while (cinfo.output_scanline < cinfo.output_height) {
37370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* jpeg_read_scanlines expects an array of pointers to scanlines.
37470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     * Here the array is only one element long, but you could ask for
37570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     * more than one scanline at a time if that's more convenient.
37670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     */
37770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    (void) jpeg_read_scanlines(&cinfo, buffer, 1);
37870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Assume put_scanline_someplace wants a pointer and sample count. */
37970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    put_scanline_someplace(buffer[0], row_stride);
38070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
38170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
38270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 7: Finish decompression */
38370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
38470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (void) jpeg_finish_decompress(&cinfo);
38570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* We can ignore the return value since suspension is not possible
38670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * with the stdio data source.
38770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
38870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
38970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Step 8: Release JPEG decompression object */
39070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
39170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* This is an important step since it will release a good deal of memory. */
39270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_destroy_decompress(&cinfo);
39370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
39470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* After finish_decompress, we can close the input file.
39570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Here we postpone it until after no more JPEG errors are possible,
39670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * so as to simplify the setjmp error logic above.  (Actually, I don't
39770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * think that jpeg_destroy can do an error exit, but why assume anything...)
39870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
39970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  fclose(infile);
40070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
40170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* At this point you may want to check to see whether any corrupt-data
40270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
40370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
40470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
40570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* And we're done! */
40670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return 1;
40770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
40870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
40970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
41070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
41170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * SOME FINE POINTS:
41270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
41370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * In the above code, we ignored the return value of jpeg_read_scanlines,
41470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * which is the number of scanlines actually read.  We could get away with
41570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * this because we asked for only one line at a time and we weren't using
41670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * a suspending data source.  See libjpeg.doc for more info.
41770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
41870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
41970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * we should have done it beforehand to ensure that the space would be
42070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * counted against the JPEG max_memory setting.  In some systems the above
42170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * code would risk an out-of-memory error.  However, in general we don't
42270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * know the output image dimensions before jpeg_start_decompress(), unless we
42370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * call jpeg_calc_output_dimensions().  See libjpeg.doc for more about this.
42470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
42570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Scanlines are returned in the same order as they appear in the JPEG file,
42670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * which is standardly top-to-bottom.  If you must emit data bottom-to-top,
42770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * you can use one of the virtual arrays provided by the JPEG memory manager
42870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * to invert the data.  See wrbmp.c for an example.
42970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
43070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * As with compression, some operating modes may require temporary files.
43170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * On some systems you may need to set up a signal handler to ensure that
43270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
43370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
434