Path: blob/master/src/java.desktop/share/native/libjavajpeg/jdinput.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jdinput.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 input control logic for the JPEG decompressor.12* These routines are concerned with controlling the decompressor's input13* processing (marker reading and coefficient decoding). The actual input14* reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.15*/1617#define JPEG_INTERNALS18#include "jinclude.h"19#include "jpeglib.h"202122/* Private state */2324typedef struct {25struct jpeg_input_controller pub; /* public fields */2627boolean inheaders; /* TRUE until first SOS is reached */28} my_input_controller;2930typedef my_input_controller * my_inputctl_ptr;313233/* Forward declarations */34METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));353637/*38* Routines to calculate various quantities related to the size of the image.39*/4041LOCAL(void)42initial_setup (j_decompress_ptr cinfo)43/* Called once, when first SOS marker is reached */44{45int ci;46jpeg_component_info *compptr;4748/* Make sure image isn't bigger than I can handle */49if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||50(long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)51ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);5253/* For now, precision must match compiled-in value... */54if (cinfo->data_precision != BITS_IN_JSAMPLE)55ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);5657/* Check that number of components won't exceed internal array sizes */58if (cinfo->num_components > MAX_COMPONENTS)59ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,60MAX_COMPONENTS);6162/* Compute maximum sampling factors; check factor validity */63cinfo->max_h_samp_factor = 1;64cinfo->max_v_samp_factor = 1;65for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;66ci++, compptr++) {67if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||68compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)69ERREXIT(cinfo, JERR_BAD_SAMPLING);70cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,71compptr->h_samp_factor);72cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,73compptr->v_samp_factor);74}7576/* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.77* In the full decompressor, this will be overridden by jdmaster.c;78* but in the transcoder, jdmaster.c is not used, so we must do it here.79*/80cinfo->min_DCT_scaled_size = DCTSIZE;8182/* Compute dimensions of components */83for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;84ci++, compptr++) {85compptr->DCT_scaled_size = DCTSIZE;86/* Size in DCT blocks */87compptr->width_in_blocks = (JDIMENSION)88jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,89(long) (cinfo->max_h_samp_factor * DCTSIZE));90compptr->height_in_blocks = (JDIMENSION)91jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,92(long) (cinfo->max_v_samp_factor * DCTSIZE));93/* downsampled_width and downsampled_height will also be overridden by94* jdmaster.c if we are doing full decompression. The transcoder library95* doesn't use these values, but the calling application might.96*/97/* Size in samples */98compptr->downsampled_width = (JDIMENSION)99jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,100(long) cinfo->max_h_samp_factor);101compptr->downsampled_height = (JDIMENSION)102jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,103(long) cinfo->max_v_samp_factor);104/* Mark component needed, until color conversion says otherwise */105compptr->component_needed = TRUE;106/* Mark no quantization table yet saved for component */107compptr->quant_table = NULL;108}109110/* Compute number of fully interleaved MCU rows. */111cinfo->total_iMCU_rows = (JDIMENSION)112jdiv_round_up((long) cinfo->image_height,113(long) (cinfo->max_v_samp_factor*DCTSIZE));114115/* Decide whether file contains multiple scans */116if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)117cinfo->inputctl->has_multiple_scans = TRUE;118else119cinfo->inputctl->has_multiple_scans = FALSE;120}121122123LOCAL(void)124per_scan_setup (j_decompress_ptr cinfo)125/* Do computations that are needed before processing a JPEG scan */126/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */127{128int ci, mcublks, tmp;129jpeg_component_info *compptr;130131if (cinfo->comps_in_scan == 1) {132133/* Noninterleaved (single-component) scan */134compptr = cinfo->cur_comp_info[0];135136/* Overall image size in MCUs */137cinfo->MCUs_per_row = compptr->width_in_blocks;138cinfo->MCU_rows_in_scan = compptr->height_in_blocks;139140/* For noninterleaved scan, always one block per MCU */141compptr->MCU_width = 1;142compptr->MCU_height = 1;143compptr->MCU_blocks = 1;144compptr->MCU_sample_width = compptr->DCT_scaled_size;145compptr->last_col_width = 1;146/* For noninterleaved scans, it is convenient to define last_row_height147* as the number of block rows present in the last iMCU row.148*/149tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);150if (tmp == 0) tmp = compptr->v_samp_factor;151compptr->last_row_height = tmp;152153/* Prepare array describing MCU composition */154cinfo->blocks_in_MCU = 1;155cinfo->MCU_membership[0] = 0;156157} else {158159/* Interleaved (multi-component) scan */160if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)161ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,162MAX_COMPS_IN_SCAN);163164/* Overall image size in MCUs */165cinfo->MCUs_per_row = (JDIMENSION)166jdiv_round_up((long) cinfo->image_width,167(long) (cinfo->max_h_samp_factor*DCTSIZE));168cinfo->MCU_rows_in_scan = (JDIMENSION)169jdiv_round_up((long) cinfo->image_height,170(long) (cinfo->max_v_samp_factor*DCTSIZE));171172cinfo->blocks_in_MCU = 0;173174for (ci = 0; ci < cinfo->comps_in_scan; ci++) {175compptr = cinfo->cur_comp_info[ci];176/* Sampling factors give # of blocks of component in each MCU */177compptr->MCU_width = compptr->h_samp_factor;178compptr->MCU_height = compptr->v_samp_factor;179compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;180compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;181/* Figure number of non-dummy blocks in last MCU column & row */182tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);183if (tmp == 0) tmp = compptr->MCU_width;184compptr->last_col_width = tmp;185tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);186if (tmp == 0) tmp = compptr->MCU_height;187compptr->last_row_height = tmp;188/* Prepare array describing MCU composition */189mcublks = compptr->MCU_blocks;190if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)191ERREXIT(cinfo, JERR_BAD_MCU_SIZE);192while (mcublks-- > 0) {193cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;194}195}196197}198}199200201/*202* Save away a copy of the Q-table referenced by each component present203* in the current scan, unless already saved during a prior scan.204*205* In a multiple-scan JPEG file, the encoder could assign different components206* the same Q-table slot number, but change table definitions between scans207* so that each component uses a different Q-table. (The IJG encoder is not208* currently capable of doing this, but other encoders might.) Since we want209* to be able to dequantize all the components at the end of the file, this210* means that we have to save away the table actually used for each component.211* We do this by copying the table at the start of the first scan containing212* the component.213* The JPEG spec prohibits the encoder from changing the contents of a Q-table214* slot between scans of a component using that slot. If the encoder does so215* anyway, this decoder will simply use the Q-table values that were current216* at the start of the first scan for the component.217*218* The decompressor output side looks only at the saved quant tables,219* not at the current Q-table slots.220*/221222LOCAL(void)223latch_quant_tables (j_decompress_ptr cinfo)224{225int ci, qtblno;226jpeg_component_info *compptr;227JQUANT_TBL * qtbl;228229for (ci = 0; ci < cinfo->comps_in_scan; ci++) {230compptr = cinfo->cur_comp_info[ci];231/* No work if we already saved Q-table for this component */232if (compptr->quant_table != NULL)233continue;234/* Make sure specified quantization table is present */235qtblno = compptr->quant_tbl_no;236if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||237cinfo->quant_tbl_ptrs[qtblno] == NULL)238ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);239/* OK, save away the quantization table */240qtbl = (JQUANT_TBL *)241(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,242SIZEOF(JQUANT_TBL));243MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));244compptr->quant_table = qtbl;245}246}247248249/*250* Initialize the input modules to read a scan of compressed data.251* The first call to this is done by jdmaster.c after initializing252* the entire decompressor (during jpeg_start_decompress).253* Subsequent calls come from consume_markers, below.254*/255256METHODDEF(void)257start_input_pass (j_decompress_ptr cinfo)258{259per_scan_setup(cinfo);260latch_quant_tables(cinfo);261(*cinfo->entropy->start_pass) (cinfo);262(*cinfo->coef->start_input_pass) (cinfo);263cinfo->inputctl->consume_input = cinfo->coef->consume_data;264}265266267/*268* Finish up after inputting a compressed-data scan.269* This is called by the coefficient controller after it's read all270* the expected data of the scan.271*/272273METHODDEF(void)274finish_input_pass (j_decompress_ptr cinfo)275{276cinfo->inputctl->consume_input = consume_markers;277}278279280/*281* Read JPEG markers before, between, or after compressed-data scans.282* Change state as necessary when a new scan is reached.283* Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.284*285* The consume_input method pointer points either here or to the286* coefficient controller's consume_data routine, depending on whether287* we are reading a compressed data segment or inter-segment markers.288*/289290METHODDEF(int)291consume_markers (j_decompress_ptr cinfo)292{293my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;294int val;295296if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */297return JPEG_REACHED_EOI;298299val = (*cinfo->marker->read_markers) (cinfo);300301switch (val) {302case JPEG_REACHED_SOS: /* Found SOS */303if (inputctl->inheaders) { /* 1st SOS */304initial_setup(cinfo);305inputctl->inheaders = FALSE;306/* Note: start_input_pass must be called by jdmaster.c307* before any more input can be consumed. jdapimin.c is308* responsible for enforcing this sequencing.309*/310} else { /* 2nd or later SOS marker */311if (! inputctl->pub.has_multiple_scans)312ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */313start_input_pass(cinfo);314}315break;316case JPEG_REACHED_EOI: /* Found EOI */317inputctl->pub.eoi_reached = TRUE;318if (inputctl->inheaders) { /* Tables-only datastream, apparently */319if (cinfo->marker->saw_SOF)320ERREXIT(cinfo, JERR_SOF_NO_SOS);321} else {322/* Prevent infinite loop in coef ctlr's decompress_data routine323* if user set output_scan_number larger than number of scans.324*/325if (cinfo->output_scan_number > cinfo->input_scan_number)326cinfo->output_scan_number = cinfo->input_scan_number;327}328break;329case JPEG_SUSPENDED:330break;331}332333return val;334}335336337/*338* Reset state to begin a fresh datastream.339*/340341METHODDEF(void)342reset_input_controller (j_decompress_ptr cinfo)343{344my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;345346inputctl->pub.consume_input = consume_markers;347inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */348inputctl->pub.eoi_reached = FALSE;349inputctl->inheaders = TRUE;350/* Reset other modules */351(*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);352(*cinfo->marker->reset_marker_reader) (cinfo);353/* Reset progression state -- would be cleaner if entropy decoder did this */354cinfo->coef_bits = NULL;355}356357358/*359* Initialize the input controller module.360* This is called only once, when the decompression object is created.361*/362363GLOBAL(void)364jinit_input_controller (j_decompress_ptr cinfo)365{366my_inputctl_ptr inputctl;367368/* Create subobject in permanent pool */369inputctl = (my_inputctl_ptr)370(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,371SIZEOF(my_input_controller));372cinfo->inputctl = (struct jpeg_input_controller *) inputctl;373/* Initialize method pointers */374inputctl->pub.consume_input = consume_markers;375inputctl->pub.reset_input_controller = reset_input_controller;376inputctl->pub.start_input_pass = start_input_pass;377inputctl->pub.finish_input_pass = finish_input_pass;378/* Initialize state: can't use reset_input_controller since we don't379* want to try to reset other modules yet.380*/381inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */382inputctl->pub.eoi_reached = FALSE;383inputctl->inheaders = TRUE;384}385386387