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