DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

jdinput.cpp (15500B)


      1 /*
      2  * jdinput.c
      3  *
      4  * Copyright (C) 1991-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 input control logic for the JPEG decompressor.
      9  * These routines are concerned with controlling the decompressor's input
     10  * processing (marker reading and coefficient decoding).  The actual input
     11  * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
     12  */
     13 
     14 #define JPEG_INTERNALS
     15 #include "jinclude.h"
     16 #include "jpeglib.h"
     17 
     18 
     19 /* Private state */
     20 
     21 typedef struct {
     22     struct jpeg_input_controller pub;/* public fields */
     23 
     24     boolean inheaders;      /* TRUE until first SOS is reached */
     25 } my_input_controller;
     26 
     27 typedef my_input_controller * my_inputctl_ptr;
     28 
     29 
     30 /* Forward declarations */
     31 METHODDEF int consume_markers JPP( (j_decompress_ptr cinfo) );
     32 
     33 
     34 /*
     35  * Routines to calculate various quantities related to the size of the image.
     36  */
     37 
     38 LOCAL void
     39 initial_setup( j_decompress_ptr cinfo ) {
     40 /* Called once, when first SOS marker is reached */
     41     int ci;
     42     jpeg_component_info * compptr;
     43 
     44     /* Make sure image isn't bigger than I can handle */
     45     if ( ( (long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ) ||
     46         ( (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION ) ) {
     47         ERREXIT1( cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION );
     48     }
     49 
     50     /* For now, precision must match compiled-in value... */
     51     if ( cinfo->data_precision != BITS_IN_JSAMPLE ) {
     52         ERREXIT1( cinfo, JERR_BAD_PRECISION, cinfo->data_precision );
     53     }
     54 
     55     /* Check that number of components won't exceed internal array sizes */
     56     if ( cinfo->num_components > MAX_COMPONENTS ) {
     57         ERREXIT2( cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
     58                   MAX_COMPONENTS );
     59     }
     60 
     61     /* Compute maximum sampling factors; check factor validity */
     62     cinfo->max_h_samp_factor = 1;
     63     cinfo->max_v_samp_factor = 1;
     64     for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
     65           ci++, compptr++ ) {
     66         if ( ( compptr->h_samp_factor <= 0 ) || ( compptr->h_samp_factor > MAX_SAMP_FACTOR ) ||
     67             ( compptr->v_samp_factor <= 0 ) || ( compptr->v_samp_factor > MAX_SAMP_FACTOR ) ) {
     68             ERREXIT( cinfo, JERR_BAD_SAMPLING );
     69         }
     70         cinfo->max_h_samp_factor = MAX( cinfo->max_h_samp_factor,
     71                                         compptr->h_samp_factor );
     72         cinfo->max_v_samp_factor = MAX( cinfo->max_v_samp_factor,
     73                                         compptr->v_samp_factor );
     74     }
     75 
     76     /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
     77      * In the full decompressor, this will be overridden by jdmaster.c;
     78      * but in the transcoder, jdmaster.c is not used, so we must do it here.
     79      */
     80     cinfo->min_DCT_scaled_size = DCTSIZE;
     81 
     82     /* Compute dimensions of components */
     83     for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
     84           ci++, compptr++ ) {
     85         compptr->DCT_scaled_size = DCTSIZE;
     86         /* Size in DCT blocks */
     87         compptr->width_in_blocks = (JDIMENSION)
     88                                    jdiv_round_up( (long) cinfo->image_width * (long) compptr->h_samp_factor,
     89                                                  (long) ( cinfo->max_h_samp_factor * DCTSIZE ) );
     90         compptr->height_in_blocks = (JDIMENSION)
     91                                     jdiv_round_up( (long) cinfo->image_height * (long) compptr->v_samp_factor,
     92                                                   (long) ( cinfo->max_v_samp_factor * DCTSIZE ) );
     93         /* downsampled_width and downsampled_height will also be overridden by
     94          * jdmaster.c if we are doing full decompression.  The transcoder library
     95          * doesn't use these values, but the calling application might.
     96          */
     97         /* Size in samples */
     98         compptr->downsampled_width = (JDIMENSION)
     99                                      jdiv_round_up( (long) cinfo->image_width * (long) compptr->h_samp_factor,
    100                                                    (long) cinfo->max_h_samp_factor );
    101         compptr->downsampled_height = (JDIMENSION)
    102                                       jdiv_round_up( (long) cinfo->image_height * (long) compptr->v_samp_factor,
    103                                                     (long) cinfo->max_v_samp_factor );
    104         /* Mark component needed, until color conversion says otherwise */
    105         compptr->component_needed = TRUE;
    106         /* Mark no quantization table yet saved for component */
    107         compptr->quant_table = NULL;
    108     }
    109 
    110     /* Compute number of fully interleaved MCU rows. */
    111     cinfo->total_iMCU_rows = (JDIMENSION)
    112                              jdiv_round_up( (long) cinfo->image_height,
    113                                            (long) ( cinfo->max_v_samp_factor * DCTSIZE ) );
    114 
    115     /* Decide whether file contains multiple scans */
    116     if ( ( cinfo->comps_in_scan < cinfo->num_components ) || ( cinfo->progressive_mode ) ) {
    117         cinfo->inputctl->has_multiple_scans = TRUE;
    118     } else {
    119         cinfo->inputctl->has_multiple_scans = FALSE;
    120     }
    121 }
    122 
    123 
    124 LOCAL void
    125 per_scan_setup( j_decompress_ptr cinfo ) {
    126 /* Do computations that are needed before processing a JPEG scan */
    127 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
    128     int ci, mcublks, tmp;
    129     jpeg_component_info * compptr;
    130 
    131     if ( cinfo->comps_in_scan == 1 ) {
    132 
    133         /* Noninterleaved (single-component) scan */
    134         compptr = cinfo->cur_comp_info[0];
    135 
    136         /* Overall image size in MCUs */
    137         cinfo->MCUs_per_row = compptr->width_in_blocks;
    138         cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
    139 
    140         /* For noninterleaved scan, always one block per MCU */
    141         compptr->MCU_width = 1;
    142         compptr->MCU_height = 1;
    143         compptr->MCU_blocks = 1;
    144         compptr->MCU_sample_width = compptr->DCT_scaled_size;
    145         compptr->last_col_width = 1;
    146         /* For noninterleaved scans, it is convenient to define last_row_height
    147          * as the number of block rows present in the last iMCU row.
    148          */
    149         tmp = (int) ( compptr->height_in_blocks % compptr->v_samp_factor );
    150         if ( tmp == 0 ) {
    151             tmp = compptr->v_samp_factor;
    152         }
    153         compptr->last_row_height = tmp;
    154 
    155         /* Prepare array describing MCU composition */
    156         cinfo->blocks_in_MCU = 1;
    157         cinfo->MCU_membership[0] = 0;
    158 
    159     } else {
    160 
    161         /* Interleaved (multi-component) scan */
    162         if ( ( cinfo->comps_in_scan <= 0 ) || ( cinfo->comps_in_scan > MAX_COMPS_IN_SCAN ) ) {
    163             ERREXIT2( cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
    164                       MAX_COMPS_IN_SCAN );
    165         }
    166 
    167         /* Overall image size in MCUs */
    168         cinfo->MCUs_per_row = (JDIMENSION)
    169                               jdiv_round_up( (long) cinfo->image_width,
    170                                             (long) ( cinfo->max_h_samp_factor * DCTSIZE ) );
    171         cinfo->MCU_rows_in_scan = (JDIMENSION)
    172                                   jdiv_round_up( (long) cinfo->image_height,
    173                                                 (long) ( cinfo->max_v_samp_factor * DCTSIZE ) );
    174 
    175         cinfo->blocks_in_MCU = 0;
    176 
    177         for ( ci = 0; ci < cinfo->comps_in_scan; ci++ ) {
    178             compptr = cinfo->cur_comp_info[ci];
    179             /* Sampling factors give # of blocks of component in each MCU */
    180             compptr->MCU_width = compptr->h_samp_factor;
    181             compptr->MCU_height = compptr->v_samp_factor;
    182             compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
    183             compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
    184             /* Figure number of non-dummy blocks in last MCU column & row */
    185             tmp = (int) ( compptr->width_in_blocks % compptr->MCU_width );
    186             if ( tmp == 0 ) {
    187                 tmp = compptr->MCU_width;
    188             }
    189             compptr->last_col_width = tmp;
    190             tmp = (int) ( compptr->height_in_blocks % compptr->MCU_height );
    191             if ( tmp == 0 ) {
    192                 tmp = compptr->MCU_height;
    193             }
    194             compptr->last_row_height = tmp;
    195             /* Prepare array describing MCU composition */
    196             mcublks = compptr->MCU_blocks;
    197             if ( cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU ) {
    198                 ERREXIT( cinfo, JERR_BAD_MCU_SIZE );
    199             }
    200             while ( mcublks-- > 0 ) {
    201                 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
    202             }
    203         }
    204 
    205     }
    206 }
    207 
    208 
    209 /*
    210  * Save away a copy of the Q-table referenced by each component present
    211  * in the current scan, unless already saved during a prior scan.
    212  *
    213  * In a multiple-scan JPEG file, the encoder could assign different components
    214  * the same Q-table slot number, but change table definitions between scans
    215  * so that each component uses a different Q-table.  (The IJG encoder is not
    216  * currently capable of doing this, but other encoders might.)  Since we want
    217  * to be able to dequantize all the components at the end of the file, this
    218  * means that we have to save away the table actually used for each component.
    219  * We do this by copying the table at the start of the first scan containing
    220  * the component.
    221  * The JPEG spec prohibits the encoder from changing the contents of a Q-table
    222  * slot between scans of a component using that slot.  If the encoder does so
    223  * anyway, this decoder will simply use the Q-table values that were current
    224  * at the start of the first scan for the component.
    225  *
    226  * The decompressor output side looks only at the saved quant tables,
    227  * not at the current Q-table slots.
    228  */
    229 
    230 LOCAL void
    231 latch_quant_tables( j_decompress_ptr cinfo ) {
    232     int ci, qtblno;
    233     jpeg_component_info * compptr;
    234     JQUANT_TBL * qtbl;
    235 
    236     for ( ci = 0; ci < cinfo->comps_in_scan; ci++ ) {
    237         compptr = cinfo->cur_comp_info[ci];
    238         /* No work if we already saved Q-table for this component */
    239         if ( compptr->quant_table != NULL ) {
    240             continue;
    241         }
    242         /* Make sure specified quantization table is present */
    243         qtblno = compptr->quant_tbl_no;
    244         if ( ( qtblno < 0 ) || ( qtblno >= NUM_QUANT_TBLS ) ||
    245             ( cinfo->quant_tbl_ptrs[qtblno] == NULL ) ) {
    246             ERREXIT1( cinfo, JERR_NO_QUANT_TABLE, qtblno );
    247         }
    248         /* OK, save away the quantization table */
    249         qtbl = (JQUANT_TBL *)
    250                ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
    251                                             SIZEOF( JQUANT_TBL ) );
    252         MEMCOPY( qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF( JQUANT_TBL ) );
    253         compptr->quant_table = qtbl;
    254     }
    255 }
    256 
    257 
    258 /*
    259  * Initialize the input modules to read a scan of compressed data.
    260  * The first call to this is done by jdmaster.c after initializing
    261  * the entire decompressor (during jpeg_start_decompress).
    262  * Subsequent calls come from consume_markers, below.
    263  */
    264 
    265 METHODDEF void
    266 start_input_pass( j_decompress_ptr cinfo ) {
    267     per_scan_setup( cinfo );
    268     latch_quant_tables( cinfo );
    269     ( *cinfo->entropy->start_pass )( cinfo );
    270     ( *cinfo->coef->start_input_pass )( cinfo );
    271     cinfo->inputctl->consume_input = cinfo->coef->consume_data;
    272 }
    273 
    274 
    275 /*
    276  * Finish up after inputting a compressed-data scan.
    277  * This is called by the coefficient controller after it's read all
    278  * the expected data of the scan.
    279  */
    280 
    281 METHODDEF void
    282 finish_input_pass( j_decompress_ptr cinfo ) {
    283     cinfo->inputctl->consume_input = consume_markers;
    284 }
    285 
    286 
    287 /*
    288  * Read JPEG markers before, between, or after compressed-data scans.
    289  * Change state as necessary when a new scan is reached.
    290  * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
    291  *
    292  * The consume_input method pointer points either here or to the
    293  * coefficient controller's consume_data routine, depending on whether
    294  * we are reading a compressed data segment or inter-segment markers.
    295  */
    296 
    297 METHODDEF int
    298 consume_markers( j_decompress_ptr cinfo ) {
    299     my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
    300     int val;
    301 
    302     if ( inputctl->pub.eoi_reached ) {/* After hitting EOI, read no further */
    303         return JPEG_REACHED_EOI;
    304     }
    305 
    306     val = ( *cinfo->marker->read_markers )( cinfo );
    307 
    308     switch ( val ) {
    309         case JPEG_REACHED_SOS:/* Found SOS */
    310             if ( inputctl->inheaders ) {/* 1st SOS */
    311                 initial_setup( cinfo );
    312                 inputctl->inheaders = FALSE;
    313                 /* Note: start_input_pass must be called by jdmaster.c
    314                  * before any more input can be consumed.  jdapi.c is
    315                  * responsible for enforcing this sequencing.
    316                  */
    317             } else {    /* 2nd or later SOS marker */
    318                 if ( !inputctl->pub.has_multiple_scans ) {
    319                     ERREXIT( cinfo, JERR_EOI_EXPECTED );
    320                 }                      /* Oops, I wasn't expecting this! */
    321                 start_input_pass( cinfo );
    322             }
    323             break;
    324         case JPEG_REACHED_EOI:/* Found EOI */
    325             inputctl->pub.eoi_reached = TRUE;
    326             if ( inputctl->inheaders ) {/* Tables-only datastream, apparently */
    327                 if ( cinfo->marker->saw_SOF ) {
    328                     ERREXIT( cinfo, JERR_SOF_NO_SOS );
    329                 }
    330             } else {
    331                 /* Prevent infinite loop in coef ctlr's decompress_data routine
    332                  * if user set output_scan_number larger than number of scans.
    333                  */
    334                 if ( cinfo->output_scan_number > cinfo->input_scan_number ) {
    335                     cinfo->output_scan_number = cinfo->input_scan_number;
    336                 }
    337             }
    338             break;
    339         case JPEG_SUSPENDED:
    340             break;
    341     }
    342 
    343     return val;
    344 }
    345 
    346 
    347 /*
    348  * Reset state to begin a fresh datastream.
    349  */
    350 
    351 METHODDEF void
    352 reset_input_controller( j_decompress_ptr cinfo ) {
    353     my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
    354 
    355     inputctl->pub.consume_input = consume_markers;
    356     inputctl->pub.has_multiple_scans = FALSE;/* "unknown" would be better */
    357     inputctl->pub.eoi_reached = FALSE;
    358     inputctl->inheaders = TRUE;
    359     /* Reset other modules */
    360     ( *cinfo->err->reset_error_mgr )( (j_common_ptr) cinfo );
    361     ( *cinfo->marker->reset_marker_reader )( cinfo );
    362     /* Reset progression state -- would be cleaner if entropy decoder did this */
    363     cinfo->coef_bits = NULL;
    364 }
    365 
    366 
    367 /*
    368  * Initialize the input controller module.
    369  * This is called only once, when the decompression object is created.
    370  */
    371 
    372 GLOBAL void
    373 jinit_input_controller( j_decompress_ptr cinfo ) {
    374     my_inputctl_ptr inputctl;
    375 
    376     /* Create subobject in permanent pool */
    377     inputctl = (my_inputctl_ptr)
    378                ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_PERMANENT,
    379                                             SIZEOF( my_input_controller ) );
    380     cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
    381     /* Initialize method pointers */
    382     inputctl->pub.consume_input = consume_markers;
    383     inputctl->pub.reset_input_controller = reset_input_controller;
    384     inputctl->pub.start_input_pass = start_input_pass;
    385     inputctl->pub.finish_input_pass = finish_input_pass;
    386     /* Initialize state: can't use reset_input_controller since we don't
    387      * want to try to reset other modules yet.
    388      */
    389     inputctl->pub.has_multiple_scans = FALSE;/* "unknown" would be better */
    390     inputctl->pub.eoi_reached = FALSE;
    391     inputctl->inheaders = TRUE;
    392 }