/src/libjpeg-turbo.2.1.x/jcmaster.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * jcmaster.c | 
| 3 |  |  * | 
| 4 |  |  * This file was part of the Independent JPEG Group's software: | 
| 5 |  |  * Copyright (C) 1991-1997, Thomas G. Lane. | 
| 6 |  |  * Modified 2003-2010 by Guido Vollbeding. | 
| 7 |  |  * libjpeg-turbo Modifications: | 
| 8 |  |  * Copyright (C) 2010, 2016, 2018, D. R. Commander. | 
| 9 |  |  * For conditions of distribution and use, see the accompanying README.ijg | 
| 10 |  |  * file. | 
| 11 |  |  * | 
| 12 |  |  * This file contains master control logic for the JPEG compressor. | 
| 13 |  |  * These routines are concerned with parameter validation, initial setup, | 
| 14 |  |  * and inter-pass control (determining the number of passes and the work | 
| 15 |  |  * to be done in each pass). | 
| 16 |  |  */ | 
| 17 |  |  | 
| 18 |  | #define JPEG_INTERNALS | 
| 19 |  | #include "jinclude.h" | 
| 20 |  | #include "jpeglib.h" | 
| 21 |  | #include "jpegcomp.h" | 
| 22 |  |  | 
| 23 |  |  | 
| 24 |  | /* Private state */ | 
| 25 |  |  | 
| 26 |  | typedef enum { | 
| 27 |  |   main_pass,                    /* input data, also do first output step */ | 
| 28 |  |   huff_opt_pass,                /* Huffman code optimization pass */ | 
| 29 |  |   output_pass                   /* data output pass */ | 
| 30 |  | } c_pass_type; | 
| 31 |  |  | 
| 32 |  | typedef struct { | 
| 33 |  |   struct jpeg_comp_master pub;  /* public fields */ | 
| 34 |  |  | 
| 35 |  |   c_pass_type pass_type;        /* the type of the current pass */ | 
| 36 |  |  | 
| 37 |  |   int pass_number;              /* # of passes completed */ | 
| 38 |  |   int total_passes;             /* total # of passes needed */ | 
| 39 |  |  | 
| 40 |  |   int scan_number;              /* current index in scan_info[] */ | 
| 41 |  |  | 
| 42 |  |   /* | 
| 43 |  |    * This is here so we can add libjpeg-turbo version/build information to the | 
| 44 |  |    * global string table without introducing a new global symbol.  Adding this | 
| 45 |  |    * information to the global string table allows one to examine a binary | 
| 46 |  |    * object and determine which version of libjpeg-turbo it was built from or | 
| 47 |  |    * linked against. | 
| 48 |  |    */ | 
| 49 |  |   const char *jpeg_version; | 
| 50 |  |  | 
| 51 |  | } my_comp_master; | 
| 52 |  |  | 
| 53 |  | typedef my_comp_master *my_master_ptr; | 
| 54 |  |  | 
| 55 |  |  | 
| 56 |  | /* | 
| 57 |  |  * Support routines that do various essential calculations. | 
| 58 |  |  */ | 
| 59 |  |  | 
| 60 |  | #if JPEG_LIB_VERSION >= 70 | 
| 61 |  | /* | 
| 62 |  |  * Compute JPEG image dimensions and related values. | 
| 63 |  |  * NOTE: this is exported for possible use by application. | 
| 64 |  |  * Hence it mustn't do anything that can't be done twice. | 
| 65 |  |  */ | 
| 66 |  |  | 
| 67 |  | GLOBAL(void) | 
| 68 |  | jpeg_calc_jpeg_dimensions(j_compress_ptr cinfo) | 
| 69 |  | /* Do computations that are needed before master selection phase */ | 
| 70 |  | { | 
| 71 |  |   /* Hardwire it to "no scaling" */ | 
| 72 |  |   cinfo->jpeg_width = cinfo->image_width; | 
| 73 |  |   cinfo->jpeg_height = cinfo->image_height; | 
| 74 |  |   cinfo->min_DCT_h_scaled_size = DCTSIZE; | 
| 75 |  |   cinfo->min_DCT_v_scaled_size = DCTSIZE; | 
| 76 |  | } | 
| 77 |  | #endif | 
| 78 |  |  | 
| 79 |  |  | 
| 80 |  | LOCAL(void) | 
| 81 |  | initial_setup(j_compress_ptr cinfo, boolean transcode_only) | 
| 82 |  | /* Do computations that are needed before master selection phase */ | 
| 83 | 4.99k | { | 
| 84 | 4.99k |   int ci; | 
| 85 | 4.99k |   jpeg_component_info *compptr; | 
| 86 | 4.99k |   long samplesperrow; | 
| 87 | 4.99k |   JDIMENSION jd_samplesperrow; | 
| 88 |  |  | 
| 89 |  | #if JPEG_LIB_VERSION >= 70 | 
| 90 |  | #if JPEG_LIB_VERSION >= 80 | 
| 91 |  |   if (!transcode_only) | 
| 92 |  | #endif | 
| 93 |  |     jpeg_calc_jpeg_dimensions(cinfo); | 
| 94 |  | #endif | 
| 95 |  |  | 
| 96 |  |   /* Sanity check on image dimensions */ | 
| 97 | 4.99k |   if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 || | 
| 98 | 4.99k |       cinfo->num_components <= 0 || cinfo->input_components <= 0) | 
| 99 | 0 |     ERREXIT(cinfo, JERR_EMPTY_IMAGE); | 
| 100 |  |  | 
| 101 |  |   /* Make sure image isn't bigger than I can handle */ | 
| 102 | 4.99k |   if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION || | 
| 103 | 4.99k |       (long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION) | 
| 104 | 52 |     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION); | 
| 105 |  |  | 
| 106 |  |   /* Width of an input scanline must be representable as JDIMENSION. */ | 
| 107 | 4.99k |   samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components; | 
| 108 | 4.99k |   jd_samplesperrow = (JDIMENSION)samplesperrow; | 
| 109 | 4.99k |   if ((long)jd_samplesperrow != samplesperrow) | 
| 110 | 0 |     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); | 
| 111 |  |  | 
| 112 |  |   /* For now, precision must match compiled-in value... */ | 
| 113 | 4.99k |   if (cinfo->data_precision != BITS_IN_JSAMPLE) | 
| 114 | 0 |     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); | 
| 115 |  |  | 
| 116 |  |   /* Check that number of components won't exceed internal array sizes */ | 
| 117 | 4.99k |   if (cinfo->num_components > MAX_COMPONENTS) | 
| 118 | 0 |     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, | 
| 119 | 4.99k |              MAX_COMPONENTS); | 
| 120 |  |  | 
| 121 |  |   /* Compute maximum sampling factors; check factor validity */ | 
| 122 | 4.99k |   cinfo->max_h_samp_factor = 1; | 
| 123 | 4.99k |   cinfo->max_v_samp_factor = 1; | 
| 124 | 18.4k |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; | 
| 125 | 13.5k |        ci++, compptr++) { | 
| 126 | 13.5k |     if (compptr->h_samp_factor <= 0 || | 
| 127 | 13.5k |         compptr->h_samp_factor > MAX_SAMP_FACTOR || | 
| 128 | 13.5k |         compptr->v_samp_factor <= 0 || | 
| 129 | 13.5k |         compptr->v_samp_factor > MAX_SAMP_FACTOR) | 
| 130 | 0 |       ERREXIT(cinfo, JERR_BAD_SAMPLING); | 
| 131 | 13.5k |     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, | 
| 132 | 13.5k |                                    compptr->h_samp_factor); | 
| 133 | 13.5k |     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, | 
| 134 | 13.5k |                                    compptr->v_samp_factor); | 
| 135 | 13.5k |   } | 
| 136 |  |  | 
| 137 |  |   /* Compute dimensions of components */ | 
| 138 | 18.4k |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; | 
| 139 | 13.5k |        ci++, compptr++) { | 
| 140 |  |     /* Fill in the correct component_index value; don't rely on application */ | 
| 141 | 13.5k |     compptr->component_index = ci; | 
| 142 |  |     /* For compression, we never do DCT scaling. */ | 
| 143 |  | #if JPEG_LIB_VERSION >= 70 | 
| 144 |  |     compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE; | 
| 145 |  | #else | 
| 146 | 13.5k |     compptr->DCT_scaled_size = DCTSIZE; | 
| 147 | 13.5k | #endif | 
| 148 |  |     /* Size in DCT blocks */ | 
| 149 | 13.5k |     compptr->width_in_blocks = (JDIMENSION) | 
| 150 | 13.5k |       jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor, | 
| 151 | 13.5k |                     (long)(cinfo->max_h_samp_factor * DCTSIZE)); | 
| 152 | 13.5k |     compptr->height_in_blocks = (JDIMENSION) | 
| 153 | 13.5k |       jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor, | 
| 154 | 13.5k |                     (long)(cinfo->max_v_samp_factor * DCTSIZE)); | 
| 155 |  |     /* Size in samples */ | 
| 156 | 13.5k |     compptr->downsampled_width = (JDIMENSION) | 
| 157 | 13.5k |       jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor, | 
| 158 | 13.5k |                     (long)cinfo->max_h_samp_factor); | 
| 159 | 13.5k |     compptr->downsampled_height = (JDIMENSION) | 
| 160 | 13.5k |       jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor, | 
| 161 | 13.5k |                     (long)cinfo->max_v_samp_factor); | 
| 162 |  |     /* Mark component needed (this flag isn't actually used for compression) */ | 
| 163 | 13.5k |     compptr->component_needed = TRUE; | 
| 164 | 13.5k |   } | 
| 165 |  |  | 
| 166 |  |   /* Compute number of fully interleaved MCU rows (number of times that | 
| 167 |  |    * main controller will call coefficient controller). | 
| 168 |  |    */ | 
| 169 | 4.99k |   cinfo->total_iMCU_rows = (JDIMENSION) | 
| 170 | 4.99k |     jdiv_round_up((long)cinfo->_jpeg_height, | 
| 171 | 4.99k |                   (long)(cinfo->max_v_samp_factor * DCTSIZE)); | 
| 172 | 4.99k | } | 
| 173 |  |  | 
| 174 |  |  | 
| 175 |  | #ifdef C_MULTISCAN_FILES_SUPPORTED | 
| 176 |  |  | 
| 177 |  | LOCAL(void) | 
| 178 |  | validate_script(j_compress_ptr cinfo) | 
| 179 |  | /* Verify that the scan script in cinfo->scan_info[] is valid; also | 
| 180 |  |  * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. | 
| 181 |  |  */ | 
| 182 | 0 | { | 
| 183 | 0 |   const jpeg_scan_info *scanptr; | 
| 184 | 0 |   int scanno, ncomps, ci, coefi, thisi; | 
| 185 | 0 |   int Ss, Se, Ah, Al; | 
| 186 | 0 |   boolean component_sent[MAX_COMPONENTS]; | 
| 187 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED | 
| 188 | 0 |   int *last_bitpos_ptr; | 
| 189 | 0 |   int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; | 
| 190 |  |   /* -1 until that coefficient has been seen; then last Al for it */ | 
| 191 | 0 | #endif | 
| 192 |  | 
 | 
| 193 | 0 |   if (cinfo->num_scans <= 0) | 
| 194 | 0 |     ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); | 
| 195 |  |  | 
| 196 |  |   /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; | 
| 197 |  |    * for progressive JPEG, no scan can have this. | 
| 198 |  |    */ | 
| 199 | 0 |   scanptr = cinfo->scan_info; | 
| 200 | 0 |   if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) { | 
| 201 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED | 
| 202 | 0 |     cinfo->progressive_mode = TRUE; | 
| 203 | 0 |     last_bitpos_ptr = &last_bitpos[0][0]; | 
| 204 | 0 |     for (ci = 0; ci < cinfo->num_components; ci++) | 
| 205 | 0 |       for (coefi = 0; coefi < DCTSIZE2; coefi++) | 
| 206 | 0 |         *last_bitpos_ptr++ = -1; | 
| 207 |  | #else | 
| 208 |  |     ERREXIT(cinfo, JERR_NOT_COMPILED); | 
| 209 |  | #endif | 
| 210 | 0 |   } else { | 
| 211 | 0 |     cinfo->progressive_mode = FALSE; | 
| 212 | 0 |     for (ci = 0; ci < cinfo->num_components; ci++) | 
| 213 | 0 |       component_sent[ci] = FALSE; | 
| 214 | 0 |   } | 
| 215 |  | 
 | 
