Path: blob/master/src/java.desktop/share/native/libjavajpeg/jcapistd.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jcapistd.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 compression half12* of the JPEG library. These are the "standard" API routines that are13* used in the normal full-compression case. They are not used by a14* transcoding-only application. Note that if an application links in15* jpeg_start_compress, it will end up linking in the entire compressor.16* We thus must separate this file from jcapimin.c to avoid linking the17* whole compression library into a transcoder.18*/1920#define JPEG_INTERNALS21#include "jinclude.h"22#include "jpeglib.h"232425/*26* Compression initialization.27* Before calling this, all parameters and a data destination must be set up.28*29* We require a write_all_tables parameter as a failsafe check when writing30* multiple datastreams from the same compression object. Since prior runs31* will have left all the tables marked sent_table=TRUE, a subsequent run32* would emit an abbreviated stream (no tables) by default. This may be what33* is wanted, but for safety's sake it should not be the default behavior:34* programmers should have to make a deliberate choice to emit abbreviated35* images. Therefore the documentation and examples should encourage people36* to pass write_all_tables=TRUE; then it will take active thought to do the37* wrong thing.38*/3940GLOBAL(void)41jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)42{43if (cinfo->global_state != CSTATE_START)44ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);4546if (write_all_tables)47jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */4849/* (Re)initialize error mgr and destination modules */50(*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);51(*cinfo->dest->init_destination) (cinfo);52/* Perform master selection of active modules */53jinit_compress_master(cinfo);54/* Set up for the first pass */55(*cinfo->master->prepare_for_pass) (cinfo);56/* Ready for application to drive first pass through jpeg_write_scanlines57* or jpeg_write_raw_data.58*/59cinfo->next_scanline = 0;60cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);61}626364/*65* Write some scanlines of data to the JPEG compressor.66*67* The return value will be the number of lines actually written.68* This should be less than the supplied num_lines only in case that69* the data destination module has requested suspension of the compressor,70* or if more than image_height scanlines are passed in.71*72* Note: we warn about excess calls to jpeg_write_scanlines() since73* this likely signals an application programmer error. However,74* excess scanlines passed in the last valid call are *silently* ignored,75* so that the application need not adjust num_lines for end-of-image76* when using a multiple-scanline buffer.77*/7879GLOBAL(JDIMENSION)80jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,81JDIMENSION num_lines)82{83JDIMENSION row_ctr, rows_left;8485if (cinfo->global_state != CSTATE_SCANNING)86ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);87if (cinfo->next_scanline >= cinfo->image_height)88WARNMS(cinfo, JWRN_TOO_MUCH_DATA);8990/* Call progress monitor hook if present */91if (cinfo->progress != NULL) {92cinfo->progress->pass_counter = (long) cinfo->next_scanline;93cinfo->progress->pass_limit = (long) cinfo->image_height;94(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);95}9697/* Give master control module another chance if this is first call to98* jpeg_write_scanlines. This lets output of the frame/scan headers be99* delayed so that application can write COM, etc, markers between100* jpeg_start_compress and jpeg_write_scanlines.101*/102if (cinfo->master->call_pass_startup)103(*cinfo->master->pass_startup) (cinfo);104105/* Ignore any extra scanlines at bottom of image. */106rows_left = cinfo->image_height - cinfo->next_scanline;107if (num_lines > rows_left)108num_lines = rows_left;109110row_ctr = 0;111(*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);112cinfo->next_scanline += row_ctr;113return row_ctr;114}115116117/*118* Alternate entry point to write raw data.119* Processes exactly one iMCU row per call, unless suspended.120*/121122GLOBAL(JDIMENSION)123jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,124JDIMENSION num_lines)125{126JDIMENSION lines_per_iMCU_row;127128if (cinfo->global_state != CSTATE_RAW_OK)129ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);130if (cinfo->next_scanline >= cinfo->image_height) {131WARNMS(cinfo, JWRN_TOO_MUCH_DATA);132return 0;133}134135/* Call progress monitor hook if present */136if (cinfo->progress != NULL) {137cinfo->progress->pass_counter = (long) cinfo->next_scanline;138cinfo->progress->pass_limit = (long) cinfo->image_height;139(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);140}141142/* Give master control module another chance if this is first call to143* jpeg_write_raw_data. This lets output of the frame/scan headers be144* delayed so that application can write COM, etc, markers between145* jpeg_start_compress and jpeg_write_raw_data.146*/147if (cinfo->master->call_pass_startup)148(*cinfo->master->pass_startup) (cinfo);149150/* Verify that at least one iMCU row has been passed. */151lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;152if (num_lines < lines_per_iMCU_row)153ERREXIT(cinfo, JERR_BUFFER_SIZE);154155/* Directly compress the row. */156if (! (*cinfo->coef->compress_data) (cinfo, data)) {157/* If compressor did not consume the whole row, suspend processing. */158return 0;159}160161/* OK, we processed one iMCU row. */162cinfo->next_scanline += lines_per_iMCU_row;163return lines_per_iMCU_row;164}165166167