kusano 7d535a
/*
kusano 7d535a
 * jdmainct.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1996, Thomas G. Lane.
kusano 7d535a
 * Modified 2002-2012 by Guido Vollbeding.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains the main buffer controller for decompression.
kusano 7d535a
 * The main buffer lies between the JPEG decompressor proper and the
kusano 7d535a
 * post-processor; it holds downsampled data in the JPEG colorspace.
kusano 7d535a
 *
kusano 7d535a
 * Note that this code is bypassed in raw-data mode, since the application
kusano 7d535a
 * supplies the equivalent of the main buffer in that case.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * In the current system design, the main buffer need never be a full-image
kusano 7d535a
 * buffer; any full-height buffers will be found inside the coefficient or
kusano 7d535a
 * postprocessing controllers.  Nonetheless, the main controller is not
kusano 7d535a
 * trivial.  Its responsibility is to provide context rows for upsampling/
kusano 7d535a
 * rescaling, and doing this in an efficient fashion is a bit tricky.
kusano 7d535a
 *
kusano 7d535a
 * Postprocessor input data is counted in "row groups".  A row group
kusano 7d535a
 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
kusano 7d535a
 * sample rows of each component.  (We require DCT_scaled_size values to be
kusano 7d535a
 * chosen such that these numbers are integers.  In practice DCT_scaled_size
kusano 7d535a
 * values will likely be powers of two, so we actually have the stronger
kusano 7d535a
 * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
kusano 7d535a
 * Upsampling will typically produce max_v_samp_factor pixel rows from each
kusano 7d535a
 * row group (times any additional scale factor that the upsampler is
kusano 7d535a
 * applying).
kusano 7d535a
 *
kusano 7d535a
 * The coefficient controller will deliver data to us one iMCU row at a time;
kusano 7d535a
 * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
kusano 7d535a
 * exactly min_DCT_scaled_size row groups.  (This amount of data corresponds
kusano 7d535a
 * to one row of MCUs when the image is fully interleaved.)  Note that the
kusano 7d535a
 * number of sample rows varies across components, but the number of row
kusano 7d535a
 * groups does not.  Some garbage sample rows may be included in the last iMCU
kusano 7d535a
 * row at the bottom of the image.
kusano 7d535a
 *
kusano 7d535a
 * Depending on the vertical scaling algorithm used, the upsampler may need
kusano 7d535a
 * access to the sample row(s) above and below its current input row group.
kusano 7d535a
 * The upsampler is required to set need_context_rows TRUE at global selection
kusano 7d535a
 * time if so.  When need_context_rows is FALSE, this controller can simply
kusano 7d535a
 * obtain one iMCU row at a time from the coefficient controller and dole it
kusano 7d535a
 * out as row groups to the postprocessor.
kusano 7d535a
 *
kusano 7d535a
 * When need_context_rows is TRUE, this controller guarantees that the buffer
kusano 7d535a
 * passed to postprocessing contains at least one row group's worth of samples
kusano 7d535a
 * above and below the row group(s) being processed.  Note that the context
kusano 7d535a
 * rows "above" the first passed row group appear at negative row offsets in
kusano 7d535a
 * the passed buffer.  At the top and bottom of the image, the required
kusano 7d535a
 * context rows are manufactured by duplicating the first or last real sample
kusano 7d535a
 * row; this avoids having special cases in the upsampling inner loops.
kusano 7d535a
 *
kusano 7d535a
 * The amount of context is fixed at one row group just because that's a
kusano 7d535a
 * convenient number for this controller to work with.  The existing
kusano 7d535a
 * upsamplers really only need one sample row of context.  An upsampler
kusano 7d535a
 * supporting arbitrary output rescaling might wish for more than one row
kusano 7d535a
 * group of context when shrinking the image; tough, we don't handle that.
kusano 7d535a
 * (This is justified by the assumption that downsizing will be handled mostly
kusano 7d535a
 * by adjusting the DCT_scaled_size values, so that the actual scale factor at
kusano 7d535a
 * the upsample step needn't be much less than one.)
kusano 7d535a
 *
kusano 7d535a
 * To provide the desired context, we have to retain the last two row groups
kusano 7d535a
 * of one iMCU row while reading in the next iMCU row.  (The last row group
kusano 7d535a
 * can't be processed until we have another row group for its below-context,
kusano 7d535a
 * and so we have to save the next-to-last group too for its above-context.)
kusano 7d535a
 * We could do this most simply by copying data around in our buffer, but
kusano 7d535a
 * that'd be very slow.  We can avoid copying any data by creating a rather
kusano 7d535a
 * strange pointer structure.  Here's how it works.  We allocate a workspace
kusano 7d535a
 * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
kusano 7d535a
 * of row groups per iMCU row).  We create two sets of redundant pointers to
kusano 7d535a
 * the workspace.  Labeling the physical row groups 0 to M+1, the synthesized
kusano 7d535a
 * pointer lists look like this:
kusano 7d535a
 *                   M+1                          M-1
kusano 7d535a
 * master pointer --> 0         master pointer --> 0
kusano 7d535a
 *                    1                            1
kusano 7d535a
 *                   ...                          ...
kusano 7d535a
 *                   M-3                          M-3
kusano 7d535a
 *                   M-2                           M
kusano 7d535a
 *                   M-1                          M+1
kusano 7d535a
 *                    M                           M-2
kusano 7d535a
 *                   M+1                          M-1
kusano 7d535a
 *                    0                            0
kusano 7d535a
 * We read alternate iMCU rows using each master pointer; thus the last two
kusano 7d535a
 * row groups of the previous iMCU row remain un-overwritten in the workspace.
kusano 7d535a
 * The pointer lists are set up so that the required context rows appear to
kusano 7d535a
 * be adjacent to the proper places when we pass the pointer lists to the
kusano 7d535a
 * upsampler.
kusano 7d535a
 *
kusano 7d535a
 * The above pictures describe the normal state of the pointer lists.
kusano 7d535a
 * At top and bottom of the image, we diddle the pointer lists to duplicate
kusano 7d535a
 * the first or last sample row as necessary (this is cheaper than copying
kusano 7d535a
 * sample rows around).
kusano 7d535a
 *
kusano 7d535a
 * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1.  In that
kusano 7d535a
 * situation each iMCU row provides only one row group so the buffering logic
kusano 7d535a
 * must be different (eg, we must read two iMCU rows before we can emit the
kusano 7d535a
 * first row group).  For now, we simply do not support providing context
kusano 7d535a
 * rows when min_DCT_scaled_size is 1.  That combination seems unlikely to
kusano 7d535a
 * be worth providing --- if someone wants a 1/8th-size preview, they probably
kusano 7d535a
 * want it quick and dirty, so a context-free upsampler is sufficient.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Private buffer controller object */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_d_main_controller pub; /* public fields */
