home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / contrib / seejpeg / seejpeg-.4 / seejpeg- / seejpeg-1.4.3 / jpeg.c < prev    next >
C/C++ Source or Header  |  1994-03-19  |  15KB  |  367 lines

  1. /*
  2.  * Previously example.c from the JPEG distribution.
  3.  */
  4.  
  5. #include "seejpeg.h"
  6. #include <setjmp.h>
  7. #include <vga.h>
  8.  
  9. /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
  10.  
  11. /* This half of the example shows how to read data from the JPEG decompressor.
  12.  * It's a little more refined than the above in that we show how to do your
  13.  * own error recovery.  If you don't care about that, you don't need these
  14.  * next two routines.
  15.  */
  16.  
  17.  
  18. /*
  19.  * These routines replace the default trace/error routines included with the
  20.  * JPEG code.  The example trace_message routine shown here is actually the
  21.  * same as the standard one, but you could modify it if you don't want messages
  22.  * sent to stderr.  The example error_exit routine is set up to return
  23.  * control to read_JPEG_file() rather than calling exit().  You can use the
  24.  * same routines for both compression and decompression error recovery.
  25.  */
  26.  
  27. /* These variables are needed by the error routines. */
  28. jmp_buf setjmp_buffer;    /* for return to caller */
  29. external_methods_ptr emethods; /* needed for access to message_parm */
  30.  
  31.  
  32. /* This routine is used for any and all trace, debug, or error printouts
  33.  * from the JPEG code.  The parameter is a printf format string; up to 8
  34.  * integer data values for the format string have been stored in the
  35.  * message_parm[] field of the external_methods struct.
  36.  */
  37.  
  38. /*METHODDEF*/ void
  39. trace_message (const char *msgtext)
  40. {
  41.   fprintf(stderr, msgtext,
  42.       emethods->message_parm[0], emethods->message_parm[1],
  43.       emethods->message_parm[2], emethods->message_parm[3],
  44.       emethods->message_parm[4], emethods->message_parm[5],
  45.       emethods->message_parm[6], emethods->message_parm[7]);
  46.   fprintf(stderr, "\n");    /* there is no \n in the format string! */
  47. }
  48.  
  49. /*
  50.  * The error_exit() routine should not return to its caller.  The default
  51.  * routine calls exit(), but here we assume that we want to return to
  52.  * read_JPEG_file, which has set up a setjmp context for the purpose.
  53.  * You should make sure that the free_all method is called, either within
  54.  * error_exit or after the return to the outer-level routine.
  55.  */
  56.  
  57. /*METHODDEF*/ void
  58. error_exit (const char *msgtext)
  59. {
  60.   trace_message(msgtext);    /* report the error message */
  61.   (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  62.   display_shutdown();
  63.   longjmp(setjmp_buffer, 1);    /* return control to outer routine */
  64. }
  65.  
  66.  
  67.  
  68. /*
  69.  * To accept the image data from decompression, you must define four routines
  70.  * output_init, put_color_map, put_pixel_rows, and output_term.
  71.  *
  72.  * You must understand the distinction between full color output mode
  73.  * (N independent color components) and colormapped output mode (a single
  74.  * output component representing an index into a color map).  You should use
  75.  * colormapped mode to write to a colormapped display screen or output file.
  76.  * Colormapped mode is also useful for reducing grayscale output to a small
  77.  * number of gray levels: when using the 1-pass quantizer on grayscale data,
  78.  * the colormap entries will be evenly spaced from 0 to MAX_JSAMPLE, so you
  79.  * can regard the indexes are directly representing gray levels at reduced
  80.  * precision.  In any other case, you should not depend on the colormap
  81.  * entries having any particular order.
  82.  * To get colormapped output, set cinfo->quantize_colors to TRUE and set
  83.  * cinfo->desired_number_of_colors to the maximum number of entries in the
  84.  * colormap.  This can be done either in your main routine or in
  85.  * d_ui_method_selection.  For grayscale quantization, also set
  86.  * cinfo->two_pass_quantize to FALSE to ensure the 1-pass quantizer is used
  87.  * (presently this is the default, but it may not be so in the future).
  88.  *
  89.  * The output file writing modules (jwrppm.c, jwrgif.c, jwrtarga.c, etc) may be
  90.  * useful examples of what these routines should actually do, although each of
  91.  * them is encrusted with a lot of specialized code for its own file format.
  92.  */
  93.  
  94. METHODDEF void
  95. output_init (decompress_info_ptr cinfo)
  96. /* This routine should do any setup required */
  97. {
  98.   /* This routine can initialize for output based on the data passed in cinfo.
  99.    * Useful fields include:
  100.    *    image_width, image_height    Pretty obvious, I hope.
  101.    *    data_precision            bits per pixel value; typically 8.
  102.    *    out_color_space            output colorspace previously requested
  103.    *    color_out_comps            number of color components in same
  104.    *    final_out_comps            number of components actually output
  105.    * final_out_comps is 1 if quantize_colors is true, else it is equal to
  106.    * color_out_comps.
  107.    *
  108.    * If you have requested color quantization, the colormap is NOT yet set.
  109.    * You may wish to defer output initialization until put_color_map is called.
  110.    */
  111.  
  112.   display_init(cinfo->image_width, cinfo->image_height,
  113.            cinfo->final_out_comps);
  114. }
  115.  
  116.  
  117. /*
  118.  * This routine is called if and only if you have set cinfo->quantize_colors
  119.  * to TRUE.  It is given the selected colormap and can complete any required
  120.  * initialization.  This call will occur after output_init and before any
  121.  * calls to put_pixel_rows.  Note that the colormap pointer is also placed
  122.  * in a cinfo field, whence it can be used by put_pixel_rows or output_term.
  123.  * num_colors will be less than or equal to desired_number_of_colors.
  124.  *
  125.  * The colormap data is supplied as a 2-D array of JSAMPLEs, indexed as
  126.  *        JSAMPLE colormap[component][indexvalue]
  127.  * where component runs from 0 to cinfo->color_out_comps-1, and indexvalue
  128.  * runs from 0 to num_colors-1.  Note that this is actually an array of
  129.  * pointers to arrays rather than a true 2D array, since C does not support
  130.  * variable-size multidimensional arrays.
  131.  * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
  132.  * to be as portable as the JPEG code proper, you should always access JSAMPLE
  133.  * values with the GETJSAMPLE() macro, which will do the right thing if the
  134.  * machine has only signed chars.
  135.  */
  136.  
  137. METHODDEF void
  138. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  139. /* Write the color map */
  140. {
  141.   display_set_palette(num_colors, colormap, cinfo->color_out_comps);
  142. }
  143.  
  144.  
  145. /*
  146.  * This function is called repeatedly, with a few more rows of pixels supplied
  147.  * on each call.  With the current JPEG code, some multiple of 8 rows will be
  148.  * passed on each call except the last, but it is extremely bad form to depend
  149.  * on this.  You CAN assume num_rows > 0.
  150.  * The data is supplied in top-to-bottom row order (the standard order within
  151.  * a JPEG file).  If you cannot readily use the data in that order, you'll
  152.  * need an intermediate array to hold the image.  See jwrrle.c for an example
  153.  * of outputting data in bottom-to-top order.
  154.  *
  155.  * The data is supplied as a 3-D array of JSAMPLEs, indexed as
  156.  *        JSAMPLE pixel_data[component][row][column]
  157.  * where component runs from 0 to cinfo->final_out_comps-1, row runs from 0 to
  158.  * num_rows-1, and column runs from 0 to cinfo->image_width-1 (column 0 is
  159.  * left edge of image).  Note that this is actually an array of pointers to
  160.  * pointers to arrays rather than a true 3D array, since C does not support
  161.  * variable-size multidimensional arrays.
  162.  * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
  163.  * to be as portable as the JPEG code proper, you should always access JSAMPLE
  164.  * values with the GETJSAMPLE() macro, which will do the right thing if the
  165.  * machine has only signed chars.
  166.  *
  167.  * If quantize_colors is true, then there is only one component, and its values
  168.  * are indexes into the previously supplied colormap.  Otherwise the values
  169.  * are actual data in your selected output colorspace.
  170.  */
  171.  
  172.  
  173. METHODDEF void
  174. put_pixel_rows (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE pixel_data)
  175. /* Write some rows of output data */
  176. {
  177.   display_rows(num_rows, pixel_data, cinfo->image_width,
  178.            cinfo->final_out_comps);
  179. }
  180.  
  181.  
  182. METHODDEF void
  183. output_term (decompress_info_ptr cinfo)
  184. /* Finish up at the end of the output */
  185. {
  186.   /* This termination routine may not need to do anything. */
  187.   /* Note that the JPEG code will only call it during successful exit; */
  188.   /* if you want it called during error exit, you gotta do that yourself. */
  189.  
  190.   scroll_until_end();
  191. }
  192.  
  193.  
  194. /*
  195.  * That's it for the routines that deal with writing the output image.
  196.  * Now we have overall control and parameter selection routines.
  197.  */
  198.  
  199.  
  200. /*
  201.  * This routine gets control after the JPEG file header has been read;
  202.  * at this point the image size and colorspace are known.
  203.  * The routine must determine what output routines are to be used, and make
  204.  * any decompression parameter changes that are desirable.  For example,
  205.  * if it is found that the JPEG file is grayscale, you might want to do
  206.  * things differently than if it is color.  You can also delay setting
  207.  * quantize_colors and associated options until this point. 
  208.  *
  209.  * j_d_defaults initializes out_color_space to CS_RGB.  If you want grayscale
  210.  * output you should set out_color_space to CS_GRAYSCALE.  Note that you can
  211.  * force grayscale output from a color JPEG file (though not vice versa).
  212.  */
  213.  
  214. METHODDEF void
  215. d_ui_method_selection (decompress_info_ptr cinfo)
  216. {
  217.   /* set 256 colour, colourmapped,  1 pass quantizing if greyscale
  218.    * else we'll use 32768 colour, 2 pass quantizing.
  219.    * this may change later if we want to add using a colourmap in
  220.    * colour modes.
  221.    */
  222.   if (cinfo->jpeg_color_space == CS_GRAYSCALE || opt_greyscale) {
  223.     cinfo->out_color_space = CS_GRAYSCALE;
  224.     cinfo->quantize_colors = TRUE;
  225.     cinfo->desired_number_of_colors = 256;
  226.     cinfo->two_pass_quantize = FALSE;
  227.   } else if (opt_quantize || opt_onepass || !vga_hasmode(TESTMODE)) {
  228.     cinfo->quantize_colors = TRUE;
  229.     cinfo->desired_number_of_colors = 256;
  230.     if (opt_onepass) {
  231.       cinfo->two_pass_quantize = FALSE;
  232.     } else {
  233.       cinfo->two_pass_quantize = TRUE;
  234.     }
  235.   } else {
  236.     cinfo->quantize_colors = FALSE;
  237.     cinfo->two_pass_quantize = TRUE;
  238.   }
  239.  
  240.   /* select output routines */
  241.   cinfo->methods->output_init = output_init;
  242.   cinfo->methods->put_color_map = put_color_map;
  243.   cinfo->methods->put_pixel_rows = put_pixel_rows;
  244.   cinfo->methods->output_term = output_term;
  245. }
  246.  
  247.  
  248. /*
  249.  * OK, here is the main function that actually causes everything to happen.
  250.  * We assume here that the JPEG filename is supplied by the caller of this
  251.  * routine, and that all decompression parameters can be default values.
  252.  * The routine returns 1 if successful, 0 if not.
  253.  */
  254.  
  255. GLOBAL int
  256. read_JPEG_file (char * filename)
  257. {
  258.   /* These three structs contain JPEG parameters and working data.
  259.    * They must survive for the duration of parameter setup and one
  260.    * call to jpeg_decompress; typically, making them local data in the
  261.    * calling routine is the best strategy.
  262.    */
  263.   struct Decompress_info_struct cinfo;
  264.   struct Decompress_methods_struct dc_methods;
  265.   struct External_methods_struct e_methods;
  266.  
  267.   /* Select the input and output files.
  268.    * In this example we want to open the input file before doing anything else,
  269.    * so that the setjmp() error recovery below can assume the file is open.
  270.    * Note that cinfo.output_file is only used if your output handling routines
  271.    * use it; otherwise, you can just make it NULL.
  272.    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  273.    * requires it in order to read binary files.
  274.    */
  275.  
  276.   if ((cinfo.input_file = fopen(filename, "rb")) == NULL) {
  277.     fprintf(stderr, "can't open %s\n", filename);
  278.     return 0;
  279.   }
  280.  
  281.   cinfo.output_file = NULL;    /* if no actual output file involved */
  282.  
  283.   /* Initialize the system-dependent method pointers. */
  284.   cinfo.methods = &dc_methods;    /* links to method structs */
  285.   cinfo.emethods = &e_methods;
  286.   /* Here we supply our own error handler; compare to use of standard error
  287.    * handler in the previous write_JPEG_file example.
  288.    */
  289.   emethods = &e_methods;    /* save struct addr for possible access */
  290.   e_methods.error_exit = error_exit; /* supply error-exit routine */
  291.   e_methods.trace_message = trace_message; /* supply trace-message routine */
  292.   e_methods.trace_level = 0;    /* default = no tracing */
  293.   e_methods.num_warnings = 0;    /* no warnings emitted yet */
  294.   e_methods.first_warning_level = 0; /* display first corrupt-data warning */
  295.   e_methods.more_warning_level = 3; /* but suppress additional ones */
  296.  
  297.   /* prepare setjmp context for possible exit from error_exit */
  298.   if (setjmp(setjmp_buffer)) {
  299.     /* If we get here, the JPEG code has signaled an error.
  300.      * Memory allocation has already been cleaned up (see free_all call in
  301.      * error_exit), but we need to close the input file before returning.
  302.      * You might also need to close an output file, etc.
  303.      */
  304.     fclose(cinfo.input_file);
  305.     return 0;
  306.   }
  307.  
  308.   /* Here we use the standard memory manager provided with the JPEG code.
  309.    * In some cases you might want to replace the memory manager, or at
  310.    * least the system-dependent part of it, with your own code.
  311.    */
  312.   jselmemmgr(&e_methods);    /* select std memory allocation routines */
  313.   /* If the decompressor requires full-image buffers (for two-pass color
  314.    * quantization or a noninterleaved JPEG file), it will create temporary
  315.    * files for anything that doesn't fit within the maximum-memory setting.
  316.    * You can change the default maximum-memory setting by changing
  317.    * e_methods.max_memory_to_use after jselmemmgr returns.
  318.    * On some systems you may also need to set up a signal handler to
  319.    * ensure that temporary files are deleted if the program is interrupted.
  320.    * (This is most important if you are on MS-DOS and use the jmemdos.c
  321.    * memory manager back end; it will try to grab extended memory for
  322.    * temp files, and that space will NOT be freed automatically.)
  323.    * See jcmain.c or jdmain.c for an example signal handler.
  324.    */
  325.  
  326.   /* Here, set up the pointer to your own routine for post-header-reading
  327.    * parameter selection.  You could also initialize the pointers to the
  328.    * output data handling routines here, if they are not dependent on the
  329.    * image type.
  330.    */
  331.   dc_methods.d_ui_method_selection = d_ui_method_selection;
  332.  
  333.   /* Set up default decompression parameters. */
  334.   j_d_defaults(&cinfo, TRUE);
  335.   /* TRUE indicates that an input buffer should be allocated.
  336.    * In unusual cases you may want to allocate the input buffer yourself;
  337.    * see jddeflts.c for commentary.
  338.    */
  339.  
  340.   /* At this point you can modify the default parameters set by j_d_defaults
  341.    * as needed; for example, you can request color quantization or force
  342.    * grayscale output.  See jdmain.c for examples of what you might change.
  343.    */
  344.  
  345.   /* Set up to read a JFIF or baseline-JPEG file. */
  346.   /* This is the only JPEG file format currently supported. */
  347.   jselrjfif(&cinfo);
  348.  
  349.   /* Here we go! */
  350.   jpeg_decompress(&cinfo);
  351.  
  352.   /* That's it, son.  Nothin' else to do, except close files. */
  353.   /* Here we assume only the input file need be closed. */
  354.   fclose(cinfo.input_file);
  355.  
  356.   /* You might want to test e_methods.num_warnings to see if bad data was
  357.    * detected.  In this example, we just blindly forge ahead.
  358.    */
  359.   return 1;            /* indicate success */
  360.  
  361.   /* Note: if you want to decompress more than one image, we recommend you
  362.    * repeat this whole routine.  You MUST repeat the j_d_defaults()/alter
  363.    * parameters/jpeg_decompress() sequence, as some data structures allocated
  364.    * in j_d_defaults are freed upon exit from jpeg_decompress.
  365.    */
  366. }
  367.