imm32/tests: Don't expect IME window if the parent is message-only window descendant.
[wine.git] / libs / jpeg / jccoefct.c
blob77851f390e1854af0674339c5ea8fb0698c8024c
1 /*
2 * jccoefct.c
4 * Copyright (C) 1994-1997, 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 coefficient buffer controller for compression.
10 * This controller is the top level of the JPEG compressor proper.
11 * The coefficient buffer lies between forward-DCT and entropy encoding steps.
14 #define JPEG_INTERNALS
15 #include "jinclude.h"
16 #include "jpeglib.h"
19 /* We use a full-image coefficient buffer when doing Huffman optimization,
20 * and also for writing multiple-scan JPEG files. In all cases, the DCT
21 * step is run during the first pass, and subsequent passes need only read
22 * the buffered coefficients.
24 #ifdef ENTROPY_OPT_SUPPORTED
25 #define FULL_COEF_BUFFER_SUPPORTED
26 #else
27 #ifdef C_MULTISCAN_FILES_SUPPORTED
28 #define FULL_COEF_BUFFER_SUPPORTED
29 #endif
30 #endif
33 /* Private buffer controller object */
35 typedef struct {
36 struct jpeg_c_coef_controller pub; /* public fields */
38 JDIMENSION iMCU_row_num; /* iMCU row # within image */
39 JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
40 int MCU_vert_offset; /* counts MCU rows within iMCU row */
41 int MCU_rows_per_iMCU_row; /* number of such rows needed */
43 /* For single-pass compression, it's sufficient to buffer just one MCU
44 * (although this may prove a bit slow in practice). We append a
45 * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it
46 * for each MCU constructed and sent.
47 * In multi-pass modes, this array points to the current MCU's blocks
48 * within the virtual arrays.
50 JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
52 /* In multi-pass modes, we need a virtual block array for each component. */
53 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
55 /* Workspace for single-pass compression (omitted otherwise). */
56 JBLOCK blk_buffer[C_MAX_BLOCKS_IN_MCU];
57 } my_coef_controller;
59 typedef my_coef_controller * my_coef_ptr;
62 /* Forward declarations */
63 METHODDEF(boolean) compress_data
64 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
65 #ifdef FULL_COEF_BUFFER_SUPPORTED
66 METHODDEF(boolean) compress_first_pass
67 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
68 METHODDEF(boolean) compress_output
69 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
70 #endif
73 LOCAL(void)
74 start_iMCU_row (j_compress_ptr cinfo)
75 /* Reset within-iMCU-row counters for a new row */
77 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
79 /* In an interleaved scan, an MCU row is the same as an iMCU row.
80 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
81 * But at the bottom of the image, process only what's left.
83 if (cinfo->comps_in_scan > 1) {
84 coef->MCU_rows_per_iMCU_row = 1;
85 } else {
86 if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
87 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
88 else
89 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
92 coef->MCU_ctr = 0;
93 coef->MCU_vert_offset = 0;
98 * Initialize for a processing pass.
101 METHODDEF(void)
102 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
104 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
106 coef->iMCU_row_num = 0;
107 start_iMCU_row(cinfo);
109 switch (pass_mode) {
110 case JBUF_PASS_THRU:
111 if (coef->whole_image[0] != NULL)
112 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
113 coef->pub.compress_data = compress_data;
114 break;
115 #ifdef FULL_COEF_BUFFER_SUPPORTED
116 case JBUF_SAVE_AND_PASS:
117 if (coef->whole_image[0] == NULL)
118 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
119 coef->pub.compress_data = compress_first_pass;
120 break;
121 case JBUF_CRANK_DEST:
122 if (coef->whole_image[0] == NULL)
123 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
124 coef->pub.compress_data = compress_output;
125 break;
126 #endif
127 default:
128 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
134 * Process some data in the single-pass case.
135 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
136 * per call, ie, v_samp_factor block rows for each component in the image.
137 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
139 * NB: input_buf contains a plane for each component in image,
140 * which we index according to the component's SOF position.
143 METHODDEF(boolean)
144 compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
146 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
147 JDIMENSION MCU_col_num; /* index of current MCU within row */
148 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
149 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
150 int ci, xindex, yindex, yoffset, blockcnt;
151 JBLOCKROW blkp;
152 JSAMPARRAY input_ptr;
153 JDIMENSION xpos;
154 jpeg_component_info *compptr;
155 forward_DCT_ptr forward_DCT;
157 /* Loop to write as much as one whole iMCU row */
158 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
159 yoffset++) {
160 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
161 MCU_col_num++) {
162 /* Determine where data comes from in input_buf and do the DCT thing.
163 * Each call on forward_DCT processes a horizontal row of DCT blocks as
164 * wide as an MCU. Dummy blocks at the right or bottom edge are filled in
165 * specially. The data in them does not matter for image reconstruction,
166 * so we fill them with values that will encode to the smallest amount of
167 * data, viz: all zeroes in the AC entries, DC entries equal to previous
168 * block's DC value. (Thanks to Thomas Kinsman for this idea.)
170 blkp = coef->blk_buffer; /* pointer to current DCT block within MCU */
171 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172 compptr = cinfo->cur_comp_info[ci];
173 forward_DCT = cinfo->fdct->forward_DCT[compptr->component_index];
174 input_ptr = input_buf[compptr->component_index] +
175 yoffset * compptr->DCT_v_scaled_size;
176 /* ypos == (yoffset + yindex) * compptr->DCT_v_scaled_size */
177 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
178 : compptr->last_col_width;
179 xpos = MCU_col_num * compptr->MCU_sample_width;
180 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
181 if (coef->iMCU_row_num < last_iMCU_row ||
182 yoffset + yindex < compptr->last_row_height) {
183 (*forward_DCT) (cinfo, compptr, input_ptr, blkp,
184 xpos, (JDIMENSION) blockcnt);
185 input_ptr += compptr->DCT_v_scaled_size;
186 blkp += blockcnt;
187 /* Dummy blocks at right edge */
188 if ((xindex = compptr->MCU_width - blockcnt) == 0)
189 continue;
190 } else {
191 /* At bottom of image, need a whole row of dummy blocks */
192 xindex = compptr->MCU_width;
194 /* Fill in any dummy blocks needed in this row */
195 MEMZERO(blkp, xindex * SIZEOF(JBLOCK));
196 do {
197 blkp[0][0] = blkp[-1][0];
198 blkp++;
199 } while (--xindex);
202 /* Try to write the MCU. In event of a suspension failure, we will
203 * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
205 if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
206 /* Suspension forced; update state counters and exit */
207 coef->MCU_vert_offset = yoffset;
208 coef->MCU_ctr = MCU_col_num;
209 return FALSE;
212 /* Completed an MCU row, but perhaps not an iMCU row */
213 coef->MCU_ctr = 0;
215 /* Completed the iMCU row, advance counters for next one */
216 coef->iMCU_row_num++;
217 start_iMCU_row(cinfo);
218 return TRUE;
222 #ifdef FULL_COEF_BUFFER_SUPPORTED
225 * Process some data in the first pass of a multi-pass case.
226 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
227 * per call, ie, v_samp_factor block rows for each component in the image.
228 * This amount of data is read from the source buffer, DCT'd and quantized,
229 * and saved into the virtual arrays. We also generate suitable dummy blocks
230 * as needed at the right and lower edges. (The dummy blocks are constructed
231 * in the virtual arrays, which have been padded appropriately.) This makes
232 * it possible for subsequent passes not to worry about real vs. dummy blocks.
234 * We must also emit the data to the entropy encoder. This is conveniently
235 * done by calling compress_output() after we've loaded the current strip
236 * of the virtual arrays.
238 * NB: input_buf contains a plane for each component in image. All
239 * components are DCT'd and loaded into the virtual arrays in this pass.
240 * However, it may be that only a subset of the components are emitted to
241 * the entropy encoder during this first pass; be careful about looking
242 * at the scan-dependent variables (MCU dimensions, etc).
245 METHODDEF(boolean)
246 compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
248 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
249 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
250 JDIMENSION blocks_across, MCUs_across, MCUindex;
251 int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
252 JCOEF lastDC;
253 jpeg_component_info *compptr;
254 JBLOCKARRAY buffer;
255 JBLOCKROW thisblockrow, lastblockrow;
256 JSAMPARRAY input_ptr;
257 forward_DCT_ptr forward_DCT;
259 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
260 ci++, compptr++) {
261 /* Align the virtual buffer for this component. */
262 buffer = (*cinfo->mem->access_virt_barray)
263 ((j_common_ptr) cinfo, coef->whole_image[ci],
264 coef->iMCU_row_num * compptr->v_samp_factor,
265 (JDIMENSION) compptr->v_samp_factor, TRUE);
266 /* Count non-dummy DCT block rows in this iMCU row. */
267 if (coef->iMCU_row_num < last_iMCU_row)
268 block_rows = compptr->v_samp_factor;
269 else {
270 /* NB: can't use last_row_height here, since may not be set! */
271 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
272 if (block_rows == 0) block_rows = compptr->v_samp_factor;
274 blocks_across = compptr->width_in_blocks;
275 h_samp_factor = compptr->h_samp_factor;
276 /* Count number of dummy blocks to be added at the right margin. */
277 ndummy = (int) (blocks_across % h_samp_factor);
278 if (ndummy > 0)
279 ndummy = h_samp_factor - ndummy;
280 forward_DCT = cinfo->fdct->forward_DCT[ci];
281 input_ptr = input_buf[ci];
282 /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
283 * on forward_DCT processes a complete horizontal row of DCT blocks.
285 for (block_row = 0; block_row < block_rows; block_row++) {
286 thisblockrow = buffer[block_row];
287 (*forward_DCT) (cinfo, compptr, input_ptr, thisblockrow,
288 (JDIMENSION) 0, blocks_across);
289 input_ptr += compptr->DCT_v_scaled_size;
290 if (ndummy > 0) {
291 /* Create dummy blocks at the right edge of the image. */
292 thisblockrow += blocks_across; /* => first dummy block */
293 FMEMZERO((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
294 lastDC = thisblockrow[-1][0];
295 for (bi = 0; bi < ndummy; bi++) {
296 thisblockrow[bi][0] = lastDC;
300 /* If at end of image, create dummy block rows as needed.
301 * The tricky part here is that within each MCU, we want the DC values
302 * of the dummy blocks to match the last real block's DC value.
303 * This squeezes a few more bytes out of the resulting file...
305 if (block_row < compptr->v_samp_factor) {
306 blocks_across += ndummy; /* include lower right corner */
307 MCUs_across = blocks_across / h_samp_factor;
308 do {
309 thisblockrow = buffer[block_row];
310 lastblockrow = buffer[block_row-1];
311 FMEMZERO((void FAR *) thisblockrow,
312 (size_t) blocks_across * SIZEOF(JBLOCK));
313 for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
314 lastDC = lastblockrow[h_samp_factor-1][0];
315 for (bi = 0; bi < h_samp_factor; bi++) {
316 thisblockrow[bi][0] = lastDC;
318 thisblockrow += h_samp_factor; /* advance to next MCU in row */
319 lastblockrow += h_samp_factor;
321 } while (++block_row < compptr->v_samp_factor);
324 /* NB: compress_output will increment iMCU_row_num if successful.
325 * A suspension return will result in redoing all the work above next time.
328 /* Emit data to the entropy encoder, sharing code with subsequent passes */
329 return compress_output(cinfo, input_buf);
334 * Process some data in subsequent passes of a multi-pass case.
335 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
336 * per call, ie, v_samp_factor block rows for each component in the scan.
337 * The data is obtained from the virtual arrays and fed to the entropy coder.
338 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
340 * NB: input_buf is ignored; it is likely to be a NULL pointer.
343 METHODDEF(boolean)
344 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
346 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
347 JDIMENSION MCU_col_num; /* index of current MCU within row */
348 int ci, xindex, yindex, yoffset;
349 JDIMENSION start_col;
350 JBLOCKARRAY blkp;
351 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
352 JBLOCKROW buffer_ptr;
353 jpeg_component_info *compptr;
355 /* Align the virtual buffers for the components used in this scan.
356 * NB: during first pass, this is safe only because the buffers will
357 * already be aligned properly, so jmemmgr.c won't need to do any I/O.
359 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
360 compptr = cinfo->cur_comp_info[ci];
361 buffer[ci] = (*cinfo->mem->access_virt_barray)
362 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
363 coef->iMCU_row_num * compptr->v_samp_factor,
364 (JDIMENSION) compptr->v_samp_factor, FALSE);
367 /* Loop to process one whole iMCU row */
368 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
369 yoffset++) {
370 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
371 MCU_col_num++) {
372 /* Construct list of pointers to DCT blocks belonging to this MCU */
373 blkp = coef->MCU_buffer; /* pointer to current DCT block within MCU */
374 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
375 compptr = cinfo->cur_comp_info[ci];
376 start_col = MCU_col_num * compptr->MCU_width;
377 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
378 buffer_ptr = buffer[ci][yoffset + yindex] + start_col;
379 xindex = compptr->MCU_width;
380 do {
381 *blkp++ = buffer_ptr++;
382 } while (--xindex);
385 /* Try to write the MCU. */
386 if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
387 /* Suspension forced; update state counters and exit */
388 coef->MCU_vert_offset = yoffset;
389 coef->MCU_ctr = MCU_col_num;
390 return FALSE;
393 /* Completed an MCU row, but perhaps not an iMCU row */
394 coef->MCU_ctr = 0;
396 /* Completed the iMCU row, advance counters for next one */
397 coef->iMCU_row_num++;
398 start_iMCU_row(cinfo);
399 return TRUE;
402 #endif /* FULL_COEF_BUFFER_SUPPORTED */
406 * Initialize coefficient buffer controller.
409 GLOBAL(void)
410 jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
412 my_coef_ptr coef;
414 if (need_full_buffer) {
415 #ifdef FULL_COEF_BUFFER_SUPPORTED
416 /* Allocate a full-image virtual array for each component, */
417 /* padded to a multiple of samp_factor DCT blocks in each direction. */
418 int ci;
419 jpeg_component_info *compptr;
421 coef = (my_coef_ptr) (*cinfo->mem->alloc_small)
422 ((j_common_ptr) cinfo, JPOOL_IMAGE,
423 SIZEOF(my_coef_controller) - SIZEOF(coef->blk_buffer));
424 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
425 ci++, compptr++) {
426 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
427 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
428 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
429 (long) compptr->h_samp_factor),
430 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
431 (long) compptr->v_samp_factor),
432 (JDIMENSION) compptr->v_samp_factor);
434 #else
435 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
436 #endif
437 } else {
438 /* We only need a single-MCU buffer. */
439 JBLOCKARRAY blkp;
440 JBLOCKROW buffer_ptr;
441 int bi;
443 coef = (my_coef_ptr) (*cinfo->mem->alloc_small)
444 ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_coef_controller));
445 blkp = coef->MCU_buffer;
446 buffer_ptr = coef->blk_buffer;
447 bi = C_MAX_BLOCKS_IN_MCU;
448 do {
449 *blkp++ = buffer_ptr++;
450 } while (--bi);
451 coef->whole_image[0] = NULL; /* flag for no virtual arrays */
454 coef->pub.start_pass = start_pass_coef;
455 cinfo->coef = &coef->pub;