DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

jctrans.cpp (14771B)


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