Path: blob/master/thirdparty/libjpeg-turbo/src/jcomapi.c
9904 views
/*1* jcomapi.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1997, Thomas G. Lane.5* libjpeg-turbo Modifications:6* Copyright (C) 2024, D. R. Commander.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains application interface routines that are used for both11* compression and decompression.12*/1314#define JPEG_INTERNALS15#include "jinclude.h"16#include "jpeglib.h"171819/*20* Abort processing of a JPEG compression or decompression operation,21* but don't destroy the object itself.22*23* For this, we merely clean up all the nonpermanent memory pools.24* Note that temp files (virtual arrays) are not allowed to belong to25* the permanent pool, so we will be able to close all temp files here.26* Closing a data source or destination, if necessary, is the application's27* responsibility.28*/2930GLOBAL(void)31jpeg_abort(j_common_ptr cinfo)32{33int pool;3435/* Do nothing if called on a not-initialized or destroyed JPEG object. */36if (cinfo->mem == NULL)37return;3839/* Releasing pools in reverse order might help avoid fragmentation40* with some (brain-damaged) malloc libraries.41*/42for (pool = JPOOL_NUMPOOLS - 1; pool > JPOOL_PERMANENT; pool--) {43(*cinfo->mem->free_pool) (cinfo, pool);44}4546/* Reset overall state for possible reuse of object */47if (cinfo->is_decompressor) {48cinfo->global_state = DSTATE_START;49/* Try to keep application from accessing now-deleted marker list.50* A bit kludgy to do it here, but this is the most central place.51*/52((j_decompress_ptr)cinfo)->marker_list = NULL;53((j_decompress_ptr)cinfo)->master->marker_list_end = NULL;54} else {55cinfo->global_state = CSTATE_START;56}57}585960/*61* Destruction of a JPEG object.62*63* Everything gets deallocated except the master jpeg_compress_struct itself64* and the error manager struct. Both of these are supplied by the application65* and must be freed, if necessary, by the application. (Often they are on66* the stack and so don't need to be freed anyway.)67* Closing a data source or destination, if necessary, is the application's68* responsibility.69*/7071GLOBAL(void)72jpeg_destroy(j_common_ptr cinfo)73{74/* We need only tell the memory manager to release everything. */75/* NB: mem pointer is NULL if memory mgr failed to initialize. */76if (cinfo->mem != NULL)77(*cinfo->mem->self_destruct) (cinfo);78cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */79cinfo->global_state = 0; /* mark it destroyed */80}818283/*84* Convenience routines for allocating quantization and Huffman tables.85* (Would jutils.c be a more reasonable place to put these?)86*/8788GLOBAL(JQUANT_TBL *)89jpeg_alloc_quant_table(j_common_ptr cinfo)90{91JQUANT_TBL *tbl;9293tbl = (JQUANT_TBL *)94(*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JQUANT_TBL));95tbl->sent_table = FALSE; /* make sure this is false in any new table */96return tbl;97}9899100GLOBAL(JHUFF_TBL *)101jpeg_alloc_huff_table(j_common_ptr cinfo)102{103JHUFF_TBL *tbl;104105tbl = (JHUFF_TBL *)106(*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JHUFF_TBL));107tbl->sent_table = FALSE; /* make sure this is false in any new table */108return tbl;109}110111112