0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jdinput.c
0N/A *
0N/A * Copyright (C) 1991-1997, Thomas G. Lane.
0N/A * This file is part of the Independent JPEG Group's software.
0N/A * For conditions of distribution and use, see the accompanying README file.
0N/A *
0N/A * This file contains input control logic for the JPEG decompressor.
0N/A * These routines are concerned with controlling the decompressor's input
0N/A * processing (marker reading and coefficient decoding). The actual input
0N/A * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
0N/A */
0N/A
0N/A#define JPEG_INTERNALS
0N/A#include "jinclude.h"
0N/A#include "jpeglib.h"
0N/A
0N/A
0N/A/* Private state */
0N/A
0N/Atypedef struct {
0N/A struct jpeg_input_controller pub; /* public fields */
0N/A
0N/A boolean inheaders; /* TRUE until first SOS is reached */
0N/A} my_input_controller;
0N/A
0N/Atypedef my_input_controller * my_inputctl_ptr;
0N/A
0N/A
0N/A/* Forward declarations */
0N/AMETHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
0N/A
0N/A
0N/A/*
0N/A * Routines to calculate various quantities related to the size of the image.
0N/A */
0N/A
0N/ALOCAL(void)
0N/Ainitial_setup (j_decompress_ptr cinfo)
0N/A/* Called once, when first SOS marker is reached */
0N/A{
0N/A int ci;
0N/A jpeg_component_info *compptr;
0N/A
0N/A /* Make sure image isn't bigger than I can handle */
0N/A if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
0N/A (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
0N/A ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
0N/A
0N/A /* For now, precision must match compiled-in value... */
0N/A if (cinfo->data_precision != BITS_IN_JSAMPLE)
0N/A ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
0N/A
0N/A /* Check that number of components won't exceed internal array sizes */
0N/A if (cinfo->num_components > MAX_COMPONENTS)
0N/A ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
0N/A MAX_COMPONENTS);
0N/A
0N/A /* Compute maximum sampling factors; check factor validity */
0N/A cinfo->max_h_samp_factor = 1;
0N/A cinfo->max_v_samp_factor = 1;
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
0N/A compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
0N/A ERREXIT(cinfo, JERR_BAD_SAMPLING);
0N/A cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
0N/A compptr->h_samp_factor);
0N/A cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
0N/A compptr->v_samp_factor);
0N/A }
0N/A
0N/A /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
0N/A * In the full decompressor, this will be overridden by jdmaster.c;
0N/A * but in the transcoder, jdmaster.c is not used, so we must do it here.
0N/A */
0N/A cinfo->min_DCT_scaled_size = DCTSIZE;
0N/A
0N/A /* Compute dimensions of components */
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A compptr->DCT_scaled_size = DCTSIZE;
0N/A /* Size in DCT blocks */
0N/A compptr->width_in_blocks = (JDIMENSION)
0N/A jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
0N/A (long) (cinfo->max_h_samp_factor * DCTSIZE));
0N/A compptr->height_in_blocks = (JDIMENSION)
0N/A jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
0N/A (long) (cinfo->max_v_samp_factor * DCTSIZE));
0N/A /* downsampled_width and downsampled_height will also be overridden by
0N/A * jdmaster.c if we are doing full decompression. The transcoder library
0N/A * doesn't use these values, but the calling application might.
0N/A */
0N/A /* Size in samples */
0N/A compptr->downsampled_width = (JDIMENSION)
0N/A jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
0N/A (long) cinfo->max_h_samp_factor);
0N/A compptr->downsampled_height = (JDIMENSION)
0N/A jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
0N/A (long) cinfo->max_v_samp_factor);
0N/A /* Mark component needed, until color conversion says otherwise */
0N/A compptr->component_needed = TRUE;
0N/A /* Mark no quantization table yet saved for component */
0N/A compptr->quant_table = NULL;
0N/A }
0N/A
0N/A /* Compute number of fully interleaved MCU rows. */
0N/A cinfo->total_iMCU_rows = (JDIMENSION)
0N/A jdiv_round_up((long) cinfo->image_height,
0N/A (long) (cinfo->max_v_samp_factor*DCTSIZE));
0N/A
0N/A /* Decide whether file contains multiple scans */
0N/A if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
0N/A cinfo->inputctl->has_multiple_scans = TRUE;
0N/A else
0N/A cinfo->inputctl->has_multiple_scans = FALSE;
0N/A}
0N/A
0N/A
0N/ALOCAL(void)
0N/Aper_scan_setup (j_decompress_ptr cinfo)
0N/A/* Do computations that are needed before processing a JPEG scan */
0N/A/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
0N/A{
0N/A int ci, mcublks, tmp;
0N/A jpeg_component_info *compptr;
0N/A
0N/A if (cinfo->comps_in_scan == 1) {
0N/A
0N/A /* Noninterleaved (single-component) scan */
0N/A compptr = cinfo->cur_comp_info[0];
0N/A
0N/A /* Overall image size in MCUs */
0N/A cinfo->MCUs_per_row = compptr->width_in_blocks;
0N/A cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
0N/A
0N/A /* For noninterleaved scan, always one block per MCU */
0N/A compptr->MCU_width = 1;
0N/A compptr->MCU_height = 1;
0N/A compptr->MCU_blocks = 1;
0N/A compptr->MCU_sample_width = compptr->DCT_scaled_size;
0N/A compptr->last_col_width = 1;
0N/A /* For noninterleaved scans, it is convenient to define last_row_height
0N/A * as the number of block rows present in the last iMCU row.
0N/A */
0N/A tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
0N/A if (tmp == 0) tmp = compptr->v_samp_factor;
0N/A compptr->last_row_height = tmp;
0N/A
0N/A /* Prepare array describing MCU composition */
0N/A cinfo->blocks_in_MCU = 1;
0N/A cinfo->MCU_membership[0] = 0;
0N/A
0N/A } else {
0N/A
0N/A /* Interleaved (multi-component) scan */
0N/A if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
0N/A ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
0N/A MAX_COMPS_IN_SCAN);
0N/A
0N/A /* Overall image size in MCUs */
0N/A cinfo->MCUs_per_row = (JDIMENSION)
0N/A jdiv_round_up((long) cinfo->image_width,
0N/A (long) (cinfo->max_h_samp_factor*DCTSIZE));
0N/A cinfo->MCU_rows_in_scan = (JDIMENSION)
0N/A jdiv_round_up((long) cinfo->image_height,
0N/A (long) (cinfo->max_v_samp_factor*DCTSIZE));
0N/A
0N/A cinfo->blocks_in_MCU = 0;
0N/A
0N/A for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
0N/A compptr = cinfo->cur_comp_info[ci];
0N/A /* Sampling factors give # of blocks of component in each MCU */
0N/A compptr->MCU_width = compptr->h_samp_factor;
0N/A compptr->MCU_height = compptr->v_samp_factor;
0N/A compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
0N/A compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
0N/A /* Figure number of non-dummy blocks in last MCU column & row */
0N/A tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
0N/A if (tmp == 0) tmp = compptr->MCU_width;
0N/A compptr->last_col_width = tmp;
0N/A tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
0N/A if (tmp == 0) tmp = compptr->MCU_height;
0N/A compptr->last_row_height = tmp;
0N/A /* Prepare array describing MCU composition */
0N/A mcublks = compptr->MCU_blocks;
0N/A if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
0N/A ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
0N/A while (mcublks-- > 0) {
0N/A cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
0N/A }
0N/A }
0N/A
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Save away a copy of the Q-table referenced by each component present
0N/A * in the current scan, unless already saved during a prior scan.
0N/A *
0N/A * In a multiple-scan JPEG file, the encoder could assign different components
0N/A * the same Q-table slot number, but change table definitions between scans
0N/A * so that each component uses a different Q-table. (The IJG encoder is not
0N/A * currently capable of doing this, but other encoders might.) Since we want
0N/A * to be able to dequantize all the components at the end of the file, this
0N/A * means that we have to save away the table actually used for each component.
0N/A * We do this by copying the table at the start of the first scan containing
0N/A * the component.
0N/A * The JPEG spec prohibits the encoder from changing the contents of a Q-table
0N/A * slot between scans of a component using that slot. If the encoder does so
0N/A * anyway, this decoder will simply use the Q-table values that were current
0N/A * at the start of the first scan for the component.
0N/A *
0N/A * The decompressor output side looks only at the saved quant tables,
0N/A * not at the current Q-table slots.
0N/A */
0N/A
0N/ALOCAL(void)
0N/Alatch_quant_tables (j_decompress_ptr cinfo)
0N/A{
0N/A int ci, qtblno;
0N/A jpeg_component_info *compptr;
0N/A JQUANT_TBL * qtbl;
0N/A
0N/A for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
0N/A compptr = cinfo->cur_comp_info[ci];
0N/A /* No work if we already saved Q-table for this component */
0N/A if (compptr->quant_table != NULL)
0N/A continue;
0N/A /* Make sure specified quantization table is present */
0N/A qtblno = compptr->quant_tbl_no;
0N/A if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
0N/A cinfo->quant_tbl_ptrs[qtblno] == NULL)
0N/A ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
0N/A /* OK, save away the quantization table */
0N/A qtbl = (JQUANT_TBL *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(JQUANT_TBL));
0N/A MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
0N/A compptr->quant_table = qtbl;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Initialize the input modules to read a scan of compressed data.
0N/A * The first call to this is done by jdmaster.c after initializing
0N/A * the entire decompressor (during jpeg_start_decompress).
0N/A * Subsequent calls come from consume_markers, below.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Astart_input_pass (j_decompress_ptr cinfo)
0N/A{
0N/A per_scan_setup(cinfo);
0N/A latch_quant_tables(cinfo);
0N/A (*cinfo->entropy->start_pass) (cinfo);
0N/A (*cinfo->coef->start_input_pass) (cinfo);
0N/A cinfo->inputctl->consume_input = cinfo->coef->consume_data;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Finish up after inputting a compressed-data scan.
0N/A * This is called by the coefficient controller after it's read all
0N/A * the expected data of the scan.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Afinish_input_pass (j_decompress_ptr cinfo)
0N/A{
0N/A cinfo->inputctl->consume_input = consume_markers;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Read JPEG markers before, between, or after compressed-data scans.
0N/A * Change state as necessary when a new scan is reached.
0N/A * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
0N/A *
0N/A * The consume_input method pointer points either here or to the
0N/A * coefficient controller's consume_data routine, depending on whether
0N/A * we are reading a compressed data segment or inter-segment markers.
0N/A */
0N/A
0N/AMETHODDEF(int)
0N/Aconsume_markers (j_decompress_ptr cinfo)
0N/A{
0N/A my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
0N/A int val;
0N/A
0N/A if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
0N/A return JPEG_REACHED_EOI;
0N/A
0N/A val = (*cinfo->marker->read_markers) (cinfo);
0N/A
0N/A switch (val) {
0N/A case JPEG_REACHED_SOS: /* Found SOS */
0N/A if (inputctl->inheaders) { /* 1st SOS */
0N/A initial_setup(cinfo);
0N/A inputctl->inheaders = FALSE;
0N/A /* Note: start_input_pass must be called by jdmaster.c
0N/A * before any more input can be consumed. jdapimin.c is
0N/A * responsible for enforcing this sequencing.
0N/A */
0N/A } else { /* 2nd or later SOS marker */
0N/A if (! inputctl->pub.has_multiple_scans)
0N/A ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
0N/A start_input_pass(cinfo);
0N/A }
0N/A break;
0N/A case JPEG_REACHED_EOI: /* Found EOI */
0N/A inputctl->pub.eoi_reached = TRUE;
0N/A if (inputctl->inheaders) { /* Tables-only datastream, apparently */
0N/A if (cinfo->marker->saw_SOF)
0N/A ERREXIT(cinfo, JERR_SOF_NO_SOS);
0N/A } else {
0N/A /* Prevent infinite loop in coef ctlr's decompress_data routine
0N/A * if user set output_scan_number larger than number of scans.
0N/A */
0N/A if (cinfo->output_scan_number > cinfo->input_scan_number)
0N/A cinfo->output_scan_number = cinfo->input_scan_number;
0N/A }
0N/A break;
0N/A case JPEG_SUSPENDED:
0N/A break;
0N/A }
0N/A
0N/A return val;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Reset state to begin a fresh datastream.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Areset_input_controller (j_decompress_ptr cinfo)
0N/A{
0N/A my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
0N/A
0N/A inputctl->pub.consume_input = consume_markers;
0N/A inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
0N/A inputctl->pub.eoi_reached = FALSE;
0N/A inputctl->inheaders = TRUE;
0N/A /* Reset other modules */
0N/A (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
0N/A (*cinfo->marker->reset_marker_reader) (cinfo);
0N/A /* Reset progression state -- would be cleaner if entropy decoder did this */
0N/A cinfo->coef_bits = NULL;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Initialize the input controller module.
0N/A * This is called only once, when the decompression object is created.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajinit_input_controller (j_decompress_ptr cinfo)
0N/A{
0N/A my_inputctl_ptr inputctl;
0N/A
0N/A /* Create subobject in permanent pool */
0N/A inputctl = (my_inputctl_ptr)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
0N/A SIZEOF(my_input_controller));
0N/A cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
0N/A /* Initialize method pointers */
0N/A inputctl->pub.consume_input = consume_markers;
0N/A inputctl->pub.reset_input_controller = reset_input_controller;
0N/A inputctl->pub.start_input_pass = start_input_pass;
0N/A inputctl->pub.finish_input_pass = finish_input_pass;
0N/A /* Initialize state: can't use reset_input_controller since we don't
0N/A * want to try to reset other modules yet.
0N/A */
0N/A inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
0N/A inputctl->pub.eoi_reached = FALSE;
0N/A inputctl->inheaders = TRUE;
0N/A}