jdinput.c

Go to the documentation of this file.
00001 /*
00002  * jdinput.c
00003  *
00004  * Copyright (C) 1991-1997, Thomas G. Lane.
00005  * Modified 2002-2009 by Guido Vollbeding.
00006  * This file is part of the Independent JPEG Group's software.
00007  * For conditions of distribution and use, see the accompanying README file.
00008  *
00009  * This file contains input control logic for the JPEG decompressor.
00010  * These routines are concerned with controlling the decompressor's input
00011  * processing (marker reading and coefficient decoding).  The actual input
00012  * reading is done in jdmarker.c, jdhuff.c, and jdarith.c.
00013  */
00014 
00015 #define JPEG_INTERNALS
00016 #include "jinclude.h"
00017 #include "jpeglib.h"
00018 
00019 
00020 /* Private state */
00021 
00022 typedef struct {
00023   struct jpeg_input_controller pub; /* public fields */
00024 
00025   int inheaders;                /* Nonzero until first SOS is reached */
00026 } my_input_controller;
00027 
00028 typedef my_input_controller * my_inputctl_ptr;
00029 
00030 
00031 /* Forward declarations */
00032 METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
00033 
00034 
00035 /*
00036  * Routines to calculate various quantities related to the size of the image.
00037  */
00038 
00039 
00040 /*
00041  * Compute output image dimensions and related values.
00042  * NOTE: this is exported for possible use by application.
00043  * Hence it mustn't do anything that can't be done twice.
00044  */
00045 
00046 GLOBAL(void)
00047 jpeg_core_output_dimensions (j_decompress_ptr cinfo)
00048 /* Do computations that are needed before master selection phase.
00049  * This function is used for transcoding and full decompression.
00050  */
00051 {
00052 #ifdef IDCT_SCALING_SUPPORTED
00053   int ci;
00054   jpeg_component_info *compptr;
00055 
00056   /* Compute actual output image dimensions and DCT scaling choices. */
00057   if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) {
00058     /* Provide 1/block_size scaling */
00059     cinfo->output_width = (JDIMENSION)
00060       jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size);
00061     cinfo->output_height = (JDIMENSION)
00062       jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size);
00063     cinfo->min_DCT_h_scaled_size = 1;
00064     cinfo->min_DCT_v_scaled_size = 1;
00065   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) {
00066     /* Provide 2/block_size scaling */
00067     cinfo->output_width = (JDIMENSION)
00068       jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size);
00069     cinfo->output_height = (JDIMENSION)
00070       jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size);
00071     cinfo->min_DCT_h_scaled_size = 2;
00072     cinfo->min_DCT_v_scaled_size = 2;
00073   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 3) {
00074     /* Provide 3/block_size scaling */
00075     cinfo->output_width = (JDIMENSION)
00076       jdiv_round_up((long) cinfo->image_width * 3L, (long) cinfo->block_size);
00077     cinfo->output_height = (JDIMENSION)
00078       jdiv_round_up((long) cinfo->image_height * 3L, (long) cinfo->block_size);
00079     cinfo->min_DCT_h_scaled_size = 3;
00080     cinfo->min_DCT_v_scaled_size = 3;
00081   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) {
00082     /* Provide 4/block_size scaling */
00083     cinfo->output_width = (JDIMENSION)
00084       jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size);
00085     cinfo->output_height = (JDIMENSION)
00086       jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size);
00087     cinfo->min_DCT_h_scaled_size = 4;
00088     cinfo->min_DCT_v_scaled_size = 4;
00089   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 5) {
00090     /* Provide 5/block_size scaling */
00091     cinfo->output_width = (JDIMENSION)
00092       jdiv_round_up((long) cinfo->image_width * 5L, (long) cinfo->block_size);
00093     cinfo->output_height = (JDIMENSION)
00094       jdiv_round_up((long) cinfo->image_height * 5L, (long) cinfo->block_size);
00095     cinfo->min_DCT_h_scaled_size = 5;
00096     cinfo->min_DCT_v_scaled_size = 5;
00097   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 6) {
00098     /* Provide 6/block_size scaling */
00099     cinfo->output_width = (JDIMENSION)
00100       jdiv_round_up((long) cinfo->image_width * 6L, (long) cinfo->block_size);
00101     cinfo->output_height = (JDIMENSION)
00102       jdiv_round_up((long) cinfo->image_height * 6L, (long) cinfo->block_size);
00103     cinfo->min_DCT_h_scaled_size = 6;
00104     cinfo->min_DCT_v_scaled_size = 6;
00105   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 7) {
00106     /* Provide 7/block_size scaling */
00107     cinfo->output_width = (JDIMENSION)
00108       jdiv_round_up((long) cinfo->image_width * 7L, (long) cinfo->block_size);
00109     cinfo->output_height = (JDIMENSION)
00110       jdiv_round_up((long) cinfo->image_height * 7L, (long) cinfo->block_size);
00111     cinfo->min_DCT_h_scaled_size = 7;
00112     cinfo->min_DCT_v_scaled_size = 7;
00113   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) {
00114     /* Provide 8/block_size scaling */
00115     cinfo->output_width = (JDIMENSION)
00116       jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size);
00117     cinfo->output_height = (JDIMENSION)
00118       jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size);
00119     cinfo->min_DCT_h_scaled_size = 8;
00120     cinfo->min_DCT_v_scaled_size = 8;
00121   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 9) {
00122     /* Provide 9/block_size scaling */
00123     cinfo->output_width = (JDIMENSION)
00124       jdiv_round_up((long) cinfo->image_width * 9L, (long) cinfo->block_size);
00125     cinfo->output_height = (JDIMENSION)
00126       jdiv_round_up((long) cinfo->image_height * 9L, (long) cinfo->block_size);
00127     cinfo->min_DCT_h_scaled_size = 9;
00128     cinfo->min_DCT_v_scaled_size = 9;
00129   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 10) {
00130     /* Provide 10/block_size scaling */
00131     cinfo->output_width = (JDIMENSION)
00132       jdiv_round_up((long) cinfo->image_width * 10L, (long) cinfo->block_size);
00133     cinfo->output_height = (JDIMENSION)
00134       jdiv_round_up((long) cinfo->image_height * 10L, (long) cinfo->block_size);
00135     cinfo->min_DCT_h_scaled_size = 10;
00136     cinfo->min_DCT_v_scaled_size = 10;
00137   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 11) {
00138     /* Provide 11/block_size scaling */
00139     cinfo->output_width = (JDIMENSION)
00140       jdiv_round_up((long) cinfo->image_width * 11L, (long) cinfo->block_size);
00141     cinfo->output_height = (JDIMENSION)
00142       jdiv_round_up((long) cinfo->image_height * 11L, (long) cinfo->block_size);
00143     cinfo->min_DCT_h_scaled_size = 11;
00144     cinfo->min_DCT_v_scaled_size = 11;
00145   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 12) {
00146     /* Provide 12/block_size scaling */
00147     cinfo->output_width = (JDIMENSION)
00148       jdiv_round_up((long) cinfo->image_width * 12L, (long) cinfo->block_size);
00149     cinfo->output_height = (JDIMENSION)
00150       jdiv_round_up((long) cinfo->image_height * 12L, (long) cinfo->block_size);
00151     cinfo->min_DCT_h_scaled_size = 12;
00152     cinfo->min_DCT_v_scaled_size = 12;
00153   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 13) {
00154     /* Provide 13/block_size scaling */
00155     cinfo->output_width = (JDIMENSION)
00156       jdiv_round_up((long) cinfo->image_width * 13L, (long) cinfo->block_size);
00157     cinfo->output_height = (JDIMENSION)
00158       jdiv_round_up((long) cinfo->image_height * 13L, (long) cinfo->block_size);
00159     cinfo->min_DCT_h_scaled_size = 13;
00160     cinfo->min_DCT_v_scaled_size = 13;
00161   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 14) {
00162     /* Provide 14/block_size scaling */
00163     cinfo->output_width = (JDIMENSION)
00164       jdiv_round_up((long) cinfo->image_width * 14L, (long) cinfo->block_size);
00165     cinfo->output_height = (JDIMENSION)
00166       jdiv_round_up((long) cinfo->image_height * 14L, (long) cinfo->block_size);
00167     cinfo->min_DCT_h_scaled_size = 14;
00168     cinfo->min_DCT_v_scaled_size = 14;
00169   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 15) {
00170     /* Provide 15/block_size scaling */
00171     cinfo->output_width = (JDIMENSION)
00172       jdiv_round_up((long) cinfo->image_width * 15L, (long) cinfo->block_size);
00173     cinfo->output_height = (JDIMENSION)
00174       jdiv_round_up((long) cinfo->image_height * 15L, (long) cinfo->block_size);
00175     cinfo->min_DCT_h_scaled_size = 15;
00176     cinfo->min_DCT_v_scaled_size = 15;
00177   } else {
00178     /* Provide 16/block_size scaling */
00179     cinfo->output_width = (JDIMENSION)
00180       jdiv_round_up((long) cinfo->image_width * 16L, (long) cinfo->block_size);
00181     cinfo->output_height = (JDIMENSION)
00182       jdiv_round_up((long) cinfo->image_height * 16L, (long) cinfo->block_size);
00183     cinfo->min_DCT_h_scaled_size = 16;
00184     cinfo->min_DCT_v_scaled_size = 16;
00185   }
00186 
00187   /* Recompute dimensions of components */
00188   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00189        ci++, compptr++) {
00190     compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size;
00191     compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size;
00192   }
00193 
00194 #else /* !IDCT_SCALING_SUPPORTED */
00195 
00196   /* Hardwire it to "no scaling" */
00197   cinfo->output_width = cinfo->image_width;
00198   cinfo->output_height = cinfo->image_height;
00199   /* jdinput.c has already initialized DCT_scaled_size,
00200    * and has computed unscaled downsampled_width and downsampled_height.
00201    */
00202 
00203 #endif /* IDCT_SCALING_SUPPORTED */
00204 }
00205 
00206 
00207 LOCAL(void)
00208 initial_setup (j_decompress_ptr cinfo)
00209 /* Called once, when first SOS marker is reached */
00210 {
00211   int ci;
00212   jpeg_component_info *compptr;
00213 
00214   /* Make sure image isn't bigger than I can handle */
00215   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
00216       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
00217     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
00218 
00219   /* For now, precision must match compiled-in value... */
00220   if (cinfo->data_precision != BITS_IN_JSAMPLE)
00221     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
00222 
00223   /* Check that number of components won't exceed internal array sizes */
00224   if (cinfo->num_components > MAX_COMPONENTS)
00225     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
00226              MAX_COMPONENTS);
00227 
00228   /* Compute maximum sampling factors; check factor validity */
00229   cinfo->max_h_samp_factor = 1;
00230   cinfo->max_v_samp_factor = 1;
00231   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00232        ci++, compptr++) {
00233     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
00234         compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
00235       ERREXIT(cinfo, JERR_BAD_SAMPLING);
00236     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
00237                                    compptr->h_samp_factor);
00238     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
00239                                    compptr->v_samp_factor);
00240   }
00241 
00242   /* Derive block_size, natural_order, and lim_Se */
00243   if (cinfo->is_baseline || (cinfo->progressive_mode &&
00244       cinfo->comps_in_scan)) { /* no pseudo SOS marker */
00245     cinfo->block_size = DCTSIZE;
00246     cinfo->natural_order = jpeg_natural_order;
00247     cinfo->lim_Se = DCTSIZE2-1;
00248   } else
00249     switch (cinfo->Se) {
00250     case (1*1-1):
00251       cinfo->block_size = 1;
00252       cinfo->natural_order = jpeg_natural_order; /* not needed */
00253       cinfo->lim_Se = cinfo->Se;
00254       break;
00255     case (2*2-1):
00256       cinfo->block_size = 2;
00257       cinfo->natural_order = jpeg_natural_order2;
00258       cinfo->lim_Se = cinfo->Se;
00259       break;
00260     case (3*3-1):
00261       cinfo->block_size = 3;
00262       cinfo->natural_order = jpeg_natural_order3;
00263       cinfo->lim_Se = cinfo->Se;
00264       break;
00265     case (4*4-1):
00266       cinfo->block_size = 4;
00267       cinfo->natural_order = jpeg_natural_order4;
00268       cinfo->lim_Se = cinfo->Se;
00269       break;
00270     case (5*5-1):
00271       cinfo->block_size = 5;
00272       cinfo->natural_order = jpeg_natural_order5;
00273       cinfo->lim_Se = cinfo->Se;
00274       break;
00275     case (6*6-1):
00276       cinfo->block_size = 6;
00277       cinfo->natural_order = jpeg_natural_order6;
00278       cinfo->lim_Se = cinfo->Se;
00279       break;
00280     case (7*7-1):
00281       cinfo->block_size = 7;
00282       cinfo->natural_order = jpeg_natural_order7;
00283       cinfo->lim_Se = cinfo->Se;
00284       break;
00285     case (8*8-1):
00286       cinfo->block_size = 8;
00287       cinfo->natural_order = jpeg_natural_order;
00288       cinfo->lim_Se = DCTSIZE2-1;
00289       break;
00290     case (9*9-1):
00291       cinfo->block_size = 9;
00292       cinfo->natural_order = jpeg_natural_order;
00293       cinfo->lim_Se = DCTSIZE2-1;
00294       break;
00295     case (10*10-1):
00296       cinfo->block_size = 10;
00297       cinfo->natural_order = jpeg_natural_order;
00298       cinfo->lim_Se = DCTSIZE2-1;
00299       break;
00300     case (11*11-1):
00301       cinfo->block_size = 11;
00302       cinfo->natural_order = jpeg_natural_order;
00303       cinfo->lim_Se = DCTSIZE2-1;
00304       break;
00305     case (12*12-1):
00306       cinfo->block_size = 12;
00307       cinfo->natural_order = jpeg_natural_order;
00308       cinfo->lim_Se = DCTSIZE2-1;
00309       break;
00310     case (13*13-1):
00311       cinfo->block_size = 13;
00312       cinfo->natural_order = jpeg_natural_order;
00313       cinfo->lim_Se = DCTSIZE2-1;
00314       break;
00315     case (14*14-1):
00316       cinfo->block_size = 14;
00317       cinfo->natural_order = jpeg_natural_order;
00318       cinfo->lim_Se = DCTSIZE2-1;
00319       break;
00320     case (15*15-1):
00321       cinfo->block_size = 15;
00322       cinfo->natural_order = jpeg_natural_order;
00323       cinfo->lim_Se = DCTSIZE2-1;
00324       break;
00325     case (16*16-1):
00326       cinfo->block_size = 16;
00327       cinfo->natural_order = jpeg_natural_order;
00328       cinfo->lim_Se = DCTSIZE2-1;
00329       break;
00330     default:
00331       ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
00332                cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
00333       break;
00334     }
00335 
00336   /* We initialize DCT_scaled_size and min_DCT_scaled_size to block_size.
00337    * In the full decompressor,
00338    * this will be overridden by jpeg_calc_output_dimensions in jdmaster.c;
00339    * but in the transcoder,
00340    * jpeg_calc_output_dimensions is not used, so we must do it here.
00341    */
00342   cinfo->min_DCT_h_scaled_size = cinfo->block_size;
00343   cinfo->min_DCT_v_scaled_size = cinfo->block_size;
00344 
00345   /* Compute dimensions of components */
00346   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00347        ci++, compptr++) {
00348     compptr->DCT_h_scaled_size = cinfo->block_size;
00349     compptr->DCT_v_scaled_size = cinfo->block_size;
00350     /* Size in DCT blocks */
00351     compptr->width_in_blocks = (JDIMENSION)
00352       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
00353                     (long) (cinfo->max_h_samp_factor * cinfo->block_size));
00354     compptr->height_in_blocks = (JDIMENSION)
00355       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
00356                     (long) (cinfo->max_v_samp_factor * cinfo->block_size));
00357     /* downsampled_width and downsampled_height will also be overridden by
00358      * jdmaster.c if we are doing full decompression.  The transcoder library
00359      * doesn't use these values, but the calling application might.
00360      */
00361     /* Size in samples */
00362     compptr->downsampled_width = (JDIMENSION)
00363       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
00364                     (long) cinfo->max_h_samp_factor);
00365     compptr->downsampled_height = (JDIMENSION)
00366       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
00367                     (long) cinfo->max_v_samp_factor);
00368     /* Mark component needed, until color conversion says otherwise */
00369     compptr->component_needed = TRUE;
00370     /* Mark no quantization table yet saved for component */
00371     compptr->quant_table = NULL;
00372   }
00373 
00374   /* Compute number of fully interleaved MCU rows. */
00375   cinfo->total_iMCU_rows = (JDIMENSION)
00376     jdiv_round_up((long) cinfo->image_height,
00377                   (long) (cinfo->max_v_samp_factor * cinfo->block_size));
00378 
00379   /* Decide whether file contains multiple scans */
00380   if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
00381     cinfo->inputctl->has_multiple_scans = TRUE;
00382   else
00383     cinfo->inputctl->has_multiple_scans = FALSE;
00384 }
00385 
00386 
00387 LOCAL(void)
00388 per_scan_setup (j_decompress_ptr cinfo)
00389 /* Do computations that are needed before processing a JPEG scan */
00390 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
00391 {
00392   int ci, mcublks, tmp;
00393   jpeg_component_info *compptr;
00394   
00395   if (cinfo->comps_in_scan == 1) {
00396     
00397     /* Noninterleaved (single-component) scan */
00398     compptr = cinfo->cur_comp_info[0];
00399     
00400     /* Overall image size in MCUs */
00401     cinfo->MCUs_per_row = compptr->width_in_blocks;
00402     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
00403     
00404     /* For noninterleaved scan, always one block per MCU */
00405     compptr->MCU_width = 1;
00406     compptr->MCU_height = 1;
00407     compptr->MCU_blocks = 1;
00408     compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
00409     compptr->last_col_width = 1;
00410     /* For noninterleaved scans, it is convenient to define last_row_height
00411      * as the number of block rows present in the last iMCU row.
00412      */
00413     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
00414     if (tmp == 0) tmp = compptr->v_samp_factor;
00415     compptr->last_row_height = tmp;
00416     
00417     /* Prepare array describing MCU composition */
00418     cinfo->blocks_in_MCU = 1;
00419     cinfo->MCU_membership[0] = 0;
00420     
00421   } else {
00422     
00423     /* Interleaved (multi-component) scan */
00424     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
00425       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
00426                MAX_COMPS_IN_SCAN);
00427     
00428     /* Overall image size in MCUs */
00429     cinfo->MCUs_per_row = (JDIMENSION)
00430       jdiv_round_up((long) cinfo->image_width,
00431                     (long) (cinfo->max_h_samp_factor * cinfo->block_size));
00432     cinfo->MCU_rows_in_scan = (JDIMENSION)
00433       jdiv_round_up((long) cinfo->image_height,
00434                     (long) (cinfo->max_v_samp_factor * cinfo->block_size));
00435     
00436     cinfo->blocks_in_MCU = 0;
00437     
00438     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00439       compptr = cinfo->cur_comp_info[ci];
00440       /* Sampling factors give # of blocks of component in each MCU */
00441       compptr->MCU_width = compptr->h_samp_factor;
00442       compptr->MCU_height = compptr->v_samp_factor;
00443       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
00444       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
00445       /* Figure number of non-dummy blocks in last MCU column & row */
00446       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
00447       if (tmp == 0) tmp = compptr->MCU_width;
00448       compptr->last_col_width = tmp;
00449       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
00450       if (tmp == 0) tmp = compptr->MCU_height;
00451       compptr->last_row_height = tmp;
00452       /* Prepare array describing MCU composition */
00453       mcublks = compptr->MCU_blocks;
00454       if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
00455         ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
00456       while (mcublks-- > 0) {
00457         cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
00458       }
00459     }
00460     
00461   }
00462 }
00463 
00464 
00465 /*
00466  * Save away a copy of the Q-table referenced by each component present
00467  * in the current scan, unless already saved during a prior scan.
00468  *
00469  * In a multiple-scan JPEG file, the encoder could assign different components
00470  * the same Q-table slot number, but change table definitions between scans
00471  * so that each component uses a different Q-table.  (The IJG encoder is not
00472  * currently capable of doing this, but other encoders might.)  Since we want
00473  * to be able to dequantize all the components at the end of the file, this
00474  * means that we have to save away the table actually used for each component.
00475  * We do this by copying the table at the start of the first scan containing
00476  * the component.
00477  * The JPEG spec prohibits the encoder from changing the contents of a Q-table
00478  * slot between scans of a component using that slot.  If the encoder does so
00479  * anyway, this decoder will simply use the Q-table values that were current
00480  * at the start of the first scan for the component.
00481  *
00482  * The decompressor output side looks only at the saved quant tables,
00483  * not at the current Q-table slots.
00484  */
00485 
00486 LOCAL(void)
00487 latch_quant_tables (j_decompress_ptr cinfo)
00488 {
00489   int ci, qtblno;
00490   jpeg_component_info *compptr;
00491   JQUANT_TBL * qtbl;
00492 
00493   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00494     compptr = cinfo->cur_comp_info[ci];
00495     /* No work if we already saved Q-table for this component */
00496     if (compptr->quant_table != NULL)
00497       continue;
00498     /* Make sure specified quantization table is present */
00499     qtblno = compptr->quant_tbl_no;
00500     if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
00501         cinfo->quant_tbl_ptrs[qtblno] == NULL)
00502       ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
00503     /* OK, save away the quantization table */
00504     qtbl = (JQUANT_TBL *)
00505       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00506                                   SIZEOF(JQUANT_TBL));
00507     MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
00508     compptr->quant_table = qtbl;
00509   }
00510 }
00511 
00512 
00513 /*
00514  * Initialize the input modules to read a scan of compressed data.
00515  * The first call to this is done by jdmaster.c after initializing
00516  * the entire decompressor (during jpeg_start_decompress).
00517  * Subsequent calls come from consume_markers, below.
00518  */
00519 
00520 METHODDEF(void)
00521 start_input_pass (j_decompress_ptr cinfo)
00522 {
00523   per_scan_setup(cinfo);
00524   latch_quant_tables(cinfo);
00525   (*cinfo->entropy->start_pass) (cinfo);
00526   (*cinfo->coef->start_input_pass) (cinfo);
00527   cinfo->inputctl->consume_input = cinfo->coef->consume_data;
00528 }
00529 
00530 
00531 /*
00532  * Finish up after inputting a compressed-data scan.
00533  * This is called by the coefficient controller after it's read all
00534  * the expected data of the scan.
00535  */
00536 
00537 METHODDEF(void)
00538 finish_input_pass (j_decompress_ptr cinfo)
00539 {
00540   cinfo->inputctl->consume_input = consume_markers;
00541 }
00542 
00543 
00544 /*
00545  * Read JPEG markers before, between, or after compressed-data scans.
00546  * Change state as necessary when a new scan is reached.
00547  * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
00548  *
00549  * The consume_input method pointer points either here or to the
00550  * coefficient controller's consume_data routine, depending on whether
00551  * we are reading a compressed data segment or inter-segment markers.
00552  *
00553  * Note: This function should NOT return a pseudo SOS marker (with zero
00554  * component number) to the caller.  A pseudo marker received by
00555  * read_markers is processed and then skipped for other markers.
00556  */
00557 
00558 METHODDEF(int)
00559 consume_markers (j_decompress_ptr cinfo)
00560 {
00561   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
00562   int val;
00563 
00564   if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
00565     return JPEG_REACHED_EOI;
00566 
00567   for (;;) {                    /* Loop to pass pseudo SOS marker */
00568     val = (*cinfo->marker->read_markers) (cinfo);
00569 
00570     switch (val) {
00571     case JPEG_REACHED_SOS:      /* Found SOS */
00572       if (inputctl->inheaders) { /* 1st SOS */
00573         if (inputctl->inheaders == 1)
00574           initial_setup(cinfo);
00575         if (cinfo->comps_in_scan == 0) { /* pseudo SOS marker */
00576           inputctl->inheaders = 2;
00577           break;
00578         }
00579         inputctl->inheaders = 0;
00580         /* Note: start_input_pass must be called by jdmaster.c
00581          * before any more input can be consumed.  jdapimin.c is
00582          * responsible for enforcing this sequencing.
00583          */
00584       } else {                  /* 2nd or later SOS marker */
00585         if (! inputctl->pub.has_multiple_scans)
00586           ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
00587         if (cinfo->comps_in_scan == 0) /* unexpected pseudo SOS marker */
00588           break;
00589         start_input_pass(cinfo);
00590       }
00591       return val;
00592     case JPEG_REACHED_EOI:      /* Found EOI */
00593       inputctl->pub.eoi_reached = TRUE;
00594       if (inputctl->inheaders) { /* Tables-only datastream, apparently */
00595         if (cinfo->marker->saw_SOF)
00596           ERREXIT(cinfo, JERR_SOF_NO_SOS);
00597       } else {
00598         /* Prevent infinite loop in coef ctlr's decompress_data routine
00599          * if user set output_scan_number larger than number of scans.
00600          */
00601         if (cinfo->output_scan_number > cinfo->input_scan_number)
00602           cinfo->output_scan_number = cinfo->input_scan_number;
00603       }
00604       return val;
00605     case JPEG_SUSPENDED:
00606       return val;
00607     default:
00608       return val;
00609     }
00610   }
00611 }
00612 
00613 
00614 /*
00615  * Reset state to begin a fresh datastream.
00616  */
00617 
00618 METHODDEF(void)
00619 reset_input_controller (j_decompress_ptr cinfo)
00620 {
00621   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
00622 
00623   inputctl->pub.consume_input = consume_markers;
00624   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
00625   inputctl->pub.eoi_reached = FALSE;
00626   inputctl->inheaders = 1;
00627   /* Reset other modules */
00628   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
00629   (*cinfo->marker->reset_marker_reader) (cinfo);
00630   /* Reset progression state -- would be cleaner if entropy decoder did this */
00631   cinfo->coef_bits = NULL;
00632 }
00633 
00634 
00635 /*
00636  * Initialize the input controller module.
00637  * This is called only once, when the decompression object is created.
00638  */
00639 
00640 GLOBAL(void)
00641 jinit_input_controller (j_decompress_ptr cinfo)
00642 {
00643   my_inputctl_ptr inputctl;
00644 
00645   /* Create subobject in permanent pool */
00646   inputctl = (my_inputctl_ptr)
00647     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00648                                 SIZEOF(my_input_controller));
00649   cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
00650   /* Initialize method pointers */
00651   inputctl->pub.consume_input = consume_markers;
00652   inputctl->pub.reset_input_controller = reset_input_controller;
00653   inputctl->pub.start_input_pass = start_input_pass;
00654   inputctl->pub.finish_input_pass = finish_input_pass;
00655   /* Initialize state: can't use reset_input_controller since we don't
00656    * want to try to reset other modules yet.
00657    */
00658   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
00659   inputctl->pub.eoi_reached = FALSE;
00660   inputctl->inheaders = 1;
00661 }

Generated on Tue Jul 5 14:13:29 2011 for ROOT_528-00b_version by  doxygen 1.5.1