Path: blob/master/src/java.desktop/share/native/libjavajpeg/jcapimin.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jcapimin.c6*7* Copyright (C) 1994-1998, 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 "minimum" API routines that may be13* needed in either the normal full-compression case or the transcoding-only14* case.15*16* Most of the routines intended to be called directly by an application17* are in this file or in jcapistd.c. But also see jcparam.c for18* parameter-setup helper routines, jcomapi.c for routines shared by19* compression and decompression, and jctrans.c for the transcoding case.20*/2122#define JPEG_INTERNALS23#include "jinclude.h"24#include "jpeglib.h"252627/*28* Initialization of a JPEG compression object.29* The error manager must already be set up (in case memory manager fails).30*/3132GLOBAL(void)33jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)34{35int i;3637/* Guard against version mismatches between library and caller. */38cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */39if (version != JPEG_LIB_VERSION)40ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);41if (structsize != SIZEOF(struct jpeg_compress_struct))42ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,43(int) SIZEOF(struct jpeg_compress_struct), (int) structsize);4445/* For debugging purposes, we zero the whole master structure.46* But the application has already set the err pointer, and may have set47* client_data, so we have to save and restore those fields.48* Note: if application hasn't set client_data, tools like Purify may49* complain here.50*/51{52struct jpeg_error_mgr * err = cinfo->err;53void * client_data = cinfo->client_data; /* ignore Purify complaint here */54MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));55cinfo->err = err;56cinfo->client_data = client_data;57}58cinfo->is_decompressor = FALSE;5960/* Initialize a memory manager instance for this object */61jinit_memory_mgr((j_common_ptr) cinfo);6263/* Zero out pointers to permanent structures. */64cinfo->progress = NULL;65cinfo->dest = NULL;6667cinfo->comp_info = NULL;6869for (i = 0; i < NUM_QUANT_TBLS; i++)70cinfo->quant_tbl_ptrs[i] = NULL;7172for (i = 0; i < NUM_HUFF_TBLS; i++) {73cinfo->dc_huff_tbl_ptrs[i] = NULL;74cinfo->ac_huff_tbl_ptrs[i] = NULL;75}7677cinfo->script_space = NULL;7879cinfo->input_gamma = 1.0; /* in case application forgets */8081/* OK, I'm ready */82cinfo->global_state = CSTATE_START;83}848586/*87* Destruction of a JPEG compression object88*/8990GLOBAL(void)91jpeg_destroy_compress (j_compress_ptr cinfo)92{93jpeg_destroy((j_common_ptr) cinfo); /* use common routine */94}959697/*98* Abort processing of a JPEG compression operation,99* but don't destroy the object itself.100*/101102GLOBAL(void)103jpeg_abort_compress (j_compress_ptr cinfo)104{105jpeg_abort((j_common_ptr) cinfo); /* use common routine */106}107108109/*110* Forcibly suppress or un-suppress all quantization and Huffman tables.111* Marks all currently defined tables as already written (if suppress)112* or not written (if !suppress). This will control whether they get emitted113* by a subsequent jpeg_start_compress call.114*115* This routine is exported for use by applications that want to produce116* abbreviated JPEG datastreams. It logically belongs in jcparam.c, but117* since it is called by jpeg_start_compress, we put it here --- otherwise118* jcparam.o would be linked whether the application used it or not.119*/120121GLOBAL(void)122jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)123{124int i;125JQUANT_TBL * qtbl;126JHUFF_TBL * htbl;127128for (i = 0; i < NUM_QUANT_TBLS; i++) {129if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)130qtbl->sent_table = suppress;131}132133for (i = 0; i < NUM_HUFF_TBLS; i++) {134if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)135htbl->sent_table = suppress;136if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)137htbl->sent_table = suppress;138}139}140141142/*143* Finish JPEG compression.144*145* If a multipass operating mode was selected, this may do a great deal of146* work including most of the actual output.147*/148149GLOBAL(void)150jpeg_finish_compress (j_compress_ptr cinfo)151{152JDIMENSION iMCU_row;153154if (cinfo->global_state == CSTATE_SCANNING ||155cinfo->global_state == CSTATE_RAW_OK) {156/* Terminate first pass */157if (cinfo->next_scanline < cinfo->image_height)158ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);159(*cinfo->master->finish_pass) (cinfo);160} else if (cinfo->global_state != CSTATE_WRCOEFS)161ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);162/* Perform any remaining passes */163while (! cinfo->master->is_last_pass) {164(*cinfo->master->prepare_for_pass) (cinfo);165for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {166if (cinfo->progress != NULL) {167cinfo->progress->pass_counter = (long) iMCU_row;168cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;169(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);170}171/* We bypass the main controller and invoke coef controller directly;172* all work is being done from the coefficient buffer.173*/174if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))175ERREXIT(cinfo, JERR_CANT_SUSPEND);176}177(*cinfo->master->finish_pass) (cinfo);178}179/* Write EOI, do final cleanup */180(*cinfo->marker->write_file_trailer) (cinfo);181(*cinfo->dest->term_destination) (cinfo);182/* We can use jpeg_abort to release memory and reset global_state */183jpeg_abort((j_common_ptr) cinfo);184}185186187/*188* Write a special marker.189* This is only recommended for writing COM or APPn markers.190* Must be called after jpeg_start_compress() and before191* first call to jpeg_write_scanlines() or jpeg_write_raw_data().192*/193194GLOBAL(void)195jpeg_write_marker (j_compress_ptr cinfo, int marker,196const JOCTET *dataptr, unsigned int datalen)197{198JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));199200if (cinfo->next_scanline != 0 ||201(cinfo->global_state != CSTATE_SCANNING &&202cinfo->global_state != CSTATE_RAW_OK &&203cinfo->global_state != CSTATE_WRCOEFS))204ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);205206(*cinfo->marker->write_marker_header) (cinfo, marker, datalen);207write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */208while (datalen--) {209(*write_marker_byte) (cinfo, *dataptr);210dataptr++;211}212}213214/* Same, but piecemeal. */215216GLOBAL(void)217jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)218{219if (cinfo->next_scanline != 0 ||220(cinfo->global_state != CSTATE_SCANNING &&221cinfo->global_state != CSTATE_RAW_OK &&222cinfo->global_state != CSTATE_WRCOEFS))223ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);224225(*cinfo->marker->write_marker_header) (cinfo, marker, datalen);226}227228GLOBAL(void)229jpeg_write_m_byte (j_compress_ptr cinfo, int val)230{231(*cinfo->marker->write_marker_byte) (cinfo, val);232}233234235/*236* Alternate compression function: just write an abbreviated table file.237* Before calling this, all parameters and a data destination must be set up.238*239* To produce a pair of files containing abbreviated tables and abbreviated240* image data, one would proceed as follows:241*242* initialize JPEG object243* set JPEG parameters244* set destination to table file245* jpeg_write_tables(cinfo);246* set destination to image file247* jpeg_start_compress(cinfo, FALSE);248* write data...249* jpeg_finish_compress(cinfo);250*251* jpeg_write_tables has the side effect of marking all tables written252* (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress253* will not re-emit the tables unless it is passed write_all_tables=TRUE.254*/255256GLOBAL(void)257jpeg_write_tables (j_compress_ptr cinfo)258{259if (cinfo->global_state != CSTATE_START)260ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);261262/* (Re)initialize error mgr and destination modules */263(*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);264(*cinfo->dest->init_destination) (cinfo);265/* Initialize the marker writer ... bit of a crock to do it here. */266jinit_marker_writer(cinfo);267/* Write them tables! */268(*cinfo->marker->write_tables_only) (cinfo);269/* And clean up. */270(*cinfo->dest->term_destination) (cinfo);271/*272* In library releases up through v6a, we called jpeg_abort() here to free273* any working memory allocated by the destination manager and marker274* writer. Some applications had a problem with that: they allocated space275* of their own from the library memory manager, and didn't want it to go276* away during write_tables. So now we do nothing. This will cause a277* memory leak if an app calls write_tables repeatedly without doing a full278* compression cycle or otherwise resetting the JPEG object. However, that279* seems less bad than unexpectedly freeing memory in the normal case.280* An app that prefers the old behavior can call jpeg_abort for itself after281* each call to jpeg_write_tables().282*/283}284285286