ReactOS 0.4.16-dev-2613-g9533ad7
jccoefct.c
Go to the documentation of this file.
1/*
2 * jccoefct.c
3 *
4 * Copyright (C) 1994-1997, Thomas G. Lane.
5 * Modified 2003-2022 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.
8 *
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.
12 */
13
14#define JPEG_INTERNALS
15#include "jinclude.h"
16#include "jpeglib.h"
17
18
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.
23 */
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
31
32
33/* Private buffer controller object */
34
35typedef struct {
36 struct jpeg_c_coef_controller pub; /* public fields */
37
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 */
42
43 /* For single-pass compression, it's sufficient to buffer just one MCU
44 * (although this may prove a bit slow in practice).
45 * We append a workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks,
46 * and reuse it 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.
49 */
51
52 /* In multi-pass modes, we need a virtual block array for each component. */
54
55 /* Workspace for single-pass compression (omitted otherwise). */
58
60
61
62/* Forward declarations */
64 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
65#ifdef FULL_COEF_BUFFER_SUPPORTED
66METHODDEF(boolean) compress_first_pass
67 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
69 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
70#endif
71
72
73LOCAL(void)
75/* Reset within-iMCU-row counters for a new row */
76{
77 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
78
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.
82 */
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))
88 else
90 }
91
92 coef->MCU_ctr = 0;
93 coef->MCU_vert_offset = 0;
94}
95
96
97/*
98 * Initialize for a processing pass.
99 */
100
101METHODDEF(void)
103{
104 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
105
106 coef->iMCU_row_num = 0;
107 start_iMCU_row(cinfo);
108
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
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);
129 }
130}
131
132
133/*
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.
138 *
139 * NB: input_buf contains a plane for each component in image,
140 * which we index according to the component's SOF position.
141 */
142
143METHODDEF(boolean)
145{
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;
155 forward_DCT_ptr forward_DCT;
156
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.)
169 */
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] +
176 /* ypos == (yoffset + yindex) * compptr->DCT_v_scaled_size */
177 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_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;
193 }
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);
200 }
201 }
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...)
204 */
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;
210 }
211 }
212 /* Completed an MCU row, but perhaps not an iMCU row */
213 coef->MCU_ctr = 0;
214 }
215 /* Completed the iMCU row, advance counters for next one */
216 coef->iMCU_row_num++;
217 start_iMCU_row(cinfo);
218 return TRUE;
219}
220
221
222#ifdef FULL_COEF_BUFFER_SUPPORTED
223
224/*
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.
233 *
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.
237 *
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).
243 */
244
245METHODDEF(boolean)
246compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
247{
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;
255 JBLOCKROW thisblockrow, lastblockrow;
256 JSAMPARRAY input_ptr;
257 forward_DCT_ptr forward_DCT;
258
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],
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;
273 }
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.
284 */
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;
297 }
298 }
299 }
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...
304 */
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;
317 }
318 thisblockrow += h_samp_factor; /* advance to next MCU in row */
319 lastblockrow += h_samp_factor;
320 }
321 } while (++block_row < compptr->v_samp_factor);
322 }
323 }
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.
326 */
327
328 /* Emit data to the entropy encoder, sharing code with subsequent passes */
329 return compress_output(cinfo, input_buf);
330}
331
332
333/*
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.
339 *
340 * NB: input_buf is ignored; it is likely to be a NULL pointer.
341 */
342
343METHODDEF(boolean)
345{
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;
350 JBLOCKARRAY blkp;
352 JBLOCKROW buffer_ptr;
354
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.
358 */
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)
365 }
366
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);
383 }
384 }
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;
391 }
392 }
393 /* Completed an MCU row, but perhaps not an iMCU row */
394 coef->MCU_ctr = 0;
395 }
396 /* Completed the iMCU row, advance counters for next one */
397 coef->iMCU_row_num++;
398 start_iMCU_row(cinfo);
399 return TRUE;
400}
401
402#endif /* FULL_COEF_BUFFER_SUPPORTED */
403
404
405/*
406 * Initialize coefficient buffer controller.
407 */
408
409GLOBAL(void)
411{
412 my_coef_ptr coef;
413
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;
420
421 coef = (my_coef_ptr) (*cinfo->mem->alloc_small)
422 ((j_common_ptr) cinfo, JPOOL_IMAGE,
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,
433 }
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;
442
443 coef = (my_coef_ptr) (*cinfo->mem->alloc_small)
445 blkp = coef->MCU_buffer;
446 buffer_ptr = coef->blk_buffer;
448 do {
449 *blkp++ = buffer_ptr++;
450 } while (--bi);
451 coef->whole_image[0] = NULL; /* flag for no virtual arrays */
452 }
453
454 coef->pub.start_pass = start_pass_coef;
455 cinfo->coef = &coef->pub;
456}
#define SIZEOF(_ar)
Definition: calc.h:97
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define FAR
Definition: zlib.h:34
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
for(i=0;i< ARRAY_SIZE(offsets);i++)
GLint GLint GLint yoffset
Definition: gl.h:1547
GLuint buffer
Definition: glext.h:5915
jinit_c_coef_controller(j_compress_ptr cinfo, boolean need_full_buffer)
Definition: jccoefct.c:410
compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
Definition: jccoefct.c:144
start_pass_coef(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
Definition: jccoefct.c:102
my_coef_controller * my_coef_ptr
Definition: jccoefct.c:59
forward_DCT(j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY sample_data, JBLOCKROW coef_blocks, JDIMENSION start_col, JDIMENSION num_blocks)
Definition: jcdctmgr.c:62
start_iMCU_row(j_compress_ptr cinfo)
Definition: jctrans.c:242
compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
Definition: jctrans.c:293
JSAMPARRAY JDIMENSION start_col
Definition: jdct.h:183
jpeg_component_info * compptr
Definition: jdct.h:252
unsigned int JDIMENSION
Definition: jmorecfg.h:265
#define MAX_COMPONENTS
Definition: jmorecfg.h:81
#define LOCAL(type)
Definition: jmorecfg.h:325
#define METHODDEF(type)
Definition: jmorecfg.h:323
short JCOEF
Definition: jmorecfg.h:187
#define GLOBAL(type)
Definition: jmorecfg.h:327
J_BUF_MODE
Definition: jpegint.h:17
@ JBUF_PASS_THRU
Definition: jpegint.h:18
@ JBUF_SAVE_AND_PASS
Definition: jpegint.h:22
@ JBUF_CRANK_DEST
Definition: jpegint.h:21
boolean need_full_buffer
Definition: jpegint.h:383
#define FMEMZERO(target, size)
Definition: jpegint.h:367
JBLOCK FAR * JBLOCKROW
Definition: jpeglib.h:80
struct jpeg_common_struct * j_common_ptr
Definition: jpeglib.h:284
#define JPP(arglist)
Definition: jpeglib.h:879
JSAMPARRAY * JSAMPIMAGE
Definition: jpeglib.h:77
#define C_MAX_BLOCKS_IN_MCU
Definition: jpeglib.h:64
JCOEF JBLOCK[DCTSIZE2]
Definition: jpeglib.h:79
JBLOCKROW * JBLOCKARRAY
Definition: jpeglib.h:81
JSAMPROW * JSAMPARRAY
Definition: jpeglib.h:76
#define JPOOL_IMAGE
Definition: jpeglib.h:810
#define MAX_COMPS_IN_SCAN
Definition: jpeglib.h:55
jround_up(long a, long b)
Definition: jutils.c:133
if(dx< 0)
Definition: linetemp.h:194
#define long
Definition: qsort.c:33
#define ERREXIT(msg)
Definition: rdjpgcom.c:72
JDIMENSION width_in_blocks
Definition: jpeglib.h:148
JDIMENSION height_in_blocks
Definition: jpeglib.h:149
jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN]
Definition: jpeglib.h:424
struct jpeg_entropy_encoder * entropy
Definition: jpeglib.h:452
jpeg_component_info * comp_info
Definition: jpeglib.h:333
struct jpeg_forward_dct * fdct
Definition: jpeglib.h:451
struct jpeg_c_coef_controller * coef
Definition: jpeglib.h:447
JDIMENSION total_iMCU_rows
Definition: jpeglib.h:412
int MCU_rows_per_iMCU_row
Definition: jccoefct.c:41
JDIMENSION MCU_ctr
Definition: jccoefct.c:39
struct jpeg_c_coef_controller pub
Definition: jccoefct.c:36
jvirt_barray_ptr whole_image[MAX_COMPONENTS]
Definition: jccoefct.c:53
JBLOCK blk_buffer[C_MAX_BLOCKS_IN_MCU]
Definition: jccoefct.c:56
JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU]
Definition: jccoefct.c:50
JDIMENSION iMCU_row_num
Definition: jccoefct.c:38
#define MEMZERO(addr, type, size)
Definition: svc_dg.c:324