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