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

  1. /*
  2.  * jdmaster.c
  3.  *
  4.  * Copyright (C) 1991, 1992, 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 the main control for the JPEG decompressor.
  9.  * The system-dependent (user interface) code should call jpeg_decompress()
  10.  * after doing appropriate setup of the decompress_info_struct parameter.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. METHODDEF void
  17. d_per_scan_method_selection (decompress_info_ptr cinfo)
  18. /* Central point for per-scan method selection */
  19. {
  20.   /* MCU disassembly */
  21.   jseldmcu(cinfo);
  22.   /* Un-subsampling of pixels */
  23.   jselunsubsample(cinfo);
  24. }
  25.  
  26.  
  27. LOCAL void
  28. d_initial_method_selection (decompress_info_ptr cinfo)
  29. /* Central point for initial method selection (after reading file header) */
  30. {
  31.   /* JPEG file scanning method selection is already done. */
  32.   /* So is output file format selection (both are done by user interface). */
  33.  
  34.   /* Entropy decoding: either Huffman or arithmetic coding. */
  35. #ifdef ARITH_CODING_SUPPORTED
  36.   jseldarithmetic(cinfo);
  37. #else
  38.   if (cinfo->arith_code) {
  39.     ERREXIT(cinfo->emethods, "Arithmetic coding not supported");
  40.   }
  41. #endif
  42.   jseldhuffman(cinfo);
  43.   /* Cross-block smoothing */
  44. #ifdef BLOCK_SMOOTHING_SUPPORTED
  45.   jselbsmooth(cinfo);
  46. #else
  47.   cinfo->do_block_smoothing = FALSE;
  48. #endif
  49.   /* Gamma and color space conversion */
  50.   jseldcolor(cinfo);
  51.  
  52.   /* Color quantization selection rules */
  53. #ifdef QUANT_1PASS_SUPPORTED
  54. #ifdef QUANT_2PASS_SUPPORTED
  55.   /* We have both, check for conditions in which 1-pass should be used */
  56.   if (cinfo->num_components != 3 || cinfo->jpeg_color_space != CS_YCbCr)
  57.     cinfo->two_pass_quantize = FALSE; /* 2-pass only handles YCbCr input */
  58.   if (cinfo->out_color_space == CS_GRAYSCALE)
  59.     cinfo->two_pass_quantize = FALSE; /* Should use 1-pass for grayscale out */
  60. #else /* not QUANT_2PASS_SUPPORTED */
  61.   cinfo->two_pass_quantize = FALSE; /* only have 1-pass */
  62. #endif
  63. #else /* not QUANT_1PASS_SUPPORTED */
  64. #ifdef QUANT_2PASS_SUPPORTED
  65.   cinfo->two_pass_quantize = TRUE; /* only have 2-pass */
  66. #else /* not QUANT_2PASS_SUPPORTED */
  67.   if (cinfo->quantize_colors) {
  68.     ERREXIT(cinfo->emethods, "Color quantization was not compiled");
  69.   }
  70. #endif
  71. #endif
  72.  
  73. #ifdef QUANT_1PASS_SUPPORTED
  74.   jsel1quantize(cinfo);
  75. #endif
  76. #ifdef QUANT_2PASS_SUPPORTED
  77.   jsel2quantize(cinfo);
  78. #endif
  79.  
  80.   /* Pipeline control */
  81.   jseldpipeline(cinfo);
  82.   /* Overall control (that's me!) */
  83.   cinfo->methods->d_per_scan_method_selection = d_per_scan_method_selection;
  84. }
  85.  
  86.  
  87. LOCAL void
  88. initial_setup (decompress_info_ptr cinfo)
  89. /* Do computations that are needed before initial method selection */
  90. {
  91.   short ci;
  92.   jpeg_component_info *compptr;
  93.  
  94.   /* Compute maximum sampling factors; check factor validity */
  95.   cinfo->max_h_samp_factor = 1;
  96.   cinfo->max_v_samp_factor = 1;
  97.   for (ci = 0; ci < cinfo->num_components; ci++) {
  98.     compptr = &cinfo->comp_info[ci];
  99.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  100.     compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  101.       ERREXIT(cinfo->emethods, "Bogus sampling factors");
  102.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  103.                    compptr->h_samp_factor);
  104.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  105.                    compptr->v_samp_factor);
  106.  
  107.   }
  108.  
  109.   /* Compute logical subsampled dimensions of components */
  110.   for (ci = 0; ci < cinfo->num_components; ci++) {
  111.     compptr = &cinfo->comp_info[ci];
  112.     compptr->true_comp_width = (cinfo->image_width * compptr->h_samp_factor
  113.                 + cinfo->max_h_samp_factor - 1)
  114.                 / cinfo->max_h_samp_factor;
  115.     compptr->true_comp_height = (cinfo->image_height * compptr->v_samp_factor
  116.                  + cinfo->max_v_samp_factor - 1)
  117.                  / cinfo->max_v_samp_factor;
  118.   }
  119. }
  120.  
  121.  
  122. /*
  123.  * This is the main entry point to the JPEG decompressor.
  124.  */
  125.  
  126.  
  127. GLOBAL void
  128. jpeg_decompress (decompress_info_ptr cinfo)
  129. {
  130.   /* Init pass counts to 0 --- total_passes is adjusted in method selection */
  131.   cinfo->total_passes = 0;
  132.   cinfo->completed_passes = 0;
  133.  
  134.   /* Read the JPEG file header markers; everything up through the first SOS
  135.    * marker is read now.  NOTE: the user interface must have initialized the
  136.    * read_file_header method pointer (eg, by calling jselrjfif or jselrtiff).
  137.    * The other file reading methods (read_scan_header etc.) were probably
  138.    * set at the same time, but could be set up by read_file_header itself.
  139.    */
  140.   (*cinfo->methods->read_file_header) (cinfo);
  141.   if (! ((*cinfo->methods->read_scan_header) (cinfo)))
  142.     ERREXIT(cinfo->emethods, "Empty JPEG file");
  143.  
  144.   /* Give UI a chance to adjust decompression parameters and select */
  145.   /* output file format based on info from file header. */
  146.   (*cinfo->methods->d_ui_method_selection) (cinfo);
  147.  
  148.   /* Now select methods for decompression steps. */
  149.   initial_setup(cinfo);
  150.   d_initial_method_selection(cinfo);
  151.  
  152.   /* Initialize the output file & other modules as needed */
  153.   /* (modules needing per-scan init are called by pipeline controller) */
  154.  
  155.   (*cinfo->methods->output_init) (cinfo);
  156.   (*cinfo->methods->colorout_init) (cinfo);
  157.   if (cinfo->quantize_colors)
  158.     (*cinfo->methods->color_quant_init) (cinfo);
  159.  
  160.   /* And let the pipeline controller do the rest. */
  161.   (*cinfo->methods->d_pipeline_controller) (cinfo);
  162.  
  163.   /* Finish output file, release working storage, etc */
  164.   if (cinfo->quantize_colors)
  165.     (*cinfo->methods->color_quant_term) (cinfo);
  166.   (*cinfo->methods->colorout_term) (cinfo);
  167.   (*cinfo->methods->output_term) (cinfo);
  168.   (*cinfo->methods->read_file_trailer) (cinfo);
  169.  
  170.   (*cinfo->emethods->free_all) ();
  171.  
  172.   /* My, that was easy, wasn't it? */
  173. }
  174.