home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / DVPG30FS.ZIP / JDPIPE.C < prev    next >
C/C++ Source or Header  |  1993-10-20  |  40KB  |  1,069 lines

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