Path: blob/master/3rdparty/libjpeg-turbo/src/jdapimin.c
16337 views
/*1* jdapimin.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1998, Thomas G. Lane.5* libjpeg-turbo Modifications:6* Copyright (C) 2016, D. R. Commander.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains application interface code for the decompression half11* of the JPEG library. These are the "minimum" API routines that may be12* needed in either the normal full-decompression case or the13* transcoding-only case.14*15* Most of the routines intended to be called directly by an application16* are in this file or in jdapistd.c. But also see jcomapi.c for routines17* shared by compression and decompression, and jdtrans.c for the transcoding18* case.19*/2021#define JPEG_INTERNALS22#include "jinclude.h"23#include "jpeglib.h"24#include "jdmaster.h"252627/*28* Initialization of a JPEG decompression object.29* The error manager must already be set up (in case memory manager fails).30*/3132GLOBAL(void)33jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)34{35int i;3637/* Guard against version mismatches between library and caller. */38cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */39if (version != JPEG_LIB_VERSION)40ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);41if (structsize != sizeof(struct jpeg_decompress_struct))42ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,43(int) sizeof(struct jpeg_decompress_struct), (int) structsize);4445/* For debugging purposes, we zero the whole master structure.46* But the application has already set the err pointer, and may have set47* client_data, so we have to save and restore those fields.48* Note: if application hasn't set client_data, tools like Purify may49* complain here.50*/51{52struct jpeg_error_mgr * err = cinfo->err;53void * client_data = cinfo->client_data; /* ignore Purify complaint here */54MEMZERO(cinfo, sizeof(struct jpeg_decompress_struct));55cinfo->err = err;56cinfo->client_data = client_data;57}58cinfo->is_decompressor = TRUE;5960/* Initialize a memory manager instance for this object */61jinit_memory_mgr((j_common_ptr) cinfo);6263/* Zero out pointers to permanent structures. */64cinfo->progress = NULL;65cinfo->src = NULL;6667for (i = 0; i < NUM_QUANT_TBLS; i++)68cinfo->quant_tbl_ptrs[i] = NULL;6970for (i = 0; i < NUM_HUFF_TBLS; i++) {71cinfo->dc_huff_tbl_ptrs[i] = NULL;72cinfo->ac_huff_tbl_ptrs[i] = NULL;73}7475/* Initialize marker processor so application can override methods76* for COM, APPn markers before calling jpeg_read_header.77*/78cinfo->marker_list = NULL;79jinit_marker_reader(cinfo);8081/* And initialize the overall input controller. */82jinit_input_controller(cinfo);8384/* OK, I'm ready */85cinfo->global_state = DSTATE_START;8687/* The master struct is used to store extension parameters, so we allocate it88* here.89*/90cinfo->master = (struct jpeg_decomp_master *)91(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,92sizeof(my_decomp_master));93MEMZERO(cinfo->master, sizeof(my_decomp_master));94}959697/*98* Destruction of a JPEG decompression object99*/100101GLOBAL(void)102jpeg_destroy_decompress (j_decompress_ptr cinfo)103{104jpeg_destroy((j_common_ptr) cinfo); /* use common routine */105}106107108/*109* Abort processing of a JPEG decompression operation,110* but don't destroy the object itself.111*/112113GLOBAL(void)114jpeg_abort_decompress (j_decompress_ptr cinfo)115{116jpeg_abort((j_common_ptr) cinfo); /* use common routine */117}118119120/*121* Set default decompression parameters.122*/123124LOCAL(void)125default_decompress_parms (j_decompress_ptr cinfo)126{127/* Guess the input colorspace, and set output colorspace accordingly. */128/* (Wish JPEG committee had provided a real way to specify this...) */129/* Note application may override our guesses. */130switch (cinfo->num_components) {131case 1:132cinfo->jpeg_color_space = JCS_GRAYSCALE;133cinfo->out_color_space = JCS_GRAYSCALE;134break;135136case 3:137if (cinfo->saw_JFIF_marker) {138cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */139} else if (cinfo->saw_Adobe_marker) {140switch (cinfo->Adobe_transform) {141case 0:142cinfo->jpeg_color_space = JCS_RGB;143break;144case 1:145cinfo->jpeg_color_space = JCS_YCbCr;146break;147default:148WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);149cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */150break;151}152} else {153/* Saw no special markers, try to guess from the component IDs */154int cid0 = cinfo->comp_info[0].component_id;155int cid1 = cinfo->comp_info[1].component_id;156int cid2 = cinfo->comp_info[2].component_id;157158if (cid0 == 1 && cid1 == 2 && cid2 == 3)159cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */160else if (cid0 == 82 && cid1 == 71 && cid2 == 66)161cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */162else {163TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);164cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */165}166}167/* Always guess RGB is proper output colorspace. */168cinfo->out_color_space = JCS_RGB;169break;170171case 4:172if (cinfo->saw_Adobe_marker) {173switch (cinfo->Adobe_transform) {174case 0:175cinfo->jpeg_color_space = JCS_CMYK;176break;177case 2:178cinfo->jpeg_color_space = JCS_YCCK;179break;180default:181WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);182cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */183break;184}185} else {186/* No special markers, assume straight CMYK. */187cinfo->jpeg_color_space = JCS_CMYK;188}189cinfo->out_color_space = JCS_CMYK;190break;191192default:193cinfo->jpeg_color_space = JCS_UNKNOWN;194cinfo->out_color_space = JCS_UNKNOWN;195break;196}197198/* Set defaults for other decompression parameters. */199cinfo->scale_num = 1; /* 1:1 scaling */200cinfo->scale_denom = 1;201cinfo->output_gamma = 1.0;202cinfo->buffered_image = FALSE;203cinfo->raw_data_out = FALSE;204cinfo->dct_method = JDCT_DEFAULT;205cinfo->do_fancy_upsampling = TRUE;206cinfo->do_block_smoothing = TRUE;207cinfo->quantize_colors = FALSE;208/* We set these in case application only sets quantize_colors. */209cinfo->dither_mode = JDITHER_FS;210#ifdef QUANT_2PASS_SUPPORTED211cinfo->two_pass_quantize = TRUE;212#else213cinfo->two_pass_quantize = FALSE;214#endif215cinfo->desired_number_of_colors = 256;216cinfo->colormap = NULL;217/* Initialize for no mode change in buffered-image mode. */218cinfo->enable_1pass_quant = FALSE;219cinfo->enable_external_quant = FALSE;220cinfo->enable_2pass_quant = FALSE;221}222223224/*225* Decompression startup: read start of JPEG datastream to see what's there.226* Need only initialize JPEG object and supply a data source before calling.227*228* This routine will read as far as the first SOS marker (ie, actual start of229* compressed data), and will save all tables and parameters in the JPEG230* object. It will also initialize the decompression parameters to default231* values, and finally return JPEG_HEADER_OK. On return, the application may232* adjust the decompression parameters and then call jpeg_start_decompress.233* (Or, if the application only wanted to determine the image parameters,234* the data need not be decompressed. In that case, call jpeg_abort or235* jpeg_destroy to release any temporary space.)236* If an abbreviated (tables only) datastream is presented, the routine will237* return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then238* re-use the JPEG object to read the abbreviated image datastream(s).239* It is unnecessary (but OK) to call jpeg_abort in this case.240* The JPEG_SUSPENDED return code only occurs if the data source module241* requests suspension of the decompressor. In this case the application242* should load more source data and then re-call jpeg_read_header to resume243* processing.244* If a non-suspending data source is used and require_image is TRUE, then the245* return code need not be inspected since only JPEG_HEADER_OK is possible.246*247* This routine is now just a front end to jpeg_consume_input, with some248* extra error checking.249*/250251GLOBAL(int)252jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)253{254int retcode;255256if (cinfo->global_state != DSTATE_START &&257cinfo->global_state != DSTATE_INHEADER)258ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);259260retcode = jpeg_consume_input(cinfo);261262switch (retcode) {263case JPEG_REACHED_SOS:264retcode = JPEG_HEADER_OK;265break;266case JPEG_REACHED_EOI:267if (require_image) /* Complain if application wanted an image */268ERREXIT(cinfo, JERR_NO_IMAGE);269/* Reset to start state; it would be safer to require the application to270* call jpeg_abort, but we can't change it now for compatibility reasons.271* A side effect is to free any temporary memory (there shouldn't be any).272*/273jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */274retcode = JPEG_HEADER_TABLES_ONLY;275break;276case JPEG_SUSPENDED:277/* no work */278break;279}280281return retcode;282}283284285/*286* Consume data in advance of what the decompressor requires.287* This can be called at any time once the decompressor object has288* been created and a data source has been set up.289*290* This routine is essentially a state machine that handles a couple291* of critical state-transition actions, namely initial setup and292* transition from header scanning to ready-for-start_decompress.293* All the actual input is done via the input controller's consume_input294* method.295*/296297GLOBAL(int)298jpeg_consume_input (j_decompress_ptr cinfo)299{300int retcode = JPEG_SUSPENDED;301302/* NB: every possible DSTATE value should be listed in this switch */303switch (cinfo->global_state) {304case DSTATE_START:305/* Start-of-datastream actions: reset appropriate modules */306(*cinfo->inputctl->reset_input_controller) (cinfo);307/* Initialize application's data source module */308(*cinfo->src->init_source) (cinfo);309cinfo->global_state = DSTATE_INHEADER;310/*FALLTHROUGH*/311case DSTATE_INHEADER:312retcode = (*cinfo->inputctl->consume_input) (cinfo);313if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */314/* Set up default parameters based on header data */315default_decompress_parms(cinfo);316/* Set global state: ready for start_decompress */317cinfo->global_state = DSTATE_READY;318}319break;320case DSTATE_READY:321/* Can't advance past first SOS until start_decompress is called */322retcode = JPEG_REACHED_SOS;323break;324case DSTATE_PRELOAD:325case DSTATE_PRESCAN:326case DSTATE_SCANNING:327case DSTATE_RAW_OK:328case DSTATE_BUFIMAGE:329case DSTATE_BUFPOST:330case DSTATE_STOPPING:331retcode = (*cinfo->inputctl->consume_input) (cinfo);332break;333default:334ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);335}336return retcode;337}338339340/*341* Have we finished reading the input file?342*/343344GLOBAL(boolean)345jpeg_input_complete (j_decompress_ptr cinfo)346{347/* Check for valid jpeg object */348if (cinfo->global_state < DSTATE_START ||349cinfo->global_state > DSTATE_STOPPING)350ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);351return cinfo->inputctl->eoi_reached;352}353354355/*356* Is there more than one scan?357*/358359GLOBAL(boolean)360jpeg_has_multiple_scans (j_decompress_ptr cinfo)361{362/* Only valid after jpeg_read_header completes */363if (cinfo->global_state < DSTATE_READY ||364cinfo->global_state > DSTATE_STOPPING)365ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);366return cinfo->inputctl->has_multiple_scans;367}368369370/*371* Finish JPEG decompression.372*373* This will normally just verify the file trailer and release temp storage.374*375* Returns FALSE if suspended. The return value need be inspected only if376* a suspending data source is used.377*/378379GLOBAL(boolean)380jpeg_finish_decompress (j_decompress_ptr cinfo)381{382if ((cinfo->global_state == DSTATE_SCANNING ||383cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {384/* Terminate final pass of non-buffered mode */385if (cinfo->output_scanline < cinfo->output_height)386ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);387(*cinfo->master->finish_output_pass) (cinfo);388cinfo->global_state = DSTATE_STOPPING;389} else if (cinfo->global_state == DSTATE_BUFIMAGE) {390/* Finishing after a buffered-image operation */391cinfo->global_state = DSTATE_STOPPING;392} else if (cinfo->global_state != DSTATE_STOPPING) {393/* STOPPING = repeat call after a suspension, anything else is error */394ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);395}396/* Read until EOI */397while (! cinfo->inputctl->eoi_reached) {398if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)399return FALSE; /* Suspend, come back later */400}401/* Do final cleanup */402(*cinfo->src->term_source) (cinfo);403/* We can use jpeg_abort to release memory and reset global_state */404jpeg_abort((j_common_ptr) cinfo);405return TRUE;406}407408409