2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: jdpostct.c /main/2 1996/05/09 03:49:39 drk $ */
27 * Copyright (C) 1994-1996, Thomas G. Lane.
28 * This file is part of the Independent JPEG Group's software.
29 * For conditions of distribution and use, see the accompanying README file.
31 * This file contains the decompression postprocessing controller.
32 * This controller manages the upsampling, color conversion, and color
33 * quantization/reduction steps; specifically, it controls the buffering
34 * between upsample/color conversion and color quantization/reduction.
36 * If no color quantization/reduction is required, then this module has no
37 * work to do, and it just hands off to the upsample/color conversion code.
38 * An integrated upsample/convert/quantize process would replace this module
42 #define JPEG_INTERNALS
47 /* Private buffer controller object */
50 struct jpeg_d_post_controller pub; /* public fields */
52 /* Color quantization source buffer: this holds output data from
53 * the upsample/color conversion step to be passed to the quantizer.
54 * For two-pass color quantization, we need a full-image buffer;
55 * for one-pass operation, a strip buffer is sufficient.
57 jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */
58 JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */
59 JDIMENSION strip_height; /* buffer size in rows */
60 /* for two-pass mode only: */
61 JDIMENSION starting_row; /* row # of first row in current strip */
62 JDIMENSION next_row; /* index of next row to fill/empty in strip */
65 typedef my_post_controller * my_post_ptr;
68 /* Forward declarations */
69 METHODDEF(void) post_process_1pass
70 JPP((j_decompress_ptr cinfo,
71 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
72 JDIMENSION in_row_groups_avail,
73 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
74 JDIMENSION out_rows_avail));
75 #ifdef QUANT_2PASS_SUPPORTED
76 METHODDEF(void) post_process_prepass
77 JPP((j_decompress_ptr cinfo,
78 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
79 JDIMENSION in_row_groups_avail,
80 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
81 JDIMENSION out_rows_avail));
82 METHODDEF(void) post_process_2pass
83 JPP((j_decompress_ptr cinfo,
84 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
85 JDIMENSION in_row_groups_avail,
86 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
87 JDIMENSION out_rows_avail));
92 * Initialize for a processing pass.
96 start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
98 my_post_ptr post = (my_post_ptr) cinfo->post;
102 if (cinfo->quantize_colors) {
103 /* Single-pass processing with color quantization. */
104 post->pub.post_process_data = post_process_1pass;
105 /* We could be doing buffered-image output before starting a 2-pass
106 * color quantization; in that case, jinit_d_post_controller did not
107 * allocate a strip buffer. Use the virtual-array buffer as workspace.
109 if (post->buffer == NULL) {
110 post->buffer = (*cinfo->mem->access_virt_sarray)
111 ((j_common_ptr) cinfo, post->whole_image,
112 (JDIMENSION) 0, post->strip_height, TRUE);
115 /* For single-pass processing without color quantization,
116 * I have no work to do; just call the upsampler directly.
118 post->pub.post_process_data = cinfo->upsample->upsample;
121 #ifdef QUANT_2PASS_SUPPORTED
122 case JBUF_SAVE_AND_PASS:
123 /* First pass of 2-pass quantization */
124 if (post->whole_image == NULL)
125 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
126 post->pub.post_process_data = post_process_prepass;
128 case JBUF_CRANK_DEST:
129 /* Second pass of 2-pass quantization */
130 if (post->whole_image == NULL)
131 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
132 post->pub.post_process_data = post_process_2pass;
134 #endif /* QUANT_2PASS_SUPPORTED */
136 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
139 post->starting_row = post->next_row = 0;
144 * Process some data in the one-pass (strip buffer) case.
145 * This is used for color precision reduction as well as one-pass quantization.
149 post_process_1pass (j_decompress_ptr cinfo,
150 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
151 JDIMENSION in_row_groups_avail,
152 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
153 JDIMENSION out_rows_avail)
155 my_post_ptr post = (my_post_ptr) cinfo->post;
156 JDIMENSION num_rows, max_rows;
158 /* Fill the buffer, but not more than what we can dump out in one go. */
159 /* Note we rely on the upsampler to detect bottom of image. */
160 max_rows = out_rows_avail - *out_row_ctr;
161 if (max_rows > post->strip_height)
162 max_rows = post->strip_height;
164 (*cinfo->upsample->upsample) (cinfo,
165 input_buf, in_row_group_ctr, in_row_groups_avail,
166 post->buffer, &num_rows, max_rows);
167 /* Quantize and emit data. */
168 (*cinfo->cquantize->color_quantize) (cinfo,
169 post->buffer, output_buf + *out_row_ctr, (int) num_rows);
170 *out_row_ctr += num_rows;
174 #ifdef QUANT_2PASS_SUPPORTED
177 * Process some data in the first pass of 2-pass quantization.
181 post_process_prepass (j_decompress_ptr cinfo,
182 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
183 JDIMENSION in_row_groups_avail,
184 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
185 JDIMENSION out_rows_avail)
187 my_post_ptr post = (my_post_ptr) cinfo->post;
188 JDIMENSION old_next_row, num_rows;
190 /* Reposition virtual buffer if at start of strip. */
191 if (post->next_row == 0) {
192 post->buffer = (*cinfo->mem->access_virt_sarray)
193 ((j_common_ptr) cinfo, post->whole_image,
194 post->starting_row, post->strip_height, TRUE);
197 /* Upsample some data (up to a strip height's worth). */
198 old_next_row = post->next_row;
199 (*cinfo->upsample->upsample) (cinfo,
200 input_buf, in_row_group_ctr, in_row_groups_avail,
201 post->buffer, &post->next_row, post->strip_height);
203 /* Allow quantizer to scan new data. No data is emitted, */
204 /* but we advance out_row_ctr so outer loop can tell when we're done. */
205 if (post->next_row > old_next_row) {
206 num_rows = post->next_row - old_next_row;
207 (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
208 (JSAMPARRAY) NULL, (int) num_rows);
209 *out_row_ctr += num_rows;
212 /* Advance if we filled the strip. */
213 if (post->next_row >= post->strip_height) {
214 post->starting_row += post->strip_height;
221 * Process some data in the second pass of 2-pass quantization.
225 post_process_2pass (j_decompress_ptr cinfo,
226 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
227 JDIMENSION in_row_groups_avail,
228 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
229 JDIMENSION out_rows_avail)
231 my_post_ptr post = (my_post_ptr) cinfo->post;
232 JDIMENSION num_rows, max_rows;
234 /* Reposition virtual buffer if at start of strip. */
235 if (post->next_row == 0) {
236 post->buffer = (*cinfo->mem->access_virt_sarray)
237 ((j_common_ptr) cinfo, post->whole_image,
238 post->starting_row, post->strip_height, FALSE);
241 /* Determine number of rows to emit. */
242 num_rows = post->strip_height - post->next_row; /* available in strip */
243 max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
244 if (num_rows > max_rows)
246 /* We have to check bottom of image here, can't depend on upsampler. */
247 max_rows = cinfo->output_height - post->starting_row;
248 if (num_rows > max_rows)
251 /* Quantize and emit data. */
252 (*cinfo->cquantize->color_quantize) (cinfo,
253 post->buffer + post->next_row, output_buf + *out_row_ctr,
255 *out_row_ctr += num_rows;
257 /* Advance if we filled the strip. */
258 post->next_row += num_rows;
259 if (post->next_row >= post->strip_height) {
260 post->starting_row += post->strip_height;
265 #endif /* QUANT_2PASS_SUPPORTED */
269 * Initialize postprocessing controller.
273 jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
278 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
279 SIZEOF(my_post_controller));
280 cinfo->post = (struct jpeg_d_post_controller *) post;
281 post->pub.start_pass = start_pass_dpost;
282 post->whole_image = NULL; /* flag for no virtual arrays */
283 post->buffer = NULL; /* flag for no strip buffer */
285 /* Create the quantization buffer, if needed */
286 if (cinfo->quantize_colors) {
287 /* The buffer strip height is max_v_samp_factor, which is typically
288 * an efficient number of rows for upsampling to return.
289 * (In the presence of output rescaling, we might want to be smarter?)
291 post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
292 if (need_full_buffer) {
293 /* Two-pass color quantization: need full-image storage. */
294 /* We round up the number of rows to a multiple of the strip height. */
295 #ifdef QUANT_2PASS_SUPPORTED
296 post->whole_image = (*cinfo->mem->request_virt_sarray)
297 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
298 cinfo->output_width * cinfo->out_color_components,
299 (JDIMENSION) jround_up((long) cinfo->output_height,
300 (long) post->strip_height),
303 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
304 #endif /* QUANT_2PASS_SUPPORTED */
306 /* One-pass color quantization: just make a strip buffer. */
307 post->buffer = (*cinfo->mem->alloc_sarray)
308 ((j_common_ptr) cinfo, JPOOL_IMAGE,
309 cinfo->output_width * cinfo->out_color_components,