kusano 7d535a
/*
kusano 7d535a
 * jdpostct.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1996, Thomas G. Lane.
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 decompression postprocessing controller.
kusano 7d535a
 * This controller manages the upsampling, color conversion, and color
kusano 7d535a
 * quantization/reduction steps; specifically, it controls the buffering
kusano 7d535a
 * between upsample/color conversion and color quantization/reduction.
kusano 7d535a
 *
kusano 7d535a
 * If no color quantization/reduction is required, then this module has no
kusano 7d535a
 * work to do, and it just hands off to the upsample/color conversion code.
kusano 7d535a
 * An integrated upsample/convert/quantize process would replace this module
kusano 7d535a
 * entirely.
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
/* Private buffer controller object */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_d_post_controller pub; /* public fields */
kusano 7d535a
kusano 7d535a
  /* Color quantization source buffer: this holds output data from
kusano 7d535a
   * the upsample/color conversion step to be passed to the quantizer.
kusano 7d535a
   * For two-pass color quantization, we need a full-image buffer;
kusano 7d535a
   * for one-pass operation, a strip buffer is sufficient.
kusano 7d535a
   */
kusano 7d535a
  jvirt_sarray_ptr whole_image;	/* virtual array, or NULL if one-pass */
kusano 7d535a
  JSAMPARRAY buffer;		/* strip buffer, or current strip of virtual */
kusano 7d535a
  JDIMENSION strip_height;	/* buffer size in rows */
kusano 7d535a
  /* for two-pass mode only: */
kusano 7d535a
  JDIMENSION starting_row;	/* row # of first row in current strip */
kusano 7d535a
  JDIMENSION next_row;		/* index of next row to fill/empty in strip */
kusano 7d535a
} my_post_controller;
kusano 7d535a
kusano 7d535a
typedef my_post_controller * my_post_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Forward declarations */
kusano 7d535a
METHODDEF(void) post_process_1pass
kusano 7d535a
	JPP((j_decompress_ptr cinfo,
kusano 7d535a
	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
kusano 7d535a
	     JDIMENSION in_row_groups_avail,
kusano 7d535a
	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
kusano 7d535a
	     JDIMENSION out_rows_avail));
kusano 7d535a
#ifdef QUANT_2PASS_SUPPORTED
kusano 7d535a
METHODDEF(void) post_process_prepass
kusano 7d535a
	JPP((j_decompress_ptr cinfo,
kusano 7d535a
	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
kusano 7d535a
	     JDIMENSION in_row_groups_avail,
kusano 7d535a
	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
kusano 7d535a
	     JDIMENSION out_rows_avail));