kusano 7d535a
kusano 7d535a
  /* Pointer to allocated workspace (M or M+2 row groups). */
kusano 7d535a
  JSAMPARRAY buffer[MAX_COMPONENTS];
kusano 7d535a
kusano 7d535a
  boolean buffer_full;		/* Have we gotten an iMCU row from decoder? */
kusano 7d535a
  JDIMENSION rowgroup_ctr;	/* counts row groups output to postprocessor */
kusano 7d535a
kusano 7d535a
  /* Remaining fields are only used in the context case. */
kusano 7d535a
kusano 7d535a
  /* These are the master pointers to the funny-order pointer lists. */
kusano 7d535a
  JSAMPIMAGE xbuffer[2];	/* pointers to weird pointer lists */
kusano 7d535a
kusano 7d535a
  int whichptr;			/* indicates which pointer set is now in use */
kusano 7d535a
  int context_state;		/* process_data state machine status */
kusano 7d535a
  JDIMENSION rowgroups_avail;	/* row groups available to postprocessor */
kusano 7d535a
  JDIMENSION iMCU_row_ctr;	/* counts iMCU rows to detect image top/bot */
kusano 7d535a
} my_main_controller;
kusano 7d535a
kusano 7d535a
typedef my_main_controller * my_main_ptr;
kusano 7d535a
kusano 7d535a
/* context_state values: */
kusano 7d535a
#define CTX_PREPARE_FOR_IMCU	0	/* need to prepare for MCU row */
kusano 7d535a
#define CTX_PROCESS_IMCU	1	/* feeding iMCU to postprocessor */
kusano 7d535a
#define CTX_POSTPONED_ROW	2	/* feeding postponed row group */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Forward declarations */
kusano 7d535a
METHODDEF(void) process_data_simple_main
kusano 7d535a
	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
