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