home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / compresn / jpegv3sr / example.c < prev    next >
C/C++ Source or Header  |  1992-03-05  |  26KB  |  625 lines

  1. /*
  2.  * example.c
  3.  *
  4.  * This file is not actually part of the JPEG software.  Rather, it provides
  5.  * a skeleton that may be useful for constructing applications that use the
  6.  * JPEG software as subroutines.  This code will NOT do anything useful as is.
  7.  *
  8.  * This file illustrates how to use the JPEG code as a subroutine library
  9.  * to read or write JPEG image files.  We assume here that you are not
  10.  * merely interested in converting the image to yet another image file format
  11.  * (if you are, you should be adding another I/O module to cjpeg/djpeg, not
  12.  * constructing a new application).  Instead, we show how to pass the
  13.  * decompressed image data into or out of routines that you provide.  For
  14.  * example, a viewer program might use the JPEG decompressor together with
  15.  * routines that write the decompressed image directly to a display.
  16.  *
  17.  * We present these routines in the same coding style used in the JPEG code
  18.  * (ANSI function definitions, etc); but you are of course free to code your
  19.  * routines in a different style if you prefer.
  20.  */
  21.  
  22. /*
  23.  * Include file for declaring JPEG data structures.
  24.  * This file also includes some system headers like <stdio.h>;
  25.  * if you prefer, you can include "jconfig.h" and "jpegdata.h" instead.
  26.  */
  27.  
  28. #include "jinclude.h"
  29.  
  30. /*
  31.  * <setjmp.h> is used for the optional error recovery mechanism shown in
  32.  * the second part of the example.
  33.  */
  34.  
  35. #include <setjmp.h>
  36.  
  37.  
  38.  
  39. /******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
  40.  
  41. /* This half of the example shows how to feed data into the JPEG compressor.
  42.  * We present a minimal version that does not worry about refinements such
  43.  * as error recovery (the JPEG code will just exit() if it gets an error).
  44.  */
  45.  
  46.  
  47. /*
  48.  * To supply the image data for compression, you must define three routines
  49.  * input_init, get_input_row, and input_term.  These routines will be called
  50.  * from the JPEG compressor via function pointer values that you store in the
  51.  * cinfo data structure; hence they need not be globally visible and the exact
  52.  * names don't matter.  (In fact, the "METHODDEF" macro expands to "static" if
  53.  * you use the unmodified JPEG include files.)
  54.  *
  55.  * The input file reading modules (jrdppm.c, jrdgif.c, jrdtarga.c, etc) may be
  56.  * useful examples of what these routines should actually do, although each of
  57.  * them is encrusted with a lot of specialized code for its own file format.
  58.  */
  59.  
  60.  
  61. METHODDEF void
  62. input_init (compress_info_ptr cinfo)
  63. /* Initialize for input; return image size and component data. */
  64. {
  65.   /* This routine must return five pieces of information about the incoming
  66.    * image, and must do any setup needed for the get_input_row routine.
  67.    * The image information is returned in fields of the cinfo struct.
  68.    * (If you don't care about modularity, you could initialize these fields
  69.    * in the main JPEG calling routine, and make this routine be a no-op.)
  70.    * We show some example values here.
  71.    */
  72.   cinfo->image_width = 640;        /* width in pixels */
  73.   cinfo->image_height = 480;        /* height in pixels */
  74.   /* JPEG views an image as being a rectangular array of pixels, with each
  75.    * pixel having the same number of "component" values (color channels).
  76.    * You must specify how many components there are and the colorspace
  77.    * interpretation of the components.  Most applications will use RGB data or
  78.    * grayscale data.  If you want to use something else, you'll need to study
  79.    * and perhaps modify jcdeflts.c, jccolor.c, and jdcolor.c.
  80.    */
  81.   cinfo->input_components = 3;        /* or 1 for grayscale */
  82.   cinfo->in_color_space = CS_RGB;    /* or CS_GRAYSCALE for grayscale */
  83.   cinfo->data_precision = 8;        /* bits per pixel component value */
  84.   /* In the current JPEG software, data_precision must be set equal to
  85.    * BITS_IN_JSAMPLE, which is 8 unless you twiddle jconfig.h.  Future
  86.    * versions might allow you to say either 8 or 12 if compiled with
  87.    * 12-bit JSAMPLEs, or up to 16 in lossless mode.  In any case,
  88.    * it is up to you to scale incoming pixel values to the range
  89.    *   0 .. (1<<data_precision)-1.
  90.    * If your image data format is fixed at a byte per component,
  91.    * then saying "8" is probably the best long-term solution.
  92.    */
  93. }
  94.  
  95.  
  96. /*
  97.  * This function is called repeatedly and must supply the next row of pixels
  98.  * on each call.  The rows MUST be returned in top-to-bottom order if you want
  99.  * your JPEG files to be compatible with everyone else's.  (If you cannot
  100.  * readily read your data in that order, you'll need an intermediate array to
  101.  * hold the image.  See jrdtarga.c or jrdrle.c for examples of handling
  102.  * bottom-to-top source data using the JPEG code's portable mechanisms.)
  103.  * The data is to be returned into a 2-D array of JSAMPLEs, indexed as
  104.  *        JSAMPLE pixel_row[component][column]
  105.  * where component runs from 0 to cinfo->input_components-1, and column runs
  106.  * from 0 to cinfo->image_width-1 (column 0 is left edge of image).  Note that
  107.  * this is actually an array of pointers to arrays rather than a true 2D array,
  108.  * since C does not support variable-size multidimensional arrays.
  109.  * JSAMPLE is typically typedef'd as "unsigned char".
  110.  */
  111.  
  112.  
  113. METHODDEF void
  114. get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  115. /* Read next row of pixels into pixel_row[][] */
  116. {
  117.   /* This example shows how you might read RGB data (3 components)
  118.    * from an input file in which the data is stored 3 bytes per pixel
  119.    * in left-to-right, top-to-bottom order.
  120.    */
  121.   register FILE * infile = cinfo->input_file;
  122.   register JSAMPROW ptr0, ptr1, ptr2;
  123.   register long col;
  124.   
  125.   ptr0 = pixel_row[0];
  126.   ptr1 = pixel_row[1];
  127.   ptr2 = pixel_row[2];
  128.   for (col = 0; col < cinfo->image_width; col++) {
  129.     *ptr0++ = (JSAMPLE) getc(infile); /* red */
  130.     *ptr1++ = (JSAMPLE) getc(infile); /* green */
  131.     *ptr2++ = (JSAMPLE) getc(infile); /* blue */
  132.   }
  133. }
  134.  
  135.  
  136. METHODDEF void
  137. input_term (compress_info_ptr cinfo)
  138. /* Finish up at the end of the input */
  139. {
  140.   /* This termination routine will very often have no work to do, */
  141.   /* but you must provide it anyway. */
  142.   /* Note that the JPEG code will only call it during successful exit; */
  143.   /* if you want it called during error exit, you gotta do that yourself. */
  144. }
  145.  
  146.  
  147. /*
  148.  * That's it for the routines that deal with reading the input image data.
  149.  * Now we have overall control and parameter selection routines.
  150.  */
  151.  
  152.  
  153. /*
  154.  * This routine must determine what output JPEG file format is to be written,
  155.  * and make any other compression parameter changes that are desirable.
  156.  * This routine gets control after the input file header has been read
  157.  * (i.e., right after input_init has been called).  You could combine its
  158.  * functions into input_init, or even into the main control routine, but
  159.  * if you have several different input_init routines, it's a definite win
  160.  * to keep this separate.  You MUST supply this routine even if it's a no-op.
  161.  */
  162.  
  163. METHODDEF void
  164. c_ui_method_selection (compress_info_ptr cinfo)
  165. {
  166.   /* If the input is gray scale, generate a monochrome JPEG file. */
  167.   if (cinfo->in_color_space == CS_GRAYSCALE)
  168.     j_monochrome_default(cinfo);
  169.   /* For now, always select JFIF output format. */
  170.   jselwjfif(cinfo);
  171. }
  172.  
  173.  
  174. /*
  175.  * OK, here is the main function that actually causes everything to happen.
  176.  * We assume here that the target filename is supplied by the caller of this
  177.  * routine, and that all JPEG compression parameters can be default values.
  178.  */
  179.  
  180. GLOBAL void
  181. write_JPEG_file (char * filename)
  182. {
  183.   /* These three structs contain JPEG parameters and working data.
  184.    * They must survive for the duration of parameter setup and one
  185.    * call to jpeg_compress; typically, making them local data in the
  186.    * calling routine is the best strategy.
  187.    */
  188.   struct compress_info_struct cinfo;
  189.   struct compress_methods_struct c_methods;
  190.   struct external_methods_struct e_methods;
  191.  
  192.   /* Initialize the system-dependent method pointers. */
  193.   cinfo.methods = &c_methods;    /* links to method structs */
  194.   cinfo.emethods = &e_methods;
  195.   /* Here we use the default JPEG error handler, which will just print
  196.    * an error message on stderr and call exit().  See the second half of
  197.    * this file for an example of more graceful error recovery.
  198.    */
  199.   jselerror(&e_methods);    /* select std error/trace message routines */
  200.   /* Here we use the standard memory manager provided with the JPEG code.
  201.    * In some cases you might want to replace the memory manager, or at
  202.    * least the system-dependent part of it, with your own code.
  203.    */
  204.   jselmemmgr(&e_methods);    /* select std memory allocation routines */
  205.   /* If the compressor requires full-image buffers (for entropy-coding
  206.    * optimization or a noninterleaved JPEG file), it will create temporary
  207.    * files for anything that doesn't fit within the maximum-memory setting.
  208.    * (Note that temp files are NOT needed if you use the default parameters.)
  209.    * You can change the default maximum-memory setting by changing
  210.    * e_methods.max_memory_to_use after jselmemmgr returns.
  211.    * On some systems you may also need to set up a signal handler to
  212.    * ensure that temporary files are deleted if the program is interrupted.
  213.    * (This is most important if you are on MS-DOS and use the jmemdos.c
  214.    * memory manager back end; it will try to grab extended memory for
  215.    * temp files, and that space will NOT be freed automatically.)
  216.    * See jcmain.c or jdmain.c for an example signal handler.
  217.    */
  218.  
  219.   /* Here, set up pointers to your own routines for input data handling
  220.    * and post-init parameter selection.
  221.    */
  222.   c_methods.input_init = input_init;
  223.   c_methods.get_input_row = get_input_row;
  224.   c_methods.input_term = input_term;
  225.   c_methods.c_ui_method_selection = c_ui_method_selection;
  226.  
  227.   /* Set up default JPEG parameters in the cinfo data structure. */
  228.   j_c_defaults(&cinfo, 75, FALSE);
  229.   /* Note: 75 is the recommended default quality level; you may instead pass
  230.    * a user-specified quality level.  Be aware that values below 25 will cause
  231.    * non-baseline JPEG files to be created (and a warning message to that
  232.    * effect to be emitted on stderr).  This won't bother our decoder, but some
  233.    * commercial JPEG implementations may choke on non-baseline JPEG files.
  234.    * If you want to force baseline compatibility, pass TRUE instead of FALSE.
  235.    * (If non-baseline files are fine, but you could do without that warning
  236.    * message, set e_methods.trace_level to -1.)
  237.    */
  238.  
  239.   /* At this point you can modify the default parameters set by j_c_defaults
  240.    * as needed.  For a minimal implementation, you shouldn't need to change
  241.    * anything.  See jcmain.c for some examples of what you might change.
  242.    */
  243.  
  244.   /* Select the input and output files.
  245.    * Note that cinfo.input_file is only used if your input reading routines
  246.    * use it; otherwise, you can just make it NULL.
  247.    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  248.    * requires it in order to write binary files.
  249.    */
  250.  
  251.   cinfo.input_file = NULL;    /* if no actual input file involved */
  252.  
  253.   if ((cinfo.output_file = fopen(filename, "wb")) == NULL) {
  254.     fprintf(stderr, "can't open %s\n", filename);
  255.     exit(1);
  256.   }
  257.  
  258.   /* Here we go! */
  259.   jpeg_compress(&cinfo);
  260.  
  261.   /* That's it, son.  Nothin' else to do, except close files. */
  262.   /* Here we assume only the output file need be closed. */
  263.   fclose(cinfo.output_file);
  264.  
  265.   /* Note: if you want to compress more than one image, we recommend you
  266.    * repeat this whole routine.  You MUST repeat the j_c_defaults()/alter
  267.    * parameters/jpeg_compress() sequence, as some data structures allocated
  268.    * in j_c_defaults are freed upon exit from jpeg_compress.
  269.    */
  270. }
  271.  
  272.  
  273.  
  274. /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
  275.  
  276. /* This half of the example shows how to read data from the JPEG decompressor.
  277.  * It's a little more refined than the above in that we show how to do your
  278.  * own error recovery.  If you don't care about that, you don't need these
  279.  * next two routines.
  280.  */
  281.  
  282.  
  283. /*
  284.  * These routines replace the default trace/error routines included with the
  285.  * JPEG code.  The example trace_message routine shown here is actually the
  286.  * same as the standard one, but you could modify it if you don't want messages
  287.  * sent to stderr.  The example error_exit routine is set up to return
  288.  * control to read_JPEG_file() rather than calling exit().  You can use the
  289.  * same routines for both compression and decompression error recovery.
  290.  */
  291.  
  292. /* These static variables are needed by the error routines. */
  293. static jmp_buf setjmp_buffer;    /* for return to caller */
  294. static external_methods_ptr emethods; /* needed for access to message_parm */
  295.  
  296.  
  297. /* This routine is used for any and all trace, debug, or error printouts
  298.  * from the JPEG code.  The parameter is a printf format string; up to 8
  299.  * integer data values for the format string have been stored in the
  300.  * message_parm[] field of the external_methods struct.
  301.  */
  302.  
  303. METHODDEF void
  304. trace_message (const char *msgtext)
  305. {
  306.   fprintf(stderr, msgtext,
  307.       emethods->message_parm[0], emethods->message_parm[1],
  308.       emethods->message_parm[2], emethods->message_parm[3],
  309.       emethods->message_parm[4], emethods->message_parm[5],
  310.       emethods->message_parm[6], emethods->message_parm[7]);
  311.   fprintf(stderr, "\n");    /* there is no \n in the format string! */
  312. }
  313.  
  314. /*
  315.  * The error_exit() routine should not return to its caller.  The default
  316.  * routine calls exit(), but here we assume that we want to return to
  317.  * read_JPEG_data, which has set up a setjmp context for the purpose.
  318.  * You should make sure that the free_all method is called, either within
  319.  * error_exit or after the return to the outer-level routine.
  320.  */
  321.  
  322. METHODDEF void
  323. error_exit (const char *msgtext)
  324. {
  325.   trace_message(msgtext);    /* report the error message */
  326.   (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  327.   longjmp(setjmp_buffer, 1);    /* return control to outer routine */
  328. }
  329.  
  330.  
  331.  
  332. /*
  333.  * To accept the image data from decompression, you must define four routines
  334.  * output_init, put_color_map, put_pixel_rows, and output_term.
  335.  *
  336.  * You must understand the distinction between full color output mode
  337.  * (N independent color components) and colormapped output mode (a single
  338.  * output component representing an index into a color map).  You should use
  339.  * colormapped mode to write to a colormapped display screen or output file.
  340.  * Colormapped mode is also useful for reducing grayscale output to a small
  341.  * number of gray levels: when using the 1-pass quantizer on grayscale data,
  342.  * the colormap entries will be evenly spaced from 0 to MAX_JSAMPLE, so you
  343.  * can regard the indexes are directly representing gray levels at reduced
  344.  * precision.  In any other case, you should not depend on the colormap
  345.  * entries having any particular order.
  346.  * To get colormapped output, set cinfo->quantize_colors to TRUE and set
  347.  * cinfo->desired_number_of_colors to the maximum number of entries in the
  348.  * colormap.  This can be done either in your main routine or in
  349.  * d_ui_method_selection.  For grayscale quantization, also set
  350.  * cinfo->two_pass_quantize to FALSE to ensure the 1-pass quantizer is used
  351.  * (presently this is the default, but it may not be so in the future).
  352.  *
  353.  * The output file writing modules (jwrppm.c, jwrgif.c, jwrtarga.c, etc) may be
  354.  * useful examples of what these routines should actually do, although each of
  355.  * them is encrusted with a lot of specialized code for its own file format.
  356.  */
  357.  
  358.  
  359. METHODDEF void
  360. output_init (decompress_info_ptr cinfo)
  361. /* This routine should do any setup required */
  362. {
  363.   /* This routine can initialize for output based on the data passed in cinfo.
  364.    * Useful fields include:
  365.    *    image_width, image_height    Pretty obvious, I hope.
  366.    *    data_precision            bits per pixel value; typically 8.
  367.    *    out_color_space            output colorspace previously requested
  368.    *    color_out_comps            number of color components in same
  369.    *    final_out_comps            number of components actually output
  370.    * final_out_comps is 1 if quantize_colors is true, else it is equal to
  371.    * color_out_comps.
  372.    *
  373.    * If you have requested color quantization, the colormap is NOT yet set.
  374.    * You may wish to defer output initialization until put_color_map is called.
  375.    */
  376. }
  377.  
  378.  
  379. /*
  380.  * This routine is called if and only if you have set cinfo->quantize_colors
  381.  * to TRUE.  It is given the selected colormap and can complete any required
  382.  * initialization.  This call will occur after output_init and before any
  383.  * calls to put_pixel_rows.  Note that the colormap pointer is also placed
  384.  * in a cinfo field, whence it can be used by put_pixel_rows or output_term.
  385.  * num_colors will be less than or equal to desired_number_of_colors.
  386.  *
  387.  * The colormap data is supplied as a 2-D array of JSAMPLEs, indexed as
  388.  *        JSAMPLE colormap[component][indexvalue]
  389.  * where component runs from 0 to cinfo->color_out_comps-1, and indexvalue
  390.  * runs from 0 to num_colors-1.  Note that this is actually an array of
  391.  * pointers to arrays rather than a true 2D array, since C does not support
  392.  * variable-size multidimensional arrays.
  393.  * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
  394.  * to be as portable as the JPEG code proper, you should always access JSAMPLE
  395.  * values with the GETJSAMPLE() macro, which will do the right thing if the
  396.  * machine has only signed chars.
  397.  */
  398.  
  399. METHODDEF void
  400. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  401. /* Write the color map */
  402. {
  403.   /* You need not provide this routine if you always set cinfo->quantize_colors
  404.    * FALSE; but a safer practice is to provide it and have it just print an
  405.    * error message, like this:
  406.    */
  407.   fprintf(stderr, "put_color_map called: there's a bug here somewhere!\n");
  408. }
  409.  
  410.  
  411. /*
  412.  * This function is called repeatedly, with a few more rows of pixels supplied
  413.  * on each call.  With the current JPEG code, some multiple of 8 rows will be
  414.  * passed on each call except the last, but it is extremely bad form to depend
  415.  * on this.  You CAN assume num_rows > 0.
  416.  * The data is supplied in top-to-bottom row order (the standard order within
  417.  * a JPEG file).  If you cannot readily use the data in that order, you'll
  418.  * need an intermediate array to hold the image.  See jwrrle.c for an example
  419.  * of outputting data in bottom-to-top order.
  420.  *
  421.  * The data is supplied as a 3-D array of JSAMPLEs, indexed as
  422.  *        JSAMPLE pixel_data[component][row][column]
  423.  * where component runs from 0 to cinfo->final_out_comps-1, row runs from 0 to
  424.  * num_rows-1, and column runs from 0 to cinfo->image_width-1 (column 0 is
  425.  * left edge of image).  Note that this is actually an array of pointers to
  426.  * pointers to arrays rather than a true 3D array, since C does not support
  427.  * variable-size multidimensional arrays.
  428.  * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
  429.  * to be as portable as the JPEG code proper, you should always access JSAMPLE
  430.  * values with the GETJSAMPLE() macro, which will do the right thing if the
  431.  * machine has only signed chars.
  432.  *
  433.  * If quantize_colors is true, then there is only one component, and its values
  434.  * are indexes into the previously supplied colormap.  Otherwise the values
  435.  * are actual data in your selected output colorspace.
  436.  */
  437.  
  438.  
  439. METHODDEF void
  440. put_pixel_rows (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE pixel_data)
  441. /* Write some rows of output data */
  442. {
  443.   /* This example shows how you might write full-color RGB data (3 components)
  444.    * to an output file in which the data is stored 3 bytes per pixel.
  445.    */
  446.   register FILE * outfile = cinfo->output_file;
  447.   register JSAMPROW ptr0, ptr1, ptr2;
  448.   register long col;
  449.   register int row;
  450.   
  451.   for (row = 0; row < num_rows; row++) {
  452.     ptr0 = pixel_data[0][row];
  453.     ptr1 = pixel_data[1][row];
  454.     ptr2 = pixel_data[2][row];
  455.     for (col = 0; col < cinfo->image_width; col++) {
  456.       putc(GETJSAMPLE(*ptr0), outfile);    /* red */
  457.       ptr0++;
  458.       putc(GETJSAMPLE(*ptr1), outfile);    /* green */
  459.       ptr1++;
  460.       putc(GETJSAMPLE(*ptr2), outfile);    /* blue */
  461.       ptr2++;
  462.     }
  463.   }
  464. }
  465.  
  466.  
  467. METHODDEF void
  468. output_term (decompress_info_ptr cinfo)
  469. /* Finish up at the end of the output */
  470. {
  471.   /* This termination routine may not need to do anything. */
  472.   /* Note that the JPEG code will only call it during successful exit; */
  473.   /* if you want it called during error exit, you gotta do that yourself. */
  474. }
  475.  
  476.  
  477. /*
  478.  * That's it for the routines that deal with writing the output image.
  479.  * Now we have overall control and parameter selection routines.
  480.  */
  481.  
  482.  
  483. /*
  484.  * This routine gets control after the JPEG file header has been read;
  485.  * at this point the image size and colorspace are known.
  486.  * The routine must determine what output routines are to be used, and make
  487.  * any decompression parameter changes that are desirable.  For example,
  488.  * if it is found that the JPEG file is grayscale, you might want to do
  489.  * things differently than if it is color.  You can also delay setting
  490.  * quantize_colors and associated options until this point. 
  491.  *
  492.  * j_d_defaults initializes out_color_space to CS_RGB.  If you want grayscale
  493.  * output you should set out_color_space to CS_GRAYSCALE.  Note that you can
  494.  * force grayscale output from a color JPEG file (though not vice versa).
  495.  */
  496.  
  497. METHODDEF void
  498. d_ui_method_selection (decompress_info_ptr cinfo)
  499. {
  500.   /* if grayscale input, force grayscale output; */
  501.   /* else leave the output colorspace as set by main routine. */
  502.   if (cinfo->jpeg_color_space == CS_GRAYSCALE)
  503.     cinfo->out_color_space = CS_GRAYSCALE;
  504.  
  505.   /* select output routines */
  506.   cinfo->methods->output_init = output_init;
  507.   cinfo->methods->put_color_map = put_color_map;
  508.   cinfo->methods->put_pixel_rows = put_pixel_rows;
  509.   cinfo->methods->output_term = output_term;
  510. }
  511.  
  512.  
  513. /*
  514.  * OK, here is the main function that actually causes everything to happen.
  515.  * We assume here that the JPEG filename is supplied by the caller of this
  516.  * routine, and that all decompression parameters can be default values.
  517.  * The routine returns 1 if successful, 0 if not.
  518.  */
  519.  
  520. GLOBAL int
  521. read_JPEG_file (char * filename)
  522. {
  523.   /* These three structs contain JPEG parameters and working data.
  524.    * They must survive for the duration of parameter setup and one
  525.    * call to jpeg_decompress; typically, making them local data in the
  526.    * calling routine is the best strategy.
  527.    */
  528.   struct decompress_info_struct cinfo;
  529.   struct decompress_methods_struct dc_methods;
  530.   struct external_methods_struct e_methods;
  531.  
  532.   /* Select the input and output files.
  533.    * In this example we want to open the input file before doing anything else,
  534.    * so that the setjmp() error recovery below can assume the file is open.
  535.    * Note that cinfo.output_file is only used if your output handling routines
  536.    * use it; otherwise, you can just make it NULL.
  537.    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  538.    * requires it in order to read binary files.
  539.    */
  540.  
  541.   if ((cinfo.input_file = fopen(filename, "rb")) == NULL) {
  542.     fprintf(stderr, "can't open %s\n", filename);
  543.     return 0;
  544.   }
  545.  
  546.   cinfo.output_file = NULL;    /* if no actual output file involved */
  547.  
  548.   /* Initialize the system-dependent method pointers. */
  549.   cinfo.methods = &dc_methods;    /* links to method structs */
  550.   cinfo.emethods = &e_methods;
  551.   /* Here we supply our own error handler; compare to use of standard error
  552.    * handler in the previous write_JPEG_file example.
  553.    */
  554.   emethods = &e_methods;    /* save struct addr for possible access */
  555.   e_methods.error_exit = error_exit; /* supply error-exit routine */
  556.   e_methods.trace_message = trace_message; /* supply trace-message routine */
  557.  
  558.   /* prepare setjmp context for possible exit from error_exit */
  559.   if (setjmp(setjmp_buffer)) {
  560.     /* If we get here, the JPEG code has signaled an error.
  561.      * Memory allocation has already been cleaned up (see free_all call in
  562.      * error_exit), but we need to close the input file before returning.
  563.      * You might also need to close an output file, etc.
  564.      */
  565.     fclose(cinfo.input_file);
  566.     return 0;
  567.   }
  568.  
  569.   /* Here we use the standard memory manager provided with the JPEG code.
  570.    * In some cases you might want to replace the memory manager, or at
  571.    * least the system-dependent part of it, with your own code.
  572.    */
  573.   jselmemmgr(&e_methods);    /* select std memory allocation routines */
  574.   /* If the decompressor requires full-image buffers (for two-pass color
  575.    * quantization or a noninterleaved JPEG file), it will create temporary
  576.    * files for anything that doesn't fit within the maximum-memory setting.
  577.    * You can change the default maximum-memory setting by changing
  578.    * e_methods.max_memory_to_use after jselmemmgr returns.
  579.    * On some systems you may also need to set up a signal handler to
  580.    * ensure that temporary files are deleted if the program is interrupted.
  581.    * (This is most important if you are on MS-DOS and use the jmemdos.c
  582.    * memory manager back end; it will try to grab extended memory for
  583.    * temp files, and that space will NOT be freed automatically.)
  584.    * See jcmain.c or jdmain.c for an example signal handler.
  585.    */
  586.  
  587.   /* Here, set up the pointer to your own routine for post-header-reading
  588.    * parameter selection.  You could also initialize the pointers to the
  589.    * output data handling routines here, if they are not dependent on the
  590.    * image type.
  591.    */
  592.   dc_methods.d_ui_method_selection = d_ui_method_selection;
  593.  
  594.   /* Set up default decompression parameters. */
  595.   j_d_defaults(&cinfo, TRUE);
  596.   /* TRUE indicates that an input buffer should be allocated.
  597.    * In unusual cases you may want to allocate the input buffer yourself;
  598.    * see jddeflts.c for commentary.
  599.    */
  600.  
  601.   /* At this point you can modify the default parameters set by j_d_defaults
  602.    * as needed; for example, you can request color quantization or force
  603.    * grayscale output.  See jdmain.c for examples of what you might change.
  604.    */
  605.  
  606.   /* Set up to read a JFIF or baseline-JPEG file. */
  607.   /* This is the only JPEG file format currently supported. */
  608.   jselrjfif(&cinfo);
  609.  
  610.   /* Here we go! */
  611.   jpeg_decompress(&cinfo);
  612.  
  613.   /* That's it, son.  Nothin' else to do, except close files. */
  614.   /* Here we assume only the input file need be closed. */
  615.   fclose(cinfo.input_file);
  616.  
  617.   return 1;            /* indicate success */
  618.  
  619.   /* Note: if you want to decompress more than one image, we recommend you
  620.    * repeat this whole routine.  You MUST repeat the j_d_defaults()/alter
  621.    * parameters/jpeg_decompress() sequence, as some data structures allocated
  622.    * in j_d_defaults are freed upon exit from jpeg_decompress.
  623.    */
  624. }
  625.