istereo

annotate libs/libjpeg/jctrans.c @ 35:23e5d274b2a2

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