Path: blob/master/thirdparty/libjpeg-turbo/src/jdmaster.c
9904 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* Lossless JPEG Modifications:7* Copyright (C) 1999, Ken Murchison.8* libjpeg-turbo Modifications:9* Copyright (C) 2009-2011, 2016, 2019, 2022-2024, D. R. Commander.10* Copyright (C) 2013, Linaro Limited.11* Copyright (C) 2015, Google, Inc.12* For conditions of distribution and use, see the accompanying README.ijg13* file.14*15* This file contains master control logic for the JPEG decompressor.16* These routines are concerned with selecting the modules to be executed17* and with determining the number of passes and the work to be done in each18* pass.19*/2021#define JPEG_INTERNALS22#include "jinclude.h"23#include "jpeglib.h"24#include "jpegapicomp.h"25#include "jdmaster.h"262728/*29* Determine whether merged upsample/color conversion should be used.30* CRUCIAL: this must match the actual capabilities of jdmerge.c!31*/3233LOCAL(boolean)34use_merged_upsample(j_decompress_ptr cinfo)35{36#ifdef UPSAMPLE_MERGING_SUPPORTED37/* Colorspace conversion is not supported with lossless JPEG images */38if (cinfo->master->lossless)39return FALSE;40/* Merging is the equivalent of plain box-filter upsampling */41if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)42return FALSE;43/* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */44if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||45(cinfo->out_color_space != JCS_RGB &&46cinfo->out_color_space != JCS_RGB565 &&47cinfo->out_color_space != JCS_EXT_RGB &&48cinfo->out_color_space != JCS_EXT_RGBX &&49cinfo->out_color_space != JCS_EXT_BGR &&50cinfo->out_color_space != JCS_EXT_BGRX &&51cinfo->out_color_space != JCS_EXT_XBGR &&52cinfo->out_color_space != JCS_EXT_XRGB &&53cinfo->out_color_space != JCS_EXT_RGBA &&54cinfo->out_color_space != JCS_EXT_BGRA &&55cinfo->out_color_space != JCS_EXT_ABGR &&56cinfo->out_color_space != JCS_EXT_ARGB))57return FALSE;58if ((cinfo->out_color_space == JCS_RGB565 &&59cinfo->out_color_components != 3) ||60(cinfo->out_color_space != JCS_RGB565 &&61cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))62return FALSE;63/* and it only handles 2h1v or 2h2v sampling ratios */64if (cinfo->comp_info[0].h_samp_factor != 2 ||65cinfo->comp_info[1].h_samp_factor != 1 ||66cinfo->comp_info[2].h_samp_factor != 1 ||67cinfo->comp_info[0].v_samp_factor > 2 ||68cinfo->comp_info[1].v_samp_factor != 1 ||69cinfo->comp_info[2].v_samp_factor != 1)70return FALSE;71/* furthermore, it doesn't work if we've scaled the IDCTs differently */72if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||73cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||74cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)75return FALSE;76/* ??? also need to test for upsample-time rescaling, when & if supported */77return TRUE; /* by golly, it'll work... */78#else79return FALSE;80#endif81}828384/*85* Compute output image dimensions and related values.86* NOTE: this is exported for possible use by application.87* Hence it mustn't do anything that can't be done twice.88*/8990#if JPEG_LIB_VERSION >= 8091GLOBAL(void)92#else93LOCAL(void)94#endif95jpeg_core_output_dimensions(j_decompress_ptr cinfo)96/* Do computations that are needed before master selection phase.97* This function is used for transcoding and full decompression.98*/99{100#ifdef IDCT_SCALING_SUPPORTED101int ci;102jpeg_component_info *compptr;103104if (!cinfo->master->lossless) {105/* Compute actual output image dimensions and DCT scaling choices. */106if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {107/* Provide 1/block_size scaling */108cinfo->output_width = (JDIMENSION)109jdiv_round_up((long)cinfo->image_width, (long)DCTSIZE);110cinfo->output_height = (JDIMENSION)111jdiv_round_up((long)cinfo->image_height, (long)DCTSIZE);112cinfo->_min_DCT_h_scaled_size = 1;113cinfo->_min_DCT_v_scaled_size = 1;114} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {115/* Provide 2/block_size scaling */116cinfo->output_width = (JDIMENSION)117jdiv_round_up((long)cinfo->image_width * 2L, (long)DCTSIZE);118cinfo->output_height = (JDIMENSION)119jdiv_round_up((long)cinfo->image_height * 2L, (long)DCTSIZE);120cinfo->_min_DCT_h_scaled_size = 2;121cinfo->_min_DCT_v_scaled_size = 2;122} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {123/* Provide 3/block_size scaling */124cinfo->output_width = (JDIMENSION)125jdiv_round_up((long)cinfo->image_width * 3L, (long)DCTSIZE);126cinfo->output_height = (JDIMENSION)127jdiv_round_up((long)cinfo->image_height * 3L, (long)DCTSIZE);128cinfo->_min_DCT_h_scaled_size = 3;129cinfo->_min_DCT_v_scaled_size = 3;130} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {131/* Provide 4/block_size scaling */132cinfo->output_width = (JDIMENSION)133jdiv_round_up((long)cinfo->image_width * 4L, (long)DCTSIZE);134cinfo->output_height = (JDIMENSION)135jdiv_round_up((long)cinfo->image_height * 4L, (long)DCTSIZE);136cinfo->_min_DCT_h_scaled_size = 4;137cinfo->_min_DCT_v_scaled_size = 4;138} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {139/* Provide 5/block_size scaling */140cinfo->output_width = (JDIMENSION)141jdiv_round_up((long)cinfo->image_width * 5L, (long)DCTSIZE);142cinfo->output_height = (JDIMENSION)143jdiv_round_up((long)cinfo->image_height * 5L, (long)DCTSIZE);144cinfo->_min_DCT_h_scaled_size = 5;145cinfo->_min_DCT_v_scaled_size = 5;146} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {147/* Provide 6/block_size scaling */148cinfo->output_width = (JDIMENSION)149jdiv_round_up((long)cinfo->image_width * 6L, (long)DCTSIZE);150cinfo->output_height = (JDIMENSION)151jdiv_round_up((long)cinfo->image_height * 6L, (long)DCTSIZE);152cinfo->_min_DCT_h_scaled_size = 6;153cinfo->_min_DCT_v_scaled_size = 6;154} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {155/* Provide 7/block_size scaling */156cinfo->output_width = (JDIMENSION)157jdiv_round_up((long)cinfo->image_width * 7L, (long)DCTSIZE);158cinfo->output_height = (JDIMENSION)159jdiv_round_up((long)cinfo->image_height * 7L, (long)DCTSIZE);160cinfo->_min_DCT_h_scaled_size = 7;161cinfo->_min_DCT_v_scaled_size = 7;162} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {163/* Provide 8/block_size scaling */164cinfo->output_width = (JDIMENSION)165jdiv_round_up((long)cinfo->image_width * 8L, (long)DCTSIZE);166cinfo->output_height = (JDIMENSION)167jdiv_round_up((long)cinfo->image_height * 8L, (long)DCTSIZE);168cinfo->_min_DCT_h_scaled_size = 8;169cinfo->_min_DCT_v_scaled_size = 8;170} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {171/* Provide 9/block_size scaling */172cinfo->output_width = (JDIMENSION)173jdiv_round_up((long)cinfo->image_width * 9L, (long)DCTSIZE);174cinfo->output_height = (JDIMENSION)175jdiv_round_up((long)cinfo->image_height * 9L, (long)DCTSIZE);176cinfo->_min_DCT_h_scaled_size = 9;177cinfo->_min_DCT_v_scaled_size = 9;178} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {179/* Provide 10/block_size scaling */180cinfo->output_width = (JDIMENSION)181jdiv_round_up((long)cinfo->image_width * 10L, (long)DCTSIZE);182cinfo->output_height = (JDIMENSION)183jdiv_round_up((long)cinfo->image_height * 10L, (long)DCTSIZE);184cinfo->_min_DCT_h_scaled_size = 10;185cinfo->_min_DCT_v_scaled_size = 10;186} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {187/* Provide 11/block_size scaling */188cinfo->output_width = (JDIMENSION)189jdiv_round_up((long)cinfo->image_width * 11L, (long)DCTSIZE);190cinfo->output_height = (JDIMENSION)191jdiv_round_up((long)cinfo->image_height * 11L, (long)DCTSIZE);192cinfo->_min_DCT_h_scaled_size = 11;193cinfo->_min_DCT_v_scaled_size = 11;194} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {195/* Provide 12/block_size scaling */196cinfo->output_width = (JDIMENSION)197jdiv_round_up((long)cinfo->image_width * 12L, (long)DCTSIZE);198cinfo->output_height = (JDIMENSION)199jdiv_round_up((long)cinfo->image_height * 12L, (long)DCTSIZE);200cinfo->_min_DCT_h_scaled_size = 12;201cinfo->_min_DCT_v_scaled_size = 12;202} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {203/* Provide 13/block_size scaling */204cinfo->output_width = (JDIMENSION)205jdiv_round_up((long)cinfo->image_width * 13L, (long)DCTSIZE);206cinfo->output_height = (JDIMENSION)207jdiv_round_up((long)cinfo->image_height * 13L, (long)DCTSIZE);208cinfo->_min_DCT_h_scaled_size = 13;209cinfo->_min_DCT_v_scaled_size = 13;210} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {211/* Provide 14/block_size scaling */212cinfo->output_width = (JDIMENSION)213jdiv_round_up((long)cinfo->image_width * 14L, (long)DCTSIZE);214cinfo->output_height = (JDIMENSION)215jdiv_round_up((long)cinfo->image_height * 14L, (long)DCTSIZE);216cinfo->_min_DCT_h_scaled_size = 14;217cinfo->_min_DCT_v_scaled_size = 14;218} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {219/* Provide 15/block_size scaling */220cinfo->output_width = (JDIMENSION)221jdiv_round_up((long)cinfo->image_width * 15L, (long)DCTSIZE);222cinfo->output_height = (JDIMENSION)223jdiv_round_up((long)cinfo->image_height * 15L, (long)DCTSIZE);224cinfo->_min_DCT_h_scaled_size = 15;225cinfo->_min_DCT_v_scaled_size = 15;226} else {227/* Provide 16/block_size scaling */228cinfo->output_width = (JDIMENSION)229jdiv_round_up((long)cinfo->image_width * 16L, (long)DCTSIZE);230cinfo->output_height = (JDIMENSION)231jdiv_round_up((long)cinfo->image_height * 16L, (long)DCTSIZE);232cinfo->_min_DCT_h_scaled_size = 16;233cinfo->_min_DCT_v_scaled_size = 16;234}235236/* Recompute dimensions of components */237for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;238ci++, compptr++) {239compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;240compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;241}242} else243#endif /* !IDCT_SCALING_SUPPORTED */244{245/* Hardwire it to "no scaling" */246cinfo->output_width = cinfo->image_width;247cinfo->output_height = cinfo->image_height;248/* jdinput.c has already initialized DCT_scaled_size,249* and has computed unscaled downsampled_width and downsampled_height.250*/251}252}253254255/*256* Compute output image dimensions and related values.257* NOTE: this is exported for possible use by application.258* Hence it mustn't do anything that can't be done twice.259* Also note that it may be called before the master module is initialized!260*/261262GLOBAL(void)263jpeg_calc_output_dimensions(j_decompress_ptr cinfo)264/* Do computations that are needed before master selection phase */265{266#ifdef IDCT_SCALING_SUPPORTED267int ci;268jpeg_component_info *compptr;269#endif270271/* Prevent application from calling me at wrong times */272if (cinfo->global_state != DSTATE_READY)273ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);274275/* Compute core output image dimensions and DCT scaling choices. */276jpeg_core_output_dimensions(cinfo);277278#ifdef IDCT_SCALING_SUPPORTED279280if (!cinfo->master->lossless) {281/* In selecting the actual DCT scaling for each component, we try to282* scale up the chroma components via IDCT scaling rather than upsampling.283* This saves time if the upsampler gets to use 1:1 scaling.284* Note this code adapts subsampling ratios which are powers of 2.285*/286for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;287ci++, compptr++) {288int ssize = cinfo->_min_DCT_scaled_size;289while (ssize < DCTSIZE &&290((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %291(compptr->h_samp_factor * ssize * 2) == 0) &&292((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %293(compptr->v_samp_factor * ssize * 2) == 0)) {294ssize = ssize * 2;295}296#if JPEG_LIB_VERSION >= 70297compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;298#else299compptr->DCT_scaled_size = ssize;300#endif301}302303/* Recompute downsampled dimensions of components;304* application needs to know these if using raw downsampled data.305*/306for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;307ci++, compptr++) {308/* Size in samples, after IDCT scaling */309compptr->downsampled_width = (JDIMENSION)310jdiv_round_up((long)cinfo->image_width *311(long)(compptr->h_samp_factor *312compptr->_DCT_scaled_size),313(long)(cinfo->max_h_samp_factor * DCTSIZE));314compptr->downsampled_height = (JDIMENSION)315jdiv_round_up((long)cinfo->image_height *316(long)(compptr->v_samp_factor *317compptr->_DCT_scaled_size),318(long)(cinfo->max_v_samp_factor * DCTSIZE));319}320} else321#endif /* IDCT_SCALING_SUPPORTED */322{323/* Hardwire it to "no scaling" */324cinfo->output_width = cinfo->image_width;325cinfo->output_height = cinfo->image_height;326/* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,327* and has computed unscaled downsampled_width and downsampled_height.328*/329}330331/* Report number of components in selected colorspace. */332/* Probably this should be in the color conversion module... */333switch (cinfo->out_color_space) {334case JCS_GRAYSCALE:335cinfo->out_color_components = 1;336break;337case JCS_RGB:338case JCS_EXT_RGB:339case JCS_EXT_RGBX:340case JCS_EXT_BGR:341case JCS_EXT_BGRX:342case JCS_EXT_XBGR:343case JCS_EXT_XRGB:344case JCS_EXT_RGBA:345case JCS_EXT_BGRA:346case JCS_EXT_ABGR:347case JCS_EXT_ARGB:348cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];349break;350case JCS_YCbCr:351case JCS_RGB565:352cinfo->out_color_components = 3;353break;354case JCS_CMYK:355case JCS_YCCK:356cinfo->out_color_components = 4;357break;358default: /* else must be same colorspace as in file */359cinfo->out_color_components = cinfo->num_components;360break;361}362cinfo->output_components = (cinfo->quantize_colors ? 1 :363cinfo->out_color_components);364365/* See if upsampler will want to emit more than one row at a time */366if (use_merged_upsample(cinfo))367cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;368else369cinfo->rec_outbuf_height = 1;370}371372373/*374* Several decompression processes need to range-limit values to the range375* 0..MAXJSAMPLE; the input value may fall somewhat outside this range376* due to noise introduced by quantization, roundoff error, etc. These377* processes are inner loops and need to be as fast as possible. On most378* machines, particularly CPUs with pipelines or instruction prefetch,379* a (subscript-check-less) C table lookup380* x = sample_range_limit[x];381* is faster than explicit tests382* if (x < 0) x = 0;383* else if (x > MAXJSAMPLE) x = MAXJSAMPLE;384* These processes all use a common table prepared by the routine below.385*386* For most steps we can mathematically guarantee that the initial value387* of x is within MAXJSAMPLE+1 of the legal range, so a table running from388* -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial389* limiting step (just after the IDCT), a wildly out-of-range value is390* possible if the input data is corrupt. To avoid any chance of indexing391* off the end of memory and getting a bad-pointer trap, we perform the392* post-IDCT limiting thus:393* x = range_limit[x & MASK];394* where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit395* samples. Under normal circumstances this is more than enough range and396* a correct output will be generated; with bogus input data the mask will397* cause wraparound, and we will safely generate a bogus-but-in-range output.398* For the post-IDCT step, we want to convert the data from signed to unsigned399* representation by adding CENTERJSAMPLE at the same time that we limit it.400* So the post-IDCT limiting table ends up looking like this:401* CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,402* MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),403* 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),404* 0,1,...,CENTERJSAMPLE-1405* Negative inputs select values from the upper half of the table after406* masking.407*408* We can save some space by overlapping the start of the post-IDCT table409* with the simpler range limiting table. The post-IDCT table begins at410* sample_range_limit + CENTERJSAMPLE.411*/412413LOCAL(void)414prepare_range_limit_table(j_decompress_ptr cinfo)415/* Allocate and fill in the sample_range_limit table */416{417JSAMPLE *table;418J12SAMPLE *table12;419#ifdef D_LOSSLESS_SUPPORTED420J16SAMPLE *table16;421#endif422int i;423424if (cinfo->data_precision <= 8) {425table = (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 */431memset(table - (MAXJSAMPLE + 1), 0, (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 */440memset(table + (2 * (MAXJSAMPLE + 1)), 0,441(2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * sizeof(JSAMPLE));442memcpy(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),443cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));444} else if (cinfo->data_precision <= 12) {445table12 = (J12SAMPLE *)446(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,447(5 * (MAXJ12SAMPLE + 1) + CENTERJ12SAMPLE) *448sizeof(J12SAMPLE));449table12 += (MAXJ12SAMPLE + 1); /* allow negative subscripts of simple450table */451cinfo->sample_range_limit = (JSAMPLE *)table12;452/* First segment of "simple" table: limit[x] = 0 for x < 0 */453memset(table12 - (MAXJ12SAMPLE + 1), 0,454(MAXJ12SAMPLE + 1) * sizeof(J12SAMPLE));455/* Main part of "simple" table: limit[x] = x */456for (i = 0; i <= MAXJ12SAMPLE; i++)457table12[i] = (J12SAMPLE)i;458table12 += CENTERJ12SAMPLE; /* Point to where post-IDCT table starts */459/* End of simple table, rest of first half of post-IDCT table */460for (i = CENTERJ12SAMPLE; i < 2 * (MAXJ12SAMPLE + 1); i++)461table12[i] = MAXJ12SAMPLE;462/* Second half of post-IDCT table */463memset(table12 + (2 * (MAXJ12SAMPLE + 1)), 0,464(2 * (MAXJ12SAMPLE + 1) - CENTERJ12SAMPLE) * sizeof(J12SAMPLE));465memcpy(table12 + (4 * (MAXJ12SAMPLE + 1) - CENTERJ12SAMPLE),466cinfo->sample_range_limit, CENTERJ12SAMPLE * sizeof(J12SAMPLE));467} else {468#ifdef D_LOSSLESS_SUPPORTED469table16 = (J16SAMPLE *)470(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,471(5 * (MAXJ16SAMPLE + 1) + CENTERJ16SAMPLE) *472sizeof(J16SAMPLE));473table16 += (MAXJ16SAMPLE + 1); /* allow negative subscripts of simple474table */475cinfo->sample_range_limit = (JSAMPLE *)table16;476/* First segment of "simple" table: limit[x] = 0 for x < 0 */477memset(table16 - (MAXJ16SAMPLE + 1), 0,478(MAXJ16SAMPLE + 1) * sizeof(J16SAMPLE));479/* Main part of "simple" table: limit[x] = x */480for (i = 0; i <= MAXJ16SAMPLE; i++)481table16[i] = (J16SAMPLE)i;482table16 += CENTERJ16SAMPLE; /* Point to where post-IDCT table starts */483/* End of simple table, rest of first half of post-IDCT table */484for (i = CENTERJ16SAMPLE; i < 2 * (MAXJ16SAMPLE + 1); i++)485table16[i] = MAXJ16SAMPLE;486/* Second half of post-IDCT table */487memset(table16 + (2 * (MAXJ16SAMPLE + 1)), 0,488(2 * (MAXJ16SAMPLE + 1) - CENTERJ16SAMPLE) * sizeof(J16SAMPLE));489memcpy(table16 + (4 * (MAXJ16SAMPLE + 1) - CENTERJ16SAMPLE),490cinfo->sample_range_limit, CENTERJ16SAMPLE * sizeof(J16SAMPLE));491#else492ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);493#endif494}495}496497498/*499* Master selection of decompression modules.500* This is done once at jpeg_start_decompress time. We determine501* which modules will be used and give them appropriate initialization calls.502* We also initialize the decompressor input side to begin consuming data.503*504* Since jpeg_read_header has finished, we know what is in the SOF505* and (first) SOS markers. We also have all the application parameter506* settings.507*/508509LOCAL(void)510master_selection(j_decompress_ptr cinfo)511{512my_master_ptr master = (my_master_ptr)cinfo->master;513boolean use_c_buffer;514long samplesperrow;515JDIMENSION jd_samplesperrow;516517/* Disable IDCT scaling and raw (downsampled) data output in lossless mode.518* IDCT scaling is not useful in lossless mode, and it must be disabled in519* order to properly calculate the output dimensions. Raw data output isn't520* particularly useful without subsampling and has not been tested in521* lossless mode.522*/523#ifdef D_LOSSLESS_SUPPORTED524if (cinfo->master->lossless) {525cinfo->raw_data_out = FALSE;526cinfo->scale_num = cinfo->scale_denom = 1;527}528#endif529530/* Initialize dimensions and other stuff */531jpeg_calc_output_dimensions(cinfo);532prepare_range_limit_table(cinfo);533534/* Width of an output scanline must be representable as JDIMENSION. */535samplesperrow = (long)cinfo->output_width *536(long)cinfo->out_color_components;537jd_samplesperrow = (JDIMENSION)samplesperrow;538if ((long)jd_samplesperrow != samplesperrow)539ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);540541/* Initialize my private state */542master->pass_number = 0;543master->using_merged_upsample = use_merged_upsample(cinfo);544545/* Color quantizer selection */546master->quantizer_1pass = NULL;547master->quantizer_2pass = NULL;548/* No mode changes if not using buffered-image mode. */549if (!cinfo->quantize_colors || !cinfo->buffered_image) {550cinfo->enable_1pass_quant = FALSE;551cinfo->enable_external_quant = FALSE;552cinfo->enable_2pass_quant = FALSE;553}554if (cinfo->quantize_colors) {555if (cinfo->raw_data_out)556ERREXIT(cinfo, JERR_NOTIMPL);557/* 2-pass quantizer only works in 3-component color space. */558if (cinfo->out_color_components != 3 ||559cinfo->out_color_space == JCS_RGB565) {560cinfo->enable_1pass_quant = TRUE;561cinfo->enable_external_quant = FALSE;562cinfo->enable_2pass_quant = FALSE;563cinfo->colormap = NULL;564} else if (cinfo->colormap != NULL) {565cinfo->enable_external_quant = TRUE;566} else if (cinfo->two_pass_quantize) {567cinfo->enable_2pass_quant = TRUE;568} else {569cinfo->enable_1pass_quant = TRUE;570}571572if (cinfo->enable_1pass_quant) {573#ifdef QUANT_1PASS_SUPPORTED574if (cinfo->data_precision == 8)575jinit_1pass_quantizer(cinfo);576else if (cinfo->data_precision == 12)577j12init_1pass_quantizer(cinfo);578else579ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);580master->quantizer_1pass = cinfo->cquantize;581#else582ERREXIT(cinfo, JERR_NOT_COMPILED);583#endif584}585586/* We use the 2-pass code to map to external colormaps. */587if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {588#ifdef QUANT_2PASS_SUPPORTED589if (cinfo->data_precision == 8)590jinit_2pass_quantizer(cinfo);591else if (cinfo->data_precision == 12)592j12init_2pass_quantizer(cinfo);593else594ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);595master->quantizer_2pass = cinfo->cquantize;596#else597ERREXIT(cinfo, JERR_NOT_COMPILED);598#endif599}600/* If both quantizers are initialized, the 2-pass one is left active;601* this is necessary for starting with quantization to an external map.602*/603}604605/* Post-processing: in particular, color conversion first */606if (!cinfo->raw_data_out) {607if (master->using_merged_upsample) {608#ifdef UPSAMPLE_MERGING_SUPPORTED609if (cinfo->data_precision == 8)610jinit_merged_upsampler(cinfo); /* does color conversion too */611else if (cinfo->data_precision == 12)612j12init_merged_upsampler(cinfo); /* does color conversion too */613else614ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);615#else616ERREXIT(cinfo, JERR_NOT_COMPILED);617#endif618} else {619if (cinfo->data_precision <= 8) {620jinit_color_deconverter(cinfo);621jinit_upsampler(cinfo);622} else if (cinfo->data_precision <= 12) {623j12init_color_deconverter(cinfo);624j12init_upsampler(cinfo);625} else {626#ifdef D_LOSSLESS_SUPPORTED627j16init_color_deconverter(cinfo);628j16init_upsampler(cinfo);629#else630ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);631#endif632}633}634if (cinfo->data_precision <= 8)635jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);636else if (cinfo->data_precision <= 12)637j12init_d_post_controller(cinfo, cinfo->enable_2pass_quant);638else639#ifdef D_LOSSLESS_SUPPORTED640j16init_d_post_controller(cinfo, cinfo->enable_2pass_quant);641#else642ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);643#endif644}645646if (cinfo->master->lossless) {647#ifdef D_LOSSLESS_SUPPORTED648/* Prediction, sample undifferencing, point transform, and sample size649* scaling650*/651if (cinfo->data_precision <= 8)652jinit_lossless_decompressor(cinfo);653else if (cinfo->data_precision <= 12)654j12init_lossless_decompressor(cinfo);655else656j16init_lossless_decompressor(cinfo);657/* Entropy decoding: either Huffman or arithmetic coding. */658if (cinfo->arith_code) {659ERREXIT(cinfo, JERR_ARITH_NOTIMPL);660} else {661jinit_lhuff_decoder(cinfo);662}663664/* Initialize principal buffer controllers. */665use_c_buffer = cinfo->inputctl->has_multiple_scans ||666cinfo->buffered_image;667if (cinfo->data_precision <= 8)668jinit_d_diff_controller(cinfo, use_c_buffer);669else if (cinfo->data_precision <= 12)670j12init_d_diff_controller(cinfo, use_c_buffer);671else672j16init_d_diff_controller(cinfo, use_c_buffer);673#else674ERREXIT(cinfo, JERR_NOT_COMPILED);675#endif676} else {677/* Inverse DCT */678if (cinfo->data_precision == 8)679jinit_inverse_dct(cinfo);680else if (cinfo->data_precision == 12)681j12init_inverse_dct(cinfo);682else683ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);684/* Entropy decoding: either Huffman or arithmetic coding. */685if (cinfo->arith_code) {686#ifdef D_ARITH_CODING_SUPPORTED687jinit_arith_decoder(cinfo);688#else689ERREXIT(cinfo, JERR_ARITH_NOTIMPL);690#endif691} else {692if (cinfo->progressive_mode) {693#ifdef D_PROGRESSIVE_SUPPORTED694jinit_phuff_decoder(cinfo);695#else696ERREXIT(cinfo, JERR_NOT_COMPILED);697#endif698} else699jinit_huff_decoder(cinfo);700}701702/* Initialize principal buffer controllers. */703use_c_buffer = cinfo->inputctl->has_multiple_scans ||704cinfo->buffered_image;705if (cinfo->data_precision == 12)706j12init_d_coef_controller(cinfo, use_c_buffer);707else708jinit_d_coef_controller(cinfo, use_c_buffer);709}710711if (!cinfo->raw_data_out) {712if (cinfo->data_precision <= 8)713jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);714else if (cinfo->data_precision <= 12)715j12init_d_main_controller(cinfo,716FALSE /* never need full buffer here */);717else718#ifdef D_LOSSLESS_SUPPORTED719j16init_d_main_controller(cinfo,720FALSE /* never need full buffer here */);721#else722ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);723#endif724}725726/* We can now tell the memory manager to allocate virtual arrays. */727(*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);728729/* Initialize input side of decompressor to consume first scan. */730(*cinfo->inputctl->start_input_pass) (cinfo);731732/* Set the first and last iMCU columns to decompress from single-scan images.733* By default, decompress all of the iMCU columns.734*/735cinfo->master->first_iMCU_col = 0;736cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;737cinfo->master->last_good_iMCU_row = 0;738739#ifdef D_MULTISCAN_FILES_SUPPORTED740/* If jpeg_start_decompress will read the whole file, initialize741* progress monitoring appropriately. The input step is counted742* as one pass.743*/744if (cinfo->progress != NULL && !cinfo->buffered_image &&745cinfo->inputctl->has_multiple_scans) {746int nscans;747/* Estimate number of scans to set pass_limit. */748if (cinfo->progressive_mode) {749/* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */750nscans = 2 + 3 * cinfo->num_components;751} else {752/* For a nonprogressive multiscan file, estimate 1 scan per component. */753nscans = cinfo->num_components;754}755cinfo->progress->pass_counter = 0L;756cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;757cinfo->progress->completed_passes = 0;758cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);759/* Count the input pass as done */760master->pass_number++;761}762#endif /* D_MULTISCAN_FILES_SUPPORTED */763}764765766/*767* Per-pass setup.768* This is called at the beginning of each output pass. We determine which769* modules will be active during this pass and give them appropriate770* start_pass calls. We also set is_dummy_pass to indicate whether this771* is a "real" output pass or a dummy pass for color quantization.772* (In the latter case, jdapistd.c will crank the pass to completion.)773*/774775METHODDEF(void)776prepare_for_output_pass(j_decompress_ptr cinfo)777{778my_master_ptr master = (my_master_ptr)cinfo->master;779780if (master->pub.is_dummy_pass) {781#ifdef QUANT_2PASS_SUPPORTED782/* Final pass of 2-pass quantization */783master->pub.is_dummy_pass = FALSE;784(*cinfo->cquantize->start_pass) (cinfo, FALSE);785(*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);786(*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);787#else788ERREXIT(cinfo, JERR_NOT_COMPILED);789#endif /* QUANT_2PASS_SUPPORTED */790} else {791if (cinfo->quantize_colors && cinfo->colormap == NULL) {792/* Select new quantization method */793if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {794cinfo->cquantize = master->quantizer_2pass;795master->pub.is_dummy_pass = TRUE;796} else if (cinfo->enable_1pass_quant) {797cinfo->cquantize = master->quantizer_1pass;798} else {799ERREXIT(cinfo, JERR_MODE_CHANGE);800}801}802(*cinfo->idct->start_pass) (cinfo);803(*cinfo->coef->start_output_pass) (cinfo);804if (!cinfo->raw_data_out) {805if (!master->using_merged_upsample)806(*cinfo->cconvert->start_pass) (cinfo);807(*cinfo->upsample->start_pass) (cinfo);808if (cinfo->quantize_colors)809(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);810(*cinfo->post->start_pass) (cinfo,811(master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));812(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);813}814}815816/* Set up progress monitor's pass info if present */817if (cinfo->progress != NULL) {818cinfo->progress->completed_passes = master->pass_number;819cinfo->progress->total_passes = master->pass_number +820(master->pub.is_dummy_pass ? 2 : 1);821/* In buffered-image mode, we assume one more output pass if EOI not822* yet reached, but no more passes if EOI has been reached.823*/824if (cinfo->buffered_image && !cinfo->inputctl->eoi_reached) {825cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);826}827}828}829830831/*832* Finish up at end of an output pass.833*/834835METHODDEF(void)836finish_output_pass(j_decompress_ptr cinfo)837{838my_master_ptr master = (my_master_ptr)cinfo->master;839840if (cinfo->quantize_colors)841(*cinfo->cquantize->finish_pass) (cinfo);842master->pass_number++;843}844845846#ifdef D_MULTISCAN_FILES_SUPPORTED847848/*849* Switch to a new external colormap between output passes.850*/851852GLOBAL(void)853jpeg_new_colormap(j_decompress_ptr cinfo)854{855my_master_ptr master = (my_master_ptr)cinfo->master;856857/* Prevent application from calling me at wrong times */858if (cinfo->global_state != DSTATE_BUFIMAGE)859ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);860861if (cinfo->quantize_colors && cinfo->enable_external_quant &&862cinfo->colormap != NULL) {863/* Select 2-pass quantizer for external colormap use */864cinfo->cquantize = master->quantizer_2pass;865/* Notify quantizer of colormap change */866(*cinfo->cquantize->new_color_map) (cinfo);867master->pub.is_dummy_pass = FALSE; /* just in case */868} else869ERREXIT(cinfo, JERR_MODE_CHANGE);870}871872#endif /* D_MULTISCAN_FILES_SUPPORTED */873874875/*876* Initialize master decompression control and select active modules.877* This is performed at the start of jpeg_start_decompress.878*/879880GLOBAL(void)881jinit_master_decompress(j_decompress_ptr cinfo)882{883my_master_ptr master = (my_master_ptr)cinfo->master;884885master->pub.prepare_for_output_pass = prepare_for_output_pass;886master->pub.finish_output_pass = finish_output_pass;887888master->pub.is_dummy_pass = FALSE;889master->pub.jinit_upsampler_no_alloc = FALSE;890891master_selection(cinfo);892}893894895