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