home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / JPEGSRC.V4.lzh / jdpipe.c < prev    next >
Text File  |  1993-01-14  |  38KB  |  1,049 lines

  1. /*
  2.  * jdpipe.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 decompression pipeline controllers.
  9.  * These routines are invoked via the d_pipeline_controller method.
  10.  *
  11.  * There are two basic pipeline controllers.  The simpler one handles a
  12.  * single-scan JPEG file (single component or fully interleaved) with no
  13.  * color quantization or 1-pass quantization.  In this case, the file can
  14.  * be processed in one top-to-bottom pass.  The more complex controller is
  15.  * used when 2-pass color quantization is requested and/or the JPEG file
  16.  * has multiple scans (noninterleaved or partially interleaved).  In this
  17.  * case, the entire image must be buffered up in a "big" array.
  18.  *
  19.  * If you need to make a minimal implementation, the more complex controller
  20.  * can be compiled out by disabling the appropriate configuration options.
  21.  * We don't recommend this, since then you can't handle all legal JPEG files.
  22.  */
  23.  
  24. #include "jinclude.h"
  25.  
  26.  
  27. #ifdef D_MULTISCAN_FILES_SUPPORTED /* wish we could assume ANSI's defined() */
  28. #define NEED_COMPLEX_CONTROLLER
  29. #else
  30. #ifdef QUANT_2PASS_SUPPORTED
  31. #define NEED_COMPLEX_CONTROLLER
  32. #endif
  33. #endif
  34.  
  35.  
  36. /*
  37.  * About the data structures:
  38.  *
  39.  * The processing chunk size for upsampling is referred to in this file as
  40.  * a "row group": a row group is defined as Vk (v_samp_factor) sample rows of
  41.  * any component while downsampled, or Vmax (max_v_samp_factor) unsubsampled
  42.  * rows.  In an interleaved scan each MCU row contains exactly DCTSIZE row
  43.  * groups of each component in the scan.  In a noninterleaved scan an MCU row
  44.  * is one row of blocks, which might not be an integral number of row groups;
  45.  * therefore, we read in Vk MCU rows to obtain the same amount of data as we'd
  46.  * have in an interleaved scan.
  47.  * To provide context for the upsampling step, we have to retain the last
  48.  * two row groups of the previous MCU row while reading in the next MCU row
  49.  * (or set of Vk MCU rows).  To do this without copying data about, we create
  50.  * a rather strange data structure.  Exactly DCTSIZE+2 row groups of samples
  51.  * are allocated, but we create two different sets of pointers to this array.
  52.  * The second set swaps the last two pairs of row groups.  By working
  53.  * alternately with the two sets of pointers, we can access the data in the
  54.  * desired order.
  55.  *
  56.  * Cross-block smoothing also needs context above and below the "current" row.
  57.  * Since this is an optional feature, I've implemented it in a way that is
  58.  * much simpler but requires more than the minimum amount of memory.  We
  59.  * simply allocate three extra MCU rows worth of coefficient blocks and use
  60.  * them to "read ahead" one MCU row in the file.  For a typical 1000-pixel-wide
  61.  * image with 2x2,1x1,1x1 sampling, each MCU row is about 50Kb; an 80x86
  62.  * machine may be unable to apply cross-block smoothing to wider images.
  63.  */
  64.  
  65.  
  66. /*
  67.  * These variables are logically local to the pipeline controller,
  68.  * but we make them static so that scan_big_image can use them
  69.  * without having to pass them through the quantization routines.
  70.  */
  71.  
  72. static int rows_in_mem;        /* # of sample rows in full-size buffers */
  73. /* Work buffer for data being passed to output module. */
  74. /* This has color_out_comps components if not quantizing, */
  75. /* but only one component when quantizing. */
  76. static JSAMPIMAGE output_workspace;
  77.  
  78. #ifdef NEED_COMPLEX_CONTROLLER
  79. /* Full-size image array holding upsampled, but not color-processed data. */
  80. static big_sarray_ptr *fullsize_image;
  81. static JSAMPIMAGE fullsize_ptrs; /* workspace for access_big_sarray() result */
  82. #endif
  83.  
  84.  
  85. /*
  86.  * Utility routines: common code for pipeline controllers
  87.  */
  88.  
  89. LOCAL void
  90. interleaved_scan_setup (decompress_info_ptr cinfo)
  91. /* Compute all derived info for an interleaved (multi-component) scan */
  92. /* On entry, cinfo->comps_in_scan and cinfo->cur_comp_info[] are set up */
  93. {
  94.   short ci, mcublks;
  95.   jpeg_component_info *compptr;
  96.  
  97.   if (cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  98.     ERREXIT(cinfo->emethods, "Too many components for interleaved scan");
  99.  
  100.   cinfo->MCUs_per_row = (cinfo->image_width
  101.              + cinfo->max_h_samp_factor*DCTSIZE - 1)
  102.             / (cinfo->max_h_samp_factor*DCTSIZE);
  103.  
  104.   cinfo->MCU_rows_in_scan = (cinfo->image_height
  105.                  + cinfo->max_v_samp_factor*DCTSIZE - 1)
  106.                 / (cinfo->max_v_samp_factor*DCTSIZE);
  107.   
  108.   cinfo->blocks_in_MCU = 0;
  109.  
  110.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  111.     compptr = cinfo->cur_comp_info[ci];
  112.     /* for interleaved scan, sampling factors give # of blocks per component */
  113.     compptr->MCU_width = compptr->h_samp_factor;
  114.     compptr->MCU_height = compptr->v_samp_factor;
  115.     compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  116.     /* compute physical dimensions of component */
  117.     compptr->downsampled_width = jround_up(compptr->true_comp_width,
  118.                        (long) (compptr->MCU_width*DCTSIZE));
  119.     compptr->downsampled_height = jround_up(compptr->true_comp_height,
  120.                         (long) (compptr->MCU_height*DCTSIZE));
  121.     /* Sanity check */
  122.     if (compptr->downsampled_width !=
  123.     (cinfo->MCUs_per_row * (compptr->MCU_width*DCTSIZE)))
  124.       ERREXIT(cinfo->emethods, "I'm confused about the image width");
  125.     /* Prepare array describing MCU composition */
  126.     mcublks = compptr->MCU_blocks;
  127.     if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
  128.       ERREXIT(cinfo->emethods, "Sampling factors too large for interleaved scan");
  129.     while (mcublks-- > 0) {
  130.       cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  131.     }
  132.   }
  133.  
  134.   (*cinfo->methods->d_per_scan_method_selection) (cinfo);
  135. }
  136.  
  137.  
  138. LOCAL void
  139. noninterleaved_scan_setup (decompress_info_ptr cinfo)
  140. /* Compute all derived info for a noninterleaved (single-component) scan */
  141. /* On entry, cinfo->comps_in_scan = 1 and cinfo->cur_comp_info[0] is set up */
  142. {
  143.   jpeg_component_info *compptr = cinfo->cur_comp_info[0];
  144.  
  145.   /* for noninterleaved scan, always one block per MCU */
  146.   compptr->MCU_width = 1;
  147.   compptr->MCU_height = 1;
  148.   compptr->MCU_blocks = 1;
  149.   /* compute physical dimensions of component */
  150.   compptr->downsampled_width = jround_up(compptr->true_comp_width,
  151.                      (long) DCTSIZE);
  152.   compptr->downsampled_height = jround_up(compptr->true_comp_height,
  153.                       (long) DCTSIZE);
  154.  
  155.   cinfo->MCUs_per_row = compptr->downsampled_width / DCTSIZE;
  156.   cinfo->MCU_rows_in_scan = compptr->downsampled_height / DCTSIZE;
  157.  
  158.   /* Prepare array describing MCU composition */
  159.   cinfo->blocks_in_MCU = 1;
  160.   cinfo->MCU_membership[0] = 0;
  161.  
  162.   (*cinfo->methods->d_per_scan_method_selection) (cinfo);
  163. }
  164.  
  165.  
  166.  
  167. LOCAL JSAMPIMAGE
  168. alloc_sampimage (decompress_info_ptr cinfo,
  169.          int num_comps, long num_rows, long num_cols)
  170. /* Allocate an in-memory sample image (all components same size) */
  171. {
  172.   JSAMPIMAGE image;
  173.   int ci;
  174.  
  175.   image = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  176.                 (num_comps * SIZEOF(JSAMPARRAY));
  177.   for (ci = 0; ci < num_comps; ci++) {
  178.     image[ci] = (*cinfo->emethods->alloc_small_sarray) (num_cols, num_rows);
  179.   }
  180.   return image;
  181. }
  182.  
  183.  
  184. #if 0                /* this routine not currently needed */
  185.  
  186. LOCAL void
  187. free_sampimage (decompress_info_ptr cinfo, JSAMPIMAGE image, int num_comps)
  188. /* Release a sample image created by alloc_sampimage */
  189. {
  190.   int ci;
  191.  
  192.   for (ci = 0; ci < num_comps; ci++) {
  193.       (*cinfo->emethods->free_small_sarray) (image[ci]);
  194.   }
  195.   (*cinfo->emethods->free_small) ((void *) image);
  196. }
  197.  
  198. #endif
  199.  
  200.  
  201. LOCAL JBLOCKIMAGE
  202. alloc_MCU_row (decompress_info_ptr cinfo)
  203. /* Allocate one MCU row's worth of coefficient blocks */
  204. {
  205.   JBLOCKIMAGE image;
  206.   int ci;
  207.  
  208.   image = (JBLOCKIMAGE) (*cinfo->emethods->alloc_small)
  209.                 (cinfo->comps_in_scan * SIZEOF(JBLOCKARRAY));
  210.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  211.     image[ci] = (*cinfo->emethods->alloc_small_barray)
  212.             (cinfo->cur_comp_info[ci]->downsampled_width / DCTSIZE,
  213.              (long) cinfo->cur_comp_info[ci]->MCU_height);
  214.   }
  215.   return image;
  216. }
  217.  
  218.  
  219. #ifdef NEED_COMPLEX_CONTROLLER    /* not used by simple controller */
  220.  
  221. LOCAL void
  222. free_MCU_row (decompress_info_ptr cinfo, JBLOCKIMAGE image)
  223. /* Release a coefficient block array created by alloc_MCU_row */
  224. {
  225.   int ci;
  226.  
  227.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  228.     (*cinfo->emethods->free_small_barray) (image[ci]);
  229.   }
  230.   (*cinfo->emethods->free_small) ((void *) image);
  231. }
  232.  
  233. #endif
  234.  
  235.  
  236. LOCAL void
  237. alloc_sampling_buffer (decompress_info_ptr cinfo, JSAMPIMAGE sampled_data[2])
  238. /* Create a downsampled-data buffer having the desired structure */
  239. /* (see comments at head of file) */
  240. {
  241.   short ci, vs, i;
  242.  
  243.   /* Get top-level space for array pointers */
  244.   sampled_data[0] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  245.                 (cinfo->comps_in_scan * SIZEOF(JSAMPARRAY));
  246.   sampled_data[1] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  247.                 (cinfo->comps_in_scan * SIZEOF(JSAMPARRAY));
  248.  
  249.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  250.     vs = cinfo->cur_comp_info[ci]->v_samp_factor; /* row group height */
  251.     /* Allocate the real storage */
  252.     sampled_data[0][ci] = (*cinfo->emethods->alloc_small_sarray)
  253.                 (cinfo->cur_comp_info[ci]->downsampled_width,
  254.                 (long) (vs * (DCTSIZE+2)));
  255.     /* Create space for the scrambled-order pointers */
  256.     sampled_data[1][ci] = (JSAMPARRAY) (*cinfo->emethods->alloc_small)
  257.                 (vs * (DCTSIZE+2) * SIZEOF(JSAMPROW));
  258.     /* Duplicate the first DCTSIZE-2 row groups */
  259.     for (i = 0; i < vs * (DCTSIZE-2); i++) {
  260.       sampled_data[1][ci][i] = sampled_data[0][ci][i];
  261.     }
  262.     /* Copy the last four row groups in swapped order */
  263.     for (i = 0; i < vs * 2; i++) {
  264.       sampled_data[1][ci][vs*DCTSIZE + i] = sampled_data[0][ci][vs*(DCTSIZE-2) + i];
  265.       sampled_data[1][ci][vs*(DCTSIZE-2) + i] = sampled_data[0][ci][vs*DCTSIZE + i];
  266.     }
  267.   }
  268. }
  269.  
  270.  
  271. #ifdef NEED_COMPLEX_CONTROLLER    /* not used by simple controller */
  272.  
  273. LOCAL void
  274. free_sampling_buffer (decompress_info_ptr cinfo, JSAMPIMAGE sampled_data[2])
  275. /* Release a sampling buffer created by alloc_sampling_buffer */
  276. {
  277.   short ci;
  278.  
  279.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  280.     /* Free the real storage */
  281.     (*cinfo->emethods->free_small_sarray) (sampled_data[0][ci]);
  282.     /* Free the scrambled-order pointers */
  283.     (*cinfo->emethods->free_small) ((void *) sampled_data[1][ci]);
  284.   }
  285.  
  286.   /* Free the top-level space */
  287.   (*cinfo->emethods->free_small) ((void *) sampled_data[0]);
  288.   (*cinfo->emethods->free_small) ((void *) sampled_data[1]);
  289. }
  290.  
  291. #endif
  292.  
  293.  
  294. /*
  295.  * Several decompression processes need to range-limit values to the range
  296.  * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
  297.  * due to noise introduced by quantization, roundoff error, etc.  These
  298.  * processes are inner loops and need to be as fast as possible.  On most
  299.  * machines, particularly CPUs with pipelines or instruction prefetch,
  300.  * a (range-check-less) C table lookup
  301.  *        x = sample_range_limit[x];
  302.  * is faster than explicit tests
  303.  *        if (x < 0)  x = 0;
  304.  *        else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
  305.  * These processes all use a common table prepared by the routine below.
  306.  *
  307.  * The table will work correctly for x within MAXJSAMPLE+1 of the legal
  308.  * range.  This is a much wider range than is needed for most cases,
  309.  * but the wide range is handy for color quantization.
  310.  * Note that the table is allocated in near data space on PCs; it's small
  311.  * enough and used often enough to justify this.
  312.  */
  313.  
  314. LOCAL void
  315. prepare_range_limit_table (decompress_info_ptr cinfo)
  316. /* Allocate and fill in the sample_range_limit table */
  317. {
  318.   JSAMPLE * table;
  319.   int i;
  320.  
  321.   table = (JSAMPLE *) (*cinfo->emethods->alloc_small)
  322.             (3 * (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
  323.   cinfo->sample_range_limit = table + (MAXJSAMPLE+1);
  324.   for (i = 0; i <= MAXJSAMPLE; i++) {
  325.     table[i] = 0;            /* sample_range_limit[x] = 0 for x<0 */
  326.     table[i+(MAXJSAMPLE+1)] = (JSAMPLE) i;    /* sample_range_limit[x] = x */
  327.     table[i+(MAXJSAMPLE+1)*2] = MAXJSAMPLE;    /* x beyond MAXJSAMPLE */
  328.   }
  329. }
  330.  
  331.  
  332. LOCAL void
  333. duplicate_row (JSAMPARRAY image_data,
  334.            long num_cols, int source_row, int num_rows)
  335. /* Duplicate the source_row at source_row+1 .. source_row+num_rows */
  336. /* This happens only at the bottom of the image, */
  337. /* so it needn't be super-efficient */
  338. {
  339.   register int row;
  340.  
  341.   for (row = 1; row <= num_rows; row++) {
  342.     jcopy_sample_rows(image_data, source_row, image_data, source_row + row,
  343.               1, num_cols);
  344.   }
  345. }
  346.  
  347.  
  348. LOCAL void
  349. expand (decompress_info_ptr cinfo,
  350.     JSAMPIMAGE sampled_data, JSAMPIMAGE fullsize_data,
  351.     long fullsize_width,
  352.     short above, short current, short below, short out)
  353. /* Do upsampling expansion of a single row group (of each component). */
  354. /* above, current, below are indexes of row groups in sampled_data;       */
  355. /* out is the index of the target row group in fullsize_data.             */
  356. /* Special case: above, below can be -1 to indicate top, bottom of image. */
  357. {
  358.   jpeg_component_info *compptr;
  359.   JSAMPARRAY above_ptr, below_ptr;
  360.   JSAMPROW dummy[MAX_SAMP_FACTOR]; /* for downsample expansion at top/bottom */
  361.   short ci, vs, i;
  362.  
  363.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  364.     compptr = cinfo->cur_comp_info[ci];
  365.     vs = compptr->v_samp_factor; /* row group height */
  366.  
  367.     if (above >= 0)
  368.       above_ptr = sampled_data[ci] + above * vs;
  369.     else {
  370.       /* Top of image: make a dummy above-context with copies of 1st row */
  371.       /* We assume current=0 in this case */
  372.       for (i = 0; i < vs; i++)
  373.     dummy[i] = sampled_data[ci][0];
  374.       above_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
  375.     }
  376.  
  377.     if (below >= 0)
  378.       below_ptr = sampled_data[ci] + below * vs;
  379.     else {
  380.       /* Bot of image: make a dummy below-context with copies of last row */
  381.       for (i = 0; i < vs; i++)
  382.     dummy[i] = sampled_data[ci][(current+1)*vs-1];
  383.       below_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
  384.     }
  385.  
  386.     (*cinfo->methods->upsample[ci])
  387.         (cinfo, (int) ci,
  388.          compptr->downsampled_width, (int) vs,
  389.          fullsize_width, (int) cinfo->max_v_samp_factor,
  390.          above_ptr,
  391.          sampled_data[ci] + current * vs,
  392.          below_ptr,
  393.          fullsize_data[ci] + out * cinfo->max_v_samp_factor);
  394.   }
  395. }
  396.  
  397.  
  398. LOCAL void
  399. emit_1pass (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE fullsize_data,
  400.         JSAMPARRAY dummy)
  401. /* Do color processing and output of num_rows full-size rows. */
  402. /* This is not used when doing 2-pass color quantization. */
  403. /* The dummy argument simply lets this be called via scan_big_image. */
  404. {
  405.   if (cinfo->quantize_colors) {
  406.     (*cinfo->methods->color_quantize) (cinfo, num_rows, fullsize_data,
  407.                        output_workspace[0]);
  408.   } else {
  409.     (*cinfo->methods->color_convert) (cinfo, num_rows, cinfo->image_width,
  410.                       fullsize_data, output_workspace);
  411.   }
  412.     
  413.   (*cinfo->methods->put_pixel_rows) (cinfo, num_rows, output_workspace);
  414. }
  415.  
  416.  
  417. /*
  418.  * Support routines for complex controller.
  419.  */
  420.  
  421. #ifdef NEED_COMPLEX_CONTROLLER
  422.  
  423. METHODDEF void
  424. scan_big_image (decompress_info_ptr cinfo, quantize_method_ptr quantize_method)
  425. /* Apply quantize_method to entire image stored in fullsize_image[]. */
  426. /* This is the "iterator" routine used by the 2-pass color quantizer. */
  427. /* We also use it directly in some cases. */
  428. {
  429.   long pixel_rows_output;
  430.   short ci;
  431.  
  432.   for (pixel_rows_output = 0; pixel_rows_output < cinfo->image_height;
  433.        pixel_rows_output += rows_in_mem) {
  434.     (*cinfo->methods->progress_monitor) (cinfo, pixel_rows_output,
  435.                      cinfo->image_height);
  436.     /* Realign the big buffers */
  437.     for (ci = 0; ci < cinfo->num_components; ci++) {
  438.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  439.     (fullsize_image[ci], pixel_rows_output, FALSE);
  440.     }
  441.     /* Let the quantizer have its way with the data.
  442.      * Note that output_workspace is simply workspace for the quantizer;
  443.      * when it's ready to output, it must call put_pixel_rows itself.
  444.      */
  445.     (*quantize_method) (cinfo,
  446.             (int) MIN((long) rows_in_mem,
  447.                   cinfo->image_height - pixel_rows_output),
  448.             fullsize_ptrs, output_workspace[0]);
  449.   }
  450.  
  451.   cinfo->completed_passes++;
  452. }
  453.  
  454. #endif /* NEED_COMPLEX_CONTROLLER */
  455.  
  456.  
  457. /*
  458.  * Support routines for cross-block smoothing.
  459.  */
  460.  
  461. #ifdef BLOCK_SMOOTHING_SUPPORTED
  462.  
  463.  
  464. LOCAL void
  465. smooth_mcu_row (decompress_info_ptr cinfo,
  466.         JBLOCKIMAGE above, JBLOCKIMAGE input, JBLOCKIMAGE below,
  467.         JBLOCKIMAGE output)
  468. /* Apply cross-block smoothing to one MCU row's worth of coefficient blocks. */
  469. /* above,below are NULL if at top/bottom of image. */
  470. {
  471.   jpeg_component_info *compptr;
  472.   short ci, ri, last;
  473.   JBLOCKROW prev;
  474.  
  475.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  476.     compptr = cinfo->cur_comp_info[ci];
  477.     last = compptr->MCU_height - 1;
  478.  
  479.     if (above == NULL)
  480.       prev = NULL;
  481.     else
  482.       prev = above[ci][last];
  483.  
  484.     for (ri = 0; ri < last; ri++) {
  485.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  486.                 prev, input[ci][ri], input[ci][ri+1],
  487.                 output[ci][ri]);
  488.       prev = input[ci][ri];
  489.     }
  490.  
  491.     if (below == NULL)
  492.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  493.                 prev, input[ci][last], (JBLOCKROW) NULL,
  494.                 output[ci][last]);
  495.     else
  496.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  497.                 prev, input[ci][last], below[ci][0],
  498.                 output[ci][last]);
  499.   }
  500. }
  501.  
  502.  
  503. LOCAL void
  504. get_smoothed_row (decompress_info_ptr cinfo, JBLOCKIMAGE coeff_data,
  505.           JBLOCKIMAGE bsmooth[3], int * whichb, long cur_mcu_row)
  506. /* Get an MCU row of coefficients, applying cross-block smoothing. */
  507. /* The output row is placed in coeff_data.  bsmooth and whichb hold */
  508. /* working state, and cur_row is needed to check for image top/bottom. */
  509. /* This routine just takes care of the buffering logic. */
  510. {
  511.   int prev, cur, next;
  512.   
  513.   /* Special case for top of image: need to pre-fetch a row & init whichb */
  514.   if (cur_mcu_row == 0) {
  515.     (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[0]);
  516.     if (cinfo->MCU_rows_in_scan > 1) {
  517.       (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[1]);
  518.       smooth_mcu_row(cinfo, (JBLOCKIMAGE) NULL, bsmooth[0], bsmooth[1],
  519.              coeff_data);
  520.     } else {
  521.       smooth_mcu_row(cinfo, (JBLOCKIMAGE) NULL, bsmooth[0], (JBLOCKIMAGE) NULL,
  522.              coeff_data);
  523.     }
  524.     *whichb = 1;        /* points to next bsmooth[] element to use */
  525.     return;
  526.   }
  527.   
  528.   cur = *whichb;        /* set up references */
  529.   prev = (cur == 0 ? 2 : cur - 1);
  530.   next = (cur == 2 ? 0 : cur + 1);
  531.   *whichb = next;        /* advance whichb for next time */
  532.   
  533.   /* Special case for bottom of image: don't read another row */
  534.   if (cur_mcu_row >= cinfo->MCU_rows_in_scan - 1) {
  535.     smooth_mcu_row(cinfo, bsmooth[prev], bsmooth[cur], (JBLOCKIMAGE) NULL,
  536.            coeff_data);
  537.     return;
  538.   }
  539.   
  540.   /* Normal case: read ahead a new row, smooth the one I got before */
  541.   (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[next]);
  542.   smooth_mcu_row(cinfo, bsmooth[prev], bsmooth[cur], bsmooth[next],
  543.          coeff_data);
  544. }
  545.  
  546.  
  547. #endif /* BLOCK_SMOOTHING_SUPPORTED */
  548.  
  549.  
  550.  
  551. /*
  552.  * Decompression pipeline controller used for single-scan files
  553.  * without 2-pass color quantization.
  554.  */
  555.  
  556. METHODDEF void
  557. simple_dcontroller (decompress_info_ptr cinfo)
  558. {
  559.   long fullsize_width;        /* # of samples per row in full-size buffers */
  560.   long cur_mcu_row;        /* counts # of MCU rows processed */
  561.   long pixel_rows_output;    /* # of pixel rows actually emitted */
  562.   int mcu_rows_per_loop;    /* # of MCU rows processed per outer loop */
  563.   /* Work buffer for dequantized coefficients (IDCT input) */
  564.   JBLOCKIMAGE coeff_data;
  565.   /* Work buffer for cross-block smoothing input */
  566. #ifdef BLOCK_SMOOTHING_SUPPORTED
  567.   JBLOCKIMAGE bsmooth[3];    /* this is optional */
  568.   int whichb;
  569. #endif
  570.   /* Work buffer for downsampled image data (see comments at head of file) */
  571.   JSAMPIMAGE sampled_data[2];
  572.   /* Work buffer for upsampled data */
  573.   JSAMPIMAGE fullsize_data;
  574.   int whichss, ri;
  575.   short i;
  576.  
  577.   /* Compute dimensions of full-size pixel buffers */
  578.   /* Note these are the same whether interleaved or not. */
  579.   rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
  580.   fullsize_width = jround_up(cinfo->image_width,
  581.                  (long) (cinfo->max_h_samp_factor * DCTSIZE));
  582.  
  583.   /* Prepare for single scan containing all components */
  584.   if (cinfo->comps_in_scan == 1) {
  585.     noninterleaved_scan_setup(cinfo);
  586.     /* Need to read Vk MCU rows to obtain Vk block rows */
  587.     mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
  588.   } else {
  589.     interleaved_scan_setup(cinfo);
  590.     /* in an interleaved scan, one MCU row provides Vk block rows */
  591.     mcu_rows_per_loop = 1;
  592.   }
  593.   cinfo->total_passes++;
  594.  
  595.   /* Allocate working memory: */
  596.   /* coeff_data holds a single MCU row of coefficient blocks */
  597.   coeff_data = alloc_MCU_row(cinfo);
  598.   /* if doing cross-block smoothing, need extra space for its input */
  599. #ifdef BLOCK_SMOOTHING_SUPPORTED
  600.   if (cinfo->do_block_smoothing) {
  601.     bsmooth[0] = alloc_MCU_row(cinfo);
  602.     bsmooth[1] = alloc_MCU_row(cinfo);
  603.     bsmooth[2] = alloc_MCU_row(cinfo);
  604.   }
  605. #endif
  606.   /* sampled_data is sample data before upsampling */
  607.   alloc_sampling_buffer(cinfo, sampled_data);
  608.   /* fullsize_data is sample data after upsampling */
  609.   fullsize_data = alloc_sampimage(cinfo, (int) cinfo->num_components,
  610.                   (long) rows_in_mem, fullsize_width);
  611.   /* output_workspace is the color-processed data */
  612.   output_workspace = alloc_sampimage(cinfo, (int) cinfo->final_out_comps,
  613.                      (long) rows_in_mem, fullsize_width);
  614.   prepare_range_limit_table(cinfo);
  615.  
  616.   /* Tell the memory manager to instantiate big arrays.
  617.    * We don't need any big arrays in this controller,
  618.    * but some other module (like the output file writer) may need one.
  619.    */
  620.   (*cinfo->emethods->alloc_big_arrays)
  621.     ((long) 0,                /* no more small sarrays */
  622.      (long) 0,                /* no more small barrays */
  623.      (long) 0);                /* no more "medium" objects */
  624.   /* NB: if quantizer needs any "medium" size objects, it must get them */
  625.   /* at color_quant_init time */
  626.  
  627.   /* Initialize to read scan data */
  628.  
  629.   (*cinfo->methods->entropy_decode_init) (cinfo);
  630.   (*cinfo->methods->upsample_init) (cinfo);
  631.   (*cinfo->methods->disassemble_init) (cinfo);
  632.  
  633.   /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
  634.  
  635.   pixel_rows_output = 0;
  636.   whichss = 1;            /* arrange to start with sampled_data[0] */
  637.  
  638.   for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
  639.        cur_mcu_row += mcu_rows_per_loop) {
  640.     (*cinfo->methods->progress_monitor) (cinfo, cur_mcu_row,
  641.                      cinfo->MCU_rows_in_scan);
  642.  
  643.     whichss ^= 1;        /* switch to other downsampled-data buffer */
  644.  
  645.     /* Obtain v_samp_factor block rows of each component in the scan. */
  646.     /* This is a single MCU row if interleaved, multiple MCU rows if not. */
  647.     /* In the noninterleaved case there might be fewer than v_samp_factor */
  648.     /* block rows remaining; if so, pad with copies of the last pixel row */
  649.     /* so that upsampling doesn't have to treat it as a special case. */
  650.  
  651.     for (ri = 0; ri < mcu_rows_per_loop; ri++) {
  652.       if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
  653.     /* OK to actually read an MCU row. */
  654. #ifdef BLOCK_SMOOTHING_SUPPORTED
  655.     if (cinfo->do_block_smoothing)
  656.       get_smoothed_row(cinfo, coeff_data,
  657.                bsmooth, &whichb, cur_mcu_row + ri);
  658.     else
  659. #endif
  660.       (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
  661.       
  662.     (*cinfo->methods->reverse_DCT) (cinfo, coeff_data,
  663.                     sampled_data[whichss],
  664.                     ri * DCTSIZE);
  665.       } else {
  666.     /* Need to pad out with copies of the last downsampled row. */
  667.     /* This can only happen if there is just one component. */
  668.     duplicate_row(sampled_data[whichss][0],
  669.               cinfo->cur_comp_info[0]->downsampled_width,
  670.               ri * DCTSIZE - 1, DCTSIZE);
  671.       }
  672.     }
  673.  
  674.     /* Upsample the data */
  675.     /* First time through is a special case */
  676.  
  677.     if (cur_mcu_row) {
  678.       /* Expand last row group of previous set */
  679.       expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
  680.          (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
  681.          (short) (DCTSIZE-1));
  682.       /* and dump the previous set's expanded data */
  683.       emit_1pass (cinfo, rows_in_mem, fullsize_data, (JSAMPARRAY) NULL);
  684.       pixel_rows_output += rows_in_mem;
  685.       /* Expand first row group of this set */
  686.       expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
  687.          (short) (DCTSIZE+1), (short) 0, (short) 1,
  688.          (short) 0);
  689.     } else {
  690.       /* Expand first row group with dummy above-context */
  691.       expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
  692.          (short) (-1), (short) 0, (short) 1,
  693.          (short) 0);
  694.     }
  695.     /* Expand second through next-to-last row groups of this set */
  696.     for (i = 1; i <= DCTSIZE-2; i++) {
  697.       expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
  698.          (short) (i-1), (short) i, (short) (i+1),
  699.          (short) i);
  700.     }
  701.   } /* end of outer loop */
  702.  
  703.   /* Expand the last row group with dummy below-context */
  704.   /* Note whichss points to last buffer side used */
  705.   expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
  706.      (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
  707.      (short) (DCTSIZE-1));
  708.   /* and dump the remaining data (may be less than full height) */
  709.   emit_1pass (cinfo, (int) (cinfo->image_height - pixel_rows_output),
  710.           fullsize_data, (JSAMPARRAY) NULL);
  711.  
  712.   /* Clean up after the scan */
  713.   (*cinfo->methods->disassemble_term) (cinfo);
  714.   (*cinfo->methods->upsample_term) (cinfo);
  715.   (*cinfo->methods->entropy_decode_term) (cinfo);
  716.   (*cinfo->methods->read_scan_trailer) (cinfo);
  717.   cinfo->completed_passes++;
  718.  
  719.   /* Verify that we've seen the whole input file */
  720.   if ((*cinfo->methods->read_scan_header) (cinfo))
  721.     WARNMS(cinfo->emethods, "Didn't expect more than one scan");
  722.  
  723.   /* Release working memory */
  724.   /* (no work -- we let free_all release what's needful) */
  725. }
  726.  
  727.  
  728. /*
  729.  * Decompression pipeline controller used for multiple-scan files
  730.  * and/or 2-pass color quantization.
  731.  *
  732.  * The current implementation places the "big" buffer at the stage of
  733.  * upsampled, non-color-processed data.  This is the only place that
  734.  * makes sense when doing 2-pass quantization.  For processing multiple-scan
  735.  * files without 2-pass quantization, it would be possible to develop another
  736.  * controller that buffers the downsampled data instead, thus reducing the size
  737.  * of the temp files (by about a factor of 2 in typical cases).  However,
  738.  * our present upsampling logic is dependent on the assumption that
  739.  * upsampling occurs during a scan, so it's much easier to do the
  740.  * enlargement as the JPEG file is read.  This also simplifies life for the
  741.  * memory manager, which would otherwise have to deal with overlapping
  742.  * access_big_sarray() requests.
  743.  * At present it appears that most JPEG files will be single-scan,
  744.  * so it doesn't seem worthwhile to worry about this optimization.
  745.  */
  746.  
  747. #ifdef NEED_COMPLEX_CONTROLLER
  748.  
  749. METHODDEF void
  750. complex_dcontroller (decompress_info_ptr cinfo)
  751. {
  752.   long fullsize_width;        /* # of samples per row in full-size buffers */
  753.   long cur_mcu_row;        /* counts # of MCU rows processed */
  754.   long pixel_rows_output;    /* # of pixel rows actually emitted */
  755.   int mcu_rows_per_loop;    /* # of MCU rows processed per outer loop */
  756.   /* Work buffer for dequantized coefficients (IDCT input) */
  757.   JBLOCKIMAGE coeff_data;
  758.   /* Work buffer for cross-block smoothing input */
  759. #ifdef BLOCK_SMOOTHING_SUPPORTED
  760.   JBLOCKIMAGE bsmooth[3];    /* this is optional */
  761.   int whichb;
  762. #endif
  763.   /* Work buffer for downsampled image data (see comments at head of file) */
  764.   JSAMPIMAGE sampled_data[2];
  765.   int whichss, ri;
  766.   short ci, i;
  767.   boolean single_scan;
  768.  
  769.   /* Compute dimensions of full-size pixel buffers */
  770.   /* Note these are the same whether interleaved or not. */
  771.   rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
  772.   fullsize_width = jround_up(cinfo->image_width,
  773.                  (long) (cinfo->max_h_samp_factor * DCTSIZE));
  774.  
  775.   /* Allocate all working memory that doesn't depend on scan info */
  776.   /* output_workspace is the color-processed data */
  777.   output_workspace = alloc_sampimage(cinfo, (int) cinfo->final_out_comps,
  778.                      (long) rows_in_mem, fullsize_width);
  779.   prepare_range_limit_table(cinfo);
  780.  
  781.   /* Get a big image: fullsize_image is sample data after upsampling. */
  782.   fullsize_image = (big_sarray_ptr *) (*cinfo->emethods->alloc_small)
  783.             (cinfo->num_components * SIZEOF(big_sarray_ptr));
  784.   for (ci = 0; ci < cinfo->num_components; ci++) {
  785.     fullsize_image[ci] = (*cinfo->emethods->request_big_sarray)
  786.             (fullsize_width,
  787.              jround_up(cinfo->image_height, (long) rows_in_mem),
  788.              (long) rows_in_mem);
  789.   }
  790.   /* Also get an area for pointers to currently accessible chunks */
  791.   fullsize_ptrs = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  792.                 (cinfo->num_components * SIZEOF(JSAMPARRAY));
  793.  
  794.   /* Tell the memory manager to instantiate big arrays */
  795.   (*cinfo->emethods->alloc_big_arrays)
  796.      /* extra sarray space is for downsampled-data buffers: */
  797.     ((long) (fullsize_width            /* max width in samples */
  798.      * cinfo->max_v_samp_factor*(DCTSIZE+2)    /* max height */
  799.      * cinfo->num_components),        /* max components per scan */
  800.      /* extra barray space is for MCU-row buffers: */
  801.      (long) ((fullsize_width / DCTSIZE)    /* max width in blocks */
  802.      * cinfo->max_v_samp_factor        /* max height */
  803.      * cinfo->num_components        /* max components per scan */
  804.      * (cinfo->do_block_smoothing ? 4 : 1)),/* how many of these we need */
  805.      /* no extra "medium"-object space */
  806.      (long) 0);
  807.   /* NB: if quantizer needs any "medium" size objects, it must get them */
  808.   /* at color_quant_init time */
  809.  
  810.   /* If file is single-scan, we can do color quantization prescan on-the-fly
  811.    * during the scan (we must be doing 2-pass quantization, else this method
  812.    * would not have been selected).  If it is multiple scans, we have to make
  813.    * a separate pass after we've collected all the components.  (We could save
  814.    * some I/O by doing CQ prescan during the last scan, but the extra logic
  815.    * doesn't seem worth the trouble.)
  816.    */
  817.  
  818.   single_scan = (cinfo->comps_in_scan == cinfo->num_components);
  819.  
  820.   /* Account for passes needed (color quantizer adds its passes separately).
  821.    * If multiscan file, we guess that each component has its own scan,
  822.    * and increment completed_passes by the number of components in the scan.
  823.    */
  824.  
  825.   if (single_scan)
  826.     cinfo->total_passes++;    /* the single scan */
  827.   else {
  828.     cinfo->total_passes += cinfo->num_components; /* guessed # of scans */
  829.     if (cinfo->two_pass_quantize)
  830.       cinfo->total_passes++;    /* account for separate CQ prescan pass */
  831.   }
  832.   if (! cinfo->two_pass_quantize)
  833.     cinfo->total_passes++;    /* count output pass unless quantizer does it */
  834.  
  835.   /* Loop over scans in file */
  836.  
  837.   do {
  838.     
  839.     /* Prepare for this scan */
  840.     if (cinfo->comps_in_scan == 1) {
  841.       noninterleaved_scan_setup(cinfo);
  842.       /* Need to read Vk MCU rows to obtain Vk block rows */
  843.       mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
  844.     } else {
  845.       interleaved_scan_setup(cinfo);
  846.       /* in an interleaved scan, one MCU row provides Vk block rows */
  847.       mcu_rows_per_loop = 1;
  848.     }
  849.     
  850.     /* Allocate scan-local working memory */
  851.     /* coeff_data holds a single MCU row of coefficient blocks */
  852.     coeff_data = alloc_MCU_row(cinfo);
  853.     /* if doing cross-block smoothing, need extra space for its input */
  854. #ifdef BLOCK_SMOOTHING_SUPPORTED
  855.     if (cinfo->do_block_smoothing) {
  856.       bsmooth[0] = alloc_MCU_row(cinfo);
  857.       bsmooth[1] = alloc_MCU_row(cinfo);
  858.       bsmooth[2] = alloc_MCU_row(cinfo);
  859.     }
  860. #endif
  861.     /* sampled_data is sample data before upsampling */
  862.     alloc_sampling_buffer(cinfo, sampled_data);
  863.  
  864.     /* line up the big buffers for components in this scan */
  865.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  866.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  867.     (fullsize_image[cinfo->cur_comp_info[ci]->component_index],
  868.      (long) 0, TRUE);
  869.     }
  870.     
  871.     /* Initialize to read scan data */
  872.     
  873.     (*cinfo->methods->entropy_decode_init) (cinfo);
  874.     (*cinfo->methods->upsample_init) (cinfo);
  875.     (*cinfo->methods->disassemble_init) (cinfo);
  876.     
  877.     /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
  878.     
  879.     pixel_rows_output = 0;
  880.     whichss = 1;        /* arrange to start with sampled_data[0] */
  881.     
  882.     for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
  883.      cur_mcu_row += mcu_rows_per_loop) {
  884.       (*cinfo->methods->progress_monitor) (cinfo, cur_mcu_row,
  885.                        cinfo->MCU_rows_in_scan);
  886.  
  887.       whichss ^= 1;        /* switch to other downsampled-data buffer */
  888.  
  889.       /* Obtain v_samp_factor block rows of each component in the scan. */
  890.       /* This is a single MCU row if interleaved, multiple MCU rows if not. */
  891.       /* In the noninterleaved case there might be fewer than v_samp_factor */
  892.       /* block rows remaining; if so, pad with copies of the last pixel row */
  893.       /* so that upsampling doesn't have to treat it as a special case. */
  894.       
  895.       for (ri = 0; ri < mcu_rows_per_loop; ri++) {
  896.     if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
  897.       /* OK to actually read an MCU row. */
  898. #ifdef BLOCK_SMOOTHING_SUPPORTED
  899.       if (cinfo->do_block_smoothing)
  900.         get_smoothed_row(cinfo, coeff_data,
  901.                  bsmooth, &whichb, cur_mcu_row + ri);
  902.       else
  903. #endif
  904.         (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
  905.       
  906.       (*cinfo->methods->reverse_DCT) (cinfo, coeff_data,
  907.                       sampled_data[whichss],
  908.                       ri * DCTSIZE);
  909.     } else {
  910.       /* Need to pad out with copies of the last downsampled row. */
  911.       /* This can only happen if there is just one component. */
  912.       duplicate_row(sampled_data[whichss][0],
  913.             cinfo->cur_comp_info[0]->downsampled_width,
  914.             ri * DCTSIZE - 1, DCTSIZE);
  915.     }
  916.       }
  917.       
  918.       /* Upsample the data */
  919.       /* First time through is a special case */
  920.       
  921.       if (cur_mcu_row) {
  922.     /* Expand last row group of previous set */
  923.     expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
  924.            (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
  925.            (short) (DCTSIZE-1));
  926.     /* If single scan, can do color quantization prescan on-the-fly */
  927.     if (single_scan)
  928.       (*cinfo->methods->color_quant_prescan) (cinfo, rows_in_mem,
  929.                           fullsize_ptrs,
  930.                           output_workspace[0]);
  931.     /* Realign the big buffers */
  932.     pixel_rows_output += rows_in_mem;
  933.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  934.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  935.         (fullsize_image[cinfo->cur_comp_info[ci]->component_index],
  936.          pixel_rows_output, TRUE);
  937.     }
  938.     /* Expand first row group of this set */
  939.     expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
  940.            (short) (DCTSIZE+1), (short) 0, (short) 1,
  941.            (short) 0);
  942.       } else {
  943.     /* Expand first row group with dummy above-context */
  944.     expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
  945.            (short) (-1), (short) 0, (short) 1,
  946.            (short) 0);
  947.       }
  948.       /* Expand second through next-to-last row groups of this set */
  949.       for (i = 1; i <= DCTSIZE-2; i++) {
  950.     expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
  951.            (short) (i-1), (short) i, (short) (i+1),
  952.            (short) i);
  953.       }
  954.     } /* end of loop over scan's data */
  955.     
  956.     /* Expand the last row group with dummy below-context */
  957.     /* Note whichss points to last buffer side used */
  958.     expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
  959.        (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
  960.        (short) (DCTSIZE-1));
  961.     /* If single scan, finish on-the-fly color quantization prescan */
  962.     if (single_scan)
  963.       (*cinfo->methods->color_quant_prescan) (cinfo,
  964.             (int) (cinfo->image_height - pixel_rows_output),
  965.             fullsize_ptrs, output_workspace[0]);
  966.     
  967.     /* Clean up after the scan */
  968.     (*cinfo->methods->disassemble_term) (cinfo);
  969.     (*cinfo->methods->upsample_term) (cinfo);
  970.     (*cinfo->methods->entropy_decode_term) (cinfo);
  971.     (*cinfo->methods->read_scan_trailer) (cinfo);
  972.     if (single_scan)
  973.       cinfo->completed_passes++;
  974.     else
  975.       cinfo->completed_passes += cinfo->comps_in_scan;
  976.  
  977.     /* Release scan-local working memory */
  978.     free_MCU_row(cinfo, coeff_data);
  979. #ifdef BLOCK_SMOOTHING_SUPPORTED
  980.     if (cinfo->do_block_smoothing) {
  981.       free_MCU_row(cinfo, bsmooth[0]);
  982.       free_MCU_row(cinfo, bsmooth[1]);
  983.       free_MCU_row(cinfo, bsmooth[2]);
  984.     }
  985. #endif
  986.     free_sampling_buffer(cinfo, sampled_data);
  987.     
  988.     /* Repeat if there is another scan */
  989.   } while ((!single_scan) && (*cinfo->methods->read_scan_header) (cinfo));
  990.  
  991.   if (single_scan) {
  992.     /* If we expected just one scan, make SURE there's just one */
  993.     if ((*cinfo->methods->read_scan_header) (cinfo))
  994.       WARNMS(cinfo->emethods, "Didn't expect more than one scan");
  995.     /* We did the CQ prescan on-the-fly, so we are all set. */
  996.   } else {
  997.     /* For multiple-scan file, do the CQ prescan as a separate pass. */
  998.     /* The main reason why prescan is passed the output_workspace is */
  999.     /* so that we can use scan_big_image to call it... */
  1000.     if (cinfo->two_pass_quantize)
  1001.       scan_big_image(cinfo, cinfo->methods->color_quant_prescan);
  1002.   }
  1003.  
  1004.   /* Now that we've collected the data, do color processing and output */
  1005.   if (cinfo->two_pass_quantize)
  1006.     (*cinfo->methods->color_quant_doit) (cinfo, scan_big_image);
  1007.   else
  1008.     scan_big_image(cinfo, emit_1pass);
  1009.  
  1010.   /* Release working memory */
  1011.   /* (no work -- we let free_all release what's needful) */
  1012. }
  1013.  
  1014. #endif /* NEED_COMPLEX_CONTROLLER */
  1015.  
  1016.  
  1017. /*
  1018.  * The method selection routine for decompression pipeline controllers.
  1019.  * Note that at this point we've already read the JPEG header and first SOS,
  1020.  * so we can tell whether the input is one scan or not.
  1021.  */
  1022.  
  1023. GLOBAL void
  1024. jseldpipeline (decompress_info_ptr cinfo)
  1025. {
  1026.   /* simplify subsequent tests on color quantization */
  1027.   if (! cinfo->quantize_colors)
  1028.     cinfo->two_pass_quantize = FALSE;
  1029.   
  1030.   if (cinfo->comps_in_scan == cinfo->num_components) {
  1031.     /* It's a single-scan file */
  1032.     if (cinfo->two_pass_quantize) {
  1033. #ifdef NEED_COMPLEX_CONTROLLER
  1034.       cinfo->methods->d_pipeline_controller = complex_dcontroller;
  1035. #else
  1036.       ERREXIT(cinfo->emethods, "2-pass quantization support was not compiled");
  1037. #endif
  1038.     } else
  1039.       cinfo->methods->d_pipeline_controller = simple_dcontroller;
  1040.   } else {
  1041.     /* It's a multiple-scan file */
  1042. #ifdef NEED_COMPLEX_CONTROLLER
  1043.     cinfo->methods->d_pipeline_controller = complex_dcontroller;
  1044. #else
  1045.     ERREXIT(cinfo->emethods, "Multiple-scan support was not compiled");
  1046. #endif
  1047.   }
  1048. }
  1049.