| 216 | 0 |   for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { | 
| 217 |  |     /* Validate component indexes */ | 
| 218 | 0 |     ncomps = scanptr->comps_in_scan; | 
| 219 | 0 |     if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) | 
| 220 | 0 |       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); | 
| 221 | 0 |     for (ci = 0; ci < ncomps; ci++) { | 
| 222 | 0 |       thisi = scanptr->component_index[ci]; | 
| 223 | 0 |       if (thisi < 0 || thisi >= cinfo->num_components) | 
| 224 | 0 |         ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); | 
| 225 |  |       /* Components must appear in SOF order within each scan */ | 
| 226 | 0 |       if (ci > 0 && thisi <= scanptr->component_index[ci - 1]) | 
| 227 | 0 |         ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); | 
| 228 | 0 |     } | 
| 229 |  |     /* Validate progression parameters */ | 
| 230 | 0 |     Ss = scanptr->Ss; | 
| 231 | 0 |     Se = scanptr->Se; | 
| 232 | 0 |     Ah = scanptr->Ah; | 
| 233 | 0 |     Al = scanptr->Al; | 
| 234 | 0 |     if (cinfo->progressive_mode) { | 
| 235 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED | 
| 236 |  |       /* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah | 
| 237 |  |        * and Al, but that seems wrong: the upper bound ought to depend on data | 
| 238 |  |        * precision.  Perhaps they really meant 0..N+1 for N-bit precision. | 
| 239 |  |        * Here we allow 0..10 for 8-bit data; Al larger than 10 results in | 
| 240 |  |        * out-of-range reconstructed DC values during the first DC scan, | 
| 241 |  |        * which might cause problems for some decoders. | 
| 242 |  |        */ | 
| 243 | 0 | #if BITS_IN_JSAMPLE == 8 | 
| 244 | 0 | #define MAX_AH_AL  10 | 
| 245 |  | #else | 
| 246 |  | #define MAX_AH_AL  13 | 
| 247 |  | #endif | 
| 248 | 0 |       if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || | 
| 249 | 0 |           Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) | 
| 250 | 0 |         ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
| 251 | 0 |       if (Ss == 0) { | 
| 252 | 0 |         if (Se != 0)            /* DC and AC together not OK */ | 
| 253 | 0 |           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
| 254 | 0 |       } else { | 
| 255 | 0 |         if (ncomps != 1)        /* AC scans must be for only one component */ | 
| 256 | 0 |           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
| 257 | 0 |       } | 
| 258 | 0 |       for (ci = 0; ci < ncomps; ci++) { | 
| 259 | 0 |         last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0]; | 
| 260 | 0 |         if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ | 
| 261 | 0 |           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
| 262 | 0 |         for (coefi = Ss; coefi <= Se; coefi++) { | 
| 263 | 0 |           if (last_bitpos_ptr[coefi] < 0) { | 
| 264 |  |             /* first scan of this coefficient */ | 
| 265 | 0 |             if (Ah != 0) | 
| 266 | 0 |               ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
| 267 | 0 |           } else { | 
| 268 |  |             /* not first scan */ | 
| 269 | 0 |             if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1) | 
| 270 | 0 |               ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
| 271 | 0 |           } | 
| 272 | 0 |           last_bitpos_ptr[coefi] = Al; | 
| 273 | 0 |         } | 
| 274 | 0 |       } | 
| 275 | 0 | #endif | 
| 276 | 0 |     } else { | 
| 277 |  |       /* For sequential JPEG, all progression parameters must be these: */ | 
| 278 | 0 |       if (Ss != 0 || Se != DCTSIZE2 - 1 || Ah != 0 || Al != 0) | 
| 279 | 0 |         ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
| 280 |  |       /* Make sure components are not sent twice */ | 
| 281 | 0 |       for (ci = 0; ci < ncomps; ci++) { | 
| 282 | 0 |         thisi = scanptr->component_index[ci]; | 
| 283 | 0 |         if (component_sent[thisi]) | 
| 284 | 0 |           ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); | 
| 285 | 0 |         component_sent[thisi] = TRUE; | 
| 286 | 0 |       } | 
| 287 | 0 |     } | 
| 288 | 0 |   } | 
| 289 |  |  | 
| 290 |  |   /* Now verify that everything got sent. */ | 
| 291 | 0 |   if (cinfo->progressive_mode) { | 
| 292 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED | 
| 293 |  |     /* For progressive mode, we only check that at least some DC data | 
| 294 |  |      * got sent for each component; the spec does not require that all bits | 
| 295 |  |      * of all coefficients be transmitted.  Would it be wiser to enforce | 
| 296 |  |      * transmission of all coefficient bits?? | 
| 297 |  |      */ | 
| 298 | 0 |     for (ci = 0; ci < cinfo->num_components; ci++) { | 
| 299 | 0 |       if (last_bitpos[ci][0] < 0) | 
| 300 | 0 |         ERREXIT(cinfo, JERR_MISSING_DATA); | 
| 301 | 0 |     } | 
| 302 | 0 | #endif | 
| 303 | 0 |   } else { | 
| 304 | 0 |     for (ci = 0; ci < cinfo->num_components; ci++) { | 
| 305 | 0 |       if (!component_sent[ci]) | 
| 306 | 0 |         ERREXIT(cinfo, JERR_MISSING_DATA); | 
| 307 | 0 |     } | 
| 308 | 0 |   } | 
| 309 | 0 | } | 
| 310 |  |  | 
| 311 |  | #endif /* C_MULTISCAN_FILES_SUPPORTED */ | 
| 312 |  |  | 
| 313 |  |  | 
| 314 |  | LOCAL(void) | 
| 315 |  | select_scan_parameters(j_compress_ptr cinfo) | 
| 316 |  | /* Set up the scan parameters for the current scan */ | 
| 317 | 4.27k | { | 
| 318 | 4.27k |   int ci; | 
| 319 |  |  | 
| 320 | 4.27k | #ifdef C_MULTISCAN_FILES_SUPPORTED | 
| 321 | 4.27k |   if (cinfo->scan_info != NULL) { | 
| 322 |  |     /* Prepare for current scan --- the script is already validated */ | 
| 323 | 0 |     my_master_ptr master = (my_master_ptr)cinfo->master; | 
| 324 | 0 |     const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number; | 
| 325 |  | 
 | 
| 326 | 0 |     cinfo->comps_in_scan = scanptr->comps_in_scan; | 
| 327 | 0 |     for (ci = 0; ci < scanptr->comps_in_scan; ci++) { | 
| 328 | 0 |       cinfo->cur_comp_info[ci] = | 
| 329 | 0 |         &cinfo->comp_info[scanptr->component_index[ci]]; | 
| 330 | 0 |     } | 
| 331 | 0 |     cinfo->Ss = scanptr->Ss; | 
| 332 | 0 |     cinfo->Se = scanptr->Se; | 
| 333 | 0 |     cinfo->Ah = scanptr->Ah; | 
| 334 | 0 |     cinfo->Al = scanptr->Al; | 
| 335 | 0 |   } else | 
| 336 | 4.27k | #endif | 
| 337 | 4.27k |   { | 
| 338 |  |     /* Prepare for single sequential-JPEG scan containing all components */ | 
| 339 | 4.27k |     if (cinfo->num_components > MAX_COMPS_IN_SCAN) | 
| 340 | 0 |       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, | 
| 341 | 4.27k |                MAX_COMPS_IN_SCAN); | 
| 342 | 4.27k |     cinfo->comps_in_scan = cinfo->num_components; | 
| 343 | 15.7k |     for (ci = 0; ci < cinfo->num_components; ci++) { | 
| 344 | 11.4k |       cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; | 
| 345 | 11.4k |     } | 
| 346 | 4.27k |     cinfo->Ss = 0; | 
| 347 | 4.27k |     cinfo->Se = DCTSIZE2 - 1; | 
| 348 | 4.27k |     cinfo->Ah = 0; | 
| 349 | 4.27k |     cinfo->Al = 0; | 
| 350 | 4.27k |   } | 
| 351 | 4.27k | } | 
| 352 |  |  | 
| 353 |  |  | 
| 354 |  | LOCAL(void) | 
| 355 |  | per_scan_setup(j_compress_ptr cinfo) | 
| 356 |  | /* Do computations that are needed before processing a JPEG scan */ | 
| 357 |  | /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ | 
| 358 | 4.27k | { | 
| 359 | 4.27k |   int ci, mcublks, tmp; | 
| 360 | 4.27k |   jpeg_component_info *compptr; | 
| 361 |  |  | 
| 362 | 4.27k |   if (cinfo->comps_in_scan == 1) { | 
| 363 |  |  | 
| 364 |  |     /* Noninterleaved (single-component) scan */ | 
| 365 | 669 |     compptr = cinfo->cur_comp_info[0]; | 
| 366 |  |  | 
| 367 |  |     /* Overall image size in MCUs */ | 
| 368 | 669 |     cinfo->MCUs_per_row = compptr->width_in_blocks; | 
| 369 | 669 |     cinfo->MCU_rows_in_scan = compptr->height_in_blocks; | 
| 370 |  |  | 
| 371 |  |     /* For noninterleaved scan, always one block per MCU */ | 
| 372 | 669 |     compptr->MCU_width = 1; | 
| 373 | 669 |     compptr->MCU_height = 1; | 
| 374 | 669 |     compptr->MCU_blocks = 1; | 
| 375 | 669 |     compptr->MCU_sample_width = DCTSIZE; | 
| 376 | 669 |     compptr->last_col_width = 1; | 
| 377 |  |     /* For noninterleaved scans, it is convenient to define last_row_height | 
| 378 |  |      * as the number of block rows present in the last iMCU row. | 
| 379 |  |      */ | 
| 380 | 669 |     tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor); | 
| 381 | 669 |     if (tmp == 0) tmp = compptr->v_samp_factor; | 
| 382 | 669 |     compptr->last_row_height = tmp; | 
| 383 |  |  | 
| 384 |  |     /* Prepare array describing MCU composition */ | 
| 385 | 669 |     cinfo->blocks_in_MCU = 1; | 
| 386 | 669 |     cinfo->MCU_membership[0] = 0; | 
| 387 |  |  | 
| 388 | 3.60k |   } else { | 
| 389 |  |  | 
| 390 |  |     /* Interleaved (multi-component) scan */ | 
| 391 | 3.60k |     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) | 
| 392 | 0 |       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, | 
| 393 | 3.60k |                MAX_COMPS_IN_SCAN); | 
| 394 |  |  | 
| 395 |  |     /* Overall image size in MCUs */ | 
| 396 | 3.60k |     cinfo->MCUs_per_row = (JDIMENSION) | 
| 397 | 3.60k |       jdiv_round_up((long)cinfo->_jpeg_width, | 
| 398 | 3.60k |                     (long)(cinfo->max_h_samp_factor * DCTSIZE)); | 
| 399 | 3.60k |     cinfo->MCU_rows_in_scan = (JDIMENSION) | 
| 400 | 3.60k |       jdiv_round_up((long)cinfo->_jpeg_height, | 
| 401 | 3.60k |                     (long)(cinfo->max_v_samp_factor * DCTSIZE)); | 
| 402 |  |  | 
| 403 | 3.60k |     cinfo->blocks_in_MCU = 0; | 
| 404 |  |  | 
| 405 | 14.4k |     for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 
| 406 | 10.8k |       compptr = cinfo->cur_comp_info[ci]; | 
| 407 |  |       /* Sampling factors give # of blocks of component in each MCU */ | 
| 408 | 10.8k |       compptr->MCU_width = compptr->h_samp_factor; | 
| 409 | 10.8k |       compptr->MCU_height = compptr->v_samp_factor; | 
| 410 | 10.8k |       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; | 
| 411 | 10.8k |       compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE; | 
| 412 |  |       /* Figure number of non-dummy blocks in last MCU column & row */ | 
| 413 | 10.8k |       tmp = (int)(compptr->width_in_blocks % compptr->MCU_width); | 
| 414 | 10.8k |       if (tmp == 0) tmp = compptr->MCU_width; | 
| 415 | 10.8k |       compptr->last_col_width = tmp; | 
| 416 | 10.8k |       tmp = (int)(compptr->height_in_blocks % compptr->MCU_height); | 
| 417 | 10.8k |       if (tmp == 0) tmp = compptr->MCU_height; | 
| 418 | 10.8k |       compptr->last_row_height = tmp; | 
| 419 |  |       /* Prepare array describing MCU composition */ | 
| 420 | 10.8k |       mcublks = compptr->MCU_blocks; | 
| 421 | 10.8k |       if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) | 
| 422 | 0 |         ERREXIT(cinfo, JERR_BAD_MCU_SIZE); | 
| 423 | 39.6k |       while (mcublks-- > 0) { | 
| 424 | 28.8k |         cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; | 
| 425 | 28.8k |       } | 
| 426 | 10.8k |     } | 
| 427 |  |  | 
| 428 | 3.60k |   } | 
| 429 |  |  | 
| 430 |  |   /* Convert restart specified in rows to actual MCU count. */ | 
| 431 |  |   /* Note that count must fit in 16 bits, so we provide limiting. */ | 
| 432 | 4.27k |   if (cinfo->restart_in_rows > 0) { | 
| 433 | 2.47k |     long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row; | 
| 434 | 2.47k |     cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L); | 
| 435 | 2.47k |   } | 
| 436 | 4.27k | } | 
| 437 |  |  | 
| 438 |  |  | 
| 439 |  | /* | 
| 440 |  |  * Per-pass setup. | 
| 441 |  |  * This is called at the beginning of each pass.  We determine which modules | 
| 442 |  |  * will be active during this pass and give them appropriate start_pass calls. | 
| 443 |  |  * We also set is_last_pass to indicate whether any more passes will be | 
| 444 |  |  * required. | 
| 445 |  |  */ | 
| 446 |  |  | 
| 447 |  | METHODDEF(void) | 
| 448 |  | prepare_for_pass(j_compress_ptr cinfo) | 
| 449 | 5.40k | { | 
| 450 | 5.40k |   my_master_ptr master = (my_master_ptr)cinfo->master; | 
| 451 |  |  | 
| 452 | 5.40k |   switch (master->pass_type) { | 
| 453 | 4.27k |   case main_pass: | 
| 454 |  |     /* Initial pass: will collect input data, and do either Huffman | 
| 455 |  |      * optimization or data output for the first scan. | 
| 456 |  |      */ | 
| 457 | 4.27k |     select_scan_parameters(cinfo); | 
| 458 | 4.27k |     per_scan_setup(cinfo); | 
| 459 | 4.27k |     if (!cinfo->raw_data_in) { | 
| 460 | 4.27k |       (*cinfo->cconvert->start_pass) (cinfo); | 
| 461 | 4.27k |       (*cinfo->downsample->start_pass) (cinfo); | 
| 462 | 4.27k |       (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); | 
| 463 | 4.27k |     } | 
| 464 | 4.27k |     (*cinfo->fdct->start_pass) (cinfo); | 
| 465 | 4.27k |     (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); | 
| 466 | 4.27k |     (*cinfo->coef->start_pass) (cinfo, | 
| 467 | 4.27k |                                 (master->total_passes > 1 ? | 
| 468 | 2.47k |                                  JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); | 
| 469 | 4.27k |     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); | 
| 470 | 4.27k |     if (cinfo->optimize_coding) { | 
| 471 |  |       /* No immediate data output; postpone writing frame/scan headers */ | 
| 472 | 2.47k |       master->pub.call_pass_startup = FALSE; | 
| 473 | 2.47k |     } else { | 
| 474 |  |       /* Will write frame/scan headers at first jpeg_write_scanlines call */ | 
| 475 | 1.80k |       master->pub.call_pass_startup = TRUE; | 
| 476 | 1.80k |     } | 
| 477 | 4.27k |     break; | 
| 478 | 0 | #ifdef ENTROPY_OPT_SUPPORTED | 
| 479 | 0 |   case huff_opt_pass: | 
| 480 |  |     /* Do Huffman optimization for a scan after the first one. */ | 
| 481 | 0 |     select_scan_parameters(cinfo); | 
| 482 | 0 |     per_scan_setup(cinfo); | 
| 483 | 0 |     if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) { | 
| 484 | 0 |       (*cinfo->entropy->start_pass) (cinfo, TRUE); | 
| 485 | 0 |       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); | 
| 486 | 0 |       master->pub.call_pass_startup = FALSE; | 
| 487 | 0 |       break; | 
| 488 | 0 |     } | 
| 489 |  |     /* Special case: Huffman DC refinement scans need no Huffman table | 
| 490 |  |      * and therefore we can skip the optimization pass for them. | 
| 491 |  |      */ | 
| 492 | 0 |     master->pass_type = output_pass; | 
| 493 | 0 |     master->pass_number++; | 
| 494 | 0 | #endif | 
| 495 |  |     FALLTHROUGH                 /*FALLTHROUGH*/ | 
| 496 | 1.12k |   case output_pass: | 
| 497 |  |     /* Do a data-output pass. */ | 
| 498 |  |     /* We need not repeat per-scan setup if prior optimization pass did it. */ | 
| 499 | 1.12k |     if (!cinfo->optimize_coding) { | 
| 500 | 0 |       select_scan_parameters(cinfo); | 
| 501 | 0 |       per_scan_setup(cinfo); | 
| 502 | 0 |     } | 
| 503 | 1.12k |     (*cinfo->entropy->start_pass) (cinfo, FALSE); | 
| 504 | 1.12k |     (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); | 
| 505 |  |     /* We emit frame/scan headers now */ | 
| 506 | 1.12k |     if (master->scan_number == 0) | 
| 507 | 1.12k |       (*cinfo->marker->write_frame_header) (cinfo); | 
| 508 | 1.12k |     (*cinfo->marker->write_scan_header) (cinfo); | 
| 509 | 1.12k |     master->pub.call_pass_startup = FALSE; | 
| 510 | 1.12k |     break; | 
| 511 | 0 |   default: | 
| 512 | 0 |     ERREXIT(cinfo, JERR_NOT_COMPILED); | 
| 513 | 5.40k |   } | 
| 514 |  |  | 
| 515 | 5.40k |   master->pub.is_last_pass = (master->pass_number == master->total_passes - 1); | 
| 516 |  |  | 
| 517 |  |   /* Set up progress monitor's pass info if present */ | 
| 518 | 5.40k |   if (cinfo->progress != NULL) { | 
| 519 | 0 |     cinfo->progress->completed_passes = master->pass_number; | 
| 520 | 0 |     cinfo->progress->total_passes = master->total_passes; | 
| 521 | 0 |   } | 
| 522 | 5.40k | } | 
| 523 |  |  | 
| 524 |  |  | 
| 525 |  | /* | 
| 526 |  |  * Special start-of-pass hook. | 
| 527 |  |  * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. | 
| 528 |  |  * In single-pass processing, we need this hook because we don't want to | 
| 529 |  |  * write frame/scan headers during jpeg_start_compress; we want to let the | 
| 530 |  |  * application write COM markers etc. between jpeg_start_compress and the | 
| 531 |  |  * jpeg_write_scanlines loop. | 
| 532 |  |  * In multi-pass processing, this routine is not used. | 
| 533 |  |  */ | 
| 534 |  |  | 
| 535 |  | METHODDEF(void) | 
| 536 |  | pass_startup(j_compress_ptr cinfo) | 
| 537 | 1.19k | { | 
| 538 | 1.19k |   cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ | 
| 539 |  |  | 
| 540 | 1.19k |   (*cinfo->marker->write_frame_header) (cinfo); | 
| 541 | 1.19k |   (*cinfo->marker->write_scan_header) (cinfo); | 
| 542 | 1.19k | } | 
| 543 |  |  | 
| 544 |  |  | 
| 545 |  | /* | 
| 546 |  |  * Finish up at end of pass. | 
| 547 |  |  */ | 
| 548 |  |  | 
| 549 |  | METHODDEF(void) | 
| 550 |  | finish_pass_master(j_compress_ptr cinfo) | 
| 551 | 3.17k | { | 
| 552 | 3.17k |   my_master_ptr master = (my_master_ptr)cinfo->master; | 
| 553 |  |  | 
| 554 |  |   /* The entropy coder always needs an end-of-pass call, | 
| 555 |  |    * either to analyze statistics or to flush its output buffer. | 
| 556 |  |    */ | 
| 557 | 3.17k |   (*cinfo->entropy->finish_pass) (cinfo); | 
| 558 |  |  | 
| 559 |  |   /* Update state for next pass */ | 
| 560 | 3.17k |   switch (master->pass_type) { | 
| 561 | 2.05k |   case main_pass: | 
| 562 |  |     /* next pass is either output of scan 0 (after optimization) | 
| 563 |  |      * or output of scan 1 (if no optimization). | 
| 564 |  |      */ | 
| 565 | 2.05k |     master->pass_type = output_pass; | 
| 566 | 2.05k |     if (!cinfo->optimize_coding) | 
| 567 | 933 |       master->scan_number++; | 
| 568 | 2.05k |     break; | 
| 569 | 0 |   case huff_opt_pass: | 
| 570 |  |     /* next pass is always output of current scan */ | 
| 571 | 0 |     master->pass_type = output_pass; | 
| 572 | 0 |     break; | 
| 573 | 1.12k |   case output_pass: | 
| 574 |  |     /* next pass is either optimization or output of next scan */ | 
| 575 | 1.12k |     if (cinfo->optimize_coding) | 
| 576 | 1.12k |       master->pass_type = huff_opt_pass; | 
| 577 | 1.12k |     master->scan_number++; | 
| 578 | 1.12k |     break; | 
| 579 | 3.17k |   } | 
| 580 |  |  | 
| 581 | 3.17k |   master->pass_number++; | 
| 582 | 3.17k | } | 
| 583 |  |  | 
| 584 |  |  | 
| 585 |  | /* | 
| 586 |  |  * Initialize master compression control. | 
| 587 |  |  */ | 
| 588 |  |  | 
| 589 |  | GLOBAL(void) | 
| 590 |  | jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only) | 
| 591 | 4.99k | { | 
| 592 | 4.99k |   my_master_ptr master; | 
| 593 |  |  | 
| 594 | 4.99k |   master = (my_master_ptr) | 
| 595 | 4.99k |     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, | 
| 596 | 4.99k |                                 sizeof(my_comp_master)); | 
| 597 | 4.99k |   cinfo->master = (struct jpeg_comp_master *)master; | 
| 598 | 4.99k |   master->pub.prepare_for_pass = prepare_for_pass; | 
| 599 | 4.99k |   master->pub.pass_startup = pass_startup; | 
| 600 | 4.99k |   master->pub.finish_pass = finish_pass_master; | 
| 601 | 4.99k |   master->pub.is_last_pass = FALSE; | 
| 602 |  |  | 
| 603 |  |   /* Validate parameters, determine derived values */ | 
| 604 | 4.99k |   initial_setup(cinfo, transcode_only); | 
| 605 |  |  | 
| 606 | 4.99k |   if (cinfo->scan_info != NULL) { | 
| 607 | 0 | #ifdef C_MULTISCAN_FILES_SUPPORTED | 
| 608 | 0 |     validate_script(cinfo); | 
| 609 |  | #else | 
| 610 |  |     ERREXIT(cinfo, JERR_NOT_COMPILED); | 
| 611 |  | #endif | 
| 612 | 4.99k |   } else { | 
| 613 | 4.99k |     cinfo->progressive_mode = FALSE; | 
| 614 | 4.99k |     cinfo->num_scans = 1; | 
| 615 | 4.99k |   } | 
| 616 |  |  | 
| 617 | 4.99k |   if (cinfo->progressive_mode && !cinfo->arith_code)  /*  TEMPORARY HACK ??? */ | 
| 618 | 0 |     cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */ | 
| 619 |  |  | 
| 620 |  |   /* Initialize my private state */ | 
| 621 | 4.99k |   if (transcode_only) { | 
| 622 |  |     /* no main pass in transcoding */ | 
| 623 | 0 |     if (cinfo->optimize_coding) | 
| 624 | 0 |       master->pass_type = huff_opt_pass; | 
| 625 | 0 |     else | 
| 626 | 0 |       master->pass_type = output_pass; | 
| 627 | 4.99k |   } else { | 
| 628 |  |     /* for normal compression, first pass is always this type: */ | 
| 629 | 4.99k |     master->pass_type = main_pass; | 
| 630 | 4.99k |   } | 
| 631 | 4.99k |   master->scan_number = 0; | 
| 632 | 4.99k |   master->pass_number = 0; | 
| 633 | 4.99k |   if (cinfo->optimize_coding) | 
| 634 | 2.47k |     master->total_passes = cinfo->num_scans * 2; | 
| 635 | 2.52k |   else | 
| 636 | 2.52k |     master->total_passes = cinfo->num_scans; | 
| 637 |  |  | 
| 638 | 4.99k |   master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")"; | 
| 639 | 4.99k | } |