nuclear@1: /* nuclear@1: * jcapimin.c nuclear@1: * nuclear@1: * Copyright (C) 1994-1998, Thomas G. Lane. nuclear@1: * This file is part of the Independent JPEG Group's software. nuclear@1: * For conditions of distribution and use, see the accompanying README file. nuclear@1: * nuclear@1: * This file contains application interface code for the compression half nuclear@1: * of the JPEG library. These are the "minimum" API routines that may be nuclear@1: * needed in either the normal full-compression case or the transcoding-only nuclear@1: * case. nuclear@1: * nuclear@1: * Most of the routines intended to be called directly by an application nuclear@1: * are in this file or in jcapistd.c. But also see jcparam.c for nuclear@1: * parameter-setup helper routines, jcomapi.c for routines shared by nuclear@1: * compression and decompression, and jctrans.c for the transcoding case. nuclear@1: */ nuclear@1: nuclear@1: #define JPEG_INTERNALS nuclear@1: #include "jinclude.h" nuclear@1: #include "jpeglib.h" nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Initialization of a JPEG compression object. nuclear@1: * The error manager must already be set up (in case memory manager fails). nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(void) nuclear@1: jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize) nuclear@1: { nuclear@1: int i; nuclear@1: nuclear@1: /* Guard against version mismatches between library and caller. */ nuclear@1: cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */ nuclear@1: if (version != JPEG_LIB_VERSION) nuclear@1: ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version); nuclear@1: if (structsize != SIZEOF(struct jpeg_compress_struct)) nuclear@1: ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, nuclear@1: (int) SIZEOF(struct jpeg_compress_struct), (int) structsize); nuclear@1: nuclear@1: /* For debugging purposes, we zero the whole master structure. nuclear@1: * But the application has already set the err pointer, and may have set nuclear@1: * client_data, so we have to save and restore those fields. nuclear@1: * Note: if application hasn't set client_data, tools like Purify may nuclear@1: * complain here. nuclear@1: */ nuclear@1: { nuclear@1: struct jpeg_error_mgr * err = cinfo->err; nuclear@1: void * client_data = cinfo->client_data; /* ignore Purify complaint here */ nuclear@1: MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct)); nuclear@1: cinfo->err = err; nuclear@1: cinfo->client_data = client_data; nuclear@1: } nuclear@1: cinfo->is_decompressor = FALSE; nuclear@1: nuclear@1: /* Initialize a memory manager instance for this object */ nuclear@1: jinit_memory_mgr((j_common_ptr) cinfo); nuclear@1: nuclear@1: /* Zero out pointers to permanent structures. */ nuclear@1: cinfo->progress = NULL; nuclear@1: cinfo->dest = NULL; nuclear@1: nuclear@1: cinfo->comp_info = NULL; nuclear@1: nuclear@1: for (i = 0; i < NUM_QUANT_TBLS; i++) nuclear@1: cinfo->quant_tbl_ptrs[i] = NULL; nuclear@1: nuclear@1: for (i = 0; i < NUM_HUFF_TBLS; i++) { nuclear@1: cinfo->dc_huff_tbl_ptrs[i] = NULL; nuclear@1: cinfo->ac_huff_tbl_ptrs[i] = NULL; nuclear@1: } nuclear@1: nuclear@1: cinfo->script_space = NULL; nuclear@1: nuclear@1: cinfo->input_gamma = 1.0; /* in case application forgets */ nuclear@1: nuclear@1: /* OK, I'm ready */ nuclear@1: cinfo->global_state = CSTATE_START; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Destruction of a JPEG compression object nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(void) nuclear@1: jpeg_destroy_compress (j_compress_ptr cinfo) nuclear@1: { nuclear@1: jpeg_destroy((j_common_ptr) cinfo); /* use common routine */ nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Abort processing of a JPEG compression operation, nuclear@1: * but don't destroy the object itself. nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(void) nuclear@1: jpeg_abort_compress (j_compress_ptr cinfo) nuclear@1: { nuclear@1: jpeg_abort((j_common_ptr) cinfo); /* use common routine */ nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Forcibly suppress or un-suppress all quantization and Huffman tables. nuclear@1: * Marks all currently defined tables as already written (if suppress) nuclear@1: * or not written (if !suppress). This will control whether they get emitted nuclear@1: * by a subsequent jpeg_start_compress call. nuclear@1: * nuclear@1: * This routine is exported for use by applications that want to produce nuclear@1: * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but nuclear@1: * since it is called by jpeg_start_compress, we put it here --- otherwise nuclear@1: * jcparam.o would be linked whether the application used it or not. nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(void) nuclear@1: jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress) nuclear@1: { nuclear@1: int i; nuclear@1: JQUANT_TBL * qtbl; nuclear@1: JHUFF_TBL * htbl; nuclear@1: nuclear@1: for (i = 0; i < NUM_QUANT_TBLS; i++) { nuclear@1: if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL) nuclear@1: qtbl->sent_table = suppress; nuclear@1: } nuclear@1: nuclear@1: for (i = 0; i < NUM_HUFF_TBLS; i++) { nuclear@1: if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL) nuclear@1: htbl->sent_table = suppress; nuclear@1: if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL) nuclear@1: htbl->sent_table = suppress; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Finish JPEG compression. nuclear@1: * nuclear@1: * If a multipass operating mode was selected, this may do a great deal of nuclear@1: * work including most of the actual output. nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(void) nuclear@1: jpeg_finish_compress (j_compress_ptr cinfo) nuclear@1: { nuclear@1: JDIMENSION iMCU_row; nuclear@1: nuclear@1: if (cinfo->global_state == CSTATE_SCANNING || nuclear@1: cinfo->global_state == CSTATE_RAW_OK) { nuclear@1: /* Terminate first pass */ nuclear@1: if (cinfo->next_scanline < cinfo->image_height) nuclear@1: ERREXIT(cinfo, JERR_TOO_LITTLE_DATA); nuclear@1: (*cinfo->master->finish_pass) (cinfo); nuclear@1: } else if (cinfo->global_state != CSTATE_WRCOEFS) nuclear@1: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@1: /* Perform any remaining passes */ nuclear@1: while (! cinfo->master->is_last_pass) { nuclear@1: (*cinfo->master->prepare_for_pass) (cinfo); nuclear@1: for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) { nuclear@1: if (cinfo->progress != NULL) { nuclear@1: cinfo->progress->pass_counter = (long) iMCU_row; nuclear@1: cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows; nuclear@1: (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); nuclear@1: } nuclear@1: /* We bypass the main controller and invoke coef controller directly; nuclear@1: * all work is being done from the coefficient buffer. nuclear@1: */ nuclear@1: if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL)) nuclear@1: ERREXIT(cinfo, JERR_CANT_SUSPEND); nuclear@1: } nuclear@1: (*cinfo->master->finish_pass) (cinfo); nuclear@1: } nuclear@1: /* Write EOI, do final cleanup */ nuclear@1: (*cinfo->marker->write_file_trailer) (cinfo); nuclear@1: (*cinfo->dest->term_destination) (cinfo); nuclear@1: /* We can use jpeg_abort to release memory and reset global_state */ nuclear@1: jpeg_abort((j_common_ptr) cinfo); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Write a special marker. nuclear@1: * This is only recommended for writing COM or APPn markers. nuclear@1: * Must be called after jpeg_start_compress() and before nuclear@1: * first call to jpeg_write_scanlines() or jpeg_write_raw_data(). nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(void) nuclear@1: jpeg_write_marker (j_compress_ptr cinfo, int marker, nuclear@1: const JOCTET *dataptr, unsigned int datalen) nuclear@1: { nuclear@1: JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val)); nuclear@1: nuclear@1: if (cinfo->next_scanline != 0 || nuclear@1: (cinfo->global_state != CSTATE_SCANNING && nuclear@1: cinfo->global_state != CSTATE_RAW_OK && nuclear@1: cinfo->global_state != CSTATE_WRCOEFS)) nuclear@1: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@1: nuclear@1: (*cinfo->marker->write_marker_header) (cinfo, marker, datalen); nuclear@1: write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */ nuclear@1: while (datalen--) { nuclear@1: (*write_marker_byte) (cinfo, *dataptr); nuclear@1: dataptr++; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* Same, but piecemeal. */ nuclear@1: nuclear@1: GLOBAL(void) nuclear@1: jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen) nuclear@1: { nuclear@1: if (cinfo->next_scanline != 0 || nuclear@1: (cinfo->global_state != CSTATE_SCANNING && nuclear@1: cinfo->global_state != CSTATE_RAW_OK && nuclear@1: cinfo->global_state != CSTATE_WRCOEFS)) nuclear@1: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@1: nuclear@1: (*cinfo->marker->write_marker_header) (cinfo, marker, datalen); nuclear@1: } nuclear@1: nuclear@1: GLOBAL(void) nuclear@1: jpeg_write_m_byte (j_compress_ptr cinfo, int val) nuclear@1: { nuclear@1: (*cinfo->marker->write_marker_byte) (cinfo, val); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Alternate compression function: just write an abbreviated table file. nuclear@1: * Before calling this, all parameters and a data destination must be set up. nuclear@1: * nuclear@1: * To produce a pair of files containing abbreviated tables and abbreviated nuclear@1: * image data, one would proceed as follows: nuclear@1: * nuclear@1: * initialize JPEG object nuclear@1: * set JPEG parameters nuclear@1: * set destination to table file nuclear@1: * jpeg_write_tables(cinfo); nuclear@1: * set destination to image file nuclear@1: * jpeg_start_compress(cinfo, FALSE); nuclear@1: * write data... nuclear@1: * jpeg_finish_compress(cinfo); nuclear@1: * nuclear@1: * jpeg_write_tables has the side effect of marking all tables written nuclear@1: * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress nuclear@1: * will not re-emit the tables unless it is passed write_all_tables=TRUE. nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(void) nuclear@1: jpeg_write_tables (j_compress_ptr cinfo) nuclear@1: { nuclear@1: if (cinfo->global_state != CSTATE_START) nuclear@1: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@1: nuclear@1: /* (Re)initialize error mgr and destination modules */ nuclear@1: (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); nuclear@1: (*cinfo->dest->init_destination) (cinfo); nuclear@1: /* Initialize the marker writer ... bit of a crock to do it here. */ nuclear@1: jinit_marker_writer(cinfo); nuclear@1: /* Write them tables! */ nuclear@1: (*cinfo->marker->write_tables_only) (cinfo); nuclear@1: /* And clean up. */ nuclear@1: (*cinfo->dest->term_destination) (cinfo); nuclear@1: /* nuclear@1: * In library releases up through v6a, we called jpeg_abort() here to free nuclear@1: * any working memory allocated by the destination manager and marker nuclear@1: * writer. Some applications had a problem with that: they allocated space nuclear@1: * of their own from the library memory manager, and didn't want it to go nuclear@1: * away during write_tables. So now we do nothing. This will cause a nuclear@1: * memory leak if an app calls write_tables repeatedly without doing a full nuclear@1: * compression cycle or otherwise resetting the JPEG object. However, that nuclear@1: * seems less bad than unexpectedly freeing memory in the normal case. nuclear@1: * An app that prefers the old behavior can call jpeg_abort for itself after nuclear@1: * each call to jpeg_write_tables(). nuclear@1: */ nuclear@1: }