kusano 7d535a
/*
kusano 7d535a
 * jcmainct.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1996, Thomas G. Lane.
kusano 7d535a
 * Modified 2003-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 compression.
kusano 7d535a
 * The main buffer lies between the pre-processor and the JPEG
kusano 7d535a
 * compressor proper; it holds downsampled data in the JPEG colorspace.
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
/* Note: currently, there is no operating mode in which a full-image buffer
kusano 7d535a
 * is needed at this step.  If there were, that mode could not be used with
kusano 7d535a
 * "raw data" input, since this module is bypassed in that case.  However,
kusano 7d535a
 * we've left the code here for possible use in special applications.
kusano 7d535a
 */
kusano 7d535a
#undef FULL_MAIN_BUFFER_SUPPORTED
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Private buffer controller object */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_c_main_controller pub; /* public fields */
kusano 7d535a
kusano 7d535a
  JDIMENSION cur_iMCU_row;	/* number of current iMCU row */
kusano 7d535a
  JDIMENSION rowgroup_ctr;	/* counts row groups received in iMCU row */
kusano 7d535a
  boolean suspended;		/* remember if we suspended output */
kusano 7d535a
  J_BUF_MODE pass_mode;		/* current operating mode */
kusano 7d535a
kusano 7d535a
  /* If using just a strip buffer, this points to the entire set of buffers
kusano 7d535a
   * (we allocate one for each component).  In the full-image case, this
kusano 7d535a
   * points to the currently accessible strips of the virtual arrays.
kusano 7d535a
   */
kusano 7d535a
  JSAMPARRAY buffer[MAX_COMPONENTS];
kusano 7d535a
kusano 7d535a
#ifdef FULL_MAIN_BUFFER_SUPPORTED
kusano 7d535a
  /* If using full-image storage, this array holds pointers to virtual-array
kusano 7d535a
   * control blocks for each component.  Unused if not full-image storage.
kusano 7d535a
   */
kusano 7d535a
  jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
kusano 7d535a
#endif
kusano 7d535a
} my_main_controller;
kusano 7d535a
kusano 7d535a
typedef my_main_controller * my_main_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Forward declarations */
kusano 7d535a
METHODDEF(void) process_data_simple_main
kusano 7d535a
	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
kusano 7d535a
	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
kusano 7d535a
#ifdef FULL_MAIN_BUFFER_SUPPORTED
kusano 7d535a
METHODDEF(void) process_data_buffer_main
kusano 7d535a
	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
kusano 7d535a
	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
kusano 7d535a
#endif
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_compress_ptr cinfo, J_BUF_MODE pass_mode)
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
kusano 7d535a
kusano 7d535a
  /* Do nothing in raw-data mode. */
kusano 7d535a
  if (cinfo->raw_data_in)
kusano 7d535a
    return;
kusano 7d535a
kusano 7d535a
  mainp->cur_iMCU_row = 0;	/* initialize counters */
kusano 7d535a
  mainp->rowgroup_ctr = 0;
kusano 7d535a
  mainp->suspended = FALSE;
kusano 7d535a
  mainp->pass_mode = pass_mode;	/* save mode for use by process_data */
