dbf-halloween2015

annotate libs/libjpeg/jcmaster.c @ 1:c3f5c32cb210

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