blob: efbb8357b0c6ee3329d761f07d83de830ae9642f [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * jcomapi.c
3 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -04004 * This file was part of the Independent JPEG Group's software:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00005 * Copyright (C) 1994-1997, Thomas G. Lane.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04006 * It was modified by The libjpeg-turbo Project to include only code relevant
7 * to libjpeg-turbo.
8 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000010 *
11 * This file contains application interface routines that are used for both
12 * compression and decompression.
13 */
14
15#define JPEG_INTERNALS
16#include "jinclude.h"
17#include "jpeglib.h"
18
19
20/*
21 * Abort processing of a JPEG compression or decompression operation,
22 * but don't destroy the object itself.
23 *
24 * For this, we merely clean up all the nonpermanent memory pools.
25 * Note that temp files (virtual arrays) are not allowed to belong to
26 * the permanent pool, so we will be able to close all temp files here.
27 * Closing a data source or destination, if necessary, is the application's
28 * responsibility.
29 */
30
31GLOBAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -080032jpeg_abort(j_common_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000033{
34 int pool;
35
36 /* Do nothing if called on a not-initialized or destroyed JPEG object. */
37 if (cinfo->mem == NULL)
38 return;
39
40 /* Releasing pools in reverse order might help avoid fragmentation
41 * with some (brain-damaged) malloc libraries.
42 */
Chris Blumecca8c4d2019-03-01 01:09:50 -080043 for (pool = JPOOL_NUMPOOLS - 1; pool > JPOOL_PERMANENT; pool--) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000044 (*cinfo->mem->free_pool) (cinfo, pool);
45 }
46
47 /* Reset overall state for possible reuse of object */
48 if (cinfo->is_decompressor) {
49 cinfo->global_state = DSTATE_START;
50 /* Try to keep application from accessing now-deleted marker list.
51 * A bit kludgy to do it here, but this is the most central place.
52 */
Chris Blumecca8c4d2019-03-01 01:09:50 -080053 ((j_decompress_ptr)cinfo)->marker_list = NULL;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000054 } else {
55 cinfo->global_state = CSTATE_START;
56 }
57}
58
59
60/*
61 * Destruction of a JPEG object.
62 *
63 * Everything gets deallocated except the master jpeg_compress_struct itself
64 * and the error manager struct. Both of these are supplied by the application
65 * and must be freed, if necessary, by the application. (Often they are on
66 * the stack and so don't need to be freed anyway.)
67 * Closing a data source or destination, if necessary, is the application's
68 * responsibility.
69 */
70
71GLOBAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -080072jpeg_destroy(j_common_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000073{
74 /* We need only tell the memory manager to release everything. */
75 /* NB: mem pointer is NULL if memory mgr failed to initialize. */
76 if (cinfo->mem != NULL)
77 (*cinfo->mem->self_destruct) (cinfo);
Tom Hudson0d47d2d2016-05-04 13:22:56 -040078 cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */
79 cinfo->global_state = 0; /* mark it destroyed */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000080}
81
82
83/*
84 * Convenience routines for allocating quantization and Huffman tables.
85 * (Would jutils.c be a more reasonable place to put these?)
86 */
87
88GLOBAL(JQUANT_TBL *)
Chris Blumecca8c4d2019-03-01 01:09:50 -080089jpeg_alloc_quant_table(j_common_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000090{
91 JQUANT_TBL *tbl;
92
93 tbl = (JQUANT_TBL *)
Tom Hudson0d47d2d2016-05-04 13:22:56 -040094 (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JQUANT_TBL));
95 tbl->sent_table = FALSE; /* make sure this is false in any new table */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000096 return tbl;
97}
98
99
100GLOBAL(JHUFF_TBL *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800101jpeg_alloc_huff_table(j_common_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000102{
103 JHUFF_TBL *tbl;
104
105 tbl = (JHUFF_TBL *)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400106 (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JHUFF_TBL));
107 tbl->sent_table = FALSE; /* make sure this is false in any new table */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000108 return tbl;
109}