media/libjpeg/jdmainct.c

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

mercurial