src/3rdparty/libjpeg/jcmaster.c
changeset 30 5dc02b23752f
parent 0 1918ee327afb
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
     1 /*
     1 /*
     2  * jcmaster.c
     2  * jcmaster.c
     3  *
     3  *
     4  * Copyright (C) 1991-1997, Thomas G. Lane.
     4  * Copyright (C) 1991-1997, Thomas G. Lane.
       
     5  * Modified 2003-2010 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 master control logic for the JPEG compressor.
     9  * This file contains master control logic for the JPEG compressor.
     9  * These routines are concerned with parameter validation, initial setup,
    10  * These routines are concerned with parameter validation, initial setup,
    40 
    41 
    41 /*
    42 /*
    42  * Support routines that do various essential calculations.
    43  * Support routines that do various essential calculations.
    43  */
    44  */
    44 
    45 
       
    46 /*
       
    47  * Compute JPEG image dimensions and related values.
       
    48  * NOTE: this is exported for possible use by application.
       
    49  * Hence it mustn't do anything that can't be done twice.
       
    50  */
       
    51 
       
    52 GLOBAL(void)
       
    53 jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
       
    54 /* Do computations that are needed before master selection phase */
       
    55 {
       
    56 #ifdef DCT_SCALING_SUPPORTED
       
    57 
       
    58   /* Compute actual JPEG image dimensions and DCT scaling choices. */
       
    59   if (cinfo->scale_num >= cinfo->scale_denom * 8) {
       
    60     /* Provide 8/1 scaling */
       
    61     cinfo->jpeg_width = cinfo->image_width << 3;
       
    62     cinfo->jpeg_height = cinfo->image_height << 3;
       
    63     cinfo->min_DCT_h_scaled_size = 1;
       
    64     cinfo->min_DCT_v_scaled_size = 1;
       
    65   } else if (cinfo->scale_num >= cinfo->scale_denom * 4) {
       
    66     /* Provide 4/1 scaling */
       
    67     cinfo->jpeg_width = cinfo->image_width << 2;
       
    68     cinfo->jpeg_height = cinfo->image_height << 2;
       
    69     cinfo->min_DCT_h_scaled_size = 2;
       
    70     cinfo->min_DCT_v_scaled_size = 2;
       
    71   } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 8) {
       
    72     /* Provide 8/3 scaling */
       
    73     cinfo->jpeg_width = (cinfo->image_width << 1) + (JDIMENSION)
       
    74       jdiv_round_up((long) cinfo->image_width * 2, 3L);
       
    75     cinfo->jpeg_height = (cinfo->image_height << 1) + (JDIMENSION)
       
    76       jdiv_round_up((long) cinfo->image_height * 2, 3L);
       
    77     cinfo->min_DCT_h_scaled_size = 3;
       
    78     cinfo->min_DCT_v_scaled_size = 3;
       
    79   } else if (cinfo->scale_num >= cinfo->scale_denom * 2) {
       
    80     /* Provide 2/1 scaling */
       
    81     cinfo->jpeg_width = cinfo->image_width << 1;
       
    82     cinfo->jpeg_height = cinfo->image_height << 1;
       
    83     cinfo->min_DCT_h_scaled_size = 4;
       
    84     cinfo->min_DCT_v_scaled_size = 4;
       
    85   } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 8) {
       
    86     /* Provide 8/5 scaling */
       
    87     cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
       
    88       jdiv_round_up((long) cinfo->image_width * 3, 5L);
       
    89     cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
       
    90       jdiv_round_up((long) cinfo->image_height * 3, 5L);
       
    91     cinfo->min_DCT_h_scaled_size = 5;
       
    92     cinfo->min_DCT_v_scaled_size = 5;
       
    93   } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 4) {
       
    94     /* Provide 4/3 scaling */
       
    95     cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
       
    96       jdiv_round_up((long) cinfo->image_width, 3L);
       
    97     cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
       
    98       jdiv_round_up((long) cinfo->image_height, 3L);
       
    99     cinfo->min_DCT_h_scaled_size = 6;
       
   100     cinfo->min_DCT_v_scaled_size = 6;
       
   101   } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 8) {
       
   102     /* Provide 8/7 scaling */
       
   103     cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
       
   104       jdiv_round_up((long) cinfo->image_width, 7L);
       
   105     cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
       
   106       jdiv_round_up((long) cinfo->image_height, 7L);
       
   107     cinfo->min_DCT_h_scaled_size = 7;
       
   108     cinfo->min_DCT_v_scaled_size = 7;
       
   109   } else if (cinfo->scale_num >= cinfo->scale_denom) {
       
   110     /* Provide 1/1 scaling */
       
   111     cinfo->jpeg_width = cinfo->image_width;
       
   112     cinfo->jpeg_height = cinfo->image_height;
       
   113     cinfo->min_DCT_h_scaled_size = 8;
       
   114     cinfo->min_DCT_v_scaled_size = 8;
       
   115   } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * 8) {
       
   116     /* Provide 8/9 scaling */
       
   117     cinfo->jpeg_width = (JDIMENSION)
       
   118       jdiv_round_up((long) cinfo->image_width * 8, 9L);
       
   119     cinfo->jpeg_height = (JDIMENSION)
       
   120       jdiv_round_up((long) cinfo->image_height * 8, 9L);
       
   121     cinfo->min_DCT_h_scaled_size = 9;
       
   122     cinfo->min_DCT_v_scaled_size = 9;
       
   123   } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 4) {
       
   124     /* Provide 4/5 scaling */
       
   125     cinfo->jpeg_width = (JDIMENSION)
       
   126       jdiv_round_up((long) cinfo->image_width * 4, 5L);
       
   127     cinfo->jpeg_height = (JDIMENSION)
       
   128       jdiv_round_up((long) cinfo->image_height * 4, 5L);
       
   129     cinfo->min_DCT_h_scaled_size = 10;
       
   130     cinfo->min_DCT_v_scaled_size = 10;
       
   131   } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * 8) {
       
   132     /* Provide 8/11 scaling */
       
   133     cinfo->jpeg_width = (JDIMENSION)
       
   134       jdiv_round_up((long) cinfo->image_width * 8, 11L);
       
   135     cinfo->jpeg_height = (JDIMENSION)
       
   136       jdiv_round_up((long) cinfo->image_height * 8, 11L);
       
   137     cinfo->min_DCT_h_scaled_size = 11;
       
   138     cinfo->min_DCT_v_scaled_size = 11;
       
   139   } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 2) {
       
   140     /* Provide 2/3 scaling */
       
   141     cinfo->jpeg_width = (JDIMENSION)
       
   142       jdiv_round_up((long) cinfo->image_width * 2, 3L);
       
   143     cinfo->jpeg_height = (JDIMENSION)
       
   144       jdiv_round_up((long) cinfo->image_height * 2, 3L);
       
   145     cinfo->min_DCT_h_scaled_size = 12;
       
   146     cinfo->min_DCT_v_scaled_size = 12;
       
   147   } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * 8) {
       
   148     /* Provide 8/13 scaling */
       
   149     cinfo->jpeg_width = (JDIMENSION)
       
   150       jdiv_round_up((long) cinfo->image_width * 8, 13L);
       
   151     cinfo->jpeg_height = (JDIMENSION)
       
   152       jdiv_round_up((long) cinfo->image_height * 8, 13L);
       
   153     cinfo->min_DCT_h_scaled_size = 13;
       
   154     cinfo->min_DCT_v_scaled_size = 13;
       
   155   } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 4) {
       
   156     /* Provide 4/7 scaling */
       
   157     cinfo->jpeg_width = (JDIMENSION)
       
   158       jdiv_round_up((long) cinfo->image_width * 4, 7L);
       
   159     cinfo->jpeg_height = (JDIMENSION)
       
   160       jdiv_round_up((long) cinfo->image_height * 4, 7L);
       
   161     cinfo->min_DCT_h_scaled_size = 14;
       
   162     cinfo->min_DCT_v_scaled_size = 14;
       
   163   } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * 8) {
       
   164     /* Provide 8/15 scaling */
       
   165     cinfo->jpeg_width = (JDIMENSION)
       
   166       jdiv_round_up((long) cinfo->image_width * 8, 15L);
       
   167     cinfo->jpeg_height = (JDIMENSION)
       
   168       jdiv_round_up((long) cinfo->image_height * 8, 15L);
       
   169     cinfo->min_DCT_h_scaled_size = 15;
       
   170     cinfo->min_DCT_v_scaled_size = 15;
       
   171   } else {
       
   172     /* Provide 1/2 scaling */
       
   173     cinfo->jpeg_width = (JDIMENSION)
       
   174       jdiv_round_up((long) cinfo->image_width, 2L);
       
   175     cinfo->jpeg_height = (JDIMENSION)
       
   176       jdiv_round_up((long) cinfo->image_height, 2L);
       
   177     cinfo->min_DCT_h_scaled_size = 16;
       
   178     cinfo->min_DCT_v_scaled_size = 16;
       
   179   }
       
   180 
       
   181 #else /* !DCT_SCALING_SUPPORTED */
       
   182 
       
   183   /* Hardwire it to "no scaling" */
       
   184   cinfo->jpeg_width = cinfo->image_width;
       
   185   cinfo->jpeg_height = cinfo->image_height;
       
   186   cinfo->min_DCT_h_scaled_size = DCTSIZE;
       
   187   cinfo->min_DCT_v_scaled_size = DCTSIZE;
       
   188 
       
   189 #endif /* DCT_SCALING_SUPPORTED */
       
   190 
       
   191   cinfo->block_size = DCTSIZE;
       
   192   cinfo->natural_order = jpeg_natural_order;
       
   193   cinfo->lim_Se = DCTSIZE2-1;
       
   194 }
       
   195 
       
   196 
    45 LOCAL(void)
   197 LOCAL(void)
    46 initial_setup (j_compress_ptr cinfo)
   198 jpeg_calc_trans_dimensions (j_compress_ptr cinfo)
       
   199 {
       
   200   if (cinfo->min_DCT_h_scaled_size < 1 || cinfo->min_DCT_h_scaled_size > 16
       
   201       || cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size)
       
   202     ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
       
   203 	     cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size);
       
   204 
       
   205   cinfo->block_size = cinfo->min_DCT_h_scaled_size;
       
   206 
       
   207   switch (cinfo->block_size) {
       
   208   case 2: cinfo->natural_order = jpeg_natural_order2; break;
       
   209   case 3: cinfo->natural_order = jpeg_natural_order3; break;
       
   210   case 4: cinfo->natural_order = jpeg_natural_order4; break;
       
   211   case 5: cinfo->natural_order = jpeg_natural_order5; break;
       
   212   case 6: cinfo->natural_order = jpeg_natural_order6; break;
       
   213   case 7: cinfo->natural_order = jpeg_natural_order7; break;
       
   214   default: cinfo->natural_order = jpeg_natural_order; break;
       
   215   }
       
   216 
       
   217   cinfo->lim_Se = cinfo->block_size < DCTSIZE ?
       
   218     cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1;
       
   219 }
       
   220 
       
   221 
       
   222 LOCAL(void)
       
   223 initial_setup (j_compress_ptr cinfo, boolean transcode_only)
    47 /* Do computations that are needed before master selection phase */
   224 /* Do computations that are needed before master selection phase */
    48 {
   225 {
    49   int ci;
   226   int ci, ssize;
    50   jpeg_component_info *compptr;
   227   jpeg_component_info *compptr;
    51   long samplesperrow;
   228   long samplesperrow;
    52   JDIMENSION jd_samplesperrow;
   229   JDIMENSION jd_samplesperrow;
    53 
   230 
       
   231   if (transcode_only)
       
   232     jpeg_calc_trans_dimensions(cinfo);
       
   233   else
       
   234     jpeg_calc_jpeg_dimensions(cinfo);
       
   235 
    54   /* Sanity check on image dimensions */
   236   /* Sanity check on image dimensions */
    55   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
   237   if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 ||
    56       || cinfo->num_components <= 0 || cinfo->input_components <= 0)
   238       cinfo->num_components <= 0 || cinfo->input_components <= 0)
    57     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
   239     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
    58 
   240 
    59   /* Make sure image isn't bigger than I can handle */
   241   /* Make sure image isn't bigger than I can handle */
    60   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
   242   if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION ||
    61       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
   243       (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION)
    62     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
   244     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
    63 
   245 
    64   /* Width of an input scanline must be representable as JDIMENSION. */
   246   /* Width of an input scanline must be representable as JDIMENSION. */
    65   samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
   247   samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
    66   jd_samplesperrow = (JDIMENSION) samplesperrow;
   248   jd_samplesperrow = (JDIMENSION) samplesperrow;
    93   /* Compute dimensions of components */
   275   /* Compute dimensions of components */
    94   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   276   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
    95        ci++, compptr++) {
   277        ci++, compptr++) {
    96     /* Fill in the correct component_index value; don't rely on application */
   278     /* Fill in the correct component_index value; don't rely on application */
    97     compptr->component_index = ci;
   279     compptr->component_index = ci;
    98     /* For compression, we never do DCT scaling. */
   280     /* In selecting the actual DCT scaling for each component, we try to
    99     compptr->DCT_scaled_size = DCTSIZE;
   281      * scale down the chroma components via DCT scaling rather than downsampling.
       
   282      * This saves time if the downsampler gets to use 1:1 scaling.
       
   283      * Note this code adapts subsampling ratios which are powers of 2.
       
   284      */
       
   285     ssize = 1;
       
   286 #ifdef DCT_SCALING_SUPPORTED
       
   287     while (cinfo->min_DCT_h_scaled_size * ssize <=
       
   288 	   (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
       
   289 	   (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
       
   290       ssize = ssize * 2;
       
   291     }
       
   292 #endif
       
   293     compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
       
   294     ssize = 1;
       
   295 #ifdef DCT_SCALING_SUPPORTED
       
   296     while (cinfo->min_DCT_v_scaled_size * ssize <=
       
   297 	   (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
       
   298 	   (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
       
   299       ssize = ssize * 2;
       
   300     }
       
   301 #endif
       
   302     compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
       
   303 
       
   304     /* We don't support DCT ratios larger than 2. */
       
   305     if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
       
   306 	compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
       
   307     else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
       
   308 	compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
       
   309 
   100     /* Size in DCT blocks */
   310     /* Size in DCT blocks */
   101     compptr->width_in_blocks = (JDIMENSION)
   311     compptr->width_in_blocks = (JDIMENSION)
   102       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
   312       jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
   103 		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
   313 		    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
   104     compptr->height_in_blocks = (JDIMENSION)
   314     compptr->height_in_blocks = (JDIMENSION)
   105       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
   315       jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
   106 		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
   316 		    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
   107     /* Size in samples */
   317     /* Size in samples */
   108     compptr->downsampled_width = (JDIMENSION)
   318     compptr->downsampled_width = (JDIMENSION)
   109       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
   319       jdiv_round_up((long) cinfo->jpeg_width *
   110 		    (long) cinfo->max_h_samp_factor);
   320 		    (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
       
   321 		    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
   111     compptr->downsampled_height = (JDIMENSION)
   322     compptr->downsampled_height = (JDIMENSION)
   112       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
   323       jdiv_round_up((long) cinfo->jpeg_height *
   113 		    (long) cinfo->max_v_samp_factor);
   324 		    (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
       
   325 		    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
   114     /* Mark component needed (this flag isn't actually used for compression) */
   326     /* Mark component needed (this flag isn't actually used for compression) */
   115     compptr->component_needed = TRUE;
   327     compptr->component_needed = TRUE;
   116   }
   328   }
   117 
   329 
   118   /* Compute number of fully interleaved MCU rows (number of times that
   330   /* Compute number of fully interleaved MCU rows (number of times that
   119    * main controller will call coefficient controller).
   331    * main controller will call coefficient controller).
   120    */
   332    */
   121   cinfo->total_iMCU_rows = (JDIMENSION)
   333   cinfo->total_iMCU_rows = (JDIMENSION)
   122     jdiv_round_up((long) cinfo->image_height,
   334     jdiv_round_up((long) cinfo->jpeg_height,
   123 		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
   335 		  (long) (cinfo->max_v_samp_factor * cinfo->block_size));
   124 }
   336 }
   125 
   337 
   126 
   338 
   127 #ifdef C_MULTISCAN_FILES_SUPPORTED
   339 #ifdef C_MULTISCAN_FILES_SUPPORTED
   128 
   340 
   258 	ERREXIT(cinfo, JERR_MISSING_DATA);
   470 	ERREXIT(cinfo, JERR_MISSING_DATA);
   259     }
   471     }
   260   }
   472   }
   261 }
   473 }
   262 
   474 
       
   475 
       
   476 LOCAL(void)
       
   477 reduce_script (j_compress_ptr cinfo)
       
   478 /* Adapt scan script for use with reduced block size;
       
   479  * assume that script has been validated before.
       
   480  */
       
   481 {
       
   482   jpeg_scan_info * scanptr;
       
   483   int idxout, idxin;
       
   484 
       
   485   /* Circumvent const declaration for this function */
       
   486   scanptr = (jpeg_scan_info *) cinfo->scan_info;
       
   487   idxout = 0;
       
   488 
       
   489   for (idxin = 0; idxin < cinfo->num_scans; idxin++) {
       
   490     /* After skipping, idxout becomes smaller than idxin */
       
   491     if (idxin != idxout)
       
   492       /* Copy rest of data;
       
   493        * note we stay in given chunk of allocated memory.
       
   494        */
       
   495       scanptr[idxout] = scanptr[idxin];
       
   496     if (scanptr[idxout].Ss > cinfo->lim_Se)
       
   497       /* Entire scan out of range - skip this entry */
       
   498       continue;
       
   499     if (scanptr[idxout].Se > cinfo->lim_Se)
       
   500       /* Limit scan to end of block */
       
   501       scanptr[idxout].Se = cinfo->lim_Se;
       
   502     idxout++;
       
   503   }
       
   504 
       
   505   cinfo->num_scans = idxout;
       
   506 }
       
   507 
   263 #endif /* C_MULTISCAN_FILES_SUPPORTED */
   508 #endif /* C_MULTISCAN_FILES_SUPPORTED */
   264 
   509 
   265 
   510 
   266 LOCAL(void)
   511 LOCAL(void)
   267 select_scan_parameters (j_compress_ptr cinfo)
   512 select_scan_parameters (j_compress_ptr cinfo)
   278     cinfo->comps_in_scan = scanptr->comps_in_scan;
   523     cinfo->comps_in_scan = scanptr->comps_in_scan;
   279     for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
   524     for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
   280       cinfo->cur_comp_info[ci] =
   525       cinfo->cur_comp_info[ci] =
   281 	&cinfo->comp_info[scanptr->component_index[ci]];
   526 	&cinfo->comp_info[scanptr->component_index[ci]];
   282     }
   527     }
   283     cinfo->Ss = scanptr->Ss;
   528     if (cinfo->progressive_mode) {
   284     cinfo->Se = scanptr->Se;
   529       cinfo->Ss = scanptr->Ss;
   285     cinfo->Ah = scanptr->Ah;
   530       cinfo->Se = scanptr->Se;
   286     cinfo->Al = scanptr->Al;
   531       cinfo->Ah = scanptr->Ah;
       
   532       cinfo->Al = scanptr->Al;
       
   533       return;
       
   534     }
   287   }
   535   }
   288   else
   536   else
   289 #endif
   537 #endif
   290   {
   538   {
   291     /* Prepare for single sequential-JPEG scan containing all components */
   539     /* Prepare for single sequential-JPEG scan containing all components */
   294 	       MAX_COMPS_IN_SCAN);
   542 	       MAX_COMPS_IN_SCAN);
   295     cinfo->comps_in_scan = cinfo->num_components;
   543     cinfo->comps_in_scan = cinfo->num_components;
   296     for (ci = 0; ci < cinfo->num_components; ci++) {
   544     for (ci = 0; ci < cinfo->num_components; ci++) {
   297       cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
   545       cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
   298     }
   546     }
   299     cinfo->Ss = 0;
   547   }
   300     cinfo->Se = DCTSIZE2-1;
   548   cinfo->Ss = 0;
   301     cinfo->Ah = 0;
   549   cinfo->Se = cinfo->block_size * cinfo->block_size - 1;
   302     cinfo->Al = 0;
   550   cinfo->Ah = 0;
   303   }
   551   cinfo->Al = 0;
   304 }
   552 }
   305 
   553 
   306 
   554 
   307 LOCAL(void)
   555 LOCAL(void)
   308 per_scan_setup (j_compress_ptr cinfo)
   556 per_scan_setup (j_compress_ptr cinfo)
   323     
   571     
   324     /* For noninterleaved scan, always one block per MCU */
   572     /* For noninterleaved scan, always one block per MCU */
   325     compptr->MCU_width = 1;
   573     compptr->MCU_width = 1;
   326     compptr->MCU_height = 1;
   574     compptr->MCU_height = 1;
   327     compptr->MCU_blocks = 1;
   575     compptr->MCU_blocks = 1;
   328     compptr->MCU_sample_width = DCTSIZE;
   576     compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
   329     compptr->last_col_width = 1;
   577     compptr->last_col_width = 1;
   330     /* For noninterleaved scans, it is convenient to define last_row_height
   578     /* For noninterleaved scans, it is convenient to define last_row_height
   331      * as the number of block rows present in the last iMCU row.
   579      * as the number of block rows present in the last iMCU row.
   332      */
   580      */
   333     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
   581     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
   345       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
   593       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
   346 	       MAX_COMPS_IN_SCAN);
   594 	       MAX_COMPS_IN_SCAN);
   347     
   595     
   348     /* Overall image size in MCUs */
   596     /* Overall image size in MCUs */
   349     cinfo->MCUs_per_row = (JDIMENSION)
   597     cinfo->MCUs_per_row = (JDIMENSION)
   350       jdiv_round_up((long) cinfo->image_width,
   598       jdiv_round_up((long) cinfo->jpeg_width,
   351 		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
   599 		    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
   352     cinfo->MCU_rows_in_scan = (JDIMENSION)
   600     cinfo->MCU_rows_in_scan = (JDIMENSION)
   353       jdiv_round_up((long) cinfo->image_height,
   601       jdiv_round_up((long) cinfo->jpeg_height,
   354 		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
   602 		    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
   355     
   603     
   356     cinfo->blocks_in_MCU = 0;
   604     cinfo->blocks_in_MCU = 0;
   357     
   605     
   358     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   606     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   359       compptr = cinfo->cur_comp_info[ci];
   607       compptr = cinfo->cur_comp_info[ci];
   360       /* Sampling factors give # of blocks of component in each MCU */
   608       /* Sampling factors give # of blocks of component in each MCU */
   361       compptr->MCU_width = compptr->h_samp_factor;
   609       compptr->MCU_width = compptr->h_samp_factor;
   362       compptr->MCU_height = compptr->v_samp_factor;
   610       compptr->MCU_height = compptr->v_samp_factor;
   363       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
   611       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
   364       compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
   612       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
   365       /* Figure number of non-dummy blocks in last MCU column & row */
   613       /* Figure number of non-dummy blocks in last MCU column & row */
   366       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
   614       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
   367       if (tmp == 0) tmp = compptr->MCU_width;
   615       if (tmp == 0) tmp = compptr->MCU_width;
   368       compptr->last_col_width = tmp;
   616       compptr->last_col_width = tmp;
   369       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
   617       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
   431 #ifdef ENTROPY_OPT_SUPPORTED
   679 #ifdef ENTROPY_OPT_SUPPORTED
   432   case huff_opt_pass:
   680   case huff_opt_pass:
   433     /* Do Huffman optimization for a scan after the first one. */
   681     /* Do Huffman optimization for a scan after the first one. */
   434     select_scan_parameters(cinfo);
   682     select_scan_parameters(cinfo);
   435     per_scan_setup(cinfo);
   683     per_scan_setup(cinfo);
   436     if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
   684     if (cinfo->Ss != 0 || cinfo->Ah == 0) {
   437       (*cinfo->entropy->start_pass) (cinfo, TRUE);
   685       (*cinfo->entropy->start_pass) (cinfo, TRUE);
   438       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
   686       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
   439       master->pub.call_pass_startup = FALSE;
   687       master->pub.call_pass_startup = FALSE;
   440       break;
   688       break;
   441     }
   689     }
   552   master->pub.pass_startup = pass_startup;
   800   master->pub.pass_startup = pass_startup;
   553   master->pub.finish_pass = finish_pass_master;
   801   master->pub.finish_pass = finish_pass_master;
   554   master->pub.is_last_pass = FALSE;
   802   master->pub.is_last_pass = FALSE;
   555 
   803 
   556   /* Validate parameters, determine derived values */
   804   /* Validate parameters, determine derived values */
   557   initial_setup(cinfo);
   805   initial_setup(cinfo, transcode_only);
   558 
   806 
   559   if (cinfo->scan_info != NULL) {
   807   if (cinfo->scan_info != NULL) {
   560 #ifdef C_MULTISCAN_FILES_SUPPORTED
   808 #ifdef C_MULTISCAN_FILES_SUPPORTED
   561     validate_script(cinfo);
   809     validate_script(cinfo);
       
   810     if (cinfo->block_size < DCTSIZE)
       
   811       reduce_script(cinfo);
   562 #else
   812 #else
   563     ERREXIT(cinfo, JERR_NOT_COMPILED);
   813     ERREXIT(cinfo, JERR_NOT_COMPILED);
   564 #endif
   814 #endif
   565   } else {
   815   } else {
   566     cinfo->progressive_mode = FALSE;
   816     cinfo->progressive_mode = FALSE;
   567     cinfo->num_scans = 1;
   817     cinfo->num_scans = 1;
   568   }
   818   }
   569 
   819 
   570   if (cinfo->progressive_mode)	/*  TEMPORARY HACK ??? */
   820   if ((cinfo->progressive_mode || cinfo->block_size < DCTSIZE) &&
   571     cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
   821       !cinfo->arith_code)			/*  TEMPORARY HACK ??? */
       
   822     /* assume default tables no good for progressive or downscale mode */
       
   823     cinfo->optimize_coding = TRUE;
   572 
   824 
   573   /* Initialize my private state */
   825   /* Initialize my private state */
   574   if (transcode_only) {
   826   if (transcode_only) {
   575     /* no main pass in transcoding */
   827     /* no main pass in transcoding */
   576     if (cinfo->optimize_coding)
   828     if (cinfo->optimize_coding)