jdinput.c
Go to the documentation of this file.
00001 /*
00002  * jdinput.c
00003  *
00004  * Copyright (C) 1991-1997, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains input control logic for the JPEG decompressor.
00009  * These routines are concerned with controlling the decompressor's input
00010  * processing (marker reading and coefficient decoding).  The actual input
00011  * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
00012  */
00013 
00014 #define JPEG_INTERNALS
00015 #include "jinclude.h"
00016 #include "jpeglib.h"
00017 
00018 
00019 /* Private state */
00020 
00021 typedef struct {
00022   struct jpeg_input_controller pub; /* public fields */
00023 
00024   boolean inheaders;            /* TRUE until first SOS is reached */
00025 } my_input_controller;
00026 
00027 typedef my_input_controller * my_inputctl_ptr;
00028 
00029 
00030 /* Forward declarations */
00031 METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
00032 
00033 
00034 /*
00035  * Routines to calculate various quantities related to the size of the image.
00036  */
00037 
00038 LOCAL(void)
00039 initial_setup (j_decompress_ptr cinfo)
00040 /* Called once, when first SOS marker is reached */
00041 {
00042   int ci;
00043   jpeg_component_info *compptr;
00044 
00045   /* Make sure image isn't bigger than I can handle */
00046   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
00047       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
00048     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
00049 
00050   /* For now, precision must match compiled-in value... */
00051   if (cinfo->data_precision != BITS_IN_JSAMPLE)
00052     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
00053 
00054   /* Check that number of components won't exceed internal array sizes */
00055   if (cinfo->num_components > MAX_COMPONENTS)
00056     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
00057              MAX_COMPONENTS);
00058 
00059   /* Compute maximum sampling factors; check factor validity */
00060   cinfo->max_h_samp_factor = 1;
00061   cinfo->max_v_samp_factor = 1;
00062   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00063        ci++, compptr++) {
00064     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
00065         compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
00066       ERREXIT(cinfo, JERR_BAD_SAMPLING);
00067     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
00068                                    compptr->h_samp_factor);
00069     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
00070                                    compptr->v_samp_factor);
00071   }
00072 
00073   /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
00074    * In the full decompressor, this will be overridden by jdmaster.c;
00075    * but in the transcoder, jdmaster.c is not used, so we must do it here.
00076    */
00077   cinfo->min_DCT_scaled_size = DCTSIZE;
00078 
00079   /* Compute dimensions of components */
00080   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00081        ci++, compptr++) {
00082     compptr->DCT_scaled_size = DCTSIZE;
00083     /* Size in DCT blocks */
00084     compptr->width_in_blocks = (JDIMENSION)
00085       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
00086                     (long) (cinfo->max_h_samp_factor * DCTSIZE));
00087     compptr->height_in_blocks = (JDIMENSION)
00088       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
00089                     (long) (cinfo->max_v_samp_factor * DCTSIZE));
00090     /* downsampled_width and downsampled_height will also be overridden by
00091      * jdmaster.c if we are doing full decompression.  The transcoder library
00092      * doesn't use these values, but the calling application might.
00093      */
00094     /* Size in samples */
00095     compptr->downsampled_width = (JDIMENSION)
00096       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
00097                     (long) cinfo->max_h_samp_factor);
00098     compptr->downsampled_height = (JDIMENSION)
00099       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
00100                     (long) cinfo->max_v_samp_factor);
00101     /* Mark component needed, until color conversion says otherwise */
00102     compptr->component_needed = TRUE;
00103     /* Mark no quantization table yet saved for component */
00104     compptr->quant_table = NULL;
00105   }
00106 
00107   /* Compute number of fully interleaved MCU rows. */
00108   cinfo->total_iMCU_rows = (JDIMENSION)
00109     jdiv_round_up((long) cinfo->image_height,
00110                   (long) (cinfo->max_v_samp_factor*DCTSIZE));
00111 
00112   /* Decide whether file contains multiple scans */
00113   if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
00114     cinfo->inputctl->has_multiple_scans = TRUE;
00115   else
00116     cinfo->inputctl->has_multiple_scans = FALSE;
00117 }
00118 
00119 
00120 LOCAL(void)
00121 per_scan_setup (j_decompress_ptr cinfo)
00122 /* Do computations that are needed before processing a JPEG scan */
00123 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
00124 {
00125   int ci, mcublks, tmp;
00126   jpeg_component_info *compptr;
00127   
00128   if (cinfo->comps_in_scan == 1) {
00129     
00130     /* Noninterleaved (single-component) scan */
00131     compptr = cinfo->cur_comp_info[0];
00132     
00133     /* Overall image size in MCUs */
00134     cinfo->MCUs_per_row = compptr->width_in_blocks;
00135     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
00136     
00137     /* For noninterleaved scan, always one block per MCU */
00138     compptr->MCU_width = 1;
00139     compptr->MCU_height = 1;
00140     compptr->MCU_blocks = 1;
00141     compptr->MCU_sample_width = compptr->DCT_scaled_size;
00142     compptr->last_col_width = 1;
00143     /* For noninterleaved scans, it is convenient to define last_row_height
00144      * as the number of block rows present in the last iMCU row.
00145      */
00146     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
00147     if (tmp == 0) tmp = compptr->v_samp_factor;
00148     compptr->last_row_height = tmp;
00149     
00150     /* Prepare array describing MCU composition */
00151     cinfo->blocks_in_MCU = 1;
00152     cinfo->MCU_membership[0] = 0;
00153     
00154   } else {
00155     
00156     /* Interleaved (multi-component) scan */
00157     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
00158       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
00159                MAX_COMPS_IN_SCAN);
00160     
00161     /* Overall image size in MCUs */
00162     cinfo->MCUs_per_row = (JDIMENSION)
00163       jdiv_round_up((long) cinfo->image_width,
00164                     (long) (cinfo->max_h_samp_factor*DCTSIZE));
00165     cinfo->MCU_rows_in_scan = (JDIMENSION)
00166       jdiv_round_up((long) cinfo->image_height,
00167                     (long) (cinfo->max_v_samp_factor*DCTSIZE));
00168     
00169     cinfo->blocks_in_MCU = 0;
00170     
00171     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00172       compptr = cinfo->cur_comp_info[ci];
00173       /* Sampling factors give # of blocks of component in each MCU */
00174       compptr->MCU_width = compptr->h_samp_factor;
00175       compptr->MCU_height = compptr->v_samp_factor;
00176       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
00177       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
00178       /* Figure number of non-dummy blocks in last MCU column & row */
00179       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
00180       if (tmp == 0) tmp = compptr->MCU_width;
00181       compptr->last_col_width = tmp;
00182       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
00183       if (tmp == 0) tmp = compptr->MCU_height;
00184       compptr->last_row_height = tmp;
00185       /* Prepare array describing MCU composition */
00186       mcublks = compptr->MCU_blocks;
00187       if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
00188         ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
00189       while (mcublks-- > 0) {
00190         cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
00191       }
00192     }
00193     
00194   }
00195 }
00196 
00197 
00198 /*
00199  * Save away a copy of the Q-table referenced by each component present
00200  * in the current scan, unless already saved during a prior scan.
00201  *
00202  * In a multiple-scan JPEG file, the encoder could assign different components
00203  * the same Q-table slot number, but change table definitions between scans
00204  * so that each component uses a different Q-table.  (The IJG encoder is not
00205  * currently capable of doing this, but other encoders might.)  Since we want
00206  * to be able to dequantize all the components at the end of the file, this
00207  * means that we have to save away the table actually used for each component.
00208  * We do this by copying the table at the start of the first scan containing
00209  * the component.
00210  * The JPEG spec prohibits the encoder from changing the contents of a Q-table
00211  * slot between scans of a component using that slot.  If the encoder does so
00212  * anyway, this decoder will simply use the Q-table values that were current
00213  * at the start of the first scan for the component.
00214  *
00215  * The decompressor output side looks only at the saved quant tables,
00216  * not at the current Q-table slots.
00217  */
00218 
00219 LOCAL(void)
00220 latch_quant_tables (j_decompress_ptr cinfo)
00221 {
00222   int ci, qtblno;
00223   jpeg_component_info *compptr;
00224   JQUANT_TBL * qtbl;
00225 
00226   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00227     compptr = cinfo->cur_comp_info[ci];
00228     /* No work if we already saved Q-table for this component */
00229     if (compptr->quant_table != NULL)
00230       continue;
00231     /* Make sure specified quantization table is present */
00232     qtblno = compptr->quant_tbl_no;
00233     if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
00234         cinfo->quant_tbl_ptrs[qtblno] == NULL)
00235       ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
00236     /* OK, save away the quantization table */
00237     qtbl = (JQUANT_TBL *)
00238       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00239                                   SIZEOF(JQUANT_TBL));
00240     MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
00241     compptr->quant_table = qtbl;
00242   }
00243 }
00244 
00245 
00246 /*
00247  * Initialize the input modules to read a scan of compressed data.
00248  * The first call to this is done by jdmaster.c after initializing
00249  * the entire decompressor (during jpeg_start_decompress).
00250  * Subsequent calls come from consume_markers, below.
00251  */
00252 
00253 METHODDEF(void)
00254 start_input_pass (j_decompress_ptr cinfo)
00255 {
00256   per_scan_setup(cinfo);
00257   latch_quant_tables(cinfo);
00258   (*cinfo->entropy->start_pass) (cinfo);
00259   (*cinfo->coef->start_input_pass) (cinfo);
00260   cinfo->inputctl->consume_input = cinfo->coef->consume_data;
00261 }
00262 
00263 
00264 /*
00265  * Finish up after inputting a compressed-data scan.
00266  * This is called by the coefficient controller after it's read all
00267  * the expected data of the scan.
00268  */
00269 
00270 METHODDEF(void)
00271 finish_input_pass (j_decompress_ptr cinfo)
00272 {
00273   cinfo->inputctl->consume_input = consume_markers;
00274 }
00275 
00276 
00277 /*
00278  * Read JPEG markers before, between, or after compressed-data scans.
00279  * Change state as necessary when a new scan is reached.
00280  * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
00281  *
00282  * The consume_input method pointer points either here or to the
00283  * coefficient controller's consume_data routine, depending on whether
00284  * we are reading a compressed data segment or inter-segment markers.
00285  */
00286 
00287 METHODDEF(int)
00288 consume_markers (j_decompress_ptr cinfo)
00289 {
00290   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
00291   int val;
00292 
00293   if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
00294     return JPEG_REACHED_EOI;
00295 
00296   val = (*cinfo->marker->read_markers) (cinfo);
00297 
00298   switch (val) {
00299   case JPEG_REACHED_SOS:        /* Found SOS */
00300     if (inputctl->inheaders) {  /* 1st SOS */
00301       initial_setup(cinfo);
00302       inputctl->inheaders = FALSE;
00303       /* Note: start_input_pass must be called by jdmaster.c
00304        * before any more input can be consumed.  jdapimin.c is
00305        * responsible for enforcing this sequencing.
00306        */
00307     } else {                    /* 2nd or later SOS marker */
00308       if (! inputctl->pub.has_multiple_scans)
00309         ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
00310       start_input_pass(cinfo);
00311     }
00312     break;
00313   case JPEG_REACHED_EOI:        /* Found EOI */
00314     inputctl->pub.eoi_reached = TRUE;
00315     if (inputctl->inheaders) {  /* Tables-only datastream, apparently */
00316       if (cinfo->marker->saw_SOF)
00317         ERREXIT(cinfo, JERR_SOF_NO_SOS);
00318     } else {
00319       /* Prevent infinite loop in coef ctlr's decompress_data routine
00320        * if user set output_scan_number larger than number of scans.
00321        */
00322       if (cinfo->output_scan_number > cinfo->input_scan_number)
00323         cinfo->output_scan_number = cinfo->input_scan_number;
00324     }
00325     break;
00326   case JPEG_SUSPENDED:
00327     break;
00328   }
00329 
00330   return val;
00331 }
00332 
00333 
00334 /*
00335  * Reset state to begin a fresh datastream.
00336  */
00337 
00338 METHODDEF(void)
00339 reset_input_controller (j_decompress_ptr cinfo)
00340 {
00341   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
00342 
00343   inputctl->pub.consume_input = consume_markers;
00344   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
00345   inputctl->pub.eoi_reached = FALSE;
00346   inputctl->inheaders = TRUE;
00347   /* Reset other modules */
00348   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
00349   (*cinfo->marker->reset_marker_reader) (cinfo);
00350   /* Reset progression state -- would be cleaner if entropy decoder did this */
00351   cinfo->coef_bits = NULL;
00352 }
00353 
00354 
00355 /*
00356  * Initialize the input controller module.
00357  * This is called only once, when the decompression object is created.
00358  */
00359 
00360 GLOBAL(void)
00361 jinit_input_controller (j_decompress_ptr cinfo)
00362 {
00363   my_inputctl_ptr inputctl;
00364 
00365   /* Create subobject in permanent pool */
00366   inputctl = (my_inputctl_ptr)
00367     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00368                                 SIZEOF(my_input_controller));
00369   cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
00370   /* Initialize method pointers */
00371   inputctl->pub.consume_input = consume_markers;
00372   inputctl->pub.reset_input_controller = reset_input_controller;
00373   inputctl->pub.start_input_pass = start_input_pass;
00374   inputctl->pub.finish_input_pass = finish_input_pass;
00375   /* Initialize state: can't use reset_input_controller since we don't
00376    * want to try to reset other modules yet.
00377    */
00378   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
00379   inputctl->pub.eoi_reached = FALSE;
00380   inputctl->inheaders = TRUE;
00381 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:55