| 6 |
| * It was modified by The libjpeg-turbo Project to include only code and |
| 7 | * information relevant to libjpeg-turbo. |
| 8 | * mozjpeg Modifications: |
| 9 | * Copyright (C) 2014, Mozilla Corporation. |
| 10 | * For conditions of distribution and use, see the accompanying README file. |
| 11 | * |
| 12 | * This file contains the coefficient buffer controller for compression. |
| 13 | * This controller is the top level of the JPEG compressor proper. |
| 14 | * The coefficient buffer lies between forward-DCT and entropy encoding steps. |
| 15 | */ |
| 16 |
| 17 | #define JPEG_INTERNALS |
| 18 | #include "jinclude.h" |
| 19 | #include "jpeglib.h" |
| 20 | #include "jchuff.h" |
| 21 |
| 22 | /* We use a full-image coefficient buffer when doing Huffman optimization, |
| 23 | * and also for writing multiple-scan JPEG files. In all cases, the DCT |
| 24 | * step is run during the first pass, and subsequent passes need only read |
| 25 | * the buffered coefficients. |
| 26 | */ |
| 27 | #ifdef ENTROPY_OPT_SUPPORTED |
| 28 | #define FULL_COEF_BUFFER_SUPPORTED |
| 29 | #else |
| 30 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 31 | #define FULL_COEF_BUFFER_SUPPORTED |
| 32 | #endif |
| 33 | #endif |
| 34 |
| 35 |
| 36 | /* Private buffer controller object */ |
| 37 |
| 38 | typedef struct { |
| 39 | struct jpeg_c_coef_controller pub; /* public fields */ |
| 40 |
| 41 | JDIMENSION iMCU_row_num; /* iMCU row # within image */ |
| 42 | JDIMENSION mcu_ctr; /* counts MCUs processed in current row */ |
| 43 | int MCU_vert_offset; /* counts MCU rows within iMCU row */ |
| 44 | int MCU_rows_per_iMCU_row; /* number of such rows needed */ |
| 45 |
| 46 | /* For single-pass compression, it's sufficient to buffer just one MCU |
| 47 | * (although this may prove a bit slow in practice). We allocate a |
| 48 | * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each |
| 49 | * MCU constructed and sent. In multi-pass modes, this array points to the |
| 50 | * current MCU's blocks within the virtual arrays. |
| 51 | */ |
| 52 | JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU]; |
| 53 |
| 54 | /* In multi-pass modes, we need a virtual block array for each component. */ |
| 55 | jvirt_barray_ptr whole_image[MAX_COMPONENTS]; |
| 56 |
| 57 | /* when using trellis quantization, need to keep a copy of all unquantized coefficients */ |
| 58 | jvirt_barray_ptr whole_image_uq[MAX_COMPONENTS]; |
| 59 |
| 60 | } my_coef_controller; |
| 61 |
| 62 | typedef my_coef_controller *my_coef_ptr; |
| 63 |
| 64 |
| 65 | /* Forward declarations */ |
| 66 | METHODDEF(boolean) compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf); |
| 67 | #ifdef FULL_COEF_BUFFER_SUPPORTED |
| 68 | METHODDEF(boolean) compress_first_pass(j_compress_ptr cinfo, |
| 69 | JSAMPIMAGE input_buf); |
| 70 | METHODDEF(boolean) compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf); |
| 71 | #endif |
| 72 | METHODDEF(boolean) compress_trellis_pass |
| 73 | (j_compress_ptr cinfo, JSAMPIMAGE input_buf); |
| 74 |
| 75 |
| 76 | LOCAL(void) |
| 77 | start_iMCU_row(j_compress_ptr cinfo) |
| 78 | /* Reset within-iMCU-row counters for a new row */ |
| 79 | { |
| 80 | my_coef_ptr coef = (my_coef_ptr)cinfo->coef; |
| 81 |
| 82 | /* In an interleaved scan, an MCU row is the same as an iMCU row. |
| 83 | * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. |
| 84 | * But at the bottom of the image, process only what's left. |
| 85 | */ |
| 86 | if (cinfo->comps_in_scan > 1) { |
| 87 | coef->MCU_rows_per_iMCU_row = 1; |
| 88 | } else { |
| 89 | if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1)) |
| 90 | coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; |
| 91 | else |
| 92 | coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; |
| 93 | } |
| 94 |
| 95 | coef->mcu_ctr = 0; |
| 96 | coef->MCU_vert_offset = 0; |
| 97 | } |
| 98 |
| 99 |
| 100 | /* |
| 101 | * Initialize for a processing pass. |
| 102 | */ |
| 103 |
| 104 | METHODDEF(void) |
| 105 | start_pass_coef(j_compress_ptr cinfo, J_BUF_MODE pass_mode) |
| 106 | { |
| 107 | my_coef_ptr coef = (my_coef_ptr)cinfo->coef; |
| 108 |
| 109 | coef->iMCU_row_num = 0; |
| 110 | start_iMCU_row(cinfo); |
| 111 |
| 112 | switch (pass_mode) { |
| 113 | case JBUF_PASS_THRU: |
| 114 | if (coef->whole_image[0] != NULL) |
| 115 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
| 116 | coef->pub.compress_data = compress_data; |
| 117 | break; |
| 118 | #ifdef FULL_COEF_BUFFER_SUPPORTED |
| 119 | case JBUF_SAVE_AND_PASS: |
| 120 | if (coef->whole_image[0] == NULL) |
| 121 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
| 122 | coef->pub.compress_data = compress_first_pass; |
| 123 | break; |
| 124 | case JBUF_CRANK_DEST: |
| 125 | if (coef->whole_image[0] == NULL) |
| 126 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
| 127 | coef->pub.compress_data = compress_output; |
| 128 | break; |
| 129 | #endif |
| 130 | case JBUF_REQUANT: |
| 131 | if (coef->whole_image[0] == NULL) |
| 132 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
| 133 | coef->pub.compress_data = compress_trellis_pass; |
| 134 | break; |
| 135 |
| 136 | default: |
| 137 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 |
| 142 |
| 143 | /* |
| 144 | * Process some data in the single-pass case. |
| 145 | * We process the equivalent of one fully interleaved MCU row ("iMCU" row) |
| 146 | * per call, ie, v_samp_factor block rows for each component in the image. |
| 147 | * Returns TRUE if the iMCU row is completed, FALSE if suspended. |
| 148 | * |
| 149 | * NB: input_buf contains a plane for each component in image, |
| 150 | * which we index according to the component's SOF position. |
| 151 | */ |
| 152 |
| 153 | METHODDEF(boolean) |
| 154 | compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf) |
| 155 | { |
| 156 | my_coef_ptr coef = (my_coef_ptr)cinfo->coef; |
| 157 | JDIMENSION MCU_col_num; /* index of current MCU within row */ |
| 158 | JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; |
| 159 | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; |
| 160 | int blkn, bi, ci, yindex, yoffset, blockcnt; |
| 161 | JDIMENSION ypos, xpos; |
| 162 | jpeg_component_info *compptr; |
| 163 |
| 164 | /* Loop to write as much as one whole iMCU row */ |
| 165 | for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; |
| 166 | yoffset++) { |
| 167 | for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col; |
| 168 | MCU_col_num++) { |
| 169 | /* Determine where data comes from in input_buf and do the DCT thing. |
| 170 | * Each call on forward_DCT processes a horizontal row of DCT blocks |
| 171 | * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks |
| 172 | * sequentially. Dummy blocks at the right or bottom edge are filled in |
| 173 | * specially. The data in them does not matter for image reconstruction, |
| 174 | * so we fill them with values that will encode to the smallest amount of |
| 175 | * data, viz: all zeroes in the AC entries, DC entries equal to previous |
| 176 | * block's DC value. (Thanks to Thomas Kinsman for this idea.) |
| 177 | */ |
| 178 | blkn = 0; |
| 179 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 180 | compptr = cinfo->cur_comp_info[ci]; |
| 181 | blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width : |
| 182 | compptr->last_col_width; |
| 183 | xpos = MCU_col_num * compptr->MCU_sample_width; |
| 184 | ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */ |
| 185 | for (yindex = 0; yindex < compptr->MCU_height; yindex++) { |
| 186 | if (coef->iMCU_row_num < last_iMCU_row || |
| 187 | yoffset + yindex < compptr->last_row_height) { |
| 188 | (*cinfo->fdct->forward_DCT) (cinfo, compptr, |
| 189 | input_buf[compptr->component_index], |
| 190 | coef->MCU_buffer[blkn], |
| 191 | ypos, xpos, (JDIMENSION) blockcnt, |
| 192 | NULL); |
| 193 | if (blockcnt < compptr->MCU_width) { |
| 194 | /* Create some dummy blocks at the right edge of the image. */ |
| 195 | jzero_far((void *)coef->MCU_buffer[blkn + blockcnt], |
| 196 | (compptr->MCU_width - blockcnt) * sizeof(JBLOCK)); |
| 197 | for (bi = blockcnt; bi < compptr->MCU_width; bi++) { |
| 198 | coef->MCU_buffer[blkn + bi][0][0] = |
| 199 | coef->MCU_buffer[blkn + bi - 1][0][0]; |
| 200 | } |
| 201 | } |
| 202 | } else { |
| 203 | /* Create a row of dummy blocks at the bottom of the image. */ |
| 204 | jzero_far((void *)coef->MCU_buffer[blkn], |
| 205 | compptr->MCU_width * sizeof(JBLOCK)); |
| 206 | for (bi = 0; bi < compptr->MCU_width; bi++) { |
| 207 | coef->MCU_buffer[blkn + bi][0][0] = |
| 208 | coef->MCU_buffer[blkn - 1][0][0]; |
| 209 | } |
| 210 | } |
| 211 | blkn += compptr->MCU_width; |
| 212 | ypos += DCTSIZE; |
| 213 | } |
| 214 | } |
| 215 | /* Try to write the MCU. In event of a suspension failure, we will |
| 216 | * re-DCT the MCU on restart (a bit inefficient, could be fixed...) |
| 217 | */ |
| 218 | if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) { |
| 219 | /* Suspension forced; update state counters and exit */ |
| 220 | coef->MCU_vert_offset = yoffset; |
| 221 | coef->mcu_ctr = MCU_col_num; |
| 222 | return FALSE; |
| 223 | } |
| 224 | } |
| 225 | /* Completed an MCU row, but perhaps not an iMCU row */ |
| 226 | coef->mcu_ctr = 0; |
| 227 | } |
| 228 | /* Completed the iMCU row, advance counters for next one */ |
| 229 | coef->iMCU_row_num++; |
| 230 | start_iMCU_row(cinfo); |
| 231 | return TRUE; |
| 232 | } |
| 233 |
| 234 |
| 235 | #ifdef FULL_COEF_BUFFER_SUPPORTED |
| 236 |
| 237 | /* |
| 238 | * Process some data in the first pass of a multi-pass case. |
| 239 | * We process the equivalent of one fully interleaved MCU row ("iMCU" row) |
| 240 | * per call, ie, v_samp_factor block rows for each component in the image. |
| 241 | * This amount of data is read from the source buffer, DCT'd and quantized, |
| 242 | * and saved into the virtual arrays. We also generate suitable dummy blocks |
| 243 | * as needed at the right and lower edges. (The dummy blocks are constructed |
| 244 | * in the virtual arrays, which have been padded appropriately.) This makes |
| 245 | * it possible for subsequent passes not to worry about real vs. dummy blocks. |
| 246 | * |
| 247 | * We must also emit the data to the entropy encoder. This is conveniently |
| 248 | * done by calling compress_output() after we've loaded the current strip |
| 249 | * of the virtual arrays. |
| 250 | * |
| 251 | * NB: input_buf contains a plane for each component in image. All |
| 252 | * components are DCT'd and loaded into the virtual arrays in this pass. |
| 253 | * However, it may be that only a subset of the components are emitted to |
| 254 | * the entropy encoder during this first pass; be careful about looking |
| 255 | * at the scan-dependent variables (MCU dimensions, etc). |
| 256 | */ |
| 257 |
| 258 | METHODDEF(boolean) |
| 259 | compress_first_pass(j_compress_ptr cinfo, JSAMPIMAGE input_buf) |
| 260 | { |
| 261 | my_coef_ptr coef = (my_coef_ptr)cinfo->coef; |
| 262 | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; |
| 263 | JDIMENSION blocks_across, MCUs_across, MCUindex; |
| 264 | int bi, ci, h_samp_factor, block_row, block_rows, ndummy; |
| 265 | JCOEF lastDC; |
| 266 | jpeg_component_info *compptr; |
| 267 | JBLOCKARRAY buffer; |
| 268 | JBLOCKROW thisblockrow, lastblockrow; |
| 269 | JBLOCKARRAY buffer_dst; |
| 270 |
| 271 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 272 | ci++, compptr++) { |
| 273 | /* Align the virtual buffer for this component. */ |
| 274 | buffer = (*cinfo->mem->access_virt_barray) |
| 275 | ((j_common_ptr)cinfo, coef->whole_image[ci], |
| 276 | coef->iMCU_row_num * compptr->v_samp_factor, |
| 277 | (JDIMENSION) compptr->v_samp_factor, TRUE); |
| 278 |
| 279 | buffer_dst = (*cinfo->mem->access_virt_barray) |
| 280 | ((j_common_ptr) cinfo, coef->whole_image_uq[ci], |
| 281 | coef->iMCU_row_num * compptr->v_samp_factor, |
| 282 | (JDIMENSION) compptr->v_samp_factor, TRUE); |
| 283 |
| 284 | /* Count non-dummy DCT block rows in this iMCU row. */ |
| 285 | if (coef->iMCU_row_num < last_iMCU_row) |
| 286 | block_rows = compptr->v_samp_factor; |
| 287 | else { |
| 288 | /* NB: can't use last_row_height here, since may not be set! */ |
| 289 | block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor); |
| 290 | if (block_rows == 0) block_rows = compptr->v_samp_factor; |
| 291 | } |
| 292 | blocks_across = compptr->width_in_blocks; |
| 293 | h_samp_factor = compptr->h_samp_factor; |
| 294 | /* Count number of dummy blocks to be added at the right margin. */ |
| 295 | ndummy = (int)(blocks_across % h_samp_factor); |
| 296 | if (ndummy > 0) |
| 297 | ndummy = h_samp_factor - ndummy; |
| 298 | /* Perform DCT for all non-dummy blocks in this iMCU row. Each call |
| 299 | * on forward_DCT processes a complete horizontal row of DCT blocks. |
| 300 | */ |
| 301 | for (block_row = 0; block_row < block_rows; block_row++) { |
| 302 | thisblockrow = buffer[block_row]; |
| 303 | (*cinfo->fdct->forward_DCT) (cinfo, compptr, |
| 304 | input_buf[ci], thisblockrow, |
| 305 | (JDIMENSION) (block_row * DCTSIZE), |
| 306 | (JDIMENSION) 0, blocks_across, |
| 307 | buffer_dst[block_row]); |
| 308 | if (ndummy > 0) { |
| 309 | /* Create dummy blocks at the right edge of the image. */ |
| 310 | thisblockrow += blocks_across; /* => first dummy block */ |
| 311 | jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK)); |
| 312 | lastDC = thisblockrow[-1][0]; |
| 313 | for (bi = 0; bi < ndummy; bi++) { |
| 314 | thisblockrow[bi][0] = lastDC; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | /* If at end of image, create dummy block rows as needed. |
| 319 | * The tricky part here is that within each MCU, we want the DC values |
| 320 | * of the dummy blocks to match the last real block's DC value. |
| 321 | * This squeezes a few more bytes out of the resulting file... |
| 322 | */ |
| 323 | if (coef->iMCU_row_num == last_iMCU_row) { |
| 324 | blocks_across += ndummy; /* include lower right corner */ |
| 325 | MCUs_across = blocks_across / h_samp_factor; |
| 326 | for (block_row = block_rows; block_row < compptr->v_samp_factor; |
| 327 | block_row++) { |
| 328 | thisblockrow = buffer[block_row]; |
| 329 | lastblockrow = buffer[block_row - 1]; |
| 330 | jzero_far((void *)thisblockrow, |
| 331 | (size_t)(blocks_across * sizeof(JBLOCK))); |
| 332 | for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) { |
| 333 | lastDC = lastblockrow[h_samp_factor - 1][0]; |
| 334 | for (bi = 0; bi < h_samp_factor; bi++) { |
| 335 | thisblockrow[bi][0] = lastDC; |
| 336 | } |
| 337 | thisblockrow += h_samp_factor; /* advance to next MCU in row */ |
| 338 | lastblockrow += h_samp_factor; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | /* NB: compress_output will increment iMCU_row_num if successful. |
| 344 | * A suspension return will result in redoing all the work above next time. |
| 345 | */ |
| 346 |
| 347 | /* Emit data to the entropy encoder, sharing code with subsequent passes */ |
| 348 | return compress_output(cinfo, input_buf); |
| 349 | } |
| 350 |
| 351 | METHODDEF(boolean) |
| 352 | compress_trellis_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf) |
| 353 | { |
| 354 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
| 355 | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; |
| 356 | JDIMENSION blocks_across, MCUs_across, MCUindex; |
| 357 | int bi, ci, h_samp_factor, block_row, block_rows, ndummy; |
| 358 | JCOEF lastDC; |
| 359 | jpeg_component_info *compptr; |
| 360 | JBLOCKARRAY buffer; |
| 361 | JBLOCKROW thisblockrow, lastblockrow; |
| 362 | JBLOCKARRAY buffer_dst; |
| 363 |
| 364 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 365 | c_derived_tbl dctbl_data; |
| 366 | c_derived_tbl *dctbl = &dctbl_data; |
| 367 | c_derived_tbl actbl_data; |
| 368 | c_derived_tbl *actbl = &actbl_data; |
| 369 |
| 370 | #ifdef C_ARITH_CODING_SUPPORTED |
| 371 | arith_rates arith_r_data; |
| 372 | arith_rates *arith_r = &arith_r_data; |
| 373 | #endif |
| 374 |
| 375 | compptr = cinfo->cur_comp_info[ci]; |
| 376 |
| 377 | #ifdef C_ARITH_CODING_SUPPORTED |
| 378 | if (cinfo->arith_code) |
| 379 | jget_arith_rates(cinfo, compptr->dc_tbl_no, compptr->ac_tbl_no, arith_r); |
| 380 | else |
| 381 | #endif |
| 382 | { |
| 383 | jpeg_make_c_derived_tbl(cinfo, TRUE, compptr->dc_tbl_no, &dctbl); |
| 384 | jpeg_make_c_derived_tbl(cinfo, FALSE, compptr->ac_tbl_no, &actbl); |
| 385 | } |
| 386 |
| 387 | /* Align the virtual buffer for this component. */ |
| 388 | buffer = (*cinfo->mem->access_virt_barray) |
| 389 | ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], |
| 390 | coef->iMCU_row_num * compptr->v_samp_factor, |
| 391 | (JDIMENSION) compptr->v_samp_factor, TRUE); |
| 392 |
| 393 | buffer_dst = (*cinfo->mem->access_virt_barray) |
| 394 | ((j_common_ptr) cinfo, coef->whole_image_uq[compptr->component_index], |
| 395 | coef->iMCU_row_num * compptr->v_samp_factor, |
| 396 | (JDIMENSION) compptr->v_samp_factor, TRUE); |
| 397 |
| 398 | /* Count non-dummy DCT block rows in this iMCU row. */ |
| 399 | if (coef->iMCU_row_num < last_iMCU_row) |
| 400 | block_rows = compptr->v_samp_factor; |
| 401 | else { |
| 402 | /* NB: can't use last_row_height here, since may not be set! */ |
| 403 | block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); |
| 404 | if (block_rows == 0) block_rows = compptr->v_samp_factor; |
| 405 | } |
| 406 | blocks_across = compptr->width_in_blocks; |
| 407 | h_samp_factor = compptr->h_samp_factor; |
| 408 | /* Count number of dummy blocks to be added at the right margin. */ |
| 409 | ndummy = (int) (blocks_across % h_samp_factor); |
| 410 | if (ndummy > 0) |
| 411 | ndummy = h_samp_factor - ndummy; |
| 412 |
| 413 | lastDC = 0; |
| 414 |
| 415 | /* Perform DCT for all non-dummy blocks in this iMCU row. Each call |
| 416 | * on forward_DCT processes a complete horizontal row of DCT blocks. |
| 417 | */ |
| 418 | for (block_row = 0; block_row < block_rows; block_row++) { |
| 419 | thisblockrow = buffer[block_row]; |
| 420 | lastblockrow = (block_row > 0) ? buffer[block_row-1] : NULL; |
| 421 | #ifdef C_ARITH_CODING_SUPPORTED |
| 422 | if (cinfo->arith_code) |
| 423 | quantize_trellis_arith(cinfo, arith_r, thisblockrow, |
| 424 | buffer_dst[block_row], blocks_across, |
| 425 | cinfo->quant_tbl_ptrs[compptr->quant_tbl_no], |
| 426 | cinfo->master->norm_src[compptr->quant_tbl_no], |
| 427 | cinfo->master->norm_coef[compptr->quant_tbl_no], |
| 428 | &lastDC, lastblockrow, buffer_dst[block_row-1]); |
| 429 | else |
| 430 | #endif |
| 431 | quantize_trellis(cinfo, dctbl, actbl, thisblockrow, |
| 432 | buffer_dst[block_row], blocks_across, |
| 433 | cinfo->quant_tbl_ptrs[compptr->quant_tbl_no], |
| 434 | cinfo->master->norm_src[compptr->quant_tbl_no], |
| 435 | cinfo->master->norm_coef[compptr->quant_tbl_no], |
| 436 | &lastDC, lastblockrow, buffer_dst[block_row-1]); |
| 437 |
| 438 | if (ndummy > 0) { |
| 439 | /* Create dummy blocks at the right edge of the image. */ |
| 440 | thisblockrow += blocks_across; /* => first dummy block */ |
| 441 | jzero_far((void *) thisblockrow, ndummy * sizeof(JBLOCK)); |
| 442 | lastDC = thisblockrow[-1][0]; |
| 443 | for (bi = 0; bi < ndummy; bi++) { |
| 444 | thisblockrow[bi][0] = lastDC; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | /* If at end of image, create dummy block rows as needed. |
| 449 | * The tricky part here is that within each MCU, we want the DC values |
| 450 | * of the dummy blocks to match the last real block's DC value. |
| 451 | * This squeezes a few more bytes out of the resulting file... |
| 452 | */ |
| 453 | if (coef->iMCU_row_num == last_iMCU_row) { |
| 454 | blocks_across += ndummy; /* include lower right corner */ |
| 455 | MCUs_across = blocks_across / h_samp_factor; |
| 456 | for (block_row = block_rows; block_row < compptr->v_samp_factor; |
| 457 | block_row++) { |
| 458 | thisblockrow = buffer[block_row]; |
| 459 | lastblockrow = buffer[block_row-1]; |
| 460 | jzero_far((void *) thisblockrow, |
| 461 | (size_t) (blocks_across * sizeof(JBLOCK))); |
| 462 | for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) { |
| 463 | lastDC = lastblockrow[h_samp_factor-1][0]; |
| 464 | for (bi = 0; bi < h_samp_factor; bi++) { |
| 465 | thisblockrow[bi][0] = lastDC; |
| 466 | } |
| 467 | thisblockrow += h_samp_factor; /* advance to next MCU in row */ |
| 468 | lastblockrow += h_samp_factor; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 |
| 474 | /* NB: compress_output will increment iMCU_row_num if successful. |
| 475 | * A suspension return will result in redoing all the work above next time. |
| 476 | */ |
| 477 |
| 478 | /* Emit data to the entropy encoder, sharing code with subsequent passes */ |
| 479 | return compress_output(cinfo, input_buf); |
| 480 | } |
| 481 |
| 482 | /* |
| 483 | * Process some data in subsequent passes of a multi-pass case. |
| 484 | * We process the equivalent of one fully interleaved MCU row ("iMCU" row) |
| 485 | * per call, ie, v_samp_factor block rows for each component in the scan. |
| 486 | * The data is obtained from the virtual arrays and fed to the entropy coder. |
| 487 | * Returns TRUE if the iMCU row is completed, FALSE if suspended. |
| 488 | * |
| 489 | * NB: input_buf is ignored; it is likely to be a NULL pointer. |
| 490 | */ |
| 491 |
| 492 | METHODDEF(boolean) |
| 493 | compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf) |
| 494 | { |
| 495 | my_coef_ptr coef = (my_coef_ptr)cinfo->coef; |
| 496 | JDIMENSION MCU_col_num; /* index of current MCU within row */ |
| 497 | int blkn, ci, xindex, yindex, yoffset; |
| 498 | JDIMENSION start_col; |
| 499 | JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; |
| 500 | JBLOCKROW buffer_ptr; |
| 501 | jpeg_component_info *compptr; |
| 502 |
| 503 | /* Align the virtual buffers for the components used in this scan. |
| 504 | * NB: during first pass, this is safe only because the buffers will |
| 505 | * already be aligned properly, so jmemmgr.c won't need to do any I/O. |
| 506 | */ |
| 507 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 508 | compptr = cinfo->cur_comp_info[ci]; |
| 509 | buffer[ci] = (*cinfo->mem->access_virt_barray) |
| 510 | ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index], |
| 511 | coef->iMCU_row_num * compptr->v_samp_factor, |
| 512 | (JDIMENSION)compptr->v_samp_factor, FALSE); |
| 513 | } |
| 514 |
| 515 | /* Loop to process one whole iMCU row */ |
| 516 | for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; |
| 517 | yoffset++) { |
| 518 | for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row; |
| 519 | MCU_col_num++) { |
| 520 | /* Construct list of pointers to DCT blocks belonging to this MCU */ |
| 521 | blkn = 0; /* index of current DCT block within MCU */ |
| 522 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 523 | compptr = cinfo->cur_comp_info[ci]; |
| 524 | start_col = MCU_col_num * compptr->MCU_width; |
| 525 | for (yindex = 0; yindex < compptr->MCU_height; yindex++) { |
| 526 | buffer_ptr = buffer[ci][yindex + yoffset] + start_col; |
| 527 | for (xindex = 0; xindex < compptr->MCU_width; xindex++) { |
| 528 | coef->MCU_buffer[blkn++] = buffer_ptr++; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | /* Try to write the MCU. */ |
| 533 | if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) { |
| 534 | /* Suspension forced; update state counters and exit */ |
| 535 | coef->MCU_vert_offset = yoffset; |
| 536 | coef->mcu_ctr = MCU_col_num; |
| 537 | return FALSE; |
| 538 | } |
| 539 | } |
| 540 | /* Completed an MCU row, but perhaps not an iMCU row */ |
| 541 | coef->mcu_ctr = 0; |
| 542 | } |
| 543 | /* Completed the iMCU row, advance counters for next one */ |
| 544 | coef->iMCU_row_num++; |
| 545 | start_iMCU_row(cinfo); |
| 546 | return TRUE; |
| 547 | } |
| 548 |
| 549 | #endif /* FULL_COEF_BUFFER_SUPPORTED */ |
| 550 |
| 551 |
| 552 | /* |
| 553 | * Initialize coefficient buffer controller. |
| 554 | */ |
| 555 |
| 556 | GLOBAL(void) |
| 557 | jinit_c_coef_controller(j_compress_ptr cinfo, boolean need_full_buffer) |
| 558 | { |
| 559 | my_coef_ptr coef; |
| 560 |
| 561 | coef = (my_coef_ptr) |
| 562 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
| 563 | sizeof(my_coef_controller)); |
| 564 | cinfo->coef = (struct jpeg_c_coef_controller *)coef; |
| 565 | coef->pub.start_pass = start_pass_coef; |
| 566 |
| 567 | /* Create the coefficient buffer. */ |
| 568 | if (need_full_buffer) { |
| 569 | #ifdef FULL_COEF_BUFFER_SUPPORTED |
| 570 | /* Allocate a full-image virtual array for each component, */ |
| 571 | /* padded to a multiple of samp_factor DCT blocks in each direction. */ |
| 572 | int ci; |
| 573 | jpeg_component_info *compptr; |
| 574 |
| 575 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 576 | ci++, compptr++) { |
| 577 | coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) |
| 578 | ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, |
| 579 | (JDIMENSION) jround_up((long) compptr->width_in_blocks, |
| 580 | (long) compptr->h_samp_factor), |
| 581 | (JDIMENSION) jround_up((long) compptr->height_in_blocks, |
| 582 | (long) compptr->v_samp_factor), |
| 583 | (JDIMENSION) compptr->v_samp_factor); |
| 584 |
| 585 | coef->whole_image_uq[ci] = (*cinfo->mem->request_virt_barray) |
| 586 | ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, |
| 587 | (JDIMENSION) jround_up((long) compptr->width_in_blocks, |
| 588 | (long) compptr->h_samp_factor), |
| 589 | (JDIMENSION) jround_up((long) compptr->height_in_blocks, |
| 590 | (long) compptr->v_samp_factor), |
| 591 | (JDIMENSION) compptr->v_samp_factor); |
| 592 | } |
| 593 | #else |
| 594 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
| 595 | #endif |
| 596 | } else { |
| 597 | /* We only need a single-MCU buffer. */ |
| 598 | JBLOCKROW buffer; |
| 599 | int i; |
| 600 |
| 601 | buffer = (JBLOCKROW) |
| 602 | (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
| 603 | C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK)); |
| 604 | for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) { |
| 605 | coef->MCU_buffer[i] = buffer + i; |
| 606 | } |
| 607 | coef->whole_image[0] = NULL; /* flag for no virtual arrays */ |
| 608 | } |
| 609 | } |
| 610 |