| 1 | /* | 
|---|
| 2 | * reserved comment block | 
|---|
| 3 | * DO NOT REMOVE OR ALTER! | 
|---|
| 4 | */ | 
|---|
| 5 | /* | 
|---|
| 6 | * jcomapi.c | 
|---|
| 7 | * | 
|---|
| 8 | * Copyright (C) 1994-1997, Thomas G. Lane. | 
|---|
| 9 | * This file is part of the Independent JPEG Group's software. | 
|---|
| 10 | * For conditions of distribution and use, see the accompanying README file. | 
|---|
| 11 | * | 
|---|
| 12 | * This file contains application interface routines that are used for both | 
|---|
| 13 | * compression and decompression. | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define JPEG_INTERNALS | 
|---|
| 17 | #include "jinclude.h" | 
|---|
| 18 | #include "jpeglib.h" | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | /* | 
|---|
| 22 | * Abort processing of a JPEG compression or decompression operation, | 
|---|
| 23 | * but don't destroy the object itself. | 
|---|
| 24 | * | 
|---|
| 25 | * For this, we merely clean up all the nonpermanent memory pools. | 
|---|
| 26 | * Note that temp files (virtual arrays) are not allowed to belong to | 
|---|
| 27 | * the permanent pool, so we will be able to close all temp files here. | 
|---|
| 28 | * Closing a data source or destination, if necessary, is the application's | 
|---|
| 29 | * responsibility. | 
|---|
| 30 | */ | 
|---|
| 31 |  | 
|---|
| 32 | GLOBAL(void) | 
|---|
| 33 | jpeg_abort (j_common_ptr cinfo) | 
|---|
| 34 | { | 
|---|
| 35 | int pool; | 
|---|
| 36 |  | 
|---|
| 37 | /* Do nothing if called on a not-initialized or destroyed JPEG object. */ | 
|---|
| 38 | if (cinfo->mem == NULL) | 
|---|
| 39 | return; | 
|---|
| 40 |  | 
|---|
| 41 | /* Releasing pools in reverse order might help avoid fragmentation | 
|---|
| 42 | * with some (brain-damaged) malloc libraries. | 
|---|
| 43 | */ | 
|---|
| 44 | for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) { | 
|---|
| 45 | (*cinfo->mem->free_pool) (cinfo, pool); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | /* Reset overall state for possible reuse of object */ | 
|---|
| 49 | if (cinfo->is_decompressor) { | 
|---|
| 50 | cinfo->global_state = DSTATE_START; | 
|---|
| 51 | /* Try to keep application from accessing now-deleted marker list. | 
|---|
| 52 | * A bit kludgy to do it here, but this is the most central place. | 
|---|
| 53 | */ | 
|---|
| 54 | ((j_decompress_ptr) cinfo)->marker_list = NULL; | 
|---|
| 55 | } else { | 
|---|
| 56 | cinfo->global_state = CSTATE_START; | 
|---|
| 57 | } | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | /* | 
|---|
| 62 | * Destruction of a JPEG object. | 
|---|
| 63 | * | 
|---|
| 64 | * Everything gets deallocated except the master jpeg_compress_struct itself | 
|---|
| 65 | * and the error manager struct.  Both of these are supplied by the application | 
|---|
| 66 | * and must be freed, if necessary, by the application.  (Often they are on | 
|---|
| 67 | * the stack and so don't need to be freed anyway.) | 
|---|
| 68 | * Closing a data source or destination, if necessary, is the application's | 
|---|
| 69 | * responsibility. | 
|---|
| 70 | */ | 
|---|
| 71 |  | 
|---|
| 72 | GLOBAL(void) | 
|---|
| 73 | jpeg_destroy (j_common_ptr cinfo) | 
|---|
| 74 | { | 
|---|
| 75 | /* We need only tell the memory manager to release everything. */ | 
|---|
| 76 | /* NB: mem pointer is NULL if memory mgr failed to initialize. */ | 
|---|
| 77 | if (cinfo->mem != NULL) | 
|---|
| 78 | (*cinfo->mem->self_destruct) (cinfo); | 
|---|
| 79 | cinfo->mem = NULL;            /* be safe if jpeg_destroy is called twice */ | 
|---|
| 80 | cinfo->global_state = 0;      /* mark it destroyed */ | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 84 | /* | 
|---|
| 85 | * Convenience routines for allocating quantization and Huffman tables. | 
|---|
| 86 | * (Would jutils.c be a more reasonable place to put these?) | 
|---|
| 87 | */ | 
|---|
| 88 |  | 
|---|
| 89 | GLOBAL(JQUANT_TBL *) | 
|---|
| 90 | jpeg_alloc_quant_table (j_common_ptr cinfo) | 
|---|
| 91 | { | 
|---|
| 92 | JQUANT_TBL *tbl; | 
|---|
| 93 |  | 
|---|
| 94 | tbl = (JQUANT_TBL *) | 
|---|
| 95 | (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL)); | 
|---|
| 96 | tbl->sent_table = FALSE;      /* make sure this is false in any new table */ | 
|---|
| 97 | return tbl; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | GLOBAL(JHUFF_TBL *) | 
|---|
| 102 | jpeg_alloc_huff_table (j_common_ptr cinfo) | 
|---|
| 103 | { | 
|---|
| 104 | JHUFF_TBL *tbl; | 
|---|
| 105 |  | 
|---|
| 106 | tbl = (JHUFF_TBL *) | 
|---|
| 107 | (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL)); | 
|---|
| 108 | tbl->sent_table = FALSE;      /* make sure this is false in any new table */ | 
|---|
| 109 | return tbl; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|