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