nuclear@1: /* nuclear@1: * jdapistd.c nuclear@1: * nuclear@1: * Copyright (C) 1994-1996, 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 "standard" API routines that are nuclear@1: * used in the normal full-decompression case. They are not used by a nuclear@1: * transcoding-only application. Note that if an application links in nuclear@1: * jpeg_start_decompress, it will end up linking in the entire decompressor. nuclear@1: * We thus must separate this file from jdapimin.c to avoid linking the nuclear@1: * whole decompression library into a transcoder. 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: /* Forward declarations */ nuclear@1: LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo)); nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Decompression initialization. nuclear@1: * jpeg_read_header must be completed before calling this. nuclear@1: * nuclear@1: * If a multipass operating mode was selected, this will do all but the nuclear@1: * last pass, and thus may take a great deal of time. 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_start_decompress (j_decompress_ptr cinfo) nuclear@1: { nuclear@1: if (cinfo->global_state == DSTATE_READY) { nuclear@1: /* First call: initialize master control, select active modules */ nuclear@1: jinit_master_decompress(cinfo); nuclear@1: if (cinfo->buffered_image) { nuclear@1: /* No more work here; expecting jpeg_start_output next */ nuclear@1: cinfo->global_state = DSTATE_BUFIMAGE; nuclear@1: return TRUE; nuclear@1: } nuclear@1: cinfo->global_state = DSTATE_PRELOAD; nuclear@1: } nuclear@1: if (cinfo->global_state == DSTATE_PRELOAD) { nuclear@1: /* If file has multiple scans, absorb them all into the coef buffer */ nuclear@1: if (cinfo->inputctl->has_multiple_scans) { nuclear@1: #ifdef D_MULTISCAN_FILES_SUPPORTED nuclear@1: for (;;) { nuclear@1: int retcode; nuclear@1: /* Call progress monitor hook if present */ nuclear@1: if (cinfo->progress != NULL) nuclear@1: (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); nuclear@1: /* Absorb some more input */ nuclear@1: retcode = (*cinfo->inputctl->consume_input) (cinfo); nuclear@1: if (retcode == JPEG_SUSPENDED) nuclear@1: return FALSE; nuclear@1: if (retcode == JPEG_REACHED_EOI) nuclear@1: break; nuclear@1: /* Advance progress counter if appropriate */ nuclear@1: if (cinfo->progress != NULL && nuclear@1: (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { nuclear@1: if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { nuclear@1: /* jdmaster underestimated number of scans; ratchet up one scan */ nuclear@1: cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: #else nuclear@1: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@1: #endif /* D_MULTISCAN_FILES_SUPPORTED */ nuclear@1: } nuclear@1: cinfo->output_scan_number = cinfo->input_scan_number; nuclear@1: } else if (cinfo->global_state != DSTATE_PRESCAN) nuclear@1: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@1: /* Perform any dummy output passes, and set up for the final pass */ nuclear@1: return output_pass_setup(cinfo); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Set up for an output pass, and perform any dummy pass(es) needed. nuclear@1: * Common subroutine for jpeg_start_decompress and jpeg_start_output. nuclear@1: * Entry: global_state = DSTATE_PRESCAN only if previously suspended. nuclear@1: * Exit: If done, returns TRUE and sets global_state for proper output mode. nuclear@1: * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN. nuclear@1: */ nuclear@1: nuclear@1: LOCAL(boolean) nuclear@1: output_pass_setup (j_decompress_ptr cinfo) nuclear@1: { nuclear@1: if (cinfo->global_state != DSTATE_PRESCAN) { nuclear@1: /* First call: do pass setup */ nuclear@1: (*cinfo->master->prepare_for_output_pass) (cinfo); nuclear@1: cinfo->output_scanline = 0; nuclear@1: cinfo->global_state = DSTATE_PRESCAN; nuclear@1: } nuclear@1: /* Loop over any required dummy passes */ nuclear@1: while (cinfo->master->is_dummy_pass) { nuclear@1: #ifdef QUANT_2PASS_SUPPORTED nuclear@1: /* Crank through the dummy pass */ nuclear@1: while (cinfo->output_scanline < cinfo->output_height) { nuclear@1: JDIMENSION last_scanline; nuclear@1: /* Call progress monitor hook if present */ nuclear@1: if (cinfo->progress != NULL) { nuclear@1: cinfo->progress->pass_counter = (long) cinfo->output_scanline; nuclear@1: cinfo->progress->pass_limit = (long) cinfo->output_height; nuclear@1: (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); nuclear@1: } nuclear@1: /* Process some data */ nuclear@1: last_scanline = cinfo->output_scanline; nuclear@1: (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL, nuclear@1: &cinfo->output_scanline, (JDIMENSION) 0); nuclear@1: if (cinfo->output_scanline == last_scanline) nuclear@1: return FALSE; /* No progress made, must suspend */ nuclear@1: } nuclear@1: /* Finish up dummy pass, and set up for another one */ nuclear@1: (*cinfo->master->finish_output_pass) (cinfo); nuclear@1: (*cinfo->master->prepare_for_output_pass) (cinfo); nuclear@1: cinfo->output_scanline = 0; nuclear@1: #else nuclear@1: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@1: #endif /* QUANT_2PASS_SUPPORTED */ nuclear@1: } nuclear@1: /* Ready for application to drive output pass through nuclear@1: * jpeg_read_scanlines or jpeg_read_raw_data. nuclear@1: */ nuclear@1: cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING; nuclear@1: return TRUE; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Read some scanlines of data from the JPEG decompressor. nuclear@1: * nuclear@1: * The return value will be the number of lines actually read. nuclear@1: * This may be less than the number requested in several cases, nuclear@1: * including bottom of image, data source suspension, and operating nuclear@1: * modes that emit multiple scanlines at a time. nuclear@1: * nuclear@1: * Note: we warn about excess calls to jpeg_read_scanlines() since nuclear@1: * this likely signals an application programmer error. However, nuclear@1: * an oversize buffer (max_lines > scanlines remaining) is not an error. nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(JDIMENSION) nuclear@1: jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines, nuclear@1: JDIMENSION max_lines) nuclear@1: { nuclear@1: JDIMENSION row_ctr; nuclear@1: nuclear@1: if (cinfo->global_state != DSTATE_SCANNING) nuclear@1: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@1: if (cinfo->output_scanline >= cinfo->output_height) { nuclear@1: WARNMS(cinfo, JWRN_TOO_MUCH_DATA); nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: /* Call progress monitor hook if present */ nuclear@1: if (cinfo->progress != NULL) { nuclear@1: cinfo->progress->pass_counter = (long) cinfo->output_scanline; nuclear@1: cinfo->progress->pass_limit = (long) cinfo->output_height; nuclear@1: (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); nuclear@1: } nuclear@1: nuclear@1: /* Process some data */ nuclear@1: row_ctr = 0; nuclear@1: (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines); nuclear@1: cinfo->output_scanline += row_ctr; nuclear@1: return row_ctr; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Alternate entry point to read raw data. nuclear@1: * Processes exactly one iMCU row per call, unless suspended. nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(JDIMENSION) nuclear@1: jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data, nuclear@1: JDIMENSION max_lines) nuclear@1: { nuclear@1: JDIMENSION lines_per_iMCU_row; nuclear@1: nuclear@1: if (cinfo->global_state != DSTATE_RAW_OK) nuclear@1: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@1: if (cinfo->output_scanline >= cinfo->output_height) { nuclear@1: WARNMS(cinfo, JWRN_TOO_MUCH_DATA); nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: /* Call progress monitor hook if present */ nuclear@1: if (cinfo->progress != NULL) { nuclear@1: cinfo->progress->pass_counter = (long) cinfo->output_scanline; nuclear@1: cinfo->progress->pass_limit = (long) cinfo->output_height; nuclear@1: (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); nuclear@1: } nuclear@1: nuclear@1: /* Verify that at least one iMCU row can be returned. */ nuclear@1: lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size; nuclear@1: if (max_lines < lines_per_iMCU_row) nuclear@1: ERREXIT(cinfo, JERR_BUFFER_SIZE); nuclear@1: nuclear@1: /* Decompress directly into user's buffer. */ nuclear@1: if (! (*cinfo->coef->decompress_data) (cinfo, data)) nuclear@1: return 0; /* suspension forced, can do nothing more */ nuclear@1: nuclear@1: /* OK, we processed one iMCU row. */ nuclear@1: cinfo->output_scanline += lines_per_iMCU_row; nuclear@1: return lines_per_iMCU_row; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* Additional entry points for buffered-image mode. */ nuclear@1: nuclear@1: #ifdef D_MULTISCAN_FILES_SUPPORTED nuclear@1: nuclear@1: /* nuclear@1: * Initialize for an output pass in buffered-image mode. nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(boolean) nuclear@1: jpeg_start_output (j_decompress_ptr cinfo, int scan_number) nuclear@1: { nuclear@1: if (cinfo->global_state != DSTATE_BUFIMAGE && nuclear@1: cinfo->global_state != DSTATE_PRESCAN) nuclear@1: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@1: /* Limit scan number to valid range */ nuclear@1: if (scan_number <= 0) nuclear@1: scan_number = 1; nuclear@1: if (cinfo->inputctl->eoi_reached && nuclear@1: scan_number > cinfo->input_scan_number) nuclear@1: scan_number = cinfo->input_scan_number; nuclear@1: cinfo->output_scan_number = scan_number; nuclear@1: /* Perform any dummy output passes, and set up for the real pass */ nuclear@1: return output_pass_setup(cinfo); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Finish up after an output pass in buffered-image mode. 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_output (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 this pass. */ nuclear@1: /* We do not require the whole pass to have been completed. */ nuclear@1: (*cinfo->master->finish_output_pass) (cinfo); nuclear@1: cinfo->global_state = DSTATE_BUFPOST; nuclear@1: } else if (cinfo->global_state != DSTATE_BUFPOST) { nuclear@1: /* BUFPOST = repeat call after a suspension, anything else is error */ nuclear@1: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@1: } nuclear@1: /* Read markers looking for SOS or EOI */ nuclear@1: while (cinfo->input_scan_number <= cinfo->output_scan_number && nuclear@1: ! 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: cinfo->global_state = DSTATE_BUFIMAGE; nuclear@1: return TRUE; nuclear@1: } nuclear@1: nuclear@1: #endif /* D_MULTISCAN_FILES_SUPPORTED */