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