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