jdk
561 строка · 19.6 Кб
1/*
2* reserved comment block
3* DO NOT REMOVE OR ALTER!
4*/
5/*
6* jdmaster.c
7*
8* Copyright (C) 1991-1997, Thomas G. Lane.
9* This file is part of the Independent JPEG Group's software.
10* For conditions of distribution and use, see the accompanying README file.
11*
12* This file contains master control logic for the JPEG decompressor.
13* These routines are concerned with selecting the modules to be executed
14* and with determining the number of passes and the work to be done in each
15* pass.
16*/
17
18#define JPEG_INTERNALS19#include "jinclude.h"20#include "jpeglib.h"21
22
23/* Private state */
24
25typedef struct {26struct jpeg_decomp_master pub; /* public fields */27
28int pass_number; /* # of passes completed */29
30boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */31
32/* Saved references to initialized quantizer modules,33* in case we need to switch modes.
34*/
35struct jpeg_color_quantizer * quantizer_1pass;36struct jpeg_color_quantizer * quantizer_2pass;37} my_decomp_master;38
39typedef my_decomp_master * my_master_ptr;40
41
42/*
43* Determine whether merged upsample/color conversion should be used.
44* CRUCIAL: this must match the actual capabilities of jdmerge.c!
45*/
46
47LOCAL(boolean)48use_merged_upsample (j_decompress_ptr cinfo)49{
50#ifdef UPSAMPLE_MERGING_SUPPORTED51/* Merging is the equivalent of plain box-filter upsampling */52if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)53return FALSE;54/* jdmerge.c only supports YCC=>RGB color conversion */55if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||56cinfo->out_color_space != JCS_RGB ||57cinfo->out_color_components != RGB_PIXELSIZE)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/* ??? also need to test for upsample-time rescaling, when & if supported */73return TRUE; /* by golly, it'll work... */74#else75return FALSE;76#endif77}
78
79
80/*
81* Compute output image dimensions and related values.
82* NOTE: this is exported for possible use by application.
83* Hence it mustn't do anything that can't be done twice.
84* Also note that it may be called before the master module is initialized!
85*/
86
87GLOBAL(void)88jpeg_calc_output_dimensions (j_decompress_ptr cinfo)89/* Do computations that are needed before master selection phase */
90{
91#ifdef IDCT_SCALING_SUPPORTED92int ci;93jpeg_component_info *compptr;94#endif95
96/* Prevent application from calling me at wrong times */97if (cinfo->global_state != DSTATE_READY)98ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);99
100#ifdef IDCT_SCALING_SUPPORTED101
102/* Compute actual output image dimensions and DCT scaling choices. */103if (cinfo->scale_num * 8 <= cinfo->scale_denom) {104/* Provide 1/8 scaling */105cinfo->output_width = (JDIMENSION)106jdiv_round_up((long) cinfo->image_width, 8L);107cinfo->output_height = (JDIMENSION)108jdiv_round_up((long) cinfo->image_height, 8L);109cinfo->min_DCT_scaled_size = 1;110} else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {111/* Provide 1/4 scaling */112cinfo->output_width = (JDIMENSION)113jdiv_round_up((long) cinfo->image_width, 4L);114cinfo->output_height = (JDIMENSION)115jdiv_round_up((long) cinfo->image_height, 4L);116cinfo->min_DCT_scaled_size = 2;117} else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {118/* Provide 1/2 scaling */119cinfo->output_width = (JDIMENSION)120jdiv_round_up((long) cinfo->image_width, 2L);121cinfo->output_height = (JDIMENSION)122jdiv_round_up((long) cinfo->image_height, 2L);123cinfo->min_DCT_scaled_size = 4;124} else {125/* Provide 1/1 scaling */126cinfo->output_width = cinfo->image_width;127cinfo->output_height = cinfo->image_height;128cinfo->min_DCT_scaled_size = DCTSIZE;129}130/* In selecting the actual DCT scaling for each component, we try to131* scale up the chroma components via IDCT scaling rather than upsampling.
132* This saves time if the upsampler gets to use 1:1 scaling.
133* Note this code assumes that the supported DCT scalings are powers of 2.
134*/
135for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;136ci++, compptr++) {137int ssize = cinfo->min_DCT_scaled_size;138while (ssize < DCTSIZE &&139(compptr->h_samp_factor * ssize * 2 <=140cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&141(compptr->v_samp_factor * ssize * 2 <=142cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {143ssize = ssize * 2;144}145compptr->DCT_scaled_size = ssize;146}147
148/* Recompute downsampled dimensions of components;149* application needs to know these if using raw downsampled data.
150*/
151for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;152ci++, compptr++) {153/* Size in samples, after IDCT scaling */154compptr->downsampled_width = (JDIMENSION)155jdiv_round_up((long) cinfo->image_width *156(long) (compptr->h_samp_factor * compptr->DCT_scaled_size),157(long) (cinfo->max_h_samp_factor * DCTSIZE));158compptr->downsampled_height = (JDIMENSION)159jdiv_round_up((long) cinfo->image_height *160(long) (compptr->v_samp_factor * compptr->DCT_scaled_size),161(long) (cinfo->max_v_samp_factor * DCTSIZE));162}163
164#else /* !IDCT_SCALING_SUPPORTED */165
166/* Hardwire it to "no scaling" */167cinfo->output_width = cinfo->image_width;168cinfo->output_height = cinfo->image_height;169/* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,170* and has computed unscaled downsampled_width and downsampled_height.
171*/
172
173#endif /* IDCT_SCALING_SUPPORTED */174
175/* Report number of components in selected colorspace. */176/* Probably this should be in the color conversion module... */177switch (cinfo->out_color_space) {178case JCS_GRAYSCALE:179cinfo->out_color_components = 1;180break;181case JCS_RGB:182#if RGB_PIXELSIZE != 3183cinfo->out_color_components = RGB_PIXELSIZE;184break;185#endif /* else share code with YCbCr */186case JCS_YCbCr:187cinfo->out_color_components = 3;188break;189case JCS_CMYK:190case JCS_YCCK:191cinfo->out_color_components = 4;192break;193default: /* else must be same colorspace as in file */194cinfo->out_color_components = cinfo->num_components;195break;196}197cinfo->output_components = (cinfo->quantize_colors ? 1 :198cinfo->out_color_components);199
200/* See if upsampler will want to emit more than one row at a time */201if (use_merged_upsample(cinfo))202cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;203else204cinfo->rec_outbuf_height = 1;205}
206
207
208/*
209* Several decompression processes need to range-limit values to the range
210* 0..MAXJSAMPLE; the input value may fall somewhat outside this range
211* due to noise introduced by quantization, roundoff error, etc. These
212* processes are inner loops and need to be as fast as possible. On most
213* machines, particularly CPUs with pipelines or instruction prefetch,
214* a (subscript-check-less) C table lookup
215* x = sample_range_limit[x];
216* is faster than explicit tests
217* if (x < 0) x = 0;
218* else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
219* These processes all use a common table prepared by the routine below.
220*
221* For most steps we can mathematically guarantee that the initial value
222* of x is within MAXJSAMPLE+1 of the legal range, so a table running from
223* -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
224* limiting step (just after the IDCT), a wildly out-of-range value is
225* possible if the input data is corrupt. To avoid any chance of indexing
226* off the end of memory and getting a bad-pointer trap, we perform the
227* post-IDCT limiting thus:
228* x = range_limit[x & MASK];
229* where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
230* samples. Under normal circumstances this is more than enough range and
231* a correct output will be generated; with bogus input data the mask will
232* cause wraparound, and we will safely generate a bogus-but-in-range output.
233* For the post-IDCT step, we want to convert the data from signed to unsigned
234* representation by adding CENTERJSAMPLE at the same time that we limit it.
235* So the post-IDCT limiting table ends up looking like this:
236* CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
237* MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
238* 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
239* 0,1,...,CENTERJSAMPLE-1
240* Negative inputs select values from the upper half of the table after
241* masking.
242*
243* We can save some space by overlapping the start of the post-IDCT table
244* with the simpler range limiting table. The post-IDCT table begins at
245* sample_range_limit + CENTERJSAMPLE.
246*
247* Note that the table is allocated in near data space on PCs; it's small
248* enough and used often enough to justify this.
249*/
250
251LOCAL(void)252prepare_range_limit_table (j_decompress_ptr cinfo)253/* Allocate and fill in the sample_range_limit table */
254{
255JSAMPLE * table;256int i;257
258table = (JSAMPLE *)259(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,260(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));261table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */262cinfo->sample_range_limit = table;263/* First segment of "simple" table: limit[x] = 0 for x < 0 */264MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));265/* Main part of "simple" table: limit[x] = x */266for (i = 0; i <= MAXJSAMPLE; i++)267table[i] = (JSAMPLE) i;268table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */269/* End of simple table, rest of first half of post-IDCT table */270for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)271table[i] = MAXJSAMPLE;272/* Second half of post-IDCT table */273MEMZERO(table + (2 * (MAXJSAMPLE+1)),274(2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));275MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),276cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));277}
278
279
280/*
281* Master selection of decompression modules.
282* This is done once at jpeg_start_decompress time. We determine
283* which modules will be used and give them appropriate initialization calls.
284* We also initialize the decompressor input side to begin consuming data.
285*
286* Since jpeg_read_header has finished, we know what is in the SOF
287* and (first) SOS markers. We also have all the application parameter
288* settings.
289*/
290
291LOCAL(void)292master_selection (j_decompress_ptr cinfo)293{
294my_master_ptr master = (my_master_ptr) cinfo->master;295boolean use_c_buffer;296long samplesperrow;297JDIMENSION jd_samplesperrow;298
299/* Initialize dimensions and other stuff */300jpeg_calc_output_dimensions(cinfo);301prepare_range_limit_table(cinfo);302
303/* Width of an output scanline must be representable as JDIMENSION. */304samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;305jd_samplesperrow = (JDIMENSION) samplesperrow;306if ((long) jd_samplesperrow != samplesperrow)307ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);308
309/* Initialize my private state */310master->pass_number = 0;311master->using_merged_upsample = use_merged_upsample(cinfo);312
313/* Color quantizer selection */314master->quantizer_1pass = NULL;315master->quantizer_2pass = NULL;316/* No mode changes if not using buffered-image mode. */317if (! cinfo->quantize_colors || ! cinfo->buffered_image) {318cinfo->enable_1pass_quant = FALSE;319cinfo->enable_external_quant = FALSE;320cinfo->enable_2pass_quant = FALSE;321}322if (cinfo->quantize_colors) {323if (cinfo->raw_data_out)324ERREXIT(cinfo, JERR_NOTIMPL);325/* 2-pass quantizer only works in 3-component color space. */326if (cinfo->out_color_components != 3) {327cinfo->enable_1pass_quant = TRUE;328cinfo->enable_external_quant = FALSE;329cinfo->enable_2pass_quant = FALSE;330cinfo->colormap = NULL;331} else if (cinfo->colormap != NULL) {332cinfo->enable_external_quant = TRUE;333} else if (cinfo->two_pass_quantize) {334cinfo->enable_2pass_quant = TRUE;335} else {336cinfo->enable_1pass_quant = TRUE;337}338
339if (cinfo->enable_1pass_quant) {340#ifdef QUANT_1PASS_SUPPORTED341jinit_1pass_quantizer(cinfo);342master->quantizer_1pass = cinfo->cquantize;343#else344ERREXIT(cinfo, JERR_NOT_COMPILED);345#endif346}347
348/* We use the 2-pass code to map to external colormaps. */349if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {350#ifdef QUANT_2PASS_SUPPORTED351jinit_2pass_quantizer(cinfo);352master->quantizer_2pass = cinfo->cquantize;353#else354ERREXIT(cinfo, JERR_NOT_COMPILED);355#endif356}357/* If both quantizers are initialized, the 2-pass one is left active;358* this is necessary for starting with quantization to an external map.
359*/
360}361
362/* Post-processing: in particular, color conversion first */363if (! cinfo->raw_data_out) {364if (master->using_merged_upsample) {365#ifdef UPSAMPLE_MERGING_SUPPORTED366jinit_merged_upsampler(cinfo); /* does color conversion too */367#else368ERREXIT(cinfo, JERR_NOT_COMPILED);369#endif370} else {371jinit_color_deconverter(cinfo);372jinit_upsampler(cinfo);373}374jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);375}376/* Inverse DCT */377jinit_inverse_dct(cinfo);378/* Entropy decoding: either Huffman or arithmetic coding. */379if (cinfo->arith_code) {380ERREXIT(cinfo, JERR_ARITH_NOTIMPL);381} else {382if (cinfo->progressive_mode) {383#ifdef D_PROGRESSIVE_SUPPORTED384jinit_phuff_decoder(cinfo);385#else386ERREXIT(cinfo, JERR_NOT_COMPILED);387#endif388} else389jinit_huff_decoder(cinfo);390}391
392/* Initialize principal buffer controllers. */393use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;394jinit_d_coef_controller(cinfo, use_c_buffer);395
396if (! cinfo->raw_data_out)397jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);398
399/* We can now tell the memory manager to allocate virtual arrays. */400(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);401
402/* Initialize input side of decompressor to consume first scan. */403(*cinfo->inputctl->start_input_pass) (cinfo);404
405#ifdef D_MULTISCAN_FILES_SUPPORTED406/* If jpeg_start_decompress will read the whole file, initialize407* progress monitoring appropriately. The input step is counted
408* as one pass.
409*/
410if (cinfo->progress != NULL && ! cinfo->buffered_image &&411cinfo->inputctl->has_multiple_scans) {412int nscans;413/* Estimate number of scans to set pass_limit. */414if (cinfo->progressive_mode) {415/* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */416nscans = 2 + 3 * cinfo->num_components;417} else {418/* For a nonprogressive multiscan file, estimate 1 scan per component. */419nscans = cinfo->num_components;420}421cinfo->progress->pass_counter = 0L;422cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;423cinfo->progress->completed_passes = 0;424cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);425/* Count the input pass as done */426master->pass_number++;427}428#endif /* D_MULTISCAN_FILES_SUPPORTED */429}
430
431
432/*
433* Per-pass setup.
434* This is called at the beginning of each output pass. We determine which
435* modules will be active during this pass and give them appropriate
436* start_pass calls. We also set is_dummy_pass to indicate whether this
437* is a "real" output pass or a dummy pass for color quantization.
438* (In the latter case, jdapistd.c will crank the pass to completion.)
439*/
440
441METHODDEF(void)442prepare_for_output_pass (j_decompress_ptr cinfo)443{
444my_master_ptr master = (my_master_ptr) cinfo->master;445
446if (master->pub.is_dummy_pass) {447#ifdef QUANT_2PASS_SUPPORTED448/* Final pass of 2-pass quantization */449master->pub.is_dummy_pass = FALSE;450(*cinfo->cquantize->start_pass) (cinfo, FALSE);451(*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);452(*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);453#else454ERREXIT(cinfo, JERR_NOT_COMPILED);455#endif /* QUANT_2PASS_SUPPORTED */456} else {457if (cinfo->quantize_colors && cinfo->colormap == NULL) {458/* Select new quantization method */459if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {460cinfo->cquantize = master->quantizer_2pass;461master->pub.is_dummy_pass = TRUE;462} else if (cinfo->enable_1pass_quant) {463cinfo->cquantize = master->quantizer_1pass;464} else {465ERREXIT(cinfo, JERR_MODE_CHANGE);466}467}468(*cinfo->idct->start_pass) (cinfo);469(*cinfo->coef->start_output_pass) (cinfo);470if (! cinfo->raw_data_out) {471if (! master->using_merged_upsample)472(*cinfo->cconvert->start_pass) (cinfo);473(*cinfo->upsample->start_pass) (cinfo);474if (cinfo->quantize_colors)475(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);476(*cinfo->post->start_pass) (cinfo,477(master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));478(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);479}480}481
482/* Set up progress monitor's pass info if present */483if (cinfo->progress != NULL) {484cinfo->progress->completed_passes = master->pass_number;485cinfo->progress->total_passes = master->pass_number +486(master->pub.is_dummy_pass ? 2 : 1);487/* In buffered-image mode, we assume one more output pass if EOI not488* yet reached, but no more passes if EOI has been reached.
489*/
490if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {491cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);492}493}494}
495
496
497/*
498* Finish up at end of an output pass.
499*/
500
501METHODDEF(void)502finish_output_pass (j_decompress_ptr cinfo)503{
504my_master_ptr master = (my_master_ptr) cinfo->master;505
506if (cinfo->quantize_colors)507(*cinfo->cquantize->finish_pass) (cinfo);508master->pass_number++;509}
510
511
512#ifdef D_MULTISCAN_FILES_SUPPORTED513
514/*
515* Switch to a new external colormap between output passes.
516*/
517
518GLOBAL(void)519jpeg_new_colormap (j_decompress_ptr cinfo)520{
521my_master_ptr master = (my_master_ptr) cinfo->master;522
523/* Prevent application from calling me at wrong times */524if (cinfo->global_state != DSTATE_BUFIMAGE)525ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);526
527if (cinfo->quantize_colors && cinfo->enable_external_quant &&528cinfo->colormap != NULL) {529/* Select 2-pass quantizer for external colormap use */530cinfo->cquantize = master->quantizer_2pass;531/* Notify quantizer of colormap change */532(*cinfo->cquantize->new_color_map) (cinfo);533master->pub.is_dummy_pass = FALSE; /* just in case */534} else535ERREXIT(cinfo, JERR_MODE_CHANGE);536}
537
538#endif /* D_MULTISCAN_FILES_SUPPORTED */539
540
541/*
542* Initialize master decompression control and select active modules.
543* This is performed at the start of jpeg_start_decompress.
544*/
545
546GLOBAL(void)547jinit_master_decompress (j_decompress_ptr cinfo)548{
549my_master_ptr master;550
551master = (my_master_ptr)552(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,553SIZEOF(my_decomp_master));554cinfo->master = (struct jpeg_decomp_master *) master;555master->pub.prepare_for_output_pass = prepare_for_output_pass;556master->pub.finish_output_pass = finish_output_pass;557
558master->pub.is_dummy_pass = FALSE;559
560master_selection(cinfo);561}
562