src/3rdparty/libjpeg/jdinput.c
changeset 30 5dc02b23752f
parent 0 1918ee327afb
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
     1 /*
     1 /*
     2  * jdinput.c
     2  * jdinput.c
     3  *
     3  *
     4  * Copyright (C) 1991-1997, Thomas G. Lane.
     4  * Copyright (C) 1991-1997, Thomas G. Lane.
       
     5  * Modified 2002-2009 by Guido Vollbeding.
     5  * This file is part of the Independent JPEG Group's software.
     6  * This file is part of the Independent JPEG Group's software.
     6  * For conditions of distribution and use, see the accompanying README file.
     7  * For conditions of distribution and use, see the accompanying README file.
     7  *
     8  *
     8  * This file contains input control logic for the JPEG decompressor.
     9  * This file contains input control logic for the JPEG decompressor.
     9  * These routines are concerned with controlling the decompressor's input
    10  * These routines are concerned with controlling the decompressor's input
    10  * processing (marker reading and coefficient decoding).  The actual input
    11  * processing (marker reading and coefficient decoding).  The actual input
    11  * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
    12  * reading is done in jdmarker.c, jdhuff.c, and jdarith.c.
    12  */
    13  */
    13 
    14 
    14 #define JPEG_INTERNALS
    15 #define JPEG_INTERNALS
    15 #include "jinclude.h"
    16 #include "jinclude.h"
    16 #include "jpeglib.h"
    17 #include "jpeglib.h"
    19 /* Private state */
    20 /* Private state */
    20 
    21 
    21 typedef struct {
    22 typedef struct {
    22   struct jpeg_input_controller pub; /* public fields */
    23   struct jpeg_input_controller pub; /* public fields */
    23 
    24 
    24   boolean inheaders;		/* TRUE until first SOS is reached */
    25   int inheaders;		/* Nonzero until first SOS is reached */
    25 } my_input_controller;
    26 } my_input_controller;
    26 
    27 
    27 typedef my_input_controller * my_inputctl_ptr;
    28 typedef my_input_controller * my_inputctl_ptr;
    28 
    29 
    29 
    30 
    32 
    33 
    33 
    34 
    34 /*
    35 /*
    35  * Routines to calculate various quantities related to the size of the image.
    36  * Routines to calculate various quantities related to the size of the image.
    36  */
    37  */
       
    38 
       
    39 
       
    40 /*
       
    41  * Compute output image dimensions and related values.
       
    42  * NOTE: this is exported for possible use by application.
       
    43  * Hence it mustn't do anything that can't be done twice.
       
    44  */
       
    45 
       
    46 GLOBAL(void)
       
    47 jpeg_core_output_dimensions (j_decompress_ptr cinfo)
       
    48 /* Do computations that are needed before master selection phase.
       
    49  * This function is used for transcoding and full decompression.
       
    50  */
       
    51 {
       
    52 #ifdef IDCT_SCALING_SUPPORTED
       
    53   int ci;
       
    54   jpeg_component_info *compptr;
       
    55 
       
    56   /* Compute actual output image dimensions and DCT scaling choices. */
       
    57   if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) {
       
    58     /* Provide 1/block_size scaling */
       
    59     cinfo->output_width = (JDIMENSION)
       
    60       jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size);
       
    61     cinfo->output_height = (JDIMENSION)
       
    62       jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size);
       
    63     cinfo->min_DCT_h_scaled_size = 1;
       
    64     cinfo->min_DCT_v_scaled_size = 1;
       
    65   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) {
       
    66     /* Provide 2/block_size scaling */
       
    67     cinfo->output_width = (JDIMENSION)
       
    68       jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size);
       
    69     cinfo->output_height = (JDIMENSION)
       
    70       jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size);
       
    71     cinfo->min_DCT_h_scaled_size = 2;
       
    72     cinfo->min_DCT_v_scaled_size = 2;
       
    73   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 3) {
       
    74     /* Provide 3/block_size scaling */
       
    75     cinfo->output_width = (JDIMENSION)
       
    76       jdiv_round_up((long) cinfo->image_width * 3L, (long) cinfo->block_size);
       
    77     cinfo->output_height = (JDIMENSION)
       
    78       jdiv_round_up((long) cinfo->image_height * 3L, (long) cinfo->block_size);
       
    79     cinfo->min_DCT_h_scaled_size = 3;
       
    80     cinfo->min_DCT_v_scaled_size = 3;
       
    81   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) {
       
    82     /* Provide 4/block_size scaling */
       
    83     cinfo->output_width = (JDIMENSION)
       
    84       jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size);
       
    85     cinfo->output_height = (JDIMENSION)
       
    86       jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size);
       
    87     cinfo->min_DCT_h_scaled_size = 4;
       
    88     cinfo->min_DCT_v_scaled_size = 4;
       
    89   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 5) {
       
    90     /* Provide 5/block_size scaling */
       
    91     cinfo->output_width = (JDIMENSION)
       
    92       jdiv_round_up((long) cinfo->image_width * 5L, (long) cinfo->block_size);
       
    93     cinfo->output_height = (JDIMENSION)
       
    94       jdiv_round_up((long) cinfo->image_height * 5L, (long) cinfo->block_size);
       
    95     cinfo->min_DCT_h_scaled_size = 5;
       
    96     cinfo->min_DCT_v_scaled_size = 5;
       
    97   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 6) {
       
    98     /* Provide 6/block_size scaling */
       
    99     cinfo->output_width = (JDIMENSION)
       
   100       jdiv_round_up((long) cinfo->image_width * 6L, (long) cinfo->block_size);
       
   101     cinfo->output_height = (JDIMENSION)
       
   102       jdiv_round_up((long) cinfo->image_height * 6L, (long) cinfo->block_size);
       
   103     cinfo->min_DCT_h_scaled_size = 6;
       
   104     cinfo->min_DCT_v_scaled_size = 6;
       
   105   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 7) {
       
   106     /* Provide 7/block_size scaling */
       
   107     cinfo->output_width = (JDIMENSION)
       
   108       jdiv_round_up((long) cinfo->image_width * 7L, (long) cinfo->block_size);
       
   109     cinfo->output_height = (JDIMENSION)
       
   110       jdiv_round_up((long) cinfo->image_height * 7L, (long) cinfo->block_size);
       
   111     cinfo->min_DCT_h_scaled_size = 7;
       
   112     cinfo->min_DCT_v_scaled_size = 7;
       
   113   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) {
       
   114     /* Provide 8/block_size scaling */
       
   115     cinfo->output_width = (JDIMENSION)
       
   116       jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size);
       
   117     cinfo->output_height = (JDIMENSION)
       
   118       jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size);
       
   119     cinfo->min_DCT_h_scaled_size = 8;
       
   120     cinfo->min_DCT_v_scaled_size = 8;
       
   121   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 9) {
       
   122     /* Provide 9/block_size scaling */
       
   123     cinfo->output_width = (JDIMENSION)
       
   124       jdiv_round_up((long) cinfo->image_width * 9L, (long) cinfo->block_size);
       
   125     cinfo->output_height = (JDIMENSION)
       
   126       jdiv_round_up((long) cinfo->image_height * 9L, (long) cinfo->block_size);
       
   127     cinfo->min_DCT_h_scaled_size = 9;
       
   128     cinfo->min_DCT_v_scaled_size = 9;
       
   129   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 10) {
       
   130     /* Provide 10/block_size scaling */
       
   131     cinfo->output_width = (JDIMENSION)
       
   132       jdiv_round_up((long) cinfo->image_width * 10L, (long) cinfo->block_size);
       
   133     cinfo->output_height = (JDIMENSION)
       
   134       jdiv_round_up((long) cinfo->image_height * 10L, (long) cinfo->block_size);
       
   135     cinfo->min_DCT_h_scaled_size = 10;
       
   136     cinfo->min_DCT_v_scaled_size = 10;
       
   137   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 11) {
       
   138     /* Provide 11/block_size scaling */
       
   139     cinfo->output_width = (JDIMENSION)
       
   140       jdiv_round_up((long) cinfo->image_width * 11L, (long) cinfo->block_size);
       
   141     cinfo->output_height = (JDIMENSION)
       
   142       jdiv_round_up((long) cinfo->image_height * 11L, (long) cinfo->block_size);
       
   143     cinfo->min_DCT_h_scaled_size = 11;
       
   144     cinfo->min_DCT_v_scaled_size = 11;
       
   145   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 12) {
       
   146     /* Provide 12/block_size scaling */
       
   147     cinfo->output_width = (JDIMENSION)
       
   148       jdiv_round_up((long) cinfo->image_width * 12L, (long) cinfo->block_size);
       
   149     cinfo->output_height = (JDIMENSION)
       
   150       jdiv_round_up((long) cinfo->image_height * 12L, (long) cinfo->block_size);
       
   151     cinfo->min_DCT_h_scaled_size = 12;
       
   152     cinfo->min_DCT_v_scaled_size = 12;
       
   153   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 13) {
       
   154     /* Provide 13/block_size scaling */
       
   155     cinfo->output_width = (JDIMENSION)
       
   156       jdiv_round_up((long) cinfo->image_width * 13L, (long) cinfo->block_size);
       
   157     cinfo->output_height = (JDIMENSION)
       
   158       jdiv_round_up((long) cinfo->image_height * 13L, (long) cinfo->block_size);
       
   159     cinfo->min_DCT_h_scaled_size = 13;
       
   160     cinfo->min_DCT_v_scaled_size = 13;
       
   161   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 14) {
       
   162     /* Provide 14/block_size scaling */
       
   163     cinfo->output_width = (JDIMENSION)
       
   164       jdiv_round_up((long) cinfo->image_width * 14L, (long) cinfo->block_size);
       
   165     cinfo->output_height = (JDIMENSION)
       
   166       jdiv_round_up((long) cinfo->image_height * 14L, (long) cinfo->block_size);
       
   167     cinfo->min_DCT_h_scaled_size = 14;
       
   168     cinfo->min_DCT_v_scaled_size = 14;
       
   169   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 15) {
       
   170     /* Provide 15/block_size scaling */
       
   171     cinfo->output_width = (JDIMENSION)
       
   172       jdiv_round_up((long) cinfo->image_width * 15L, (long) cinfo->block_size);
       
   173     cinfo->output_height = (JDIMENSION)
       
   174       jdiv_round_up((long) cinfo->image_height * 15L, (long) cinfo->block_size);
       
   175     cinfo->min_DCT_h_scaled_size = 15;
       
   176     cinfo->min_DCT_v_scaled_size = 15;
       
   177   } else {
       
   178     /* Provide 16/block_size scaling */
       
   179     cinfo->output_width = (JDIMENSION)
       
   180       jdiv_round_up((long) cinfo->image_width * 16L, (long) cinfo->block_size);
       
   181     cinfo->output_height = (JDIMENSION)
       
   182       jdiv_round_up((long) cinfo->image_height * 16L, (long) cinfo->block_size);
       
   183     cinfo->min_DCT_h_scaled_size = 16;
       
   184     cinfo->min_DCT_v_scaled_size = 16;
       
   185   }
       
   186 
       
   187   /* Recompute dimensions of components */
       
   188   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       
   189        ci++, compptr++) {
       
   190     compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size;
       
   191     compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size;
       
   192   }
       
   193 
       
   194 #else /* !IDCT_SCALING_SUPPORTED */
       
   195 
       
   196   /* Hardwire it to "no scaling" */
       
   197   cinfo->output_width = cinfo->image_width;
       
   198   cinfo->output_height = cinfo->image_height;
       
   199   /* jdinput.c has already initialized DCT_scaled_size,
       
   200    * and has computed unscaled downsampled_width and downsampled_height.
       
   201    */
       
   202 
       
   203 #endif /* IDCT_SCALING_SUPPORTED */
       
   204 }
       
   205 
    37 
   206 
    38 LOCAL(void)
   207 LOCAL(void)
    39 initial_setup (j_decompress_ptr cinfo)
   208 initial_setup (j_decompress_ptr cinfo)
    40 /* Called once, when first SOS marker is reached */
   209 /* Called once, when first SOS marker is reached */
    41 {
   210 {
    68 				   compptr->h_samp_factor);
   237 				   compptr->h_samp_factor);
    69     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
   238     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
    70 				   compptr->v_samp_factor);
   239 				   compptr->v_samp_factor);
    71   }
   240   }
    72 
   241 
    73   /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
   242   /* Derive block_size, natural_order, and lim_Se */
    74    * In the full decompressor, this will be overridden by jdmaster.c;
   243   if (cinfo->is_baseline || (cinfo->progressive_mode &&
    75    * but in the transcoder, jdmaster.c is not used, so we must do it here.
   244       cinfo->comps_in_scan)) { /* no pseudo SOS marker */
       
   245     cinfo->block_size = DCTSIZE;
       
   246     cinfo->natural_order = jpeg_natural_order;
       
   247     cinfo->lim_Se = DCTSIZE2-1;
       
   248   } else
       
   249     switch (cinfo->Se) {
       
   250     case (1*1-1):
       
   251       cinfo->block_size = 1;
       
   252       cinfo->natural_order = jpeg_natural_order; /* not needed */
       
   253       cinfo->lim_Se = cinfo->Se;
       
   254       break;
       
   255     case (2*2-1):
       
   256       cinfo->block_size = 2;
       
   257       cinfo->natural_order = jpeg_natural_order2;
       
   258       cinfo->lim_Se = cinfo->Se;
       
   259       break;
       
   260     case (3*3-1):
       
   261       cinfo->block_size = 3;
       
   262       cinfo->natural_order = jpeg_natural_order3;
       
   263       cinfo->lim_Se = cinfo->Se;
       
   264       break;
       
   265     case (4*4-1):
       
   266       cinfo->block_size = 4;
       
   267       cinfo->natural_order = jpeg_natural_order4;
       
   268       cinfo->lim_Se = cinfo->Se;
       
   269       break;
       
   270     case (5*5-1):
       
   271       cinfo->block_size = 5;
       
   272       cinfo->natural_order = jpeg_natural_order5;
       
   273       cinfo->lim_Se = cinfo->Se;
       
   274       break;
       
   275     case (6*6-1):
       
   276       cinfo->block_size = 6;
       
   277       cinfo->natural_order = jpeg_natural_order6;
       
   278       cinfo->lim_Se = cinfo->Se;
       
   279       break;
       
   280     case (7*7-1):
       
   281       cinfo->block_size = 7;
       
   282       cinfo->natural_order = jpeg_natural_order7;
       
   283       cinfo->lim_Se = cinfo->Se;
       
   284       break;
       
   285     case (8*8-1):
       
   286       cinfo->block_size = 8;
       
   287       cinfo->natural_order = jpeg_natural_order;
       
   288       cinfo->lim_Se = DCTSIZE2-1;
       
   289       break;
       
   290     case (9*9-1):
       
   291       cinfo->block_size = 9;
       
   292       cinfo->natural_order = jpeg_natural_order;
       
   293       cinfo->lim_Se = DCTSIZE2-1;
       
   294       break;
       
   295     case (10*10-1):
       
   296       cinfo->block_size = 10;
       
   297       cinfo->natural_order = jpeg_natural_order;
       
   298       cinfo->lim_Se = DCTSIZE2-1;
       
   299       break;
       
   300     case (11*11-1):
       
   301       cinfo->block_size = 11;
       
   302       cinfo->natural_order = jpeg_natural_order;
       
   303       cinfo->lim_Se = DCTSIZE2-1;
       
   304       break;
       
   305     case (12*12-1):
       
   306       cinfo->block_size = 12;
       
   307       cinfo->natural_order = jpeg_natural_order;
       
   308       cinfo->lim_Se = DCTSIZE2-1;
       
   309       break;
       
   310     case (13*13-1):
       
   311       cinfo->block_size = 13;
       
   312       cinfo->natural_order = jpeg_natural_order;
       
   313       cinfo->lim_Se = DCTSIZE2-1;
       
   314       break;
       
   315     case (14*14-1):
       
   316       cinfo->block_size = 14;
       
   317       cinfo->natural_order = jpeg_natural_order;
       
   318       cinfo->lim_Se = DCTSIZE2-1;
       
   319       break;
       
   320     case (15*15-1):
       
   321       cinfo->block_size = 15;
       
   322       cinfo->natural_order = jpeg_natural_order;
       
   323       cinfo->lim_Se = DCTSIZE2-1;
       
   324       break;
       
   325     case (16*16-1):
       
   326       cinfo->block_size = 16;
       
   327       cinfo->natural_order = jpeg_natural_order;
       
   328       cinfo->lim_Se = DCTSIZE2-1;
       
   329       break;
       
   330     default:
       
   331       ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
       
   332 	       cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
       
   333       break;
       
   334     }
       
   335 
       
   336   /* We initialize DCT_scaled_size and min_DCT_scaled_size to block_size.
       
   337    * In the full decompressor,
       
   338    * this will be overridden by jpeg_calc_output_dimensions in jdmaster.c;
       
   339    * but in the transcoder,
       
   340    * jpeg_calc_output_dimensions is not used, so we must do it here.
    76    */
   341    */
    77   cinfo->min_DCT_scaled_size = DCTSIZE;
   342   cinfo->min_DCT_h_scaled_size = cinfo->block_size;
       
   343   cinfo->min_DCT_v_scaled_size = cinfo->block_size;
    78 
   344 
    79   /* Compute dimensions of components */
   345   /* Compute dimensions of components */
    80   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   346   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
    81        ci++, compptr++) {
   347        ci++, compptr++) {
    82     compptr->DCT_scaled_size = DCTSIZE;
   348     compptr->DCT_h_scaled_size = cinfo->block_size;
       
   349     compptr->DCT_v_scaled_size = cinfo->block_size;
    83     /* Size in DCT blocks */
   350     /* Size in DCT blocks */
    84     compptr->width_in_blocks = (JDIMENSION)
   351     compptr->width_in_blocks = (JDIMENSION)
    85       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
   352       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
    86 		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
   353 		    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
    87     compptr->height_in_blocks = (JDIMENSION)
   354     compptr->height_in_blocks = (JDIMENSION)
    88       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
   355       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
    89 		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
   356 		    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
    90     /* downsampled_width and downsampled_height will also be overridden by
   357     /* downsampled_width and downsampled_height will also be overridden by
    91      * jdmaster.c if we are doing full decompression.  The transcoder library
   358      * jdmaster.c if we are doing full decompression.  The transcoder library
    92      * doesn't use these values, but the calling application might.
   359      * doesn't use these values, but the calling application might.
    93      */
   360      */
    94     /* Size in samples */
   361     /* Size in samples */
   105   }
   372   }
   106 
   373 
   107   /* Compute number of fully interleaved MCU rows. */
   374   /* Compute number of fully interleaved MCU rows. */
   108   cinfo->total_iMCU_rows = (JDIMENSION)
   375   cinfo->total_iMCU_rows = (JDIMENSION)
   109     jdiv_round_up((long) cinfo->image_height,
   376     jdiv_round_up((long) cinfo->image_height,
   110 		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
   377 	          (long) (cinfo->max_v_samp_factor * cinfo->block_size));
   111 
   378 
   112   /* Decide whether file contains multiple scans */
   379   /* Decide whether file contains multiple scans */
   113   if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
   380   if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
   114     cinfo->inputctl->has_multiple_scans = TRUE;
   381     cinfo->inputctl->has_multiple_scans = TRUE;
   115   else
   382   else
   136     
   403     
   137     /* For noninterleaved scan, always one block per MCU */
   404     /* For noninterleaved scan, always one block per MCU */
   138     compptr->MCU_width = 1;
   405     compptr->MCU_width = 1;
   139     compptr->MCU_height = 1;
   406     compptr->MCU_height = 1;
   140     compptr->MCU_blocks = 1;
   407     compptr->MCU_blocks = 1;
   141     compptr->MCU_sample_width = compptr->DCT_scaled_size;
   408     compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
   142     compptr->last_col_width = 1;
   409     compptr->last_col_width = 1;
   143     /* For noninterleaved scans, it is convenient to define last_row_height
   410     /* For noninterleaved scans, it is convenient to define last_row_height
   144      * as the number of block rows present in the last iMCU row.
   411      * as the number of block rows present in the last iMCU row.
   145      */
   412      */
   146     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
   413     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
   159 	       MAX_COMPS_IN_SCAN);
   426 	       MAX_COMPS_IN_SCAN);
   160     
   427     
   161     /* Overall image size in MCUs */
   428     /* Overall image size in MCUs */
   162     cinfo->MCUs_per_row = (JDIMENSION)
   429     cinfo->MCUs_per_row = (JDIMENSION)
   163       jdiv_round_up((long) cinfo->image_width,
   430       jdiv_round_up((long) cinfo->image_width,
   164 		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
   431 		    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
   165     cinfo->MCU_rows_in_scan = (JDIMENSION)
   432     cinfo->MCU_rows_in_scan = (JDIMENSION)
   166       jdiv_round_up((long) cinfo->image_height,
   433       jdiv_round_up((long) cinfo->image_height,
   167 		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
   434 		    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
   168     
   435     
   169     cinfo->blocks_in_MCU = 0;
   436     cinfo->blocks_in_MCU = 0;
   170     
   437     
   171     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   438     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   172       compptr = cinfo->cur_comp_info[ci];
   439       compptr = cinfo->cur_comp_info[ci];
   173       /* Sampling factors give # of blocks of component in each MCU */
   440       /* Sampling factors give # of blocks of component in each MCU */
   174       compptr->MCU_width = compptr->h_samp_factor;
   441       compptr->MCU_width = compptr->h_samp_factor;
   175       compptr->MCU_height = compptr->v_samp_factor;
   442       compptr->MCU_height = compptr->v_samp_factor;
   176       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
   443       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
   177       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
   444       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
   178       /* Figure number of non-dummy blocks in last MCU column & row */
   445       /* Figure number of non-dummy blocks in last MCU column & row */
   179       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
   446       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
   180       if (tmp == 0) tmp = compptr->MCU_width;
   447       if (tmp == 0) tmp = compptr->MCU_width;
   181       compptr->last_col_width = tmp;
   448       compptr->last_col_width = tmp;
   182       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
   449       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
   280  * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
   547  * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
   281  *
   548  *
   282  * The consume_input method pointer points either here or to the
   549  * The consume_input method pointer points either here or to the
   283  * coefficient controller's consume_data routine, depending on whether
   550  * coefficient controller's consume_data routine, depending on whether
   284  * we are reading a compressed data segment or inter-segment markers.
   551  * we are reading a compressed data segment or inter-segment markers.
       
   552  *
       
   553  * Note: This function should NOT return a pseudo SOS marker (with zero
       
   554  * component number) to the caller.  A pseudo marker received by
       
   555  * read_markers is processed and then skipped for other markers.
   285  */
   556  */
   286 
   557 
   287 METHODDEF(int)
   558 METHODDEF(int)
   288 consume_markers (j_decompress_ptr cinfo)
   559 consume_markers (j_decompress_ptr cinfo)
   289 {
   560 {
   291   int val;
   562   int val;
   292 
   563 
   293   if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
   564   if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
   294     return JPEG_REACHED_EOI;
   565     return JPEG_REACHED_EOI;
   295 
   566 
   296   val = (*cinfo->marker->read_markers) (cinfo);
   567   for (;;) {			/* Loop to pass pseudo SOS marker */
   297 
   568     val = (*cinfo->marker->read_markers) (cinfo);
   298   switch (val) {
   569 
   299   case JPEG_REACHED_SOS:	/* Found SOS */
   570     switch (val) {
   300     if (inputctl->inheaders) {	/* 1st SOS */
   571     case JPEG_REACHED_SOS:	/* Found SOS */
   301       initial_setup(cinfo);
   572       if (inputctl->inheaders) { /* 1st SOS */
   302       inputctl->inheaders = FALSE;
   573 	if (inputctl->inheaders == 1)
   303       /* Note: start_input_pass must be called by jdmaster.c
   574 	  initial_setup(cinfo);
   304        * before any more input can be consumed.  jdapimin.c is
   575 	if (cinfo->comps_in_scan == 0) { /* pseudo SOS marker */
   305        * responsible for enforcing this sequencing.
   576 	  inputctl->inheaders = 2;
   306        */
   577 	  break;
   307     } else {			/* 2nd or later SOS marker */
   578 	}
   308       if (! inputctl->pub.has_multiple_scans)
   579 	inputctl->inheaders = 0;
   309 	ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
   580 	/* Note: start_input_pass must be called by jdmaster.c
   310       start_input_pass(cinfo);
   581 	 * before any more input can be consumed.  jdapimin.c is
       
   582 	 * responsible for enforcing this sequencing.
       
   583 	 */
       
   584       } else {			/* 2nd or later SOS marker */
       
   585 	if (! inputctl->pub.has_multiple_scans)
       
   586 	  ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
       
   587 	if (cinfo->comps_in_scan == 0) /* unexpected pseudo SOS marker */
       
   588 	  break;
       
   589 	start_input_pass(cinfo);
       
   590       }
       
   591       return val;
       
   592     case JPEG_REACHED_EOI:	/* Found EOI */
       
   593       inputctl->pub.eoi_reached = TRUE;
       
   594       if (inputctl->inheaders) { /* Tables-only datastream, apparently */
       
   595 	if (cinfo->marker->saw_SOF)
       
   596 	  ERREXIT(cinfo, JERR_SOF_NO_SOS);
       
   597       } else {
       
   598 	/* Prevent infinite loop in coef ctlr's decompress_data routine
       
   599 	 * if user set output_scan_number larger than number of scans.
       
   600 	 */
       
   601 	if (cinfo->output_scan_number > cinfo->input_scan_number)
       
   602 	  cinfo->output_scan_number = cinfo->input_scan_number;
       
   603       }
       
   604       return val;
       
   605     case JPEG_SUSPENDED:
       
   606       return val;
       
   607     default:
       
   608       return val;
   311     }
   609     }
   312     break;
       
   313   case JPEG_REACHED_EOI:	/* Found EOI */
       
   314     inputctl->pub.eoi_reached = TRUE;
       
   315     if (inputctl->inheaders) {	/* Tables-only datastream, apparently */
       
   316       if (cinfo->marker->saw_SOF)
       
   317 	ERREXIT(cinfo, JERR_SOF_NO_SOS);
       
   318     } else {
       
   319       /* Prevent infinite loop in coef ctlr's decompress_data routine
       
   320        * if user set output_scan_number larger than number of scans.
       
   321        */
       
   322       if (cinfo->output_scan_number > cinfo->input_scan_number)
       
   323 	cinfo->output_scan_number = cinfo->input_scan_number;
       
   324     }
       
   325     break;
       
   326   case JPEG_SUSPENDED:
       
   327     break;
       
   328   }
   610   }
   329 
       
   330   return val;
       
   331 }
   611 }
   332 
   612 
   333 
   613 
   334 /*
   614 /*
   335  * Reset state to begin a fresh datastream.
   615  * Reset state to begin a fresh datastream.
   341   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
   621   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
   342 
   622 
   343   inputctl->pub.consume_input = consume_markers;
   623   inputctl->pub.consume_input = consume_markers;
   344   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
   624   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
   345   inputctl->pub.eoi_reached = FALSE;
   625   inputctl->pub.eoi_reached = FALSE;
   346   inputctl->inheaders = TRUE;
   626   inputctl->inheaders = 1;
   347   /* Reset other modules */
   627   /* Reset other modules */
   348   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
   628   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
   349   (*cinfo->marker->reset_marker_reader) (cinfo);
   629   (*cinfo->marker->reset_marker_reader) (cinfo);
   350   /* Reset progression state -- would be cleaner if entropy decoder did this */
   630   /* Reset progression state -- would be cleaner if entropy decoder did this */
   351   cinfo->coef_bits = NULL;
   631   cinfo->coef_bits = NULL;
   375   /* Initialize state: can't use reset_input_controller since we don't
   655   /* Initialize state: can't use reset_input_controller since we don't
   376    * want to try to reset other modules yet.
   656    * want to try to reset other modules yet.
   377    */
   657    */
   378   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
   658   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
   379   inputctl->pub.eoi_reached = FALSE;
   659   inputctl->pub.eoi_reached = FALSE;
   380   inputctl->inheaders = TRUE;
   660   inputctl->inheaders = 1;
   381 }
   661 }