kusano 7d535a
kusano 7d535a
  switch (pass_mode) {
kusano 7d535a
  case JBUF_PASS_THRU:
kusano 7d535a
#ifdef FULL_MAIN_BUFFER_SUPPORTED
kusano 7d535a
    if (mainp->whole_image[0] != NULL)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
#endif
kusano 7d535a
    mainp->pub.process_data = process_data_simple_main;
kusano 7d535a
    break;
kusano 7d535a
#ifdef FULL_MAIN_BUFFER_SUPPORTED
kusano 7d535a
  case JBUF_SAVE_SOURCE:
kusano 7d535a
  case JBUF_CRANK_DEST:
kusano 7d535a
  case JBUF_SAVE_AND_PASS:
kusano 7d535a
    if (mainp->whole_image[0] == NULL)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
    mainp->pub.process_data = process_data_buffer_main;
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 routine handles the simple pass-through mode,
kusano 7d535a
 * where we have only a strip buffer.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
process_data_simple_main (j_compress_ptr cinfo,
kusano 7d535a
			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
kusano 7d535a
			  JDIMENSION in_rows_avail)
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
kusano 7d535a
kusano 7d535a
  while (mainp->cur_iMCU_row < cinfo->total_iMCU_rows) {
kusano 7d535a
    /* Read input data if we haven't filled the main buffer yet */
kusano 7d535a
    if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
kusano 7d535a
      (*cinfo->prep->pre_process_data) (cinfo,
kusano 7d535a
					input_buf, in_row_ctr, in_rows_avail,
kusano 7d535a
					mainp->buffer, &mainp->rowgroup_ctr,
kusano 7d535a
					(JDIMENSION) cinfo->min_DCT_v_scaled_size);
kusano 7d535a
kusano 7d535a
    /* If we don't have a full iMCU row buffered, return to application for
kusano 7d535a
     * more data.  Note that preprocessor will always pad to fill the iMCU row
kusano 7d535a
     * at the bottom of the image.
kusano 7d535a
     */
kusano 7d535a
    if (mainp->rowgroup_ctr != (JDIMENSION) cinfo->min_DCT_v_scaled_size)
kusano 7d535a
      return;
kusano 7d535a
kusano 7d535a
    /* Send the completed row to the compressor */
kusano 7d535a
    if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
kusano 7d535a
      /* If compressor did not consume the whole row, then we must need to
kusano 7d535a
       * suspend processing and return to the application.  In this situation
kusano 7d535a
       * we pretend we didn't yet consume the last input row; otherwise, if
kusano 7d535a
       * it happened to be the last row of the image, the application would
kusano 7d535a
       * think we were done.
kusano 7d535a
       */
kusano 7d535a
      if (! mainp->suspended) {
kusano 7d535a
	(*in_row_ctr)--;
kusano 7d535a
	mainp->suspended = TRUE;
kusano 7d535a
      }
kusano 7d535a
      return;
kusano 7d535a
    }
kusano 7d535a
    /* We did finish the row.  Undo our little suspension hack if a previous
kusano 7d535a
     * call suspended; then mark the main buffer empty.
kusano 7d535a
     */
kusano 7d535a
    if (mainp->suspended) {
kusano 7d535a
      (*in_row_ctr)++;
kusano 7d535a
      mainp->suspended = FALSE;
kusano 7d535a
    }
kusano 7d535a
    mainp->rowgroup_ctr = 0;
kusano 7d535a
    mainp->cur_iMCU_row++;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
#ifdef FULL_MAIN_BUFFER_SUPPORTED
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Process some data.
kusano 7d535a
 * This routine handles all of the modes that use a full-size buffer.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
process_data_buffer_main (j_compress_ptr cinfo,
kusano 7d535a
			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
kusano 7d535a
			  JDIMENSION in_rows_avail)
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
kusano 7d535a
  int ci;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  boolean writing = (mainp->pass_mode != JBUF_CRANK_DEST);
kusano 7d535a
kusano 7d535a
  while (mainp->cur_iMCU_row < cinfo->total_iMCU_rows) {
kusano 7d535a
    /* Realign the virtual buffers if at the start of an iMCU row. */
kusano 7d535a
    if (mainp->rowgroup_ctr == 0) {
kusano 7d535a
      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
	   ci++, compptr++) {
kusano 7d535a
	mainp->buffer[ci] = (*cinfo->mem->access_virt_sarray)
kusano 7d535a
	  ((j_common_ptr) cinfo, mainp->whole_image[ci], mainp->cur_iMCU_row *
kusano 7d535a
	   ((JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size)),
kusano 7d535a
	   (JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size),
kusano 7d535a
	   writing);
kusano 7d535a
      }
kusano 7d535a
      /* In a read pass, pretend we just read some source data. */
kusano 7d535a
      if (! writing) {
kusano 7d535a
	*in_row_ctr += (JDIMENSION)
kusano 7d535a
	  (cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size);
kusano 7d535a
	mainp->rowgroup_ctr = (JDIMENSION) cinfo->min_DCT_v_scaled_size;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    /* If a write pass, read input data until the current iMCU row is full. */
kusano 7d535a
    /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
kusano 7d535a
    if (writing) {
kusano 7d535a
      (*cinfo->prep->pre_process_data) (cinfo,
kusano 7d535a
					input_buf, in_row_ctr, in_rows_avail,
kusano 7d535a
					mainp->buffer, &mainp->rowgroup_ctr,
kusano 7d535a
					(JDIMENSION) cinfo->min_DCT_v_scaled_size);
kusano 7d535a
      /* Return to application if we need more data to fill the iMCU row. */
kusano 7d535a
      if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
kusano 7d535a
	return;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    /* Emit data, unless this is a sink-only pass. */
kusano 7d535a
    if (mainp->pass_mode != JBUF_SAVE_SOURCE) {
kusano 7d535a
      if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
kusano 7d535a
	/* If compressor did not consume the whole row, then we must need to
kusano 7d535a
	 * suspend processing and return to the application.  In this situation
kusano 7d535a
	 * we pretend we didn't yet consume the last input row; otherwise, if
kusano 7d535a
	 * it happened to be the last row of the image, the application would
kusano 7d535a
	 * think we were done.
kusano 7d535a
	 */
kusano 7d535a
	if (! mainp->suspended) {
kusano 7d535a
	  (*in_row_ctr)--;
kusano 7d535a
	  mainp->suspended = TRUE;
kusano 7d535a
	}
kusano 7d535a
	return;
kusano 7d535a
      }
kusano 7d535a
      /* We did finish the row.  Undo our little suspension hack if a previous
kusano 7d535a
       * call suspended; then mark the main buffer empty.
kusano 7d535a
       */
kusano 7d535a
      if (mainp->suspended) {
kusano 7d535a
	(*in_row_ctr)++;
kusano 7d535a
	mainp->suspended = FALSE;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    /* If get here, we are done with this iMCU row.  Mark buffer empty. */
kusano 7d535a
    mainp->rowgroup_ctr = 0;
kusano 7d535a
    mainp->cur_iMCU_row++;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* FULL_MAIN_BUFFER_SUPPORTED */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize main buffer controller.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
kusano 7d535a
{
kusano 7d535a
  my_main_ptr mainp;
kusano 7d535a
  int ci;
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
  /* We don't need to create a buffer in raw-data mode. */
kusano 7d535a
  if (cinfo->raw_data_in)
kusano 7d535a
    return;
kusano 7d535a
kusano 7d535a
  /* Create the buffer.  It holds downsampled data, so each component
kusano 7d535a
   * may be of a different size.
kusano 7d535a
   */
kusano 7d535a
  if (need_full_buffer) {
kusano 7d535a
#ifdef FULL_MAIN_BUFFER_SUPPORTED
kusano 7d535a
    /* Allocate a full-image virtual array for each component */
kusano 7d535a
    /* Note we pad the bottom to a multiple of the iMCU height */
kusano 7d535a
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
	 ci++, compptr++) {
kusano 7d535a
      mainp->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
kusano 7d535a
	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
kusano 7d535a
	 compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
kusano 7d535a
	 ((JDIMENSION) jround_up((long) compptr->height_in_blocks,
kusano 7d535a
				 (long) compptr->v_samp_factor)) *
kusano 7d535a
	 ((JDIMENSION) cinfo->min_DCT_v_scaled_size),
kusano 7d535a
	 (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
kusano 7d535a
    }
kusano 7d535a
#else
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
#endif
kusano 7d535a
  } else {
kusano 7d535a
#ifdef FULL_MAIN_BUFFER_SUPPORTED
kusano 7d535a
    mainp->whole_image[0] = NULL; /* flag for no virtual arrays */
kusano 7d535a
#endif
kusano 7d535a
    /* Allocate a strip buffer for each component */
kusano 7d535a
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
	 ci++, compptr++) {
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) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}