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