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