istereo2

annotate libs/libjpeg/jcmaster.c @ 35:643f4ab609a4

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