home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / jpeglib_5a.lzh / JPEG_5A / jdmaster.c < prev    next >
Text File  |  1995-01-14  |  23KB  |  649 lines

  1. /*
  2.  * jdmaster.c
  3.  *
  4.  * Copyright (C) 1991-1994, 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 master control logic for the JPEG decompressor.
  9.  * These routines are concerned with selecting the modules to be executed
  10.  * and with determining the number of passes and the work to be done in each
  11.  * pass.
  12.  */
  13.  
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17.  
  18.  
  19. /* Private state */
  20.  
  21. typedef enum {
  22.     main_pass,        /* read and process a single-scan file */
  23.     preread_pass,        /* read one scan of a multi-scan file */
  24.     output_pass,        /* primary processing pass for multi-scan */
  25.     post_pass        /* optional post-pass for 2-pass quant. */
  26. } D_PASS_TYPE;
  27.  
  28. typedef struct {
  29.   struct jpeg_decomp_master pub; /* public fields */
  30.  
  31.   boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
  32.  
  33.   D_PASS_TYPE pass_type;    /* the type of the current pass */
  34.  
  35.   int pass_number;        /* # of passes completed */
  36.   int total_passes;        /* estimated total # of passes needed */
  37.  
  38.   boolean need_post_pass;    /* are we using full two-pass quantization? */
  39. } my_decomp_master;
  40.  
  41. typedef my_decomp_master * my_master_ptr;
  42.  
  43.  
  44. /*
  45.  * Determine whether merged upsample/color conversion should be used.
  46.  * CRUCIAL: this must match the actual capabilities of jdmerge.c!
  47.  */
  48.  
  49. LOCAL boolean
  50. use_merged_upsample (j_decompress_ptr cinfo)
  51. {
  52. #ifdef UPSAMPLE_MERGING_SUPPORTED
  53.   /* Merging is the equivalent of plain box-filter upsampling */
  54.   if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
  55.     return FALSE;
  56.   /* jdmerge.c only supports YCC=>RGB color conversion */
  57.   if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
  58.       cinfo->out_color_space != JCS_RGB ||
  59.       cinfo->out_color_components != RGB_PIXELSIZE)
  60.     return FALSE;
  61.   /* and it only handles 2h1v or 2h2v sampling ratios */
  62.   if (cinfo->comp_info[0].h_samp_factor != 2 ||
  63.       cinfo->comp_info[1].h_samp_factor != 1 ||
  64.       cinfo->comp_info[2].h_samp_factor != 1 ||
  65.       cinfo->comp_info[0].v_samp_factor >  2 ||
  66.       cinfo->comp_info[1].v_samp_factor != 1 ||
  67.       cinfo->comp_info[2].v_samp_factor != 1)
  68.     return FALSE;
  69.   /* furthermore, it doesn't work if we've scaled the IDCTs differently */
  70.   if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
  71.       cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
  72.       cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
  73.     return FALSE;
  74.   /* ??? also need to test for upsample-time rescaling, when & if supported */
  75.   /* by golly, it'll work... */
  76.   return TRUE;
  77. #else
  78.   return FALSE;
  79. #endif
  80. }
  81.  
  82.  
  83. /*
  84.  * Support routines that do various essential calculations.
  85.  *
  86.  * jpeg_calc_output_dimensions is exported for possible use by application.
  87.  * Hence it mustn't do anything that can't be done twice.
  88.  */
  89.  
  90. GLOBAL void
  91. jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
  92. /* Do computations that are needed before master selection phase */
  93. {
  94.   int ci;
  95.   jpeg_component_info *compptr;
  96.  
  97.   /* Compute maximum sampling factors; check factor validity */
  98.   cinfo->max_h_samp_factor = 1;
  99.   cinfo->max_v_samp_factor = 1;
  100.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  101.        ci++, compptr++) {
  102.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  103.     compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  104.       ERREXIT(cinfo, JERR_BAD_SAMPLING);
  105.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  106.                    compptr->h_samp_factor);
  107.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  108.                    compptr->v_samp_factor);
  109.   }
  110.  
  111.   /* Compute actual output image dimensions and DCT scaling choices. */
  112. #ifdef IDCT_SCALING_SUPPORTED
  113.   if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
  114.     /* Provide 1/8 scaling */
  115.     cinfo->output_width = (JDIMENSION)
  116.       jdiv_round_up((long) cinfo->image_width, 8L);
  117.     cinfo->output_height = (JDIMENSION)
  118.       jdiv_round_up((long) cinfo->image_height, 8L);
  119.     cinfo->min_DCT_scaled_size = 1;
  120.   } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
  121.     /* Provide 1/4 scaling */
  122.     cinfo->output_width = (JDIMENSION)
  123.       jdiv_round_up((long) cinfo->image_width, 4L);
  124.     cinfo->output_height = (JDIMENSION)
  125.       jdiv_round_up((long) cinfo->image_height, 4L);
  126.     cinfo->min_DCT_scaled_size = 2;
  127.   } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
  128.     /* Provide 1/2 scaling */
  129.     cinfo->output_width = (JDIMENSION)
  130.       jdiv_round_up((long) cinfo->image_width, 2L);
  131.     cinfo->output_height = (JDIMENSION)
  132.       jdiv_round_up((long) cinfo->image_height, 2L);
  133.     cinfo->min_DCT_scaled_size = 4;
  134.   } else {
  135.     /* Provide 1/1 scaling */
  136.     cinfo->output_width = cinfo->image_width;
  137.     cinfo->output_height = cinfo->image_height;
  138.     cinfo->min_DCT_scaled_size = DCTSIZE;
  139.   }
  140.   /* In selecting the actual DCT scaling for each component, we try to
  141.    * scale up the chroma components via IDCT scaling rather than upsampling.
  142.    * This saves time if the upsampler gets to use 1:1 scaling.
  143.    * Note this code assumes that the supported DCT scalings are powers of 2.
  144.    */
  145.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  146.        ci++, compptr++) {
  147.     int ssize = cinfo->min_DCT_scaled_size;
  148.     while (ssize < DCTSIZE &&
  149.        (compptr->h_samp_factor * ssize * 2 <=
  150.         cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
  151.        (compptr->v_samp_factor * ssize * 2 <=
  152.         cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
  153.       ssize = ssize * 2;
  154.     }
  155.     compptr->DCT_scaled_size = ssize;
  156.   }
  157. #else /* !IDCT_SCALING_SUPPORTED */
  158.   /* Hardwire it to "no scaling" */
  159.   cinfo->output_width = cinfo->image_width;
  160.   cinfo->output_height = cinfo->image_height;
  161.   cinfo->min_DCT_scaled_size = DCTSIZE;
  162.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  163.        ci++, compptr++) {
  164.     compptr->DCT_scaled_size = DCTSIZE;
  165.   }
  166. #endif /* IDCT_SCALING_SUPPORTED */
  167.  
  168.   /* Report number of components in selected colorspace. */
  169.   /* Probably this should be in the color conversion module... */
  170.   switch (cinfo->out_color_space) {
  171.   case JCS_GRAYSCALE:
  172.     cinfo->out_color_components = 1;
  173.     break;
  174.   case JCS_RGB:
  175. #if RGB_PIXELSIZE != 3
  176.     cinfo->out_color_components = RGB_PIXELSIZE;
  177.     break;
  178. #endif /* else share code with YCbCr */
  179.   case JCS_YCbCr:
  180.     cinfo->out_color_components = 3;
  181.     break;
  182.   case JCS_CMYK:
  183.   case JCS_YCCK:
  184.     cinfo->out_color_components = 4;
  185.     break;
  186.   default:            /* else must be same colorspace as in file */
  187.     cinfo->out_color_components = cinfo->num_components;
  188.     break;
  189.   }
  190.   cinfo->output_components = (cinfo->quantize_colors ? 1 :
  191.                   cinfo->out_color_components);
  192.  
  193.   /* See if upsampler will want to emit more than one row at a time */
  194.   if (use_merged_upsample(cinfo))
  195.     cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  196.   else
  197.     cinfo->rec_outbuf_height = 1;
  198.  
  199.   /* Compute various sampling-related dimensions.
  200.    * Some of these are of interest to the application if it is dealing with
  201.    * "raw" (not upsampled) output, so we do the calculations here.
  202.    */
  203.  
  204.   /* Compute dimensions of components */
  205.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  206.        ci++, compptr++) {
  207.     /* Size in DCT blocks */
  208.     compptr->width_in_blocks = (JDIMENSION)
  209.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  210.             (long) (cinfo->max_h_samp_factor * DCTSIZE));
  211.     compptr->height_in_blocks = (JDIMENSION)
  212.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  213.             (long) (cinfo->max_v_samp_factor * DCTSIZE));
  214.     /* Size in samples, after IDCT scaling */
  215.     compptr->downsampled_width = (JDIMENSION)
  216.       jdiv_round_up((long) cinfo->image_width *
  217.             (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
  218.             (long) (cinfo->max_h_samp_factor * DCTSIZE));
  219.     compptr->downsampled_height = (JDIMENSION)
  220.       jdiv_round_up((long) cinfo->image_height *
  221.             (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
  222.             (long) (cinfo->max_v_samp_factor * DCTSIZE));
  223.     /* Mark component needed, until color conversion says otherwise */
  224.     compptr->component_needed = TRUE;
  225.   }
  226.  
  227.   /* Compute number of fully interleaved MCU rows (number of times that
  228.    * main controller will call coefficient controller).
  229.    */
  230.   cinfo->total_iMCU_rows = (JDIMENSION)
  231.     jdiv_round_up((long) cinfo->image_height,
  232.           (long) (cinfo->max_v_samp_factor*DCTSIZE));
  233. }
  234.  
  235.  
  236. LOCAL void
  237. per_scan_setup (j_decompress_ptr cinfo)
  238. /* Do computations that are needed before processing a JPEG scan */
  239. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
  240. {
  241.   int ci, mcublks, tmp;
  242.   jpeg_component_info *compptr;
  243.   
  244.   if (cinfo->comps_in_scan == 1) {
  245.     
  246.     /* Noninterleaved (single-component) scan */
  247.     compptr = cinfo->cur_comp_info[0];
  248.     
  249.     /* Overall image size in MCUs */
  250.     cinfo->MCUs_per_row = compptr->width_in_blocks;
  251.     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  252.     
  253.     /* For noninterleaved scan, always one block per MCU */
  254.     compptr->MCU_width = 1;
  255.     compptr->MCU_height = 1;
  256.     compptr->MCU_blocks = 1;
  257.     compptr->MCU_sample_width = compptr->DCT_scaled_size;
  258.     compptr->last_col_width = 1;
  259.     compptr->last_row_height = 1;
  260.     
  261.     /* Prepare array describing MCU composition */
  262.     cinfo->blocks_in_MCU = 1;
  263.     cinfo->MCU_membership[0] = 0;
  264.     
  265.   } else {
  266.     
  267.     /* Interleaved (multi-component) scan */
  268.     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  269.       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  270.            MAX_COMPS_IN_SCAN);
  271.     
  272.     /* Overall image size in MCUs */
  273.     cinfo->MCUs_per_row = (JDIMENSION)
  274.       jdiv_round_up((long) cinfo->image_width,
  275.             (long) (cinfo->max_h_samp_factor*DCTSIZE));
  276.     cinfo->MCU_rows_in_scan = (JDIMENSION)
  277.       jdiv_round_up((long) cinfo->image_height,
  278.             (long) (cinfo->max_v_samp_factor*DCTSIZE));
  279.     
  280.     cinfo->blocks_in_MCU = 0;
  281.     
  282.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  283.       compptr = cinfo->cur_comp_info[ci];
  284.       /* Sampling factors give # of blocks of component in each MCU */
  285.       compptr->MCU_width = compptr->h_samp_factor;
  286.       compptr->MCU_height = compptr->v_samp_factor;
  287.       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  288.       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
  289.       /* Figure number of non-dummy blocks in last MCU column & row */
  290.       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  291.       if (tmp == 0) tmp = compptr->MCU_width;
  292.       compptr->last_col_width = tmp;
  293.       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  294.       if (tmp == 0) tmp = compptr->MCU_height;
  295.       compptr->last_row_height = tmp;
  296.       /* Prepare array describing MCU composition */
  297.       mcublks = compptr->MCU_blocks;
  298.       if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
  299.     ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  300.       while (mcublks-- > 0) {
  301.     cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  302.       }
  303.     }
  304.     
  305.   }
  306. }
  307.  
  308.  
  309. /*
  310.  * Several decompression processes need to range-limit values to the range
  311.  * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
  312.  * due to noise introduced by quantization, roundoff error, etc.  These
  313.  * processes are inner loops and need to be as fast as possible.  On most
  314.  * machines, particularly CPUs with pipelines or instruction prefetch,
  315.  * a (subscript-check-less) C table lookup
  316.  *        x = sample_range_limit[x];
  317.  * is faster than explicit tests
  318.  *        if (x < 0)  x = 0;
  319.  *        else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
  320.  * These processes all use a common table prepared by the routine below.
  321.  *
  322.  * For most steps we can mathematically guarantee that the initial value
  323.  * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
  324.  * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
  325.  * limiting step (just after the IDCT), a wildly out-of-range value is 
  326.  * possible if the input data is corrupt.  To avoid any chance of indexing
  327.  * off the end of memory and getting a bad-pointer trap, we perform the
  328.  * post-IDCT limiting thus:
  329.  *        x = range_limit[x & MASK];
  330.  * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
  331.  * samples.  Under normal circumstances this is more than enough range and
  332.  * a correct output will be generated; with bogus input data the mask will
  333.  * cause wraparound, and we will safely generate a bogus-but-in-range output.
  334.  * For the post-IDCT step, we want to convert the data from signed to unsigned
  335.  * representation by adding CENTERJSAMPLE at the same time that we limit it.
  336.  * So the post-IDCT limiting table ends up looking like this:
  337.  *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
  338.  *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  339.  *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  340.  *   0,1,...,CENTERJSAMPLE-1
  341.  * Negative inputs select values from the upper half of the table after
  342.  * masking.
  343.  *
  344.  * We can save some space by overlapping the start of the post-IDCT table
  345.  * with the simpler range limiting table.  The post-IDCT table begins at
  346.  * sample_range_limit + CENTERJSAMPLE.
  347.  *
  348.  * Note that the table is allocated in near data space on PCs; it's small
  349.  * enough and used often enough to justify this.
  350.  */
  351.  
  352. LOCAL void
  353. prepare_range_limit_table (j_decompress_ptr cinfo)
  354. /* Allocate and fill in the sample_range_limit table */
  355. {
  356.   JSAMPLE * table;
  357.   int i;
  358.  
  359.   table = (JSAMPLE *)
  360.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  361.         (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  362.   table += (MAXJSAMPLE+1);    /* allow negative subscripts of simple table */
  363.   cinfo->sample_range_limit = table;
  364.   /* First segment of "simple" table: limit[x] = 0 for x < 0 */
  365.   MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
  366.   /* Main part of "simple" table: limit[x] = x */
  367.   for (i = 0; i <= MAXJSAMPLE; i++)
  368.     table[i] = (JSAMPLE) i;
  369.   table += CENTERJSAMPLE;    /* Point to where post-IDCT table starts */
  370.   /* End of simple table, rest of first half of post-IDCT table */
  371.   for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
  372.     table[i] = MAXJSAMPLE;
  373.   /* Second half of post-IDCT table */
  374.   MEMZERO(table + (2 * (MAXJSAMPLE+1)),
  375.       (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  376.   MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
  377.       cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
  378. }
  379.  
  380.  
  381. /*
  382.  * Master selection of decompression modules.
  383.  * This is done once at the start of processing an image.  We determine
  384.  * which modules will be used and give them appropriate initialization calls.
  385.  *
  386.  * Note that this is called only after jpeg_read_header has finished.
  387.  * We therefore know what is in the SOF and (first) SOS markers.
  388.  */
  389.  
  390. LOCAL void
  391. master_selection (j_decompress_ptr cinfo)
  392. {
  393.   my_master_ptr master = (my_master_ptr) cinfo->master;
  394.   long samplesperrow;
  395.   JDIMENSION jd_samplesperrow;
  396.  
  397.   /* Initialize dimensions and other stuff */
  398.   jpeg_calc_output_dimensions(cinfo);
  399.   prepare_range_limit_table(cinfo);
  400.  
  401.   /* Width of an output scanline must be representable as JDIMENSION. */
  402.   samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
  403.   jd_samplesperrow = (JDIMENSION) samplesperrow;
  404.   if ((long) jd_samplesperrow != samplesperrow)
  405.     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  406.  
  407.   /* Initialize my private state */
  408.   master->pub.eoi_processed = FALSE;
  409.   master->pass_number = 0;
  410.   master->need_post_pass = FALSE;
  411.   if (cinfo->comps_in_scan == cinfo->num_components) {
  412.     master->pass_type = main_pass;
  413.     master->total_passes = 1;
  414.   } else {
  415. #ifdef D_MULTISCAN_FILES_SUPPORTED
  416.     master->pass_type = preread_pass;
  417.     /* Assume there is a separate scan for each component; */
  418.     /* if partially interleaved, we'll increment pass_number appropriately */
  419.     master->total_passes = cinfo->num_components + 1;
  420. #else
  421.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  422. #endif
  423.   }
  424.   master->using_merged_upsample = use_merged_upsample(cinfo);
  425.  
  426.   /* There's not a lot of smarts here right now, but it'll get more
  427.    * complicated when we have multiple implementations available...
  428.    */
  429.  
  430.   /* Color quantizer selection */
  431.   if (cinfo->quantize_colors) {
  432.     if (cinfo->raw_data_out)
  433.       ERREXIT(cinfo, JERR_NOTIMPL);
  434. #ifdef QUANT_2PASS_SUPPORTED
  435.     /* 2-pass quantizer only works in 3-component color space.
  436.      * We use the "2-pass" code in a single pass if a colormap is given.
  437.      */
  438.     if (cinfo->out_color_components != 3)
  439.       cinfo->two_pass_quantize = FALSE;
  440.     else if (cinfo->colormap != NULL)
  441.       cinfo->two_pass_quantize = TRUE;
  442. #else
  443.     /* Force 1-pass quantize if we don't have 2-pass code compiled. */
  444.     cinfo->two_pass_quantize = FALSE;
  445. #endif
  446.  
  447.     if (cinfo->two_pass_quantize) {
  448. #ifdef QUANT_2PASS_SUPPORTED
  449.       if (cinfo->colormap == NULL) {
  450.     master->need_post_pass = TRUE;
  451.     master->total_passes++;
  452.       }
  453.       jinit_2pass_quantizer(cinfo);
  454. #else
  455.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  456. #endif
  457.     } else {
  458. #ifdef QUANT_1PASS_SUPPORTED
  459.       jinit_1pass_quantizer(cinfo);
  460. #else
  461.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  462. #endif
  463.     }
  464.   }
  465.  
  466.   /* Post-processing: in particular, color conversion first */
  467.   if (! cinfo->raw_data_out) {
  468.     if (master->using_merged_upsample) {
  469. #ifdef UPSAMPLE_MERGING_SUPPORTED
  470.       jinit_merged_upsampler(cinfo); /* does color conversion too */
  471. #else
  472.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  473. #endif
  474.     } else {
  475.       jinit_color_deconverter(cinfo);
  476.       jinit_upsampler(cinfo);
  477.     }
  478.     jinit_d_post_controller(cinfo, master->need_post_pass);
  479.   }
  480.   /* Inverse DCT */
  481.   jinit_inverse_dct(cinfo);
  482.   /* Entropy decoding: either Huffman or arithmetic coding. */
  483.   if (cinfo->arith_code) {
  484. #ifdef D_ARITH_CODING_SUPPORTED
  485.     jinit_arith_decoder(cinfo);
  486. #else
  487.     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  488. #endif
  489.   } else
  490.     jinit_huff_decoder(cinfo);
  491.  
  492.   jinit_d_coef_controller(cinfo, (master->pass_type == preread_pass));
  493.   jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
  494.   /* Note that main controller is initialized even in raw-data mode. */
  495.  
  496.   /* We can now tell the memory manager to allocate virtual arrays. */
  497.   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  498. }
  499.  
  500.  
  501. /*
  502.  * Per-pass setup.
  503.  * This is called at the beginning of each pass.  We determine which modules
  504.  * will be active during this pass and give them appropriate start_pass calls.
  505.  * We also set is_last_pass to indicate whether any more passes will be
  506.  * required.
  507.  */
  508.  
  509. METHODDEF void
  510. prepare_for_pass (j_decompress_ptr cinfo)
  511. {
  512.   my_master_ptr master = (my_master_ptr) cinfo->master;
  513.  
  514.   switch (master->pass_type) {
  515.   case main_pass:
  516.     /* Set up to read and decompress single-scan file in one pass */
  517.     per_scan_setup(cinfo);
  518.     master->pub.is_last_pass = ! master->need_post_pass;
  519.     if (! cinfo->raw_data_out) {
  520.       if (! master->using_merged_upsample)
  521.     (*cinfo->cconvert->start_pass) (cinfo);
  522.       (*cinfo->upsample->start_pass) (cinfo);
  523.       if (cinfo->quantize_colors)
  524.     (*cinfo->cquantize->start_pass) (cinfo, master->need_post_pass);
  525.       (*cinfo->post->start_pass) (cinfo,
  526.         (master->need_post_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  527.     }
  528.     (*cinfo->idct->start_input_pass) (cinfo);
  529.     (*cinfo->idct->start_output_pass) (cinfo);
  530.     (*cinfo->entropy->start_pass) (cinfo);
  531.     (*cinfo->coef->start_pass) (cinfo, JBUF_PASS_THRU);
  532.     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  533.     break;
  534. #ifdef D_MULTISCAN_FILES_SUPPORTED
  535.   case preread_pass:
  536.     /* Read (another) scan of a multi-scan file */
  537.     per_scan_setup(cinfo);
  538.     master->pub.is_last_pass = FALSE;
  539.     (*cinfo->idct->start_input_pass) (cinfo);
  540.     (*cinfo->entropy->start_pass) (cinfo);
  541.     (*cinfo->coef->start_pass) (cinfo, JBUF_SAVE_SOURCE);
  542.     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_SOURCE);
  543.     break;
  544.   case output_pass:
  545.     /* All scans read, now do the IDCT and subsequent processing */
  546.     master->pub.is_last_pass = ! master->need_post_pass;
  547.     if (! cinfo->raw_data_out) {
  548.       if (! master->using_merged_upsample)
  549.     (*cinfo->cconvert->start_pass) (cinfo);
  550.       (*cinfo->upsample->start_pass) (cinfo);
  551.       if (cinfo->quantize_colors)
  552.     (*cinfo->cquantize->start_pass) (cinfo, master->need_post_pass);
  553.       (*cinfo->post->start_pass) (cinfo,
  554.         (master->need_post_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  555.     }
  556.     (*cinfo->idct->start_output_pass) (cinfo);
  557.     (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  558.     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  559.     break;
  560. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  561. #ifdef QUANT_2PASS_SUPPORTED
  562.   case post_pass:
  563.     /* Final pass of 2-pass quantization */
  564.     master->pub.is_last_pass = TRUE;
  565.     (*cinfo->cquantize->start_pass) (cinfo, FALSE);
  566.     (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
  567.     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
  568.     break;
  569. #endif /* QUANT_2PASS_SUPPORTED */
  570.   default:
  571.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  572.   }
  573.  
  574.   /* Set up progress monitor's pass info if present */
  575.   if (cinfo->progress != NULL) {
  576.     cinfo->progress->completed_passes = master->pass_number;
  577.     cinfo->progress->total_passes = master->total_passes;
  578.   }
  579. }
  580.  
  581.  
  582. /*
  583.  * Finish up at end of pass.
  584.  * In multi-scan mode, we must read next scan header and set the next
  585.  * pass_type correctly for prepare_for_pass.
  586.  */
  587.  
  588. METHODDEF void
  589. finish_pass_master (j_decompress_ptr cinfo)
  590. {
  591.   my_master_ptr master = (my_master_ptr) cinfo->master;
  592.  
  593.   switch (master->pass_type) {
  594.   case main_pass:
  595.   case output_pass:
  596.     if (cinfo->quantize_colors)
  597.       (*cinfo->cquantize->finish_pass) (cinfo);
  598.     master->pass_number++;
  599.     master->pass_type = post_pass; /* in case need_post_pass is true */
  600.     break;
  601. #ifdef D_MULTISCAN_FILES_SUPPORTED
  602.   case preread_pass:
  603.     /* Count one pass done for each component in this scan */
  604.     master->pass_number += cinfo->comps_in_scan;
  605.     switch ((*cinfo->marker->read_markers) (cinfo)) {
  606.     case JPEG_HEADER_OK:    /* Found SOS, do another preread pass */
  607.       break;
  608.     case JPEG_HEADER_TABLES_ONLY: /* Found EOI, no more preread passes */
  609.       master->pub.eoi_processed = TRUE;
  610.       master->pass_type = output_pass;
  611.       break;
  612.     case JPEG_SUSPENDED:
  613.       ERREXIT(cinfo, JERR_CANT_SUSPEND);
  614.     }
  615.     break;
  616. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  617. #ifdef QUANT_2PASS_SUPPORTED
  618.   case post_pass:
  619.     (*cinfo->cquantize->finish_pass) (cinfo);
  620.     /* there will be no more passes, don't bother to change state */
  621.     break;
  622. #endif /* QUANT_2PASS_SUPPORTED */
  623.   default:
  624.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  625.   }
  626. }
  627.  
  628.  
  629. /*
  630.  * Initialize master decompression control.
  631.  * This creates my own subrecord and also performs the master selection phase,
  632.  * which causes other modules to create their subrecords.
  633.  */
  634.  
  635. GLOBAL void
  636. jinit_master_decompress (j_decompress_ptr cinfo)
  637. {
  638.   my_master_ptr master;
  639.  
  640.   master = (my_master_ptr)
  641.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  642.                   SIZEOF(my_decomp_master));
  643.   cinfo->master = (struct jpeg_decomp_master *) master;
  644.   master->pub.prepare_for_pass = prepare_for_pass;
  645.   master->pub.finish_pass = finish_pass_master;
  646.  
  647.   master_selection(cinfo);
  648. }
  649.