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