Path: blob/master/src/java.desktop/share/native/libjavajpeg/jdapimin.c
41152 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jdapimin.c6*7* Copyright (C) 1994-1998, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains application interface code for the decompression half12* of the JPEG library. These are the "minimum" API routines that may be13* needed in either the normal full-decompression case or the14* transcoding-only case.15*16* Most of the routines intended to be called directly by an application17* are in this file or in jdapistd.c. But also see jcomapi.c for routines18* shared by compression and decompression, and jdtrans.c for the transcoding19* case.20*/2122#define JPEG_INTERNALS23#include "jinclude.h"24#include "jpeglib.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;86}878889/*90* Destruction of a JPEG decompression object91*/9293GLOBAL(void)94jpeg_destroy_decompress (j_decompress_ptr cinfo)95{96jpeg_destroy((j_common_ptr) cinfo); /* use common routine */97}9899100/*101* Abort processing of a JPEG decompression operation,102* but don't destroy the object itself.103*/104105GLOBAL(void)106jpeg_abort_decompress (j_decompress_ptr cinfo)107{108jpeg_abort((j_common_ptr) cinfo); /* use common routine */109}110111112/*113* Set default decompression parameters.114*/115116LOCAL(void)117default_decompress_parms (j_decompress_ptr cinfo)118{119/* Guess the input colorspace, and set output colorspace accordingly. */120/* (Wish JPEG committee had provided a real way to specify this...) */121/* Note application may override our guesses. */122switch (cinfo->num_components) {123case 1:124cinfo->jpeg_color_space = JCS_GRAYSCALE;125cinfo->out_color_space = JCS_GRAYSCALE;126break;127128case 3:129if (cinfo->saw_JFIF_marker) {130cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */131} else if (cinfo->saw_Adobe_marker) {132switch (cinfo->Adobe_transform) {133case 0:134cinfo->jpeg_color_space = JCS_RGB;135break;136case 1:137cinfo->jpeg_color_space = JCS_YCbCr;138break;139default:140WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);141cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */142break;143}144} else {145/* Saw no special markers, try to guess from the component IDs */146int cid0 = cinfo->comp_info[0].component_id;147int cid1 = cinfo->comp_info[1].component_id;148int cid2 = cinfo->comp_info[2].component_id;149150if (cid0 == 1 && cid1 == 2 && cid2 == 3)151cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */152else if (cid0 == 82 && cid1 == 71 && cid2 == 66)153cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */154else {155TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);156cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */157}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 = 1; /* 1:1 scaling */192cinfo->scale_denom = 1;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