rev |
line source |
nuclear@0
|
1 /*
|
nuclear@0
|
2 * jcprepct.c
|
nuclear@0
|
3 *
|
nuclear@0
|
4 * Copyright (C) 1994-1996, Thomas G. Lane.
|
nuclear@0
|
5 * This file is part of the Independent JPEG Group's software.
|
nuclear@0
|
6 * For conditions of distribution and use, see the accompanying README file.
|
nuclear@0
|
7 *
|
nuclear@0
|
8 * This file contains the compression preprocessing controller.
|
nuclear@0
|
9 * This controller manages the color conversion, downsampling,
|
nuclear@0
|
10 * and edge expansion steps.
|
nuclear@0
|
11 *
|
nuclear@0
|
12 * Most of the complexity here is associated with buffering input rows
|
nuclear@0
|
13 * as required by the downsampler. See the comments at the head of
|
nuclear@0
|
14 * jcsample.c for the downsampler's needs.
|
nuclear@0
|
15 */
|
nuclear@0
|
16
|
nuclear@0
|
17 #define JPEG_INTERNALS
|
nuclear@0
|
18 #include "jinclude.h"
|
nuclear@0
|
19 #include "jpeglib.h"
|
nuclear@0
|
20
|
nuclear@0
|
21
|
nuclear@0
|
22 /* At present, jcsample.c can request context rows only for smoothing.
|
nuclear@0
|
23 * In the future, we might also need context rows for CCIR601 sampling
|
nuclear@0
|
24 * or other more-complex downsampling procedures. The code to support
|
nuclear@0
|
25 * context rows should be compiled only if needed.
|
nuclear@0
|
26 */
|
nuclear@0
|
27 #ifdef INPUT_SMOOTHING_SUPPORTED
|
nuclear@0
|
28 #define CONTEXT_ROWS_SUPPORTED
|
nuclear@0
|
29 #endif
|
nuclear@0
|
30
|
nuclear@0
|
31
|
nuclear@0
|
32 /*
|
nuclear@0
|
33 * For the simple (no-context-row) case, we just need to buffer one
|
nuclear@0
|
34 * row group's worth of pixels for the downsampling step. At the bottom of
|
nuclear@0
|
35 * the image, we pad to a full row group by replicating the last pixel row.
|
nuclear@0
|
36 * The downsampler's last output row is then replicated if needed to pad
|
nuclear@0
|
37 * out to a full iMCU row.
|
nuclear@0
|
38 *
|
nuclear@0
|
39 * When providing context rows, we must buffer three row groups' worth of
|
nuclear@0
|
40 * pixels. Three row groups are physically allocated, but the row pointer
|
nuclear@0
|
41 * arrays are made five row groups high, with the extra pointers above and
|
nuclear@0
|
42 * below "wrapping around" to point to the last and first real row groups.
|
nuclear@0
|
43 * This allows the downsampler to access the proper context rows.
|
nuclear@0
|
44 * At the top and bottom of the image, we create dummy context rows by
|
nuclear@0
|
45 * copying the first or last real pixel row. This copying could be avoided
|
nuclear@0
|
46 * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
|
nuclear@0
|
47 * trouble on the compression side.
|
nuclear@0
|
48 */
|
nuclear@0
|
49
|
nuclear@0
|
50
|
nuclear@0
|
51 /* Private buffer controller object */
|
nuclear@0
|
52
|
nuclear@0
|
53 typedef struct {
|
nuclear@0
|
54 struct jpeg_c_prep_controller pub; /* public fields */
|
nuclear@0
|
55
|
nuclear@0
|
56 /* Downsampling input buffer. This buffer holds color-converted data
|
nuclear@0
|
57 * until we have enough to do a downsample step.
|
nuclear@0
|
58 */
|
nuclear@0
|
59 JSAMPARRAY color_buf[MAX_COMPONENTS];
|
nuclear@0
|
60
|
nuclear@0
|
61 JDIMENSION rows_to_go; /* counts rows remaining in source image */
|
nuclear@0
|
62 int next_buf_row; /* index of next row to store in color_buf */
|
nuclear@0
|
63
|
nuclear@0
|
64 #ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
|
nuclear@0
|
65 int this_row_group; /* starting row index of group to process */
|
nuclear@0
|
66 int next_buf_stop; /* downsample when we reach this index */
|
nuclear@0
|
67 #endif
|
nuclear@0
|
68 } my_prep_controller;
|
nuclear@0
|
69
|
nuclear@0
|
70 typedef my_prep_controller * my_prep_ptr;
|
nuclear@0
|
71
|
nuclear@0
|
72
|
nuclear@0
|
73 /*
|
nuclear@0
|
74 * Initialize for a processing pass.
|
nuclear@0
|
75 */
|
nuclear@0
|
76
|
nuclear@0
|
77 METHODDEF(void)
|
nuclear@0
|
78 start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
|
nuclear@0
|
79 {
|
nuclear@0
|
80 my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
|
nuclear@0
|
81
|
nuclear@0
|
82 if (pass_mode != JBUF_PASS_THRU)
|
nuclear@0
|
83 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
|
nuclear@0
|
84
|
nuclear@0
|
85 /* Initialize total-height counter for detecting bottom of image */
|
nuclear@0
|
86 prep->rows_to_go = cinfo->image_height;
|
nuclear@0
|
87 /* Mark the conversion buffer empty */
|
nuclear@0
|
88 prep->next_buf_row = 0;
|
nuclear@0
|
89 #ifdef CONTEXT_ROWS_SUPPORTED
|
nuclear@0
|
90 /* Preset additional state variables for context mode.
|
nuclear@0
|
91 * These aren't used in non-context mode, so we needn't test which mode.
|
nuclear@0
|
92 */
|
nuclear@0
|
93 prep->this_row_group = 0;
|
nuclear@0
|
94 /* Set next_buf_stop to stop after two row groups have been read in. */
|
nuclear@0
|
95 prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
|
nuclear@0
|
96 #endif
|
nuclear@0
|
97 }
|
nuclear@0
|
98
|
nuclear@0
|
99
|
nuclear@0
|
100 /*
|
nuclear@0
|
101 * Expand an image vertically from height input_rows to height output_rows,
|
nuclear@0
|
102 * by duplicating the bottom row.
|
nuclear@0
|
103 */
|
nuclear@0
|
104
|
nuclear@0
|
105 LOCAL(void)
|
nuclear@0
|
106 expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
|
nuclear@0
|
107 int input_rows, int output_rows)
|
nuclear@0
|
108 {
|
nuclear@0
|
109 register int row;
|
nuclear@0
|
110
|
nuclear@0
|
111 for (row = input_rows; row < output_rows; row++) {
|
nuclear@0
|
112 jcopy_sample_rows(image_data, input_rows-1, image_data, row,
|
nuclear@0
|
113 1, num_cols);
|
nuclear@0
|
114 }
|
nuclear@0
|
115 }
|
nuclear@0
|
116
|
nuclear@0
|
117
|
nuclear@0
|
118 /*
|
nuclear@0
|
119 * Process some data in the simple no-context case.
|
nuclear@0
|
120 *
|
nuclear@0
|
121 * Preprocessor output data is counted in "row groups". A row group
|
nuclear@0
|
122 * is defined to be v_samp_factor sample rows of each component.
|
nuclear@0
|
123 * Downsampling will produce this much data from each max_v_samp_factor
|
nuclear@0
|
124 * input rows.
|
nuclear@0
|
125 */
|
nuclear@0
|
126
|
nuclear@0
|
127 METHODDEF(void)
|
nuclear@0
|
128 pre_process_data (j_compress_ptr cinfo,
|
nuclear@0
|
129 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
|
nuclear@0
|
130 JDIMENSION in_rows_avail,
|
nuclear@0
|
131 JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
|
nuclear@0
|
132 JDIMENSION out_row_groups_avail)
|
nuclear@0
|
133 {
|
nuclear@0
|
134 my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
|
nuclear@0
|
135 int numrows, ci;
|
nuclear@0
|
136 JDIMENSION inrows;
|
nuclear@0
|
137 jpeg_component_info * compptr;
|
nuclear@0
|
138
|
nuclear@0
|
139 while (*in_row_ctr < in_rows_avail &&
|
nuclear@0
|
140 *out_row_group_ctr < out_row_groups_avail) {
|
nuclear@0
|
141 /* Do color conversion to fill the conversion buffer. */
|
nuclear@0
|
142 inrows = in_rows_avail - *in_row_ctr;
|
nuclear@0
|
143 numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
|
nuclear@0
|
144 numrows = (int) MIN((JDIMENSION) numrows, inrows);
|
nuclear@0
|
145 (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
|
nuclear@0
|
146 prep->color_buf,
|
nuclear@0
|
147 (JDIMENSION) prep->next_buf_row,
|
nuclear@0
|
148 numrows);
|
nuclear@0
|
149 *in_row_ctr += numrows;
|
nuclear@0
|
150 prep->next_buf_row += numrows;
|
nuclear@0
|
151 prep->rows_to_go -= numrows;
|
nuclear@0
|
152 /* If at bottom of image, pad to fill the conversion buffer. */
|
nuclear@0
|
153 if (prep->rows_to_go == 0 &&
|
nuclear@0
|
154 prep->next_buf_row < cinfo->max_v_samp_factor) {
|
nuclear@0
|
155 for (ci = 0; ci < cinfo->num_components; ci++) {
|
nuclear@0
|
156 expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
|
nuclear@0
|
157 prep->next_buf_row, cinfo->max_v_samp_factor);
|
nuclear@0
|
158 }
|
nuclear@0
|
159 prep->next_buf_row = cinfo->max_v_samp_factor;
|
nuclear@0
|
160 }
|
nuclear@0
|
161 /* If we've filled the conversion buffer, empty it. */
|
nuclear@0
|
162 if (prep->next_buf_row == cinfo->max_v_samp_factor) {
|
nuclear@0
|
163 (*cinfo->downsample->downsample) (cinfo,
|
nuclear@0
|
164 prep->color_buf, (JDIMENSION) 0,
|
nuclear@0
|
165 output_buf, *out_row_group_ctr);
|
nuclear@0
|
166 prep->next_buf_row = 0;
|
nuclear@0
|
167 (*out_row_group_ctr)++;
|
nuclear@0
|
168 }
|
nuclear@0
|
169 /* If at bottom of image, pad the output to a full iMCU height.
|
nuclear@0
|
170 * Note we assume the caller is providing a one-iMCU-height output buffer!
|
nuclear@0
|
171 */
|
nuclear@0
|
172 if (prep->rows_to_go == 0 &&
|
nuclear@0
|
173 *out_row_group_ctr < out_row_groups_avail) {
|
nuclear@0
|
174 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
nuclear@0
|
175 ci++, compptr++) {
|
nuclear@0
|
176 expand_bottom_edge(output_buf[ci],
|
nuclear@0
|
177 compptr->width_in_blocks * DCTSIZE,
|
nuclear@0
|
178 (int) (*out_row_group_ctr * compptr->v_samp_factor),
|
nuclear@0
|
179 (int) (out_row_groups_avail * compptr->v_samp_factor));
|
nuclear@0
|
180 }
|
nuclear@0
|
181 *out_row_group_ctr = out_row_groups_avail;
|
nuclear@0
|
182 break; /* can exit outer loop without test */
|
nuclear@0
|
183 }
|
nuclear@0
|
184 }
|
nuclear@0
|
185 }
|
nuclear@0
|
186
|
nuclear@0
|
187
|
nuclear@0
|
188 #ifdef CONTEXT_ROWS_SUPPORTED
|
nuclear@0
|
189
|
nuclear@0
|
190 /*
|
nuclear@0
|
191 * Process some data in the context case.
|
nuclear@0
|
192 */
|
nuclear@0
|
193
|
nuclear@0
|
194 METHODDEF(void)
|
nuclear@0
|
195 pre_process_context (j_compress_ptr cinfo,
|
nuclear@0
|
196 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
|
nuclear@0
|
197 JDIMENSION in_rows_avail,
|
nuclear@0
|
198 JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
|
nuclear@0
|
199 JDIMENSION out_row_groups_avail)
|
nuclear@0
|
200 {
|
nuclear@0
|
201 my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
|
nuclear@0
|
202 int numrows, ci;
|
nuclear@0
|
203 int buf_height = cinfo->max_v_samp_factor * 3;
|
nuclear@0
|
204 JDIMENSION inrows;
|
nuclear@0
|
205
|
nuclear@0
|
206 while (*out_row_group_ctr < out_row_groups_avail) {
|
nuclear@0
|
207 if (*in_row_ctr < in_rows_avail) {
|
nuclear@0
|
208 /* Do color conversion to fill the conversion buffer. */
|
nuclear@0
|
209 inrows = in_rows_avail - *in_row_ctr;
|
nuclear@0
|
210 numrows = prep->next_buf_stop - prep->next_buf_row;
|
nuclear@0
|
211 numrows = (int) MIN((JDIMENSION) numrows, inrows);
|
nuclear@0
|
212 (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
|
nuclear@0
|
213 prep->color_buf,
|
nuclear@0
|
214 (JDIMENSION) prep->next_buf_row,
|
nuclear@0
|
215 numrows);
|
nuclear@0
|
216 /* Pad at top of image, if first time through */
|
nuclear@0
|
217 if (prep->rows_to_go == cinfo->image_height) {
|
nuclear@0
|
218 for (ci = 0; ci < cinfo->num_components; ci++) {
|
nuclear@0
|
219 int row;
|
nuclear@0
|
220 for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
|
nuclear@0
|
221 jcopy_sample_rows(prep->color_buf[ci], 0,
|
nuclear@0
|
222 prep->color_buf[ci], -row,
|
nuclear@0
|
223 1, cinfo->image_width);
|
nuclear@0
|
224 }
|
nuclear@0
|
225 }
|
nuclear@0
|
226 }
|
nuclear@0
|
227 *in_row_ctr += numrows;
|
nuclear@0
|
228 prep->next_buf_row += numrows;
|
nuclear@0
|
229 prep->rows_to_go -= numrows;
|
nuclear@0
|
230 } else {
|
nuclear@0
|
231 /* Return for more data, unless we are at the bottom of the image. */
|
nuclear@0
|
232 if (prep->rows_to_go != 0)
|
nuclear@0
|
233 break;
|
nuclear@0
|
234 /* When at bottom of image, pad to fill the conversion buffer. */
|
nuclear@0
|
235 if (prep->next_buf_row < prep->next_buf_stop) {
|
nuclear@0
|
236 for (ci = 0; ci < cinfo->num_components; ci++) {
|
nuclear@0
|
237 expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
|
nuclear@0
|
238 prep->next_buf_row, prep->next_buf_stop);
|
nuclear@0
|
239 }
|
nuclear@0
|
240 prep->next_buf_row = prep->next_buf_stop;
|
nuclear@0
|
241 }
|
nuclear@0
|
242 }
|
nuclear@0
|
243 /* If we've gotten enough data, downsample a row group. */
|
nuclear@0
|
244 if (prep->next_buf_row == prep->next_buf_stop) {
|
nuclear@0
|
245 (*cinfo->downsample->downsample) (cinfo,
|
nuclear@0
|
246 prep->color_buf,
|
nuclear@0
|
247 (JDIMENSION) prep->this_row_group,
|
nuclear@0
|
248 output_buf, *out_row_group_ctr);
|
nuclear@0
|
249 (*out_row_group_ctr)++;
|
nuclear@0
|
250 /* Advance pointers with wraparound as necessary. */
|
nuclear@0
|
251 prep->this_row_group += cinfo->max_v_samp_factor;
|
nuclear@0
|
252 if (prep->this_row_group >= buf_height)
|
nuclear@0
|
253 prep->this_row_group = 0;
|
nuclear@0
|
254 if (prep->next_buf_row >= buf_height)
|
nuclear@0
|
255 prep->next_buf_row = 0;
|
nuclear@0
|
256 prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
|
nuclear@0
|
257 }
|
nuclear@0
|
258 }
|
nuclear@0
|
259 }
|
nuclear@0
|
260
|
nuclear@0
|
261
|
nuclear@0
|
262 /*
|
nuclear@0
|
263 * Create the wrapped-around downsampling input buffer needed for context mode.
|
nuclear@0
|
264 */
|
nuclear@0
|
265
|
nuclear@0
|
266 LOCAL(void)
|
nuclear@0
|
267 create_context_buffer (j_compress_ptr cinfo)
|
nuclear@0
|
268 {
|
nuclear@0
|
269 my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
|
nuclear@0
|
270 int rgroup_height = cinfo->max_v_samp_factor;
|
nuclear@0
|
271 int ci, i;
|
nuclear@0
|
272 jpeg_component_info * compptr;
|
nuclear@0
|
273 JSAMPARRAY true_buffer, fake_buffer;
|
nuclear@0
|
274
|
nuclear@0
|
275 /* Grab enough space for fake row pointers for all the components;
|
nuclear@0
|
276 * we need five row groups' worth of pointers for each component.
|
nuclear@0
|
277 */
|
nuclear@0
|
278 fake_buffer = (JSAMPARRAY)
|
nuclear@0
|
279 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@0
|
280 (cinfo->num_components * 5 * rgroup_height) *
|
nuclear@0
|
281 SIZEOF(JSAMPROW));
|
nuclear@0
|
282
|
nuclear@0
|
283 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
nuclear@0
|
284 ci++, compptr++) {
|
nuclear@0
|
285 /* Allocate the actual buffer space (3 row groups) for this component.
|
nuclear@0
|
286 * We make the buffer wide enough to allow the downsampler to edge-expand
|
nuclear@0
|
287 * horizontally within the buffer, if it so chooses.
|
nuclear@0
|
288 */
|
nuclear@0
|
289 true_buffer = (*cinfo->mem->alloc_sarray)
|
nuclear@0
|
290 ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@0
|
291 (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
|
nuclear@0
|
292 cinfo->max_h_samp_factor) / compptr->h_samp_factor),
|
nuclear@0
|
293 (JDIMENSION) (3 * rgroup_height));
|
nuclear@0
|
294 /* Copy true buffer row pointers into the middle of the fake row array */
|
nuclear@0
|
295 MEMCOPY(fake_buffer + rgroup_height, true_buffer,
|
nuclear@0
|
296 3 * rgroup_height * SIZEOF(JSAMPROW));
|
nuclear@0
|
297 /* Fill in the above and below wraparound pointers */
|
nuclear@0
|
298 for (i = 0; i < rgroup_height; i++) {
|
nuclear@0
|
299 fake_buffer[i] = true_buffer[2 * rgroup_height + i];
|
nuclear@0
|
300 fake_buffer[4 * rgroup_height + i] = true_buffer[i];
|
nuclear@0
|
301 }
|
nuclear@0
|
302 prep->color_buf[ci] = fake_buffer + rgroup_height;
|
nuclear@0
|
303 fake_buffer += 5 * rgroup_height; /* point to space for next component */
|
nuclear@0
|
304 }
|
nuclear@0
|
305 }
|
nuclear@0
|
306
|
nuclear@0
|
307 #endif /* CONTEXT_ROWS_SUPPORTED */
|
nuclear@0
|
308
|
nuclear@0
|
309
|
nuclear@0
|
310 /*
|
nuclear@0
|
311 * Initialize preprocessing controller.
|
nuclear@0
|
312 */
|
nuclear@0
|
313
|
nuclear@0
|
314 GLOBAL(void)
|
nuclear@0
|
315 jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
|
nuclear@0
|
316 {
|
nuclear@0
|
317 my_prep_ptr prep;
|
nuclear@0
|
318 int ci;
|
nuclear@0
|
319 jpeg_component_info * compptr;
|
nuclear@0
|
320
|
nuclear@0
|
321 if (need_full_buffer) /* safety check */
|
nuclear@0
|
322 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
|
nuclear@0
|
323
|
nuclear@0
|
324 prep = (my_prep_ptr)
|
nuclear@0
|
325 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@0
|
326 SIZEOF(my_prep_controller));
|
nuclear@0
|
327 cinfo->prep = (struct jpeg_c_prep_controller *) prep;
|
nuclear@0
|
328 prep->pub.start_pass = start_pass_prep;
|
nuclear@0
|
329
|
nuclear@0
|
330 /* Allocate the color conversion buffer.
|
nuclear@0
|
331 * We make the buffer wide enough to allow the downsampler to edge-expand
|
nuclear@0
|
332 * horizontally within the buffer, if it so chooses.
|
nuclear@0
|
333 */
|
nuclear@0
|
334 if (cinfo->downsample->need_context_rows) {
|
nuclear@0
|
335 /* Set up to provide context rows */
|
nuclear@0
|
336 #ifdef CONTEXT_ROWS_SUPPORTED
|
nuclear@0
|
337 prep->pub.pre_process_data = pre_process_context;
|
nuclear@0
|
338 create_context_buffer(cinfo);
|
nuclear@0
|
339 #else
|
nuclear@0
|
340 ERREXIT(cinfo, JERR_NOT_COMPILED);
|
nuclear@0
|
341 #endif
|
nuclear@0
|
342 } else {
|
nuclear@0
|
343 /* No context, just make it tall enough for one row group */
|
nuclear@0
|
344 prep->pub.pre_process_data = pre_process_data;
|
nuclear@0
|
345 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
nuclear@0
|
346 ci++, compptr++) {
|
nuclear@0
|
347 prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
|
nuclear@0
|
348 ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@0
|
349 (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
|
nuclear@0
|
350 cinfo->max_h_samp_factor) / compptr->h_samp_factor),
|
nuclear@0
|
351 (JDIMENSION) cinfo->max_v_samp_factor);
|
nuclear@0
|
352 }
|
nuclear@0
|
353 }
|
nuclear@0
|
354 }
|