nuclear@0: /* nuclear@0: * jdapimin.c nuclear@0: * nuclear@0: * Copyright (C) 1994-1998, Thomas G. Lane. nuclear@0: * This file is part of the Independent JPEG Group's software. nuclear@0: * For conditions of distribution and use, see the accompanying README file. nuclear@0: * nuclear@0: * This file contains application interface code for the decompression half nuclear@0: * of the JPEG library. These are the "minimum" API routines that may be nuclear@0: * needed in either the normal full-decompression case or the nuclear@0: * transcoding-only case. nuclear@0: * nuclear@0: * Most of the routines intended to be called directly by an application nuclear@0: * are in this file or in jdapistd.c. But also see jcomapi.c for routines nuclear@0: * shared by compression and decompression, and jdtrans.c for the transcoding nuclear@0: * case. nuclear@0: */ nuclear@0: nuclear@0: #define JPEG_INTERNALS nuclear@0: #include "jinclude.h" nuclear@0: #include "jpeglib.h" nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: * Initialization of a JPEG decompression object. nuclear@0: * The error manager must already be set up (in case memory manager fails). nuclear@0: */ nuclear@0: nuclear@0: GLOBAL(void) nuclear@0: jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize) nuclear@0: { nuclear@0: int i; nuclear@0: nuclear@0: /* Guard against version mismatches between library and caller. */ nuclear@0: cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */ nuclear@0: if (version != JPEG_LIB_VERSION) nuclear@0: ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version); nuclear@0: if (structsize != SIZEOF(struct jpeg_decompress_struct)) nuclear@0: ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, nuclear@0: (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize); nuclear@0: nuclear@0: /* For debugging purposes, we zero the whole master structure. nuclear@0: * But the application has already set the err pointer, and may have set nuclear@0: * client_data, so we have to save and restore those fields. nuclear@0: * Note: if application hasn't set client_data, tools like Purify may nuclear@0: * complain here. nuclear@0: */ nuclear@0: { nuclear@0: struct jpeg_error_mgr * err = cinfo->err; nuclear@0: void * client_data = cinfo->client_data; /* ignore Purify complaint here */ nuclear@0: MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct)); nuclear@0: cinfo->err = err; nuclear@0: cinfo->client_data = client_data; nuclear@0: } nuclear@0: cinfo->is_decompressor = TRUE; nuclear@0: nuclear@0: /* Initialize a memory manager instance for this object */ nuclear@0: jinit_memory_mgr((j_common_ptr) cinfo); nuclear@0: nuclear@0: /* Zero out pointers to permanent structures. */ nuclear@0: cinfo->progress = NULL; nuclear@0: cinfo->src = NULL; nuclear@0: nuclear@0: for (i = 0; i < NUM_QUANT_TBLS; i++) nuclear@0: cinfo->quant_tbl_ptrs[i] = NULL; nuclear@0: nuclear@0: for (i = 0; i < NUM_HUFF_TBLS; i++) { nuclear@0: cinfo->dc_huff_tbl_ptrs[i] = NULL; nuclear@0: cinfo->ac_huff_tbl_ptrs[i] = NULL; nuclear@0: } nuclear@0: nuclear@0: /* Initialize marker processor so application can override methods nuclear@0: * for COM, APPn markers before calling jpeg_read_header. nuclear@0: */ nuclear@0: cinfo->marker_list = NULL; nuclear@0: jinit_marker_reader(cinfo); nuclear@0: nuclear@0: /* And initialize the overall input controller. */ nuclear@0: jinit_input_controller(cinfo); nuclear@0: nuclear@0: /* OK, I'm ready */ nuclear@0: cinfo->global_state = DSTATE_START; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: * Destruction of a JPEG decompression object nuclear@0: */ nuclear@0: nuclear@0: GLOBAL(void) nuclear@0: jpeg_destroy_decompress (j_decompress_ptr cinfo) nuclear@0: { nuclear@0: jpeg_destroy((j_common_ptr) cinfo); /* use common routine */ nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: * Abort processing of a JPEG decompression operation, nuclear@0: * but don't destroy the object itself. nuclear@0: */ nuclear@0: nuclear@0: GLOBAL(void) nuclear@0: jpeg_abort_decompress (j_decompress_ptr cinfo) nuclear@0: { nuclear@0: jpeg_abort((j_common_ptr) cinfo); /* use common routine */ nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: * Set default decompression parameters. nuclear@0: */ nuclear@0: nuclear@0: LOCAL(void) nuclear@0: default_decompress_parms (j_decompress_ptr cinfo) nuclear@0: { nuclear@0: /* Guess the input colorspace, and set output colorspace accordingly. */ nuclear@0: /* (Wish JPEG committee had provided a real way to specify this...) */ nuclear@0: /* Note application may override our guesses. */ nuclear@0: switch (cinfo->num_components) { nuclear@0: case 1: nuclear@0: cinfo->jpeg_color_space = JCS_GRAYSCALE; nuclear@0: cinfo->out_color_space = JCS_GRAYSCALE; nuclear@0: break; nuclear@0: nuclear@0: case 3: nuclear@0: if (cinfo->saw_JFIF_marker) { nuclear@0: cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */ nuclear@0: } else if (cinfo->saw_Adobe_marker) { nuclear@0: switch (cinfo->Adobe_transform) { nuclear@0: case 0: nuclear@0: cinfo->jpeg_color_space = JCS_RGB; nuclear@0: break; nuclear@0: case 1: nuclear@0: cinfo->jpeg_color_space = JCS_YCbCr; nuclear@0: break; nuclear@0: default: nuclear@0: WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform); nuclear@0: cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */ nuclear@0: break; nuclear@0: } nuclear@0: } else { nuclear@0: /* Saw no special markers, try to guess from the component IDs */ nuclear@0: int cid0 = cinfo->comp_info[0].component_id; nuclear@0: int cid1 = cinfo->comp_info[1].component_id; nuclear@0: int cid2 = cinfo->comp_info[2].component_id; nuclear@0: nuclear@0: if (cid0 == 1 && cid1 == 2 && cid2 == 3) nuclear@0: cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */ nuclear@0: else if (cid0 == 82 && cid1 == 71 && cid2 == 66) nuclear@0: cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */ nuclear@0: else { nuclear@0: TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2); nuclear@0: cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */ nuclear@0: } nuclear@0: } nuclear@0: /* Always guess RGB is proper output colorspace. */ nuclear@0: cinfo->out_color_space = JCS_RGB; nuclear@0: break; nuclear@0: nuclear@0: case 4: nuclear@0: if (cinfo->saw_Adobe_marker) { nuclear@0: switch (cinfo->Adobe_transform) { nuclear@0: case 0: nuclear@0: cinfo->jpeg_color_space = JCS_CMYK; nuclear@0: break; nuclear@0: case 2: nuclear@0: cinfo->jpeg_color_space = JCS_YCCK; nuclear@0: break; nuclear@0: default: nuclear@0: WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform); nuclear@0: cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */ nuclear@0: break; nuclear@0: } nuclear@0: } else { nuclear@0: /* No special markers, assume straight CMYK. */ nuclear@0: cinfo->jpeg_color_space = JCS_CMYK; nuclear@0: } nuclear@0: cinfo->out_color_space = JCS_CMYK; nuclear@0: break; nuclear@0: nuclear@0: default: nuclear@0: cinfo->jpeg_color_space = JCS_UNKNOWN; nuclear@0: cinfo->out_color_space = JCS_UNKNOWN; nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: /* Set defaults for other decompression parameters. */ nuclear@0: cinfo->scale_num = 1; /* 1:1 scaling */ nuclear@0: cinfo->scale_denom = 1; nuclear@0: cinfo->output_gamma = 1.0; nuclear@0: cinfo->buffered_image = FALSE; nuclear@0: cinfo->raw_data_out = FALSE; nuclear@0: cinfo->dct_method = JDCT_DEFAULT; nuclear@0: cinfo->do_fancy_upsampling = TRUE; nuclear@0: cinfo->do_block_smoothing = TRUE; nuclear@0: cinfo->quantize_colors = FALSE; nuclear@0: /* We set these in case application only sets quantize_colors. */ nuclear@0: cinfo->dither_mode = JDITHER_FS; nuclear@0: #ifdef QUANT_2PASS_SUPPORTED nuclear@0: cinfo->two_pass_quantize = TRUE; nuclear@0: #else nuclear@0: cinfo->two_pass_quantize = FALSE; nuclear@0: #endif nuclear@0: cinfo->desired_number_of_colors = 256; nuclear@0: cinfo->colormap = NULL; nuclear@0: /* Initialize for no mode change in buffered-image mode. */ nuclear@0: cinfo->enable_1pass_quant = FALSE; nuclear@0: cinfo->enable_external_quant = FALSE; nuclear@0: cinfo->enable_2pass_quant = FALSE; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: * Decompression startup: read start of JPEG datastream to see what's there. nuclear@0: * Need only initialize JPEG object and supply a data source before calling. nuclear@0: * nuclear@0: * This routine will read as far as the first SOS marker (ie, actual start of nuclear@0: * compressed data), and will save all tables and parameters in the JPEG nuclear@0: * object. It will also initialize the decompression parameters to default nuclear@0: * values, and finally return JPEG_HEADER_OK. On return, the application may nuclear@0: * adjust the decompression parameters and then call jpeg_start_decompress. nuclear@0: * (Or, if the application only wanted to determine the image parameters, nuclear@0: * the data need not be decompressed. In that case, call jpeg_abort or nuclear@0: * jpeg_destroy to release any temporary space.) nuclear@0: * If an abbreviated (tables only) datastream is presented, the routine will nuclear@0: * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then nuclear@0: * re-use the JPEG object to read the abbreviated image datastream(s). nuclear@0: * It is unnecessary (but OK) to call jpeg_abort in this case. nuclear@0: * The JPEG_SUSPENDED return code only occurs if the data source module nuclear@0: * requests suspension of the decompressor. In this case the application nuclear@0: * should load more source data and then re-call jpeg_read_header to resume nuclear@0: * processing. nuclear@0: * If a non-suspending data source is used and require_image is TRUE, then the nuclear@0: * return code need not be inspected since only JPEG_HEADER_OK is possible. nuclear@0: * nuclear@0: * This routine is now just a front end to jpeg_consume_input, with some nuclear@0: * extra error checking. nuclear@0: */ nuclear@0: nuclear@0: GLOBAL(int) nuclear@0: jpeg_read_header (j_decompress_ptr cinfo, boolean require_image) nuclear@0: { nuclear@0: int retcode; nuclear@0: nuclear@0: if (cinfo->global_state != DSTATE_START && nuclear@0: cinfo->global_state != DSTATE_INHEADER) nuclear@0: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@0: nuclear@0: retcode = jpeg_consume_input(cinfo); nuclear@0: nuclear@0: switch (retcode) { nuclear@0: case JPEG_REACHED_SOS: nuclear@0: retcode = JPEG_HEADER_OK; nuclear@0: break; nuclear@0: case JPEG_REACHED_EOI: nuclear@0: if (require_image) /* Complain if application wanted an image */ nuclear@0: ERREXIT(cinfo, JERR_NO_IMAGE); nuclear@0: /* Reset to start state; it would be safer to require the application to nuclear@0: * call jpeg_abort, but we can't change it now for compatibility reasons. nuclear@0: * A side effect is to free any temporary memory (there shouldn't be any). nuclear@0: */ nuclear@0: jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */ nuclear@0: retcode = JPEG_HEADER_TABLES_ONLY; nuclear@0: break; nuclear@0: case JPEG_SUSPENDED: nuclear@0: /* no work */ nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: return retcode; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: * Consume data in advance of what the decompressor requires. nuclear@0: * This can be called at any time once the decompressor object has nuclear@0: * been created and a data source has been set up. nuclear@0: * nuclear@0: * This routine is essentially a state machine that handles a couple nuclear@0: * of critical state-transition actions, namely initial setup and nuclear@0: * transition from header scanning to ready-for-start_decompress. nuclear@0: * All the actual input is done via the input controller's consume_input nuclear@0: * method. nuclear@0: */ nuclear@0: nuclear@0: GLOBAL(int) nuclear@0: jpeg_consume_input (j_decompress_ptr cinfo) nuclear@0: { nuclear@0: int retcode = JPEG_SUSPENDED; nuclear@0: nuclear@0: /* NB: every possible DSTATE value should be listed in this switch */ nuclear@0: switch (cinfo->global_state) { nuclear@0: case DSTATE_START: nuclear@0: /* Start-of-datastream actions: reset appropriate modules */ nuclear@0: (*cinfo->inputctl->reset_input_controller) (cinfo); nuclear@0: /* Initialize application's data source module */ nuclear@0: (*cinfo->src->init_source) (cinfo); nuclear@0: cinfo->global_state = DSTATE_INHEADER; nuclear@0: /*FALLTHROUGH*/ nuclear@0: case DSTATE_INHEADER: nuclear@0: retcode = (*cinfo->inputctl->consume_input) (cinfo); nuclear@0: if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */ nuclear@0: /* Set up default parameters based on header data */ nuclear@0: default_decompress_parms(cinfo); nuclear@0: /* Set global state: ready for start_decompress */ nuclear@0: cinfo->global_state = DSTATE_READY; nuclear@0: } nuclear@0: break; nuclear@0: case DSTATE_READY: nuclear@0: /* Can't advance past first SOS until start_decompress is called */ nuclear@0: retcode = JPEG_REACHED_SOS; nuclear@0: break; nuclear@0: case DSTATE_PRELOAD: nuclear@0: case DSTATE_PRESCAN: nuclear@0: case DSTATE_SCANNING: nuclear@0: case DSTATE_RAW_OK: nuclear@0: case DSTATE_BUFIMAGE: nuclear@0: case DSTATE_BUFPOST: nuclear@0: case DSTATE_STOPPING: nuclear@0: retcode = (*cinfo->inputctl->consume_input) (cinfo); nuclear@0: break; nuclear@0: default: nuclear@0: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@0: } nuclear@0: return retcode; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: * Have we finished reading the input file? nuclear@0: */ nuclear@0: nuclear@0: GLOBAL(boolean) nuclear@0: jpeg_input_complete (j_decompress_ptr cinfo) nuclear@0: { nuclear@0: /* Check for valid jpeg object */ nuclear@0: if (cinfo->global_state < DSTATE_START || nuclear@0: cinfo->global_state > DSTATE_STOPPING) nuclear@0: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@0: return cinfo->inputctl->eoi_reached; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: * Is there more than one scan? nuclear@0: */ nuclear@0: nuclear@0: GLOBAL(boolean) nuclear@0: jpeg_has_multiple_scans (j_decompress_ptr cinfo) nuclear@0: { nuclear@0: /* Only valid after jpeg_read_header completes */ nuclear@0: if (cinfo->global_state < DSTATE_READY || nuclear@0: cinfo->global_state > DSTATE_STOPPING) nuclear@0: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@0: return cinfo->inputctl->has_multiple_scans; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: * Finish JPEG decompression. nuclear@0: * nuclear@0: * This will normally just verify the file trailer and release temp storage. nuclear@0: * nuclear@0: * Returns FALSE if suspended. The return value need be inspected only if nuclear@0: * a suspending data source is used. nuclear@0: */ nuclear@0: nuclear@0: GLOBAL(boolean) nuclear@0: jpeg_finish_decompress (j_decompress_ptr cinfo) nuclear@0: { nuclear@0: if ((cinfo->global_state == DSTATE_SCANNING || nuclear@0: cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) { nuclear@0: /* Terminate final pass of non-buffered mode */ nuclear@0: if (cinfo->output_scanline < cinfo->output_height) nuclear@0: ERREXIT(cinfo, JERR_TOO_LITTLE_DATA); nuclear@0: (*cinfo->master->finish_output_pass) (cinfo); nuclear@0: cinfo->global_state = DSTATE_STOPPING; nuclear@0: } else if (cinfo->global_state == DSTATE_BUFIMAGE) { nuclear@0: /* Finishing after a buffered-image operation */ nuclear@0: cinfo->global_state = DSTATE_STOPPING; nuclear@0: } else if (cinfo->global_state != DSTATE_STOPPING) { nuclear@0: /* STOPPING = repeat call after a suspension, anything else is error */ nuclear@0: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@0: } nuclear@0: /* Read until EOI */ nuclear@0: while (! cinfo->inputctl->eoi_reached) { nuclear@0: if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED) nuclear@0: return FALSE; /* Suspend, come back later */ nuclear@0: } nuclear@0: /* Do final cleanup */ nuclear@0: (*cinfo->src->term_source) (cinfo); nuclear@0: /* We can use jpeg_abort to release memory and reset global_state */ nuclear@0: jpeg_abort((j_common_ptr) cinfo); nuclear@0: return TRUE; nuclear@0: }