1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libjpeg/jcmaster.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,625 @@ 1.4 +/* 1.5 + * jcmaster.c 1.6 + * 1.7 + * This file was part of the Independent JPEG Group's software: 1.8 + * Copyright (C) 1991-1997, Thomas G. Lane. 1.9 + * Modified 2003-2010 by Guido Vollbeding. 1.10 + * libjpeg-turbo Modifications: 1.11 + * Copyright (C) 2010, D. R. Commander. 1.12 + * For conditions of distribution and use, see the accompanying README file. 1.13 + * 1.14 + * This file contains master control logic for the JPEG compressor. 1.15 + * These routines are concerned with parameter validation, initial setup, 1.16 + * and inter-pass control (determining the number of passes and the work 1.17 + * to be done in each pass). 1.18 + */ 1.19 + 1.20 +#define JPEG_INTERNALS 1.21 +#include "jinclude.h" 1.22 +#include "jpeglib.h" 1.23 +#include "jpegcomp.h" 1.24 + 1.25 + 1.26 +/* Private state */ 1.27 + 1.28 +typedef enum { 1.29 + main_pass, /* input data, also do first output step */ 1.30 + huff_opt_pass, /* Huffman code optimization pass */ 1.31 + output_pass /* data output pass */ 1.32 +} c_pass_type; 1.33 + 1.34 +typedef struct { 1.35 + struct jpeg_comp_master pub; /* public fields */ 1.36 + 1.37 + c_pass_type pass_type; /* the type of the current pass */ 1.38 + 1.39 + int pass_number; /* # of passes completed */ 1.40 + int total_passes; /* total # of passes needed */ 1.41 + 1.42 + int scan_number; /* current index in scan_info[] */ 1.43 +} my_comp_master; 1.44 + 1.45 +typedef my_comp_master * my_master_ptr; 1.46 + 1.47 + 1.48 +/* 1.49 + * Support routines that do various essential calculations. 1.50 + */ 1.51 + 1.52 +#if JPEG_LIB_VERSION >= 70 1.53 +/* 1.54 + * Compute JPEG image dimensions and related values. 1.55 + * NOTE: this is exported for possible use by application. 1.56 + * Hence it mustn't do anything that can't be done twice. 1.57 + */ 1.58 + 1.59 +GLOBAL(void) 1.60 +jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo) 1.61 +/* Do computations that are needed before master selection phase */ 1.62 +{ 1.63 + /* Hardwire it to "no scaling" */ 1.64 + cinfo->jpeg_width = cinfo->image_width; 1.65 + cinfo->jpeg_height = cinfo->image_height; 1.66 + cinfo->min_DCT_h_scaled_size = DCTSIZE; 1.67 + cinfo->min_DCT_v_scaled_size = DCTSIZE; 1.68 +} 1.69 +#endif 1.70 + 1.71 + 1.72 +LOCAL(void) 1.73 +initial_setup (j_compress_ptr cinfo, boolean transcode_only) 1.74 +/* Do computations that are needed before master selection phase */ 1.75 +{ 1.76 + int ci; 1.77 + jpeg_component_info *compptr; 1.78 + long samplesperrow; 1.79 + JDIMENSION jd_samplesperrow; 1.80 + 1.81 +#if JPEG_LIB_VERSION >= 70 1.82 +#if JPEG_LIB_VERSION >= 80 1.83 + if (!transcode_only) 1.84 +#endif 1.85 + jpeg_calc_jpeg_dimensions(cinfo); 1.86 +#endif 1.87 + 1.88 + /* Sanity check on image dimensions */ 1.89 + if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 1.90 + || cinfo->num_components <= 0 || cinfo->input_components <= 0) 1.91 + ERREXIT(cinfo, JERR_EMPTY_IMAGE); 1.92 + 1.93 + /* Make sure image isn't bigger than I can handle */ 1.94 + if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION || 1.95 + (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION) 1.96 + ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); 1.97 + 1.98 + /* Width of an input scanline must be representable as JDIMENSION. */ 1.99 + samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; 1.100 + jd_samplesperrow = (JDIMENSION) samplesperrow; 1.101 + if ((long) jd_samplesperrow != samplesperrow) 1.102 + ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); 1.103 + 1.104 + /* For now, precision must match compiled-in value... */ 1.105 + if (cinfo->data_precision != BITS_IN_JSAMPLE) 1.106 + ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); 1.107 + 1.108 + /* Check that number of components won't exceed internal array sizes */ 1.109 + if (cinfo->num_components > MAX_COMPONENTS) 1.110 + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 1.111 + MAX_COMPONENTS); 1.112 + 1.113 + /* Compute maximum sampling factors; check factor validity */ 1.114 + cinfo->max_h_samp_factor = 1; 1.115 + cinfo->max_v_samp_factor = 1; 1.116 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.117 + ci++, compptr++) { 1.118 + if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || 1.119 + compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) 1.120 + ERREXIT(cinfo, JERR_BAD_SAMPLING); 1.121 + cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, 1.122 + compptr->h_samp_factor); 1.123 + cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, 1.124 + compptr->v_samp_factor); 1.125 + } 1.126 + 1.127 + /* Compute dimensions of components */ 1.128 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.129 + ci++, compptr++) { 1.130 + /* Fill in the correct component_index value; don't rely on application */ 1.131 + compptr->component_index = ci; 1.132 + /* For compression, we never do DCT scaling. */ 1.133 +#if JPEG_LIB_VERSION >= 70 1.134 + compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE; 1.135 +#else 1.136 + compptr->DCT_scaled_size = DCTSIZE; 1.137 +#endif 1.138 + /* Size in DCT blocks */ 1.139 + compptr->width_in_blocks = (JDIMENSION) 1.140 + jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor, 1.141 + (long) (cinfo->max_h_samp_factor * DCTSIZE)); 1.142 + compptr->height_in_blocks = (JDIMENSION) 1.143 + jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor, 1.144 + (long) (cinfo->max_v_samp_factor * DCTSIZE)); 1.145 + /* Size in samples */ 1.146 + compptr->downsampled_width = (JDIMENSION) 1.147 + jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor, 1.148 + (long) cinfo->max_h_samp_factor); 1.149 + compptr->downsampled_height = (JDIMENSION) 1.150 + jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor, 1.151 + (long) cinfo->max_v_samp_factor); 1.152 + /* Mark component needed (this flag isn't actually used for compression) */ 1.153 + compptr->component_needed = TRUE; 1.154 + } 1.155 + 1.156 + /* Compute number of fully interleaved MCU rows (number of times that 1.157 + * main controller will call coefficient controller). 1.158 + */ 1.159 + cinfo->total_iMCU_rows = (JDIMENSION) 1.160 + jdiv_round_up((long) cinfo->_jpeg_height, 1.161 + (long) (cinfo->max_v_samp_factor*DCTSIZE)); 1.162 +} 1.163 + 1.164 + 1.165 +#ifdef C_MULTISCAN_FILES_SUPPORTED 1.166 + 1.167 +LOCAL(void) 1.168 +validate_script (j_compress_ptr cinfo) 1.169 +/* Verify that the scan script in cinfo->scan_info[] is valid; also 1.170 + * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. 1.171 + */ 1.172 +{ 1.173 + const jpeg_scan_info * scanptr; 1.174 + int scanno, ncomps, ci, coefi, thisi; 1.175 + int Ss, Se, Ah, Al; 1.176 + boolean component_sent[MAX_COMPONENTS]; 1.177 +#ifdef C_PROGRESSIVE_SUPPORTED 1.178 + int * last_bitpos_ptr; 1.179 + int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; 1.180 + /* -1 until that coefficient has been seen; then last Al for it */ 1.181 +#endif 1.182 + 1.183 + if (cinfo->num_scans <= 0) 1.184 + ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); 1.185 + 1.186 + /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; 1.187 + * for progressive JPEG, no scan can have this. 1.188 + */ 1.189 + scanptr = cinfo->scan_info; 1.190 + if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { 1.191 +#ifdef C_PROGRESSIVE_SUPPORTED 1.192 + cinfo->progressive_mode = TRUE; 1.193 + last_bitpos_ptr = & last_bitpos[0][0]; 1.194 + for (ci = 0; ci < cinfo->num_components; ci++) 1.195 + for (coefi = 0; coefi < DCTSIZE2; coefi++) 1.196 + *last_bitpos_ptr++ = -1; 1.197 +#else 1.198 + ERREXIT(cinfo, JERR_NOT_COMPILED); 1.199 +#endif 1.200 + } else { 1.201 + cinfo->progressive_mode = FALSE; 1.202 + for (ci = 0; ci < cinfo->num_components; ci++) 1.203 + component_sent[ci] = FALSE; 1.204 + } 1.205 + 1.206 + for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { 1.207 + /* Validate component indexes */ 1.208 + ncomps = scanptr->comps_in_scan; 1.209 + if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) 1.210 + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); 1.211 + for (ci = 0; ci < ncomps; ci++) { 1.212 + thisi = scanptr->component_index[ci]; 1.213 + if (thisi < 0 || thisi >= cinfo->num_components) 1.214 + ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 1.215 + /* Components must appear in SOF order within each scan */ 1.216 + if (ci > 0 && thisi <= scanptr->component_index[ci-1]) 1.217 + ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 1.218 + } 1.219 + /* Validate progression parameters */ 1.220 + Ss = scanptr->Ss; 1.221 + Se = scanptr->Se; 1.222 + Ah = scanptr->Ah; 1.223 + Al = scanptr->Al; 1.224 + if (cinfo->progressive_mode) { 1.225 +#ifdef C_PROGRESSIVE_SUPPORTED 1.226 + /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that 1.227 + * seems wrong: the upper bound ought to depend on data precision. 1.228 + * Perhaps they really meant 0..N+1 for N-bit precision. 1.229 + * Here we allow 0..10 for 8-bit data; Al larger than 10 results in 1.230 + * out-of-range reconstructed DC values during the first DC scan, 1.231 + * which might cause problems for some decoders. 1.232 + */ 1.233 +#if BITS_IN_JSAMPLE == 8 1.234 +#define MAX_AH_AL 10 1.235 +#else 1.236 +#define MAX_AH_AL 13 1.237 +#endif 1.238 + if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || 1.239 + Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) 1.240 + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 1.241 + if (Ss == 0) { 1.242 + if (Se != 0) /* DC and AC together not OK */ 1.243 + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 1.244 + } else { 1.245 + if (ncomps != 1) /* AC scans must be for only one component */ 1.246 + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 1.247 + } 1.248 + for (ci = 0; ci < ncomps; ci++) { 1.249 + last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; 1.250 + if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ 1.251 + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 1.252 + for (coefi = Ss; coefi <= Se; coefi++) { 1.253 + if (last_bitpos_ptr[coefi] < 0) { 1.254 + /* first scan of this coefficient */ 1.255 + if (Ah != 0) 1.256 + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 1.257 + } else { 1.258 + /* not first scan */ 1.259 + if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) 1.260 + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 1.261 + } 1.262 + last_bitpos_ptr[coefi] = Al; 1.263 + } 1.264 + } 1.265 +#endif 1.266 + } else { 1.267 + /* For sequential JPEG, all progression parameters must be these: */ 1.268 + if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0) 1.269 + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 1.270 + /* Make sure components are not sent twice */ 1.271 + for (ci = 0; ci < ncomps; ci++) { 1.272 + thisi = scanptr->component_index[ci]; 1.273 + if (component_sent[thisi]) 1.274 + ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 1.275 + component_sent[thisi] = TRUE; 1.276 + } 1.277 + } 1.278 + } 1.279 + 1.280 + /* Now verify that everything got sent. */ 1.281 + if (cinfo->progressive_mode) { 1.282 +#ifdef C_PROGRESSIVE_SUPPORTED 1.283 + /* For progressive mode, we only check that at least some DC data 1.284 + * got sent for each component; the spec does not require that all bits 1.285 + * of all coefficients be transmitted. Would it be wiser to enforce 1.286 + * transmission of all coefficient bits?? 1.287 + */ 1.288 + for (ci = 0; ci < cinfo->num_components; ci++) { 1.289 + if (last_bitpos[ci][0] < 0) 1.290 + ERREXIT(cinfo, JERR_MISSING_DATA); 1.291 + } 1.292 +#endif 1.293 + } else { 1.294 + for (ci = 0; ci < cinfo->num_components; ci++) { 1.295 + if (! component_sent[ci]) 1.296 + ERREXIT(cinfo, JERR_MISSING_DATA); 1.297 + } 1.298 + } 1.299 +} 1.300 + 1.301 +#endif /* C_MULTISCAN_FILES_SUPPORTED */ 1.302 + 1.303 + 1.304 +LOCAL(void) 1.305 +select_scan_parameters (j_compress_ptr cinfo) 1.306 +/* Set up the scan parameters for the current scan */ 1.307 +{ 1.308 + int ci; 1.309 + 1.310 +#ifdef C_MULTISCAN_FILES_SUPPORTED 1.311 + if (cinfo->scan_info != NULL) { 1.312 + /* Prepare for current scan --- the script is already validated */ 1.313 + my_master_ptr master = (my_master_ptr) cinfo->master; 1.314 + const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number; 1.315 + 1.316 + cinfo->comps_in_scan = scanptr->comps_in_scan; 1.317 + for (ci = 0; ci < scanptr->comps_in_scan; ci++) { 1.318 + cinfo->cur_comp_info[ci] = 1.319 + &cinfo->comp_info[scanptr->component_index[ci]]; 1.320 + } 1.321 + cinfo->Ss = scanptr->Ss; 1.322 + cinfo->Se = scanptr->Se; 1.323 + cinfo->Ah = scanptr->Ah; 1.324 + cinfo->Al = scanptr->Al; 1.325 + } 1.326 + else 1.327 +#endif 1.328 + { 1.329 + /* Prepare for single sequential-JPEG scan containing all components */ 1.330 + if (cinfo->num_components > MAX_COMPS_IN_SCAN) 1.331 + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 1.332 + MAX_COMPS_IN_SCAN); 1.333 + cinfo->comps_in_scan = cinfo->num_components; 1.334 + for (ci = 0; ci < cinfo->num_components; ci++) { 1.335 + cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; 1.336 + } 1.337 + cinfo->Ss = 0; 1.338 + cinfo->Se = DCTSIZE2-1; 1.339 + cinfo->Ah = 0; 1.340 + cinfo->Al = 0; 1.341 + } 1.342 +} 1.343 + 1.344 + 1.345 +LOCAL(void) 1.346 +per_scan_setup (j_compress_ptr cinfo) 1.347 +/* Do computations that are needed before processing a JPEG scan */ 1.348 +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ 1.349 +{ 1.350 + int ci, mcublks, tmp; 1.351 + jpeg_component_info *compptr; 1.352 + 1.353 + if (cinfo->comps_in_scan == 1) { 1.354 + 1.355 + /* Noninterleaved (single-component) scan */ 1.356 + compptr = cinfo->cur_comp_info[0]; 1.357 + 1.358 + /* Overall image size in MCUs */ 1.359 + cinfo->MCUs_per_row = compptr->width_in_blocks; 1.360 + cinfo->MCU_rows_in_scan = compptr->height_in_blocks; 1.361 + 1.362 + /* For noninterleaved scan, always one block per MCU */ 1.363 + compptr->MCU_width = 1; 1.364 + compptr->MCU_height = 1; 1.365 + compptr->MCU_blocks = 1; 1.366 + compptr->MCU_sample_width = DCTSIZE; 1.367 + compptr->last_col_width = 1; 1.368 + /* For noninterleaved scans, it is convenient to define last_row_height 1.369 + * as the number of block rows present in the last iMCU row. 1.370 + */ 1.371 + tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 1.372 + if (tmp == 0) tmp = compptr->v_samp_factor; 1.373 + compptr->last_row_height = tmp; 1.374 + 1.375 + /* Prepare array describing MCU composition */ 1.376 + cinfo->blocks_in_MCU = 1; 1.377 + cinfo->MCU_membership[0] = 0; 1.378 + 1.379 + } else { 1.380 + 1.381 + /* Interleaved (multi-component) scan */ 1.382 + if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) 1.383 + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, 1.384 + MAX_COMPS_IN_SCAN); 1.385 + 1.386 + /* Overall image size in MCUs */ 1.387 + cinfo->MCUs_per_row = (JDIMENSION) 1.388 + jdiv_round_up((long) cinfo->_jpeg_width, 1.389 + (long) (cinfo->max_h_samp_factor*DCTSIZE)); 1.390 + cinfo->MCU_rows_in_scan = (JDIMENSION) 1.391 + jdiv_round_up((long) cinfo->_jpeg_height, 1.392 + (long) (cinfo->max_v_samp_factor*DCTSIZE)); 1.393 + 1.394 + cinfo->blocks_in_MCU = 0; 1.395 + 1.396 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.397 + compptr = cinfo->cur_comp_info[ci]; 1.398 + /* Sampling factors give # of blocks of component in each MCU */ 1.399 + compptr->MCU_width = compptr->h_samp_factor; 1.400 + compptr->MCU_height = compptr->v_samp_factor; 1.401 + compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; 1.402 + compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE; 1.403 + /* Figure number of non-dummy blocks in last MCU column & row */ 1.404 + tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); 1.405 + if (tmp == 0) tmp = compptr->MCU_width; 1.406 + compptr->last_col_width = tmp; 1.407 + tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); 1.408 + if (tmp == 0) tmp = compptr->MCU_height; 1.409 + compptr->last_row_height = tmp; 1.410 + /* Prepare array describing MCU composition */ 1.411 + mcublks = compptr->MCU_blocks; 1.412 + if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) 1.413 + ERREXIT(cinfo, JERR_BAD_MCU_SIZE); 1.414 + while (mcublks-- > 0) { 1.415 + cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; 1.416 + } 1.417 + } 1.418 + 1.419 + } 1.420 + 1.421 + /* Convert restart specified in rows to actual MCU count. */ 1.422 + /* Note that count must fit in 16 bits, so we provide limiting. */ 1.423 + if (cinfo->restart_in_rows > 0) { 1.424 + long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row; 1.425 + cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L); 1.426 + } 1.427 +} 1.428 + 1.429 + 1.430 +/* 1.431 + * Per-pass setup. 1.432 + * This is called at the beginning of each pass. We determine which modules 1.433 + * will be active during this pass and give them appropriate start_pass calls. 1.434 + * We also set is_last_pass to indicate whether any more passes will be 1.435 + * required. 1.436 + */ 1.437 + 1.438 +METHODDEF(void) 1.439 +prepare_for_pass (j_compress_ptr cinfo) 1.440 +{ 1.441 + my_master_ptr master = (my_master_ptr) cinfo->master; 1.442 + 1.443 + switch (master->pass_type) { 1.444 + case main_pass: 1.445 + /* Initial pass: will collect input data, and do either Huffman 1.446 + * optimization or data output for the first scan. 1.447 + */ 1.448 + select_scan_parameters(cinfo); 1.449 + per_scan_setup(cinfo); 1.450 + if (! cinfo->raw_data_in) { 1.451 + (*cinfo->cconvert->start_pass) (cinfo); 1.452 + (*cinfo->downsample->start_pass) (cinfo); 1.453 + (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); 1.454 + } 1.455 + (*cinfo->fdct->start_pass) (cinfo); 1.456 + (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); 1.457 + (*cinfo->coef->start_pass) (cinfo, 1.458 + (master->total_passes > 1 ? 1.459 + JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); 1.460 + (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); 1.461 + if (cinfo->optimize_coding) { 1.462 + /* No immediate data output; postpone writing frame/scan headers */ 1.463 + master->pub.call_pass_startup = FALSE; 1.464 + } else { 1.465 + /* Will write frame/scan headers at first jpeg_write_scanlines call */ 1.466 + master->pub.call_pass_startup = TRUE; 1.467 + } 1.468 + break; 1.469 +#ifdef ENTROPY_OPT_SUPPORTED 1.470 + case huff_opt_pass: 1.471 + /* Do Huffman optimization for a scan after the first one. */ 1.472 + select_scan_parameters(cinfo); 1.473 + per_scan_setup(cinfo); 1.474 + if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) { 1.475 + (*cinfo->entropy->start_pass) (cinfo, TRUE); 1.476 + (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); 1.477 + master->pub.call_pass_startup = FALSE; 1.478 + break; 1.479 + } 1.480 + /* Special case: Huffman DC refinement scans need no Huffman table 1.481 + * and therefore we can skip the optimization pass for them. 1.482 + */ 1.483 + master->pass_type = output_pass; 1.484 + master->pass_number++; 1.485 + /*FALLTHROUGH*/ 1.486 +#endif 1.487 + case output_pass: 1.488 + /* Do a data-output pass. */ 1.489 + /* We need not repeat per-scan setup if prior optimization pass did it. */ 1.490 + if (! cinfo->optimize_coding) { 1.491 + select_scan_parameters(cinfo); 1.492 + per_scan_setup(cinfo); 1.493 + } 1.494 + (*cinfo->entropy->start_pass) (cinfo, FALSE); 1.495 + (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); 1.496 + /* We emit frame/scan headers now */ 1.497 + if (master->scan_number == 0) 1.498 + (*cinfo->marker->write_frame_header) (cinfo); 1.499 + (*cinfo->marker->write_scan_header) (cinfo); 1.500 + master->pub.call_pass_startup = FALSE; 1.501 + break; 1.502 + default: 1.503 + ERREXIT(cinfo, JERR_NOT_COMPILED); 1.504 + } 1.505 + 1.506 + master->pub.is_last_pass = (master->pass_number == master->total_passes-1); 1.507 + 1.508 + /* Set up progress monitor's pass info if present */ 1.509 + if (cinfo->progress != NULL) { 1.510 + cinfo->progress->completed_passes = master->pass_number; 1.511 + cinfo->progress->total_passes = master->total_passes; 1.512 + } 1.513 +} 1.514 + 1.515 + 1.516 +/* 1.517 + * Special start-of-pass hook. 1.518 + * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. 1.519 + * In single-pass processing, we need this hook because we don't want to 1.520 + * write frame/scan headers during jpeg_start_compress; we want to let the 1.521 + * application write COM markers etc. between jpeg_start_compress and the 1.522 + * jpeg_write_scanlines loop. 1.523 + * In multi-pass processing, this routine is not used. 1.524 + */ 1.525 + 1.526 +METHODDEF(void) 1.527 +pass_startup (j_compress_ptr cinfo) 1.528 +{ 1.529 + cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ 1.530 + 1.531 + (*cinfo->marker->write_frame_header) (cinfo); 1.532 + (*cinfo->marker->write_scan_header) (cinfo); 1.533 +} 1.534 + 1.535 + 1.536 +/* 1.537 + * Finish up at end of pass. 1.538 + */ 1.539 + 1.540 +METHODDEF(void) 1.541 +finish_pass_master (j_compress_ptr cinfo) 1.542 +{ 1.543 + my_master_ptr master = (my_master_ptr) cinfo->master; 1.544 + 1.545 + /* The entropy coder always needs an end-of-pass call, 1.546 + * either to analyze statistics or to flush its output buffer. 1.547 + */ 1.548 + (*cinfo->entropy->finish_pass) (cinfo); 1.549 + 1.550 + /* Update state for next pass */ 1.551 + switch (master->pass_type) { 1.552 + case main_pass: 1.553 + /* next pass is either output of scan 0 (after optimization) 1.554 + * or output of scan 1 (if no optimization). 1.555 + */ 1.556 + master->pass_type = output_pass; 1.557 + if (! cinfo->optimize_coding) 1.558 + master->scan_number++; 1.559 + break; 1.560 + case huff_opt_pass: 1.561 + /* next pass is always output of current scan */ 1.562 + master->pass_type = output_pass; 1.563 + break; 1.564 + case output_pass: 1.565 + /* next pass is either optimization or output of next scan */ 1.566 + if (cinfo->optimize_coding) 1.567 + master->pass_type = huff_opt_pass; 1.568 + master->scan_number++; 1.569 + break; 1.570 + } 1.571 + 1.572 + master->pass_number++; 1.573 +} 1.574 + 1.575 + 1.576 +/* 1.577 + * Initialize master compression control. 1.578 + */ 1.579 + 1.580 +GLOBAL(void) 1.581 +jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) 1.582 +{ 1.583 + my_master_ptr master; 1.584 + 1.585 + master = (my_master_ptr) 1.586 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.587 + SIZEOF(my_comp_master)); 1.588 + cinfo->master = (struct jpeg_comp_master *) master; 1.589 + master->pub.prepare_for_pass = prepare_for_pass; 1.590 + master->pub.pass_startup = pass_startup; 1.591 + master->pub.finish_pass = finish_pass_master; 1.592 + master->pub.is_last_pass = FALSE; 1.593 + 1.594 + /* Validate parameters, determine derived values */ 1.595 + initial_setup(cinfo, transcode_only); 1.596 + 1.597 + if (cinfo->scan_info != NULL) { 1.598 +#ifdef C_MULTISCAN_FILES_SUPPORTED 1.599 + validate_script(cinfo); 1.600 +#else 1.601 + ERREXIT(cinfo, JERR_NOT_COMPILED); 1.602 +#endif 1.603 + } else { 1.604 + cinfo->progressive_mode = FALSE; 1.605 + cinfo->num_scans = 1; 1.606 + } 1.607 + 1.608 + if (cinfo->progressive_mode && !cinfo->arith_code) /* TEMPORARY HACK ??? */ 1.609 + cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */ 1.610 + 1.611 + /* Initialize my private state */ 1.612 + if (transcode_only) { 1.613 + /* no main pass in transcoding */ 1.614 + if (cinfo->optimize_coding) 1.615 + master->pass_type = huff_opt_pass; 1.616 + else 1.617 + master->pass_type = output_pass; 1.618 + } else { 1.619 + /* for normal compression, first pass is always this type: */ 1.620 + master->pass_type = main_pass; 1.621 + } 1.622 + master->scan_number = 0; 1.623 + master->pass_number = 0; 1.624 + if (cinfo->optimize_coding) 1.625 + master->total_passes = cinfo->num_scans * 2; 1.626 + else 1.627 + master->total_passes = cinfo->num_scans; 1.628 +}