7 * Copyright (C) 1994-1995, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
11 * For conditions of distribution and use, see the accompanying README file.
15 * This file contains the decompression postprocessing controller.
17 * This controller manages the upsampling, color conversion, and color
19 * quantization/reduction steps; specifically, it controls the buffering
21 * between upsample/color conversion and color quantization/reduction.
25 * If no color quantization/reduction is required, then this module has no
27 * work to do, and it just hands off to the upsample/color conversion code.
29 * An integrated upsample/convert/quantize process would replace this module
37 #define JPEG_INTERNALS
41 #include "radiant_jpeglib.h"
47 /* Private buffer controller object */
53 struct jpeg_d_post_controller pub; /* public fields */
57 /* Color quantization source buffer: this holds output data from
59 * the upsample/color conversion step to be passed to the quantizer.
61 * For two-pass color quantization, we need a full-image buffer;
63 * for one-pass operation, a strip buffer is sufficient.
67 jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */
69 JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */
71 JDIMENSION strip_height; /* buffer size in rows */
73 /* for two-pass mode only: */
75 JDIMENSION starting_row; /* row # of first row in current strip */
77 JDIMENSION next_row; /* index of next row to fill/empty in strip */
83 typedef my_post_controller * my_post_ptr;
89 /* Forward declarations */
91 METHODDEF void post_process_1pass
93 JPP((j_decompress_ptr cinfo,
95 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
97 JDIMENSION in_row_groups_avail,
99 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
101 JDIMENSION out_rows_avail));
103 #ifdef QUANT_2PASS_SUPPORTED
105 METHODDEF void post_process_prepass
107 JPP((j_decompress_ptr cinfo,
109 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
111 JDIMENSION in_row_groups_avail,
113 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
115 JDIMENSION out_rows_avail));
117 METHODDEF void post_process_2pass
119 JPP((j_decompress_ptr cinfo,
121 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
123 JDIMENSION in_row_groups_avail,
125 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
127 JDIMENSION out_rows_avail));
137 * Initialize for a processing pass.
145 start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
149 my_post_ptr post = (my_post_ptr) cinfo->post;
157 if (cinfo->quantize_colors) {
159 /* Single-pass processing with color quantization. */
161 post->pub.post_process_data = post_process_1pass;
163 /* We could be doing buffered-image output before starting a 2-pass
165 * color quantization; in that case, jinit_d_post_controller did not
167 * allocate a strip buffer. Use the virtual-array buffer as workspace.
171 if (post->buffer == NULL) {
173 post->buffer = (*cinfo->mem->access_virt_sarray)
175 ((j_common_ptr) cinfo, post->whole_image,
177 (JDIMENSION) 0, post->strip_height, TRUE);
183 /* For single-pass processing without color quantization,
185 * I have no work to do; just call the upsampler directly.
189 post->pub.post_process_data = cinfo->upsample->upsample;
195 #ifdef QUANT_2PASS_SUPPORTED
197 case JBUF_SAVE_AND_PASS:
199 /* First pass of 2-pass quantization */
201 if (post->whole_image == NULL)
203 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
205 post->pub.post_process_data = post_process_prepass;
209 case JBUF_CRANK_DEST:
211 /* Second pass of 2-pass quantization */
213 if (post->whole_image == NULL)
215 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
217 post->pub.post_process_data = post_process_2pass;
221 #endif /* QUANT_2PASS_SUPPORTED */
225 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
231 post->starting_row = post->next_row = 0;
241 * Process some data in the one-pass (strip buffer) case.
243 * This is used for color precision reduction as well as one-pass quantization.
251 post_process_1pass (j_decompress_ptr cinfo,
253 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
255 JDIMENSION in_row_groups_avail,
257 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
259 JDIMENSION out_rows_avail)
263 my_post_ptr post = (my_post_ptr) cinfo->post;
265 JDIMENSION num_rows, max_rows;
269 /* Fill the buffer, but not more than what we can dump out in one go. */
271 /* Note we rely on the upsampler to detect bottom of image. */
273 max_rows = out_rows_avail - *out_row_ctr;
275 if (max_rows > post->strip_height)
277 max_rows = post->strip_height;
281 (*cinfo->upsample->upsample) (cinfo,
283 input_buf, in_row_group_ctr, in_row_groups_avail,
285 post->buffer, &num_rows, max_rows);
287 /* Quantize and emit data. */
289 (*cinfo->cquantize->color_quantize) (cinfo,
291 post->buffer, output_buf + *out_row_ctr, (int) num_rows);
293 *out_row_ctr += num_rows;
301 #ifdef QUANT_2PASS_SUPPORTED
307 * Process some data in the first pass of 2-pass quantization.
315 post_process_prepass (j_decompress_ptr cinfo,
317 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
319 JDIMENSION in_row_groups_avail,
321 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
323 JDIMENSION out_rows_avail)
327 my_post_ptr post = (my_post_ptr) cinfo->post;
329 JDIMENSION old_next_row, num_rows;
333 /* Reposition virtual buffer if at start of strip. */
335 if (post->next_row == 0) {
337 post->buffer = (*cinfo->mem->access_virt_sarray)
339 ((j_common_ptr) cinfo, post->whole_image,
341 post->starting_row, post->strip_height, TRUE);
347 /* Upsample some data (up to a strip height's worth). */
349 old_next_row = post->next_row;
351 (*cinfo->upsample->upsample) (cinfo,
353 input_buf, in_row_group_ctr, in_row_groups_avail,
355 post->buffer, &post->next_row, post->strip_height);
359 /* Allow quantizer to scan new data. No data is emitted, */
361 /* but we advance out_row_ctr so outer loop can tell when we're done. */
363 if (post->next_row > old_next_row) {
365 num_rows = post->next_row - old_next_row;
367 (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
369 (JSAMPARRAY) NULL, (int) num_rows);
371 *out_row_ctr += num_rows;
377 /* Advance if we filled the strip. */
379 if (post->next_row >= post->strip_height) {
381 post->starting_row += post->strip_height;
395 * Process some data in the second pass of 2-pass quantization.
403 post_process_2pass (j_decompress_ptr cinfo,
405 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
407 JDIMENSION in_row_groups_avail,
409 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
411 JDIMENSION out_rows_avail)
415 my_post_ptr post = (my_post_ptr) cinfo->post;
417 JDIMENSION num_rows, max_rows;
421 /* Reposition virtual buffer if at start of strip. */
423 if (post->next_row == 0) {
425 post->buffer = (*cinfo->mem->access_virt_sarray)
427 ((j_common_ptr) cinfo, post->whole_image,
429 post->starting_row, post->strip_height, FALSE);
435 /* Determine number of rows to emit. */
437 num_rows = post->strip_height - post->next_row; /* available in strip */
439 max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
441 if (num_rows > max_rows)
445 /* We have to check bottom of image here, can't depend on upsampler. */
447 max_rows = cinfo->output_height - post->starting_row;
449 if (num_rows > max_rows)
455 /* Quantize and emit data. */
457 (*cinfo->cquantize->color_quantize) (cinfo,
459 post->buffer + post->next_row, output_buf + *out_row_ctr,
463 *out_row_ctr += num_rows;
467 /* Advance if we filled the strip. */
469 post->next_row += num_rows;
471 if (post->next_row >= post->strip_height) {
473 post->starting_row += post->strip_height;
483 #endif /* QUANT_2PASS_SUPPORTED */
491 * Initialize postprocessing controller.
499 jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
509 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
511 SIZEOF(my_post_controller));
513 cinfo->post = (struct jpeg_d_post_controller *) post;
515 post->pub.start_pass = start_pass_dpost;
517 post->whole_image = NULL; /* flag for no virtual arrays */
519 post->buffer = NULL; /* flag for no strip buffer */
523 /* Create the quantization buffer, if needed */
525 if (cinfo->quantize_colors) {
527 /* The buffer strip height is max_v_samp_factor, which is typically
529 * an efficient number of rows for upsampling to return.
531 * (In the presence of output rescaling, we might want to be smarter?)
535 post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
537 if (need_full_buffer) {
539 /* Two-pass color quantization: need full-image storage. */
541 /* We round up the number of rows to a multiple of the strip height. */
543 #ifdef QUANT_2PASS_SUPPORTED
545 post->whole_image = (*cinfo->mem->request_virt_sarray)
547 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
549 cinfo->output_width * cinfo->out_color_components,
551 (JDIMENSION) jround_up((long) cinfo->output_height,
553 (long) post->strip_height),
559 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
561 #endif /* QUANT_2PASS_SUPPORTED */
565 /* One-pass color quantization: just make a strip buffer. */
567 post->buffer = (*cinfo->mem->alloc_sarray)
569 ((j_common_ptr) cinfo, JPOOL_IMAGE,
571 cinfo->output_width * cinfo->out_color_components,