home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / DVPG30FS.ZIP / JDDEFLTS.C < prev    next >
C/C++ Source or Header  |  1993-10-24  |  7KB  |  186 lines

  1. /*
  2.  * jddeflts.c
  3.  *
  4.  * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains optional default-setting code for the JPEG decompressor.
  9.  * User interfaces do not have to use this file, but those that don't use it
  10.  * must know more about the innards of the JPEG code.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. /* Default do-nothing progress monitoring routine.
  17.  * This can be overridden by a user interface that wishes to
  18.  * provide progress monitoring; just set methods->progress_monitor
  19.  * after j_d_defaults is done.  The routine will be called periodically
  20.  * during the decompression process.
  21.  *
  22.  * During any one pass, loopcounter increases from 0 up to (not including)
  23.  * looplimit; the step size is not necessarily 1.  Both the step size and
  24.  * the limit may differ between passes.  The expected total number of passes
  25.  * is in cinfo->total_passes, and the number of passes already completed is
  26.  * in cinfo->completed_passes.  Thus the fraction of work completed may be
  27.  * estimated as
  28.  *        completed_passes + (loopcounter/looplimit)
  29.  *        ------------------------------------------
  30.  *                total_passes
  31.  * ignoring the fact that the passes may not be equal amounts of work.
  32.  *
  33.  * When decompressing, the total_passes figure is an estimate that may be
  34.  * on the high side; completed_passes will jump by more than one if some
  35.  * passes are skipped.
  36.  */
  37.  
  38. METHODDEF void
  39. progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
  40. {
  41.   /* do nothing */
  42. }
  43.  
  44.  
  45. /* Default comment-block processing routine.
  46.  * This can be overridden by an application that wishes to examine
  47.  * COM blocks found in the JPEG file.  The default routine does nothing.
  48.  * CAUTION: the comment processing routine MUST call JGETC() exactly
  49.  * comment_length times to read the comment data, whether it intends
  50.  * to do anything with the data or not!
  51.  * Keep in mind that (a) there may be more than one COM block in a file;
  52.  * (b) there's no guarantee that what's in the block is ASCII data.
  53.  */
  54.  
  55. METHODDEF void
  56. process_comment (decompress_info_ptr cinfo, long comment_length)
  57. {
  58.   while (comment_length-- > 0) {
  59.     (void) JGETC(cinfo);
  60.   }
  61. }
  62.  
  63.  
  64. /*
  65.  * Reload the input buffer after it's been emptied, and return the next byte.
  66.  * See the JGETC macro for calling conditions.  Note in particular that
  67.  * read_jpeg_data may NOT return EOF.  If no more data is available, it must
  68.  * exit via ERREXIT, or perhaps synthesize fake data (such as an RST marker).
  69.  * In the present implementation, we insert an EOI marker; this might not be
  70.  * appropriate for non-JFIF file formats, but it usually allows us to handle
  71.  * a truncated JFIF file.
  72.  *
  73.  * This routine can be overridden by the system-dependent user interface,
  74.  * in case the data source is not a stdio stream or some other special
  75.  * condition applies.  Note, however, that this capability only applies for
  76.  * JFIF or similar serial-access JPEG file formats.  The input file control
  77.  * module for a random-access format such as TIFF/JPEG would most likely
  78.  * override the read_jpeg_data method with its own routine.
  79.  */
  80.  
  81. METHODDEF int
  82. read_jpeg_data (decompress_info_ptr cinfo)
  83. {
  84.   cinfo->next_input_byte = cinfo->input_buffer + MIN_UNGET;
  85.  
  86.   cinfo->bytes_in_buffer = (int) JFREAD(cinfo->input_file,
  87.                     cinfo->next_input_byte,
  88.                     JPEG_BUF_SIZE);
  89.   
  90.   if (cinfo->bytes_in_buffer <= 0) {
  91.     WARNMS(cinfo->emethods, "Premature EOF in JPEG file");
  92.     cinfo->next_input_byte[0] = (char) 0xFF;
  93.     cinfo->next_input_byte[1] = (char) 0xD9; /* EOI marker */
  94.     cinfo->bytes_in_buffer = 2;
  95.   }
  96.  
  97.   return JGETC(cinfo);
  98. }
  99.  
  100.  
  101.  
  102. /* Default parameter setup for decompression.
  103.  *
  104.  * User interfaces that don't choose to use this routine must do their
  105.  * own setup of all these parameters.  Alternately, you can call this
  106.  * to establish defaults and then alter parameters selectively.  This
  107.  * is the recommended approach since, if we add any new parameters,
  108.  * your code will still work (they'll be set to reasonable defaults).
  109.  *
  110.  * standard_buffering should be TRUE to cause an input buffer to be allocated
  111.  * (the normal case); if FALSE, the user interface must provide a buffer.
  112.  * This option is most useful in the case that the buffer must not be freed
  113.  * at the end of an image.  (For example, when reading a sequence of images
  114.  * from a single file, the remaining data in the buffer represents the
  115.  * start of the next image and mustn't be discarded.)  To handle this,
  116.  * allocate the input buffer yourself at startup, WITHOUT using alloc_small
  117.  * (probably a direct call to malloc() instead).  Then pass FALSE on each
  118.  * call to j_d_defaults to ensure the buffer state is not modified.
  119.  *
  120.  * If the source of the JPEG data is not a stdio stream, override the
  121.  * read_jpeg_data method with your own routine after calling j_d_defaults.
  122.  * You can still use the standard buffer if it's appropriate.
  123.  *
  124.  * CAUTION: if you want to decompress multiple images per run, it's necessary
  125.  * to call j_d_defaults before *each* call to jpeg_decompress, since subsidiary
  126.  * structures like the quantization tables are automatically freed during
  127.  * cleanup.
  128.  */
  129.  
  130. GLOBAL void
  131. j_d_defaults (decompress_info_ptr cinfo, boolean standard_buffering)
  132. /* NB: the external methods must already be set up. */
  133. {
  134.   short i;
  135.  
  136.   /* Initialize pointers as needed to mark stuff unallocated. */
  137.   /* Outer application may fill in default tables for abbreviated files... */
  138.   cinfo->comp_info = NULL;
  139.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  140.     cinfo->quant_tbl_ptrs[i] = NULL;
  141.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  142.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  143.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  144.   }
  145.   cinfo->colormap = NULL;
  146.  
  147.   /* Default to RGB output */
  148.   /* UI can override by changing out_color_space */
  149.   cinfo->out_color_space = CS_RGB;
  150.   cinfo->jpeg_color_space = CS_UNKNOWN;
  151.   /* Setting any other value in jpeg_color_space overrides heuristics in */
  152.   /* jrdjfif.c.  That might be useful when reading non-JFIF JPEG files, */
  153.   /* but ordinarily the UI shouldn't change it. */
  154.   
  155.   /* Default to no gamma correction of output */
  156.   cinfo->output_gamma = 1.0;
  157.   
  158.   /* Default to no color quantization */
  159.   cinfo->quantize_colors = FALSE;
  160.   /* but set reasonable default parameters for quantization, */
  161.   /* so that turning on quantize_colors is sufficient to do something useful */
  162.   cinfo->two_pass_quantize = TRUE;
  163.   cinfo->use_dithering = TRUE;
  164.   cinfo->desired_number_of_colors = 256;
  165.   
  166.   /* Default to no smoothing */
  167.   cinfo->do_block_smoothing = FALSE;
  168.   cinfo->do_pixel_smoothing = FALSE;
  169.   
  170.   /* Allocate memory for input buffer, unless outer application provides it. */
  171.   if (standard_buffering) {
  172.      cinfo->input_buffer = (char *) (*cinfo->emethods->alloc_small)
  173.                     ((size_t) (JPEG_BUF_SIZE + MIN_UNGET));
  174.     cinfo->bytes_in_buffer = 0;    /* initialize buffer to empty */
  175.   }
  176.  
  177.   /* Install standard buffer-reloading method (outer code may override). */
  178.   cinfo->methods->read_jpeg_data = read_jpeg_data;
  179.  
  180.   /* Install default do-nothing progress monitoring method. */
  181.   cinfo->methods->progress_monitor = progress_monitor;
  182.  
  183.   /* Install default comment-block processing method. */
  184.   cinfo->methods->process_comment = process_comment;
  185. }
  186.