kusano 7d535a
	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
kusano 7d535a
METHODDEF(void) process_data_context_main
kusano 7d535a
	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
kusano 7d535a
	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
kusano 7d535a
#ifdef QUANT_2PASS_SUPPORTED
kusano 7d535a
METHODDEF(void) process_data_crank_post
kusano 7d535a
	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
kusano 7d535a
	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
alloc_funny_pointers (j_decompress_ptr cinfo)
kusano 7d535a
/* Allocate space for the funny pointer lists.
kusano 7d535a
 * This is done only once, not once per pass.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
kusano 7d535a
  int ci, rgroup;
kusano 7d535a
  int M = cinfo->min_DCT_v_scaled_size;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  JSAMPARRAY xbuf;
kusano 7d535a
kusano 7d535a
  /* Get top-level space for component array pointers.
kusano 7d535a
   * We alloc both arrays with one call to save a few cycles.
kusano 7d535a
   */
kusano 7d535a
  mainp->xbuffer[0] = (JSAMPIMAGE)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
kusano 7d535a
  mainp->xbuffer[1] = mainp->xbuffer[0] + cinfo->num_components;
kusano 7d535a
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
kusano 7d535a
      cinfo->min_DCT_v_scaled_size; /* height of a row group of component */
kusano 7d535a
    /* Get space for pointer lists --- M+4 row groups in each list.
kusano 7d535a
     * We alloc both pointer lists with one call to save a few cycles.
kusano 7d535a
     */
kusano 7d535a
    xbuf = (JSAMPARRAY)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
kusano 7d535a
    xbuf += rgroup;		/* want one row group at negative offsets */
kusano 7d535a
    mainp->xbuffer[0][ci] = xbuf;
kusano 7d535a
    xbuf += rgroup * (M + 4);
kusano 7d535a
    mainp->xbuffer[1][ci] = xbuf;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
make_funny_pointers (j_decompress_ptr cinfo)
kusano 7d535a
/* Create the funny pointer lists discussed in the comments above.
kusano 7d535a
 * The actual workspace is already allocated (in main->buffer),
kusano 7d535a
 * and the space for the pointer lists is allocated too.
kusano 7d535a
 * This routine just fills in the curiously ordered lists.
kusano 7d535a
 * This will be repeated at the beginning of each pass.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
kusano 7d535a
  int ci, i, rgroup;
kusano 7d535a
  int M = cinfo->min_DCT_v_scaled_size;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  JSAMPARRAY buf, xbuf0, xbuf1;
kusano 7d535a
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
kusano 7d535a
      cinfo->min_DCT_v_scaled_size; /* height of a row group of component */
kusano 7d535a
    xbuf0 = mainp->xbuffer[0][ci];
kusano 7d535a
    xbuf1 = mainp->xbuffer[1][ci];
kusano 7d535a
    /* First copy the workspace pointers as-is */
kusano 7d535a
    buf = mainp->buffer[ci];
kusano 7d535a
    for (i = 0; i < rgroup * (M + 2); i++) {
kusano 7d535a
      xbuf0[i] = xbuf1[i] = buf[i];
kusano 7d535a
    }
kusano 7d535a
    /* In the second list, put the last four row groups in swapped order */
kusano 7d535a
    for (i = 0; i < rgroup * 2; i++) {
kusano 7d535a
      xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
kusano 7d535a
      xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
kusano 7d535a
    }
kusano 7d535a
    /* The wraparound pointers at top and bottom will be filled later
kusano 7d535a
     * (see set_wraparound_pointers, below).  Initially we want the "above"
kusano 7d535a
     * pointers to duplicate the first actual data line.  This only needs
kusano 7d535a
     * to happen in xbuffer[0].
kusano 7d535a
     */
