media/libjpeg/jctrans.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * jctrans.c
michael@0 3 *
michael@0 4 * Copyright (C) 1995-1998, Thomas G. Lane.
michael@0 5 * Modified 2000-2009 by Guido Vollbeding.
michael@0 6 * This file is part of the Independent JPEG Group's software.
michael@0 7 * For conditions of distribution and use, see the accompanying README file.
michael@0 8 *
michael@0 9 * This file contains library routines for transcoding compression,
michael@0 10 * that is, writing raw DCT coefficient arrays to an output JPEG file.
michael@0 11 * The routines in jcapimin.c will also be needed by a transcoder.
michael@0 12 */
michael@0 13
michael@0 14 #define JPEG_INTERNALS
michael@0 15 #include "jinclude.h"
michael@0 16 #include "jpeglib.h"
michael@0 17
michael@0 18
michael@0 19 /* Forward declarations */
michael@0 20 LOCAL(void) transencode_master_selection
michael@0 21 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
michael@0 22 LOCAL(void) transencode_coef_controller
michael@0 23 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
michael@0 24
michael@0 25
michael@0 26 /*
michael@0 27 * Compression initialization for writing raw-coefficient data.
michael@0 28 * Before calling this, all parameters and a data destination must be set up.
michael@0 29 * Call jpeg_finish_compress() to actually write the data.
michael@0 30 *
michael@0 31 * The number of passed virtual arrays must match cinfo->num_components.
michael@0 32 * Note that the virtual arrays need not be filled or even realized at
michael@0 33 * the time write_coefficients is called; indeed, if the virtual arrays
michael@0 34 * were requested from this compression object's memory manager, they
michael@0 35 * typically will be realized during this routine and filled afterwards.
michael@0 36 */
michael@0 37
michael@0 38 GLOBAL(void)
michael@0 39 jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
michael@0 40 {
michael@0 41 if (cinfo->global_state != CSTATE_START)
michael@0 42 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
michael@0 43 /* Mark all tables to be written */
michael@0 44 jpeg_suppress_tables(cinfo, FALSE);
michael@0 45 /* (Re)initialize error mgr and destination modules */
michael@0 46 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
michael@0 47 (*cinfo->dest->init_destination) (cinfo);
michael@0 48 /* Perform master selection of active modules */
michael@0 49 transencode_master_selection(cinfo, coef_arrays);
michael@0 50 /* Wait for jpeg_finish_compress() call */
michael@0 51 cinfo->next_scanline = 0; /* so jpeg_write_marker works */
michael@0 52 cinfo->global_state = CSTATE_WRCOEFS;
michael@0 53 }
michael@0 54
michael@0 55
michael@0 56 /*
michael@0 57 * Initialize the compression object with default parameters,
michael@0 58 * then copy from the source object all parameters needed for lossless
michael@0 59 * transcoding. Parameters that can be varied without loss (such as
michael@0 60 * scan script and Huffman optimization) are left in their default states.
michael@0 61 */
michael@0 62
michael@0 63 GLOBAL(void)
michael@0 64 jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
michael@0 65 j_compress_ptr dstinfo)
michael@0 66 {
michael@0 67 JQUANT_TBL ** qtblptr;
michael@0 68 jpeg_component_info *incomp, *outcomp;
michael@0 69 JQUANT_TBL *c_quant, *slot_quant;
michael@0 70 int tblno, ci, coefi;
michael@0 71
michael@0 72 /* Safety check to ensure start_compress not called yet. */
michael@0 73 if (dstinfo->global_state != CSTATE_START)
michael@0 74 ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
michael@0 75 /* Copy fundamental image dimensions */
michael@0 76 dstinfo->image_width = srcinfo->image_width;
michael@0 77 dstinfo->image_height = srcinfo->image_height;
michael@0 78 dstinfo->input_components = srcinfo->num_components;
michael@0 79 dstinfo->in_color_space = srcinfo->jpeg_color_space;
michael@0 80 #if JPEG_LIB_VERSION >= 70
michael@0 81 dstinfo->jpeg_width = srcinfo->output_width;
michael@0 82 dstinfo->jpeg_height = srcinfo->output_height;
michael@0 83 dstinfo->min_DCT_h_scaled_size = srcinfo->min_DCT_h_scaled_size;
michael@0 84 dstinfo->min_DCT_v_scaled_size = srcinfo->min_DCT_v_scaled_size;
michael@0 85 #endif
michael@0 86 /* Initialize all parameters to default values */
michael@0 87 jpeg_set_defaults(dstinfo);
michael@0 88 /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
michael@0 89 * Fix it to get the right header markers for the image colorspace.
michael@0 90 */
michael@0 91 jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
michael@0 92 dstinfo->data_precision = srcinfo->data_precision;
michael@0 93 dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
michael@0 94 /* Copy the source's quantization tables. */
michael@0 95 for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
michael@0 96 if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
michael@0 97 qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
michael@0 98 if (*qtblptr == NULL)
michael@0 99 *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
michael@0 100 MEMCOPY((*qtblptr)->quantval,
michael@0 101 srcinfo->quant_tbl_ptrs[tblno]->quantval,
michael@0 102 SIZEOF((*qtblptr)->quantval));
michael@0 103 (*qtblptr)->sent_table = FALSE;
michael@0 104 }
michael@0 105 }
michael@0 106 /* Copy the source's per-component info.
michael@0 107 * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
michael@0 108 */
michael@0 109 dstinfo->num_components = srcinfo->num_components;
michael@0 110 if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
michael@0 111 ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
michael@0 112 MAX_COMPONENTS);
michael@0 113 for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
michael@0 114 ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
michael@0 115 outcomp->component_id = incomp->component_id;
michael@0 116 outcomp->h_samp_factor = incomp->h_samp_factor;
michael@0 117 outcomp->v_samp_factor = incomp->v_samp_factor;
michael@0 118 outcomp->quant_tbl_no = incomp->quant_tbl_no;
michael@0 119 /* Make sure saved quantization table for component matches the qtable
michael@0 120 * slot. If not, the input file re-used this qtable slot.
michael@0 121 * IJG encoder currently cannot duplicate this.
michael@0 122 */
michael@0 123 tblno = outcomp->quant_tbl_no;
michael@0 124 if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
michael@0 125 srcinfo->quant_tbl_ptrs[tblno] == NULL)
michael@0 126 ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
michael@0 127 slot_quant = srcinfo->quant_tbl_ptrs[tblno];
michael@0 128 c_quant = incomp->quant_table;
michael@0 129 if (c_quant != NULL) {
michael@0 130 for (coefi = 0; coefi < DCTSIZE2; coefi++) {
michael@0 131 if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
michael@0 132 ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
michael@0 133 }
michael@0 134 }
michael@0 135 /* Note: we do not copy the source's Huffman table assignments;
michael@0 136 * instead we rely on jpeg_set_colorspace to have made a suitable choice.
michael@0 137 */
michael@0 138 }
michael@0 139 /* Also copy JFIF version and resolution information, if available.
michael@0 140 * Strictly speaking this isn't "critical" info, but it's nearly
michael@0 141 * always appropriate to copy it if available. In particular,
michael@0 142 * if the application chooses to copy JFIF 1.02 extension markers from
michael@0 143 * the source file, we need to copy the version to make sure we don't
michael@0 144 * emit a file that has 1.02 extensions but a claimed version of 1.01.
michael@0 145 * We will *not*, however, copy version info from mislabeled "2.01" files.
michael@0 146 */
michael@0 147 if (srcinfo->saw_JFIF_marker) {
michael@0 148 if (srcinfo->JFIF_major_version == 1) {
michael@0 149 dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
michael@0 150 dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
michael@0 151 }
michael@0 152 dstinfo->density_unit = srcinfo->density_unit;
michael@0 153 dstinfo->X_density = srcinfo->X_density;
michael@0 154 dstinfo->Y_density = srcinfo->Y_density;
michael@0 155 }
michael@0 156 }
michael@0 157
michael@0 158
michael@0 159 /*
michael@0 160 * Master selection of compression modules for transcoding.
michael@0 161 * This substitutes for jcinit.c's initialization of the full compressor.
michael@0 162 */
michael@0 163
michael@0 164 LOCAL(void)
michael@0 165 transencode_master_selection (j_compress_ptr cinfo,
michael@0 166 jvirt_barray_ptr * coef_arrays)
michael@0 167 {
michael@0 168 /* Although we don't actually use input_components for transcoding,
michael@0 169 * jcmaster.c's initial_setup will complain if input_components is 0.
michael@0 170 */
michael@0 171 cinfo->input_components = 1;
michael@0 172 /* Initialize master control (includes parameter checking/processing) */
michael@0 173 jinit_c_master_control(cinfo, TRUE /* transcode only */);
michael@0 174
michael@0 175 /* Entropy encoding: either Huffman or arithmetic coding. */
michael@0 176 if (cinfo->arith_code) {
michael@0 177 #ifdef C_ARITH_CODING_SUPPORTED
michael@0 178 jinit_arith_encoder(cinfo);
michael@0 179 #else
michael@0 180 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
michael@0 181 #endif
michael@0 182 } else {
michael@0 183 if (cinfo->progressive_mode) {
michael@0 184 #ifdef C_PROGRESSIVE_SUPPORTED
michael@0 185 jinit_phuff_encoder(cinfo);
michael@0 186 #else
michael@0 187 ERREXIT(cinfo, JERR_NOT_COMPILED);
michael@0 188 #endif
michael@0 189 } else
michael@0 190 jinit_huff_encoder(cinfo);
michael@0 191 }
michael@0 192
michael@0 193 /* We need a special coefficient buffer controller. */
michael@0 194 transencode_coef_controller(cinfo, coef_arrays);
michael@0 195
michael@0 196 jinit_marker_writer(cinfo);
michael@0 197
michael@0 198 /* We can now tell the memory manager to allocate virtual arrays. */
michael@0 199 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
michael@0 200
michael@0 201 /* Write the datastream header (SOI, JFIF) immediately.
michael@0 202 * Frame and scan headers are postponed till later.
michael@0 203 * This lets application insert special markers after the SOI.
michael@0 204 */
michael@0 205 (*cinfo->marker->write_file_header) (cinfo);
michael@0 206 }
michael@0 207
michael@0 208
michael@0 209 /*
michael@0 210 * The rest of this file is a special implementation of the coefficient
michael@0 211 * buffer controller. This is similar to jccoefct.c, but it handles only
michael@0 212 * output from presupplied virtual arrays. Furthermore, we generate any
michael@0 213 * dummy padding blocks on-the-fly rather than expecting them to be present
michael@0 214 * in the arrays.
michael@0 215 */
michael@0 216
michael@0 217 /* Private buffer controller object */
michael@0 218
michael@0 219 typedef struct {
michael@0 220 struct jpeg_c_coef_controller pub; /* public fields */
michael@0 221
michael@0 222 JDIMENSION iMCU_row_num; /* iMCU row # within image */
michael@0 223 JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
michael@0 224 int MCU_vert_offset; /* counts MCU rows within iMCU row */
michael@0 225 int MCU_rows_per_iMCU_row; /* number of such rows needed */
michael@0 226
michael@0 227 /* Virtual block array for each component. */
michael@0 228 jvirt_barray_ptr * whole_image;
michael@0 229
michael@0 230 /* Workspace for constructing dummy blocks at right/bottom edges. */
michael@0 231 JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
michael@0 232 } my_coef_controller;
michael@0 233
michael@0 234 typedef my_coef_controller * my_coef_ptr;
michael@0 235
michael@0 236
michael@0 237 LOCAL(void)
michael@0 238 start_iMCU_row (j_compress_ptr cinfo)
michael@0 239 /* Reset within-iMCU-row counters for a new row */
michael@0 240 {
michael@0 241 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
michael@0 242
michael@0 243 /* In an interleaved scan, an MCU row is the same as an iMCU row.
michael@0 244 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
michael@0 245 * But at the bottom of the image, process only what's left.
michael@0 246 */
michael@0 247 if (cinfo->comps_in_scan > 1) {
michael@0 248 coef->MCU_rows_per_iMCU_row = 1;
michael@0 249 } else {
michael@0 250 if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
michael@0 251 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
michael@0 252 else
michael@0 253 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
michael@0 254 }
michael@0 255
michael@0 256 coef->mcu_ctr = 0;
michael@0 257 coef->MCU_vert_offset = 0;
michael@0 258 }
michael@0 259
michael@0 260
michael@0 261 /*
michael@0 262 * Initialize for a processing pass.
michael@0 263 */
michael@0 264
michael@0 265 METHODDEF(void)
michael@0 266 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
michael@0 267 {
michael@0 268 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
michael@0 269
michael@0 270 if (pass_mode != JBUF_CRANK_DEST)
michael@0 271 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
michael@0 272
michael@0 273 coef->iMCU_row_num = 0;
michael@0 274 start_iMCU_row(cinfo);
michael@0 275 }
michael@0 276
michael@0 277
michael@0 278 /*
michael@0 279 * Process some data.
michael@0 280 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
michael@0 281 * per call, ie, v_samp_factor block rows for each component in the scan.
michael@0 282 * The data is obtained from the virtual arrays and fed to the entropy coder.
michael@0 283 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
michael@0 284 *
michael@0 285 * NB: input_buf is ignored; it is likely to be a NULL pointer.
michael@0 286 */
michael@0 287
michael@0 288 METHODDEF(boolean)
michael@0 289 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
michael@0 290 {
michael@0 291 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
michael@0 292 JDIMENSION MCU_col_num; /* index of current MCU within row */
michael@0 293 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
michael@0 294 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
michael@0 295 int blkn, ci, xindex, yindex, yoffset, blockcnt;
michael@0 296 JDIMENSION start_col;
michael@0 297 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
michael@0 298 JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
michael@0 299 JBLOCKROW buffer_ptr;
michael@0 300 jpeg_component_info *compptr;
michael@0 301
michael@0 302 /* Align the virtual buffers for the components used in this scan. */
michael@0 303 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
michael@0 304 compptr = cinfo->cur_comp_info[ci];
michael@0 305 buffer[ci] = (*cinfo->mem->access_virt_barray)
michael@0 306 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
michael@0 307 coef->iMCU_row_num * compptr->v_samp_factor,
michael@0 308 (JDIMENSION) compptr->v_samp_factor, FALSE);
michael@0 309 }
michael@0 310
michael@0 311 /* Loop to process one whole iMCU row */
michael@0 312 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
michael@0 313 yoffset++) {
michael@0 314 for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
michael@0 315 MCU_col_num++) {
michael@0 316 /* Construct list of pointers to DCT blocks belonging to this MCU */
michael@0 317 blkn = 0; /* index of current DCT block within MCU */
michael@0 318 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
michael@0 319 compptr = cinfo->cur_comp_info[ci];
michael@0 320 start_col = MCU_col_num * compptr->MCU_width;
michael@0 321 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
michael@0 322 : compptr->last_col_width;
michael@0 323 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
michael@0 324 if (coef->iMCU_row_num < last_iMCU_row ||
michael@0 325 yindex+yoffset < compptr->last_row_height) {
michael@0 326 /* Fill in pointers to real blocks in this row */
michael@0 327 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
michael@0 328 for (xindex = 0; xindex < blockcnt; xindex++)
michael@0 329 MCU_buffer[blkn++] = buffer_ptr++;
michael@0 330 } else {
michael@0 331 /* At bottom of image, need a whole row of dummy blocks */
michael@0 332 xindex = 0;
michael@0 333 }
michael@0 334 /* Fill in any dummy blocks needed in this row.
michael@0 335 * Dummy blocks are filled in the same way as in jccoefct.c:
michael@0 336 * all zeroes in the AC entries, DC entries equal to previous
michael@0 337 * block's DC value. The init routine has already zeroed the
michael@0 338 * AC entries, so we need only set the DC entries correctly.
michael@0 339 */
michael@0 340 for (; xindex < compptr->MCU_width; xindex++) {
michael@0 341 MCU_buffer[blkn] = coef->dummy_buffer[blkn];
michael@0 342 MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
michael@0 343 blkn++;
michael@0 344 }
michael@0 345 }
michael@0 346 }
michael@0 347 /* Try to write the MCU. */
michael@0 348 if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
michael@0 349 /* Suspension forced; update state counters and exit */
michael@0 350 coef->MCU_vert_offset = yoffset;
michael@0 351 coef->mcu_ctr = MCU_col_num;
michael@0 352 return FALSE;
michael@0 353 }
michael@0 354 }
michael@0 355 /* Completed an MCU row, but perhaps not an iMCU row */
michael@0 356 coef->mcu_ctr = 0;
michael@0 357 }
michael@0 358 /* Completed the iMCU row, advance counters for next one */
michael@0 359 coef->iMCU_row_num++;
michael@0 360 start_iMCU_row(cinfo);
michael@0 361 return TRUE;
michael@0 362 }
michael@0 363
michael@0 364
michael@0 365 /*
michael@0 366 * Initialize coefficient buffer controller.
michael@0 367 *
michael@0 368 * Each passed coefficient array must be the right size for that
michael@0 369 * coefficient: width_in_blocks wide and height_in_blocks high,
michael@0 370 * with unitheight at least v_samp_factor.
michael@0 371 */
michael@0 372
michael@0 373 LOCAL(void)
michael@0 374 transencode_coef_controller (j_compress_ptr cinfo,
michael@0 375 jvirt_barray_ptr * coef_arrays)
michael@0 376 {
michael@0 377 my_coef_ptr coef;
michael@0 378 JBLOCKROW buffer;
michael@0 379 int i;
michael@0 380
michael@0 381 coef = (my_coef_ptr)
michael@0 382 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 383 SIZEOF(my_coef_controller));
michael@0 384 cinfo->coef = (struct jpeg_c_coef_controller *) coef;
michael@0 385 coef->pub.start_pass = start_pass_coef;
michael@0 386 coef->pub.compress_data = compress_output;
michael@0 387
michael@0 388 /* Save pointer to virtual arrays */
michael@0 389 coef->whole_image = coef_arrays;
michael@0 390
michael@0 391 /* Allocate and pre-zero space for dummy DCT blocks. */
michael@0 392 buffer = (JBLOCKROW)
michael@0 393 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 394 C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
michael@0 395 jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
michael@0 396 for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
michael@0 397 coef->dummy_buffer[i] = buffer + i;
michael@0 398 }
michael@0 399 }

mercurial