/*1* jdapimin.c2*3* Copyright (C) 1994-1998, Thomas G. Lane.4* Modified 2009-2020 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, cid3;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/* For robust detection of standard colorspaces132* regardless of the presence of special markers,133* check component IDs from SOF marker first.134*/135if (cid0 == 0x01 && cid1 == 0x02 && cid2 == 0x03)136cinfo->jpeg_color_space = JCS_YCbCr;137else if (cid0 == 0x01 && cid1 == 0x22 && cid2 == 0x23)138cinfo->jpeg_color_space = JCS_BG_YCC;139else if (cid0 == 0x52 && cid1 == 0x47 && cid2 == 0x42)140cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */141else if (cid0 == 0x72 && cid1 == 0x67 && cid2 == 0x62)142cinfo->jpeg_color_space = JCS_BG_RGB; /* ASCII 'r', 'g', 'b' */143else if (cinfo->saw_JFIF_marker)144cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */145else if (cinfo->saw_Adobe_marker) {146switch (cinfo->Adobe_transform) {147case 0:148cinfo->jpeg_color_space = JCS_RGB;149break;150case 1:151cinfo->jpeg_color_space = JCS_YCbCr;152break;153default:154WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);155cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */156}157} else {158TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);159cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */160}161/* Always guess RGB is proper output colorspace. */162cinfo->out_color_space = JCS_RGB;163break;164165case 4:166cid0 = cinfo->comp_info[0].component_id;167cid1 = cinfo->comp_info[1].component_id;168cid2 = cinfo->comp_info[2].component_id;169cid3 = cinfo->comp_info[3].component_id;170171/* For robust detection of standard colorspaces172* regardless of the presence of special markers,173* check component IDs from SOF marker first.174*/175if (cid0 == 0x01 && cid1 == 0x02 && cid2 == 0x03 && cid3 == 0x04)176cinfo->jpeg_color_space = JCS_YCCK;177else if (cid0 == 0x43 && cid1 == 0x4D && cid2 == 0x59 && cid3 == 0x4B)178cinfo->jpeg_color_space = JCS_CMYK; /* ASCII 'C', 'M', 'Y', 'K' */179else if (cinfo->saw_Adobe_marker) {180switch (cinfo->Adobe_transform) {181case 0:182cinfo->jpeg_color_space = JCS_CMYK;183break;184case 2:185cinfo->jpeg_color_space = JCS_YCCK;186break;187default:188WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);189cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */190}191} else {192/* Unknown IDs and no special markers, assume straight CMYK. */193cinfo->jpeg_color_space = JCS_CMYK;194}195cinfo->out_color_space = JCS_CMYK;196break;197198default:199cinfo->jpeg_color_space = JCS_UNKNOWN;200cinfo->out_color_space = JCS_UNKNOWN;201}202203/* Set defaults for other decompression parameters. */204cinfo->scale_num = cinfo->block_size; /* 1:1 scaling */205cinfo->scale_denom = cinfo->block_size;206cinfo->output_gamma = 1.0;207cinfo->buffered_image = FALSE;208cinfo->raw_data_out = FALSE;209cinfo->dct_method = JDCT_DEFAULT;210cinfo->do_fancy_upsampling = TRUE;211cinfo->do_block_smoothing = TRUE;212cinfo->quantize_colors = FALSE;213/* We set these in case application only sets quantize_colors. */214cinfo->dither_mode = JDITHER_FS;215#ifdef QUANT_2PASS_SUPPORTED216cinfo->two_pass_quantize = TRUE;217#else218cinfo->two_pass_quantize = FALSE;219#endif220cinfo->desired_number_of_colors = 256;221cinfo->colormap = NULL;222/* Initialize for no mode change in buffered-image mode. */223cinfo->enable_1pass_quant = FALSE;224cinfo->enable_external_quant = FALSE;225cinfo->enable_2pass_quant = FALSE;226}227228229/*230* Decompression startup: read start of JPEG datastream to see what's there.231* Need only initialize JPEG object and supply a data source before calling.232*233* This routine will read as far as the first SOS marker (ie, actual start of234* compressed data), and will save all tables and parameters in the JPEG235* object. It will also initialize the decompression parameters to default236* values, and finally return JPEG_HEADER_OK. On return, the application may237* adjust the decompression parameters and then call jpeg_start_decompress.238* (Or, if the application only wanted to determine the image parameters,239* the data need not be decompressed. In that case, call jpeg_abort or240* jpeg_destroy to release any temporary space.)241* If an abbreviated (tables only) datastream is presented, the routine will242* return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then243* re-use the JPEG object to read the abbreviated image datastream(s).244* It is unnecessary (but OK) to call jpeg_abort in this case.245* The JPEG_SUSPENDED return code only occurs if the data source module246* requests suspension of the decompressor. In this case the application247* should load more source data and then re-call jpeg_read_header to resume248* processing.249* If a non-suspending data source is used and require_image is TRUE, then the250* return code need not be inspected since only JPEG_HEADER_OK is possible.251*252* This routine is now just a front end to jpeg_consume_input, with some253* extra error checking.254*/255256GLOBAL(int)257jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)258{259int retcode;260261if (cinfo->global_state != DSTATE_START &&262cinfo->global_state != DSTATE_INHEADER)263ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);264265retcode = jpeg_consume_input(cinfo);266267switch (retcode) {268case JPEG_REACHED_SOS:269retcode = JPEG_HEADER_OK;270break;271case JPEG_REACHED_EOI:272if (require_image) /* Complain if application wanted an image */273ERREXIT(cinfo, JERR_NO_IMAGE);274/* Reset to start state; it would be safer to require the application to275* call jpeg_abort, but we can't change it now for compatibility reasons.276* A side effect is to free any temporary memory (there shouldn't be any).277*/278jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */279retcode = JPEG_HEADER_TABLES_ONLY;280break;281case JPEG_SUSPENDED:282/* no work */283break;284}285286return retcode;287}288289290/*291* Consume data in advance of what the decompressor requires.292* This can be called at any time once the decompressor object has293* been created and a data source has been set up.294*295* This routine is essentially a state machine that handles a couple296* of critical state-transition actions, namely initial setup and297* transition from header scanning to ready-for-start_decompress.298* All the actual input is done via the input controller's consume_input299* method.300*/301302GLOBAL(int)303jpeg_consume_input (j_decompress_ptr cinfo)304{305int retcode = JPEG_SUSPENDED;306307/* NB: every possible DSTATE value should be listed in this switch */308switch (cinfo->global_state) {309case DSTATE_START:310/* Start-of-datastream actions: reset appropriate modules */311(*cinfo->inputctl->reset_input_controller) (cinfo);312/* Initialize application's data source module */313(*cinfo->src->init_source) (cinfo);314cinfo->global_state = DSTATE_INHEADER;315/*FALLTHROUGH*/316case DSTATE_INHEADER:317retcode = (*cinfo->inputctl->consume_input) (cinfo);318if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */319/* Set up default parameters based on header data */320default_decompress_parms(cinfo);321/* Set global state: ready for start_decompress */322cinfo->global_state = DSTATE_READY;323}324break;325case DSTATE_READY:326/* Can't advance past first SOS until start_decompress is called */327retcode = JPEG_REACHED_SOS;328break;329case DSTATE_PRELOAD:330case DSTATE_PRESCAN:331case DSTATE_SCANNING:332case DSTATE_RAW_OK:333case DSTATE_BUFIMAGE:334case DSTATE_BUFPOST:335case DSTATE_STOPPING:336retcode = (*cinfo->inputctl->consume_input) (cinfo);337break;338default:339ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);340}341return retcode;342}343344345/*346* Have we finished reading the input file?347*/348349GLOBAL(boolean)350jpeg_input_complete (j_decompress_ptr cinfo)351{352/* Check for valid jpeg object */353if (cinfo->global_state < DSTATE_START ||354cinfo->global_state > DSTATE_STOPPING)355ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);356return cinfo->inputctl->eoi_reached;357}358359360/*361* Is there more than one scan?362*/363364GLOBAL(boolean)365jpeg_has_multiple_scans (j_decompress_ptr cinfo)366{367/* Only valid after jpeg_read_header completes */368if (cinfo->global_state < DSTATE_READY ||369cinfo->global_state > DSTATE_STOPPING)370ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);371return cinfo->inputctl->has_multiple_scans;372}373374375/*376* Finish JPEG decompression.377*378* This will normally just verify the file trailer and release temp storage.379*380* Returns FALSE if suspended. The return value need be inspected only if381* a suspending data source is used.382*/383384GLOBAL(boolean)385jpeg_finish_decompress (j_decompress_ptr cinfo)386{387if ((cinfo->global_state == DSTATE_SCANNING ||388cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {389/* Terminate final pass of non-buffered mode */390if (cinfo->output_scanline < cinfo->output_height)391ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);392(*cinfo->master->finish_output_pass) (cinfo);393cinfo->global_state = DSTATE_STOPPING;394} else if (cinfo->global_state == DSTATE_BUFIMAGE) {395/* Finishing after a buffered-image operation */396cinfo->global_state = DSTATE_STOPPING;397} else if (cinfo->global_state != DSTATE_STOPPING) {398/* STOPPING = repeat call after a suspension, anything else is error */399ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);400}401/* Read until EOI */402while (! cinfo->inputctl->eoi_reached) {403if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)404return FALSE; /* Suspend, come back later */405}406/* Do final cleanup */407(*cinfo->src->term_source) (cinfo);408/* We can use jpeg_abort to release memory and reset global_state */409jpeg_abort((j_common_ptr) cinfo);410return TRUE;411}412413414