blob: f6655cd5a0ed4f67a74fb84553977baafb16f8c6 [file] [log] [blame] [edit]
/*
* jcomapi.c
*
* This file was part of the Independent JPEG Group's software:
* Copyright (C) 1994-1997, Thomas G. Lane.
* libjpeg-turbo Modifications:
* Copyright (C) 2024-2025, D. R. Commander.
* For conditions of distribution and use, see the accompanying README.ijg
* file.
*
* This file contains application interface routines that are used for both
* compression and decompression.
*/
#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"
#ifdef WITH_PROFILE
#include <stdio.h>
#endif
/*
* Abort processing of a JPEG compression or decompression operation,
* but don't destroy the object itself.
*
* For this, we merely clean up all the nonpermanent memory pools.
* Note that temp files (virtual arrays) are not allowed to belong to
* the permanent pool, so we will be able to close all temp files here.
* Closing a data source or destination, if necessary, is the application's
* responsibility.
*/
GLOBAL(void)
jpeg_abort(j_common_ptr cinfo)
{
int pool;
/* Do nothing if called on a not-initialized or destroyed JPEG object. */
if (cinfo->mem == NULL)
return;
/* Releasing pools in reverse order might help avoid fragmentation
* with some (brain-damaged) malloc libraries.
*/
for (pool = JPOOL_NUMPOOLS - 1; pool > JPOOL_PERMANENT; pool--) {
(*cinfo->mem->free_pool) (cinfo, pool);
}
/* Reset overall state for possible reuse of object */
if (cinfo->is_decompressor) {
cinfo->global_state = DSTATE_START;
/* Try to keep application from accessing now-deleted marker list.
* A bit kludgy to do it here, but this is the most central place.
*/
((j_decompress_ptr)cinfo)->marker_list = NULL;
((j_decompress_ptr)cinfo)->master->marker_list_end = NULL;
} else {
cinfo->global_state = CSTATE_START;
}
}
/*
* Destruction of a JPEG object.
*
* Everything gets deallocated except the master jpeg_compress_struct itself
* and the error manager struct. Both of these are supplied by the application
* and must be freed, if necessary, by the application. (Often they are on
* the stack and so don't need to be freed anyway.)
* Closing a data source or destination, if necessary, is the application's
* responsibility.
*/
GLOBAL(void)
jpeg_destroy(j_common_ptr cinfo)
{
#ifdef WITH_PROFILE
if (cinfo->is_decompressor) {
if (((j_decompress_ptr)cinfo)->master->idct_mcoeffs > 0.0)
fprintf(stderr, "Inverse DCT: %f Mcoefficients/sec\n",
((j_decompress_ptr)cinfo)->master->idct_mcoeffs /
((j_decompress_ptr)cinfo)->master->idct_elapsed);
if (((j_decompress_ptr)cinfo)->master->merged_upsample_mpixels > 0.0)
fprintf(stderr, "Merged upsampling: %f Mpixels/sec\n",
((j_decompress_ptr)cinfo)->master->merged_upsample_mpixels /
((j_decompress_ptr)cinfo)->master->merged_upsample_elapsed);
if (((j_decompress_ptr)cinfo)->master->upsample_msamples > 0.0)
fprintf(stderr, "Upsampling: %f Msamples/sec\n",
((j_decompress_ptr)cinfo)->master->upsample_msamples /
((j_decompress_ptr)cinfo)->master->upsample_elapsed);
if (((j_decompress_ptr)cinfo)->master->cconvert_mpixels > 0.0)
fprintf(stderr, "Color deconversion: %f Mpixels/sec\n",
((j_decompress_ptr)cinfo)->master->cconvert_mpixels /
((j_decompress_ptr)cinfo)->master->cconvert_elapsed);
} else {
if (((j_compress_ptr)cinfo)->master->cconvert_mpixels > 0.0)
fprintf(stderr, "Color conversion: %f Mpixels/sec\n",
((j_compress_ptr)cinfo)->master->cconvert_mpixels /
((j_compress_ptr)cinfo)->master->cconvert_elapsed);
if (((j_compress_ptr)cinfo)->master->downsample_msamples > 0.0)
fprintf(stderr, "Downsampling: %f Msamples/sec\n",
((j_compress_ptr)cinfo)->master->downsample_msamples /
((j_compress_ptr)cinfo)->master->downsample_elapsed);
if (((j_compress_ptr)cinfo)->master->convsamp_msamples > 0.0)
fprintf(stderr, "Sample conversion: %f Msamples/sec\n",
((j_compress_ptr)cinfo)->master->convsamp_msamples /
((j_compress_ptr)cinfo)->master->convsamp_elapsed);
if (((j_compress_ptr)cinfo)->master->fdct_mcoeffs > 0.0)
fprintf(stderr, "Forward DCT: %f Mcoefficients/sec\n",
((j_compress_ptr)cinfo)->master->fdct_mcoeffs /
((j_compress_ptr)cinfo)->master->fdct_elapsed);
if (((j_compress_ptr)cinfo)->master->quantize_mcoeffs > 0.0)
fprintf(stderr, "Quantization: %f Mcoefficients/sec\n",
((j_compress_ptr)cinfo)->master->quantize_mcoeffs /
((j_compress_ptr)cinfo)->master->quantize_elapsed);
if (((j_compress_ptr)cinfo)->master->entropy_mcoeffs > 0.0)
fprintf(stderr, "Entropy encoding: %f Mcoefficients/sec\n",
((j_compress_ptr)cinfo)->master->entropy_mcoeffs /
((j_compress_ptr)cinfo)->master->entropy_elapsed);
}
#endif
/* We need only tell the memory manager to release everything. */
/* NB: mem pointer is NULL if memory mgr failed to initialize. */
if (cinfo->mem != NULL)
(*cinfo->mem->self_destruct) (cinfo);
cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */
cinfo->global_state = 0; /* mark it destroyed */
}
/*
* Convenience routines for allocating quantization and Huffman tables.
* (Would jutils.c be a more reasonable place to put these?)
*/
GLOBAL(JQUANT_TBL *)
jpeg_alloc_quant_table(j_common_ptr cinfo)
{
JQUANT_TBL *tbl;
tbl = (JQUANT_TBL *)
(*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JQUANT_TBL));
tbl->sent_table = FALSE; /* make sure this is false in any new table */
return tbl;
}
GLOBAL(JHUFF_TBL *)
jpeg_alloc_huff_table(j_common_ptr cinfo)
{
JHUFF_TBL *tbl;
tbl = (JHUFF_TBL *)
(*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JHUFF_TBL));
tbl->sent_table = FALSE; /* make sure this is false in any new table */
return tbl;
}