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