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