📄 jdmaster.c
字号:
* possible if the input data is corrupt. To avoid any chance of indexing
* off the end of memory and getting a bad-pointer trap, we perform the
* post-IDCT limiting thus:
* x = range_limit[x & MASK];
* where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
* samples. Under normal circumstances this is more than enough range and
* a correct output will be generated; with bogus input data the mask will
* cause wraparound, and we will safely generate a bogus-but-in-range output.
* For the post-IDCT step, we want to convert the data from signed to unsigned
* representation by adding CENTERJSAMPLE at the same time that we limit it.
* So the post-IDCT limiting table ends up looking like this:
* CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
* MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
* 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
* 0,1,...,CENTERJSAMPLE-1
* Negative inputs select values from the upper half of the table after
* masking.
*
* We can save some space by overlapping the start of the post-IDCT table
* with the simpler range limiting table. The post-IDCT table begins at
* sample_range_limit + CENTERJSAMPLE.
*
* Note that the table is allocated in near data space on PCs; it's small
* enough and used often enough to justify this.
*/
LOCAL void
prepare_range_limit_table (j_decompress_ptr cinfo)
/* Allocate and fill in the sample_range_limit table */
{
JSAMPLE * table;
int i;
table = (JSAMPLE *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
cinfo->sample_range_limit = table;
/* First segment of "simple" table: limit[x] = 0 for x < 0 */
MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
/* Main part of "simple" table: limit[x] = x */
for (i = 0; i <= MAXJSAMPLE; i++)
table[i] = (JSAMPLE) i;
table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
/* End of simple table, rest of first half of post-IDCT table */
for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
table[i] = MAXJSAMPLE;
/* Second half of post-IDCT table */
MEMZERO(table + (2 * (MAXJSAMPLE+1)),
(2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
}
/*
* Master selection of decompression modules.
* This is done once at the start of processing an image. We determine
* which modules will be used and give them appropriate initialization calls.
*
* Note that this is called only after jpeg_read_header has finished.
* We therefore know what is in the SOF and (first) SOS markers.
*/
LOCAL void
master_selection (j_decompress_ptr cinfo)
{
my_master_ptr master = (my_master_ptr) cinfo->master;
long samplesperrow;
JDIMENSION jd_samplesperrow;
/* Initialize dimensions and other stuff */
jpeg_calc_output_dimensions(cinfo);
prepare_range_limit_table(cinfo);
/* Width of an output scanline must be representable as JDIMENSION. */
samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
jd_samplesperrow = (JDIMENSION) samplesperrow;
if ((long) jd_samplesperrow != samplesperrow)
ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
/* Initialize my private state */
master->pub.eoi_processed = FALSE;
master->pass_number = 0;
master->need_post_pass = FALSE;
if (cinfo->comps_in_scan == cinfo->num_components) {
master->pass_type = main_pass;
master->total_passes = 1;
} else {
#ifdef D_MULTISCAN_FILES_SUPPORTED
master->pass_type = preread_pass;
/* Assume there is a separate scan for each component; */
/* if partially interleaved, we'll increment pass_number appropriately */
master->total_passes = cinfo->num_components + 1;
#else
ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
}
master->using_merged_upsample = use_merged_upsample(cinfo);
/* There's not a lot of smarts here right now, but it'll get more
* complicated when we have multiple implementations available...
*/
/* Color quantizer selection */
if (cinfo->quantize_colors) {
if (cinfo->raw_data_out)
ERREXIT(cinfo, JERR_NOTIMPL);
#ifdef QUANT_2PASS_SUPPORTED
/* 2-pass quantizer only works in 3-component color space.
* We use the "2-pass" code in a single pass if a colormap is given.
*/
if (cinfo->out_color_components != 3)
cinfo->two_pass_quantize = FALSE;
else if (cinfo->colormap != NULL)
cinfo->two_pass_quantize = TRUE;
#else
/* Force 1-pass quantize if we don't have 2-pass code compiled. */
cinfo->two_pass_quantize = FALSE;
#endif
if (cinfo->two_pass_quantize) {
#ifdef QUANT_2PASS_SUPPORTED
if (cinfo->colormap == NULL) {
master->need_post_pass = TRUE;
master->total_passes++;
}
jinit_2pass_quantizer(cinfo);
#else
ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
} else {
#ifdef QUANT_1PASS_SUPPORTED
jinit_1pass_quantizer(cinfo);
#else
ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
}
}
/* Post-processing: in particular, color conversion first */
if (! cinfo->raw_data_out) {
if (master->using_merged_upsample) {
#ifdef UPSAMPLE_MERGING_SUPPORTED
jinit_merged_upsampler(cinfo); /* does color conversion too */
#else
ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
} else {
jinit_color_deconverter(cinfo);
jinit_upsampler(cinfo);
}
jinit_d_post_controller(cinfo, master->need_post_pass);
}
/* Inverse DCT */
jinit_inverse_dct(cinfo);
/* Entropy decoding: either Huffman or arithmetic coding. */
if (cinfo->arith_code) {
#ifdef D_ARITH_CODING_SUPPORTED
jinit_arith_decoder(cinfo);
#else
ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
#endif
} else
jinit_huff_decoder(cinfo);
jinit_d_coef_controller(cinfo, (master->pass_type == preread_pass));
jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
/* Note that main controller is initialized even in raw-data mode. */
/* We can now tell the memory manager to allocate virtual arrays. */
(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
}
/*
* Per-pass setup.
* This is called at the beginning of each pass. We determine which modules
* will be active during this pass and give them appropriate start_pass calls.
* We also set is_last_pass to indicate whether any more passes will be
* required.
*/
METHODDEF void
prepare_for_pass (j_decompress_ptr cinfo)
{
my_master_ptr master = (my_master_ptr) cinfo->master;
switch (master->pass_type) {
case main_pass:
/* Set up to read and decompress single-scan file in one pass */
per_scan_setup(cinfo);
master->pub.is_last_pass = ! master->need_post_pass;
if (! cinfo->raw_data_out) {
if (! master->using_merged_upsample)
(*cinfo->cconvert->start_pass) (cinfo);
(*cinfo->upsample->start_pass) (cinfo);
if (cinfo->quantize_colors)
(*cinfo->cquantize->start_pass) (cinfo, master->need_post_pass);
(*cinfo->post->start_pass) (cinfo,
(master->need_post_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
}
(*cinfo->idct->start_input_pass) (cinfo);
(*cinfo->idct->start_output_pass) (cinfo);
(*cinfo->entropy->start_pass) (cinfo);
(*cinfo->coef->start_pass) (cinfo, JBUF_PASS_THRU);
(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
break;
#ifdef D_MULTISCAN_FILES_SUPPORTED
case preread_pass:
/* Read (another) scan of a multi-scan file */
per_scan_setup(cinfo);
master->pub.is_last_pass = FALSE;
(*cinfo->idct->start_input_pass) (cinfo);
(*cinfo->entropy->start_pass) (cinfo);
(*cinfo->coef->start_pass) (cinfo, JBUF_SAVE_SOURCE);
(*cinfo->main->start_pass) (cinfo, JBUF_CRANK_SOURCE);
break;
case output_pass:
/* All scans read, now do the IDCT and subsequent processing */
master->pub.is_last_pass = ! master->need_post_pass;
if (! cinfo->raw_data_out) {
if (! master->using_merged_upsample)
(*cinfo->cconvert->start_pass) (cinfo);
(*cinfo->upsample->start_pass) (cinfo);
if (cinfo->quantize_colors)
(*cinfo->cquantize->start_pass) (cinfo, master->need_post_pass);
(*cinfo->post->start_pass) (cinfo,
(master->need_post_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
}
(*cinfo->idct->start_output_pass) (cinfo);
(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
break;
#endif /* D_MULTISCAN_FILES_SUPPORTED */
#ifdef QUANT_2PASS_SUPPORTED
case post_pass:
/* Final pass of 2-pass quantization */
master->pub.is_last_pass = TRUE;
(*cinfo->cquantize->start_pass) (cinfo, FALSE);
(*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
(*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
break;
#endif /* QUANT_2PASS_SUPPORTED */
default:
ERREXIT(cinfo, JERR_NOT_COMPILED);
}
/* Set up progress monitor's pass info if present */
if (cinfo->progress != NULL) {
cinfo->progress->completed_passes = master->pass_number;
cinfo->progress->total_passes = master->total_passes;
}
}
/*
* Finish up at end of pass.
* In multi-scan mode, we must read next scan header and set the next
* pass_type correctly for prepare_for_pass.
*/
METHODDEF void
finish_pass_master (j_decompress_ptr cinfo)
{
my_master_ptr master = (my_master_ptr) cinfo->master;
switch (master->pass_type) {
case main_pass:
case output_pass:
if (cinfo->quantize_colors)
(*cinfo->cquantize->finish_pass) (cinfo);
master->pass_number++;
master->pass_type = post_pass; /* in case need_post_pass is true */
break;
#ifdef D_MULTISCAN_FILES_SUPPORTED
case preread_pass:
/* Count one pass done for each component in this scan */
master->pass_number += cinfo->comps_in_scan;
switch ((*cinfo->marker->read_markers) (cinfo)) {
case JPEG_HEADER_OK: /* Found SOS, do another preread pass */
break;
case JPEG_HEADER_TABLES_ONLY: /* Found EOI, no more preread passes */
master->pub.eoi_processed = TRUE;
master->pass_type = output_pass;
break;
case JPEG_SUSPENDED:
ERREXIT(cinfo, JERR_CANT_SUSPEND);
}
break;
#endif /* D_MULTISCAN_FILES_SUPPORTED */
#ifdef QUANT_2PASS_SUPPORTED
case post_pass:
(*cinfo->cquantize->finish_pass) (cinfo);
/* there will be no more passes, don't bother to change state */
break;
#endif /* QUANT_2PASS_SUPPORTED */
default:
ERREXIT(cinfo, JERR_NOT_COMPILED);
}
}
/*
* Initialize master decompression control.
* This creates my own subrecord and also performs the master selection phase,
* which causes other modules to create their subrecords.
*/
GLOBAL void
jinit_master_decompress (j_decompress_ptr cinfo)
{
my_master_ptr master;
master = (my_master_ptr)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
SIZEOF(my_decomp_master));
cinfo->master = (struct jpeg_decomp_master *) master;
master->pub.prepare_for_pass = prepare_for_pass;
master->pub.finish_pass = finish_pass_master;
master_selection(cinfo);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -