jdk
358 строк · 12.8 Кб
1/*
2* reserved comment block
3* DO NOT REMOVE OR ALTER!
4*/
5/*
6* jcprepct.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 compression preprocessing controller.
13* This controller manages the color conversion, downsampling,
14* and edge expansion steps.
15*
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.
19*/
20
21#define JPEG_INTERNALS
22#include "jinclude.h"
23#include "jpeglib.h"
24
25
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.
30*/
31#ifdef INPUT_SMOOTHING_SUPPORTED
32#define CONTEXT_ROWS_SUPPORTED
33#endif
34
35
36/*
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.
42*
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.
52*/
53
54
55/* Private buffer controller object */
56
57typedef struct {
58struct jpeg_c_prep_controller pub; /* public fields */
59
60/* Downsampling input buffer. This buffer holds color-converted data
61* until we have enough to do a downsample step.
62*/
63JSAMPARRAY color_buf[MAX_COMPONENTS];
64
65JDIMENSION rows_to_go; /* counts rows remaining in source image */
66int next_buf_row; /* index of next row to store in color_buf */
67
68#ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
69int this_row_group; /* starting row index of group to process */
70int next_buf_stop; /* downsample when we reach this index */
71#endif
72} my_prep_controller;
73
74typedef my_prep_controller * my_prep_ptr;
75
76
77/*
78* Initialize for a processing pass.
79*/
80
81METHODDEF(void)
82start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
83{
84my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
85
86if (pass_mode != JBUF_PASS_THRU)
87ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
88
89/* Initialize total-height counter for detecting bottom of image */
90prep->rows_to_go = cinfo->image_height;
91/* Mark the conversion buffer empty */
92prep->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.
96*/
97prep->this_row_group = 0;
98/* Set next_buf_stop to stop after two row groups have been read in. */
99prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
100#endif
101}
102
103
104/*
105* Expand an image vertically from height input_rows to height output_rows,
106* by duplicating the bottom row.
107*/
108
109LOCAL(void)
110expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
111int input_rows, int output_rows)
112{
113register int row;
114
115for (row = input_rows; row < output_rows; row++) {
116jcopy_sample_rows(image_data, input_rows-1, image_data, row,
1171, num_cols);
118}
119}
120
121
122/*
123* Process some data in the simple no-context case.
124*
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
128* input rows.
129*/
130
131METHODDEF(void)
132pre_process_data (j_compress_ptr cinfo,
133JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
134JDIMENSION in_rows_avail,
135JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
136JDIMENSION out_row_groups_avail)
137{
138my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
139int numrows, ci;
140JDIMENSION inrows;
141jpeg_component_info * compptr;
142
143while (*in_row_ctr < in_rows_avail &&
144*out_row_group_ctr < out_row_groups_avail) {
145/* Do color conversion to fill the conversion buffer. */
146inrows = in_rows_avail - *in_row_ctr;
147numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
148numrows = (int) MIN((JDIMENSION) numrows, inrows);
149(*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
150prep->color_buf,
151(JDIMENSION) prep->next_buf_row,
152numrows);
153*in_row_ctr += numrows;
154prep->next_buf_row += numrows;
155prep->rows_to_go -= numrows;
156/* If at bottom of image, pad to fill the conversion buffer. */
157if (prep->rows_to_go == 0 &&
158prep->next_buf_row < cinfo->max_v_samp_factor) {
159for (ci = 0; ci < cinfo->num_components; ci++) {
160expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
161prep->next_buf_row, cinfo->max_v_samp_factor);
162}
163prep->next_buf_row = cinfo->max_v_samp_factor;
164}
165/* If we've filled the conversion buffer, empty it. */
166if (prep->next_buf_row == cinfo->max_v_samp_factor) {
167(*cinfo->downsample->downsample) (cinfo,
168prep->color_buf, (JDIMENSION) 0,
169output_buf, *out_row_group_ctr);
170prep->next_buf_row = 0;
171(*out_row_group_ctr)++;
172}
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!
175*/
176if (prep->rows_to_go == 0 &&
177*out_row_group_ctr < out_row_groups_avail) {
178for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
179ci++, compptr++) {
180expand_bottom_edge(output_buf[ci],
181compptr->width_in_blocks * DCTSIZE,
182(int) (*out_row_group_ctr * compptr->v_samp_factor),
183(int) (out_row_groups_avail * compptr->v_samp_factor));
184}
185*out_row_group_ctr = out_row_groups_avail;
186break; /* can exit outer loop without test */
187}
188}
189}
190
191
192#ifdef CONTEXT_ROWS_SUPPORTED
193
194/*
195* Process some data in the context case.
196*/
197
198METHODDEF(void)
199pre_process_context (j_compress_ptr cinfo,
200JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
201JDIMENSION in_rows_avail,
202JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
203JDIMENSION out_row_groups_avail)
204{
205my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
206int numrows, ci;
207int buf_height = cinfo->max_v_samp_factor * 3;
208JDIMENSION inrows;
209
210while (*out_row_group_ctr < out_row_groups_avail) {
211if (*in_row_ctr < in_rows_avail) {
212/* Do color conversion to fill the conversion buffer. */
213inrows = in_rows_avail - *in_row_ctr;
214numrows = prep->next_buf_stop - prep->next_buf_row;
215numrows = (int) MIN((JDIMENSION) numrows, inrows);
216(*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
217prep->color_buf,
218(JDIMENSION) prep->next_buf_row,
219numrows);
220/* Pad at top of image, if first time through */
221if (prep->rows_to_go == cinfo->image_height) {
222for (ci = 0; ci < cinfo->num_components; ci++) {
223int row;
224for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
225jcopy_sample_rows(prep->color_buf[ci], 0,
226prep->color_buf[ci], -row,
2271, cinfo->image_width);
228}
229}
230}
231*in_row_ctr += numrows;
232prep->next_buf_row += numrows;
233prep->rows_to_go -= numrows;
234} else {
235/* Return for more data, unless we are at the bottom of the image. */
236if (prep->rows_to_go != 0)
237break;
238/* When at bottom of image, pad to fill the conversion buffer. */
239if (prep->next_buf_row < prep->next_buf_stop) {
240for (ci = 0; ci < cinfo->num_components; ci++) {
241expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
242prep->next_buf_row, prep->next_buf_stop);
243}
244prep->next_buf_row = prep->next_buf_stop;
245}
246}
247/* If we've gotten enough data, downsample a row group. */
248if (prep->next_buf_row == prep->next_buf_stop) {
249(*cinfo->downsample->downsample) (cinfo,
250prep->color_buf,
251(JDIMENSION) prep->this_row_group,
252output_buf, *out_row_group_ctr);
253(*out_row_group_ctr)++;
254/* Advance pointers with wraparound as necessary. */
255prep->this_row_group += cinfo->max_v_samp_factor;
256if (prep->this_row_group >= buf_height)
257prep->this_row_group = 0;
258if (prep->next_buf_row >= buf_height)
259prep->next_buf_row = 0;
260prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
261}
262}
263}
264
265
266/*
267* Create the wrapped-around downsampling input buffer needed for context mode.
268*/
269
270LOCAL(void)
271create_context_buffer (j_compress_ptr cinfo)
272{
273my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
274int rgroup_height = cinfo->max_v_samp_factor;
275int ci, i;
276jpeg_component_info * compptr;
277JSAMPARRAY true_buffer, fake_buffer;
278
279/* Grab enough space for fake row pointers for all the components;
280* we need five row groups' worth of pointers for each component.
281*/
282fake_buffer = (JSAMPARRAY)
283(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
284(cinfo->num_components * 5 * rgroup_height) *
285SIZEOF(JSAMPROW));
286
287for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
288ci++, compptr++) {
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.
292*/
293true_buffer = (*cinfo->mem->alloc_sarray)
294((j_common_ptr) cinfo, JPOOL_IMAGE,
295(JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
296cinfo->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 */
299MEMCOPY(fake_buffer + rgroup_height, true_buffer,
3003 * rgroup_height * SIZEOF(JSAMPROW));
301/* Fill in the above and below wraparound pointers */
302for (i = 0; i < rgroup_height; i++) {
303fake_buffer[i] = true_buffer[2 * rgroup_height + i];
304fake_buffer[4 * rgroup_height + i] = true_buffer[i];
305}
306prep->color_buf[ci] = fake_buffer + rgroup_height;
307fake_buffer += 5 * rgroup_height; /* point to space for next component */
308}
309}
310
311#endif /* CONTEXT_ROWS_SUPPORTED */
312
313
314/*
315* Initialize preprocessing controller.
316*/
317
318GLOBAL(void)
319jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
320{
321my_prep_ptr prep;
322int ci;
323jpeg_component_info * compptr;
324
325if (need_full_buffer) /* safety check */
326ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
327
328prep = (my_prep_ptr)
329(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
330SIZEOF(my_prep_controller));
331cinfo->prep = (struct jpeg_c_prep_controller *) prep;
332prep->pub.start_pass = start_pass_prep;
333
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.
337*/
338if (cinfo->downsample->need_context_rows) {
339/* Set up to provide context rows */
340#ifdef CONTEXT_ROWS_SUPPORTED
341prep->pub.pre_process_data = pre_process_context;
342create_context_buffer(cinfo);
343#else
344ERREXIT(cinfo, JERR_NOT_COMPILED);
345#endif
346} else {
347/* No context, just make it tall enough for one row group */
348prep->pub.pre_process_data = pre_process_data;
349for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
350ci++, compptr++) {
351prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
352((j_common_ptr) cinfo, JPOOL_IMAGE,
353(JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
354cinfo->max_h_samp_factor) / compptr->h_samp_factor),
355(JDIMENSION) cinfo->max_v_samp_factor);
356}
357}
358}
359