michael@0: /* michael@0: * jcomapi.c michael@0: * michael@0: * Copyright (C) 1994-1997, Thomas G. Lane. michael@0: * This file is part of the Independent JPEG Group's software. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains application interface routines that are used for both michael@0: * compression and decompression. michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: michael@0: michael@0: /* michael@0: * Abort processing of a JPEG compression or decompression operation, michael@0: * but don't destroy the object itself. michael@0: * michael@0: * For this, we merely clean up all the nonpermanent memory pools. michael@0: * Note that temp files (virtual arrays) are not allowed to belong to michael@0: * the permanent pool, so we will be able to close all temp files here. michael@0: * Closing a data source or destination, if necessary, is the application's michael@0: * responsibility. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jpeg_abort (j_common_ptr cinfo) michael@0: { michael@0: int pool; michael@0: michael@0: /* Do nothing if called on a not-initialized or destroyed JPEG object. */ michael@0: if (cinfo->mem == NULL) michael@0: return; michael@0: michael@0: /* Releasing pools in reverse order might help avoid fragmentation michael@0: * with some (brain-damaged) malloc libraries. michael@0: */ michael@0: for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) { michael@0: (*cinfo->mem->free_pool) (cinfo, pool); michael@0: } michael@0: michael@0: /* Reset overall state for possible reuse of object */ michael@0: if (cinfo->is_decompressor) { michael@0: cinfo->global_state = DSTATE_START; michael@0: /* Try to keep application from accessing now-deleted marker list. michael@0: * A bit kludgy to do it here, but this is the most central place. michael@0: */ michael@0: ((j_decompress_ptr) cinfo)->marker_list = NULL; michael@0: } else { michael@0: cinfo->global_state = CSTATE_START; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Destruction of a JPEG object. michael@0: * michael@0: * Everything gets deallocated except the master jpeg_compress_struct itself michael@0: * and the error manager struct. Both of these are supplied by the application michael@0: * and must be freed, if necessary, by the application. (Often they are on michael@0: * the stack and so don't need to be freed anyway.) michael@0: * Closing a data source or destination, if necessary, is the application's michael@0: * responsibility. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jpeg_destroy (j_common_ptr cinfo) michael@0: { michael@0: /* We need only tell the memory manager to release everything. */ michael@0: /* NB: mem pointer is NULL if memory mgr failed to initialize. */ michael@0: if (cinfo->mem != NULL) michael@0: (*cinfo->mem->self_destruct) (cinfo); michael@0: cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */ michael@0: cinfo->global_state = 0; /* mark it destroyed */ michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Convenience routines for allocating quantization and Huffman tables. michael@0: * (Would jutils.c be a more reasonable place to put these?) michael@0: */ michael@0: michael@0: GLOBAL(JQUANT_TBL *) michael@0: jpeg_alloc_quant_table (j_common_ptr cinfo) michael@0: { michael@0: JQUANT_TBL *tbl; michael@0: michael@0: tbl = (JQUANT_TBL *) michael@0: (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL)); michael@0: tbl->sent_table = FALSE; /* make sure this is false in any new table */ michael@0: return tbl; michael@0: } michael@0: michael@0: michael@0: GLOBAL(JHUFF_TBL *) michael@0: jpeg_alloc_huff_table (j_common_ptr cinfo) michael@0: { michael@0: JHUFF_TBL *tbl; michael@0: michael@0: tbl = (JHUFF_TBL *) michael@0: (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL)); michael@0: tbl->sent_table = FALSE; /* make sure this is false in any new table */ michael@0: return tbl; michael@0: }