Path: blob/master/3rdparty/libjpeg-turbo/src/jdpostct.c
16337 views
/*1* jdpostct.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1996, Thomas G. Lane.5* It was modified by The libjpeg-turbo Project to include only code relevant6* to libjpeg-turbo.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains the decompression postprocessing controller.11* This controller manages the upsampling, color conversion, and color12* quantization/reduction steps; specifically, it controls the buffering13* between upsample/color conversion and color quantization/reduction.14*15* If no color quantization/reduction is required, then this module has no16* work to do, and it just hands off to the upsample/color conversion code.17* An integrated upsample/convert/quantize process would replace this module18* entirely.19*/2021#define JPEG_INTERNALS22#include "jinclude.h"23#include "jpeglib.h"242526/* Private buffer controller object */2728typedef struct {29struct jpeg_d_post_controller pub; /* public fields */3031/* Color quantization source buffer: this holds output data from32* the upsample/color conversion step to be passed to the quantizer.33* For two-pass color quantization, we need a full-image buffer;34* for one-pass operation, a strip buffer is sufficient.35*/36jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */37JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */38JDIMENSION strip_height; /* buffer size in rows */39/* for two-pass mode only: */40JDIMENSION starting_row; /* row # of first row in current strip */41JDIMENSION next_row; /* index of next row to fill/empty in strip */42} my_post_controller;4344typedef my_post_controller *my_post_ptr;454647/* Forward declarations */48METHODDEF(void) post_process_1pass49(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,50JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail,51JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,52JDIMENSION out_rows_avail);53#ifdef QUANT_2PASS_SUPPORTED54METHODDEF(void) post_process_prepass55(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,56JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail,57JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,58JDIMENSION out_rows_avail);59METHODDEF(void) post_process_2pass60(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,61JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail,62JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,63JDIMENSION out_rows_avail);64#endif656667/*68* Initialize for a processing pass.69*/7071METHODDEF(void)72start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)73{74my_post_ptr post = (my_post_ptr) cinfo->post;7576switch (pass_mode) {77case JBUF_PASS_THRU:78if (cinfo->quantize_colors) {79/* Single-pass processing with color quantization. */80post->pub.post_process_data = post_process_1pass;81/* We could be doing buffered-image output before starting a 2-pass82* color quantization; in that case, jinit_d_post_controller did not83* allocate a strip buffer. Use the virtual-array buffer as workspace.84*/85if (post->buffer == NULL) {86post->buffer = (*cinfo->mem->access_virt_sarray)87((j_common_ptr) cinfo, post->whole_image,88(JDIMENSION) 0, post->strip_height, TRUE);89}90} else {91/* For single-pass processing without color quantization,92* I have no work to do; just call the upsampler directly.93*/94post->pub.post_process_data = cinfo->upsample->upsample;95}96break;97#ifdef QUANT_2PASS_SUPPORTED98case JBUF_SAVE_AND_PASS:99/* First pass of 2-pass quantization */100if (post->whole_image == NULL)101ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);102post->pub.post_process_data = post_process_prepass;103break;104case JBUF_CRANK_DEST:105/* Second pass of 2-pass quantization */106if (post->whole_image == NULL)107ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);108post->pub.post_process_data = post_process_2pass;109break;110#endif /* QUANT_2PASS_SUPPORTED */111default:112ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);113break;114}115post->starting_row = post->next_row = 0;116}117118119/*120* Process some data in the one-pass (strip buffer) case.121* This is used for color precision reduction as well as one-pass quantization.122*/123124METHODDEF(void)125post_process_1pass (j_decompress_ptr cinfo,126JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,127JDIMENSION in_row_groups_avail,128JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,129JDIMENSION out_rows_avail)130{131my_post_ptr post = (my_post_ptr) cinfo->post;132JDIMENSION num_rows, max_rows;133134/* Fill the buffer, but not more than what we can dump out in one go. */135/* Note we rely on the upsampler to detect bottom of image. */136max_rows = out_rows_avail - *out_row_ctr;137if (max_rows > post->strip_height)138max_rows = post->strip_height;139num_rows = 0;140(*cinfo->upsample->upsample) (cinfo,141input_buf, in_row_group_ctr, in_row_groups_avail,142post->buffer, &num_rows, max_rows);143/* Quantize and emit data. */144(*cinfo->cquantize->color_quantize) (cinfo,145post->buffer, output_buf + *out_row_ctr, (int) num_rows);146*out_row_ctr += num_rows;147}148149150#ifdef QUANT_2PASS_SUPPORTED151152/*153* Process some data in the first pass of 2-pass quantization.154*/155156METHODDEF(void)157post_process_prepass (j_decompress_ptr cinfo,158JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,159JDIMENSION in_row_groups_avail,160JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,161JDIMENSION out_rows_avail)162{163my_post_ptr post = (my_post_ptr) cinfo->post;164JDIMENSION old_next_row, num_rows;165166/* Reposition virtual buffer if at start of strip. */167if (post->next_row == 0) {168post->buffer = (*cinfo->mem->access_virt_sarray)169((j_common_ptr) cinfo, post->whole_image,170post->starting_row, post->strip_height, TRUE);171}172173/* Upsample some data (up to a strip height's worth). */174old_next_row = post->next_row;175(*cinfo->upsample->upsample) (cinfo,176input_buf, in_row_group_ctr, in_row_groups_avail,177post->buffer, &post->next_row, post->strip_height);178179/* Allow quantizer to scan new data. No data is emitted, */180/* but we advance out_row_ctr so outer loop can tell when we're done. */181if (post->next_row > old_next_row) {182num_rows = post->next_row - old_next_row;183(*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,184(JSAMPARRAY) NULL, (int) num_rows);185*out_row_ctr += num_rows;186}187188/* Advance if we filled the strip. */189if (post->next_row >= post->strip_height) {190post->starting_row += post->strip_height;191post->next_row = 0;192}193}194195196/*197* Process some data in the second pass of 2-pass quantization.198*/199200METHODDEF(void)201post_process_2pass (j_decompress_ptr cinfo,202JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,203JDIMENSION in_row_groups_avail,204JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,205JDIMENSION out_rows_avail)206{207my_post_ptr post = (my_post_ptr) cinfo->post;208JDIMENSION num_rows, max_rows;209210/* Reposition virtual buffer if at start of strip. */211if (post->next_row == 0) {212post->buffer = (*cinfo->mem->access_virt_sarray)213((j_common_ptr) cinfo, post->whole_image,214post->starting_row, post->strip_height, FALSE);215}216217/* Determine number of rows to emit. */218num_rows = post->strip_height - post->next_row; /* available in strip */219max_rows = out_rows_avail - *out_row_ctr; /* available in output area */220if (num_rows > max_rows)221num_rows = max_rows;222/* We have to check bottom of image here, can't depend on upsampler. */223max_rows = cinfo->output_height - post->starting_row;224if (num_rows > max_rows)225num_rows = max_rows;226227/* Quantize and emit data. */228(*cinfo->cquantize->color_quantize) (cinfo,229post->buffer + post->next_row, output_buf + *out_row_ctr,230(int) num_rows);231*out_row_ctr += num_rows;232233/* Advance if we filled the strip. */234post->next_row += num_rows;235if (post->next_row >= post->strip_height) {236post->starting_row += post->strip_height;237post->next_row = 0;238}239}240241#endif /* QUANT_2PASS_SUPPORTED */242243244/*245* Initialize postprocessing controller.246*/247248GLOBAL(void)249jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)250{251my_post_ptr post;252253post = (my_post_ptr)254(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,255sizeof(my_post_controller));256cinfo->post = (struct jpeg_d_post_controller *) post;257post->pub.start_pass = start_pass_dpost;258post->whole_image = NULL; /* flag for no virtual arrays */259post->buffer = NULL; /* flag for no strip buffer */260261/* Create the quantization buffer, if needed */262if (cinfo->quantize_colors) {263/* The buffer strip height is max_v_samp_factor, which is typically264* an efficient number of rows for upsampling to return.265* (In the presence of output rescaling, we might want to be smarter?)266*/267post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;268if (need_full_buffer) {269/* Two-pass color quantization: need full-image storage. */270/* We round up the number of rows to a multiple of the strip height. */271#ifdef QUANT_2PASS_SUPPORTED272post->whole_image = (*cinfo->mem->request_virt_sarray)273((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,274cinfo->output_width * cinfo->out_color_components,275(JDIMENSION) jround_up((long) cinfo->output_height,276(long) post->strip_height),277post->strip_height);278#else279ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);280#endif /* QUANT_2PASS_SUPPORTED */281} else {282/* One-pass color quantization: just make a strip buffer. */283post->buffer = (*cinfo->mem->alloc_sarray)284((j_common_ptr) cinfo, JPOOL_IMAGE,285cinfo->output_width * cinfo->out_color_components,286post->strip_height);287}288}289}290291292