/*1* jcapimin.c2*3* Copyright (C) 1994-1998, Thomas G. Lane.4* Modified 2003-2010 by Guido Vollbeding.5* This file is part of the Independent JPEG Group's software.6* For conditions of distribution and use, see the accompanying README file.7*8* This file contains application interface code for the compression half9* of the JPEG library. These are the "minimum" API routines that may be10* needed in either the normal full-compression case or the transcoding-only11* case.12*13* Most of the routines intended to be called directly by an application14* are in this file or in jcapistd.c. But also see jcparam.c for15* parameter-setup helper routines, jcomapi.c for routines shared by16* compression and decompression, and jctrans.c for the transcoding case.17*/1819#define JPEG_INTERNALS20#include "jinclude.h"21#include "jpeglib.h"222324/*25* Initialization of a JPEG compression object.26* The error manager must already be set up (in case memory manager fails).27*/2829GLOBAL(void)30jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)31{32int i;3334/* Guard against version mismatches between library and caller. */35cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */36if (version != JPEG_LIB_VERSION)37ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);38if (structsize != SIZEOF(struct jpeg_compress_struct))39ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,40(int) SIZEOF(struct jpeg_compress_struct), (int) structsize);4142/* For debugging purposes, we zero the whole master structure.43* But the application has already set the err pointer, and may have set44* client_data, so we have to save and restore those fields.45* Note: if application hasn't set client_data, tools like Purify may46* complain here.47*/48{49struct jpeg_error_mgr * err = cinfo->err;50void * client_data = cinfo->client_data; /* ignore Purify complaint here */51MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));52cinfo->err = err;53cinfo->client_data = client_data;54}55cinfo->is_decompressor = FALSE;5657/* Initialize a memory manager instance for this object */58jinit_memory_mgr((j_common_ptr) cinfo);5960/* Zero out pointers to permanent structures. */61cinfo->progress = NULL;62cinfo->dest = NULL;6364cinfo->comp_info = NULL;6566for (i = 0; i < NUM_QUANT_TBLS; i++) {67cinfo->quant_tbl_ptrs[i] = NULL;68cinfo->q_scale_factor[i] = 100;69}7071for (i = 0; i < NUM_HUFF_TBLS; i++) {72cinfo->dc_huff_tbl_ptrs[i] = NULL;73cinfo->ac_huff_tbl_ptrs[i] = NULL;74}7576/* Must do it here for emit_dqt in case jpeg_write_tables is used */77cinfo->block_size = DCTSIZE;78cinfo->natural_order = jpeg_natural_order;79cinfo->lim_Se = DCTSIZE2-1;8081cinfo->script_space = NULL;8283cinfo->input_gamma = 1.0; /* in case application forgets */8485/* OK, I'm ready */86cinfo->global_state = CSTATE_START;87}888990/*91* Destruction of a JPEG compression object92*/9394GLOBAL(void)95jpeg_destroy_compress (j_compress_ptr cinfo)96{97jpeg_destroy((j_common_ptr) cinfo); /* use common routine */98}99100101/*102* Abort processing of a JPEG compression operation,103* but don't destroy the object itself.104*/105106GLOBAL(void)107jpeg_abort_compress (j_compress_ptr cinfo)108{109jpeg_abort((j_common_ptr) cinfo); /* use common routine */110}111112113/*114* Forcibly suppress or un-suppress all quantization and Huffman tables.115* Marks all currently defined tables as already written (if suppress)116* or not written (if !suppress). This will control whether they get emitted117* by a subsequent jpeg_start_compress call.118*119* This routine is exported for use by applications that want to produce120* abbreviated JPEG datastreams. It logically belongs in jcparam.c, but121* since it is called by jpeg_start_compress, we put it here --- otherwise122* jcparam.o would be linked whether the application used it or not.123*/124125GLOBAL(void)126jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)127{128int i;129JQUANT_TBL * qtbl;130JHUFF_TBL * htbl;131132for (i = 0; i < NUM_QUANT_TBLS; i++) {133if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)134qtbl->sent_table = suppress;135}136137for (i = 0; i < NUM_HUFF_TBLS; i++) {138if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)139htbl->sent_table = suppress;140if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)141htbl->sent_table = suppress;142}143}144145146/*147* Finish JPEG compression.148*149* If a multipass operating mode was selected, this may do a great deal of150* work including most of the actual output.151*/152153GLOBAL(void)154jpeg_finish_compress (j_compress_ptr cinfo)155{156JDIMENSION iMCU_row;157158if (cinfo->global_state == CSTATE_SCANNING ||159cinfo->global_state == CSTATE_RAW_OK) {160/* Terminate first pass */161if (cinfo->next_scanline < cinfo->image_height)162ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);163(*cinfo->master->finish_pass) (cinfo);164} else if (cinfo->global_state != CSTATE_WRCOEFS)165ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);166/* Perform any remaining passes */167while (! cinfo->master->is_last_pass) {168(*cinfo->master->prepare_for_pass) (cinfo);169for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {170if (cinfo->progress != NULL) {171cinfo->progress->pass_counter = (long) iMCU_row;172cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;173(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);174}175/* We bypass the main controller and invoke coef controller directly;176* all work is being done from the coefficient buffer.177*/178if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))179ERREXIT(cinfo, JERR_CANT_SUSPEND);180}181(*cinfo->master->finish_pass) (cinfo);182}183/* Write EOI, do final cleanup */184(*cinfo->marker->write_file_trailer) (cinfo);185(*cinfo->dest->term_destination) (cinfo);186/* We can use jpeg_abort to release memory and reset global_state */187jpeg_abort((j_common_ptr) cinfo);188}189190191/*192* Write a special marker.193* This is only recommended for writing COM or APPn markers.194* Must be called after jpeg_start_compress() and before195* first call to jpeg_write_scanlines() or jpeg_write_raw_data().196*/197198GLOBAL(void)199jpeg_write_marker (j_compress_ptr cinfo, int marker,200const JOCTET *dataptr, unsigned int datalen)201{202JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));203204if (cinfo->next_scanline != 0 ||205(cinfo->global_state != CSTATE_SCANNING &&206cinfo->global_state != CSTATE_RAW_OK &&207cinfo->global_state != CSTATE_WRCOEFS))208ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);209210(*cinfo->marker->write_marker_header) (cinfo, marker, datalen);211write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */212while (datalen--) {213(*write_marker_byte) (cinfo, *dataptr);214dataptr++;215}216}217218/* Same, but piecemeal. */219220GLOBAL(void)221jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)222{223if (cinfo->next_scanline != 0 ||224(cinfo->global_state != CSTATE_SCANNING &&225cinfo->global_state != CSTATE_RAW_OK &&226cinfo->global_state != CSTATE_WRCOEFS))227ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);228229(*cinfo->marker->write_marker_header) (cinfo, marker, datalen);230}231232GLOBAL(void)233jpeg_write_m_byte (j_compress_ptr cinfo, int val)234{235(*cinfo->marker->write_marker_byte) (cinfo, val);236}237238239/*240* Alternate compression function: just write an abbreviated table file.241* Before calling this, all parameters and a data destination must be set up.242*243* To produce a pair of files containing abbreviated tables and abbreviated244* image data, one would proceed as follows:245*246* initialize JPEG object247* set JPEG parameters248* set destination to table file249* jpeg_write_tables(cinfo);250* set destination to image file251* jpeg_start_compress(cinfo, FALSE);252* write data...253* jpeg_finish_compress(cinfo);254*255* jpeg_write_tables has the side effect of marking all tables written256* (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress257* will not re-emit the tables unless it is passed write_all_tables=TRUE.258*/259260GLOBAL(void)261jpeg_write_tables (j_compress_ptr cinfo)262{263if (cinfo->global_state != CSTATE_START)264ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);265266/* (Re)initialize error mgr and destination modules */267(*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);268(*cinfo->dest->init_destination) (cinfo);269/* Initialize the marker writer ... bit of a crock to do it here. */270jinit_marker_writer(cinfo);271/* Write them tables! */272(*cinfo->marker->write_tables_only) (cinfo);273/* And clean up. */274(*cinfo->dest->term_destination) (cinfo);275/*276* In library releases up through v6a, we called jpeg_abort() here to free277* any working memory allocated by the destination manager and marker278* writer. Some applications had a problem with that: they allocated space279* of their own from the library memory manager, and didn't want it to go280* away during write_tables. So now we do nothing. This will cause a281* memory leak if an app calls write_tables repeatedly without doing a full282* compression cycle or otherwise resetting the JPEG object. However, that283* seems less bad than unexpectedly freeing memory in the normal case.284* An app that prefers the old behavior can call jpeg_abort for itself after285* each call to jpeg_write_tables().286*/287}288289290