dbf-halloween2015

diff libs/libjpeg/jdmainct.c @ 1:c3f5c32cb210

barfed all the libraries in the source tree to make porting easier
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:36:56 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libjpeg/jdmainct.c	Sun Nov 01 00:36:56 2015 +0200
     1.3 @@ -0,0 +1,512 @@
     1.4 +/*
     1.5 + * jdmainct.c
     1.6 + *
     1.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
     1.8 + * This file is part of the Independent JPEG Group's software.
     1.9 + * For conditions of distribution and use, see the accompanying README file.
    1.10 + *
    1.11 + * This file contains the main buffer controller for decompression.
    1.12 + * The main buffer lies between the JPEG decompressor proper and the
    1.13 + * post-processor; it holds downsampled data in the JPEG colorspace.
    1.14 + *
    1.15 + * Note that this code is bypassed in raw-data mode, since the application
    1.16 + * supplies the equivalent of the main buffer in that case.
    1.17 + */
    1.18 +
    1.19 +#define JPEG_INTERNALS
    1.20 +#include "jinclude.h"
    1.21 +#include "jpeglib.h"
    1.22 +
    1.23 +
    1.24 +/*
    1.25 + * In the current system design, the main buffer need never be a full-image
    1.26 + * buffer; any full-height buffers will be found inside the coefficient or
    1.27 + * postprocessing controllers.  Nonetheless, the main controller is not
    1.28 + * trivial.  Its responsibility is to provide context rows for upsampling/
    1.29 + * rescaling, and doing this in an efficient fashion is a bit tricky.
    1.30 + *
    1.31 + * Postprocessor input data is counted in "row groups".  A row group
    1.32 + * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
    1.33 + * sample rows of each component.  (We require DCT_scaled_size values to be
    1.34 + * chosen such that these numbers are integers.  In practice DCT_scaled_size
    1.35 + * values will likely be powers of two, so we actually have the stronger
    1.36 + * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
    1.37 + * Upsampling will typically produce max_v_samp_factor pixel rows from each
    1.38 + * row group (times any additional scale factor that the upsampler is
    1.39 + * applying).
    1.40 + *
    1.41 + * The coefficient controller will deliver data to us one iMCU row at a time;
    1.42 + * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
    1.43 + * exactly min_DCT_scaled_size row groups.  (This amount of data corresponds
    1.44 + * to one row of MCUs when the image is fully interleaved.)  Note that the
    1.45 + * number of sample rows varies across components, but the number of row
    1.46 + * groups does not.  Some garbage sample rows may be included in the last iMCU
    1.47 + * row at the bottom of the image.
    1.48 + *
    1.49 + * Depending on the vertical scaling algorithm used, the upsampler may need
    1.50 + * access to the sample row(s) above and below its current input row group.
    1.51 + * The upsampler is required to set need_context_rows TRUE at global selection
    1.52 + * time if so.  When need_context_rows is FALSE, this controller can simply
    1.53 + * obtain one iMCU row at a time from the coefficient controller and dole it
    1.54 + * out as row groups to the postprocessor.
    1.55 + *
    1.56 + * When need_context_rows is TRUE, this controller guarantees that the buffer
    1.57 + * passed to postprocessing contains at least one row group's worth of samples
    1.58 + * above and below the row group(s) being processed.  Note that the context
    1.59 + * rows "above" the first passed row group appear at negative row offsets in
    1.60 + * the passed buffer.  At the top and bottom of the image, the required
    1.61 + * context rows are manufactured by duplicating the first or last real sample
    1.62 + * row; this avoids having special cases in the upsampling inner loops.
    1.63 + *
    1.64 + * The amount of context is fixed at one row group just because that's a
    1.65 + * convenient number for this controller to work with.  The existing
    1.66 + * upsamplers really only need one sample row of context.  An upsampler
    1.67 + * supporting arbitrary output rescaling might wish for more than one row
    1.68 + * group of context when shrinking the image; tough, we don't handle that.
    1.69 + * (This is justified by the assumption that downsizing will be handled mostly
    1.70 + * by adjusting the DCT_scaled_size values, so that the actual scale factor at
    1.71 + * the upsample step needn't be much less than one.)
    1.72 + *
    1.73 + * To provide the desired context, we have to retain the last two row groups
    1.74 + * of one iMCU row while reading in the next iMCU row.  (The last row group
    1.75 + * can't be processed until we have another row group for its below-context,
    1.76 + * and so we have to save the next-to-last group too for its above-context.)
    1.77 + * We could do this most simply by copying data around in our buffer, but
    1.78 + * that'd be very slow.  We can avoid copying any data by creating a rather
    1.79 + * strange pointer structure.  Here's how it works.  We allocate a workspace
    1.80 + * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
    1.81 + * of row groups per iMCU row).  We create two sets of redundant pointers to
    1.82 + * the workspace.  Labeling the physical row groups 0 to M+1, the synthesized
    1.83 + * pointer lists look like this:
    1.84 + *                   M+1                          M-1
    1.85 + * master pointer --> 0         master pointer --> 0
    1.86 + *                    1                            1
    1.87 + *                   ...                          ...
    1.88 + *                   M-3                          M-3
    1.89 + *                   M-2                           M
    1.90 + *                   M-1                          M+1
    1.91 + *                    M                           M-2
    1.92 + *                   M+1                          M-1
    1.93 + *                    0                            0
    1.94 + * We read alternate iMCU rows using each master pointer; thus the last two
    1.95 + * row groups of the previous iMCU row remain un-overwritten in the workspace.
    1.96 + * The pointer lists are set up so that the required context rows appear to
    1.97 + * be adjacent to the proper places when we pass the pointer lists to the
    1.98 + * upsampler.
    1.99 + *
   1.100 + * The above pictures describe the normal state of the pointer lists.
   1.101 + * At top and bottom of the image, we diddle the pointer lists to duplicate
   1.102 + * the first or last sample row as necessary (this is cheaper than copying
   1.103 + * sample rows around).
   1.104 + *
   1.105 + * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1.  In that
   1.106 + * situation each iMCU row provides only one row group so the buffering logic
   1.107 + * must be different (eg, we must read two iMCU rows before we can emit the
   1.108 + * first row group).  For now, we simply do not support providing context
   1.109 + * rows when min_DCT_scaled_size is 1.  That combination seems unlikely to
   1.110 + * be worth providing --- if someone wants a 1/8th-size preview, they probably
   1.111 + * want it quick and dirty, so a context-free upsampler is sufficient.
   1.112 + */
   1.113 +
   1.114 +
   1.115 +/* Private buffer controller object */
   1.116 +
   1.117 +typedef struct {
   1.118 +  struct jpeg_d_main_controller pub; /* public fields */
   1.119 +
   1.120 +  /* Pointer to allocated workspace (M or M+2 row groups). */
   1.121 +  JSAMPARRAY buffer[MAX_COMPONENTS];
   1.122 +
   1.123 +  boolean buffer_full;		/* Have we gotten an iMCU row from decoder? */
   1.124 +  JDIMENSION rowgroup_ctr;	/* counts row groups output to postprocessor */
   1.125 +
   1.126 +  /* Remaining fields are only used in the context case. */
   1.127 +
   1.128 +  /* These are the master pointers to the funny-order pointer lists. */
   1.129 +  JSAMPIMAGE xbuffer[2];	/* pointers to weird pointer lists */
   1.130 +
   1.131 +  int whichptr;			/* indicates which pointer set is now in use */
   1.132 +  int context_state;		/* process_data state machine status */
   1.133 +  JDIMENSION rowgroups_avail;	/* row groups available to postprocessor */
   1.134 +  JDIMENSION iMCU_row_ctr;	/* counts iMCU rows to detect image top/bot */
   1.135 +} my_main_controller;
   1.136 +
   1.137 +typedef my_main_controller * my_main_ptr;
   1.138 +
   1.139 +/* context_state values: */
   1.140 +#define CTX_PREPARE_FOR_IMCU	0	/* need to prepare for MCU row */
   1.141 +#define CTX_PROCESS_IMCU	1	/* feeding iMCU to postprocessor */
   1.142 +#define CTX_POSTPONED_ROW	2	/* feeding postponed row group */
   1.143 +
   1.144 +
   1.145 +/* Forward declarations */
   1.146 +METHODDEF(void) process_data_simple_main
   1.147 +	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
   1.148 +	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
   1.149 +METHODDEF(void) process_data_context_main
   1.150 +	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
   1.151 +	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
   1.152 +#ifdef QUANT_2PASS_SUPPORTED
   1.153 +METHODDEF(void) process_data_crank_post
   1.154 +	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
   1.155 +	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
   1.156 +#endif
   1.157 +
   1.158 +
   1.159 +LOCAL(void)
   1.160 +alloc_funny_pointers (j_decompress_ptr cinfo)
   1.161 +/* Allocate space for the funny pointer lists.
   1.162 + * This is done only once, not once per pass.
   1.163 + */
   1.164 +{
   1.165 +  my_main_ptr main = (my_main_ptr) cinfo->main;
   1.166 +  int ci, rgroup;
   1.167 +  int M = cinfo->min_DCT_scaled_size;
   1.168 +  jpeg_component_info *compptr;
   1.169 +  JSAMPARRAY xbuf;
   1.170 +
   1.171 +  /* Get top-level space for component array pointers.
   1.172 +   * We alloc both arrays with one call to save a few cycles.
   1.173 +   */
   1.174 +  main->xbuffer[0] = (JSAMPIMAGE)
   1.175 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.176 +				cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
   1.177 +  main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components;
   1.178 +
   1.179 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.180 +       ci++, compptr++) {
   1.181 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
   1.182 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
   1.183 +    /* Get space for pointer lists --- M+4 row groups in each list.
   1.184 +     * We alloc both pointer lists with one call to save a few cycles.
   1.185 +     */
   1.186 +    xbuf = (JSAMPARRAY)
   1.187 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.188 +				  2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
   1.189 +    xbuf += rgroup;		/* want one row group at negative offsets */
   1.190 +    main->xbuffer[0][ci] = xbuf;
   1.191 +    xbuf += rgroup * (M + 4);
   1.192 +    main->xbuffer[1][ci] = xbuf;
   1.193 +  }
   1.194 +}
   1.195 +
   1.196 +
   1.197 +LOCAL(void)
   1.198 +make_funny_pointers (j_decompress_ptr cinfo)
   1.199 +/* Create the funny pointer lists discussed in the comments above.
   1.200 + * The actual workspace is already allocated (in main->buffer),
   1.201 + * and the space for the pointer lists is allocated too.
   1.202 + * This routine just fills in the curiously ordered lists.
   1.203 + * This will be repeated at the beginning of each pass.
   1.204 + */
   1.205 +{
   1.206 +  my_main_ptr main = (my_main_ptr) cinfo->main;
   1.207 +  int ci, i, rgroup;
   1.208 +  int M = cinfo->min_DCT_scaled_size;
   1.209 +  jpeg_component_info *compptr;
   1.210 +  JSAMPARRAY buf, xbuf0, xbuf1;
   1.211 +
   1.212 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.213 +       ci++, compptr++) {
   1.214 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
   1.215 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
   1.216 +    xbuf0 = main->xbuffer[0][ci];
   1.217 +    xbuf1 = main->xbuffer[1][ci];
   1.218 +    /* First copy the workspace pointers as-is */
   1.219 +    buf = main->buffer[ci];
   1.220 +    for (i = 0; i < rgroup * (M + 2); i++) {
   1.221 +      xbuf0[i] = xbuf1[i] = buf[i];
   1.222 +    }
   1.223 +    /* In the second list, put the last four row groups in swapped order */
   1.224 +    for (i = 0; i < rgroup * 2; i++) {
   1.225 +      xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
   1.226 +      xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
   1.227 +    }
   1.228 +    /* The wraparound pointers at top and bottom will be filled later
   1.229 +     * (see set_wraparound_pointers, below).  Initially we want the "above"
   1.230 +     * pointers to duplicate the first actual data line.  This only needs
   1.231 +     * to happen in xbuffer[0].
   1.232 +     */
   1.233 +    for (i = 0; i < rgroup; i++) {
   1.234 +      xbuf0[i - rgroup] = xbuf0[0];
   1.235 +    }
   1.236 +  }
   1.237 +}
   1.238 +
   1.239 +
   1.240 +LOCAL(void)
   1.241 +set_wraparound_pointers (j_decompress_ptr cinfo)
   1.242 +/* Set up the "wraparound" pointers at top and bottom of the pointer lists.
   1.243 + * This changes the pointer list state from top-of-image to the normal state.
   1.244 + */
   1.245 +{
   1.246 +  my_main_ptr main = (my_main_ptr) cinfo->main;
   1.247 +  int ci, i, rgroup;
   1.248 +  int M = cinfo->min_DCT_scaled_size;
   1.249 +  jpeg_component_info *compptr;
   1.250 +  JSAMPARRAY xbuf0, xbuf1;
   1.251 +
   1.252 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.253 +       ci++, compptr++) {
   1.254 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
   1.255 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
   1.256 +    xbuf0 = main->xbuffer[0][ci];
   1.257 +    xbuf1 = main->xbuffer[1][ci];
   1.258 +    for (i = 0; i < rgroup; i++) {
   1.259 +      xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
   1.260 +      xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
   1.261 +      xbuf0[rgroup*(M+2) + i] = xbuf0[i];
   1.262 +      xbuf1[rgroup*(M+2) + i] = xbuf1[i];
   1.263 +    }
   1.264 +  }
   1.265 +}
   1.266 +
   1.267 +
   1.268 +LOCAL(void)
   1.269 +set_bottom_pointers (j_decompress_ptr cinfo)
   1.270 +/* Change the pointer lists to duplicate the last sample row at the bottom
   1.271 + * of the image.  whichptr indicates which xbuffer holds the final iMCU row.
   1.272 + * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
   1.273 + */
   1.274 +{
   1.275 +  my_main_ptr main = (my_main_ptr) cinfo->main;
   1.276 +  int ci, i, rgroup, iMCUheight, rows_left;
   1.277 +  jpeg_component_info *compptr;
   1.278 +  JSAMPARRAY xbuf;
   1.279 +
   1.280 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.281 +       ci++, compptr++) {
   1.282 +    /* Count sample rows in one iMCU row and in one row group */
   1.283 +    iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
   1.284 +    rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
   1.285 +    /* Count nondummy sample rows remaining for this component */
   1.286 +    rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
   1.287 +    if (rows_left == 0) rows_left = iMCUheight;
   1.288 +    /* Count nondummy row groups.  Should get same answer for each component,
   1.289 +     * so we need only do it once.
   1.290 +     */
   1.291 +    if (ci == 0) {
   1.292 +      main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
   1.293 +    }
   1.294 +    /* Duplicate the last real sample row rgroup*2 times; this pads out the
   1.295 +     * last partial rowgroup and ensures at least one full rowgroup of context.
   1.296 +     */
   1.297 +    xbuf = main->xbuffer[main->whichptr][ci];
   1.298 +    for (i = 0; i < rgroup * 2; i++) {
   1.299 +      xbuf[rows_left + i] = xbuf[rows_left-1];
   1.300 +    }
   1.301 +  }
   1.302 +}
   1.303 +
   1.304 +
   1.305 +/*
   1.306 + * Initialize for a processing pass.
   1.307 + */
   1.308 +
   1.309 +METHODDEF(void)
   1.310 +start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
   1.311 +{
   1.312 +  my_main_ptr main = (my_main_ptr) cinfo->main;
   1.313 +
   1.314 +  switch (pass_mode) {
   1.315 +  case JBUF_PASS_THRU:
   1.316 +    if (cinfo->upsample->need_context_rows) {
   1.317 +      main->pub.process_data = process_data_context_main;
   1.318 +      make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
   1.319 +      main->whichptr = 0;	/* Read first iMCU row into xbuffer[0] */
   1.320 +      main->context_state = CTX_PREPARE_FOR_IMCU;
   1.321 +      main->iMCU_row_ctr = 0;
   1.322 +    } else {
   1.323 +      /* Simple case with no context needed */
   1.324 +      main->pub.process_data = process_data_simple_main;
   1.325 +    }
   1.326 +    main->buffer_full = FALSE;	/* Mark buffer empty */
   1.327 +    main->rowgroup_ctr = 0;
   1.328 +    break;
   1.329 +#ifdef QUANT_2PASS_SUPPORTED
   1.330 +  case JBUF_CRANK_DEST:
   1.331 +    /* For last pass of 2-pass quantization, just crank the postprocessor */
   1.332 +    main->pub.process_data = process_data_crank_post;
   1.333 +    break;
   1.334 +#endif
   1.335 +  default:
   1.336 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   1.337 +    break;
   1.338 +  }
   1.339 +}
   1.340 +
   1.341 +
   1.342 +/*
   1.343 + * Process some data.
   1.344 + * This handles the simple case where no context is required.
   1.345 + */
   1.346 +
   1.347 +METHODDEF(void)
   1.348 +process_data_simple_main (j_decompress_ptr cinfo,
   1.349 +			  JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   1.350 +			  JDIMENSION out_rows_avail)
   1.351 +{
   1.352 +  my_main_ptr main = (my_main_ptr) cinfo->main;
   1.353 +  JDIMENSION rowgroups_avail;
   1.354 +
   1.355 +  /* Read input data if we haven't filled the main buffer yet */
   1.356 +  if (! main->buffer_full) {
   1.357 +    if (! (*cinfo->coef->decompress_data) (cinfo, main->buffer))
   1.358 +      return;			/* suspension forced, can do nothing more */
   1.359 +    main->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
   1.360 +  }
   1.361 +
   1.362 +  /* There are always min_DCT_scaled_size row groups in an iMCU row. */
   1.363 +  rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
   1.364 +  /* Note: at the bottom of the image, we may pass extra garbage row groups
   1.365 +   * to the postprocessor.  The postprocessor has to check for bottom
   1.366 +   * of image anyway (at row resolution), so no point in us doing it too.
   1.367 +   */
   1.368 +
   1.369 +  /* Feed the postprocessor */
   1.370 +  (*cinfo->post->post_process_data) (cinfo, main->buffer,
   1.371 +				     &main->rowgroup_ctr, rowgroups_avail,
   1.372 +				     output_buf, out_row_ctr, out_rows_avail);
   1.373 +
   1.374 +  /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
   1.375 +  if (main->rowgroup_ctr >= rowgroups_avail) {
   1.376 +    main->buffer_full = FALSE;
   1.377 +    main->rowgroup_ctr = 0;
   1.378 +  }
   1.379 +}
   1.380 +
   1.381 +
   1.382 +/*
   1.383 + * Process some data.
   1.384 + * This handles the case where context rows must be provided.
   1.385 + */
   1.386 +
   1.387 +METHODDEF(void)
   1.388 +process_data_context_main (j_decompress_ptr cinfo,
   1.389 +			   JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   1.390 +			   JDIMENSION out_rows_avail)
   1.391 +{
   1.392 +  my_main_ptr main = (my_main_ptr) cinfo->main;
   1.393 +
   1.394 +  /* Read input data if we haven't filled the main buffer yet */
   1.395 +  if (! main->buffer_full) {
   1.396 +    if (! (*cinfo->coef->decompress_data) (cinfo,
   1.397 +					   main->xbuffer[main->whichptr]))
   1.398 +      return;			/* suspension forced, can do nothing more */
   1.399 +    main->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
   1.400 +    main->iMCU_row_ctr++;	/* count rows received */
   1.401 +  }
   1.402 +
   1.403 +  /* Postprocessor typically will not swallow all the input data it is handed
   1.404 +   * in one call (due to filling the output buffer first).  Must be prepared
   1.405 +   * to exit and restart.  This switch lets us keep track of how far we got.
   1.406 +   * Note that each case falls through to the next on successful completion.
   1.407 +   */
   1.408 +  switch (main->context_state) {
   1.409 +  case CTX_POSTPONED_ROW:
   1.410 +    /* Call postprocessor using previously set pointers for postponed row */
   1.411 +    (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
   1.412 +			&main->rowgroup_ctr, main->rowgroups_avail,
   1.413 +			output_buf, out_row_ctr, out_rows_avail);
   1.414 +    if (main->rowgroup_ctr < main->rowgroups_avail)
   1.415 +      return;			/* Need to suspend */
   1.416 +    main->context_state = CTX_PREPARE_FOR_IMCU;
   1.417 +    if (*out_row_ctr >= out_rows_avail)
   1.418 +      return;			/* Postprocessor exactly filled output buf */
   1.419 +    /*FALLTHROUGH*/
   1.420 +  case CTX_PREPARE_FOR_IMCU:
   1.421 +    /* Prepare to process first M-1 row groups of this iMCU row */
   1.422 +    main->rowgroup_ctr = 0;
   1.423 +    main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
   1.424 +    /* Check for bottom of image: if so, tweak pointers to "duplicate"
   1.425 +     * the last sample row, and adjust rowgroups_avail to ignore padding rows.
   1.426 +     */
   1.427 +    if (main->iMCU_row_ctr == cinfo->total_iMCU_rows)
   1.428 +      set_bottom_pointers(cinfo);
   1.429 +    main->context_state = CTX_PROCESS_IMCU;
   1.430 +    /*FALLTHROUGH*/
   1.431 +  case CTX_PROCESS_IMCU:
   1.432 +    /* Call postprocessor using previously set pointers */
   1.433 +    (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
   1.434 +			&main->rowgroup_ctr, main->rowgroups_avail,
   1.435 +			output_buf, out_row_ctr, out_rows_avail);
   1.436 +    if (main->rowgroup_ctr < main->rowgroups_avail)
   1.437 +      return;			/* Need to suspend */
   1.438 +    /* After the first iMCU, change wraparound pointers to normal state */
   1.439 +    if (main->iMCU_row_ctr == 1)
   1.440 +      set_wraparound_pointers(cinfo);
   1.441 +    /* Prepare to load new iMCU row using other xbuffer list */
   1.442 +    main->whichptr ^= 1;	/* 0=>1 or 1=>0 */
   1.443 +    main->buffer_full = FALSE;
   1.444 +    /* Still need to process last row group of this iMCU row, */
   1.445 +    /* which is saved at index M+1 of the other xbuffer */
   1.446 +    main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
   1.447 +    main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
   1.448 +    main->context_state = CTX_POSTPONED_ROW;
   1.449 +  }
   1.450 +}
   1.451 +
   1.452 +
   1.453 +/*
   1.454 + * Process some data.
   1.455 + * Final pass of two-pass quantization: just call the postprocessor.
   1.456 + * Source data will be the postprocessor controller's internal buffer.
   1.457 + */
   1.458 +
   1.459 +#ifdef QUANT_2PASS_SUPPORTED
   1.460 +
   1.461 +METHODDEF(void)
   1.462 +process_data_crank_post (j_decompress_ptr cinfo,
   1.463 +			 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   1.464 +			 JDIMENSION out_rows_avail)
   1.465 +{
   1.466 +  (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
   1.467 +				     (JDIMENSION *) NULL, (JDIMENSION) 0,
   1.468 +				     output_buf, out_row_ctr, out_rows_avail);
   1.469 +}
   1.470 +
   1.471 +#endif /* QUANT_2PASS_SUPPORTED */
   1.472 +
   1.473 +
   1.474 +/*
   1.475 + * Initialize main buffer controller.
   1.476 + */
   1.477 +
   1.478 +GLOBAL(void)
   1.479 +jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
   1.480 +{
   1.481 +  my_main_ptr main;
   1.482 +  int ci, rgroup, ngroups;
   1.483 +  jpeg_component_info *compptr;
   1.484 +
   1.485 +  main = (my_main_ptr)
   1.486 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.487 +				SIZEOF(my_main_controller));
   1.488 +  cinfo->main = (struct jpeg_d_main_controller *) main;
   1.489 +  main->pub.start_pass = start_pass_main;
   1.490 +
   1.491 +  if (need_full_buffer)		/* shouldn't happen */
   1.492 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   1.493 +
   1.494 +  /* Allocate the workspace.
   1.495 +   * ngroups is the number of row groups we need.
   1.496 +   */
   1.497 +  if (cinfo->upsample->need_context_rows) {
   1.498 +    if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */
   1.499 +      ERREXIT(cinfo, JERR_NOTIMPL);
   1.500 +    alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
   1.501 +    ngroups = cinfo->min_DCT_scaled_size + 2;
   1.502 +  } else {
   1.503 +    ngroups = cinfo->min_DCT_scaled_size;
   1.504 +  }
   1.505 +
   1.506 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.507 +       ci++, compptr++) {
   1.508 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
   1.509 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
   1.510 +    main->buffer[ci] = (*cinfo->mem->alloc_sarray)
   1.511 +			((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.512 +			 compptr->width_in_blocks * compptr->DCT_scaled_size,
   1.513 +			 (JDIMENSION) (rgroup * ngroups));
   1.514 +  }
   1.515 +}