/*1* jdapimin.c2*3* Copyright (C) 1994-1998, Thomas G. Lane.4* Modified 2009-2013 by Guido Vollbeding.5* This file is part of the Independent JPEG Group's software.6* For conditions of distribution and use, see the accompanying README file.7*8* This file contains application interface code for the decompression half9* of the JPEG library. These are the "minimum" API routines that may be10* needed in either the normal full-decompression case or the11* transcoding-only case.12*13* Most of the routines intended to be called directly by an application14* are in this file or in jdapistd.c. But also see jcomapi.c for routines15* shared by compression and decompression, and jdtrans.c for the transcoding16* case.17*/1819#define JPEG_INTERNALS20#include "jinclude.h"21#include "jpeglib.h"222324/*25* Initialization of a JPEG decompression object.26* The error manager must already be set up (in case memory manager fails).27*/2829GLOBAL(void)30jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)31{32int i;3334/* Guard against version mismatches between library and caller. */35cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */36if (version != JPEG_LIB_VERSION)37ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);38if (structsize != SIZEOF(struct jpeg_decompress_struct))39ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,40(int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);4142/* For debugging purposes, we zero the whole master structure.43* But the application has already set the err pointer, and may have set44* client_data, so we have to save and restore those fields.45* Note: if application hasn't set client_data, tools like Purify may46* complain here.47*/48{49struct jpeg_error_mgr * err = cinfo->err;50void * client_data = cinfo->client_data; /* ignore Purify complaint here */51MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));52cinfo->err = err;53cinfo->client_data = client_data;54}55cinfo->is_decompressor = TRUE;5657/* Initialize a memory manager instance for this object */58jinit_memory_mgr((j_common_ptr) cinfo);5960/* Zero out pointers to permanent structures. */61cinfo->progress = NULL;62cinfo->src = NULL;6364for (i = 0; i < NUM_QUANT_TBLS; i++)65cinfo->quant_tbl_ptrs[i] = NULL;6667for (i = 0; i < NUM_HUFF_TBLS; i++) {68cinfo->dc_huff_tbl_ptrs[i] = NULL;69cinfo->ac_huff_tbl_ptrs[i] = NULL;70}7172/* Initialize marker processor so application can override methods73* for COM, APPn markers before calling jpeg_read_header.74*/75cinfo->marker_list = NULL;76jinit_marker_reader(cinfo);7778/* And initialize the overall input controller. */79jinit_input_controller(cinfo);8081/* OK, I'm ready */82cinfo->global_state = DSTATE_START;83}848586/*87* Destruction of a JPEG decompression object88*/8990GLOBAL(void)91jpeg_destroy_decompress (j_decompress_ptr cinfo)92{93jpeg_destroy((j_common_ptr) cinfo); /* use common routine */94}959697/*98* Abort processing of a JPEG decompression operation,99* but don't destroy the object itself.100*/101102GLOBAL(void)103jpeg_abort_decompress (j_decompress_ptr cinfo)104{105jpeg_abort((j_common_ptr) cinfo); /* use common routine */106}107108109/*110* Set default decompression parameters.111*/112113LOCAL(void)114default_decompress_parms (j_decompress_ptr cinfo)115{116int cid0, cid1, cid2;117118/* Guess the input colorspace, and set output colorspace accordingly. */119/* Note application may override our guesses. */120switch (cinfo->num_components) {121case 1:122cinfo->jpeg_color_space = JCS_GRAYSCALE;123cinfo->out_color_space = JCS_GRAYSCALE;124break;125126case 3:127cid0 = cinfo->comp_info[0].component_id;128cid1 = cinfo->comp_info[1].component_id;129cid2 = cinfo->comp_info[2].component_id;130131/* First try to guess from the component IDs */132if (cid0 == 0x01 && cid1 == 0x02 && cid2 == 0x03)133cinfo->jpeg_color_space = JCS_YCbCr;134else if (cid0 == 0x01 && cid1 == 0x22 && cid2 == 0x23)135cinfo->jpeg_color_space = JCS_BG_YCC;136else if (cid0 == 0x52 && cid1 == 0x47 && cid2 == 0x42)137cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */138else if (cid0 == 0x72 && cid1 == 0x67 && cid2 == 0x62)139cinfo->jpeg_color_space = JCS_BG_RGB; /* ASCII 'r', 'g', 'b' */140else if (cinfo->saw_JFIF_marker)141cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */142else if (cinfo->saw_Adobe_marker) {143switch (cinfo->Adobe_transform) {144case 0:145cinfo->jpeg_color_space = JCS_RGB;146break;147case 1:148cinfo->jpeg_color_space = JCS_YCbCr;149break;150default:151WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);152cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */153break;154}155} else {156TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);157cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */158}159/* Always guess RGB is proper output colorspace. */160cinfo->out_color_space = JCS_RGB;161break;162163case 4:164if (cinfo->saw_Adobe_marker) {165switch (cinfo->Adobe_transform) {166case 0:167cinfo->jpeg_color_space = JCS_CMYK;168break;169case 2:170cinfo->jpeg_color_space = JCS_YCCK;171break;172default:173WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);174cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */175break;176}177} else {178/* No special markers, assume straight CMYK. */179cinfo->jpeg_color_space = JCS_CMYK;180}181cinfo->out_color_space = JCS_CMYK;182break;183184default:185cinfo->jpeg_color_space = JCS_UNKNOWN;186cinfo->out_color_space = JCS_UNKNOWN;187break;188}189190/* Set defaults for other decompression parameters. */191cinfo->scale_num = cinfo->block_size; /* 1:1 scaling */192cinfo->scale_denom = cinfo->block_size;193cinfo->output_gamma = 1.0;194cinfo->buffered_image = FALSE;195cinfo->raw_data_out = FALSE;196cinfo->dct_method = JDCT_DEFAULT;197cinfo->do_fancy_upsampling = TRUE;198cinfo->do_block_smoothing = TRUE;199cinfo->quantize_colors = FALSE;200/* We set these in case application only sets quantize_colors. */201cinfo->dither_mode = JDITHER_FS;202#ifdef QUANT_2PASS_SUPPORTED203cinfo->two_pass_quantize = TRUE;204#else205cinfo->two_pass_quantize = FALSE;206#endif207cinfo->desired_number_of_colors = 256;208cinfo->colormap = NULL;209/* Initialize for no mode change in buffered-image mode. */210cinfo->enable_1pass_quant = FALSE;211cinfo->enable_external_quant = FALSE;212cinfo->enable_2pass_quant = FALSE;213}214215216/*217* Decompression startup: read start of JPEG datastream to see what's there.218* Need only initialize JPEG object and supply a data source before calling.219*220* This routine will read as far as the first SOS marker (ie, actual start of221* compressed data), and will save all tables and parameters in the JPEG222* object. It will also initialize the decompression parameters to default223* values, and finally return JPEG_HEADER_OK. On return, the application may224* adjust the decompression parameters and then call jpeg_start_decompress.225* (Or, if the application only wanted to determine the image parameters,226* the data need not be decompressed. In that case, call jpeg_abort or227* jpeg_destroy to release any temporary space.)228* If an abbreviated (tables only) datastream is presented, the routine will229* return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then230* re-use the JPEG object to read the abbreviated image datastream(s).231* It is unnecessary (but OK) to call jpeg_abort in this case.232* The JPEG_SUSPENDED return code only occurs if the data source module233* requests suspension of the decompressor. In this case the application234* should load more source data and then re-call jpeg_read_header to resume235* processing.236* If a non-suspending data source is used and require_image is TRUE, then the237* return code need not be inspected since only JPEG_HEADER_OK is possible.238*239* This routine is now just a front end to jpeg_consume_input, with some240* extra error checking.241*/242243GLOBAL(int)244jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)245{246int retcode;247248if (cinfo->global_state != DSTATE_START &&249cinfo->global_state != DSTATE_INHEADER)250ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);251252retcode = jpeg_consume_input(cinfo);253254switch (retcode) {255case JPEG_REACHED_SOS:256retcode = JPEG_HEADER_OK;257break;258case JPEG_REACHED_EOI:259if (require_image) /* Complain if application wanted an image */260ERREXIT(cinfo, JERR_NO_IMAGE);261/* Reset to start state; it would be safer to require the application to262* call jpeg_abort, but we can't change it now for compatibility reasons.263* A side effect is to free any temporary memory (there shouldn't be any).264*/265jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */266retcode = JPEG_HEADER_TABLES_ONLY;267break;268case JPEG_SUSPENDED:269/* no work */270break;271}272273return retcode;274}275276277/*278* Consume data in advance of what the decompressor requires.279* This can be called at any time once the decompressor object has280* been created and a data source has been set up.281*282* This routine is essentially a state machine that handles a couple283* of critical state-transition actions, namely initial setup and284* transition from header scanning to ready-for-start_decompress.285* All the actual input is done via the input controller's consume_input286* method.287*/288289GLOBAL(int)290jpeg_consume_input (j_decompress_ptr cinfo)291{292int retcode = JPEG_SUSPENDED;293294/* NB: every possible DSTATE value should be listed in this switch */295switch (cinfo->global_state) {296case DSTATE_START:297/* Start-of-datastream actions: reset appropriate modules */298(*cinfo->inputctl->reset_input_controller) (cinfo);299/* Initialize application's data source module */300(*cinfo->src->init_source) (cinfo);301cinfo->global_state = DSTATE_INHEADER;302/*FALLTHROUGH*/303case DSTATE_INHEADER:304retcode = (*cinfo->inputctl->consume_input) (cinfo);305if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */306/* Set up default parameters based on header data */307default_decompress_parms(cinfo);308/* Set global state: ready for start_decompress */309cinfo->global_state = DSTATE_READY;310}311break;312case DSTATE_READY:313/* Can't advance past first SOS until start_decompress is called */314retcode = JPEG_REACHED_SOS;315break;316case DSTATE_PRELOAD:317case DSTATE_PRESCAN:318case DSTATE_SCANNING:319case DSTATE_RAW_OK:320case DSTATE_BUFIMAGE:321case DSTATE_BUFPOST:322case DSTATE_STOPPING:323retcode = (*cinfo->inputctl->consume_input) (cinfo);324break;325default:326ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);327}328return retcode;329}330331332/*333* Have we finished reading the input file?334*/335336GLOBAL(boolean)337jpeg_input_complete (j_decompress_ptr cinfo)338{339/* Check for valid jpeg object */340if (cinfo->global_state < DSTATE_START ||341cinfo->global_state > DSTATE_STOPPING)342ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);343return cinfo->inputctl->eoi_reached;344}345346347/*348* Is there more than one scan?349*/350351GLOBAL(boolean)352jpeg_has_multiple_scans (j_decompress_ptr cinfo)353{354/* Only valid after jpeg_read_header completes */355if (cinfo->global_state < DSTATE_READY ||356cinfo->global_state > DSTATE_STOPPING)357ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);358return cinfo->inputctl->has_multiple_scans;359}360361362/*363* Finish JPEG decompression.364*365* This will normally just verify the file trailer and release temp storage.366*367* Returns FALSE if suspended. The return value need be inspected only if368* a suspending data source is used.369*/370371GLOBAL(boolean)372jpeg_finish_decompress (j_decompress_ptr cinfo)373{374if ((cinfo->global_state == DSTATE_SCANNING ||375cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {376/* Terminate final pass of non-buffered mode */377if (cinfo->output_scanline < cinfo->output_height)378ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);379(*cinfo->master->finish_output_pass) (cinfo);380cinfo->global_state = DSTATE_STOPPING;381} else if (cinfo->global_state == DSTATE_BUFIMAGE) {382/* Finishing after a buffered-image operation */383cinfo->global_state = DSTATE_STOPPING;384} else if (cinfo->global_state != DSTATE_STOPPING) {385/* STOPPING = repeat call after a suspension, anything else is error */386ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);387}388/* Read until EOI */389while (! cinfo->inputctl->eoi_reached) {390if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)391return FALSE; /* Suspend, come back later */392}393/* Do final cleanup */394(*cinfo->src->term_source) (cinfo);395/* We can use jpeg_abort to release memory and reset global_state */396jpeg_abort((j_common_ptr) cinfo);397return TRUE;398}399400401