Path: blob/master/src/java.desktop/share/native/libjavajpeg/jdapistd.c
41152 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jdapistd.c6*7* Copyright (C) 1994-1996, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains application interface code for the decompression half12* of the JPEG library. These are the "standard" API routines that are13* used in the normal full-decompression case. They are not used by a14* transcoding-only application. Note that if an application links in15* jpeg_start_decompress, it will end up linking in the entire decompressor.16* We thus must separate this file from jdapimin.c to avoid linking the17* whole decompression library into a transcoder.18*/1920#define JPEG_INTERNALS21#include "jinclude.h"22#include "jpeglib.h"232425/* Forward declarations */26LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));272829/*30* Decompression initialization.31* jpeg_read_header must be completed before calling this.32*33* If a multipass operating mode was selected, this will do all but the34* last pass, and thus may take a great deal of time.35*36* Returns FALSE if suspended. The return value need be inspected only if37* a suspending data source is used.38*/3940GLOBAL(boolean)41jpeg_start_decompress (j_decompress_ptr cinfo)42{43if (cinfo->global_state == DSTATE_READY) {44/* First call: initialize master control, select active modules */45jinit_master_decompress(cinfo);46if (cinfo->buffered_image) {47/* No more work here; expecting jpeg_start_output next */48cinfo->global_state = DSTATE_BUFIMAGE;49return TRUE;50}51cinfo->global_state = DSTATE_PRELOAD;52}53if (cinfo->global_state == DSTATE_PRELOAD) {54/* If file has multiple scans, absorb them all into the coef buffer */55if (cinfo->inputctl->has_multiple_scans) {56#ifdef D_MULTISCAN_FILES_SUPPORTED57for (;;) {58int retcode;59/* Call progress monitor hook if present */60if (cinfo->progress != NULL)61(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);62/* Absorb some more input */63retcode = (*cinfo->inputctl->consume_input) (cinfo);64if (retcode == JPEG_SUSPENDED)65return FALSE;66if (retcode == JPEG_REACHED_EOI)67break;68/* Advance progress counter if appropriate */69if (cinfo->progress != NULL &&70(retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {71if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {72/* jdmaster underestimated number of scans; ratchet up one scan */73cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;74}75}76}77#else78ERREXIT(cinfo, JERR_NOT_COMPILED);79#endif /* D_MULTISCAN_FILES_SUPPORTED */80}81cinfo->output_scan_number = cinfo->input_scan_number;82} else if (cinfo->global_state != DSTATE_PRESCAN)83ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);84/* Perform any dummy output passes, and set up for the final pass */85return output_pass_setup(cinfo);86}878889/*90* Set up for an output pass, and perform any dummy pass(es) needed.91* Common subroutine for jpeg_start_decompress and jpeg_start_output.92* Entry: global_state = DSTATE_PRESCAN only if previously suspended.93* Exit: If done, returns TRUE and sets global_state for proper output mode.94* If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.95*/9697LOCAL(boolean)98output_pass_setup (j_decompress_ptr cinfo)99{100if (cinfo->global_state != DSTATE_PRESCAN) {101/* First call: do pass setup */102(*cinfo->master->prepare_for_output_pass) (cinfo);103cinfo->output_scanline = 0;104cinfo->global_state = DSTATE_PRESCAN;105}106/* Loop over any required dummy passes */107while (cinfo->master->is_dummy_pass) {108#ifdef QUANT_2PASS_SUPPORTED109/* Crank through the dummy pass */110while (cinfo->output_scanline < cinfo->output_height) {111JDIMENSION last_scanline;112/* Call progress monitor hook if present */113if (cinfo->progress != NULL) {114cinfo->progress->pass_counter = (long) cinfo->output_scanline;115cinfo->progress->pass_limit = (long) cinfo->output_height;116(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);117}118/* Process some data */119last_scanline = cinfo->output_scanline;120(*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,121&cinfo->output_scanline, (JDIMENSION) 0);122if (cinfo->output_scanline == last_scanline)123return FALSE; /* No progress made, must suspend */124}125/* Finish up dummy pass, and set up for another one */126(*cinfo->master->finish_output_pass) (cinfo);127(*cinfo->master->prepare_for_output_pass) (cinfo);128cinfo->output_scanline = 0;129#else130ERREXIT(cinfo, JERR_NOT_COMPILED);131#endif /* QUANT_2PASS_SUPPORTED */132}133/* Ready for application to drive output pass through134* jpeg_read_scanlines or jpeg_read_raw_data.135*/136cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;137return TRUE;138}139140141/*142* Read some scanlines of data from the JPEG decompressor.143*144* The return value will be the number of lines actually read.145* This may be less than the number requested in several cases,146* including bottom of image, data source suspension, and operating147* modes that emit multiple scanlines at a time.148*149* Note: we warn about excess calls to jpeg_read_scanlines() since150* this likely signals an application programmer error. However,151* an oversize buffer (max_lines > scanlines remaining) is not an error.152*/153154GLOBAL(JDIMENSION)155jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,156JDIMENSION max_lines)157{158JDIMENSION row_ctr;159160if (cinfo->global_state != DSTATE_SCANNING)161ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);162if (cinfo->output_scanline >= cinfo->output_height) {163WARNMS(cinfo, JWRN_TOO_MUCH_DATA);164return 0;165}166167/* Call progress monitor hook if present */168if (cinfo->progress != NULL) {169cinfo->progress->pass_counter = (long) cinfo->output_scanline;170cinfo->progress->pass_limit = (long) cinfo->output_height;171(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);172}173174/* Process some data */175row_ctr = 0;176(*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);177cinfo->output_scanline += row_ctr;178return row_ctr;179}180181182/*183* Alternate entry point to read raw data.184* Processes exactly one iMCU row per call, unless suspended.185*/186187GLOBAL(JDIMENSION)188jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,189JDIMENSION max_lines)190{191JDIMENSION lines_per_iMCU_row;192193if (cinfo->global_state != DSTATE_RAW_OK)194ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);195if (cinfo->output_scanline >= cinfo->output_height) {196WARNMS(cinfo, JWRN_TOO_MUCH_DATA);197return 0;198}199200/* Call progress monitor hook if present */201if (cinfo->progress != NULL) {202cinfo->progress->pass_counter = (long) cinfo->output_scanline;203cinfo->progress->pass_limit = (long) cinfo->output_height;204(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);205}206207/* Verify that at least one iMCU row can be returned. */208lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;209if (max_lines < lines_per_iMCU_row)210ERREXIT(cinfo, JERR_BUFFER_SIZE);211212/* Decompress directly into user's buffer. */213if (! (*cinfo->coef->decompress_data) (cinfo, data))214return 0; /* suspension forced, can do nothing more */215216/* OK, we processed one iMCU row. */217cinfo->output_scanline += lines_per_iMCU_row;218return lines_per_iMCU_row;219}220221222/* Additional entry points for buffered-image mode. */223224#ifdef D_MULTISCAN_FILES_SUPPORTED225226/*227* Initialize for an output pass in buffered-image mode.228*/229230GLOBAL(boolean)231jpeg_start_output (j_decompress_ptr cinfo, int scan_number)232{233if (cinfo->global_state != DSTATE_BUFIMAGE &&234cinfo->global_state != DSTATE_PRESCAN)235ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);236/* Limit scan number to valid range */237if (scan_number <= 0)238scan_number = 1;239if (cinfo->inputctl->eoi_reached &&240scan_number > cinfo->input_scan_number)241scan_number = cinfo->input_scan_number;242cinfo->output_scan_number = scan_number;243/* Perform any dummy output passes, and set up for the real pass */244return output_pass_setup(cinfo);245}246247248/*249* Finish up after an output pass in buffered-image mode.250*251* Returns FALSE if suspended. The return value need be inspected only if252* a suspending data source is used.253*/254255GLOBAL(boolean)256jpeg_finish_output (j_decompress_ptr cinfo)257{258if ((cinfo->global_state == DSTATE_SCANNING ||259cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {260/* Terminate this pass. */261/* We do not require the whole pass to have been completed. */262(*cinfo->master->finish_output_pass) (cinfo);263cinfo->global_state = DSTATE_BUFPOST;264} else if (cinfo->global_state != DSTATE_BUFPOST) {265/* BUFPOST = repeat call after a suspension, anything else is error */266ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);267}268/* Read markers looking for SOS or EOI */269while (cinfo->input_scan_number <= cinfo->output_scan_number &&270! cinfo->inputctl->eoi_reached) {271if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)272return FALSE; /* Suspend, come back later */273}274cinfo->global_state = DSTATE_BUFIMAGE;275return TRUE;276}277278#endif /* D_MULTISCAN_FILES_SUPPORTED */279280281