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