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