Blame gtkmm-osx/jpeg-6b/jdpostct.c

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