kusano 7d535a
METHODDEF(void) post_process_2pass
kusano 7d535a
	JPP((j_decompress_ptr cinfo,
kusano 7d535a
	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
kusano 7d535a
	     JDIMENSION in_row_groups_avail,
kusano 7d535a
	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
kusano 7d535a
	     JDIMENSION out_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_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
kusano 7d535a
{
kusano 7d535a
  my_post_ptr post = (my_post_ptr) cinfo->post;
kusano 7d535a
kusano 7d535a
  switch (pass_mode) {
kusano 7d535a
  case JBUF_PASS_THRU:
kusano 7d535a
    if (cinfo->quantize_colors) {
kusano 7d535a
      /* Single-pass processing with color quantization. */
kusano 7d535a
      post->pub.post_process_data = post_process_1pass;
kusano 7d535a
      /* We could be doing buffered-image output before starting a 2-pass
kusano 7d535a
       * color quantization; in that case, jinit_d_post_controller did not
kusano 7d535a
       * allocate a strip buffer.  Use the virtual-array buffer as workspace.
kusano 7d535a
       */
kusano 7d535a
      if (post->buffer == NULL) {
kusano 7d535a
	post->buffer = (*cinfo->mem->access_virt_sarray)
kusano 7d535a
	  ((j_common_ptr) cinfo, post->whole_image,
kusano 7d535a
	   (JDIMENSION) 0, post->strip_height, TRUE);
kusano 7d535a
      }
kusano 7d535a
    } else {
kusano 7d535a
      /* For single-pass processing without color quantization,
kusano 7d535a
       * I have no work to do; just call the upsampler directly.
kusano 7d535a
       */
kusano 7d535a
      post->pub.post_process_data = cinfo->upsample->upsample;
kusano 7d535a
    }
kusano 7d535a
    break;
kusano 7d535a
#ifdef QUANT_2PASS_SUPPORTED
kusano 7d535a
  case JBUF_SAVE_AND_PASS:
kusano 7d535a
    /* First pass of 2-pass quantization */
kusano 7d535a
    if (post->whole_image == NULL)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
    post->pub.post_process_data = post_process_prepass;
kusano 7d535a
    break;
kusano 7d535a
  case JBUF_CRANK_DEST:
kusano 7d535a
    /* Second pass of 2-pass quantization */
kusano 7d535a
    if (post->whole_image == NULL)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
    post->pub.post_process_data = post_process_2pass;
kusano 7d535a
    break;
kusano 7d535a
#endif /* QUANT_2PASS_SUPPORTED */
kusano 7d535a
  default:
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
  post->starting_row = post->next_row = 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Process some data in the one-pass (strip buffer) case.
kusano 7d535a
 * This is used for color precision reduction as well as one-pass quantization.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
post_process_1pass (j_decompress_ptr cinfo,
kusano 7d535a
		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
kusano 7d535a
		    JDIMENSION in_row_groups_avail,
kusano 7d535a
		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
kusano 7d535a
		    JDIMENSION out_rows_avail)
kusano 7d535a
{
kusano 7d535a
  my_post_ptr post = (my_post_ptr) cinfo->post;
kusano 7d535a
  JDIMENSION num_rows, max_rows;
kusano 7d535a
kusano 7d535a
  /* Fill the buffer, but not more than what we can dump out in one go. */
kusano 7d535a
  /* Note we rely on the upsampler to detect bottom of image. */
kusano 7d535a
  max_rows = out_rows_avail - *out_row_ctr;
kusano 7d535a
  if (max_rows > post->strip_height)
kusano 7d535a
    max_rows = post->strip_height;
kusano 7d535a
  num_rows = 0;
kusano 7d535a
  (*cinfo->upsample->upsample) (cinfo,
kusano 7d535a
		input_buf, in_row_group_ctr, in_row_groups_avail,
kusano 7d535a
		post->buffer, &num_rows, max_rows);
kusano 7d535a
  /* Quantize and emit data. */
kusano 7d535a
  (*cinfo->cquantize->color_quantize) (cinfo,
kusano 7d535a
		post->buffer, output_buf + *out_row_ctr, (int) num_rows);
kusano 7d535a
  *out_row_ctr += num_rows;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
#ifdef QUANT_2PASS_SUPPORTED
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Process some data in the first pass of 2-pass quantization.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
post_process_prepass (j_decompress_ptr cinfo,
kusano 7d535a
		      JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
kusano 7d535a
		      JDIMENSION in_row_groups_avail,
kusano 7d535a
		      JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
kusano 7d535a
		      JDIMENSION out_rows_avail)
kusano 7d535a
{
kusano 7d535a
  my_post_ptr post = (my_post_ptr) cinfo->post;
kusano 7d535a
  JDIMENSION old_next_row, num_rows;
kusano 7d535a
kusano 7d535a
  /* Reposition virtual buffer if at start of strip. */
kusano 7d535a
  if (post->next_row == 0) {
kusano 7d535a
    post->buffer = (*cinfo->mem->access_virt_sarray)
kusano 7d535a
	((j_common_ptr) cinfo, post->whole_image,
kusano 7d535a
	 post->starting_row, post->strip_height, TRUE);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Upsample some data (up to a strip height's worth). */
kusano 7d535a
  old_next_row = post->next_row;
kusano 7d535a
  (*cinfo->upsample->upsample) (cinfo,
kusano 7d535a
		input_buf, in_row_group_ctr, in_row_groups_avail,
kusano 7d535a
		post->buffer, &post->next_row, post->strip_height);
kusano 7d535a
kusano 7d535a
  /* Allow quantizer to scan new data.  No data is emitted, */
kusano 7d535a
  /* but we advance out_row_ctr so outer loop can tell when we're done. */
kusano 7d535a
  if (post->next_row > old_next_row) {
kusano 7d535a
    num_rows = post->next_row - old_next_row;
kusano 7d535a
    (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
kusano 7d535a
					 (JSAMPARRAY) NULL, (int) num_rows);
kusano 7d535a
    *out_row_ctr += num_rows;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Advance if we filled the strip. */
kusano 7d535a
  if (post->next_row >= post->strip_height) {
kusano 7d535a
    post->starting_row += post->strip_height;
kusano 7d535a
    post->next_row = 0;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Process some data in the second pass of 2-pass quantization.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
post_process_2pass (j_decompress_ptr cinfo,
kusano 7d535a
		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
kusano 7d535a
		    JDIMENSION in_row_groups_avail,
kusano 7d535a
		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
kusano 7d535a
		    JDIMENSION out_rows_avail)
kusano 7d535a
{
kusano 7d535a
  my_post_ptr post = (my_post_ptr) cinfo->post;
kusano 7d535a
  JDIMENSION num_rows, max_rows;
kusano 7d535a
kusano 7d535a
  /* Reposition virtual buffer if at start of strip. */
kusano 7d535a
  if (post->next_row == 0) {
kusano 7d535a
    post->buffer = (*cinfo->mem->access_virt_sarray)
kusano 7d535a
	((j_common_ptr) cinfo, post->whole_image,
kusano 7d535a
	 post->starting_row, post->strip_height, FALSE);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Determine number of rows to emit. */
kusano 7d535a
  num_rows = post->strip_height - post->next_row; /* available in strip */
kusano 7d535a
  max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
kusano 7d535a
  if (num_rows > max_rows)
kusano 7d535a
    num_rows = max_rows;
kusano 7d535a
  /* We have to check bottom of image here, can't depend on upsampler. */
kusano 7d535a
  max_rows = cinfo->output_height - post->starting_row;
kusano 7d535a
  if (num_rows > max_rows)
kusano 7d535a
    num_rows = max_rows;
kusano 7d535a
kusano 7d535a
  /* Quantize and emit data. */
kusano 7d535a
  (*cinfo->cquantize->color_quantize) (cinfo,
kusano 7d535a
		post->buffer + post->next_row, output_buf + *out_row_ctr,
kusano 7d535a
		(int) num_rows);
kusano 7d535a
  *out_row_ctr += num_rows;
kusano 7d535a
kusano 7d535a
  /* Advance if we filled the strip. */
kusano 7d535a
  post->next_row += num_rows;
kusano 7d535a
  if (post->next_row >= post->strip_height) {
kusano 7d535a
    post->starting_row += post->strip_height;
kusano 7d535a
    post->next_row = 0;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* QUANT_2PASS_SUPPORTED */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize postprocessing controller.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
kusano 7d535a
{
kusano 7d535a
  my_post_ptr post;
kusano 7d535a
kusano 7d535a
  post = (my_post_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(my_post_controller));
kusano 7d535a
  cinfo->post = (struct jpeg_d_post_controller *) post;
kusano 7d535a
  post->pub.start_pass = start_pass_dpost;
kusano 7d535a
  post->whole_image = NULL;	/* flag for no virtual arrays */
kusano 7d535a
  post->buffer = NULL;		/* flag for no strip buffer */
kusano 7d535a
kusano 7d535a
  /* Create the quantization buffer, if needed */
kusano 7d535a
  if (cinfo->quantize_colors) {
kusano 7d535a
    /* The buffer strip height is max_v_samp_factor, which is typically
kusano 7d535a
     * an efficient number of rows for upsampling to return.
kusano 7d535a
     * (In the presence of output rescaling, we might want to be smarter?)
kusano 7d535a
     */
kusano 7d535a
    post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
kusano 7d535a
    if (need_full_buffer) {
kusano 7d535a
      /* Two-pass color quantization: need full-image storage. */
kusano 7d535a
      /* We round up the number of rows to a multiple of the strip height. */
kusano 7d535a
#ifdef QUANT_2PASS_SUPPORTED
kusano 7d535a
      post->whole_image = (*cinfo->mem->request_virt_sarray)
kusano 7d535a
	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
kusano 7d535a
	 cinfo->output_width * cinfo->out_color_components,
kusano 7d535a
	 (JDIMENSION) jround_up((long) cinfo->output_height,
kusano 7d535a
				(long) post->strip_height),
kusano 7d535a
	 post->strip_height);
kusano 7d535a
#else
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
#endif /* QUANT_2PASS_SUPPORTED */
kusano 7d535a
    } else {
kusano 7d535a
      /* One-pass color quantization: just make a strip buffer. */
kusano 7d535a
      post->buffer = (*cinfo->mem->alloc_sarray)
kusano 7d535a
	((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
	 cinfo->output_width * cinfo->out_color_components,
kusano 7d535a
	 post->strip_height);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}