Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jcomapi.c |
| 3 | * |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
| 5 | * Copyright (C) 1994-1997, Thomas G. Lane.0 |
| 6 | * It was modified by The libjpeg-turbo Project to include only code relevant |
| 7 | * to libjpeg-turbo. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 8 | * For conditions of distribution and use, see the accompanying README file. |
| 9 | * |
| 10 | * This file contains application interface routines that are used for both |
| 11 | * compression and decompression. |
| 12 | */ |
| 13 | |
| 14 | #define JPEG_INTERNALS |
| 15 | #include "jinclude.h" |
| 16 | #include "jpeglib.h" |
| 17 | |
| 18 | |
| 19 | /* |
| 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 to |
| 25 | * 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's |
| 27 | * responsibility. |
| 28 | */ |
| 29 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 30 | GLOBAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 31 | jpeg_abort (j_common_ptr cinfo) |
| 32 | { |
| 33 | int pool; |
| 34 | |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 35 | /* Do nothing if called on a not-initialized or destroyed JPEG object. */ |
| 36 | if (cinfo->mem == NULL) |
| 37 | return; |
| 38 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 39 | /* Releasing pools in reverse order might help avoid fragmentation |
| 40 | * with some (brain-damaged) malloc libraries. |
| 41 | */ |
| 42 | for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) { |
| 43 | (*cinfo->mem->free_pool) (cinfo, pool); |
| 44 | } |
| 45 | |
| 46 | /* Reset overall state for possible reuse of object */ |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 47 | if (cinfo->is_decompressor) { |
| 48 | cinfo->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 | } else { |
| 54 | cinfo->global_state = CSTATE_START; |
| 55 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | |
| 59 | /* |
| 60 | * Destruction of a JPEG object. |
| 61 | * |
| 62 | * Everything gets deallocated except the master jpeg_compress_struct itself |
| 63 | * and the error manager struct. Both of these are supplied by the application |
| 64 | * and must be freed, if necessary, by the application. (Often they are on |
| 65 | * the stack and so don't need to be freed anyway.) |
| 66 | * Closing a data source or destination, if necessary, is the application's |
| 67 | * responsibility. |
| 68 | */ |
| 69 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 70 | GLOBAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 71 | jpeg_destroy (j_common_ptr cinfo) |
| 72 | { |
| 73 | /* We need only tell the memory manager to release everything. */ |
| 74 | /* NB: mem pointer is NULL if memory mgr failed to initialize. */ |
| 75 | if (cinfo->mem != NULL) |
| 76 | (*cinfo->mem->self_destruct) (cinfo); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 77 | cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */ |
| 78 | cinfo->global_state = 0; /* mark it destroyed */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | * Convenience routines for allocating quantization and Huffman tables. |
| 84 | * (Would jutils.c be a more reasonable place to put these?) |
| 85 | */ |
| 86 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 87 | GLOBAL(JQUANT_TBL *) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 88 | jpeg_alloc_quant_table (j_common_ptr cinfo) |
| 89 | { |
| 90 | JQUANT_TBL *tbl; |
| 91 | |
| 92 | tbl = (JQUANT_TBL *) |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 93 | (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JQUANT_TBL)); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 94 | tbl->sent_table = FALSE; /* make sure this is false in any new table */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 95 | return tbl; |
| 96 | } |
| 97 | |
| 98 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 99 | GLOBAL(JHUFF_TBL *) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 100 | jpeg_alloc_huff_table (j_common_ptr cinfo) |
| 101 | { |
| 102 | JHUFF_TBL *tbl; |
| 103 | |
| 104 | tbl = (JHUFF_TBL *) |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 105 | (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JHUFF_TBL)); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 106 | tbl->sent_table = FALSE; /* make sure this is false in any new table */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 107 | return tbl; |
| 108 | } |