home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / compresn / jpegv3sr / jdpipe.c < prev    next >
C/C++ Source or Header  |  1992-03-02  |  37KB  |  1,009 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 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 unsubsampling 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 subsampled, 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 unsubsampling 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 desubsampled, 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->subsampled_width = jround_up(compptr->true_comp_width,
  118.                       (long) (compptr->MCU_width*DCTSIZE));
  119.     compptr->subsampled_height = jround_up(compptr->true_comp_height,
  120.                        (long) (compptr->MCU_height*DCTSIZE));
  121.     /* Sanity check */
  122.     if (compptr->subsampled_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->subsampled_width = jround_up(compptr->true_comp_width,
  151.                     (long) DCTSIZE);
  152.   compptr->subsampled_height = jround_up(compptr->true_comp_height,
  153.                      (long) DCTSIZE);
  154.  
  155.   cinfo->MCUs_per_row = compptr->subsampled_width / DCTSIZE;
  156.   cinfo->MCU_rows_in_scan = compptr->subsampled_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]->subsampled_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 subsampled_data[2])
  238. /* Create a subsampled-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.   subsampled_data[0] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  245.                 (cinfo->comps_in_scan * SIZEOF(JSAMPARRAY));
  246.   subsampled_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.     subsampled_data[0][ci] = (*cinfo->emethods->alloc_small_sarray)
  253.                 (cinfo->cur_comp_info[ci]->subsampled_width,
  254.                 (long) (vs * (DCTSIZE+2)));
  255.     /* Create space for the scrambled-order pointers */
  256.     subsampled_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.       subsampled_data[1][ci][i] = subsampled_data[0][ci][i];
  261.     }
  262.     /* Copy the last four row groups in swapped order */
  263.     for (i = 0; i < vs * 2; i++) {
  264.       subsampled_data[1][ci][vs*DCTSIZE + i] = subsampled_data[0][ci][vs*(DCTSIZE-2) + i];
  265.       subsampled_data[1][ci][vs*(DCTSIZE-2) + i] = subsampled_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 subsampled_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) (subsampled_data[0][ci]);
  282.     /* Free the scrambled-order pointers */
  283.     (*cinfo->emethods->free_small) ((void *) subsampled_data[1][ci]);
  284.   }
  285.  
  286.   /* Free the top-level space */
  287.   (*cinfo->emethods->free_small) ((void *) subsampled_data[0]);
  288.   (*cinfo->emethods->free_small) ((void *) subsampled_data[1]);
  289. }
  290.  
  291. #endif
  292.  
  293.  
  294. LOCAL void
  295. duplicate_row (JSAMPARRAY image_data,
  296.            long num_cols, int source_row, int num_rows)
  297. /* Duplicate the source_row at source_row+1 .. source_row+num_rows */
  298. /* This happens only at the bottom of the image, */
  299. /* so it needn't be super-efficient */
  300. {
  301.   register int row;
  302.  
  303.   for (row = 1; row <= num_rows; row++) {
  304.     jcopy_sample_rows(image_data, source_row, image_data, source_row + row,
  305.               1, num_cols);
  306.   }
  307. }
  308.  
  309.  
  310. LOCAL void
  311. expand (decompress_info_ptr cinfo,
  312.     JSAMPIMAGE subsampled_data, JSAMPIMAGE fullsize_data,
  313.     long fullsize_width,
  314.     short above, short current, short below, short out)
  315. /* Do unsubsampling expansion of a single row group (of each component).  */
  316. /* above, current, below are indexes of row groups in subsampled_data;    */
  317. /* out is the index of the target row group in fullsize_data.             */
  318. /* Special case: above, below can be -1 to indicate top, bottom of image. */
  319. {
  320.   jpeg_component_info *compptr;
  321.   JSAMPARRAY above_ptr, below_ptr;
  322.   JSAMPROW dummy[MAX_SAMP_FACTOR]; /* for subsample expansion at top/bottom */
  323.   short ci, vs, i;
  324.  
  325.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  326.     compptr = cinfo->cur_comp_info[ci];
  327.     vs = compptr->v_samp_factor; /* row group height */
  328.  
  329.     if (above >= 0)
  330.       above_ptr = subsampled_data[ci] + above * vs;
  331.     else {
  332.       /* Top of image: make a dummy above-context with copies of 1st row */
  333.       /* We assume current=0 in this case */
  334.       for (i = 0; i < vs; i++)
  335.     dummy[i] = subsampled_data[ci][0];
  336.       above_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
  337.     }
  338.  
  339.     if (below >= 0)
  340.       below_ptr = subsampled_data[ci] + below * vs;
  341.     else {
  342.       /* Bot of image: make a dummy below-context with copies of last row */
  343.       for (i = 0; i < vs; i++)
  344.     dummy[i] = subsampled_data[ci][(current+1)*vs-1];
  345.       below_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
  346.     }
  347.  
  348.     (*cinfo->methods->unsubsample[ci])
  349.         (cinfo, (int) ci,
  350.          compptr->subsampled_width, (int) vs,
  351.          fullsize_width, (int) cinfo->max_v_samp_factor,
  352.          above_ptr,
  353.          subsampled_data[ci] + current * vs,
  354.          below_ptr,
  355.          fullsize_data[ci] + out * cinfo->max_v_samp_factor);
  356.   }
  357. }
  358.  
  359.  
  360. LOCAL void
  361. emit_1pass (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE fullsize_data,
  362.         JSAMPARRAY dummy)
  363. /* Do color processing and output of num_rows full-size rows. */
  364. /* This is not used when doing 2-pass color quantization. */
  365. /* The dummy argument simply lets this be called via scan_big_image. */
  366. {
  367.   if (cinfo->quantize_colors) {
  368.     (*cinfo->methods->color_quantize) (cinfo, num_rows, fullsize_data,
  369.                        output_workspace[0]);
  370.   } else {
  371.     (*cinfo->methods->color_convert) (cinfo, num_rows, cinfo->image_width,
  372.                       fullsize_data, output_workspace);
  373.   }
  374.     
  375.   (*cinfo->methods->put_pixel_rows) (cinfo, num_rows, output_workspace);
  376. }
  377.  
  378.  
  379. /*
  380.  * Support routines for complex controller.
  381.  */
  382.  
  383. #ifdef NEED_COMPLEX_CONTROLLER
  384.  
  385. METHODDEF void
  386. scan_big_image (decompress_info_ptr cinfo, quantize_method_ptr quantize_method)
  387. /* Apply quantize_method to entire image stored in fullsize_image[]. */
  388. /* This is the "iterator" routine used by the 2-pass color quantizer. */
  389. /* We also use it directly in some cases. */
  390. {
  391.   long pixel_rows_output;
  392.   short ci;
  393.  
  394.   for (pixel_rows_output = 0; pixel_rows_output < cinfo->image_height;
  395.        pixel_rows_output += rows_in_mem) {
  396.     (*cinfo->methods->progress_monitor) (cinfo, pixel_rows_output,
  397.                      cinfo->image_height);
  398.     /* Realign the big buffers */
  399.     for (ci = 0; ci < cinfo->num_components; ci++) {
  400.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  401.     (fullsize_image[ci], pixel_rows_output, FALSE);
  402.     }
  403.     /* Let the quantizer have its way with the data.
  404.      * Note that output_workspace is simply workspace for the quantizer;
  405.      * when it's ready to output, it must call put_pixel_rows itself.
  406.      */
  407.     (*quantize_method) (cinfo,
  408.             (int) MIN((long) rows_in_mem,
  409.                   cinfo->image_height - pixel_rows_output),
  410.             fullsize_ptrs, output_workspace[0]);
  411.   }
  412.  
  413.   cinfo->completed_passes++;
  414. }
  415.  
  416. #endif /* NEED_COMPLEX_CONTROLLER */
  417.  
  418.  
  419. /*
  420.  * Support routines for cross-block smoothing.
  421.  */
  422.  
  423. #ifdef BLOCK_SMOOTHING_SUPPORTED
  424.  
  425.  
  426. LOCAL void
  427. smooth_mcu_row (decompress_info_ptr cinfo,
  428.         JBLOCKIMAGE above, JBLOCKIMAGE input, JBLOCKIMAGE below,
  429.         JBLOCKIMAGE output)
  430. /* Apply cross-block smoothing to one MCU row's worth of coefficient blocks. */
  431. /* above,below are NULL if at top/bottom of image. */
  432. {
  433.   jpeg_component_info *compptr;
  434.   short ci, ri, last;
  435.   JBLOCKROW prev;
  436.  
  437.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  438.     compptr = cinfo->cur_comp_info[ci];
  439.     last = compptr->MCU_height - 1;
  440.  
  441.     if (above == NULL)
  442.       prev = NULL;
  443.     else
  444.       prev = above[ci][last];
  445.  
  446.     for (ri = 0; ri < last; ri++) {
  447.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  448.                 prev, input[ci][ri], input[ci][ri+1],
  449.                 output[ci][ri]);
  450.       prev = input[ci][ri];
  451.     }
  452.  
  453.     if (below == NULL)
  454.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  455.                 prev, input[ci][last], (JBLOCKROW) NULL,
  456.                 output[ci][last]);
  457.     else
  458.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  459.                 prev, input[ci][last], below[ci][0],
  460.                 output[ci][last]);
  461.   }
  462. }
  463.  
  464.  
  465. LOCAL void
  466. get_smoothed_row (decompress_info_ptr cinfo, JBLOCKIMAGE coeff_data,
  467.           JBLOCKIMAGE bsmooth[3], int * whichb, long cur_mcu_row)
  468. /* Get an MCU row of coefficients, applying cross-block smoothing. */
  469. /* The output row is placed in coeff_data.  bsmooth and whichb hold */
  470. /* working state, and cur_row is needed to check for image top/bottom. */
  471. /* This routine just takes care of the buffering logic. */
  472. {
  473.   int prev, cur, next;
  474.   
  475.   /* Special case for top of image: need to pre-fetch a row & init whichb */
  476.   if (cur_mcu_row == 0) {
  477.     (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[0]);
  478.     if (cinfo->MCU_rows_in_scan > 1) {
  479.       (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[1]);
  480.       smooth_mcu_row(cinfo, (JBLOCKIMAGE) NULL, bsmooth[0], bsmooth[1],
  481.              coeff_data);
  482.     } else {
  483.       smooth_mcu_row(cinfo, (JBLOCKIMAGE) NULL, bsmooth[0], (JBLOCKIMAGE) NULL,
  484.              coeff_data);
  485.     }
  486.     *whichb = 1;        /* points to next bsmooth[] element to use */
  487.     return;
  488.   }
  489.   
  490.   cur = *whichb;        /* set up references */
  491.   prev = (cur == 0 ? 2 : cur - 1);
  492.   next = (cur == 2 ? 0 : cur + 1);
  493.   *whichb = next;        /* advance whichb for next time */
  494.   
  495.   /* Special case for bottom of image: don't read another row */
  496.   if (cur_mcu_row >= cinfo->MCU_rows_in_scan - 1) {
  497.     smooth_mcu_row(cinfo, bsmooth[prev], bsmooth[cur], (JBLOCKIMAGE) NULL,
  498.            coeff_data);
  499.     return;
  500.   }
  501.   
  502.   /* Normal case: read ahead a new row, smooth the one I got before */
  503.   (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[next]);
  504.   smooth_mcu_row(cinfo, bsmooth[prev], bsmooth[cur], bsmooth[next],
  505.          coeff_data);
  506. }
  507.  
  508.  
  509. #endif /* BLOCK_SMOOTHING_SUPPORTED */
  510.  
  511.  
  512.  
  513. /*
  514.  * Decompression pipeline controller used for single-scan files
  515.  * without 2-pass color quantization.
  516.  */
  517.  
  518. METHODDEF void
  519. simple_dcontroller (decompress_info_ptr cinfo)
  520. {
  521.   long fullsize_width;        /* # of samples per row in full-size buffers */
  522.   long cur_mcu_row;        /* counts # of MCU rows processed */
  523.   long pixel_rows_output;    /* # of pixel rows actually emitted */
  524.   int mcu_rows_per_loop;    /* # of MCU rows processed per outer loop */
  525.   /* Work buffer for dequantized coefficients (IDCT input) */
  526.   JBLOCKIMAGE coeff_data;
  527.   /* Work buffer for cross-block smoothing input */
  528. #ifdef BLOCK_SMOOTHING_SUPPORTED
  529.   JBLOCKIMAGE bsmooth[3];    /* this is optional */
  530.   int whichb;
  531. #endif
  532.   /* Work buffer for subsampled image data (see comments at head of file) */
  533.   JSAMPIMAGE subsampled_data[2];
  534.   /* Work buffer for desubsampled data */
  535.   JSAMPIMAGE fullsize_data;
  536.   int whichss, ri;
  537.   short i;
  538.  
  539.   /* Compute dimensions of full-size pixel buffers */
  540.   /* Note these are the same whether interleaved or not. */
  541.   rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
  542.   fullsize_width = jround_up(cinfo->image_width,
  543.                  (long) (cinfo->max_h_samp_factor * DCTSIZE));
  544.  
  545.   /* Prepare for single scan containing all components */
  546.   if (cinfo->comps_in_scan == 1) {
  547.     noninterleaved_scan_setup(cinfo);
  548.     /* Need to read Vk MCU rows to obtain Vk block rows */
  549.     mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
  550.   } else {
  551.     interleaved_scan_setup(cinfo);
  552.     /* in an interleaved scan, one MCU row provides Vk block rows */
  553.     mcu_rows_per_loop = 1;
  554.   }
  555.   cinfo->total_passes++;
  556.  
  557.   /* Allocate working memory: */
  558.   /* coeff_data holds a single MCU row of coefficient blocks */
  559.   coeff_data = alloc_MCU_row(cinfo);
  560.   /* if doing cross-block smoothing, need extra space for its input */
  561. #ifdef BLOCK_SMOOTHING_SUPPORTED
  562.   if (cinfo->do_block_smoothing) {
  563.     bsmooth[0] = alloc_MCU_row(cinfo);
  564.     bsmooth[1] = alloc_MCU_row(cinfo);
  565.     bsmooth[2] = alloc_MCU_row(cinfo);
  566.   }
  567. #endif
  568.   /* subsampled_data is sample data before unsubsampling */
  569.   alloc_sampling_buffer(cinfo, subsampled_data);
  570.   /* fullsize_data is sample data after unsubsampling */
  571.   fullsize_data = alloc_sampimage(cinfo, (int) cinfo->num_components,
  572.                   (long) rows_in_mem, fullsize_width);
  573.   /* output_workspace is the color-processed data */
  574.   output_workspace = alloc_sampimage(cinfo, (int) cinfo->final_out_comps,
  575.                      (long) rows_in_mem, fullsize_width);
  576.  
  577.   /* Tell the memory manager to instantiate big arrays.
  578.    * We don't need any big arrays in this controller,
  579.    * but some other module (like the output file writer) may need one.
  580.    */
  581.   (*cinfo->emethods->alloc_big_arrays)
  582.     ((long) 0,                /* no more small sarrays */
  583.      (long) 0,                /* no more small barrays */
  584.      (long) 0);                /* no more "medium" objects */
  585.   /* NB: if quantizer needs any "medium" size objects, it must get them */
  586.   /* at color_quant_init time */
  587.  
  588.   /* Initialize to read scan data */
  589.  
  590.   (*cinfo->methods->entropy_decoder_init) (cinfo);
  591.   (*cinfo->methods->unsubsample_init) (cinfo);
  592.   (*cinfo->methods->disassemble_init) (cinfo);
  593.  
  594.   /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
  595.  
  596.   pixel_rows_output = 0;
  597.   whichss = 1;            /* arrange to start with subsampled_data[0] */
  598.  
  599.   for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
  600.        cur_mcu_row += mcu_rows_per_loop) {
  601.     (*cinfo->methods->progress_monitor) (cinfo, cur_mcu_row,
  602.                      cinfo->MCU_rows_in_scan);
  603.  
  604.     whichss ^= 1;        /* switch to other subsample buffer */
  605.  
  606.     /* Obtain v_samp_factor block rows of each component in the scan. */
  607.     /* This is a single MCU row if interleaved, multiple MCU rows if not. */
  608.     /* In the noninterleaved case there might be fewer than v_samp_factor */
  609.     /* block rows remaining; if so, pad with copies of the last pixel row */
  610.     /* so that unsubsampling doesn't have to treat it as a special case. */
  611.  
  612.     for (ri = 0; ri < mcu_rows_per_loop; ri++) {
  613.       if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
  614.     /* OK to actually read an MCU row. */
  615. #ifdef BLOCK_SMOOTHING_SUPPORTED
  616.     if (cinfo->do_block_smoothing)
  617.       get_smoothed_row(cinfo, coeff_data,
  618.                bsmooth, &whichb, cur_mcu_row + ri);
  619.     else
  620. #endif
  621.       (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
  622.       
  623.     (*cinfo->methods->reverse_DCT) (cinfo, coeff_data,
  624.                     subsampled_data[whichss],
  625.                     ri * DCTSIZE);
  626.       } else {
  627.     /* Need to pad out with copies of the last subsampled row. */
  628.     /* This can only happen if there is just one component. */
  629.     duplicate_row(subsampled_data[whichss][0],
  630.               cinfo->cur_comp_info[0]->subsampled_width,
  631.               ri * DCTSIZE - 1, DCTSIZE);
  632.       }
  633.     }
  634.  
  635.     /* Unsubsample the data */
  636.     /* First time through is a special case */
  637.  
  638.     if (cur_mcu_row) {
  639.       /* Expand last row group of previous set */
  640.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  641.          (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
  642.          (short) (DCTSIZE-1));
  643.       /* and dump the previous set's expanded data */
  644.       emit_1pass (cinfo, rows_in_mem, fullsize_data, NULL);
  645.       pixel_rows_output += rows_in_mem;
  646.       /* Expand first row group of this set */
  647.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  648.          (short) (DCTSIZE+1), (short) 0, (short) 1,
  649.          (short) 0);
  650.     } else {
  651.       /* Expand first row group with dummy above-context */
  652.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  653.          (short) (-1), (short) 0, (short) 1,
  654.          (short) 0);
  655.     }
  656.     /* Expand second through next-to-last row groups of this set */
  657.     for (i = 1; i <= DCTSIZE-2; i++) {
  658.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  659.          (short) (i-1), (short) i, (short) (i+1),
  660.          (short) i);
  661.     }
  662.   } /* end of outer loop */
  663.  
  664.   /* Expand the last row group with dummy below-context */
  665.   /* Note whichss points to last buffer side used */
  666.   expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  667.      (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
  668.      (short) (DCTSIZE-1));
  669.   /* and dump the remaining data (may be less than full height) */
  670.   emit_1pass (cinfo, (int) (cinfo->image_height - pixel_rows_output),
  671.           fullsize_data, NULL);
  672.  
  673.   /* Clean up after the scan */
  674.   (*cinfo->methods->disassemble_term) (cinfo);
  675.   (*cinfo->methods->unsubsample_term) (cinfo);
  676.   (*cinfo->methods->entropy_decoder_term) (cinfo);
  677.   (*cinfo->methods->read_scan_trailer) (cinfo);
  678.   cinfo->completed_passes++;
  679.  
  680.   /* Verify that we've seen the whole input file */
  681.   if ((*cinfo->methods->read_scan_header) (cinfo))
  682.     ERREXIT(cinfo->emethods, "Didn't expect more than one scan");
  683.  
  684.   /* Release working memory */
  685.   /* (no work -- we let free_all release what's needful) */
  686. }
  687.  
  688.  
  689. /*
  690.  * Decompression pipeline controller used for multiple-scan files
  691.  * and/or 2-pass color quantization.
  692.  *
  693.  * The current implementation places the "big" buffer at the stage of
  694.  * desubsampled, non-color-processed data.  This is the only place that
  695.  * makes sense when doing 2-pass quantization.  For processing multiple-scan
  696.  * files without 2-pass quantization, it would be possible to develop another
  697.  * controller that buffers the subsampled data instead, thus reducing the size
  698.  * of the temp files (by about a factor of 2 in typical cases).  However,
  699.  * our present unsubsampling logic is dependent on the assumption that
  700.  * unsubsampling occurs during a scan, so it's much easier to do the
  701.  * enlargement as the JPEG file is read.  This also simplifies life for the
  702.  * memory manager, which would otherwise have to deal with overlapping
  703.  * access_big_sarray() requests.
  704.  * At present it appears that most JPEG files will be single-scan,
  705.  * so it doesn't seem worthwhile to worry about this optimization.
  706.  */
  707.  
  708. #ifdef NEED_COMPLEX_CONTROLLER
  709.  
  710. METHODDEF void
  711. complex_dcontroller (decompress_info_ptr cinfo)
  712. {
  713.   long fullsize_width;        /* # of samples per row in full-size buffers */
  714.   long cur_mcu_row;        /* counts # of MCU rows processed */
  715.   long pixel_rows_output;    /* # of pixel rows actually emitted */
  716.   int mcu_rows_per_loop;    /* # of MCU rows processed per outer loop */
  717.   /* Work buffer for dequantized coefficients (IDCT input) */
  718.   JBLOCKIMAGE coeff_data;
  719.   /* Work buffer for cross-block smoothing input */
  720. #ifdef BLOCK_SMOOTHING_SUPPORTED
  721.   JBLOCKIMAGE bsmooth[3];    /* this is optional */
  722.   int whichb;
  723. #endif
  724.   /* Work buffer for subsampled image data (see comments at head of file) */
  725.   JSAMPIMAGE subsampled_data[2];
  726.   int whichss, ri;
  727.   short ci, i;
  728.   boolean single_scan;
  729.  
  730.   /* Compute dimensions of full-size pixel buffers */
  731.   /* Note these are the same whether interleaved or not. */
  732.   rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
  733.   fullsize_width = jround_up(cinfo->image_width,
  734.                  (long) (cinfo->max_h_samp_factor * DCTSIZE));
  735.  
  736.   /* Allocate all working memory that doesn't depend on scan info */
  737.   /* output_workspace is the color-processed data */
  738.   output_workspace = alloc_sampimage(cinfo, (int) cinfo->final_out_comps,
  739.                      (long) rows_in_mem, fullsize_width);
  740.  
  741.   /* Get a big image: fullsize_image is sample data after unsubsampling. */
  742.   fullsize_image = (big_sarray_ptr *) (*cinfo->emethods->alloc_small)
  743.             (cinfo->num_components * SIZEOF(big_sarray_ptr));
  744.   for (ci = 0; ci < cinfo->num_components; ci++) {
  745.     fullsize_image[ci] = (*cinfo->emethods->request_big_sarray)
  746.             (fullsize_width,
  747.              jround_up(cinfo->image_height, (long) rows_in_mem),
  748.              (long) rows_in_mem);
  749.   }
  750.   /* Also get an area for pointers to currently accessible chunks */
  751.   fullsize_ptrs = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  752.                 (cinfo->num_components * SIZEOF(JSAMPARRAY));
  753.  
  754.   /* Tell the memory manager to instantiate big arrays */
  755.   (*cinfo->emethods->alloc_big_arrays)
  756.      /* extra sarray space is for subsampled-data buffers: */
  757.     ((long) (fullsize_width            /* max width in samples */
  758.      * cinfo->max_v_samp_factor*(DCTSIZE+2)    /* max height */
  759.      * cinfo->num_components),        /* max components per scan */
  760.      /* extra barray space is for MCU-row buffers: */
  761.      (long) ((fullsize_width / DCTSIZE)    /* max width in blocks */
  762.      * cinfo->max_v_samp_factor        /* max height */
  763.      * cinfo->num_components        /* max components per scan */
  764.      * (cinfo->do_block_smoothing ? 4 : 1)),/* how many of these we need */
  765.      /* no extra "medium"-object space */
  766.      (long) 0);
  767.   /* NB: if quantizer needs any "medium" size objects, it must get them */
  768.   /* at color_quant_init time */
  769.  
  770.   /* If file is single-scan, we can do color quantization prescan on-the-fly
  771.    * during the scan (we must be doing 2-pass quantization, else this method
  772.    * would not have been selected).  If it is multiple scans, we have to make
  773.    * a separate pass after we've collected all the components.  (We could save
  774.    * some I/O by doing CQ prescan during the last scan, but the extra logic
  775.    * doesn't seem worth the trouble.)
  776.    */
  777.  
  778.   single_scan = (cinfo->comps_in_scan == cinfo->num_components);
  779.  
  780.   /* Account for passes needed (color quantizer adds its passes separately).
  781.    * If multiscan file, we guess that each component has its own scan,
  782.    * and increment completed_passes by the number of components in the scan.
  783.    */
  784.  
  785.   if (single_scan)
  786.     cinfo->total_passes++;    /* the single scan */
  787.   else {
  788.     cinfo->total_passes += cinfo->num_components; /* guessed # of scans */
  789.     if (cinfo->two_pass_quantize)
  790.       cinfo->total_passes++;    /* account for separate CQ prescan pass */
  791.   }
  792.   if (! cinfo->two_pass_quantize)
  793.     cinfo->total_passes++;    /* count output pass unless quantizer does it */
  794.  
  795.   /* Loop over scans in file */
  796.  
  797.   do {
  798.     
  799.     /* Prepare for this scan */
  800.     if (cinfo->comps_in_scan == 1) {
  801.       noninterleaved_scan_setup(cinfo);
  802.       /* Need to read Vk MCU rows to obtain Vk block rows */
  803.       mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
  804.     } else {
  805.       interleaved_scan_setup(cinfo);
  806.       /* in an interleaved scan, one MCU row provides Vk block rows */
  807.       mcu_rows_per_loop = 1;
  808.     }
  809.     
  810.     /* Allocate scan-local working memory */
  811.     /* coeff_data holds a single MCU row of coefficient blocks */
  812.     coeff_data = alloc_MCU_row(cinfo);
  813.     /* if doing cross-block smoothing, need extra space for its input */
  814. #ifdef BLOCK_SMOOTHING_SUPPORTED
  815.     if (cinfo->do_block_smoothing) {
  816.       bsmooth[0] = alloc_MCU_row(cinfo);
  817.       bsmooth[1] = alloc_MCU_row(cinfo);
  818.       bsmooth[2] = alloc_MCU_row(cinfo);
  819.     }
  820. #endif
  821.     /* subsampled_data is sample data before unsubsampling */
  822.     alloc_sampling_buffer(cinfo, subsampled_data);
  823.  
  824.     /* line up the big buffers for components in this scan */
  825.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  826.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  827.     (fullsize_image[cinfo->cur_comp_info[ci]->component_index],
  828.      (long) 0, TRUE);
  829.     }
  830.     
  831.     /* Initialize to read scan data */
  832.     
  833.     (*cinfo->methods->entropy_decoder_init) (cinfo);
  834.     (*cinfo->methods->unsubsample_init) (cinfo);
  835.     (*cinfo->methods->disassemble_init) (cinfo);
  836.     
  837.     /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
  838.     
  839.     pixel_rows_output = 0;
  840.     whichss = 1;        /* arrange to start with subsampled_data[0] */
  841.     
  842.     for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
  843.      cur_mcu_row += mcu_rows_per_loop) {
  844.       (*cinfo->methods->progress_monitor) (cinfo, cur_mcu_row,
  845.                        cinfo->MCU_rows_in_scan);
  846.  
  847.       whichss ^= 1;        /* switch to other subsample buffer */
  848.  
  849.       /* Obtain v_samp_factor block rows of each component in the scan. */
  850.       /* This is a single MCU row if interleaved, multiple MCU rows if not. */
  851.       /* In the noninterleaved case there might be fewer than v_samp_factor */
  852.       /* block rows remaining; if so, pad with copies of the last pixel row */
  853.       /* so that unsubsampling doesn't have to treat it as a special case. */
  854.       
  855.       for (ri = 0; ri < mcu_rows_per_loop; ri++) {
  856.     if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
  857.       /* OK to actually read an MCU row. */
  858. #ifdef BLOCK_SMOOTHING_SUPPORTED
  859.       if (cinfo->do_block_smoothing)
  860.         get_smoothed_row(cinfo, coeff_data,
  861.                  bsmooth, &whichb, cur_mcu_row + ri);
  862.       else
  863. #endif
  864.         (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
  865.       
  866.       (*cinfo->methods->reverse_DCT) (cinfo, coeff_data,
  867.                       subsampled_data[whichss],
  868.                       ri * DCTSIZE);
  869.     } else {
  870.       /* Need to pad out with copies of the last subsampled row. */
  871.       /* This can only happen if there is just one component. */
  872.       duplicate_row(subsampled_data[whichss][0],
  873.             cinfo->cur_comp_info[0]->subsampled_width,
  874.             ri * DCTSIZE - 1, DCTSIZE);
  875.     }
  876.       }
  877.       
  878.       /* Unsubsample the data */
  879.       /* First time through is a special case */
  880.       
  881.       if (cur_mcu_row) {
  882.     /* Expand last row group of previous set */
  883.     expand(cinfo, subsampled_data[whichss], fullsize_ptrs, fullsize_width,
  884.            (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
  885.            (short) (DCTSIZE-1));
  886.     /* If single scan, can do color quantization prescan on-the-fly */
  887.     if (single_scan)
  888.       (*cinfo->methods->color_quant_prescan) (cinfo, rows_in_mem,
  889.                           fullsize_ptrs,
  890.                           output_workspace[0]);
  891.     /* Realign the big buffers */
  892.     pixel_rows_output += rows_in_mem;
  893.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  894.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  895.         (fullsize_image[cinfo->cur_comp_info[ci]->component_index],
  896.          pixel_rows_output, TRUE);
  897.     }
  898.     /* Expand first row group of this set */
  899.     expand(cinfo, subsampled_data[whichss], fullsize_ptrs, fullsize_width,
  900.            (short) (DCTSIZE+1), (short) 0, (short) 1,
  901.            (short) 0);
  902.       } else {
  903.     /* Expand first row group with dummy above-context */
  904.     expand(cinfo, subsampled_data[whichss], fullsize_ptrs, fullsize_width,
  905.            (short) (-1), (short) 0, (short) 1,
  906.            (short) 0);
  907.       }
  908.       /* Expand second through next-to-last row groups of this set */
  909.       for (i = 1; i <= DCTSIZE-2; i++) {
  910.     expand(cinfo, subsampled_data[whichss], fullsize_ptrs, fullsize_width,
  911.            (short) (i-1), (short) i, (short) (i+1),
  912.            (short) i);
  913.       }
  914.     } /* end of loop over scan's data */
  915.     
  916.     /* Expand the last row group with dummy below-context */
  917.     /* Note whichss points to last buffer side used */
  918.     expand(cinfo, subsampled_data[whichss], fullsize_ptrs, fullsize_width,
  919.        (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
  920.        (short) (DCTSIZE-1));
  921.     /* If single scan, finish on-the-fly color quantization prescan */
  922.     if (single_scan)
  923.       (*cinfo->methods->color_quant_prescan) (cinfo,
  924.             (int) (cinfo->image_height - pixel_rows_output),
  925.             fullsize_ptrs, output_workspace[0]);
  926.     
  927.     /* Clean up after the scan */
  928.     (*cinfo->methods->disassemble_term) (cinfo);
  929.     (*cinfo->methods->unsubsample_term) (cinfo);
  930.     (*cinfo->methods->entropy_decoder_term) (cinfo);
  931.     (*cinfo->methods->read_scan_trailer) (cinfo);
  932.     if (single_scan)
  933.       cinfo->completed_passes++;
  934.     else
  935.       cinfo->completed_passes += cinfo->comps_in_scan;
  936.  
  937.     /* Release scan-local working memory */
  938.     free_MCU_row(cinfo, coeff_data);
  939. #ifdef BLOCK_SMOOTHING_SUPPORTED
  940.     if (cinfo->do_block_smoothing) {
  941.       free_MCU_row(cinfo, bsmooth[0]);
  942.       free_MCU_row(cinfo, bsmooth[1]);
  943.       free_MCU_row(cinfo, bsmooth[2]);
  944.     }
  945. #endif
  946.     free_sampling_buffer(cinfo, subsampled_data);
  947.     
  948.     /* Repeat if there is another scan */
  949.   } while ((!single_scan) && (*cinfo->methods->read_scan_header) (cinfo));
  950.  
  951.   if (single_scan) {
  952.     /* If we expected just one scan, make SURE there's just one */
  953.     if ((*cinfo->methods->read_scan_header) (cinfo))
  954.       ERREXIT(cinfo->emethods, "Didn't expect more than one scan");
  955.     /* We did the CQ prescan on-the-fly, so we are all set. */
  956.   } else {
  957.     /* For multiple-scan file, do the CQ prescan as a separate pass. */
  958.     /* The main reason why prescan is passed the output_workspace is */
  959.     /* so that we can use scan_big_image to call it... */
  960.     if (cinfo->two_pass_quantize)
  961.       scan_big_image(cinfo, cinfo->methods->color_quant_prescan);
  962.   }
  963.  
  964.   /* Now that we've collected the data, do color processing and output */
  965.   if (cinfo->two_pass_quantize)
  966.     (*cinfo->methods->color_quant_doit) (cinfo, scan_big_image);
  967.   else
  968.     scan_big_image(cinfo, emit_1pass);
  969.  
  970.   /* Release working memory */
  971.   /* (no work -- we let free_all release what's needful) */
  972. }
  973.  
  974. #endif /* NEED_COMPLEX_CONTROLLER */
  975.  
  976.  
  977. /*
  978.  * The method selection routine for decompression pipeline controllers.
  979.  * Note that at this point we've already read the JPEG header and first SOS,
  980.  * so we can tell whether the input is one scan or not.
  981.  */
  982.  
  983. GLOBAL void
  984. jseldpipeline (decompress_info_ptr cinfo)
  985. {
  986.   /* simplify subsequent tests on color quantization */
  987.   if (! cinfo->quantize_colors)
  988.     cinfo->two_pass_quantize = FALSE;
  989.   
  990.   if (cinfo->comps_in_scan == cinfo->num_components) {
  991.     /* It's a single-scan file */
  992.     if (cinfo->two_pass_quantize) {
  993. #ifdef NEED_COMPLEX_CONTROLLER
  994.       cinfo->methods->d_pipeline_controller = complex_dcontroller;
  995. #else
  996.       ERREXIT(cinfo->emethods, "2-pass quantization support was not compiled");
  997. #endif
  998.     } else
  999.       cinfo->methods->d_pipeline_controller = simple_dcontroller;
  1000.   } else {
  1001.     /* It's a multiple-scan file */
  1002. #ifdef NEED_COMPLEX_CONTROLLER
  1003.     cinfo->methods->d_pipeline_controller = complex_dcontroller;
  1004. #else
  1005.     ERREXIT(cinfo->emethods, "Multiple-scan support was not compiled");
  1006. #endif
  1007.   }
  1008. }
  1009.