Path: blob/master/src/java.desktop/share/native/libjavajpeg/jdmaster.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jdmaster.c6*7* Copyright (C) 1991-1997, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains master control logic for the JPEG decompressor.12* These routines are concerned with selecting the modules to be executed13* and with determining the number of passes and the work to be done in each14* pass.15*/1617#define JPEG_INTERNALS18#include "jinclude.h"19#include "jpeglib.h"202122/* Private state */2324typedef struct {25struct jpeg_decomp_master pub; /* public fields */2627int pass_number; /* # of passes completed */2829boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */3031/* Saved references to initialized quantizer modules,32* in case we need to switch modes.33*/34struct jpeg_color_quantizer * quantizer_1pass;35struct jpeg_color_quantizer * quantizer_2pass;36} my_decomp_master;3738typedef my_decomp_master * my_master_ptr;394041/*42* Determine whether merged upsample/color conversion should be used.43* CRUCIAL: this must match the actual capabilities of jdmerge.c!44*/4546LOCAL(boolean)47use_merged_upsample (j_decompress_ptr cinfo)48{49#ifdef UPSAMPLE_MERGING_SUPPORTED50/* Merging is the equivalent of plain box-filter upsampling */51if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)52return FALSE;53/* jdmerge.c only supports YCC=>RGB color conversion */54if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||55cinfo->out_color_space != JCS_RGB ||56cinfo->out_color_components != RGB_PIXELSIZE)57return FALSE;58/* and it only handles 2h1v or 2h2v sampling ratios */59if (cinfo->comp_info[0].h_samp_factor != 2 ||60cinfo->comp_info[1].h_samp_factor != 1 ||61cinfo->comp_info[2].h_samp_factor != 1 ||62cinfo->comp_info[0].v_samp_factor > 2 ||63cinfo->comp_info[1].v_samp_factor != 1 ||64cinfo->comp_info[2].v_samp_factor != 1)65return FALSE;66/* furthermore, it doesn't work if we've scaled the IDCTs differently */67if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||68cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||69cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)70return FALSE;71/* ??? also need to test for upsample-time rescaling, when & if supported */72return TRUE; /* by golly, it'll work... */73#else74return FALSE;75#endif76}777879/*80* Compute output image dimensions and related values.81* NOTE: this is exported for possible use by application.82* Hence it mustn't do anything that can't be done twice.83* Also note that it may be called before the master module is initialized!84*/8586GLOBAL(void)87jpeg_calc_output_dimensions (j_decompress_ptr cinfo)88/* Do computations that are needed before master selection phase */89{90#ifdef IDCT_SCALING_SUPPORTED91int ci;92jpeg_component_info *compptr;93#endif9495/* Prevent application from calling me at wrong times */96if (cinfo->global_state != DSTATE_READY)97ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);9899#ifdef IDCT_SCALING_SUPPORTED100101/* Compute actual output image dimensions and DCT scaling choices. */102if (cinfo->scale_num * 8 <= cinfo->scale_denom) {103/* Provide 1/8 scaling */104cinfo->output_width = (JDIMENSION)105jdiv_round_up((long) cinfo->image_width, 8L);106cinfo->output_height = (JDIMENSION)107jdiv_round_up((long) cinfo->image_height, 8L);108cinfo->min_DCT_scaled_size = 1;109} else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {110/* Provide 1/4 scaling */111cinfo->output_width = (JDIMENSION)112jdiv_round_up((long) cinfo->image_width, 4L);113cinfo->output_height = (JDIMENSION)114jdiv_round_up((long) cinfo->image_height, 4L);115cinfo->min_DCT_scaled_size = 2;116} else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {117/* Provide 1/2 scaling */118cinfo->output_width = (JDIMENSION)119jdiv_round_up((long) cinfo->image_width, 2L);120cinfo->output_height = (JDIMENSION)121jdiv_round_up((long) cinfo->image_height, 2L);122cinfo->min_DCT_scaled_size = 4;123} else {124/* Provide 1/1 scaling */125cinfo->output_width = cinfo->image_width;126cinfo->output_height = cinfo->image_height;127cinfo->min_DCT_scaled_size = DCTSIZE;128}129/* In selecting the actual DCT scaling for each component, we try to130* scale up the chroma components via IDCT scaling rather than upsampling.131* This saves time if the upsampler gets to use 1:1 scaling.132* Note this code assumes that the supported DCT scalings are powers of 2.133*/134for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;135ci++, compptr++) {136int ssize = cinfo->min_DCT_scaled_size;137while (ssize < DCTSIZE &&138(compptr->h_samp_factor * ssize * 2 <=139cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&140(compptr->v_samp_factor * ssize * 2 <=141cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {142ssize = ssize * 2;143}144compptr->DCT_scaled_size = ssize;145}146147/* Recompute downsampled dimensions of components;148* application needs to know these if using raw downsampled data.149*/150for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;151ci++, compptr++) {152/* Size in samples, after IDCT scaling */153compptr->downsampled_width = (JDIMENSION)154jdiv_round_up((long) cinfo->image_width *155(long) (compptr->h_samp_factor * compptr->DCT_scaled_size),156(long) (cinfo->max_h_samp_factor * DCTSIZE));157compptr->downsampled_height = (JDIMENSION)158jdiv_round_up((long) cinfo->image_height *159(long) (compptr->v_samp_factor * compptr->DCT_scaled_size),160(long) (cinfo->max_v_samp_factor * DCTSIZE));161}162163#else /* !IDCT_SCALING_SUPPORTED */164165/* Hardwire it to "no scaling" */166cinfo->output_width = cinfo->image_width;167cinfo->output_height = cinfo->image_height;168/* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,169* and has computed unscaled downsampled_width and downsampled_height.170*/171172#endif /* IDCT_SCALING_SUPPORTED */173174/* Report number of components in selected colorspace. */175/* Probably this should be in the color conversion module... */176switch (cinfo->out_color_space) {177case JCS_GRAYSCALE:178cinfo->out_color_components = 1;179break;180case JCS_RGB:181#if RGB_PIXELSIZE != 3182cinfo->out_color_components = RGB_PIXELSIZE;183break;184#endif /* else share code with YCbCr */185case JCS_YCbCr:186cinfo->out_color_components = 3;187break;188case JCS_CMYK:189case JCS_YCCK:190cinfo->out_color_components = 4;191break;192default: /* else must be same colorspace as in file */193cinfo->out_color_components = cinfo->num_components;194break;195}196cinfo->output_components = (cinfo->quantize_colors ? 1 :197cinfo->out_color_components);198199/* See if upsampler will want to emit more than one row at a time */200if (use_merged_upsample(cinfo))201cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;202else203cinfo->rec_outbuf_height = 1;204}205206207/*208* Several decompression processes need to range-limit values to the range209* 0..MAXJSAMPLE; the input value may fall somewhat outside this range210* due to noise introduced by quantization, roundoff error, etc. These211* processes are inner loops and need to be as fast as possible. On most212* machines, particularly CPUs with pipelines or instruction prefetch,213* a (subscript-check-less) C table lookup214* x = sample_range_limit[x];215* is faster than explicit tests216* if (x < 0) x = 0;217* else if (x > MAXJSAMPLE) x = MAXJSAMPLE;218* These processes all use a common table prepared by the routine below.219*220* For most steps we can mathematically guarantee that the initial value221* of x is within MAXJSAMPLE+1 of the legal range, so a table running from222* -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial223* limiting step (just after the IDCT), a wildly out-of-range value is224* possible if the input data is corrupt. To avoid any chance of indexing225* off the end of memory and getting a bad-pointer trap, we perform the226* post-IDCT limiting thus:227* x = range_limit[x & MASK];228* where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit229* samples. Under normal circumstances this is more than enough range and230* a correct output will be generated; with bogus input data the mask will231* cause wraparound, and we will safely generate a bogus-but-in-range output.232* For the post-IDCT step, we want to convert the data from signed to unsigned233* representation by adding CENTERJSAMPLE at the same time that we limit it.234* So the post-IDCT limiting table ends up looking like this:235* CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,236* MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),237* 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),238* 0,1,...,CENTERJSAMPLE-1239* Negative inputs select values from the upper half of the table after240* masking.241*242* We can save some space by overlapping the start of the post-IDCT table243* with the simpler range limiting table. The post-IDCT table begins at244* sample_range_limit + CENTERJSAMPLE.245*246* Note that the table is allocated in near data space on PCs; it's small247* enough and used often enough to justify this.248*/249250LOCAL(void)251prepare_range_limit_table (j_decompress_ptr cinfo)252/* Allocate and fill in the sample_range_limit table */253{254JSAMPLE * table;255int i;256257table = (JSAMPLE *)258(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,259(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));260table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */261cinfo->sample_range_limit = table;262/* First segment of "simple" table: limit[x] = 0 for x < 0 */263MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));264/* Main part of "simple" table: limit[x] = x */265for (i = 0; i <= MAXJSAMPLE; i++)266table[i] = (JSAMPLE) i;267table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */268/* End of simple table, rest of first half of post-IDCT table */269for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)270table[i] = MAXJSAMPLE;271/* Second half of post-IDCT table */272MEMZERO(table + (2 * (MAXJSAMPLE+1)),273(2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));274MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),275cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));276}277278279/*280* Master selection of decompression modules.281* This is done once at jpeg_start_decompress time. We determine282* which modules will be used and give them appropriate initialization calls.283* We also initialize the decompressor input side to begin consuming data.284*285* Since jpeg_read_header has finished, we know what is in the SOF286* and (first) SOS markers. We also have all the application parameter287* settings.288*/289290LOCAL(void)291master_selection (j_decompress_ptr cinfo)292{293my_master_ptr master = (my_master_ptr) cinfo->master;294boolean use_c_buffer;295long samplesperrow;296JDIMENSION jd_samplesperrow;297298/* Initialize dimensions and other stuff */299jpeg_calc_output_dimensions(cinfo);300prepare_range_limit_table(cinfo);301302/* Width of an output scanline must be representable as JDIMENSION. */303samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;304jd_samplesperrow = (JDIMENSION) samplesperrow;305if ((long) jd_samplesperrow != samplesperrow)306ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);307308/* Initialize my private state */309master->pass_number = 0;310master->using_merged_upsample = use_merged_upsample(cinfo);311312/* Color quantizer selection */313master->quantizer_1pass = NULL;314master->quantizer_2pass = NULL;315/* No mode changes if not using buffered-image mode. */316if (! cinfo->quantize_colors || ! cinfo->buffered_image) {317cinfo->enable_1pass_quant = FALSE;318cinfo->enable_external_quant = FALSE;319cinfo->enable_2pass_quant = FALSE;320}321if (cinfo->quantize_colors) {322if (cinfo->raw_data_out)323ERREXIT(cinfo, JERR_NOTIMPL);324/* 2-pass quantizer only works in 3-component color space. */325if (cinfo->out_color_components != 3) {326cinfo->enable_1pass_quant = TRUE;327cinfo->enable_external_quant = FALSE;328cinfo->enable_2pass_quant = FALSE;329cinfo->colormap = NULL;330} else if (cinfo->colormap != NULL) {331cinfo->enable_external_quant = TRUE;332} else if (cinfo->two_pass_quantize) {333cinfo->enable_2pass_quant = TRUE;334} else {335cinfo->enable_1pass_quant = TRUE;336}337338if (cinfo->enable_1pass_quant) {339#ifdef QUANT_1PASS_SUPPORTED340jinit_1pass_quantizer(cinfo);341master->quantizer_1pass = cinfo->cquantize;342#else343ERREXIT(cinfo, JERR_NOT_COMPILED);344#endif345}346347/* We use the 2-pass code to map to external colormaps. */348if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {349#ifdef QUANT_2PASS_SUPPORTED350jinit_2pass_quantizer(cinfo);351master->quantizer_2pass = cinfo->cquantize;352#else353ERREXIT(cinfo, JERR_NOT_COMPILED);354#endif355}356/* If both quantizers are initialized, the 2-pass one is left active;357* this is necessary for starting with quantization to an external map.358*/359}360361/* Post-processing: in particular, color conversion first */362if (! cinfo->raw_data_out) {363if (master->using_merged_upsample) {364#ifdef UPSAMPLE_MERGING_SUPPORTED365jinit_merged_upsampler(cinfo); /* does color conversion too */366#else367ERREXIT(cinfo, JERR_NOT_COMPILED);368#endif369} else {370jinit_color_deconverter(cinfo);371jinit_upsampler(cinfo);372}373jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);374}375/* Inverse DCT */376jinit_inverse_dct(cinfo);377/* Entropy decoding: either Huffman or arithmetic coding. */378if (cinfo->arith_code) {379ERREXIT(cinfo, JERR_ARITH_NOTIMPL);380} else {381if (cinfo->progressive_mode) {382#ifdef D_PROGRESSIVE_SUPPORTED383jinit_phuff_decoder(cinfo);384#else385ERREXIT(cinfo, JERR_NOT_COMPILED);386#endif387} else388jinit_huff_decoder(cinfo);389}390391/* Initialize principal buffer controllers. */392use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;393jinit_d_coef_controller(cinfo, use_c_buffer);394395if (! cinfo->raw_data_out)396jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);397398/* We can now tell the memory manager to allocate virtual arrays. */399(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);400401/* Initialize input side of decompressor to consume first scan. */402(*cinfo->inputctl->start_input_pass) (cinfo);403404#ifdef D_MULTISCAN_FILES_SUPPORTED405/* If jpeg_start_decompress will read the whole file, initialize406* progress monitoring appropriately. The input step is counted407* as one pass.408*/409if (cinfo->progress != NULL && ! cinfo->buffered_image &&410cinfo->inputctl->has_multiple_scans) {411int nscans;412/* Estimate number of scans to set pass_limit. */413if (cinfo->progressive_mode) {414/* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */415nscans = 2 + 3 * cinfo->num_components;416} else {417/* For a nonprogressive multiscan file, estimate 1 scan per component. */418nscans = cinfo->num_components;419}420cinfo->progress->pass_counter = 0L;421cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;422cinfo->progress->completed_passes = 0;423cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);424/* Count the input pass as done */425master->pass_number++;426}427#endif /* D_MULTISCAN_FILES_SUPPORTED */428}429430431/*432* Per-pass setup.433* This is called at the beginning of each output pass. We determine which434* modules will be active during this pass and give them appropriate435* start_pass calls. We also set is_dummy_pass to indicate whether this436* is a "real" output pass or a dummy pass for color quantization.437* (In the latter case, jdapistd.c will crank the pass to completion.)438*/439440METHODDEF(void)441prepare_for_output_pass (j_decompress_ptr cinfo)442{443my_master_ptr master = (my_master_ptr) cinfo->master;444445if (master->pub.is_dummy_pass) {446#ifdef QUANT_2PASS_SUPPORTED447/* Final pass of 2-pass quantization */448master->pub.is_dummy_pass = FALSE;449(*cinfo->cquantize->start_pass) (cinfo, FALSE);450(*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);451(*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);452#else453ERREXIT(cinfo, JERR_NOT_COMPILED);454#endif /* QUANT_2PASS_SUPPORTED */455} else {456if (cinfo->quantize_colors && cinfo->colormap == NULL) {457/* Select new quantization method */458if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {459cinfo->cquantize = master->quantizer_2pass;460master->pub.is_dummy_pass = TRUE;461} else if (cinfo->enable_1pass_quant) {462cinfo->cquantize = master->quantizer_1pass;463} else {464ERREXIT(cinfo, JERR_MODE_CHANGE);465}466}467(*cinfo->idct->start_pass) (cinfo);468(*cinfo->coef->start_output_pass) (cinfo);469if (! cinfo->raw_data_out) {470if (! master->using_merged_upsample)471(*cinfo->cconvert->start_pass) (cinfo);472(*cinfo->upsample->start_pass) (cinfo);473if (cinfo->quantize_colors)474(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);475(*cinfo->post->start_pass) (cinfo,476(master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));477(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);478}479}480481/* Set up progress monitor's pass info if present */482if (cinfo->progress != NULL) {483cinfo->progress->completed_passes = master->pass_number;484cinfo->progress->total_passes = master->pass_number +485(master->pub.is_dummy_pass ? 2 : 1);486/* In buffered-image mode, we assume one more output pass if EOI not487* yet reached, but no more passes if EOI has been reached.488*/489if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {490cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);491}492}493}494495496/*497* Finish up at end of an output pass.498*/499500METHODDEF(void)501finish_output_pass (j_decompress_ptr cinfo)502{503my_master_ptr master = (my_master_ptr) cinfo->master;504505if (cinfo->quantize_colors)506(*cinfo->cquantize->finish_pass) (cinfo);507master->pass_number++;508}509510511#ifdef D_MULTISCAN_FILES_SUPPORTED512513/*514* Switch to a new external colormap between output passes.515*/516517GLOBAL(void)518jpeg_new_colormap (j_decompress_ptr cinfo)519{520my_master_ptr master = (my_master_ptr) cinfo->master;521522/* Prevent application from calling me at wrong times */523if (cinfo->global_state != DSTATE_BUFIMAGE)524ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);525526if (cinfo->quantize_colors && cinfo->enable_external_quant &&527cinfo->colormap != NULL) {528/* Select 2-pass quantizer for external colormap use */529cinfo->cquantize = master->quantizer_2pass;530/* Notify quantizer of colormap change */531(*cinfo->cquantize->new_color_map) (cinfo);532master->pub.is_dummy_pass = FALSE; /* just in case */533} else534ERREXIT(cinfo, JERR_MODE_CHANGE);535}536537#endif /* D_MULTISCAN_FILES_SUPPORTED */538539540/*541* Initialize master decompression control and select active modules.542* This is performed at the start of jpeg_start_decompress.543*/544545GLOBAL(void)546jinit_master_decompress (j_decompress_ptr cinfo)547{548my_master_ptr master;549550master = (my_master_ptr)551(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,552SIZEOF(my_decomp_master));553cinfo->master = (struct jpeg_decomp_master *) master;554master->pub.prepare_for_output_pass = prepare_for_output_pass;555master->pub.finish_output_pass = finish_output_pass;556557master->pub.is_dummy_pass = FALSE;558559master_selection(cinfo);560}561562563