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