d3d8/tests: Test the presentation parameters after creating an additional swap chain.
[wine.git] / libs / jpeg / jcprepct.c
blob586964bd446aac0c36e66c9fd7c9ec5e73e8ed8c
1 /*
2 * jcprepct.c
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * Modified 2003-2020 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
9 * This file contains the compression preprocessing controller.
10 * This controller manages the color conversion, downsampling,
11 * and edge expansion steps.
13 * Most of the complexity here is associated with buffering input rows
14 * as required by the downsampler. See the comments at the head of
15 * jcsample.c for the downsampler's needs.
18 #define JPEG_INTERNALS
19 #include "jinclude.h"
20 #include "jpeglib.h"
23 /* At present, jcsample.c can request context rows only for smoothing.
24 * In the future, we might also need context rows for CCIR601 sampling
25 * or other more-complex downsampling procedures. The code to support
26 * context rows should be compiled only if needed.
28 #ifdef INPUT_SMOOTHING_SUPPORTED
29 #define CONTEXT_ROWS_SUPPORTED
30 #endif
34 * For the simple (no-context-row) case, we just need to buffer one
35 * row group's worth of pixels for the downsampling step. At the bottom of
36 * the image, we pad to a full row group by replicating the last pixel row.
37 * The downsampler's last output row is then replicated if needed to pad
38 * out to a full iMCU row.
40 * When providing context rows, we must buffer three row groups' worth of
41 * pixels. Three row groups are physically allocated, but the row pointer
42 * arrays are made five row groups high, with the extra pointers above and
43 * below "wrapping around" to point to the last and first real row groups.
44 * This allows the downsampler to access the proper context rows.
45 * At the top and bottom of the image, we create dummy context rows by
46 * copying the first or last real pixel row. This copying could be avoided
47 * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
48 * trouble on the compression side.
52 /* Private buffer controller object */
54 typedef struct {
55 struct jpeg_c_prep_controller pub; /* public fields */
57 /* Downsampling input buffer. This buffer holds color-converted data
58 * until we have enough to do a downsample step.
60 JSAMPARRAY color_buf[MAX_COMPONENTS];
62 JDIMENSION rows_to_go; /* counts rows remaining in source image */
63 int next_buf_row; /* index of next row to store in color_buf */
65 #ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
66 int this_row_group; /* starting row index of group to process */
67 int next_buf_stop; /* downsample when we reach this index */
68 #endif
69 } my_prep_controller;
71 typedef my_prep_controller * my_prep_ptr;
75 * Initialize for a processing pass.
78 METHODDEF(void)
79 start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
81 my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
83 if (pass_mode != JBUF_PASS_THRU)
84 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
86 /* Initialize total-height counter for detecting bottom of image */
87 prep->rows_to_go = cinfo->image_height;
88 /* Mark the conversion buffer empty */
89 prep->next_buf_row = 0;
90 #ifdef CONTEXT_ROWS_SUPPORTED
91 /* Preset additional state variables for context mode.
92 * These aren't used in non-context mode, so we needn't test which mode.
94 prep->this_row_group = 0;
95 /* Set next_buf_stop to stop after two row groups have been read in. */
96 prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
97 #endif
102 * Expand an image vertically from height input_rows to height output_rows,
103 * by duplicating the bottom row.
106 LOCAL(void)
107 expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
108 int input_rows, int output_rows)
110 register int row;
112 for (row = input_rows; row < output_rows; row++) {
113 jcopy_sample_rows(image_data + input_rows - 1,
114 image_data + row,
115 1, num_cols);
121 * Process some data in the simple no-context case.
123 * Preprocessor output data is counted in "row groups". A row group
124 * is defined to be v_samp_factor sample rows of each component.
125 * Downsampling will produce this much data from each max_v_samp_factor
126 * input rows.
129 METHODDEF(void)
130 pre_process_data (j_compress_ptr cinfo,
131 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
132 JDIMENSION in_rows_avail,
133 JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
134 JDIMENSION out_row_groups_avail)
136 my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
137 int numrows, ci;
138 JDIMENSION inrows;
139 jpeg_component_info * compptr;
141 while (*in_row_ctr < in_rows_avail &&
142 *out_row_group_ctr < out_row_groups_avail) {
143 /* Do color conversion to fill the conversion buffer. */
144 inrows = in_rows_avail - *in_row_ctr;
145 numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
146 numrows = (int) MIN((JDIMENSION) numrows, inrows);
147 (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
148 prep->color_buf,
149 (JDIMENSION) prep->next_buf_row,
150 numrows);
151 *in_row_ctr += numrows;
152 prep->next_buf_row += numrows;
153 prep->rows_to_go -= numrows;
154 /* If at bottom of image, pad to fill the conversion buffer. */
155 if (prep->rows_to_go == 0 &&
156 prep->next_buf_row < cinfo->max_v_samp_factor) {
157 for (ci = 0; ci < cinfo->num_components; ci++) {
158 expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
159 prep->next_buf_row, cinfo->max_v_samp_factor);
161 prep->next_buf_row = cinfo->max_v_samp_factor;
163 /* If we've filled the conversion buffer, empty it. */
164 if (prep->next_buf_row == cinfo->max_v_samp_factor) {
165 (*cinfo->downsample->downsample) (cinfo,
166 prep->color_buf, (JDIMENSION) 0,
167 output_buf, *out_row_group_ctr);
168 prep->next_buf_row = 0;
169 (*out_row_group_ctr)++;
171 /* If at bottom of image, pad the output to a full iMCU height.
172 * Note we assume the caller is providing a one-iMCU-height output buffer!
174 if (prep->rows_to_go == 0 &&
175 *out_row_group_ctr < out_row_groups_avail) {
176 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
177 ci++, compptr++) {
178 numrows = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
179 cinfo->min_DCT_v_scaled_size;
180 expand_bottom_edge(output_buf[ci],
181 compptr->width_in_blocks * compptr->DCT_h_scaled_size,
182 (int) (*out_row_group_ctr * numrows),
183 (int) (out_row_groups_avail * numrows));
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.
198 METHODDEF(void)
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;
206 int numrows, ci;
207 int buf_height = cinfo->max_v_samp_factor * 3;
208 JDIMENSION inrows;
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,
217 prep->color_buf,
218 (JDIMENSION) prep->next_buf_row,
219 numrows);
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++) {
223 int row;
224 for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
225 jcopy_sample_rows(prep->color_buf[ci],
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;
234 } else {
235 /* Return for more data, unless we are at the bottom of the image. */
236 if (prep->rows_to_go != 0)
237 break;
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,
250 prep->color_buf,
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.
270 LOCAL(void)
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;
275 int ci, i;
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) (*cinfo->mem->alloc_small)
283 ((j_common_ptr) cinfo, JPOOL_IMAGE,
284 (cinfo->num_components * 5 * rgroup_height) * SIZEOF(JSAMPROW));
286 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
287 ci++, compptr++) {
288 /* Allocate the actual buffer space (3 row groups) for this component.
289 * We make the buffer wide enough to allow the downsampler to edge-expand
290 * horizontally within the buffer, if it so chooses.
292 true_buffer = (*cinfo->mem->alloc_sarray)
293 ((j_common_ptr) cinfo, JPOOL_IMAGE,
294 (JDIMENSION) (((long) compptr->width_in_blocks *
295 cinfo->min_DCT_h_scaled_size *
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.
318 GLOBAL(void)
319 jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
321 my_prep_ptr prep;
322 int ci;
323 jpeg_component_info * compptr;
325 if (need_full_buffer) /* safety check */
326 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
328 prep = (my_prep_ptr) (*cinfo->mem->alloc_small)
329 ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_prep_controller));
330 cinfo->prep = &prep->pub;
331 prep->pub.start_pass = start_pass_prep;
333 /* Allocate the color conversion buffer.
334 * We make the buffer wide enough to allow the downsampler to edge-expand
335 * horizontally within the buffer, if it so chooses.
337 if (cinfo->downsample->need_context_rows) {
338 /* Set up to provide context rows */
339 #ifdef CONTEXT_ROWS_SUPPORTED
340 prep->pub.pre_process_data = pre_process_context;
341 create_context_buffer(cinfo);
342 #else
343 ERREXIT(cinfo, JERR_NOT_COMPILED);
344 #endif
345 } else {
346 /* No context, just make it tall enough for one row group */
347 prep->pub.pre_process_data = pre_process_data;
348 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
349 ci++, compptr++) {
350 prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
351 ((j_common_ptr) cinfo, JPOOL_IMAGE,
352 (JDIMENSION) (((long) compptr->width_in_blocks *
353 cinfo->min_DCT_h_scaled_size *
354 cinfo->max_h_samp_factor) / compptr->h_samp_factor),
355 (JDIMENSION) cinfo->max_v_samp_factor);