Path: blob/master/3rdparty/libjpeg-turbo/src/jdmaster.c
16337 views
/*1* jdmaster.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1997, Thomas G. Lane.5* Modified 2002-2009 by Guido Vollbeding.6* libjpeg-turbo Modifications:7* Copyright (C) 2009-2011, 2016, D. R. Commander.8* Copyright (C) 2013, Linaro Limited.9* Copyright (C) 2015, Google, Inc.10* For conditions of distribution and use, see the accompanying README.ijg11* file.12*13* This file contains master control logic for the JPEG decompressor.14* These routines are concerned with selecting the modules to be executed15* and with determining the number of passes and the work to be done in each16* pass.17*/1819#define JPEG_INTERNALS20#include "jinclude.h"21#include "jpeglib.h"22#include "jpegcomp.h"23#include "jdmaster.h"24#include "jsimd.h"252627/*28* Determine whether merged upsample/color conversion should be used.29* CRUCIAL: this must match the actual capabilities of jdmerge.c!30*/3132LOCAL(boolean)33use_merged_upsample (j_decompress_ptr cinfo)34{35#ifdef UPSAMPLE_MERGING_SUPPORTED36/* Merging is the equivalent of plain box-filter upsampling */37if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)38return FALSE;39/* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */40if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||41(cinfo->out_color_space != JCS_RGB &&42cinfo->out_color_space != JCS_RGB565 &&43cinfo->out_color_space != JCS_EXT_RGB &&44cinfo->out_color_space != JCS_EXT_RGBX &&45cinfo->out_color_space != JCS_EXT_BGR &&46cinfo->out_color_space != JCS_EXT_BGRX &&47cinfo->out_color_space != JCS_EXT_XBGR &&48cinfo->out_color_space != JCS_EXT_XRGB &&49cinfo->out_color_space != JCS_EXT_RGBA &&50cinfo->out_color_space != JCS_EXT_BGRA &&51cinfo->out_color_space != JCS_EXT_ABGR &&52cinfo->out_color_space != JCS_EXT_ARGB))53return FALSE;54if ((cinfo->out_color_space == JCS_RGB565 &&55cinfo->out_color_components != 3) ||56(cinfo->out_color_space != JCS_RGB565 &&57cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))58return FALSE;59/* and it only handles 2h1v or 2h2v sampling ratios */60if (cinfo->comp_info[0].h_samp_factor != 2 ||61cinfo->comp_info[1].h_samp_factor != 1 ||62cinfo->comp_info[2].h_samp_factor != 1 ||63cinfo->comp_info[0].v_samp_factor > 2 ||64cinfo->comp_info[1].v_samp_factor != 1 ||65cinfo->comp_info[2].v_samp_factor != 1)66return FALSE;67/* furthermore, it doesn't work if we've scaled the IDCTs differently */68if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||69cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||70cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)71return FALSE;72#ifdef WITH_SIMD73/* If YCbCr-to-RGB color conversion is SIMD-accelerated but merged upsampling74isn't, then disabling merged upsampling is likely to be faster when75decompressing YCbCr JPEG images. */76if (!jsimd_can_h2v2_merged_upsample() && !jsimd_can_h2v1_merged_upsample() &&77jsimd_can_ycc_rgb() && cinfo->jpeg_color_space == JCS_YCbCr &&78(cinfo->out_color_space == JCS_RGB ||79(cinfo->out_color_space >= JCS_EXT_RGB &&80cinfo->out_color_space <= JCS_EXT_ARGB)))81return FALSE;82#endif83/* ??? also need to test for upsample-time rescaling, when & if supported */84return TRUE; /* by golly, it'll work... */85#else86return FALSE;87#endif88}899091/*92* Compute output image dimensions and related values.93* NOTE: this is exported for possible use by application.94* Hence it mustn't do anything that can't be done twice.95*/9697#if JPEG_LIB_VERSION >= 8098GLOBAL(void)99#else100LOCAL(void)101#endif102jpeg_core_output_dimensions (j_decompress_ptr cinfo)103/* Do computations that are needed before master selection phase.104* This function is used for transcoding and full decompression.105*/106{107#ifdef IDCT_SCALING_SUPPORTED108int ci;109jpeg_component_info *compptr;110111/* Compute actual output image dimensions and DCT scaling choices. */112if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {113/* Provide 1/block_size scaling */114cinfo->output_width = (JDIMENSION)115jdiv_round_up((long) cinfo->image_width, (long) DCTSIZE);116cinfo->output_height = (JDIMENSION)117jdiv_round_up((long) cinfo->image_height, (long) DCTSIZE);118cinfo->_min_DCT_h_scaled_size = 1;119cinfo->_min_DCT_v_scaled_size = 1;120} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {121/* Provide 2/block_size scaling */122cinfo->output_width = (JDIMENSION)123jdiv_round_up((long) cinfo->image_width * 2L, (long) DCTSIZE);124cinfo->output_height = (JDIMENSION)125jdiv_round_up((long) cinfo->image_height * 2L, (long) DCTSIZE);126cinfo->_min_DCT_h_scaled_size = 2;127cinfo->_min_DCT_v_scaled_size = 2;128} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {129/* Provide 3/block_size scaling */130cinfo->output_width = (JDIMENSION)131jdiv_round_up((long) cinfo->image_width * 3L, (long) DCTSIZE);132cinfo->output_height = (JDIMENSION)133jdiv_round_up((long) cinfo->image_height * 3L, (long) DCTSIZE);134cinfo->_min_DCT_h_scaled_size = 3;135cinfo->_min_DCT_v_scaled_size = 3;136} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {137/* Provide 4/block_size scaling */138cinfo->output_width = (JDIMENSION)139jdiv_round_up((long) cinfo->image_width * 4L, (long) DCTSIZE);140cinfo->output_height = (JDIMENSION)141jdiv_round_up((long) cinfo->image_height * 4L, (long) DCTSIZE);142cinfo->_min_DCT_h_scaled_size = 4;143cinfo->_min_DCT_v_scaled_size = 4;144} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {145/* Provide 5/block_size scaling */146cinfo->output_width = (JDIMENSION)147jdiv_round_up((long) cinfo->image_width * 5L, (long) DCTSIZE);148cinfo->output_height = (JDIMENSION)149jdiv_round_up((long) cinfo->image_height * 5L, (long) DCTSIZE);150cinfo->_min_DCT_h_scaled_size = 5;151cinfo->_min_DCT_v_scaled_size = 5;152} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {153/* Provide 6/block_size scaling */154cinfo->output_width = (JDIMENSION)155jdiv_round_up((long) cinfo->image_width * 6L, (long) DCTSIZE);156cinfo->output_height = (JDIMENSION)157jdiv_round_up((long) cinfo->image_height * 6L, (long) DCTSIZE);158cinfo->_min_DCT_h_scaled_size = 6;159cinfo->_min_DCT_v_scaled_size = 6;160} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {161/* Provide 7/block_size scaling */162cinfo->output_width = (JDIMENSION)163jdiv_round_up((long) cinfo->image_width * 7L, (long) DCTSIZE);164cinfo->output_height = (JDIMENSION)165jdiv_round_up((long) cinfo->image_height * 7L, (long) DCTSIZE);166cinfo->_min_DCT_h_scaled_size = 7;167cinfo->_min_DCT_v_scaled_size = 7;168} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {169/* Provide 8/block_size scaling */170cinfo->output_width = (JDIMENSION)171jdiv_round_up((long) cinfo->image_width * 8L, (long) DCTSIZE);172cinfo->output_height = (JDIMENSION)173jdiv_round_up((long) cinfo->image_height * 8L, (long) DCTSIZE);174cinfo->_min_DCT_h_scaled_size = 8;175cinfo->_min_DCT_v_scaled_size = 8;176} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {177/* Provide 9/block_size scaling */178cinfo->output_width = (JDIMENSION)179jdiv_round_up((long) cinfo->image_width * 9L, (long) DCTSIZE);180cinfo->output_height = (JDIMENSION)181jdiv_round_up((long) cinfo->image_height * 9L, (long) DCTSIZE);182cinfo->_min_DCT_h_scaled_size = 9;183cinfo->_min_DCT_v_scaled_size = 9;184} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {185/* Provide 10/block_size scaling */186cinfo->output_width = (JDIMENSION)187jdiv_round_up((long) cinfo->image_width * 10L, (long) DCTSIZE);188cinfo->output_height = (JDIMENSION)189jdiv_round_up((long) cinfo->image_height * 10L, (long) DCTSIZE);190cinfo->_min_DCT_h_scaled_size = 10;191cinfo->_min_DCT_v_scaled_size = 10;192} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {193/* Provide 11/block_size scaling */194cinfo->output_width = (JDIMENSION)195jdiv_round_up((long) cinfo->image_width * 11L, (long) DCTSIZE);196cinfo->output_height = (JDIMENSION)197jdiv_round_up((long) cinfo->image_height * 11L, (long) DCTSIZE);198cinfo->_min_DCT_h_scaled_size = 11;199cinfo->_min_DCT_v_scaled_size = 11;200} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {201/* Provide 12/block_size scaling */202cinfo->output_width = (JDIMENSION)203jdiv_round_up((long) cinfo->image_width * 12L, (long) DCTSIZE);204cinfo->output_height = (JDIMENSION)205jdiv_round_up((long) cinfo->image_height * 12L, (long) DCTSIZE);206cinfo->_min_DCT_h_scaled_size = 12;207cinfo->_min_DCT_v_scaled_size = 12;208} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {209/* Provide 13/block_size scaling */210cinfo->output_width = (JDIMENSION)211jdiv_round_up((long) cinfo->image_width * 13L, (long) DCTSIZE);212cinfo->output_height = (JDIMENSION)213jdiv_round_up((long) cinfo->image_height * 13L, (long) DCTSIZE);214cinfo->_min_DCT_h_scaled_size = 13;215cinfo->_min_DCT_v_scaled_size = 13;216} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {217/* Provide 14/block_size scaling */218cinfo->output_width = (JDIMENSION)219jdiv_round_up((long) cinfo->image_width * 14L, (long) DCTSIZE);220cinfo->output_height = (JDIMENSION)221jdiv_round_up((long) cinfo->image_height * 14L, (long) DCTSIZE);222cinfo->_min_DCT_h_scaled_size = 14;223cinfo->_min_DCT_v_scaled_size = 14;224} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {225/* Provide 15/block_size scaling */226cinfo->output_width = (JDIMENSION)227jdiv_round_up((long) cinfo->image_width * 15L, (long) DCTSIZE);228cinfo->output_height = (JDIMENSION)229jdiv_round_up((long) cinfo->image_height * 15L, (long) DCTSIZE);230cinfo->_min_DCT_h_scaled_size = 15;231cinfo->_min_DCT_v_scaled_size = 15;232} else {233/* Provide 16/block_size scaling */234cinfo->output_width = (JDIMENSION)235jdiv_round_up((long) cinfo->image_width * 16L, (long) DCTSIZE);236cinfo->output_height = (JDIMENSION)237jdiv_round_up((long) cinfo->image_height * 16L, (long) DCTSIZE);238cinfo->_min_DCT_h_scaled_size = 16;239cinfo->_min_DCT_v_scaled_size = 16;240}241242/* Recompute dimensions of components */243for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;244ci++, compptr++) {245compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;246compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;247}248249#else /* !IDCT_SCALING_SUPPORTED */250251/* Hardwire it to "no scaling" */252cinfo->output_width = cinfo->image_width;253cinfo->output_height = cinfo->image_height;254/* jdinput.c has already initialized DCT_scaled_size,255* and has computed unscaled downsampled_width and downsampled_height.256*/257258#endif /* IDCT_SCALING_SUPPORTED */259}260261262/*263* Compute output image dimensions and related values.264* NOTE: this is exported for possible use by application.265* Hence it mustn't do anything that can't be done twice.266* Also note that it may be called before the master module is initialized!267*/268269GLOBAL(void)270jpeg_calc_output_dimensions (j_decompress_ptr cinfo)271/* Do computations that are needed before master selection phase */272{273#ifdef IDCT_SCALING_SUPPORTED274int ci;275jpeg_component_info *compptr;276#endif277278/* Prevent application from calling me at wrong times */279if (cinfo->global_state != DSTATE_READY)280ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);281282/* Compute core output image dimensions and DCT scaling choices. */283jpeg_core_output_dimensions(cinfo);284285#ifdef IDCT_SCALING_SUPPORTED286287/* In selecting the actual DCT scaling for each component, we try to288* scale up the chroma components via IDCT scaling rather than upsampling.289* This saves time if the upsampler gets to use 1:1 scaling.290* Note this code adapts subsampling ratios which are powers of 2.291*/292for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;293ci++, compptr++) {294int ssize = cinfo->_min_DCT_scaled_size;295while (ssize < DCTSIZE &&296((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %297(compptr->h_samp_factor * ssize * 2) == 0) &&298((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %299(compptr->v_samp_factor * ssize * 2) == 0)) {300ssize = ssize * 2;301}302#if JPEG_LIB_VERSION >= 70303compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;304#else305compptr->DCT_scaled_size = ssize;306#endif307}308309/* Recompute downsampled dimensions of components;310* application needs to know these if using raw downsampled data.311*/312for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;313ci++, compptr++) {314/* Size in samples, after IDCT scaling */315compptr->downsampled_width = (JDIMENSION)316jdiv_round_up((long) cinfo->image_width *317(long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),318(long) (cinfo->max_h_samp_factor * DCTSIZE));319compptr->downsampled_height = (JDIMENSION)320jdiv_round_up((long) cinfo->image_height *321(long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),322(long) (cinfo->max_v_samp_factor * DCTSIZE));323}324325#else /* !IDCT_SCALING_SUPPORTED */326327/* Hardwire it to "no scaling" */328cinfo->output_width = cinfo->image_width;329cinfo->output_height = cinfo->image_height;330/* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,331* and has computed unscaled downsampled_width and downsampled_height.332*/333334#endif /* IDCT_SCALING_SUPPORTED */335336/* Report number of components in selected colorspace. */337/* Probably this should be in the color conversion module... */338switch (cinfo->out_color_space) {339case JCS_GRAYSCALE:340cinfo->out_color_components = 1;341break;342case JCS_RGB:343case JCS_EXT_RGB:344case JCS_EXT_RGBX:345case JCS_EXT_BGR:346case JCS_EXT_BGRX:347case JCS_EXT_XBGR:348case JCS_EXT_XRGB:349case JCS_EXT_RGBA:350case JCS_EXT_BGRA:351case JCS_EXT_ABGR:352case JCS_EXT_ARGB:353cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];354break;355case JCS_YCbCr:356case JCS_RGB565:357cinfo->out_color_components = 3;358break;359case JCS_CMYK:360case JCS_YCCK:361cinfo->out_color_components = 4;362break;363default: /* else must be same colorspace as in file */364cinfo->out_color_components = cinfo->num_components;365break;366}367cinfo->output_components = (cinfo->quantize_colors ? 1 :368cinfo->out_color_components);369370/* See if upsampler will want to emit more than one row at a time */371if (use_merged_upsample(cinfo))372cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;373else374cinfo->rec_outbuf_height = 1;375}376377378/*379* Several decompression processes need to range-limit values to the range380* 0..MAXJSAMPLE; the input value may fall somewhat outside this range381* due to noise introduced by quantization, roundoff error, etc. These382* processes are inner loops and need to be as fast as possible. On most383* machines, particularly CPUs with pipelines or instruction prefetch,384* a (subscript-check-less) C table lookup385* x = sample_range_limit[x];386* is faster than explicit tests387* if (x < 0) x = 0;388* else if (x > MAXJSAMPLE) x = MAXJSAMPLE;389* These processes all use a common table prepared by the routine below.390*391* For most steps we can mathematically guarantee that the initial value392* of x is within MAXJSAMPLE+1 of the legal range, so a table running from393* -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial394* limiting step (just after the IDCT), a wildly out-of-range value is395* possible if the input data is corrupt. To avoid any chance of indexing396* off the end of memory and getting a bad-pointer trap, we perform the397* post-IDCT limiting thus:398* x = range_limit[x & MASK];399* where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit400* samples. Under normal circumstances this is more than enough range and401* a correct output will be generated; with bogus input data the mask will402* cause wraparound, and we will safely generate a bogus-but-in-range output.403* For the post-IDCT step, we want to convert the data from signed to unsigned404* representation by adding CENTERJSAMPLE at the same time that we limit it.405* So the post-IDCT limiting table ends up looking like this:406* CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,407* MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),408* 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),409* 0,1,...,CENTERJSAMPLE-1410* Negative inputs select values from the upper half of the table after411* masking.412*413* We can save some space by overlapping the start of the post-IDCT table414* with the simpler range limiting table. The post-IDCT table begins at415* sample_range_limit + CENTERJSAMPLE.416*/417418LOCAL(void)419prepare_range_limit_table (j_decompress_ptr cinfo)420/* Allocate and fill in the sample_range_limit table */421{422JSAMPLE *table;423int i;424425table = (JSAMPLE *)426(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,427(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * sizeof(JSAMPLE));428table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */429cinfo->sample_range_limit = table;430/* First segment of "simple" table: limit[x] = 0 for x < 0 */431MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * sizeof(JSAMPLE));432/* Main part of "simple" table: limit[x] = x */433for (i = 0; i <= MAXJSAMPLE; i++)434table[i] = (JSAMPLE) i;435table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */436/* End of simple table, rest of first half of post-IDCT table */437for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)438table[i] = MAXJSAMPLE;439/* Second half of post-IDCT table */440MEMZERO(table + (2 * (MAXJSAMPLE+1)),441(2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * sizeof(JSAMPLE));442MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),443cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));444}445446447/*448* Master selection of decompression modules.449* This is done once at jpeg_start_decompress time. We determine450* which modules will be used and give them appropriate initialization calls.451* We also initialize the decompressor input side to begin consuming data.452*453* Since jpeg_read_header has finished, we know what is in the SOF454* and (first) SOS markers. We also have all the application parameter455* settings.456*/457458LOCAL(void)459master_selection (j_decompress_ptr cinfo)460{461my_master_ptr master = (my_master_ptr) cinfo->master;462boolean use_c_buffer;463long samplesperrow;464JDIMENSION jd_samplesperrow;465466/* Initialize dimensions and other stuff */467jpeg_calc_output_dimensions(cinfo);468prepare_range_limit_table(cinfo);469470/* Width of an output scanline must be representable as JDIMENSION. */471samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;472jd_samplesperrow = (JDIMENSION) samplesperrow;473if ((long) jd_samplesperrow != samplesperrow)474ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);475476/* Initialize my private state */477master->pass_number = 0;478master->using_merged_upsample = use_merged_upsample(cinfo);479480/* Color quantizer selection */481master->quantizer_1pass = NULL;482master->quantizer_2pass = NULL;483/* No mode changes if not using buffered-image mode. */484if (! cinfo->quantize_colors || ! cinfo->buffered_image) {485cinfo->enable_1pass_quant = FALSE;486cinfo->enable_external_quant = FALSE;487cinfo->enable_2pass_quant = FALSE;488}489if (cinfo->quantize_colors) {490if (cinfo->raw_data_out)491ERREXIT(cinfo, JERR_NOTIMPL);492/* 2-pass quantizer only works in 3-component color space. */493if (cinfo->out_color_components != 3) {494cinfo->enable_1pass_quant = TRUE;495cinfo->enable_external_quant = FALSE;496cinfo->enable_2pass_quant = FALSE;497cinfo->colormap = NULL;498} else if (cinfo->colormap != NULL) {499cinfo->enable_external_quant = TRUE;500} else if (cinfo->two_pass_quantize) {501cinfo->enable_2pass_quant = TRUE;502} else {503cinfo->enable_1pass_quant = TRUE;504}505506if (cinfo->enable_1pass_quant) {507#ifdef QUANT_1PASS_SUPPORTED508jinit_1pass_quantizer(cinfo);509master->quantizer_1pass = cinfo->cquantize;510#else511ERREXIT(cinfo, JERR_NOT_COMPILED);512#endif513}514515/* We use the 2-pass code to map to external colormaps. */516if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {517#ifdef QUANT_2PASS_SUPPORTED518jinit_2pass_quantizer(cinfo);519master->quantizer_2pass = cinfo->cquantize;520#else521ERREXIT(cinfo, JERR_NOT_COMPILED);522#endif523}524/* If both quantizers are initialized, the 2-pass one is left active;525* this is necessary for starting with quantization to an external map.526*/527}528529/* Post-processing: in particular, color conversion first */530if (! cinfo->raw_data_out) {531if (master->using_merged_upsample) {532#ifdef UPSAMPLE_MERGING_SUPPORTED533jinit_merged_upsampler(cinfo); /* does color conversion too */534#else535ERREXIT(cinfo, JERR_NOT_COMPILED);536#endif537} else {538jinit_color_deconverter(cinfo);539jinit_upsampler(cinfo);540}541jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);542}543/* Inverse DCT */544jinit_inverse_dct(cinfo);545/* Entropy decoding: either Huffman or arithmetic coding. */546if (cinfo->arith_code) {547#ifdef D_ARITH_CODING_SUPPORTED548jinit_arith_decoder(cinfo);549#else550ERREXIT(cinfo, JERR_ARITH_NOTIMPL);551#endif552} else {553if (cinfo->progressive_mode) {554#ifdef D_PROGRESSIVE_SUPPORTED555jinit_phuff_decoder(cinfo);556#else557ERREXIT(cinfo, JERR_NOT_COMPILED);558#endif559} else560jinit_huff_decoder(cinfo);561}562563/* Initialize principal buffer controllers. */564use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;565jinit_d_coef_controller(cinfo, use_c_buffer);566567if (! cinfo->raw_data_out)568jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);569570/* We can now tell the memory manager to allocate virtual arrays. */571(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);572573/* Initialize input side of decompressor to consume first scan. */574(*cinfo->inputctl->start_input_pass) (cinfo);575576/* Set the first and last iMCU columns to decompress from single-scan images.577* By default, decompress all of the iMCU columns.578*/579cinfo->master->first_iMCU_col = 0;580cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;581582#ifdef D_MULTISCAN_FILES_SUPPORTED583/* If jpeg_start_decompress will read the whole file, initialize584* progress monitoring appropriately. The input step is counted585* as one pass.586*/587if (cinfo->progress != NULL && ! cinfo->buffered_image &&588cinfo->inputctl->has_multiple_scans) {589int nscans;590/* Estimate number of scans to set pass_limit. */591if (cinfo->progressive_mode) {592/* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */593nscans = 2 + 3 * cinfo->num_components;594} else {595/* For a nonprogressive multiscan file, estimate 1 scan per component. */596nscans = cinfo->num_components;597}598cinfo->progress->pass_counter = 0L;599cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;600cinfo->progress->completed_passes = 0;601cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);602/* Count the input pass as done */603master->pass_number++;604}605#endif /* D_MULTISCAN_FILES_SUPPORTED */606}607608609/*610* Per-pass setup.611* This is called at the beginning of each output pass. We determine which612* modules will be active during this pass and give them appropriate613* start_pass calls. We also set is_dummy_pass to indicate whether this614* is a "real" output pass or a dummy pass for color quantization.615* (In the latter case, jdapistd.c will crank the pass to completion.)616*/617618METHODDEF(void)619prepare_for_output_pass (j_decompress_ptr cinfo)620{621my_master_ptr master = (my_master_ptr) cinfo->master;622623if (master->pub.is_dummy_pass) {624#ifdef QUANT_2PASS_SUPPORTED625/* Final pass of 2-pass quantization */626master->pub.is_dummy_pass = FALSE;627(*cinfo->cquantize->start_pass) (cinfo, FALSE);628(*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);629(*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);630#else631ERREXIT(cinfo, JERR_NOT_COMPILED);632#endif /* QUANT_2PASS_SUPPORTED */633} else {634if (cinfo->quantize_colors && cinfo->colormap == NULL) {635/* Select new quantization method */636if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {637cinfo->cquantize = master->quantizer_2pass;638master->pub.is_dummy_pass = TRUE;639} else if (cinfo->enable_1pass_quant) {640cinfo->cquantize = master->quantizer_1pass;641} else {642ERREXIT(cinfo, JERR_MODE_CHANGE);643}644}645(*cinfo->idct->start_pass) (cinfo);646(*cinfo->coef->start_output_pass) (cinfo);647if (! cinfo->raw_data_out) {648if (! master->using_merged_upsample)649(*cinfo->cconvert->start_pass) (cinfo);650(*cinfo->upsample->start_pass) (cinfo);651if (cinfo->quantize_colors)652(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);653(*cinfo->post->start_pass) (cinfo,654(master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));655(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);656}657}658659/* Set up progress monitor's pass info if present */660if (cinfo->progress != NULL) {661cinfo->progress->completed_passes = master->pass_number;662cinfo->progress->total_passes = master->pass_number +663(master->pub.is_dummy_pass ? 2 : 1);664/* In buffered-image mode, we assume one more output pass if EOI not665* yet reached, but no more passes if EOI has been reached.666*/667if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {668cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);669}670}671}672673674/*675* Finish up at end of an output pass.676*/677678METHODDEF(void)679finish_output_pass (j_decompress_ptr cinfo)680{681my_master_ptr master = (my_master_ptr) cinfo->master;682683if (cinfo->quantize_colors)684(*cinfo->cquantize->finish_pass) (cinfo);685master->pass_number++;686}687688689#ifdef D_MULTISCAN_FILES_SUPPORTED690691/*692* Switch to a new external colormap between output passes.693*/694695GLOBAL(void)696jpeg_new_colormap (j_decompress_ptr cinfo)697{698my_master_ptr master = (my_master_ptr) cinfo->master;699700/* Prevent application from calling me at wrong times */701if (cinfo->global_state != DSTATE_BUFIMAGE)702ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);703704if (cinfo->quantize_colors && cinfo->enable_external_quant &&705cinfo->colormap != NULL) {706/* Select 2-pass quantizer for external colormap use */707cinfo->cquantize = master->quantizer_2pass;708/* Notify quantizer of colormap change */709(*cinfo->cquantize->new_color_map) (cinfo);710master->pub.is_dummy_pass = FALSE; /* just in case */711} else712ERREXIT(cinfo, JERR_MODE_CHANGE);713}714715#endif /* D_MULTISCAN_FILES_SUPPORTED */716717718/*719* Initialize master decompression control and select active modules.720* This is performed at the start of jpeg_start_decompress.721*/722723GLOBAL(void)724jinit_master_decompress (j_decompress_ptr cinfo)725{726my_master_ptr master = (my_master_ptr) cinfo->master;727728master->pub.prepare_for_output_pass = prepare_for_output_pass;729master->pub.finish_output_pass = finish_output_pass;730731master->pub.is_dummy_pass = FALSE;732master->pub.jinit_upsampler_no_alloc = FALSE;733734master_selection(cinfo);735}736737738