kusano 7d535a
    for (i = 0; i < rgroup; i++) {
kusano 7d535a
      xbuf0[i - rgroup] = xbuf0[0];
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
set_wraparound_pointers (j_decompress_ptr cinfo)
kusano 7d535a
/* Set up the "wraparound" pointers at top and bottom of the pointer lists.
kusano 7d535a
 * This changes the pointer list state from top-of-image to the normal state.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
kusano 7d535a
  int ci, i, rgroup;
kusano 7d535a
  int M = cinfo->min_DCT_v_scaled_size;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  JSAMPARRAY xbuf0, xbuf1;
kusano 7d535a
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
kusano 7d535a
      cinfo->min_DCT_v_scaled_size; /* height of a row group of component */
kusano 7d535a
    xbuf0 = mainp->xbuffer[0][ci];
kusano 7d535a
    xbuf1 = mainp->xbuffer[1][ci];
kusano 7d535a
    for (i = 0; i < rgroup; i++) {
kusano 7d535a
      xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
kusano 7d535a
      xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
kusano 7d535a
      xbuf0[rgroup*(M+2) + i] = xbuf0[i];
kusano 7d535a
      xbuf1[rgroup*(M+2) + i] = xbuf1[i];
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
set_bottom_pointers (j_decompress_ptr cinfo)
kusano 7d535a
/* Change the pointer lists to duplicate the last sample row at the bottom
kusano 7d535a
 * of the image.  whichptr indicates which xbuffer holds the final iMCU row.
kusano 7d535a
 * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
kusano 7d535a
  int ci, i, rgroup, iMCUheight, rows_left;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  JSAMPARRAY xbuf;
kusano 7d535a
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    /* Count sample rows in one iMCU row and in one row group */
kusano 7d535a
    iMCUheight = compptr->v_samp_factor * compptr->DCT_v_scaled_size;
kusano 7d535a
    rgroup = iMCUheight / cinfo->min_DCT_v_scaled_size;
kusano 7d535a
    /* Count nondummy sample rows remaining for this component */
kusano 7d535a
    rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
kusano 7d535a
    if (rows_left == 0) rows_left = iMCUheight;
kusano 7d535a
    /* Count nondummy row groups.  Should get same answer for each component,
kusano 7d535a
     * so we need only do it once.
kusano 7d535a
     */
kusano 7d535a
    if (ci == 0) {
kusano 7d535a
      mainp->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
kusano 7d535a
    }
kusano 7d535a
    /* Duplicate the last real sample row rgroup*2 times; this pads out the
kusano 7d535a
     * last partial rowgroup and ensures at least one full rowgroup of context.
kusano 7d535a
     */
kusano 7d535a
    xbuf = mainp->xbuffer[mainp->whichptr][ci];
kusano 7d535a
    for (i = 0; i < rgroup * 2; i++) {
kusano 7d535a
      xbuf[rows_left + i] = xbuf[rows_left-1];
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize for a processing pass.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
kusano 7d535a
kusano 7d535a
  switch (pass_mode) {
kusano 7d535a
  case JBUF_PASS_THRU:
kusano 7d535a
    if (cinfo->upsample->need_context_rows) {
kusano 7d535a
      mainp->pub.process_data = process_data_context_main;
kusano 7d535a
      make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
kusano 7d535a
      mainp->whichptr = 0;	/* Read first iMCU row into xbuffer[0] */
kusano 7d535a
      mainp->context_state = CTX_PREPARE_FOR_IMCU;
kusano 7d535a
      mainp->iMCU_row_ctr = 0;
kusano 7d535a
    } else {
kusano 7d535a
      /* Simple case with no context needed */
kusano 7d535a
      mainp->pub.process_data = process_data_simple_main;
kusano 7d535a
    }
kusano 7d535a
    mainp->buffer_full = FALSE;	/* Mark buffer empty */
kusano 7d535a
    mainp->rowgroup_ctr = 0;
kusano 7d535a
    break;
kusano 7d535a
#ifdef QUANT_2PASS_SUPPORTED
kusano 7d535a
  case JBUF_CRANK_DEST:
kusano 7d535a
    /* For last pass of 2-pass quantization, just crank the postprocessor */
kusano 7d535a
    mainp->pub.process_data = process_data_crank_post;
kusano 7d535a
    break;
kusano 7d535a
#endif
kusano 7d535a
  default:
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Process some data.
kusano 7d535a
 * This handles the simple case where no context is required.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
process_data_simple_main (j_decompress_ptr cinfo,
kusano 7d535a
			  JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
kusano 7d535a
			  JDIMENSION out_rows_avail)
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
kusano 7d535a
  JDIMENSION rowgroups_avail;
kusano 7d535a
kusano 7d535a
  /* Read input data if we haven't filled the main buffer yet */
kusano 7d535a
  if (! mainp->buffer_full) {
kusano 7d535a
    if (! (*cinfo->coef->decompress_data) (cinfo, mainp->buffer))
kusano 7d535a
      return;			/* suspension forced, can do nothing more */
kusano 7d535a
    mainp->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* There are always min_DCT_scaled_size row groups in an iMCU row. */
kusano 7d535a
  rowgroups_avail = (JDIMENSION) cinfo->min_DCT_v_scaled_size;
kusano 7d535a
  /* Note: at the bottom of the image, we may pass extra garbage row groups
kusano 7d535a
   * to the postprocessor.  The postprocessor has to check for bottom
kusano 7d535a
   * of image anyway (at row resolution), so no point in us doing it too.
kusano 7d535a
   */
kusano 7d535a
kusano 7d535a
  /* Feed the postprocessor */
kusano 7d535a
  (*cinfo->post->post_process_data) (cinfo, mainp->buffer,
kusano 7d535a
				     &mainp->rowgroup_ctr, rowgroups_avail,
kusano 7d535a
				     output_buf, out_row_ctr, out_rows_avail);
kusano 7d535a
kusano 7d535a
  /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
kusano 7d535a
  if (mainp->rowgroup_ctr >= rowgroups_avail) {
kusano 7d535a
    mainp->buffer_full = FALSE;
kusano 7d535a
    mainp->rowgroup_ctr = 0;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Process some data.
kusano 7d535a
 * This handles the case where context rows must be provided.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
process_data_context_main (j_decompress_ptr cinfo,
kusano 7d535a
			   JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
kusano 7d535a
			   JDIMENSION out_rows_avail)
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
kusano 7d535a
kusano 7d535a
  /* Read input data if we haven't filled the main buffer yet */
kusano 7d535a
  if (! mainp->buffer_full) {
kusano 7d535a
    if (! (*cinfo->coef->decompress_data) (cinfo,
kusano 7d535a
					   mainp->xbuffer[mainp->whichptr]))
kusano 7d535a
      return;			/* suspension forced, can do nothing more */
kusano 7d535a
    mainp->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
kusano 7d535a
    mainp->iMCU_row_ctr++;	/* count rows received */
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Postprocessor typically will not swallow all the input data it is handed
kusano 7d535a
   * in one call (due to filling the output buffer first).  Must be prepared
kusano 7d535a
   * to exit and restart.  This switch lets us keep track of how far we got.
kusano 7d535a
   * Note that each case falls through to the next on successful completion.
kusano 7d535a
   */
kusano 7d535a
  switch (mainp->context_state) {
kusano 7d535a
  case CTX_POSTPONED_ROW:
kusano 7d535a
    /* Call postprocessor using previously set pointers for postponed row */
kusano 7d535a
    (*cinfo->post->post_process_data) (cinfo, mainp->xbuffer[mainp->whichptr],
kusano 7d535a
			&mainp->rowgroup_ctr, mainp->rowgroups_avail,
kusano 7d535a
			output_buf, out_row_ctr, out_rows_avail);
kusano 7d535a
    if (mainp->rowgroup_ctr < mainp->rowgroups_avail)
kusano 7d535a
      return;			/* Need to suspend */
kusano 7d535a
    mainp->context_state = CTX_PREPARE_FOR_IMCU;
kusano 7d535a
    if (*out_row_ctr >= out_rows_avail)
kusano 7d535a
      return;			/* Postprocessor exactly filled output buf */
kusano 7d535a
    /*FALLTHROUGH*/
kusano 7d535a
  case CTX_PREPARE_FOR_IMCU:
kusano 7d535a
    /* Prepare to process first M-1 row groups of this iMCU row */
kusano 7d535a
    mainp->rowgroup_ctr = 0;
kusano 7d535a
    mainp->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_v_scaled_size - 1);
kusano 7d535a
    /* Check for bottom of image: if so, tweak pointers to "duplicate"
kusano 7d535a
     * the last sample row, and adjust rowgroups_avail to ignore padding rows.
kusano 7d535a
     */
kusano 7d535a
    if (mainp->iMCU_row_ctr == cinfo->total_iMCU_rows)
kusano 7d535a
      set_bottom_pointers(cinfo);
kusano 7d535a
    mainp->context_state = CTX_PROCESS_IMCU;
kusano 7d535a
    /*FALLTHROUGH*/
kusano 7d535a
  case CTX_PROCESS_IMCU:
kusano 7d535a
    /* Call postprocessor using previously set pointers */
kusano 7d535a
    (*cinfo->post->post_process_data) (cinfo, mainp->xbuffer[mainp->whichptr],
kusano 7d535a
			&mainp->rowgroup_ctr, mainp->rowgroups_avail,
kusano 7d535a
			output_buf, out_row_ctr, out_rows_avail);
kusano 7d535a
    if (mainp->rowgroup_ctr < mainp->rowgroups_avail)
kusano 7d535a
      return;			/* Need to suspend */
kusano 7d535a
    /* After the first iMCU, change wraparound pointers to normal state */
kusano 7d535a
    if (mainp->iMCU_row_ctr == 1)
kusano 7d535a
      set_wraparound_pointers(cinfo);
kusano 7d535a
    /* Prepare to load new iMCU row using other xbuffer list */
kusano 7d535a
    mainp->whichptr ^= 1;	/* 0=>1 or 1=>0 */
kusano 7d535a
    mainp->buffer_full = FALSE;
kusano 7d535a
    /* Still need to process last row group of this iMCU row, */
kusano 7d535a
    /* which is saved at index M+1 of the other xbuffer */
kusano 7d535a
    mainp->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_v_scaled_size + 1);
kusano 7d535a
    mainp->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_v_scaled_size + 2);
kusano 7d535a
    mainp->context_state = CTX_POSTPONED_ROW;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Process some data.
kusano 7d535a
 * Final pass of two-pass quantization: just call the postprocessor.
kusano 7d535a
 * Source data will be the postprocessor controller's internal buffer.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#ifdef QUANT_2PASS_SUPPORTED
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
process_data_crank_post (j_decompress_ptr cinfo,
kusano 7d535a
			 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
kusano 7d535a
			 JDIMENSION out_rows_avail)
kusano 7d535a
{
kusano 7d535a
  (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
kusano 7d535a
				     (JDIMENSION *) NULL, (JDIMENSION) 0,
kusano 7d535a
				     output_buf, out_row_ctr, out_rows_avail);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* QUANT_2PASS_SUPPORTED */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize main buffer controller.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp;
kusano 7d535a
  int ci, rgroup, ngroups;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  mainp = (my_main_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(my_main_controller));
kusano 7d535a
  cinfo->main = &mainp->pub;
kusano 7d535a
  mainp->pub.start_pass = start_pass_main;
kusano 7d535a
kusano 7d535a
  if (need_full_buffer)		/* shouldn't happen */
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
kusano 7d535a
  /* Allocate the workspace.
kusano 7d535a
   * ngroups is the number of row groups we need.
kusano 7d535a
   */
kusano 7d535a
  if (cinfo->upsample->need_context_rows) {
kusano 7d535a
    if (cinfo->min_DCT_v_scaled_size < 2) /* unsupported, see comments above */
kusano 7d535a
      ERREXIT(cinfo, JERR_NOTIMPL);
kusano 7d535a
    alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
kusano 7d535a
    ngroups = cinfo->min_DCT_v_scaled_size + 2;
kusano 7d535a
  } else {
kusano 7d535a
    ngroups = cinfo->min_DCT_v_scaled_size;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
kusano 7d535a
      cinfo->min_DCT_v_scaled_size; /* height of a row group of component */
kusano 7d535a
    mainp->buffer[ci] = (*cinfo->mem->alloc_sarray)
kusano 7d535a
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
       compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
kusano 7d535a
       (JDIMENSION) (rgroup * ngroups));
kusano 7d535a
  }
kusano 7d535a
}