8 * Copyright (C) 1994-1998, 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.
12 * This file contains the compression preprocessing controller.
13 * This controller manages the color conversion, downsampling,
14 * and edge expansion steps.
16 * Most of the complexity here is associated with buffering input rows
17 * as required by the downsampler. See the comments at the head of
18 * jcsample.c for the downsampler's needs.
21 #define JPEG_INTERNALS
26 /* At present, jcsample.c can request context rows only for smoothing.
27 * In the future, we might also need context rows for CCIR601 sampling
28 * or other more-complex downsampling procedures. The code to support
29 * context rows should be compiled only if needed.
31 #ifdef INPUT_SMOOTHING_SUPPORTED
32 #define CONTEXT_ROWS_SUPPORTED
37 * For the simple (no-context-row) case, we just need to buffer one
38 * row group's worth of pixels for the downsampling step. At the bottom of
39 * the image, we pad to a full row group by replicating the last pixel row.
40 * The downsampler's last output row is then replicated if needed to pad
41 * out to a full iMCU row.
43 * When providing context rows, we must buffer three row groups' worth of
44 * pixels. Three row groups are physically allocated, but the row pointer
45 * arrays are made five row groups high, with the extra pointers above and
46 * below "wrapping around" to point to the last and first real row groups.
47 * This allows the downsampler to access the proper context rows.
48 * At the top and bottom of the image, we create dummy context rows by
49 * copying the first or last real pixel row. This copying could be avoided
50 * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
51 * trouble on the compression side.
55 /* Private buffer controller object */
58 struct jpeg_c_prep_controller pub
; /* public fields */
60 /* Downsampling input buffer. This buffer holds color-converted data
61 * until we have enough to do a downsample step.
63 JSAMPARRAY color_buf
[MAX_COMPONENTS
];
65 JDIMENSION rows_to_go
; /* counts rows remaining in source image */
66 int next_buf_row
; /* index of next row to store in color_buf */
68 #ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
69 int this_row_group
; /* starting row index of group to process */
70 int next_buf_stop
; /* downsample when we reach this index */
74 typedef my_prep_controller
* my_prep_ptr
;
78 * Initialize for a processing pass.
82 start_pass_prep (j_compress_ptr cinfo
, J_BUF_MODE pass_mode
)
84 my_prep_ptr prep
= (my_prep_ptr
) cinfo
->prep
;
86 if (pass_mode
!= JBUF_PASS_THRU
)
87 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
89 /* Initialize total-height counter for detecting bottom of image */
90 prep
->rows_to_go
= cinfo
->image_height
;
91 /* Mark the conversion buffer empty */
92 prep
->next_buf_row
= 0;
93 #ifdef CONTEXT_ROWS_SUPPORTED
94 /* Preset additional state variables for context mode.
95 * These aren't used in non-context mode, so we needn't test which mode.
97 prep
->this_row_group
= 0;
98 /* Set next_buf_stop to stop after two row groups have been read in. */
99 prep
->next_buf_stop
= 2 * cinfo
->max_v_samp_factor
;
105 * Expand an image vertically from height input_rows to height output_rows,
106 * by duplicating the bottom row.
110 expand_bottom_edge (JSAMPARRAY image_data
, JDIMENSION num_cols
,
111 int input_rows
, int output_rows
)
115 for (row
= input_rows
; row
< output_rows
; row
++) {
116 jcopy_sample_rows(image_data
, input_rows
-1, image_data
, row
,
123 * Process some data in the simple no-context case.
125 * Preprocessor output data is counted in "row groups". A row group
126 * is defined to be v_samp_factor sample rows of each component.
127 * Downsampling will produce this much data from each max_v_samp_factor
132 pre_process_data (j_compress_ptr cinfo
,
133 JSAMPARRAY input_buf
, JDIMENSION
*in_row_ctr
,
134 JDIMENSION in_rows_avail
,
135 JSAMPIMAGE output_buf
, JDIMENSION
*out_row_group_ctr
,
136 JDIMENSION out_row_groups_avail
)
138 my_prep_ptr prep
= (my_prep_ptr
) cinfo
->prep
;
141 jpeg_component_info
* compptr
;
143 while (*in_row_ctr
< in_rows_avail
&&
144 *out_row_group_ctr
< out_row_groups_avail
) {
145 /* Do color conversion to fill the conversion buffer. */
146 inrows
= in_rows_avail
- *in_row_ctr
;
147 numrows
= cinfo
->max_v_samp_factor
- prep
->next_buf_row
;
148 numrows
= (int) MIN((JDIMENSION
) numrows
, inrows
);
149 (*cinfo
->cconvert
->color_convert
) (cinfo
, input_buf
+ *in_row_ctr
,
151 (JDIMENSION
) prep
->next_buf_row
,
153 *in_row_ctr
+= numrows
;
154 prep
->next_buf_row
+= numrows
;
155 prep
->rows_to_go
-= numrows
;
156 /* If at bottom of image, pad to fill the conversion buffer. */
157 if (prep
->rows_to_go
== 0 &&
158 prep
->next_buf_row
< cinfo
->max_v_samp_factor
) {
159 for (ci
= 0; ci
< cinfo
->num_components
; ci
++) {
160 expand_bottom_edge(prep
->color_buf
[ci
], cinfo
->image_width
,
161 prep
->next_buf_row
, cinfo
->max_v_samp_factor
);
163 prep
->next_buf_row
= cinfo
->max_v_samp_factor
;
165 /* If we've filled the conversion buffer, empty it. */
166 if (prep
->next_buf_row
== cinfo
->max_v_samp_factor
) {
167 (*cinfo
->downsample
->downsample
) (cinfo
,
168 prep
->color_buf
, (JDIMENSION
) 0,
169 output_buf
, *out_row_group_ctr
);
170 prep
->next_buf_row
= 0;
171 (*out_row_group_ctr
)++;
173 /* If at bottom of image, pad the output to a full iMCU height.
174 * Note we assume the caller is providing a one-iMCU-height output buffer!
176 if (prep
->rows_to_go
== 0 &&
177 *out_row_group_ctr
< out_row_groups_avail
) {
178 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
180 expand_bottom_edge(output_buf
[ci
],
181 compptr
->width_in_data_units
* cinfo
->data_unit
,
182 (int) (*out_row_group_ctr
* compptr
->v_samp_factor
),
183 (int) (out_row_groups_avail
* compptr
->v_samp_factor
));
185 *out_row_group_ctr
= out_row_groups_avail
;
186 break; /* can exit outer loop without test */
192 #ifdef CONTEXT_ROWS_SUPPORTED
195 * Process some data in the context case.
199 pre_process_context (j_compress_ptr cinfo
,
200 JSAMPARRAY input_buf
, JDIMENSION
*in_row_ctr
,
201 JDIMENSION in_rows_avail
,
202 JSAMPIMAGE output_buf
, JDIMENSION
*out_row_group_ctr
,
203 JDIMENSION out_row_groups_avail
)
205 my_prep_ptr prep
= (my_prep_ptr
) cinfo
->prep
;
207 int buf_height
= cinfo
->max_v_samp_factor
* 3;
210 while (*out_row_group_ctr
< out_row_groups_avail
) {
211 if (*in_row_ctr
< in_rows_avail
) {
212 /* Do color conversion to fill the conversion buffer. */
213 inrows
= in_rows_avail
- *in_row_ctr
;
214 numrows
= prep
->next_buf_stop
- prep
->next_buf_row
;
215 numrows
= (int) MIN((JDIMENSION
) numrows
, inrows
);
216 (*cinfo
->cconvert
->color_convert
) (cinfo
, input_buf
+ *in_row_ctr
,
218 (JDIMENSION
) prep
->next_buf_row
,
220 /* Pad at top of image, if first time through */
221 if (prep
->rows_to_go
== cinfo
->image_height
) {
222 for (ci
= 0; ci
< cinfo
->num_components
; ci
++) {
224 for (row
= 1; row
<= cinfo
->max_v_samp_factor
; row
++) {
225 jcopy_sample_rows(prep
->color_buf
[ci
], 0,
226 prep
->color_buf
[ci
], -row
,
227 1, cinfo
->image_width
);
231 *in_row_ctr
+= numrows
;
232 prep
->next_buf_row
+= numrows
;
233 prep
->rows_to_go
-= numrows
;
235 /* Return for more data, unless we are at the bottom of the image. */
236 if (prep
->rows_to_go
!= 0)
238 /* When at bottom of image, pad to fill the conversion buffer. */
239 if (prep
->next_buf_row
< prep
->next_buf_stop
) {
240 for (ci
= 0; ci
< cinfo
->num_components
; ci
++) {
241 expand_bottom_edge(prep
->color_buf
[ci
], cinfo
->image_width
,
242 prep
->next_buf_row
, prep
->next_buf_stop
);
244 prep
->next_buf_row
= prep
->next_buf_stop
;
247 /* If we've gotten enough data, downsample a row group. */
248 if (prep
->next_buf_row
== prep
->next_buf_stop
) {
249 (*cinfo
->downsample
->downsample
) (cinfo
,
251 (JDIMENSION
) prep
->this_row_group
,
252 output_buf
, *out_row_group_ctr
);
253 (*out_row_group_ctr
)++;
254 /* Advance pointers with wraparound as necessary. */
255 prep
->this_row_group
+= cinfo
->max_v_samp_factor
;
256 if (prep
->this_row_group
>= buf_height
)
257 prep
->this_row_group
= 0;
258 if (prep
->next_buf_row
>= buf_height
)
259 prep
->next_buf_row
= 0;
260 prep
->next_buf_stop
= prep
->next_buf_row
+ cinfo
->max_v_samp_factor
;
267 * Create the wrapped-around downsampling input buffer needed for context mode.
271 create_context_buffer (j_compress_ptr cinfo
)
273 my_prep_ptr prep
= (my_prep_ptr
) cinfo
->prep
;
274 int rgroup_height
= cinfo
->max_v_samp_factor
;
276 jpeg_component_info
* compptr
;
277 JSAMPARRAY true_buffer
, fake_buffer
;
279 /* Grab enough space for fake row pointers for all the components;
280 * we need five row groups' worth of pointers for each component.
282 fake_buffer
= (JSAMPARRAY
)
283 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
284 (cinfo
->num_components
* 5 * rgroup_height
) *
287 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
289 /* Allocate the actual buffer space (3 row groups) for this component.
290 * We make the buffer wide enough to allow the downsampler to edge-expand
291 * horizontally within the buffer, if it so chooses.
293 true_buffer
= (*cinfo
->mem
->alloc_sarray
)
294 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
295 (JDIMENSION
) (((long) compptr
->width_in_data_units
* cinfo
->data_unit
*
296 cinfo
->max_h_samp_factor
) / compptr
->h_samp_factor
),
297 (JDIMENSION
) (3 * rgroup_height
));
298 /* Copy true buffer row pointers into the middle of the fake row array */
299 MEMCOPY(fake_buffer
+ rgroup_height
, true_buffer
,
300 3 * rgroup_height
* SIZEOF(JSAMPROW
));
301 /* Fill in the above and below wraparound pointers */
302 for (i
= 0; i
< rgroup_height
; i
++) {
303 fake_buffer
[i
] = true_buffer
[2 * rgroup_height
+ i
];
304 fake_buffer
[4 * rgroup_height
+ i
] = true_buffer
[i
];
306 prep
->color_buf
[ci
] = fake_buffer
+ rgroup_height
;
307 fake_buffer
+= 5 * rgroup_height
; /* point to space for next component */
311 #endif /* CONTEXT_ROWS_SUPPORTED */
315 * Initialize preprocessing controller.
319 jinit_c_prep_controller (j_compress_ptr cinfo
, boolean need_full_buffer
)
323 jpeg_component_info
* compptr
;
325 if (need_full_buffer
) /* safety check */
326 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
329 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
330 SIZEOF(my_prep_controller
));
331 cinfo
->prep
= (struct jpeg_c_prep_controller
*) prep
;
332 prep
->pub
.start_pass
= start_pass_prep
;
334 /* Allocate the color conversion buffer.
335 * We make the buffer wide enough to allow the downsampler to edge-expand
336 * horizontally within the buffer, if it so chooses.
338 if (cinfo
->downsample
->need_context_rows
) {
339 /* Set up to provide context rows */
340 #ifdef CONTEXT_ROWS_SUPPORTED
341 prep
->pub
.pre_process_data
= pre_process_context
;
342 create_context_buffer(cinfo
);
344 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
347 /* No context, just make it tall enough for one row group */
348 prep
->pub
.pre_process_data
= pre_process_data
;
349 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
351 prep
->color_buf
[ci
] = (*cinfo
->mem
->alloc_sarray
)
352 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
353 (JDIMENSION
) (((long) compptr
->width_in_data_units
* cinfo
->data_unit
*
354 cinfo
->max_h_samp_factor
) / compptr
->h_samp_factor
),
355 (JDIMENSION
) cinfo
->max_v_samp_factor
);