3dphotoshoot

annotate libs/libjpeg/jcmaster.c @ 16:c6952cc82cca

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