0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jdapimin.c
0N/A *
0N/A * Copyright (C) 1994-1998, 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 application interface code for the decompression half
0N/A * of the JPEG library. These are the "minimum" API routines that may be
0N/A * needed in either the normal full-decompression case or the
0N/A * transcoding-only case.
0N/A *
0N/A * Most of the routines intended to be called directly by an application
0N/A * are in this file or in jdapistd.c. But also see jcomapi.c for routines
0N/A * shared by compression and decompression, and jdtrans.c for the transcoding
0N/A * case.
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/*
0N/A * Initialization of a JPEG decompression object.
0N/A * The error manager must already be set up (in case memory manager fails).
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
0N/A{
0N/A int i;
0N/A
0N/A /* Guard against version mismatches between library and caller. */
0N/A cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
0N/A if (version != JPEG_LIB_VERSION)
0N/A ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
0N/A if (structsize != SIZEOF(struct jpeg_decompress_struct))
0N/A ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
0N/A (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
0N/A
0N/A /* For debugging purposes, we zero the whole master structure.
0N/A * But the application has already set the err pointer, and may have set
0N/A * client_data, so we have to save and restore those fields.
0N/A * Note: if application hasn't set client_data, tools like Purify may
0N/A * complain here.
0N/A */
0N/A {
0N/A struct jpeg_error_mgr * err = cinfo->err;
0N/A void * client_data = cinfo->client_data; /* ignore Purify complaint here */
0N/A MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
0N/A cinfo->err = err;
0N/A cinfo->client_data = client_data;
0N/A }
0N/A cinfo->is_decompressor = TRUE;
0N/A
0N/A /* Initialize a memory manager instance for this object */
0N/A jinit_memory_mgr((j_common_ptr) cinfo);
0N/A
0N/A /* Zero out pointers to permanent structures. */
0N/A cinfo->progress = NULL;
0N/A cinfo->src = NULL;
0N/A
0N/A for (i = 0; i < NUM_QUANT_TBLS; i++)
0N/A cinfo->quant_tbl_ptrs[i] = NULL;
0N/A
0N/A for (i = 0; i < NUM_HUFF_TBLS; i++) {
0N/A cinfo->dc_huff_tbl_ptrs[i] = NULL;
0N/A cinfo->ac_huff_tbl_ptrs[i] = NULL;
0N/A }
0N/A
0N/A /* Initialize marker processor so application can override methods
0N/A * for COM, APPn markers before calling jpeg_read_header.
0N/A */
0N/A cinfo->marker_list = NULL;
0N/A jinit_marker_reader(cinfo);
0N/A
0N/A /* And initialize the overall input controller. */
0N/A jinit_input_controller(cinfo);
0N/A
0N/A /* OK, I'm ready */
0N/A cinfo->global_state = DSTATE_START;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Destruction of a JPEG decompression object
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajpeg_destroy_decompress (j_decompress_ptr cinfo)
0N/A{
0N/A jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Abort processing of a JPEG decompression operation,
0N/A * but don't destroy the object itself.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajpeg_abort_decompress (j_decompress_ptr cinfo)
0N/A{
0N/A jpeg_abort((j_common_ptr) cinfo); /* use common routine */
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Set default decompression parameters.
0N/A */
0N/A
0N/ALOCAL(void)
0N/Adefault_decompress_parms (j_decompress_ptr cinfo)
0N/A{
0N/A /* Guess the input colorspace, and set output colorspace accordingly. */
0N/A /* (Wish JPEG committee had provided a real way to specify this...) */
0N/A /* Note application may override our guesses. */
0N/A switch (cinfo->num_components) {
0N/A case 1:
0N/A cinfo->jpeg_color_space = JCS_GRAYSCALE;
0N/A cinfo->out_color_space = JCS_GRAYSCALE;
0N/A break;
0N/A
0N/A case 3:
0N/A if (cinfo->saw_JFIF_marker) {
0N/A cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
0N/A } else if (cinfo->saw_Adobe_marker) {
0N/A switch (cinfo->Adobe_transform) {
0N/A case 0:
0N/A cinfo->jpeg_color_space = JCS_RGB;
0N/A break;
0N/A case 1:
0N/A cinfo->jpeg_color_space = JCS_YCbCr;
0N/A break;
0N/A default:
0N/A WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
0N/A cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
0N/A break;
0N/A }
0N/A } else {
0N/A /* Saw no special markers, try to guess from the component IDs */
0N/A int cid0 = cinfo->comp_info[0].component_id;
0N/A int cid1 = cinfo->comp_info[1].component_id;
0N/A int cid2 = cinfo->comp_info[2].component_id;
0N/A
0N/A if (cid0 == 1 && cid1 == 2 && cid2 == 3)
0N/A cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
0N/A else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
0N/A cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
0N/A else {
0N/A TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
0N/A cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
0N/A }
0N/A }
0N/A /* Always guess RGB is proper output colorspace. */
0N/A cinfo->out_color_space = JCS_RGB;
0N/A break;
0N/A
0N/A case 4:
0N/A if (cinfo->saw_Adobe_marker) {
0N/A switch (cinfo->Adobe_transform) {
0N/A case 0:
0N/A cinfo->jpeg_color_space = JCS_CMYK;
0N/A break;
0N/A case 2:
0N/A cinfo->jpeg_color_space = JCS_YCCK;
0N/A break;
0N/A default:
0N/A WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
0N/A cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
0N/A break;
0N/A }
0N/A } else {
0N/A /* No special markers, assume straight CMYK. */
0N/A cinfo->jpeg_color_space = JCS_CMYK;
0N/A }
0N/A cinfo->out_color_space = JCS_CMYK;
0N/A break;
0N/A
0N/A default:
0N/A cinfo->jpeg_color_space = JCS_UNKNOWN;
0N/A cinfo->out_color_space = JCS_UNKNOWN;
0N/A break;
0N/A }
0N/A
0N/A /* Set defaults for other decompression parameters. */
0N/A cinfo->scale_num = 1; /* 1:1 scaling */
0N/A cinfo->scale_denom = 1;
0N/A cinfo->output_gamma = 1.0;
0N/A cinfo->buffered_image = FALSE;
0N/A cinfo->raw_data_out = FALSE;
0N/A cinfo->dct_method = JDCT_DEFAULT;
0N/A cinfo->do_fancy_upsampling = TRUE;
0N/A cinfo->do_block_smoothing = TRUE;
0N/A cinfo->quantize_colors = FALSE;
0N/A /* We set these in case application only sets quantize_colors. */
0N/A cinfo->dither_mode = JDITHER_FS;
0N/A#ifdef QUANT_2PASS_SUPPORTED
0N/A cinfo->two_pass_quantize = TRUE;
0N/A#else
0N/A cinfo->two_pass_quantize = FALSE;
0N/A#endif
0N/A cinfo->desired_number_of_colors = 256;
0N/A cinfo->colormap = NULL;
0N/A /* Initialize for no mode change in buffered-image mode. */
0N/A cinfo->enable_1pass_quant = FALSE;
0N/A cinfo->enable_external_quant = FALSE;
0N/A cinfo->enable_2pass_quant = FALSE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Decompression startup: read start of JPEG datastream to see what's there.
0N/A * Need only initialize JPEG object and supply a data source before calling.
0N/A *
0N/A * This routine will read as far as the first SOS marker (ie, actual start of
0N/A * compressed data), and will save all tables and parameters in the JPEG
0N/A * object. It will also initialize the decompression parameters to default
0N/A * values, and finally return JPEG_HEADER_OK. On return, the application may
0N/A * adjust the decompression parameters and then call jpeg_start_decompress.
0N/A * (Or, if the application only wanted to determine the image parameters,
0N/A * the data need not be decompressed. In that case, call jpeg_abort or
0N/A * jpeg_destroy to release any temporary space.)
0N/A * If an abbreviated (tables only) datastream is presented, the routine will
0N/A * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
0N/A * re-use the JPEG object to read the abbreviated image datastream(s).
0N/A * It is unnecessary (but OK) to call jpeg_abort in this case.
0N/A * The JPEG_SUSPENDED return code only occurs if the data source module
0N/A * requests suspension of the decompressor. In this case the application
0N/A * should load more source data and then re-call jpeg_read_header to resume
0N/A * processing.
0N/A * If a non-suspending data source is used and require_image is TRUE, then the
0N/A * return code need not be inspected since only JPEG_HEADER_OK is possible.
0N/A *
0N/A * This routine is now just a front end to jpeg_consume_input, with some
0N/A * extra error checking.
0N/A */
0N/A
0N/AGLOBAL(int)
0N/Ajpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
0N/A{
0N/A int retcode;
0N/A
0N/A if (cinfo->global_state != DSTATE_START &&
0N/A cinfo->global_state != DSTATE_INHEADER)
0N/A ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
0N/A
0N/A retcode = jpeg_consume_input(cinfo);
0N/A
0N/A switch (retcode) {
0N/A case JPEG_REACHED_SOS:
0N/A retcode = JPEG_HEADER_OK;
0N/A break;
0N/A case JPEG_REACHED_EOI:
0N/A if (require_image) /* Complain if application wanted an image */
0N/A ERREXIT(cinfo, JERR_NO_IMAGE);
0N/A /* Reset to start state; it would be safer to require the application to
0N/A * call jpeg_abort, but we can't change it now for compatibility reasons.
0N/A * A side effect is to free any temporary memory (there shouldn't be any).
0N/A */
0N/A jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
0N/A retcode = JPEG_HEADER_TABLES_ONLY;
0N/A break;
0N/A case JPEG_SUSPENDED:
0N/A /* no work */
0N/A break;
0N/A }
0N/A
0N/A return retcode;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Consume data in advance of what the decompressor requires.
0N/A * This can be called at any time once the decompressor object has
0N/A * been created and a data source has been set up.
0N/A *
0N/A * This routine is essentially a state machine that handles a couple
0N/A * of critical state-transition actions, namely initial setup and
0N/A * transition from header scanning to ready-for-start_decompress.
0N/A * All the actual input is done via the input controller's consume_input
0N/A * method.
0N/A */
0N/A
0N/AGLOBAL(int)
0N/Ajpeg_consume_input (j_decompress_ptr cinfo)
0N/A{
0N/A int retcode = JPEG_SUSPENDED;
0N/A
0N/A /* NB: every possible DSTATE value should be listed in this switch */
0N/A switch (cinfo->global_state) {
0N/A case DSTATE_START:
0N/A /* Start-of-datastream actions: reset appropriate modules */
0N/A (*cinfo->inputctl->reset_input_controller) (cinfo);
0N/A /* Initialize application's data source module */
0N/A (*cinfo->src->init_source) (cinfo);
0N/A cinfo->global_state = DSTATE_INHEADER;
0N/A /*FALLTHROUGH*/
0N/A case DSTATE_INHEADER:
0N/A retcode = (*cinfo->inputctl->consume_input) (cinfo);
0N/A if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
0N/A /* Set up default parameters based on header data */
0N/A default_decompress_parms(cinfo);
0N/A /* Set global state: ready for start_decompress */
0N/A cinfo->global_state = DSTATE_READY;
0N/A }
0N/A break;
0N/A case DSTATE_READY:
0N/A /* Can't advance past first SOS until start_decompress is called */
0N/A retcode = JPEG_REACHED_SOS;
0N/A break;
0N/A case DSTATE_PRELOAD:
0N/A case DSTATE_PRESCAN:
0N/A case DSTATE_SCANNING:
0N/A case DSTATE_RAW_OK:
0N/A case DSTATE_BUFIMAGE:
0N/A case DSTATE_BUFPOST:
0N/A case DSTATE_STOPPING:
0N/A retcode = (*cinfo->inputctl->consume_input) (cinfo);
0N/A break;
0N/A default:
0N/A ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
0N/A }
0N/A return retcode;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Have we finished reading the input file?
0N/A */
0N/A
0N/AGLOBAL(boolean)
0N/Ajpeg_input_complete (j_decompress_ptr cinfo)
0N/A{
0N/A /* Check for valid jpeg object */
0N/A if (cinfo->global_state < DSTATE_START ||
0N/A cinfo->global_state > DSTATE_STOPPING)
0N/A ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
0N/A return cinfo->inputctl->eoi_reached;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Is there more than one scan?
0N/A */
0N/A
0N/AGLOBAL(boolean)
0N/Ajpeg_has_multiple_scans (j_decompress_ptr cinfo)
0N/A{
0N/A /* Only valid after jpeg_read_header completes */
0N/A if (cinfo->global_state < DSTATE_READY ||
0N/A cinfo->global_state > DSTATE_STOPPING)
0N/A ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
0N/A return cinfo->inputctl->has_multiple_scans;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Finish JPEG decompression.
0N/A *
0N/A * This will normally just verify the file trailer and release temp storage.
0N/A *
0N/A * Returns FALSE if suspended. The return value need be inspected only if
0N/A * a suspending data source is used.
0N/A */
0N/A
0N/AGLOBAL(boolean)
0N/Ajpeg_finish_decompress (j_decompress_ptr cinfo)
0N/A{
0N/A if ((cinfo->global_state == DSTATE_SCANNING ||
0N/A cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
0N/A /* Terminate final pass of non-buffered mode */
0N/A if (cinfo->output_scanline < cinfo->output_height)
0N/A ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
0N/A (*cinfo->master->finish_output_pass) (cinfo);
0N/A cinfo->global_state = DSTATE_STOPPING;
0N/A } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
0N/A /* Finishing after a buffered-image operation */
0N/A cinfo->global_state = DSTATE_STOPPING;
0N/A } else if (cinfo->global_state != DSTATE_STOPPING) {
0N/A /* STOPPING = repeat call after a suspension, anything else is error */
0N/A ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
0N/A }
0N/A /* Read until EOI */
0N/A while (! cinfo->inputctl->eoi_reached) {
0N/A if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
0N/A return FALSE; /* Suspend, come back later */
0N/A }
0N/A /* Do final cleanup */
0N/A (*cinfo->src->term_source) (cinfo);
0N/A /* We can use jpeg_abort to release memory and reset global_state */
0N/A jpeg_abort((j_common_ptr) cinfo);
0N/A return TRUE;
0N/A}