Path: blob/master/src/java.desktop/share/native/libjavajpeg/jdpostct.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jdpostct.c6*7* Copyright (C) 1994-1996, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains the decompression postprocessing controller.12* This controller manages the upsampling, color conversion, and color13* quantization/reduction steps; specifically, it controls the buffering14* between upsample/color conversion and color quantization/reduction.15*16* If no color quantization/reduction is required, then this module has no17* work to do, and it just hands off to the upsample/color conversion code.18* An integrated upsample/convert/quantize process would replace this module19* entirely.20*/2122#define JPEG_INTERNALS23#include "jinclude.h"24#include "jpeglib.h"252627/* Private buffer controller object */2829typedef struct {30struct jpeg_d_post_controller pub; /* public fields */3132/* Color quantization source buffer: this holds output data from33* the upsample/color conversion step to be passed to the quantizer.34* For two-pass color quantization, we need a full-image buffer;35* for one-pass operation, a strip buffer is sufficient.36*/37jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */38JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */39JDIMENSION strip_height; /* buffer size in rows */40/* for two-pass mode only: */41JDIMENSION starting_row; /* row # of first row in current strip */42JDIMENSION next_row; /* index of next row to fill/empty in strip */43} my_post_controller;4445typedef my_post_controller * my_post_ptr;464748/* Forward declarations */49METHODDEF(void) post_process_1pass50JPP((j_decompress_ptr cinfo,51JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,52JDIMENSION in_row_groups_avail,53JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,54JDIMENSION out_rows_avail));55#ifdef QUANT_2PASS_SUPPORTED56METHODDEF(void) post_process_prepass57JPP((j_decompress_ptr cinfo,58JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,59JDIMENSION in_row_groups_avail,60JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,61JDIMENSION out_rows_avail));62METHODDEF(void) post_process_2pass63JPP((j_decompress_ptr cinfo,64JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,65JDIMENSION in_row_groups_avail,66JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,67JDIMENSION out_rows_avail));68#endif697071/*72* Initialize for a processing pass.73*/7475METHODDEF(void)76start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)77{78my_post_ptr post = (my_post_ptr) cinfo->post;7980switch (pass_mode) {81case JBUF_PASS_THRU:82if (cinfo->quantize_colors) {83/* Single-pass processing with color quantization. */84post->pub.post_process_data = post_process_1pass;85/* We could be doing buffered-image output before starting a 2-pass86* color quantization; in that case, jinit_d_post_controller did not87* allocate a strip buffer. Use the virtual-array buffer as workspace.88*/89if (post->buffer == NULL) {90post->buffer = (*cinfo->mem->access_virt_sarray)91((j_common_ptr) cinfo, post->whole_image,92(JDIMENSION) 0, post->strip_height, TRUE);93}94} else {95/* For single-pass processing without color quantization,96* I have no work to do; just call the upsampler directly.97*/98post->pub.post_process_data = cinfo->upsample->upsample;99}100break;101#ifdef QUANT_2PASS_SUPPORTED102case JBUF_SAVE_AND_PASS:103/* First pass of 2-pass quantization */104if (post->whole_image == NULL)105ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);106post->pub.post_process_data = post_process_prepass;107break;108case JBUF_CRANK_DEST:109/* Second pass of 2-pass quantization */110if (post->whole_image == NULL)111ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);112post->pub.post_process_data = post_process_2pass;113break;114#endif /* QUANT_2PASS_SUPPORTED */115default:116ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);117break;118}119post->starting_row = post->next_row = 0;120}121122123/*124* Process some data in the one-pass (strip buffer) case.125* This is used for color precision reduction as well as one-pass quantization.126*/127128METHODDEF(void)129post_process_1pass (j_decompress_ptr cinfo,130JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,131JDIMENSION in_row_groups_avail,132JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,133JDIMENSION out_rows_avail)134{135my_post_ptr post = (my_post_ptr) cinfo->post;136JDIMENSION num_rows, max_rows;137138/* Fill the buffer, but not more than what we can dump out in one go. */139/* Note we rely on the upsampler to detect bottom of image. */140max_rows = out_rows_avail - *out_row_ctr;141if (max_rows > post->strip_height)142max_rows = post->strip_height;143num_rows = 0;144(*cinfo->upsample->upsample) (cinfo,145input_buf, in_row_group_ctr, in_row_groups_avail,146post->buffer, &num_rows, max_rows);147/* Quantize and emit data. */148(*cinfo->cquantize->color_quantize) (cinfo,149post->buffer, output_buf + *out_row_ctr, (int) num_rows);150*out_row_ctr += num_rows;151}152153154#ifdef QUANT_2PASS_SUPPORTED155156/*157* Process some data in the first pass of 2-pass quantization.158*/159160METHODDEF(void)161post_process_prepass (j_decompress_ptr cinfo,162JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,163JDIMENSION in_row_groups_avail,164JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,165JDIMENSION out_rows_avail)166{167my_post_ptr post = (my_post_ptr) cinfo->post;168JDIMENSION old_next_row, num_rows;169170/* Reposition virtual buffer if at start of strip. */171if (post->next_row == 0) {172post->buffer = (*cinfo->mem->access_virt_sarray)173((j_common_ptr) cinfo, post->whole_image,174post->starting_row, post->strip_height, TRUE);175}176177/* Upsample some data (up to a strip height's worth). */178old_next_row = post->next_row;179(*cinfo->upsample->upsample) (cinfo,180input_buf, in_row_group_ctr, in_row_groups_avail,181post->buffer, &post->next_row, post->strip_height);182183/* Allow quantizer to scan new data. No data is emitted, */184/* but we advance out_row_ctr so outer loop can tell when we're done. */185if (post->next_row > old_next_row) {186num_rows = post->next_row - old_next_row;187(*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,188(JSAMPARRAY) NULL, (int) num_rows);189*out_row_ctr += num_rows;190}191192/* Advance if we filled the strip. */193if (post->next_row >= post->strip_height) {194post->starting_row += post->strip_height;195post->next_row = 0;196}197}198199200/*201* Process some data in the second pass of 2-pass quantization.202*/203204METHODDEF(void)205post_process_2pass (j_decompress_ptr cinfo,206JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,207JDIMENSION in_row_groups_avail,208JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,209JDIMENSION out_rows_avail)210{211my_post_ptr post = (my_post_ptr) cinfo->post;212JDIMENSION num_rows, max_rows;213214/* Reposition virtual buffer if at start of strip. */215if (post->next_row == 0) {216post->buffer = (*cinfo->mem->access_virt_sarray)217((j_common_ptr) cinfo, post->whole_image,218post->starting_row, post->strip_height, FALSE);219}220221/* Determine number of rows to emit. */222num_rows = post->strip_height - post->next_row; /* available in strip */223max_rows = out_rows_avail - *out_row_ctr; /* available in output area */224if (num_rows > max_rows)225num_rows = max_rows;226/* We have to check bottom of image here, can't depend on upsampler. */227max_rows = cinfo->output_height - post->starting_row;228if (num_rows > max_rows)229num_rows = max_rows;230231/* Quantize and emit data. */232(*cinfo->cquantize->color_quantize) (cinfo,233post->buffer + post->next_row, output_buf + *out_row_ctr,234(int) num_rows);235*out_row_ctr += num_rows;236237/* Advance if we filled the strip. */238post->next_row += num_rows;239if (post->next_row >= post->strip_height) {240post->starting_row += post->strip_height;241post->next_row = 0;242}243}244245#endif /* QUANT_2PASS_SUPPORTED */246247248/*249* Initialize postprocessing controller.250*/251252GLOBAL(void)253jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)254{255my_post_ptr post;256257post = (my_post_ptr)258(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,259SIZEOF(my_post_controller));260cinfo->post = (struct jpeg_d_post_controller *) post;261post->pub.start_pass = start_pass_dpost;262post->whole_image = NULL; /* flag for no virtual arrays */263post->buffer = NULL; /* flag for no strip buffer */264265/* Create the quantization buffer, if needed */266if (cinfo->quantize_colors) {267/* The buffer strip height is max_v_samp_factor, which is typically268* an efficient number of rows for upsampling to return.269* (In the presence of output rescaling, we might want to be smarter?)270*/271post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;272if (need_full_buffer) {273/* Two-pass color quantization: need full-image storage. */274/* We round up the number of rows to a multiple of the strip height. */275#ifdef QUANT_2PASS_SUPPORTED276post->whole_image = (*cinfo->mem->request_virt_sarray)277((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,278cinfo->output_width * cinfo->out_color_components,279(JDIMENSION) jround_up((long) cinfo->output_height,280(long) post->strip_height),281post->strip_height);282#else283ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);284#endif /* QUANT_2PASS_SUPPORTED */285} else {286/* One-pass color quantization: just make a strip buffer. */287post->buffer = (*cinfo->mem->alloc_sarray)288((j_common_ptr) cinfo, JPOOL_IMAGE,289cinfo->output_width * cinfo->out_color_components,290post->strip_height);291}292}293}294295296