istereo2

annotate libs/libjpeg/jctrans.c @ 2:81d35769f546

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