1 /* 2 * jcmaster.c 3 * 4 * Copyright (C) 1991-1997, Thomas G. Lane. 5 * Modified 2003-2009 by Guido Vollbeding. 6 * This file is part of the Independent JPEG Group's software. 7 * For conditions of distribution and use, see the accompanying README file. 8 * 9 * This file contains master control logic for the JPEG compressor. 10 * These routines are concerned with parameter validation, initial setup, 11 * and inter-pass control (determining the number of passes and the work 12 * to be done in each pass). 13 */ 14 15 #define JPEG_INTERNALS 16 #include "jinclude.h" 17 #include "jpeglib.h" 18 19 20 /* Private state */ 21 22 typedef enum { 23 main_pass, /* input data, also do first output step */ 24 huff_opt_pass, /* Huffman code optimization pass */ 25 output_pass /* data output pass */ 26 } c_pass_type; 27 28 typedef struct { 29 struct jpeg_comp_master pub; /* public fields */ 30 31 c_pass_type pass_type; /* the type of the current pass */ 32 33 int pass_number; /* # of passes completed */ 34 int total_passes; /* total # of passes needed */ 35 36 int scan_number; /* current index in scan_info[] */ 37 } my_comp_master; 38 39 typedef my_comp_master * my_master_ptr; 40 41 42 /* 43 * Support routines that do various essential calculations. 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 = DCTSIZE; 114 cinfo->min_DCT_v_scaled_size = DCTSIZE; 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 192 193 LOCAL(void) 194 initial_setup (j_compress_ptr cinfo) 195 /* Do computations that are needed before master selection phase */ 196 { 197 int ci, ssize; 198 jpeg_component_info *compptr; 199 long samplesperrow; 200 JDIMENSION jd_samplesperrow; 201 202 jpeg_calc_jpeg_dimensions(cinfo); 203 204 /* Sanity check on image dimensions */ 205 if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 206 || cinfo->num_components <= 0 || cinfo->input_components <= 0) 207 ERREXIT(cinfo, JERR_EMPTY_IMAGE); 208 209 /* Make sure image isn't bigger than I can handle */ 210 if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION || 211 (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION) 212 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); 213 214 /* Width of an input scanline must be representable as JDIMENSION. */ 215 samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; 216 jd_samplesperrow = (JDIMENSION) samplesperrow; 217 if ((long) jd_samplesperrow != samplesperrow) 218 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); 219 220 /* For now, precision must match compiled-in value... */ 221 if (cinfo->data_precision != BITS_IN_JSAMPLE) 222 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); 223 224 /* Check that number of components won't exceed internal array sizes */ 225 if (cinfo->num_components > MAX_COMPONENTS) 226 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 227 MAX_COMPONENTS); 228 229 /* Compute maximum sampling factors; check factor validity */ 230 cinfo->max_h_samp_factor = 1; 231 cinfo->max_v_samp_factor = 1; 232 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 233 ci++, compptr++) { 234 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || 235 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) 236 ERREXIT(cinfo, JERR_BAD_SAMPLING); 237 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, 238 compptr->h_samp_factor); 239 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, 240 compptr->v_samp_factor); 241 } 261 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; 262 ssize = 1; 263 #ifdef DCT_SCALING_SUPPORTED 264 while (cinfo->min_DCT_v_scaled_size * ssize <= 265 (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && 266 (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { 267 ssize = ssize * 2; 268 } 269 #endif 270 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; 271 272 /* We don't support DCT ratios larger than 2. */ 273 if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2) 274 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2; 275 else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2) 276 compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2; 277 278 /* Size in DCT blocks */ 279 compptr->width_in_blocks = (JDIMENSION) 280 jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor, 281 (long) (cinfo->max_h_samp_factor * DCTSIZE)); 282 compptr->height_in_blocks = (JDIMENSION) 283 jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor, 284 (long) (cinfo->max_v_samp_factor * DCTSIZE)); 285 /* Size in samples */ 286 compptr->downsampled_width = (JDIMENSION) 287 jdiv_round_up((long) cinfo->jpeg_width * 288 (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size), 289 (long) (cinfo->max_h_samp_factor * DCTSIZE)); 290 compptr->downsampled_height = (JDIMENSION) 291 jdiv_round_up((long) cinfo->jpeg_height * 292 (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size), 293 (long) (cinfo->max_v_samp_factor * DCTSIZE)); 294 /* Mark component needed (this flag isn't actually used for compression) */ 295 compptr->component_needed = TRUE; 296 } 297 298 /* Compute number of fully interleaved MCU rows (number of times that 299 * main controller will call coefficient controller). 300 */ 301 cinfo->total_iMCU_rows = (JDIMENSION) 302 jdiv_round_up((long) cinfo->jpeg_height, 303 (long) (cinfo->max_v_samp_factor*DCTSIZE)); 304 } 305 306 307 #ifdef C_MULTISCAN_FILES_SUPPORTED 308 309 LOCAL(void) 310 validate_script (j_compress_ptr cinfo) 311 /* Verify that the scan script in cinfo->scan_info[] is valid; also 312 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. 313 */ 314 { 315 const jpeg_scan_info * scanptr; 316 int scanno, ncomps, ci, coefi, thisi; 317 int Ss, Se, Ah, Al; 318 boolean component_sent[MAX_COMPONENTS]; 319 #ifdef C_PROGRESSIVE_SUPPORTED 320 int * last_bitpos_ptr; 321 int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; 322 /* -1 until that coefficient has been seen; then last Al for it */ 323 MEMZERO(last_bitpos, SIZEOF(last_bitpos)); 324 #endif 325 326 MEMZERO(component_sent, SIZEOF(component_sent)); 327 328 if (cinfo->num_scans <= 0) 329 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); 330 331 /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; 332 * for progressive JPEG, no scan can have this. 333 */ 334 scanptr = cinfo->scan_info; 335 if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { 336 #ifdef C_PROGRESSIVE_SUPPORTED 337 cinfo->progressive_mode = TRUE; 338 last_bitpos_ptr = & last_bitpos[0][0]; 339 for (ci = 0; ci < cinfo->num_components; ci++) 340 for (coefi = 0; coefi < DCTSIZE2; coefi++) 341 *last_bitpos_ptr++ = -1; 342 #else 343 ERREXIT(cinfo, JERR_NOT_COMPILED); 344 #endif 345 } else { 346 cinfo->progressive_mode = FALSE; 347 for (ci = 0; ci < cinfo->num_components; ci++) 358 if (thisi < 0 || thisi >= cinfo->num_components) 359 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 360 /* Components must appear in SOF order within each scan */ 361 if (ci > 0 && thisi <= scanptr->component_index[ci-1]) 362 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 363 } 364 /* Validate progression parameters */ 365 Ss = scanptr->Ss; 366 Se = scanptr->Se; 367 Ah = scanptr->Ah; 368 Al = scanptr->Al; 369 if (cinfo->progressive_mode) { 370 #ifdef C_PROGRESSIVE_SUPPORTED 371 /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that 372 * seems wrong: the upper bound ought to depend on data precision. 373 * Perhaps they really meant 0..N+1 for N-bit precision. 374 * Here we allow 0..10 for 8-bit data; Al larger than 10 results in 375 * out-of-range reconstructed DC values during the first DC scan, 376 * which might cause problems for some decoders. 377 */ 378 #if BITS_IN_JSAMPLE == 8 379 #define MAX_AH_AL 10 380 #else 381 #define MAX_AH_AL 13 382 #endif 383 if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || 384 Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) 385 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 386 if (Ss == 0) { 387 if (Se != 0) /* DC and AC together not OK */ 388 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 389 } else { 390 if (ncomps != 1) /* AC scans must be for only one component */ 391 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 392 } 393 for (ci = 0; ci < ncomps; ci++) { 394 last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; 395 if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ 396 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 397 for (coefi = Ss; coefi <= Se; coefi++) { 398 if (last_bitpos_ptr[coefi] < 0) { 399 /* first scan of this coefficient */ 400 if (Ah != 0) 401 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 402 } else { 403 /* not first scan */ 404 if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) 426 if (cinfo->progressive_mode) { 427 #ifdef C_PROGRESSIVE_SUPPORTED 428 /* For progressive mode, we only check that at least some DC data 429 * got sent for each component; the spec does not require that all bits 430 * of all coefficients be transmitted. Would it be wiser to enforce 431 * transmission of all coefficient bits?? 432 */ 433 for (ci = 0; ci < cinfo->num_components; ci++) { 434 if (last_bitpos[ci][0] < 0) 435 ERREXIT(cinfo, JERR_MISSING_DATA); 436 } 437 #endif 438 } else { 439 for (ci = 0; ci < cinfo->num_components; ci++) { 440 if (! component_sent[ci]) 441 ERREXIT(cinfo, JERR_MISSING_DATA); 442 } 443 } 444 } 445 446 #endif /* C_MULTISCAN_FILES_SUPPORTED */ 447 448 449 LOCAL(void) 450 select_scan_parameters (j_compress_ptr cinfo) 451 /* Set up the scan parameters for the current scan */ 452 { 453 int ci; 454 455 #ifdef C_MULTISCAN_FILES_SUPPORTED 456 if (cinfo->scan_info != NULL) { 457 /* Prepare for current scan --- the script is already validated */ 458 my_master_ptr master = (my_master_ptr) cinfo->master; 459 const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number; 460 461 cinfo->comps_in_scan = scanptr->comps_in_scan; 462 for (ci = 0; ci < scanptr->comps_in_scan; ci++) { 463 cinfo->cur_comp_info[ci] = 464 &cinfo->comp_info[scanptr->component_index[ci]]; 465 } 466 cinfo->Ss = scanptr->Ss; 467 cinfo->Se = scanptr->Se; 468 cinfo->Ah = scanptr->Ah; 469 cinfo->Al = scanptr->Al; 470 } 471 else 472 #endif 473 { 474 /* Prepare for single sequential-JPEG scan containing all components */ 475 if (cinfo->num_components > MAX_COMPS_IN_SCAN) 476 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 477 MAX_COMPS_IN_SCAN); 478 cinfo->comps_in_scan = cinfo->num_components; 479 for (ci = 0; ci < cinfo->num_components; ci++) { 480 cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; 481 } 482 cinfo->Ss = 0; 483 cinfo->Se = DCTSIZE2-1; 484 cinfo->Ah = 0; 485 cinfo->Al = 0; 486 } 487 } 488 489 490 LOCAL(void) 491 per_scan_setup (j_compress_ptr cinfo) 492 /* Do computations that are needed before processing a JPEG scan */ 493 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ 494 { 495 int ci, mcublks, tmp; 496 jpeg_component_info *compptr; 497 498 if (cinfo->comps_in_scan == 1) { 499 500 /* Noninterleaved (single-component) scan */ 501 compptr = cinfo->cur_comp_info[0]; 502 503 /* Overall image size in MCUs */ 504 cinfo->MCUs_per_row = compptr->width_in_blocks; 505 cinfo->MCU_rows_in_scan = compptr->height_in_blocks; 506 514 * as the number of block rows present in the last iMCU row. 515 */ 516 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 517 if (tmp == 0) tmp = compptr->v_samp_factor; 518 compptr->last_row_height = tmp; 519 520 /* Prepare array describing MCU composition */ 521 cinfo->blocks_in_MCU = 1; 522 cinfo->MCU_membership[0] = 0; 523 524 } else { 525 526 /* Interleaved (multi-component) scan */ 527 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) 528 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, 529 MAX_COMPS_IN_SCAN); 530 531 /* Overall image size in MCUs */ 532 cinfo->MCUs_per_row = (JDIMENSION) 533 jdiv_round_up((long) cinfo->jpeg_width, 534 (long) (cinfo->max_h_samp_factor*DCTSIZE)); 535 cinfo->MCU_rows_in_scan = (JDIMENSION) 536 jdiv_round_up((long) cinfo->jpeg_height, 537 (long) (cinfo->max_v_samp_factor*DCTSIZE)); 538 539 cinfo->blocks_in_MCU = 0; 540 541 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 542 compptr = cinfo->cur_comp_info[ci]; 543 /* Sampling factors give # of blocks of component in each MCU */ 544 compptr->MCU_width = compptr->h_samp_factor; 545 compptr->MCU_height = compptr->v_samp_factor; 546 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; 547 compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size; 548 /* Figure number of non-dummy blocks in last MCU column & row */ 549 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); 550 if (tmp == 0) tmp = compptr->MCU_width; 551 compptr->last_col_width = tmp; 552 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); 553 if (tmp == 0) tmp = compptr->MCU_height; 554 compptr->last_row_height = tmp; 555 /* Prepare array describing MCU composition */ 556 mcublks = compptr->MCU_blocks; 557 if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) 713 master->scan_number++; 714 break; 715 } 716 717 master->pass_number++; 718 } 719 720 721 /* 722 * Initialize master compression control. 723 */ 724 725 GLOBAL(void) 726 jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) 727 { 728 my_master_ptr master; 729 730 master = (my_master_ptr) 731 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 732 SIZEOF(my_comp_master)); 733 cinfo->master = (struct jpeg_comp_master *) master; 734 master->pub.prepare_for_pass = prepare_for_pass; 735 master->pub.pass_startup = pass_startup; 736 master->pub.finish_pass = finish_pass_master; 737 master->pub.is_last_pass = FALSE; 738 739 /* Validate parameters, determine derived values */ 740 initial_setup(cinfo); 741 742 if (cinfo->scan_info != NULL) { 743 #ifdef C_MULTISCAN_FILES_SUPPORTED 744 validate_script(cinfo); 745 #else 746 ERREXIT(cinfo, JERR_NOT_COMPILED); 747 #endif 748 } else { 749 cinfo->progressive_mode = FALSE; 750 cinfo->num_scans = 1; 751 } 752 753 if (cinfo->progressive_mode && cinfo->arith_code == 0) /* TEMPORARY HACK ??? */ 754 cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */ 755 756 /* Initialize my private state */ 757 if (transcode_only) { 758 /* no main pass in transcoding */ 759 if (cinfo->optimize_coding) 760 master->pass_type = huff_opt_pass; 761 else 762 master->pass_type = output_pass; 763 } else { 764 /* for normal compression, first pass is always this type: */ 765 master->pass_type = main_pass; 766 } 767 master->scan_number = 0; 768 master->pass_number = 0; 769 if (cinfo->optimize_coding) 770 master->total_passes = cinfo->num_scans * 2; 771 else 772 master->total_passes = cinfo->num_scans; 773 } | 1 /* 2 * jcmaster.c 3 * 4 * Copyright (C) 1991-1997, Thomas G. Lane. 5 * Modified 2003-2017 by Guido Vollbeding. 6 * This file is part of the Independent JPEG Group's software. 7 * For conditions of distribution and use, see the accompanying README file. 8 * 9 * This file contains master control logic for the JPEG compressor. 10 * These routines are concerned with parameter validation, initial setup, 11 * and inter-pass control (determining the number of passes and the work 12 * to be done in each pass). 13 */ 14 15 #define JPEG_INTERNALS 16 #include "jinclude.h" 17 #include "jpeglib.h" 18 19 20 /* Private state */ 21 22 typedef enum { 23 main_pass, /* input data, also do first output step */ 24 huff_opt_pass, /* Huffman code optimization pass */ 25 output_pass /* data output pass */ 26 } c_pass_type; 27 28 typedef struct { 29 struct jpeg_comp_master pub; /* public fields */ 30 31 c_pass_type pass_type; /* the type of the current pass */ 32 33 int pass_number; /* # of passes completed */ 34 int total_passes; /* total # of passes needed */ 35 36 int scan_number; /* current index in scan_info[] */ 37 } my_comp_master; 38 39 typedef my_comp_master * my_master_ptr; 40 41 42 /* 43 * Support routines that do various essential calculations. 44 */ 45 46 LOCAL(void) 47 initial_setup (j_compress_ptr cinfo) 48 /* Do computations that are needed before master selection phase */ 49 { 50 int ci, ssize; 51 jpeg_component_info *compptr; 52 53 /* Sanity check on block_size */ 54 if (cinfo->block_size < 1 || cinfo->block_size > 16) 55 ERREXIT2(cinfo, JERR_BAD_DCTSIZE, cinfo->block_size, cinfo->block_size); 56 57 /* Derive natural_order from block_size */ 58 switch (cinfo->block_size) { 59 case 2: cinfo->natural_order = jpeg_natural_order2; break; 60 case 3: cinfo->natural_order = jpeg_natural_order3; break; 61 case 4: cinfo->natural_order = jpeg_natural_order4; break; 62 case 5: cinfo->natural_order = jpeg_natural_order5; break; 63 case 6: cinfo->natural_order = jpeg_natural_order6; break; 64 case 7: cinfo->natural_order = jpeg_natural_order7; break; 65 default: cinfo->natural_order = jpeg_natural_order; break; 66 } 67 68 /* Derive lim_Se from block_size */ 69 cinfo->lim_Se = cinfo->block_size < DCTSIZE ? 70 cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1; 71 72 /* Sanity check on image dimensions */ 73 if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 || 74 cinfo->num_components <= 0) 75 ERREXIT(cinfo, JERR_EMPTY_IMAGE); 76 77 /* Make sure image isn't bigger than I can handle */ 78 if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION || 79 (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION) 80 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); 81 82 /* Only 8 to 12 bits data precision are supported for DCT based JPEG */ 83 if (cinfo->data_precision < 8 || cinfo->data_precision > 12) 84 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); 85 86 /* Check that number of components won't exceed internal array sizes */ 87 if (cinfo->num_components > MAX_COMPONENTS) 88 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 89 MAX_COMPONENTS); 90 91 /* Compute maximum sampling factors; check factor validity */ 92 cinfo->max_h_samp_factor = 1; 93 cinfo->max_v_samp_factor = 1; 94 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 95 ci++, compptr++) { 96 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || 97 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) 98 ERREXIT(cinfo, JERR_BAD_SAMPLING); 99 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, 100 compptr->h_samp_factor); 101 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, 102 compptr->v_samp_factor); 103 } 123 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; 124 ssize = 1; 125 #ifdef DCT_SCALING_SUPPORTED 126 while (cinfo->min_DCT_v_scaled_size * ssize <= 127 (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && 128 (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { 129 ssize = ssize * 2; 130 } 131 #endif 132 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; 133 134 /* We don't support DCT ratios larger than 2. */ 135 if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2) 136 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2; 137 else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2) 138 compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2; 139 140 /* Size in DCT blocks */ 141 compptr->width_in_blocks = (JDIMENSION) 142 jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor, 143 (long) (cinfo->max_h_samp_factor * cinfo->block_size)); 144 compptr->height_in_blocks = (JDIMENSION) 145 jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor, 146 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); 147 /* Size in samples */ 148 compptr->downsampled_width = (JDIMENSION) 149 jdiv_round_up((long) cinfo->jpeg_width * 150 (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size), 151 (long) (cinfo->max_h_samp_factor * cinfo->block_size)); 152 compptr->downsampled_height = (JDIMENSION) 153 jdiv_round_up((long) cinfo->jpeg_height * 154 (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size), 155 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); 156 /* Don't need quantization scale after DCT, 157 * until color conversion says otherwise. 158 */ 159 compptr->component_needed = FALSE; 160 } 161 162 /* Compute number of fully interleaved MCU rows (number of times that 163 * main controller will call coefficient controller). 164 */ 165 cinfo->total_iMCU_rows = (JDIMENSION) 166 jdiv_round_up((long) cinfo->jpeg_height, 167 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); 168 } 169 170 171 #ifdef C_MULTISCAN_FILES_SUPPORTED 172 173 LOCAL(void) 174 validate_script (j_compress_ptr cinfo) 175 /* Verify that the scan script in cinfo->scan_info[] is valid; also 176 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. 177 */ 178 { 179 const jpeg_scan_info * scanptr; 180 int scanno, ncomps, ci, coefi, thisi; 181 int Ss, Se, Ah, Al; 182 boolean component_sent[MAX_COMPONENTS]; 183 #ifdef C_PROGRESSIVE_SUPPORTED 184 int * last_bitpos_ptr; 185 int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; 186 /* -1 until that coefficient has been seen; then last Al for it */ 187 #endif 188 189 if (cinfo->num_scans <= 0) 190 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); 191 192 /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; 193 * for progressive JPEG, no scan can have this. 194 */ 195 scanptr = cinfo->scan_info; 196 if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { 197 #ifdef C_PROGRESSIVE_SUPPORTED 198 cinfo->progressive_mode = TRUE; 199 last_bitpos_ptr = & last_bitpos[0][0]; 200 for (ci = 0; ci < cinfo->num_components; ci++) 201 for (coefi = 0; coefi < DCTSIZE2; coefi++) 202 *last_bitpos_ptr++ = -1; 203 #else 204 ERREXIT(cinfo, JERR_NOT_COMPILED); 205 #endif 206 } else { 207 cinfo->progressive_mode = FALSE; 208 for (ci = 0; ci < cinfo->num_components; ci++) 219 if (thisi < 0 || thisi >= cinfo->num_components) 220 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 221 /* Components must appear in SOF order within each scan */ 222 if (ci > 0 && thisi <= scanptr->component_index[ci-1]) 223 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 224 } 225 /* Validate progression parameters */ 226 Ss = scanptr->Ss; 227 Se = scanptr->Se; 228 Ah = scanptr->Ah; 229 Al = scanptr->Al; 230 if (cinfo->progressive_mode) { 231 #ifdef C_PROGRESSIVE_SUPPORTED 232 /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that 233 * seems wrong: the upper bound ought to depend on data precision. 234 * Perhaps they really meant 0..N+1 for N-bit precision. 235 * Here we allow 0..10 for 8-bit data; Al larger than 10 results in 236 * out-of-range reconstructed DC values during the first DC scan, 237 * which might cause problems for some decoders. 238 */ 239 if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || 240 Ah < 0 || Ah > (cinfo->data_precision > 8 ? 13 : 10) || 241 Al < 0 || Al > (cinfo->data_precision > 8 ? 13 : 10)) 242 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 243 if (Ss == 0) { 244 if (Se != 0) /* DC and AC together not OK */ 245 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 246 } else { 247 if (ncomps != 1) /* AC scans must be for only one component */ 248 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 249 } 250 for (ci = 0; ci < ncomps; ci++) { 251 last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; 252 if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ 253 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 254 for (coefi = Ss; coefi <= Se; coefi++) { 255 if (last_bitpos_ptr[coefi] < 0) { 256 /* first scan of this coefficient */ 257 if (Ah != 0) 258 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 259 } else { 260 /* not first scan */ 261 if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) 283 if (cinfo->progressive_mode) { 284 #ifdef C_PROGRESSIVE_SUPPORTED 285 /* For progressive mode, we only check that at least some DC data 286 * got sent for each component; the spec does not require that all bits 287 * of all coefficients be transmitted. Would it be wiser to enforce 288 * transmission of all coefficient bits?? 289 */ 290 for (ci = 0; ci < cinfo->num_components; ci++) { 291 if (last_bitpos[ci][0] < 0) 292 ERREXIT(cinfo, JERR_MISSING_DATA); 293 } 294 #endif 295 } else { 296 for (ci = 0; ci < cinfo->num_components; ci++) { 297 if (! component_sent[ci]) 298 ERREXIT(cinfo, JERR_MISSING_DATA); 299 } 300 } 301 } 302 303 304 LOCAL(void) 305 reduce_script (j_compress_ptr cinfo) 306 /* Adapt scan script for use with reduced block size; 307 * assume that script has been validated before. 308 */ 309 { 310 jpeg_scan_info * scanptr; 311 int idxout, idxin; 312 313 /* Circumvent const declaration for this function */ 314 scanptr = (jpeg_scan_info *) cinfo->scan_info; 315 idxout = 0; 316 317 for (idxin = 0; idxin < cinfo->num_scans; idxin++) { 318 /* After skipping, idxout becomes smaller than idxin */ 319 if (idxin != idxout) 320 /* Copy rest of data; 321 * note we stay in given chunk of allocated memory. 322 */ 323 scanptr[idxout] = scanptr[idxin]; 324 if (scanptr[idxout].Ss > cinfo->lim_Se) 325 /* Entire scan out of range - skip this entry */ 326 continue; 327 if (scanptr[idxout].Se > cinfo->lim_Se) 328 /* Limit scan to end of block */ 329 scanptr[idxout].Se = cinfo->lim_Se; 330 idxout++; 331 } 332 333 cinfo->num_scans = idxout; 334 } 335 336 #endif /* C_MULTISCAN_FILES_SUPPORTED */ 337 338 339 LOCAL(void) 340 select_scan_parameters (j_compress_ptr cinfo) 341 /* Set up the scan parameters for the current scan */ 342 { 343 int ci; 344 345 #ifdef C_MULTISCAN_FILES_SUPPORTED 346 if (cinfo->scan_info != NULL) { 347 /* Prepare for current scan --- the script is already validated */ 348 my_master_ptr master = (my_master_ptr) cinfo->master; 349 const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number; 350 351 cinfo->comps_in_scan = scanptr->comps_in_scan; 352 for (ci = 0; ci < scanptr->comps_in_scan; ci++) { 353 cinfo->cur_comp_info[ci] = 354 &cinfo->comp_info[scanptr->component_index[ci]]; 355 } 356 if (cinfo->progressive_mode) { 357 cinfo->Ss = scanptr->Ss; 358 cinfo->Se = scanptr->Se; 359 cinfo->Ah = scanptr->Ah; 360 cinfo->Al = scanptr->Al; 361 return; 362 } 363 } 364 else 365 #endif 366 { 367 /* Prepare for single sequential-JPEG scan containing all components */ 368 if (cinfo->num_components > MAX_COMPS_IN_SCAN) 369 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 370 MAX_COMPS_IN_SCAN); 371 cinfo->comps_in_scan = cinfo->num_components; 372 for (ci = 0; ci < cinfo->num_components; ci++) { 373 cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; 374 } 375 } 376 cinfo->Ss = 0; 377 cinfo->Se = cinfo->block_size * cinfo->block_size - 1; 378 cinfo->Ah = 0; 379 cinfo->Al = 0; 380 } 381 382 383 LOCAL(void) 384 per_scan_setup (j_compress_ptr cinfo) 385 /* Do computations that are needed before processing a JPEG scan */ 386 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ 387 { 388 int ci, mcublks, tmp; 389 jpeg_component_info *compptr; 390 391 if (cinfo->comps_in_scan == 1) { 392 393 /* Noninterleaved (single-component) scan */ 394 compptr = cinfo->cur_comp_info[0]; 395 396 /* Overall image size in MCUs */ 397 cinfo->MCUs_per_row = compptr->width_in_blocks; 398 cinfo->MCU_rows_in_scan = compptr->height_in_blocks; 399 407 * as the number of block rows present in the last iMCU row. 408 */ 409 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 410 if (tmp == 0) tmp = compptr->v_samp_factor; 411 compptr->last_row_height = tmp; 412 413 /* Prepare array describing MCU composition */ 414 cinfo->blocks_in_MCU = 1; 415 cinfo->MCU_membership[0] = 0; 416 417 } else { 418 419 /* Interleaved (multi-component) scan */ 420 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) 421 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, 422 MAX_COMPS_IN_SCAN); 423 424 /* Overall image size in MCUs */ 425 cinfo->MCUs_per_row = (JDIMENSION) 426 jdiv_round_up((long) cinfo->jpeg_width, 427 (long) (cinfo->max_h_samp_factor * cinfo->block_size)); 428 cinfo->MCU_rows_in_scan = (JDIMENSION) 429 jdiv_round_up((long) cinfo->jpeg_height, 430 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); 431 432 cinfo->blocks_in_MCU = 0; 433 434 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 435 compptr = cinfo->cur_comp_info[ci]; 436 /* Sampling factors give # of blocks of component in each MCU */ 437 compptr->MCU_width = compptr->h_samp_factor; 438 compptr->MCU_height = compptr->v_samp_factor; 439 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; 440 compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size; 441 /* Figure number of non-dummy blocks in last MCU column & row */ 442 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); 443 if (tmp == 0) tmp = compptr->MCU_width; 444 compptr->last_col_width = tmp; 445 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); 446 if (tmp == 0) tmp = compptr->MCU_height; 447 compptr->last_row_height = tmp; 448 /* Prepare array describing MCU composition */ 449 mcublks = compptr->MCU_blocks; 450 if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) 606 master->scan_number++; 607 break; 608 } 609 610 master->pass_number++; 611 } 612 613 614 /* 615 * Initialize master compression control. 616 */ 617 618 GLOBAL(void) 619 jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) 620 { 621 my_master_ptr master; 622 623 master = (my_master_ptr) 624 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 625 SIZEOF(my_comp_master)); 626 cinfo->master = &master->pub; 627 master->pub.prepare_for_pass = prepare_for_pass; 628 master->pub.pass_startup = pass_startup; 629 master->pub.finish_pass = finish_pass_master; 630 master->pub.is_last_pass = FALSE; 631 632 /* Validate parameters, determine derived values */ 633 initial_setup(cinfo); 634 635 if (cinfo->scan_info != NULL) { 636 #ifdef C_MULTISCAN_FILES_SUPPORTED 637 validate_script(cinfo); 638 if (cinfo->block_size < DCTSIZE) 639 reduce_script(cinfo); 640 #else 641 ERREXIT(cinfo, JERR_NOT_COMPILED); 642 #endif 643 } else { 644 cinfo->progressive_mode = FALSE; 645 cinfo->num_scans = 1; 646 } 647 648 if (cinfo->optimize_coding) 649 cinfo->arith_code = FALSE; /* disable arithmetic coding */ 650 else if (! cinfo->arith_code && 651 (cinfo->progressive_mode || 652 (cinfo->block_size > 1 && cinfo->block_size < DCTSIZE))) 653 /* TEMPORARY HACK ??? */ 654 /* assume default tables no good for progressive or reduced AC mode */ 655 cinfo->optimize_coding = TRUE; /* force Huffman optimization */ 656 657 /* Initialize my private state */ 658 if (transcode_only) { 659 /* no main pass in transcoding */ 660 if (cinfo->optimize_coding) 661 master->pass_type = huff_opt_pass; 662 else 663 master->pass_type = output_pass; 664 } else { 665 /* for normal compression, first pass is always this type: */ 666 master->pass_type = main_pass; 667 } 668 master->scan_number = 0; 669 master->pass_number = 0; 670 if (cinfo->optimize_coding) 671 master->total_passes = cinfo->num_scans * 2; 672 else 673 master->total_passes = cinfo->num_scans; 674 } |