Path: blob/master/thirdparty/libjpeg-turbo/src/jdapimin.c
9904 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* Lossless JPEG Modifications:6* Copyright (C) 1999, Ken Murchison.7* libjpeg-turbo Modifications:8* Copyright (C) 2016, 2022, 2024, D. R. Commander.9* For conditions of distribution and use, see the accompanying README.ijg10* file.11*12* This file contains application interface code for the decompression half13* of the JPEG library. These are the "minimum" API routines that may be14* needed in either the normal full-decompression case or the15* transcoding-only case.16*17* Most of the routines intended to be called directly by an application18* are in this file or in jdapistd.c. But also see jcomapi.c for routines19* shared by compression and decompression, and jdtrans.c for the transcoding20* case.21*/2223#define JPEG_INTERNALS24#include "jinclude.h"25#include "jpeglib.h"26#include "jdmaster.h"272829/*30* Initialization of a JPEG decompression object.31* The error manager must already be set up (in case memory manager fails).32*/3334GLOBAL(void)35jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)36{37int i;3839/* Guard against version mismatches between library and caller. */40cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */41if (version != JPEG_LIB_VERSION)42ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);43if (structsize != sizeof(struct jpeg_decompress_struct))44ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,45(int)sizeof(struct jpeg_decompress_struct), (int)structsize);4647/* For debugging purposes, we zero the whole master structure.48* But the application has already set the err pointer, and may have set49* client_data, so we have to save and restore those fields.50* Note: if application hasn't set client_data, tools like Purify may51* complain here.52*/53{54struct jpeg_error_mgr *err = cinfo->err;55void *client_data = cinfo->client_data; /* ignore Purify complaint here */56memset(cinfo, 0, sizeof(struct jpeg_decompress_struct));57cinfo->err = err;58cinfo->client_data = client_data;59}60cinfo->is_decompressor = TRUE;6162/* Initialize a memory manager instance for this object */63jinit_memory_mgr((j_common_ptr)cinfo);6465/* Zero out pointers to permanent structures. */66cinfo->progress = NULL;67cinfo->src = NULL;6869for (i = 0; i < NUM_QUANT_TBLS; i++)70cinfo->quant_tbl_ptrs[i] = NULL;7172for (i = 0; i < NUM_HUFF_TBLS; i++) {73cinfo->dc_huff_tbl_ptrs[i] = NULL;74cinfo->ac_huff_tbl_ptrs[i] = NULL;75}7677/* Initialize marker processor so application can override methods78* for COM, APPn markers before calling jpeg_read_header.79*/80cinfo->marker_list = NULL;81jinit_marker_reader(cinfo);8283/* And initialize the overall input controller. */84jinit_input_controller(cinfo);8586cinfo->data_precision = BITS_IN_JSAMPLE;8788/* OK, I'm ready */89cinfo->global_state = DSTATE_START;9091/* The master struct is used to store extension parameters, so we allocate it92* here.93*/94cinfo->master = (struct jpeg_decomp_master *)95(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,96sizeof(my_decomp_master));97memset(cinfo->master, 0, sizeof(my_decomp_master));98}99100101/*102* Destruction of a JPEG decompression object103*/104105GLOBAL(void)106jpeg_destroy_decompress(j_decompress_ptr cinfo)107{108jpeg_destroy((j_common_ptr)cinfo); /* use common routine */109}110111112/*113* Abort processing of a JPEG decompression operation,114* but don't destroy the object itself.115*/116117GLOBAL(void)118jpeg_abort_decompress(j_decompress_ptr cinfo)119{120jpeg_abort((j_common_ptr)cinfo); /* use common routine */121}122123124/*125* Set default decompression parameters.126*/127128LOCAL(void)129default_decompress_parms(j_decompress_ptr cinfo)130{131/* Guess the input colorspace, and set output colorspace accordingly. */132/* (Wish JPEG committee had provided a real way to specify this...) */133/* Note application may override our guesses. */134switch (cinfo->num_components) {135case 1:136cinfo->jpeg_color_space = JCS_GRAYSCALE;137cinfo->out_color_space = JCS_GRAYSCALE;138break;139140case 3:141if (cinfo->saw_JFIF_marker) {142cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */143} else if (cinfo->saw_Adobe_marker) {144switch (cinfo->Adobe_transform) {145case 0:146cinfo->jpeg_color_space = JCS_RGB;147break;148case 1:149cinfo->jpeg_color_space = JCS_YCbCr;150break;151default:152WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);153cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */154break;155}156} else {157/* Saw no special markers, try to guess from the component IDs */158int cid0 = cinfo->comp_info[0].component_id;159int cid1 = cinfo->comp_info[1].component_id;160int cid2 = cinfo->comp_info[2].component_id;161162if (cid0 == 1 && cid1 == 2 && cid2 == 3) {163#ifdef D_LOSSLESS_SUPPORTED164if (cinfo->master->lossless)165cinfo->jpeg_color_space = JCS_RGB; /* assume RGB w/out marker */166else167#endif168cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */169} else if (cid0 == 82 && cid1 == 71 && cid2 == 66)170cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */171else {172TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);173#ifdef D_LOSSLESS_SUPPORTED174if (cinfo->master->lossless)175cinfo->jpeg_color_space = JCS_RGB; /* assume it's RGB */176else177#endif178cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */179}180}181/* Always guess RGB is proper output colorspace. */182cinfo->out_color_space = JCS_RGB;183break;184185case 4:186if (cinfo->saw_Adobe_marker) {187switch (cinfo->Adobe_transform) {188case 0:189cinfo->jpeg_color_space = JCS_CMYK;190break;191case 2:192cinfo->jpeg_color_space = JCS_YCCK;193break;194default:195WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);196cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */197break;198}199} else {200/* No special markers, assume straight CMYK. */201cinfo->jpeg_color_space = JCS_CMYK;202}203cinfo->out_color_space = JCS_CMYK;204break;205206default:207cinfo->jpeg_color_space = JCS_UNKNOWN;208cinfo->out_color_space = JCS_UNKNOWN;209break;210}211212/* Set defaults for other decompression parameters. */213cinfo->scale_num = 1; /* 1:1 scaling */214cinfo->scale_denom = 1;215cinfo->output_gamma = 1.0;216cinfo->buffered_image = FALSE;217cinfo->raw_data_out = FALSE;218cinfo->dct_method = JDCT_DEFAULT;219cinfo->do_fancy_upsampling = TRUE;220cinfo->do_block_smoothing = TRUE;221cinfo->quantize_colors = FALSE;222/* We set these in case application only sets quantize_colors. */223cinfo->dither_mode = JDITHER_FS;224#ifdef QUANT_2PASS_SUPPORTED225cinfo->two_pass_quantize = TRUE;226#else227cinfo->two_pass_quantize = FALSE;228#endif229cinfo->desired_number_of_colors = 256;230cinfo->colormap = NULL;231/* Initialize for no mode change in buffered-image mode. */232cinfo->enable_1pass_quant = FALSE;233cinfo->enable_external_quant = FALSE;234cinfo->enable_2pass_quant = FALSE;235}236237238/*239* Decompression startup: read start of JPEG datastream to see what's there.240* Need only initialize JPEG object and supply a data source before calling.241*242* This routine will read as far as the first SOS marker (ie, actual start of243* compressed data), and will save all tables and parameters in the JPEG244* object. It will also initialize the decompression parameters to default245* values, and finally return JPEG_HEADER_OK. On return, the application may246* adjust the decompression parameters and then call jpeg_start_decompress.247* (Or, if the application only wanted to determine the image parameters,248* the data need not be decompressed. In that case, call jpeg_abort or249* jpeg_destroy to release any temporary space.)250* If an abbreviated (tables only) datastream is presented, the routine will251* return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then252* re-use the JPEG object to read the abbreviated image datastream(s).253* It is unnecessary (but OK) to call jpeg_abort in this case.254* The JPEG_SUSPENDED return code only occurs if the data source module255* requests suspension of the decompressor. In this case the application256* should load more source data and then re-call jpeg_read_header to resume257* processing.258* If a non-suspending data source is used and require_image is TRUE, then the259* return code need not be inspected since only JPEG_HEADER_OK is possible.260*261* This routine is now just a front end to jpeg_consume_input, with some262* extra error checking.263*/264265GLOBAL(int)266jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)267{268int retcode;269270if (cinfo->global_state != DSTATE_START &&271cinfo->global_state != DSTATE_INHEADER)272ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);273274retcode = jpeg_consume_input(cinfo);275276switch (retcode) {277case JPEG_REACHED_SOS:278retcode = JPEG_HEADER_OK;279break;280case JPEG_REACHED_EOI:281if (require_image) /* Complain if application wanted an image */282ERREXIT(cinfo, JERR_NO_IMAGE);283/* Reset to start state; it would be safer to require the application to284* call jpeg_abort, but we can't change it now for compatibility reasons.285* A side effect is to free any temporary memory (there shouldn't be any).286*/287jpeg_abort((j_common_ptr)cinfo); /* sets state = DSTATE_START */288retcode = JPEG_HEADER_TABLES_ONLY;289break;290case JPEG_SUSPENDED:291/* no work */292break;293}294295return retcode;296}297298299/*300* Consume data in advance of what the decompressor requires.301* This can be called at any time once the decompressor object has302* been created and a data source has been set up.303*304* This routine is essentially a state machine that handles a couple305* of critical state-transition actions, namely initial setup and306* transition from header scanning to ready-for-start_decompress.307* All the actual input is done via the input controller's consume_input308* method.309*/310311GLOBAL(int)312jpeg_consume_input(j_decompress_ptr cinfo)313{314int retcode = JPEG_SUSPENDED;315316/* NB: every possible DSTATE value should be listed in this switch */317switch (cinfo->global_state) {318case DSTATE_START:319/* Start-of-datastream actions: reset appropriate modules */320(*cinfo->inputctl->reset_input_controller) (cinfo);321/* Initialize application's data source module */322(*cinfo->src->init_source) (cinfo);323cinfo->global_state = DSTATE_INHEADER;324FALLTHROUGH /*FALLTHROUGH*/325case DSTATE_INHEADER:326retcode = (*cinfo->inputctl->consume_input) (cinfo);327if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */328/* Set up default parameters based on header data */329default_decompress_parms(cinfo);330/* Set global state: ready for start_decompress */331cinfo->global_state = DSTATE_READY;332}333break;334case DSTATE_READY:335/* Can't advance past first SOS until start_decompress is called */336retcode = JPEG_REACHED_SOS;337break;338case DSTATE_PRELOAD:339case DSTATE_PRESCAN:340case DSTATE_SCANNING:341case DSTATE_RAW_OK:342case DSTATE_BUFIMAGE:343case DSTATE_BUFPOST:344case DSTATE_STOPPING:345retcode = (*cinfo->inputctl->consume_input) (cinfo);346break;347default:348ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);349}350return retcode;351}352353354/*355* Have we finished reading the input file?356*/357358GLOBAL(boolean)359jpeg_input_complete(j_decompress_ptr cinfo)360{361/* Check for valid jpeg object */362if (cinfo->global_state < DSTATE_START ||363cinfo->global_state > DSTATE_STOPPING)364ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);365return cinfo->inputctl->eoi_reached;366}367368369/*370* Is there more than one scan?371*/372373GLOBAL(boolean)374jpeg_has_multiple_scans(j_decompress_ptr cinfo)375{376/* Only valid after jpeg_read_header completes */377if (cinfo->global_state < DSTATE_READY ||378cinfo->global_state > DSTATE_STOPPING)379ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);380return cinfo->inputctl->has_multiple_scans;381}382383384/*385* Finish JPEG decompression.386*387* This will normally just verify the file trailer and release temp storage.388*389* Returns FALSE if suspended. The return value need be inspected only if390* a suspending data source is used.391*/392393GLOBAL(boolean)394jpeg_finish_decompress(j_decompress_ptr cinfo)395{396if ((cinfo->global_state == DSTATE_SCANNING ||397cinfo->global_state == DSTATE_RAW_OK) && !cinfo->buffered_image) {398/* Terminate final pass of non-buffered mode */399if (cinfo->output_scanline < cinfo->output_height)400ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);401(*cinfo->master->finish_output_pass) (cinfo);402cinfo->global_state = DSTATE_STOPPING;403} else if (cinfo->global_state == DSTATE_BUFIMAGE) {404/* Finishing after a buffered-image operation */405cinfo->global_state = DSTATE_STOPPING;406} else if (cinfo->global_state != DSTATE_STOPPING) {407/* STOPPING = repeat call after a suspension, anything else is error */408ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);409}410/* Read until EOI */411while (!cinfo->inputctl->eoi_reached) {412if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)413return FALSE; /* Suspend, come back later */414}415/* Do final cleanup */416(*cinfo->src->term_source) (cinfo);417/* We can use jpeg_abort to release memory and reset global_state */418jpeg_abort((j_common_ptr)cinfo);419return TRUE;420}421422423