jcmaster.c (28025B)
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 * Lossless JPEG Modifications: 8 * Copyright (C) 1999, Ken Murchison. 9 * libjpeg-turbo Modifications: 10 * Copyright (C) 2010, 2016, 2018, 2022-2024, D. R. Commander. 11 * For conditions of distribution and use, see the accompanying README.ijg 12 * file. 13 * 14 * This file contains master control logic for the JPEG compressor. 15 * These routines are concerned with parameter validation, initial setup, 16 * and inter-pass control (determining the number of passes and the work 17 * to be done in each pass). 18 */ 19 20 #define JPEG_INTERNALS 21 #include "jinclude.h" 22 #include "jpeglib.h" 23 #include "jpegapicomp.h" 24 #include "jcmaster.h" 25 26 27 /* 28 * Support routines that do various essential calculations. 29 */ 30 31 #if JPEG_LIB_VERSION >= 70 32 /* 33 * Compute JPEG image dimensions and related values. 34 * NOTE: this is exported for possible use by application. 35 * Hence it mustn't do anything that can't be done twice. 36 */ 37 38 GLOBAL(void) 39 jpeg_calc_jpeg_dimensions(j_compress_ptr cinfo) 40 /* Do computations that are needed before master selection phase */ 41 { 42 int data_unit = cinfo->master->lossless ? 1 : DCTSIZE; 43 44 /* Hardwire it to "no scaling" */ 45 cinfo->jpeg_width = cinfo->image_width; 46 cinfo->jpeg_height = cinfo->image_height; 47 cinfo->min_DCT_h_scaled_size = data_unit; 48 cinfo->min_DCT_v_scaled_size = data_unit; 49 } 50 #endif 51 52 53 LOCAL(boolean) 54 using_std_huff_tables(j_compress_ptr cinfo) 55 { 56 int i; 57 58 static const UINT8 bits_dc_luminance[17] = { 59 /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 60 }; 61 static const UINT8 val_dc_luminance[] = { 62 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 63 }; 64 65 static const UINT8 bits_dc_chrominance[17] = { 66 /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 67 }; 68 static const UINT8 val_dc_chrominance[] = { 69 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 70 }; 71 72 static const UINT8 bits_ac_luminance[17] = { 73 /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d 74 }; 75 static const UINT8 val_ac_luminance[] = { 76 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 77 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 78 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 79 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 80 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 81 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 82 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 83 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 84 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 85 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 86 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 87 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 88 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 89 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 90 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 91 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 92 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 93 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 94 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 95 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 96 0xf9, 0xfa 97 }; 98 99 static const UINT8 bits_ac_chrominance[17] = { 100 /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 101 }; 102 static const UINT8 val_ac_chrominance[] = { 103 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 104 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 105 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 106 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 107 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 108 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 109 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 110 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 111 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 112 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 113 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 114 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 115 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 116 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 117 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 118 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 119 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 120 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 121 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 122 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 123 0xf9, 0xfa 124 }; 125 126 if (cinfo->dc_huff_tbl_ptrs[0] == NULL || 127 cinfo->ac_huff_tbl_ptrs[0] == NULL || 128 cinfo->dc_huff_tbl_ptrs[1] == NULL || 129 cinfo->ac_huff_tbl_ptrs[1] == NULL) 130 return FALSE; 131 132 for (i = 2; i < NUM_HUFF_TBLS; i++) { 133 if (cinfo->dc_huff_tbl_ptrs[i] != NULL || 134 cinfo->ac_huff_tbl_ptrs[i] != NULL) 135 return FALSE; 136 } 137 138 if (memcmp(cinfo->dc_huff_tbl_ptrs[0]->bits, bits_dc_luminance, 139 sizeof(bits_dc_luminance)) || 140 memcmp(cinfo->dc_huff_tbl_ptrs[0]->huffval, val_dc_luminance, 141 sizeof(val_dc_luminance)) || 142 memcmp(cinfo->ac_huff_tbl_ptrs[0]->bits, bits_ac_luminance, 143 sizeof(bits_ac_luminance)) || 144 memcmp(cinfo->ac_huff_tbl_ptrs[0]->huffval, val_ac_luminance, 145 sizeof(val_ac_luminance)) || 146 memcmp(cinfo->dc_huff_tbl_ptrs[1]->bits, bits_dc_chrominance, 147 sizeof(bits_dc_chrominance)) || 148 memcmp(cinfo->dc_huff_tbl_ptrs[1]->huffval, val_dc_chrominance, 149 sizeof(val_dc_chrominance)) || 150 memcmp(cinfo->ac_huff_tbl_ptrs[1]->bits, bits_ac_chrominance, 151 sizeof(bits_ac_chrominance)) || 152 memcmp(cinfo->ac_huff_tbl_ptrs[1]->huffval, val_ac_chrominance, 153 sizeof(val_ac_chrominance))) 154 return FALSE; 155 156 return TRUE; 157 } 158 159 160 LOCAL(void) 161 initial_setup(j_compress_ptr cinfo, boolean transcode_only) 162 /* Do computations that are needed before master selection phase */ 163 { 164 int ci; 165 jpeg_component_info *compptr; 166 long samplesperrow; 167 JDIMENSION jd_samplesperrow; 168 int data_unit = cinfo->master->lossless ? 1 : DCTSIZE; 169 170 #if JPEG_LIB_VERSION >= 70 171 #if JPEG_LIB_VERSION >= 80 172 if (!transcode_only) 173 #endif 174 jpeg_calc_jpeg_dimensions(cinfo); 175 #endif 176 177 /* Sanity check on image dimensions */ 178 if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 || 179 cinfo->num_components <= 0 || cinfo->input_components <= 0) 180 ERREXIT(cinfo, JERR_EMPTY_IMAGE); 181 182 /* Make sure image isn't bigger than I can handle */ 183 if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION || 184 (long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION) 185 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION); 186 187 /* Width of an input scanline must be representable as JDIMENSION. */ 188 samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components; 189 jd_samplesperrow = (JDIMENSION)samplesperrow; 190 if ((long)jd_samplesperrow != samplesperrow) 191 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); 192 193 #ifdef C_LOSSLESS_SUPPORTED 194 if (cinfo->data_precision != 8 && cinfo->data_precision != 12 && 195 cinfo->data_precision != 16) 196 #else 197 if (cinfo->data_precision != 8 && cinfo->data_precision != 12) 198 #endif 199 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); 200 201 /* Check that number of components won't exceed internal array sizes */ 202 if (cinfo->num_components > MAX_COMPONENTS) 203 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 204 MAX_COMPONENTS); 205 206 /* Compute maximum sampling factors; check factor validity */ 207 cinfo->max_h_samp_factor = 1; 208 cinfo->max_v_samp_factor = 1; 209 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 210 ci++, compptr++) { 211 if (compptr->h_samp_factor <= 0 || 212 compptr->h_samp_factor > MAX_SAMP_FACTOR || 213 compptr->v_samp_factor <= 0 || 214 compptr->v_samp_factor > MAX_SAMP_FACTOR) 215 ERREXIT(cinfo, JERR_BAD_SAMPLING); 216 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, 217 compptr->h_samp_factor); 218 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, 219 compptr->v_samp_factor); 220 } 221 222 /* Compute dimensions of components */ 223 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 224 ci++, compptr++) { 225 /* Fill in the correct component_index value; don't rely on application */ 226 compptr->component_index = ci; 227 /* For compression, we never do DCT scaling. */ 228 #if JPEG_LIB_VERSION >= 70 229 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = data_unit; 230 #else 231 compptr->DCT_scaled_size = data_unit; 232 #endif 233 /* Size in data units */ 234 compptr->width_in_blocks = (JDIMENSION) 235 jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor, 236 (long)(cinfo->max_h_samp_factor * data_unit)); 237 compptr->height_in_blocks = (JDIMENSION) 238 jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor, 239 (long)(cinfo->max_v_samp_factor * data_unit)); 240 /* Size in samples */ 241 compptr->downsampled_width = (JDIMENSION) 242 jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor, 243 (long)cinfo->max_h_samp_factor); 244 compptr->downsampled_height = (JDIMENSION) 245 jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor, 246 (long)cinfo->max_v_samp_factor); 247 /* Mark component needed (this flag isn't actually used for compression) */ 248 compptr->component_needed = TRUE; 249 } 250 251 /* Compute number of fully interleaved MCU rows (number of times that 252 * main controller will call coefficient or difference controller). 253 */ 254 cinfo->total_iMCU_rows = (JDIMENSION) 255 jdiv_round_up((long)cinfo->_jpeg_height, 256 (long)(cinfo->max_v_samp_factor * data_unit)); 257 } 258 259 260 #if defined(C_MULTISCAN_FILES_SUPPORTED) || defined(C_LOSSLESS_SUPPORTED) 261 #define NEED_SCAN_SCRIPT 262 #endif 263 264 #ifdef NEED_SCAN_SCRIPT 265 266 LOCAL(void) 267 validate_script(j_compress_ptr cinfo) 268 /* Verify that the scan script in cinfo->scan_info[] is valid; also 269 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. 270 */ 271 { 272 const jpeg_scan_info *scanptr; 273 int scanno, ncomps, ci, coefi, thisi; 274 int Ss, Se, Ah, Al; 275 boolean component_sent[MAX_COMPONENTS]; 276 #ifdef C_PROGRESSIVE_SUPPORTED 277 int *last_bitpos_ptr; 278 int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; 279 /* -1 until that coefficient has been seen; then last Al for it */ 280 #endif 281 282 if (cinfo->num_scans <= 0) 283 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); 284 285 #ifndef C_MULTISCAN_FILES_SUPPORTED 286 if (cinfo->num_scans > 1) 287 ERREXIT(cinfo, JERR_NOT_COMPILED); 288 #endif 289 290 scanptr = cinfo->scan_info; 291 if (scanptr->Ss != 0 && scanptr->Se == 0) { 292 #ifdef C_LOSSLESS_SUPPORTED 293 cinfo->master->lossless = TRUE; 294 cinfo->progressive_mode = FALSE; 295 for (ci = 0; ci < cinfo->num_components; ci++) 296 component_sent[ci] = FALSE; 297 #else 298 ERREXIT(cinfo, JERR_NOT_COMPILED); 299 #endif 300 } 301 /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; 302 * for progressive JPEG, no scan can have this. 303 */ 304 else if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) { 305 #ifdef C_PROGRESSIVE_SUPPORTED 306 cinfo->progressive_mode = TRUE; 307 cinfo->master->lossless = FALSE; 308 last_bitpos_ptr = &last_bitpos[0][0]; 309 for (ci = 0; ci < cinfo->num_components; ci++) 310 for (coefi = 0; coefi < DCTSIZE2; coefi++) 311 *last_bitpos_ptr++ = -1; 312 #else 313 ERREXIT(cinfo, JERR_NOT_COMPILED); 314 #endif 315 } else { 316 cinfo->progressive_mode = cinfo->master->lossless = FALSE; 317 for (ci = 0; ci < cinfo->num_components; ci++) 318 component_sent[ci] = FALSE; 319 } 320 321 for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { 322 /* Validate component indexes */ 323 ncomps = scanptr->comps_in_scan; 324 if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) 325 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); 326 for (ci = 0; ci < ncomps; ci++) { 327 thisi = scanptr->component_index[ci]; 328 if (thisi < 0 || thisi >= cinfo->num_components) 329 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 330 /* Components must appear in SOF order within each scan */ 331 if (ci > 0 && thisi <= scanptr->component_index[ci - 1]) 332 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 333 } 334 /* Validate progression parameters */ 335 Ss = scanptr->Ss; 336 Se = scanptr->Se; 337 Ah = scanptr->Ah; 338 Al = scanptr->Al; 339 if (cinfo->progressive_mode) { 340 #ifdef C_PROGRESSIVE_SUPPORTED 341 /* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah 342 * and Al, but that seems wrong: the upper bound ought to depend on data 343 * precision. Perhaps they really meant 0..N+1 for N-bit precision. 344 * Here we allow 0..10 for 8-bit data; Al larger than 10 results in 345 * out-of-range reconstructed DC values during the first DC scan, 346 * which might cause problems for some decoders. 347 */ 348 int max_Ah_Al = cinfo->data_precision == 12 ? 13 : 10; 349 350 if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || 351 Ah < 0 || Ah > max_Ah_Al || Al < 0 || Al > max_Ah_Al) 352 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 353 if (Ss == 0) { 354 if (Se != 0) /* DC and AC together not OK */ 355 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 356 } else { 357 if (ncomps != 1) /* AC scans must be for only one component */ 358 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 359 } 360 for (ci = 0; ci < ncomps; ci++) { 361 last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0]; 362 if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ 363 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 364 for (coefi = Ss; coefi <= Se; coefi++) { 365 if (last_bitpos_ptr[coefi] < 0) { 366 /* first scan of this coefficient */ 367 if (Ah != 0) 368 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 369 } else { 370 /* not first scan */ 371 if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1) 372 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 373 } 374 last_bitpos_ptr[coefi] = Al; 375 } 376 } 377 #endif 378 } else { 379 #ifdef C_LOSSLESS_SUPPORTED 380 if (cinfo->master->lossless) { 381 /* The JPEG spec simply gives the range 0..15 for Al (Pt), but that 382 * seems wrong: the upper bound ought to depend on data precision. 383 * Perhaps they really meant 0..N-1 for N-bit precision, which is what 384 * we allow here. Values greater than or equal to the data precision 385 * will result in a blank image. 386 */ 387 if (Ss < 1 || Ss > 7 || /* predictor selection value */ 388 Se != 0 || Ah != 0 || 389 Al < 0 || Al >= cinfo->data_precision) /* point transform */ 390 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 391 } else 392 #endif 393 { 394 /* For sequential JPEG, all progression parameters must be these: */ 395 if (Ss != 0 || Se != DCTSIZE2 - 1 || Ah != 0 || Al != 0) 396 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 397 } 398 /* Make sure components are not sent twice */ 399 for (ci = 0; ci < ncomps; ci++) { 400 thisi = scanptr->component_index[ci]; 401 if (component_sent[thisi]) 402 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 403 component_sent[thisi] = TRUE; 404 } 405 } 406 } 407 408 /* Now verify that everything got sent. */ 409 if (cinfo->progressive_mode) { 410 #ifdef C_PROGRESSIVE_SUPPORTED 411 /* For progressive mode, we only check that at least some DC data 412 * got sent for each component; the spec does not require that all bits 413 * of all coefficients be transmitted. Would it be wiser to enforce 414 * transmission of all coefficient bits?? 415 */ 416 for (ci = 0; ci < cinfo->num_components; ci++) { 417 if (last_bitpos[ci][0] < 0) 418 ERREXIT(cinfo, JERR_MISSING_DATA); 419 } 420 #endif 421 } else { 422 for (ci = 0; ci < cinfo->num_components; ci++) { 423 if (!component_sent[ci]) 424 ERREXIT(cinfo, JERR_MISSING_DATA); 425 } 426 } 427 } 428 429 #endif /* NEED_SCAN_SCRIPT */ 430 431 432 LOCAL(void) 433 select_scan_parameters(j_compress_ptr cinfo) 434 /* Set up the scan parameters for the current scan */ 435 { 436 int ci; 437 438 #ifdef NEED_SCAN_SCRIPT 439 if (cinfo->scan_info != NULL) { 440 /* Prepare for current scan --- the script is already validated */ 441 my_master_ptr master = (my_master_ptr)cinfo->master; 442 const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number; 443 444 cinfo->comps_in_scan = scanptr->comps_in_scan; 445 for (ci = 0; ci < scanptr->comps_in_scan; ci++) { 446 cinfo->cur_comp_info[ci] = 447 &cinfo->comp_info[scanptr->component_index[ci]]; 448 } 449 cinfo->Ss = scanptr->Ss; 450 cinfo->Se = scanptr->Se; 451 cinfo->Ah = scanptr->Ah; 452 cinfo->Al = scanptr->Al; 453 } else 454 #endif 455 { 456 /* Prepare for single sequential-JPEG scan containing all components */ 457 if (cinfo->num_components > MAX_COMPS_IN_SCAN) 458 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 459 MAX_COMPS_IN_SCAN); 460 cinfo->comps_in_scan = cinfo->num_components; 461 for (ci = 0; ci < cinfo->num_components; ci++) { 462 cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; 463 } 464 if (!cinfo->master->lossless) { 465 cinfo->Ss = 0; 466 cinfo->Se = DCTSIZE2 - 1; 467 cinfo->Ah = 0; 468 cinfo->Al = 0; 469 } 470 } 471 } 472 473 474 LOCAL(void) 475 per_scan_setup(j_compress_ptr cinfo) 476 /* Do computations that are needed before processing a JPEG scan */ 477 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ 478 { 479 int ci, mcublks, tmp; 480 jpeg_component_info *compptr; 481 int data_unit = cinfo->master->lossless ? 1 : DCTSIZE; 482 483 if (cinfo->comps_in_scan == 1) { 484 485 /* Noninterleaved (single-component) scan */ 486 compptr = cinfo->cur_comp_info[0]; 487 488 /* Overall image size in MCUs */ 489 cinfo->MCUs_per_row = compptr->width_in_blocks; 490 cinfo->MCU_rows_in_scan = compptr->height_in_blocks; 491 492 /* For noninterleaved scan, always one block per MCU */ 493 compptr->MCU_width = 1; 494 compptr->MCU_height = 1; 495 compptr->MCU_blocks = 1; 496 compptr->MCU_sample_width = data_unit; 497 compptr->last_col_width = 1; 498 /* For noninterleaved scans, it is convenient to define last_row_height 499 * as the number of block rows present in the last iMCU row. 500 */ 501 tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor); 502 if (tmp == 0) tmp = compptr->v_samp_factor; 503 compptr->last_row_height = tmp; 504 505 /* Prepare array describing MCU composition */ 506 cinfo->blocks_in_MCU = 1; 507 cinfo->MCU_membership[0] = 0; 508 509 } else { 510 511 /* Interleaved (multi-component) scan */ 512 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) 513 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, 514 MAX_COMPS_IN_SCAN); 515 516 /* Overall image size in MCUs */ 517 cinfo->MCUs_per_row = (JDIMENSION) 518 jdiv_round_up((long)cinfo->_jpeg_width, 519 (long)(cinfo->max_h_samp_factor * data_unit)); 520 cinfo->MCU_rows_in_scan = (JDIMENSION) 521 jdiv_round_up((long)cinfo->_jpeg_height, 522 (long)(cinfo->max_v_samp_factor * data_unit)); 523 524 cinfo->blocks_in_MCU = 0; 525 526 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 527 compptr = cinfo->cur_comp_info[ci]; 528 /* Sampling factors give # of blocks of component in each MCU */ 529 compptr->MCU_width = compptr->h_samp_factor; 530 compptr->MCU_height = compptr->v_samp_factor; 531 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; 532 compptr->MCU_sample_width = compptr->MCU_width * data_unit; 533 /* Figure number of non-dummy blocks in last MCU column & row */ 534 tmp = (int)(compptr->width_in_blocks % compptr->MCU_width); 535 if (tmp == 0) tmp = compptr->MCU_width; 536 compptr->last_col_width = tmp; 537 tmp = (int)(compptr->height_in_blocks % compptr->MCU_height); 538 if (tmp == 0) tmp = compptr->MCU_height; 539 compptr->last_row_height = tmp; 540 /* Prepare array describing MCU composition */ 541 mcublks = compptr->MCU_blocks; 542 if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) 543 ERREXIT(cinfo, JERR_BAD_MCU_SIZE); 544 while (mcublks-- > 0) { 545 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; 546 } 547 } 548 549 } 550 551 /* Convert restart specified in rows to actual MCU count. */ 552 /* Note that count must fit in 16 bits, so we provide limiting. */ 553 if (cinfo->restart_in_rows > 0) { 554 long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row; 555 cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L); 556 } 557 } 558 559 560 /* 561 * Per-pass setup. 562 * This is called at the beginning of each pass. We determine which modules 563 * will be active during this pass and give them appropriate start_pass calls. 564 * We also set is_last_pass to indicate whether any more passes will be 565 * required. 566 */ 567 568 METHODDEF(void) 569 prepare_for_pass(j_compress_ptr cinfo) 570 { 571 my_master_ptr master = (my_master_ptr)cinfo->master; 572 573 switch (master->pass_type) { 574 case main_pass: 575 /* Initial pass: will collect input data, and do either Huffman 576 * optimization or data output for the first scan. 577 */ 578 select_scan_parameters(cinfo); 579 per_scan_setup(cinfo); 580 if (!cinfo->raw_data_in) { 581 (*cinfo->cconvert->start_pass) (cinfo); 582 (*cinfo->downsample->start_pass) (cinfo); 583 (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); 584 } 585 (*cinfo->fdct->start_pass) (cinfo); 586 (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); 587 (*cinfo->coef->start_pass) (cinfo, 588 (master->total_passes > 1 ? 589 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); 590 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); 591 if (cinfo->optimize_coding) { 592 /* No immediate data output; postpone writing frame/scan headers */ 593 master->pub.call_pass_startup = FALSE; 594 } else { 595 /* Will write frame/scan headers at first jpeg_write_scanlines call */ 596 master->pub.call_pass_startup = TRUE; 597 } 598 break; 599 #ifdef ENTROPY_OPT_SUPPORTED 600 case huff_opt_pass: 601 /* Do Huffman optimization for a scan after the first one. */ 602 select_scan_parameters(cinfo); 603 per_scan_setup(cinfo); 604 if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code || 605 cinfo->master->lossless) { 606 (*cinfo->entropy->start_pass) (cinfo, TRUE); 607 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); 608 master->pub.call_pass_startup = FALSE; 609 break; 610 } 611 /* Special case: Huffman DC refinement scans need no Huffman table 612 * and therefore we can skip the optimization pass for them. 613 */ 614 master->pass_type = output_pass; 615 master->pass_number++; 616 #endif 617 FALLTHROUGH /*FALLTHROUGH*/ 618 case output_pass: 619 /* Do a data-output pass. */ 620 /* We need not repeat per-scan setup if prior optimization pass did it. */ 621 if (!cinfo->optimize_coding) { 622 select_scan_parameters(cinfo); 623 per_scan_setup(cinfo); 624 } 625 (*cinfo->entropy->start_pass) (cinfo, FALSE); 626 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); 627 /* We emit frame/scan headers now */ 628 if (master->scan_number == 0) 629 (*cinfo->marker->write_frame_header) (cinfo); 630 (*cinfo->marker->write_scan_header) (cinfo); 631 master->pub.call_pass_startup = FALSE; 632 break; 633 default: 634 ERREXIT(cinfo, JERR_NOT_COMPILED); 635 } 636 637 master->pub.is_last_pass = (master->pass_number == master->total_passes - 1); 638 639 /* Set up progress monitor's pass info if present */ 640 if (cinfo->progress != NULL) { 641 cinfo->progress->completed_passes = master->pass_number; 642 cinfo->progress->total_passes = master->total_passes; 643 } 644 } 645 646 647 /* 648 * Special start-of-pass hook. 649 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. 650 * In single-pass processing, we need this hook because we don't want to 651 * write frame/scan headers during jpeg_start_compress; we want to let the 652 * application write COM markers etc. between jpeg_start_compress and the 653 * jpeg_write_scanlines loop. 654 * In multi-pass processing, this routine is not used. 655 */ 656 657 METHODDEF(void) 658 pass_startup(j_compress_ptr cinfo) 659 { 660 cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ 661 662 (*cinfo->marker->write_frame_header) (cinfo); 663 (*cinfo->marker->write_scan_header) (cinfo); 664 } 665 666 667 /* 668 * Finish up at end of pass. 669 */ 670 671 METHODDEF(void) 672 finish_pass_master(j_compress_ptr cinfo) 673 { 674 my_master_ptr master = (my_master_ptr)cinfo->master; 675 676 /* The entropy coder always needs an end-of-pass call, 677 * either to analyze statistics or to flush its output buffer. 678 */ 679 (*cinfo->entropy->finish_pass) (cinfo); 680 681 /* Update state for next pass */ 682 switch (master->pass_type) { 683 case main_pass: 684 /* next pass is either output of scan 0 (after optimization) 685 * or output of scan 1 (if no optimization). 686 */ 687 master->pass_type = output_pass; 688 if (!cinfo->optimize_coding) 689 master->scan_number++; 690 break; 691 case huff_opt_pass: 692 /* next pass is always output of current scan */ 693 master->pass_type = output_pass; 694 break; 695 case output_pass: 696 /* next pass is either optimization or output of next scan */ 697 if (cinfo->optimize_coding) 698 master->pass_type = huff_opt_pass; 699 master->scan_number++; 700 break; 701 } 702 703 master->pass_number++; 704 } 705 706 707 /* 708 * Initialize master compression control. 709 */ 710 711 GLOBAL(void) 712 jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only) 713 { 714 my_master_ptr master = (my_master_ptr)cinfo->master; 715 boolean empty_huff_tables = TRUE; 716 int i; 717 718 master->pub.prepare_for_pass = prepare_for_pass; 719 master->pub.pass_startup = pass_startup; 720 master->pub.finish_pass = finish_pass_master; 721 master->pub.is_last_pass = FALSE; 722 723 if (cinfo->scan_info != NULL) { 724 #ifdef NEED_SCAN_SCRIPT 725 validate_script(cinfo); 726 #else 727 ERREXIT(cinfo, JERR_NOT_COMPILED); 728 #endif 729 } else { 730 cinfo->progressive_mode = FALSE; 731 cinfo->num_scans = 1; 732 } 733 734 /* Disable smoothing and subsampling in lossless mode, since those are lossy 735 * algorithms. Set the JPEG colorspace to the input colorspace. Disable raw 736 * (downsampled) data input, because it isn't particularly useful without 737 * subsampling and has not been tested in lossless mode. 738 */ 739 if (cinfo->master->lossless) { 740 int ci; 741 jpeg_component_info *compptr; 742 743 cinfo->raw_data_in = FALSE; 744 cinfo->smoothing_factor = 0; 745 jpeg_default_colorspace(cinfo); 746 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 747 ci++, compptr++) 748 compptr->h_samp_factor = compptr->v_samp_factor = 1; 749 } 750 751 /* Validate parameters, determine derived values */ 752 initial_setup(cinfo, transcode_only); 753 754 if (cinfo->arith_code) 755 cinfo->optimize_coding = FALSE; 756 else { 757 if (cinfo->master->lossless || /* TEMPORARY HACK ??? */ 758 cinfo->progressive_mode) 759 cinfo->optimize_coding = TRUE; /* assume default tables no good for 760 progressive mode or lossless mode */ 761 for (i = 0; i < NUM_HUFF_TBLS; i++) { 762 if (cinfo->dc_huff_tbl_ptrs[i] != NULL || 763 cinfo->ac_huff_tbl_ptrs[i] != NULL) { 764 empty_huff_tables = FALSE; 765 break; 766 } 767 } 768 if (cinfo->data_precision == 12 && !cinfo->optimize_coding && 769 (empty_huff_tables || using_std_huff_tables(cinfo))) 770 cinfo->optimize_coding = TRUE; /* assume default tables no good for 771 12-bit data precision */ 772 } 773 774 /* Initialize my private state */ 775 if (transcode_only) { 776 /* no main pass in transcoding */ 777 if (cinfo->optimize_coding) 778 master->pass_type = huff_opt_pass; 779 else 780 master->pass_type = output_pass; 781 } else { 782 /* for normal compression, first pass is always this type: */ 783 master->pass_type = main_pass; 784 } 785 master->scan_number = 0; 786 master->pass_number = 0; 787 if (cinfo->optimize_coding) 788 master->total_passes = cinfo->num_scans * 2; 789 else 790 master->total_passes = cinfo->num_scans; 791 792 master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")"; 793 }