vrshoot

annotate libs/libjpeg/jcmaster.c @ 0:b2f14e535253

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