nuclear@2: /* nuclear@2: * jdmaster.c nuclear@2: * nuclear@2: * Copyright (C) 1991-1997, Thomas G. Lane. nuclear@2: * This file is part of the Independent JPEG Group's software. nuclear@2: * For conditions of distribution and use, see the accompanying README file. nuclear@2: * nuclear@2: * This file contains master control logic for the JPEG decompressor. nuclear@2: * These routines are concerned with selecting the modules to be executed nuclear@2: * and with determining the number of passes and the work to be done in each nuclear@2: * pass. nuclear@2: */ nuclear@2: nuclear@2: #define JPEG_INTERNALS nuclear@2: #include "jinclude.h" nuclear@2: #include "jpeglib.h" nuclear@2: nuclear@2: nuclear@2: /* Private state */ nuclear@2: nuclear@2: typedef struct { nuclear@2: struct jpeg_decomp_master pub; /* public fields */ nuclear@2: nuclear@2: int pass_number; /* # of passes completed */ nuclear@2: nuclear@2: boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */ nuclear@2: nuclear@2: /* Saved references to initialized quantizer modules, nuclear@2: * in case we need to switch modes. nuclear@2: */ nuclear@2: struct jpeg_color_quantizer * quantizer_1pass; nuclear@2: struct jpeg_color_quantizer * quantizer_2pass; nuclear@2: } my_decomp_master; nuclear@2: nuclear@2: typedef my_decomp_master * my_master_ptr; nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Determine whether merged upsample/color conversion should be used. nuclear@2: * CRUCIAL: this must match the actual capabilities of jdmerge.c! nuclear@2: */ nuclear@2: nuclear@2: LOCAL(boolean) nuclear@2: use_merged_upsample (j_decompress_ptr cinfo) nuclear@2: { nuclear@2: #ifdef UPSAMPLE_MERGING_SUPPORTED nuclear@2: /* Merging is the equivalent of plain box-filter upsampling */ nuclear@2: if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling) nuclear@2: return FALSE; nuclear@2: /* jdmerge.c only supports YCC=>RGB color conversion */ nuclear@2: if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 || nuclear@2: cinfo->out_color_space != JCS_RGB || nuclear@2: cinfo->out_color_components != RGB_PIXELSIZE) nuclear@2: return FALSE; nuclear@2: /* and it only handles 2h1v or 2h2v sampling ratios */ nuclear@2: if (cinfo->comp_info[0].h_samp_factor != 2 || nuclear@2: cinfo->comp_info[1].h_samp_factor != 1 || nuclear@2: cinfo->comp_info[2].h_samp_factor != 1 || nuclear@2: cinfo->comp_info[0].v_samp_factor > 2 || nuclear@2: cinfo->comp_info[1].v_samp_factor != 1 || nuclear@2: cinfo->comp_info[2].v_samp_factor != 1) nuclear@2: return FALSE; nuclear@2: /* furthermore, it doesn't work if we've scaled the IDCTs differently */ nuclear@2: if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size || nuclear@2: cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size || nuclear@2: cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size) nuclear@2: return FALSE; nuclear@2: /* ??? also need to test for upsample-time rescaling, when & if supported */ nuclear@2: return TRUE; /* by golly, it'll work... */ nuclear@2: #else nuclear@2: return FALSE; nuclear@2: #endif nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Compute output image dimensions and related values. nuclear@2: * NOTE: this is exported for possible use by application. nuclear@2: * Hence it mustn't do anything that can't be done twice. nuclear@2: * Also note that it may be called before the master module is initialized! nuclear@2: */ nuclear@2: nuclear@2: GLOBAL(void) nuclear@2: jpeg_calc_output_dimensions (j_decompress_ptr cinfo) nuclear@2: /* Do computations that are needed before master selection phase */ nuclear@2: { nuclear@2: #ifdef IDCT_SCALING_SUPPORTED nuclear@2: int ci; nuclear@2: jpeg_component_info *compptr; nuclear@2: #endif nuclear@2: nuclear@2: /* Prevent application from calling me at wrong times */ nuclear@2: if (cinfo->global_state != DSTATE_READY) nuclear@2: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@2: nuclear@2: #ifdef IDCT_SCALING_SUPPORTED nuclear@2: nuclear@2: /* Compute actual output image dimensions and DCT scaling choices. */ nuclear@2: if (cinfo->scale_num * 8 <= cinfo->scale_denom) { nuclear@2: /* Provide 1/8 scaling */ nuclear@2: cinfo->output_width = (JDIMENSION) nuclear@2: jdiv_round_up((long) cinfo->image_width, 8L); nuclear@2: cinfo->output_height = (JDIMENSION) nuclear@2: jdiv_round_up((long) cinfo->image_height, 8L); nuclear@2: cinfo->min_DCT_scaled_size = 1; nuclear@2: } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) { nuclear@2: /* Provide 1/4 scaling */ nuclear@2: cinfo->output_width = (JDIMENSION) nuclear@2: jdiv_round_up((long) cinfo->image_width, 4L); nuclear@2: cinfo->output_height = (JDIMENSION) nuclear@2: jdiv_round_up((long) cinfo->image_height, 4L); nuclear@2: cinfo->min_DCT_scaled_size = 2; nuclear@2: } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) { nuclear@2: /* Provide 1/2 scaling */ nuclear@2: cinfo->output_width = (JDIMENSION) nuclear@2: jdiv_round_up((long) cinfo->image_width, 2L); nuclear@2: cinfo->output_height = (JDIMENSION) nuclear@2: jdiv_round_up((long) cinfo->image_height, 2L); nuclear@2: cinfo->min_DCT_scaled_size = 4; nuclear@2: } else { nuclear@2: /* Provide 1/1 scaling */ nuclear@2: cinfo->output_width = cinfo->image_width; nuclear@2: cinfo->output_height = cinfo->image_height; nuclear@2: cinfo->min_DCT_scaled_size = DCTSIZE; nuclear@2: } nuclear@2: /* In selecting the actual DCT scaling for each component, we try to nuclear@2: * scale up the chroma components via IDCT scaling rather than upsampling. nuclear@2: * This saves time if the upsampler gets to use 1:1 scaling. nuclear@2: * Note this code assumes that the supported DCT scalings are powers of 2. nuclear@2: */ nuclear@2: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; nuclear@2: ci++, compptr++) { nuclear@2: int ssize = cinfo->min_DCT_scaled_size; nuclear@2: while (ssize < DCTSIZE && nuclear@2: (compptr->h_samp_factor * ssize * 2 <= nuclear@2: cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) && nuclear@2: (compptr->v_samp_factor * ssize * 2 <= nuclear@2: cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) { nuclear@2: ssize = ssize * 2; nuclear@2: } nuclear@2: compptr->DCT_scaled_size = ssize; nuclear@2: } nuclear@2: nuclear@2: /* Recompute downsampled dimensions of components; nuclear@2: * application needs to know these if using raw downsampled data. nuclear@2: */ nuclear@2: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; nuclear@2: ci++, compptr++) { nuclear@2: /* Size in samples, after IDCT scaling */ nuclear@2: compptr->downsampled_width = (JDIMENSION) nuclear@2: jdiv_round_up((long) cinfo->image_width * nuclear@2: (long) (compptr->h_samp_factor * compptr->DCT_scaled_size), nuclear@2: (long) (cinfo->max_h_samp_factor * DCTSIZE)); nuclear@2: compptr->downsampled_height = (JDIMENSION) nuclear@2: jdiv_round_up((long) cinfo->image_height * nuclear@2: (long) (compptr->v_samp_factor * compptr->DCT_scaled_size), nuclear@2: (long) (cinfo->max_v_samp_factor * DCTSIZE)); nuclear@2: } nuclear@2: nuclear@2: #else /* !IDCT_SCALING_SUPPORTED */ nuclear@2: nuclear@2: /* Hardwire it to "no scaling" */ nuclear@2: cinfo->output_width = cinfo->image_width; nuclear@2: cinfo->output_height = cinfo->image_height; nuclear@2: /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE, nuclear@2: * and has computed unscaled downsampled_width and downsampled_height. nuclear@2: */ nuclear@2: nuclear@2: #endif /* IDCT_SCALING_SUPPORTED */ nuclear@2: nuclear@2: /* Report number of components in selected colorspace. */ nuclear@2: /* Probably this should be in the color conversion module... */ nuclear@2: switch (cinfo->out_color_space) { nuclear@2: case JCS_GRAYSCALE: nuclear@2: cinfo->out_color_components = 1; nuclear@2: break; nuclear@2: case JCS_RGB: nuclear@2: #if RGB_PIXELSIZE != 3 nuclear@2: cinfo->out_color_components = RGB_PIXELSIZE; nuclear@2: break; nuclear@2: #endif /* else share code with YCbCr */ nuclear@2: case JCS_YCbCr: nuclear@2: cinfo->out_color_components = 3; nuclear@2: break; nuclear@2: case JCS_CMYK: nuclear@2: case JCS_YCCK: nuclear@2: cinfo->out_color_components = 4; nuclear@2: break; nuclear@2: default: /* else must be same colorspace as in file */ nuclear@2: cinfo->out_color_components = cinfo->num_components; nuclear@2: break; nuclear@2: } nuclear@2: cinfo->output_components = (cinfo->quantize_colors ? 1 : nuclear@2: cinfo->out_color_components); nuclear@2: nuclear@2: /* See if upsampler will want to emit more than one row at a time */ nuclear@2: if (use_merged_upsample(cinfo)) nuclear@2: cinfo->rec_outbuf_height = cinfo->max_v_samp_factor; nuclear@2: else nuclear@2: cinfo->rec_outbuf_height = 1; nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Several decompression processes need to range-limit values to the range nuclear@2: * 0..MAXJSAMPLE; the input value may fall somewhat outside this range nuclear@2: * due to noise introduced by quantization, roundoff error, etc. These nuclear@2: * processes are inner loops and need to be as fast as possible. On most nuclear@2: * machines, particularly CPUs with pipelines or instruction prefetch, nuclear@2: * a (subscript-check-less) C table lookup nuclear@2: * x = sample_range_limit[x]; nuclear@2: * is faster than explicit tests nuclear@2: * if (x < 0) x = 0; nuclear@2: * else if (x > MAXJSAMPLE) x = MAXJSAMPLE; nuclear@2: * These processes all use a common table prepared by the routine below. nuclear@2: * nuclear@2: * For most steps we can mathematically guarantee that the initial value nuclear@2: * of x is within MAXJSAMPLE+1 of the legal range, so a table running from nuclear@2: * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial nuclear@2: * limiting step (just after the IDCT), a wildly out-of-range value is nuclear@2: * possible if the input data is corrupt. To avoid any chance of indexing nuclear@2: * off the end of memory and getting a bad-pointer trap, we perform the nuclear@2: * post-IDCT limiting thus: nuclear@2: * x = range_limit[x & MASK]; nuclear@2: * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit nuclear@2: * samples. Under normal circumstances this is more than enough range and nuclear@2: * a correct output will be generated; with bogus input data the mask will nuclear@2: * cause wraparound, and we will safely generate a bogus-but-in-range output. nuclear@2: * For the post-IDCT step, we want to convert the data from signed to unsigned nuclear@2: * representation by adding CENTERJSAMPLE at the same time that we limit it. nuclear@2: * So the post-IDCT limiting table ends up looking like this: nuclear@2: * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE, nuclear@2: * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times), nuclear@2: * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times), nuclear@2: * 0,1,...,CENTERJSAMPLE-1 nuclear@2: * Negative inputs select values from the upper half of the table after nuclear@2: * masking. nuclear@2: * nuclear@2: * We can save some space by overlapping the start of the post-IDCT table nuclear@2: * with the simpler range limiting table. The post-IDCT table begins at nuclear@2: * sample_range_limit + CENTERJSAMPLE. nuclear@2: * nuclear@2: * Note that the table is allocated in near data space on PCs; it's small nuclear@2: * enough and used often enough to justify this. nuclear@2: */ nuclear@2: nuclear@2: LOCAL(void) nuclear@2: prepare_range_limit_table (j_decompress_ptr cinfo) nuclear@2: /* Allocate and fill in the sample_range_limit table */ nuclear@2: { nuclear@2: JSAMPLE * table; nuclear@2: int i; nuclear@2: nuclear@2: table = (JSAMPLE *) nuclear@2: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@2: (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE)); nuclear@2: table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */ nuclear@2: cinfo->sample_range_limit = table; nuclear@2: /* First segment of "simple" table: limit[x] = 0 for x < 0 */ nuclear@2: MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE)); nuclear@2: /* Main part of "simple" table: limit[x] = x */ nuclear@2: for (i = 0; i <= MAXJSAMPLE; i++) nuclear@2: table[i] = (JSAMPLE) i; nuclear@2: table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */ nuclear@2: /* End of simple table, rest of first half of post-IDCT table */ nuclear@2: for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++) nuclear@2: table[i] = MAXJSAMPLE; nuclear@2: /* Second half of post-IDCT table */ nuclear@2: MEMZERO(table + (2 * (MAXJSAMPLE+1)), nuclear@2: (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE)); nuclear@2: MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE), nuclear@2: cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE)); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Master selection of decompression modules. nuclear@2: * This is done once at jpeg_start_decompress time. We determine nuclear@2: * which modules will be used and give them appropriate initialization calls. nuclear@2: * We also initialize the decompressor input side to begin consuming data. nuclear@2: * nuclear@2: * Since jpeg_read_header has finished, we know what is in the SOF nuclear@2: * and (first) SOS markers. We also have all the application parameter nuclear@2: * settings. nuclear@2: */ nuclear@2: nuclear@2: LOCAL(void) nuclear@2: master_selection (j_decompress_ptr cinfo) nuclear@2: { nuclear@2: my_master_ptr master = (my_master_ptr) cinfo->master; nuclear@2: boolean use_c_buffer; nuclear@2: long samplesperrow; nuclear@2: JDIMENSION jd_samplesperrow; nuclear@2: nuclear@2: /* Initialize dimensions and other stuff */ nuclear@2: jpeg_calc_output_dimensions(cinfo); nuclear@2: prepare_range_limit_table(cinfo); nuclear@2: nuclear@2: /* Width of an output scanline must be representable as JDIMENSION. */ nuclear@2: samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components; nuclear@2: jd_samplesperrow = (JDIMENSION) samplesperrow; nuclear@2: if ((long) jd_samplesperrow != samplesperrow) nuclear@2: ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); nuclear@2: nuclear@2: /* Initialize my private state */ nuclear@2: master->pass_number = 0; nuclear@2: master->using_merged_upsample = use_merged_upsample(cinfo); nuclear@2: nuclear@2: /* Color quantizer selection */ nuclear@2: master->quantizer_1pass = NULL; nuclear@2: master->quantizer_2pass = NULL; nuclear@2: /* No mode changes if not using buffered-image mode. */ nuclear@2: if (! cinfo->quantize_colors || ! cinfo->buffered_image) { nuclear@2: cinfo->enable_1pass_quant = FALSE; nuclear@2: cinfo->enable_external_quant = FALSE; nuclear@2: cinfo->enable_2pass_quant = FALSE; nuclear@2: } nuclear@2: if (cinfo->quantize_colors) { nuclear@2: if (cinfo->raw_data_out) nuclear@2: ERREXIT(cinfo, JERR_NOTIMPL); nuclear@2: /* 2-pass quantizer only works in 3-component color space. */ nuclear@2: if (cinfo->out_color_components != 3) { nuclear@2: cinfo->enable_1pass_quant = TRUE; nuclear@2: cinfo->enable_external_quant = FALSE; nuclear@2: cinfo->enable_2pass_quant = FALSE; nuclear@2: cinfo->colormap = NULL; nuclear@2: } else if (cinfo->colormap != NULL) { nuclear@2: cinfo->enable_external_quant = TRUE; nuclear@2: } else if (cinfo->two_pass_quantize) { nuclear@2: cinfo->enable_2pass_quant = TRUE; nuclear@2: } else { nuclear@2: cinfo->enable_1pass_quant = TRUE; nuclear@2: } nuclear@2: nuclear@2: if (cinfo->enable_1pass_quant) { nuclear@2: #ifdef QUANT_1PASS_SUPPORTED nuclear@2: jinit_1pass_quantizer(cinfo); nuclear@2: master->quantizer_1pass = cinfo->cquantize; nuclear@2: #else nuclear@2: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@2: #endif nuclear@2: } nuclear@2: nuclear@2: /* We use the 2-pass code to map to external colormaps. */ nuclear@2: if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) { nuclear@2: #ifdef QUANT_2PASS_SUPPORTED nuclear@2: jinit_2pass_quantizer(cinfo); nuclear@2: master->quantizer_2pass = cinfo->cquantize; nuclear@2: #else nuclear@2: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@2: #endif nuclear@2: } nuclear@2: /* If both quantizers are initialized, the 2-pass one is left active; nuclear@2: * this is necessary for starting with quantization to an external map. nuclear@2: */ nuclear@2: } nuclear@2: nuclear@2: /* Post-processing: in particular, color conversion first */ nuclear@2: if (! cinfo->raw_data_out) { nuclear@2: if (master->using_merged_upsample) { nuclear@2: #ifdef UPSAMPLE_MERGING_SUPPORTED nuclear@2: jinit_merged_upsampler(cinfo); /* does color conversion too */ nuclear@2: #else nuclear@2: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@2: #endif nuclear@2: } else { nuclear@2: jinit_color_deconverter(cinfo); nuclear@2: jinit_upsampler(cinfo); nuclear@2: } nuclear@2: jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant); nuclear@2: } nuclear@2: /* Inverse DCT */ nuclear@2: jinit_inverse_dct(cinfo); nuclear@2: /* Entropy decoding: either Huffman or arithmetic coding. */ nuclear@2: if (cinfo->arith_code) { nuclear@2: ERREXIT(cinfo, JERR_ARITH_NOTIMPL); nuclear@2: } else { nuclear@2: if (cinfo->progressive_mode) { nuclear@2: #ifdef D_PROGRESSIVE_SUPPORTED nuclear@2: jinit_phuff_decoder(cinfo); nuclear@2: #else nuclear@2: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@2: #endif nuclear@2: } else nuclear@2: jinit_huff_decoder(cinfo); nuclear@2: } nuclear@2: nuclear@2: /* Initialize principal buffer controllers. */ nuclear@2: use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image; nuclear@2: jinit_d_coef_controller(cinfo, use_c_buffer); nuclear@2: nuclear@2: if (! cinfo->raw_data_out) nuclear@2: jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */); nuclear@2: nuclear@2: /* We can now tell the memory manager to allocate virtual arrays. */ nuclear@2: (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); nuclear@2: nuclear@2: /* Initialize input side of decompressor to consume first scan. */ nuclear@2: (*cinfo->inputctl->start_input_pass) (cinfo); nuclear@2: nuclear@2: #ifdef D_MULTISCAN_FILES_SUPPORTED nuclear@2: /* If jpeg_start_decompress will read the whole file, initialize nuclear@2: * progress monitoring appropriately. The input step is counted nuclear@2: * as one pass. nuclear@2: */ nuclear@2: if (cinfo->progress != NULL && ! cinfo->buffered_image && nuclear@2: cinfo->inputctl->has_multiple_scans) { nuclear@2: int nscans; nuclear@2: /* Estimate number of scans to set pass_limit. */ nuclear@2: if (cinfo->progressive_mode) { nuclear@2: /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ nuclear@2: nscans = 2 + 3 * cinfo->num_components; nuclear@2: } else { nuclear@2: /* For a nonprogressive multiscan file, estimate 1 scan per component. */ nuclear@2: nscans = cinfo->num_components; nuclear@2: } nuclear@2: cinfo->progress->pass_counter = 0L; nuclear@2: cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; nuclear@2: cinfo->progress->completed_passes = 0; nuclear@2: cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2); nuclear@2: /* Count the input pass as done */ nuclear@2: master->pass_number++; nuclear@2: } nuclear@2: #endif /* D_MULTISCAN_FILES_SUPPORTED */ nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Per-pass setup. nuclear@2: * This is called at the beginning of each output pass. We determine which nuclear@2: * modules will be active during this pass and give them appropriate nuclear@2: * start_pass calls. We also set is_dummy_pass to indicate whether this nuclear@2: * is a "real" output pass or a dummy pass for color quantization. nuclear@2: * (In the latter case, jdapistd.c will crank the pass to completion.) nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: prepare_for_output_pass (j_decompress_ptr cinfo) nuclear@2: { nuclear@2: my_master_ptr master = (my_master_ptr) cinfo->master; nuclear@2: nuclear@2: if (master->pub.is_dummy_pass) { nuclear@2: #ifdef QUANT_2PASS_SUPPORTED nuclear@2: /* Final pass of 2-pass quantization */ nuclear@2: master->pub.is_dummy_pass = FALSE; nuclear@2: (*cinfo->cquantize->start_pass) (cinfo, FALSE); nuclear@2: (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST); nuclear@2: (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST); nuclear@2: #else nuclear@2: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@2: #endif /* QUANT_2PASS_SUPPORTED */ nuclear@2: } else { nuclear@2: if (cinfo->quantize_colors && cinfo->colormap == NULL) { nuclear@2: /* Select new quantization method */ nuclear@2: if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) { nuclear@2: cinfo->cquantize = master->quantizer_2pass; nuclear@2: master->pub.is_dummy_pass = TRUE; nuclear@2: } else if (cinfo->enable_1pass_quant) { nuclear@2: cinfo->cquantize = master->quantizer_1pass; nuclear@2: } else { nuclear@2: ERREXIT(cinfo, JERR_MODE_CHANGE); nuclear@2: } nuclear@2: } nuclear@2: (*cinfo->idct->start_pass) (cinfo); nuclear@2: (*cinfo->coef->start_output_pass) (cinfo); nuclear@2: if (! cinfo->raw_data_out) { nuclear@2: if (! master->using_merged_upsample) nuclear@2: (*cinfo->cconvert->start_pass) (cinfo); nuclear@2: (*cinfo->upsample->start_pass) (cinfo); nuclear@2: if (cinfo->quantize_colors) nuclear@2: (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass); nuclear@2: (*cinfo->post->start_pass) (cinfo, nuclear@2: (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); nuclear@2: (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: /* Set up progress monitor's pass info if present */ nuclear@2: if (cinfo->progress != NULL) { nuclear@2: cinfo->progress->completed_passes = master->pass_number; nuclear@2: cinfo->progress->total_passes = master->pass_number + nuclear@2: (master->pub.is_dummy_pass ? 2 : 1); nuclear@2: /* In buffered-image mode, we assume one more output pass if EOI not nuclear@2: * yet reached, but no more passes if EOI has been reached. nuclear@2: */ nuclear@2: if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) { nuclear@2: cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1); nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Finish up at end of an output pass. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: finish_output_pass (j_decompress_ptr cinfo) nuclear@2: { nuclear@2: my_master_ptr master = (my_master_ptr) cinfo->master; nuclear@2: nuclear@2: if (cinfo->quantize_colors) nuclear@2: (*cinfo->cquantize->finish_pass) (cinfo); nuclear@2: master->pass_number++; nuclear@2: } nuclear@2: nuclear@2: nuclear@2: #ifdef D_MULTISCAN_FILES_SUPPORTED nuclear@2: nuclear@2: /* nuclear@2: * Switch to a new external colormap between output passes. nuclear@2: */ nuclear@2: nuclear@2: GLOBAL(void) nuclear@2: jpeg_new_colormap (j_decompress_ptr cinfo) nuclear@2: { nuclear@2: my_master_ptr master = (my_master_ptr) cinfo->master; nuclear@2: nuclear@2: /* Prevent application from calling me at wrong times */ nuclear@2: if (cinfo->global_state != DSTATE_BUFIMAGE) nuclear@2: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@2: nuclear@2: if (cinfo->quantize_colors && cinfo->enable_external_quant && nuclear@2: cinfo->colormap != NULL) { nuclear@2: /* Select 2-pass quantizer for external colormap use */ nuclear@2: cinfo->cquantize = master->quantizer_2pass; nuclear@2: /* Notify quantizer of colormap change */ nuclear@2: (*cinfo->cquantize->new_color_map) (cinfo); nuclear@2: master->pub.is_dummy_pass = FALSE; /* just in case */ nuclear@2: } else nuclear@2: ERREXIT(cinfo, JERR_MODE_CHANGE); nuclear@2: } nuclear@2: nuclear@2: #endif /* D_MULTISCAN_FILES_SUPPORTED */ nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Initialize master decompression control and select active modules. nuclear@2: * This is performed at the start of jpeg_start_decompress. nuclear@2: */ nuclear@2: nuclear@2: GLOBAL(void) nuclear@2: jinit_master_decompress (j_decompress_ptr cinfo) nuclear@2: { nuclear@2: my_master_ptr master; nuclear@2: nuclear@2: master = (my_master_ptr) nuclear@2: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@2: SIZEOF(my_decomp_master)); nuclear@2: cinfo->master = (struct jpeg_decomp_master *) master; nuclear@2: master->pub.prepare_for_output_pass = prepare_for_output_pass; nuclear@2: master->pub.finish_output_pass = finish_output_pass; nuclear@2: nuclear@2: master->pub.is_dummy_pass = FALSE; nuclear@2: nuclear@2: master_selection(cinfo); nuclear@2: }