Path: blob/master/3rdparty/libjpeg-turbo/src/jcapimin.c
16337 views
/*1* jcapimin.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1998, Thomas G. Lane.5* Modified 2003-2010 by Guido Vollbeding.6* It was modified by The libjpeg-turbo Project to include only code relevant7* to libjpeg-turbo.8* For conditions of distribution and use, see the accompanying README.ijg9* 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;71#if JPEG_LIB_VERSION >= 7072cinfo->q_scale_factor[i] = 100;73#endif74}7576for (i = 0; i < NUM_HUFF_TBLS; i++) {77cinfo->dc_huff_tbl_ptrs[i] = NULL;78cinfo->ac_huff_tbl_ptrs[i] = NULL;79}8081#if JPEG_LIB_VERSION >= 8082/* Must do it here for emit_dqt in case jpeg_write_tables is used */83cinfo->block_size = DCTSIZE;84cinfo->natural_order = jpeg_natural_order;85cinfo->lim_Se = DCTSIZE2-1;86#endif8788cinfo->script_space = NULL;8990cinfo->input_gamma = 1.0; /* in case application forgets */9192/* OK, I'm ready */93cinfo->global_state = CSTATE_START;94}959697/*98* Destruction of a JPEG compression object99*/100101GLOBAL(void)102jpeg_destroy_compress (j_compress_ptr cinfo)103{104jpeg_destroy((j_common_ptr) cinfo); /* use common routine */105}106107108/*109* Abort processing of a JPEG compression operation,110* but don't destroy the object itself.111*/112113GLOBAL(void)114jpeg_abort_compress (j_compress_ptr cinfo)115{116jpeg_abort((j_common_ptr) cinfo); /* use common routine */117}118119120/*121* Forcibly suppress or un-suppress all quantization and Huffman tables.122* Marks all currently defined tables as already written (if suppress)123* or not written (if !suppress). This will control whether they get emitted124* by a subsequent jpeg_start_compress call.125*126* This routine is exported for use by applications that want to produce127* abbreviated JPEG datastreams. It logically belongs in jcparam.c, but128* since it is called by jpeg_start_compress, we put it here --- otherwise129* jcparam.o would be linked whether the application used it or not.130*/131132GLOBAL(void)133jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)134{135int i;136JQUANT_TBL *qtbl;137JHUFF_TBL *htbl;138139for (i = 0; i < NUM_QUANT_TBLS; i++) {140if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)141qtbl->sent_table = suppress;142}143144for (i = 0; i < NUM_HUFF_TBLS; i++) {145if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)146htbl->sent_table = suppress;147if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)148htbl->sent_table = suppress;149}150}151152153/*154* Finish JPEG compression.155*156* If a multipass operating mode was selected, this may do a great deal of157* work including most of the actual output.158*/159160GLOBAL(void)161jpeg_finish_compress (j_compress_ptr cinfo)162{163JDIMENSION iMCU_row;164165if (cinfo->global_state == CSTATE_SCANNING ||166cinfo->global_state == CSTATE_RAW_OK) {167/* Terminate first pass */168if (cinfo->next_scanline < cinfo->image_height)169ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);170(*cinfo->master->finish_pass) (cinfo);171} else if (cinfo->global_state != CSTATE_WRCOEFS)172ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);173/* Perform any remaining passes */174while (! cinfo->master->is_last_pass) {175(*cinfo->master->prepare_for_pass) (cinfo);176for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {177if (cinfo->progress != NULL) {178cinfo->progress->pass_counter = (long) iMCU_row;179cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;180(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);181}182/* We bypass the main controller and invoke coef controller directly;183* all work is being done from the coefficient buffer.184*/185if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))186ERREXIT(cinfo, JERR_CANT_SUSPEND);187}188(*cinfo->master->finish_pass) (cinfo);189}190/* Write EOI, do final cleanup */191(*cinfo->marker->write_file_trailer) (cinfo);192(*cinfo->dest->term_destination) (cinfo);193/* We can use jpeg_abort to release memory and reset global_state */194jpeg_abort((j_common_ptr) cinfo);195}196197198/*199* Write a special marker.200* This is only recommended for writing COM or APPn markers.201* Must be called after jpeg_start_compress() and before202* first call to jpeg_write_scanlines() or jpeg_write_raw_data().203*/204205GLOBAL(void)206jpeg_write_marker (j_compress_ptr cinfo, int marker,207const JOCTET *dataptr, unsigned int datalen)208{209void (*write_marker_byte) (j_compress_ptr info, int val);210211if (cinfo->next_scanline != 0 ||212(cinfo->global_state != CSTATE_SCANNING &&213cinfo->global_state != CSTATE_RAW_OK &&214cinfo->global_state != CSTATE_WRCOEFS))215ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);216217(*cinfo->marker->write_marker_header) (cinfo, marker, datalen);218write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */219while (datalen--) {220(*write_marker_byte) (cinfo, *dataptr);221dataptr++;222}223}224225/* Same, but piecemeal. */226227GLOBAL(void)228jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)229{230if (cinfo->next_scanline != 0 ||231(cinfo->global_state != CSTATE_SCANNING &&232cinfo->global_state != CSTATE_RAW_OK &&233cinfo->global_state != CSTATE_WRCOEFS))234ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);235236(*cinfo->marker->write_marker_header) (cinfo, marker, datalen);237}238239GLOBAL(void)240jpeg_write_m_byte (j_compress_ptr cinfo, int val)241{242(*cinfo->marker->write_marker_byte) (cinfo, val);243}244245246/*247* Alternate compression function: just write an abbreviated table file.248* Before calling this, all parameters and a data destination must be set up.249*250* To produce a pair of files containing abbreviated tables and abbreviated251* image data, one would proceed as follows:252*253* initialize JPEG object254* set JPEG parameters255* set destination to table file256* jpeg_write_tables(cinfo);257* set destination to image file258* jpeg_start_compress(cinfo, FALSE);259* write data...260* jpeg_finish_compress(cinfo);261*262* jpeg_write_tables has the side effect of marking all tables written263* (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress264* will not re-emit the tables unless it is passed write_all_tables=TRUE.265*/266267GLOBAL(void)268jpeg_write_tables (j_compress_ptr cinfo)269{270if (cinfo->global_state != CSTATE_START)271ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);272273/* (Re)initialize error mgr and destination modules */274(*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);275(*cinfo->dest->init_destination) (cinfo);276/* Initialize the marker writer ... bit of a crock to do it here. */277jinit_marker_writer(cinfo);278/* Write them tables! */279(*cinfo->marker->write_tables_only) (cinfo);280/* And clean up. */281(*cinfo->dest->term_destination) (cinfo);282/*283* In library releases up through v6a, we called jpeg_abort() here to free284* any working memory allocated by the destination manager and marker285* writer. Some applications had a problem with that: they allocated space286* of their own from the library memory manager, and didn't want it to go287* away during write_tables. So now we do nothing. This will cause a288* memory leak if an app calls write_tables repeatedly without doing a full289* compression cycle or otherwise resetting the JPEG object. However, that290* seems less bad than unexpectedly freeing memory in the normal case.291* An app that prefers the old behavior can call jpeg_abort for itself after292* each call to jpeg_write_tables().293*/294}295296297