1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * jdpostct.c
7 *
8 * Copyright (C) 1994-1996, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
11 *
12 * This file contains the decompression postprocessing controller.
13 * This controller manages the upsampling, color conversion, and color
14 * quantization/reduction steps; specifically, it controls the buffering
15 * between upsample/color conversion and color quantization/reduction.
16 *
17 * If no color quantization/reduction is required, then this module has no
18 * work to do, and it just hands off to the upsample/color conversion code.
19 * An integrated upsample/convert/quantize process would replace this module
20 * entirely.
21 */
22
23#define JPEG_INTERNALS
24#include "jinclude.h"
25#include "jpeglib.h"
26
27
28/* Private buffer controller object */
29
30typedef struct {
31 struct jpeg_d_post_controller pub; /* public fields */
32
33 /* Color quantization source buffer: this holds output data from
34 * the upsample/color conversion step to be passed to the quantizer.
35 * For two-pass color quantization, we need a full-image buffer;
36 * for one-pass operation, a strip buffer is sufficient.
37 */
38 jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */
39 JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */
40 JDIMENSION strip_height; /* buffer size in rows */
41 /* for two-pass mode only: */
42 JDIMENSION starting_row; /* row # of first row in current strip */
43 JDIMENSION next_row; /* index of next row to fill/empty in strip */
44} my_post_controller;
45
46typedef my_post_controller * my_post_ptr;
47
48
49/* Forward declarations */
50METHODDEF(void) post_process_1pass
51 JPP((j_decompress_ptr cinfo,
52 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
53 JDIMENSION in_row_groups_avail,
54 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
55 JDIMENSION out_rows_avail));
56#ifdef QUANT_2PASS_SUPPORTED
57METHODDEF(void) post_process_prepass
58 JPP((j_decompress_ptr cinfo,
59 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
60 JDIMENSION in_row_groups_avail,
61 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
62 JDIMENSION out_rows_avail));
63METHODDEF(void) post_process_2pass
64 JPP((j_decompress_ptr cinfo,
65 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
66 JDIMENSION in_row_groups_avail,
67 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
68 JDIMENSION out_rows_avail));
69#endif
70
71
72/*
73 * Initialize for a processing pass.
74 */
75
76METHODDEF(void)
77start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
78{
79 my_post_ptr post = (my_post_ptr) cinfo->post;
80
81 switch (pass_mode) {
82 case JBUF_PASS_THRU:
83 if (cinfo->quantize_colors) {
84 /* Single-pass processing with color quantization. */
85 post->pub.post_process_data = post_process_1pass;
86 /* We could be doing buffered-image output before starting a 2-pass
87 * color quantization; in that case, jinit_d_post_controller did not
88 * allocate a strip buffer. Use the virtual-array buffer as workspace.
89 */
90 if (post->buffer == NULL) {
91 post->buffer = (*cinfo->mem->access_virt_sarray)
92 ((j_common_ptr) cinfo, post->whole_image,
93 (JDIMENSION) 0, post->strip_height, TRUE);
94 }
95 } else {
96 /* For single-pass processing without color quantization,
97 * I have no work to do; just call the upsampler directly.
98 */
99 post->pub.post_process_data = cinfo->upsample->upsample;
100 }
101 break;
102#ifdef QUANT_2PASS_SUPPORTED
103 case JBUF_SAVE_AND_PASS:
104 /* First pass of 2-pass quantization */
105 if (post->whole_image == NULL)
106 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
107 post->pub.post_process_data = post_process_prepass;
108 break;
109 case JBUF_CRANK_DEST:
110 /* Second pass of 2-pass quantization */
111 if (post->whole_image == NULL)
112 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
113 post->pub.post_process_data = post_process_2pass;
114 break;
115#endif /* QUANT_2PASS_SUPPORTED */
116 default:
117 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
118 break;
119 }
120 post->starting_row = post->next_row = 0;
121}
122
123
124/*
125 * Process some data in the one-pass (strip buffer) case.
126 * This is used for color precision reduction as well as one-pass quantization.
127 */
128
129METHODDEF(void)
130post_process_1pass (j_decompress_ptr cinfo,
131 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
132 JDIMENSION in_row_groups_avail,
133 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
134 JDIMENSION out_rows_avail)
135{
136 my_post_ptr post = (my_post_ptr) cinfo->post;
137 JDIMENSION num_rows, max_rows;
138
139 /* Fill the buffer, but not more than what we can dump out in one go. */
140 /* Note we rely on the upsampler to detect bottom of image. */
141 max_rows = out_rows_avail - *out_row_ctr;
142 if (max_rows > post->strip_height)
143 max_rows = post->strip_height;
144 num_rows = 0;
145 (*cinfo->upsample->upsample) (cinfo,
146 input_buf, in_row_group_ctr, in_row_groups_avail,
147 post->buffer, &num_rows, max_rows);
148 /* Quantize and emit data. */
149 (*cinfo->cquantize->color_quantize) (cinfo,
150 post->buffer, output_buf + *out_row_ctr, (int) num_rows);
151 *out_row_ctr += num_rows;
152}
153
154
155#ifdef QUANT_2PASS_SUPPORTED
156
157/*
158 * Process some data in the first pass of 2-pass quantization.
159 */
160
161METHODDEF(void)
162post_process_prepass (j_decompress_ptr cinfo,
163 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
164 JDIMENSION in_row_groups_avail,
165 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
166 JDIMENSION out_rows_avail)
167{
168 my_post_ptr post = (my_post_ptr) cinfo->post;
169 JDIMENSION old_next_row, num_rows;
170
171 /* Reposition virtual buffer if at start of strip. */
172 if (post->next_row == 0) {
173 post->buffer = (*cinfo->mem->access_virt_sarray)
174 ((j_common_ptr) cinfo, post->whole_image,
175 post->starting_row, post->strip_height, TRUE);
176 }
177
178 /* Upsample some data (up to a strip height's worth). */
179 old_next_row = post->next_row;
180 (*cinfo->upsample->upsample) (cinfo,
181 input_buf, in_row_group_ctr, in_row_groups_avail,
182 post->buffer, &post->next_row, post->strip_height);
183
184 /* Allow quantizer to scan new data. No data is emitted, */
185 /* but we advance out_row_ctr so outer loop can tell when we're done. */
186 if (post->next_row > old_next_row) {
187 num_rows = post->next_row - old_next_row;
188 (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
189 (JSAMPARRAY) NULL, (int) num_rows);
190 *out_row_ctr += num_rows;
191 }
192
193 /* Advance if we filled the strip. */
194 if (post->next_row >= post->strip_height) {
195 post->starting_row += post->strip_height;
196 post->next_row = 0;
197 }
198}
199
200
201/*
202 * Process some data in the second pass of 2-pass quantization.
203 */
204
205METHODDEF(void)
206post_process_2pass (j_decompress_ptr cinfo,
207 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
208 JDIMENSION in_row_groups_avail,
209 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
210 JDIMENSION out_rows_avail)
211{
212 my_post_ptr post = (my_post_ptr) cinfo->post;
213 JDIMENSION num_rows, max_rows;
214
215 /* Reposition virtual buffer if at start of strip. */
216 if (post->next_row == 0) {
217 post->buffer = (*cinfo->mem->access_virt_sarray)
218 ((j_common_ptr) cinfo, post->whole_image,
219 post->starting_row, post->strip_height, FALSE);
220 }
221
222 /* Determine number of rows to emit. */
223 num_rows = post->strip_height - post->next_row; /* available in strip */
224 max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
225 if (num_rows > max_rows)
226 num_rows = max_rows;
227 /* We have to check bottom of image here, can't depend on upsampler. */
228 max_rows = cinfo->output_height - post->starting_row;
229 if (num_rows > max_rows)
230 num_rows = max_rows;
231
232 /* Quantize and emit data. */
233 (*cinfo->cquantize->color_quantize) (cinfo,
234 post->buffer + post->next_row, output_buf + *out_row_ctr,
235 (int) num_rows);
236 *out_row_ctr += num_rows;
237
238 /* Advance if we filled the strip. */
239 post->next_row += num_rows;
240 if (post->next_row >= post->strip_height) {
241 post->starting_row += post->strip_height;
242 post->next_row = 0;
243 }
244}
245
246#endif /* QUANT_2PASS_SUPPORTED */
247
248
249/*
250 * Initialize postprocessing controller.
251 */
252
253GLOBAL(void)
254jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
255{
256 my_post_ptr post;
257
258 post = (my_post_ptr)
259 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
260 SIZEOF(my_post_controller));
261 cinfo->post = (struct jpeg_d_post_controller *) post;
262 post->pub.start_pass = start_pass_dpost;
263 post->whole_image = NULL; /* flag for no virtual arrays */
264 post->buffer = NULL; /* flag for no strip buffer */
265
266 /* Create the quantization buffer, if needed */
267 if (cinfo->quantize_colors) {
268 /* The buffer strip height is max_v_samp_factor, which is typically
269 * an efficient number of rows for upsampling to return.
270 * (In the presence of output rescaling, we might want to be smarter?)
271 */
272 post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
273 if (need_full_buffer) {
274 /* Two-pass color quantization: need full-image storage. */
275 /* We round up the number of rows to a multiple of the strip height. */
276#ifdef QUANT_2PASS_SUPPORTED
277 post->whole_image = (*cinfo->mem->request_virt_sarray)
278 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
279 cinfo->output_width * cinfo->out_color_components,
280 (JDIMENSION) jround_up((long) cinfo->output_height,
281 (long) post->strip_height),
282 post->strip_height);
283#else
284 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
285#endif /* QUANT_2PASS_SUPPORTED */
286 } else {
287 /* One-pass color quantization: just make a strip buffer. */
288 post->buffer = (*cinfo->mem->alloc_sarray)
289 ((j_common_ptr) cinfo, JPOOL_IMAGE,
290 cinfo->output_width * cinfo->out_color_components,
291 post->strip_height);
292 }
293 }
294}
295