/src/libjpeg-turbo.dev/src/jcomapi.c
Line | Count | Source |
1 | | /* |
2 | | * jcomapi.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1994-1997, Thomas G. Lane. |
6 | | * libjpeg-turbo Modifications: |
7 | | * Copyright (C) 2024-2025, D. R. Commander. |
8 | | * For conditions of distribution and use, see the accompanying README.ijg |
9 | | * file. |
10 | | * |
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 | | #ifdef WITH_PROFILE |
19 | | #include <stdio.h> |
20 | | #endif |
21 | | |
22 | | |
23 | | /* |
24 | | * Abort processing of a JPEG compression or decompression operation, |
25 | | * but don't destroy the object itself. |
26 | | * |
27 | | * For this, we merely clean up all the nonpermanent memory pools. |
28 | | * Note that temp files (virtual arrays) are not allowed to belong to |
29 | | * the permanent pool, so we will be able to close all temp files here. |
30 | | * Closing a data source or destination, if necessary, is the application's |
31 | | * responsibility. |
32 | | */ |
33 | | |
34 | | GLOBAL(void) |
35 | | jpeg_abort(j_common_ptr cinfo) |
36 | 14.6k | { |
37 | 14.6k | int pool; |
38 | | |
39 | | /* Do nothing if called on a not-initialized or destroyed JPEG object. */ |
40 | 14.6k | if (cinfo->mem == NULL) |
41 | 0 | return; |
42 | | |
43 | | /* Releasing pools in reverse order might help avoid fragmentation |
44 | | * with some (brain-damaged) malloc libraries. |
45 | | */ |
46 | 29.3k | for (pool = JPOOL_NUMPOOLS - 1; pool > JPOOL_PERMANENT; pool--) { |
47 | 14.6k | (*cinfo->mem->free_pool) (cinfo, pool); |
48 | 14.6k | } |
49 | | |
50 | | /* Reset overall state for possible reuse of object */ |
51 | 14.6k | if (cinfo->is_decompressor) { |
52 | 14.6k | cinfo->global_state = DSTATE_START; |
53 | | /* Try to keep application from accessing now-deleted marker list. |
54 | | * A bit kludgy to do it here, but this is the most central place. |
55 | | */ |
56 | 14.6k | ((j_decompress_ptr)cinfo)->marker_list = NULL; |
57 | 14.6k | ((j_decompress_ptr)cinfo)->master->marker_list_end = NULL; |
58 | 14.6k | } else { |
59 | 0 | cinfo->global_state = CSTATE_START; |
60 | 0 | } |
61 | 14.6k | } |
62 | | |
63 | | |
64 | | /* |
65 | | * Destruction of a JPEG object. |
66 | | * |
67 | | * Everything gets deallocated except the master jpeg_compress_struct itself |
68 | | * and the error manager struct. Both of these are supplied by the application |
69 | | * and must be freed, if necessary, by the application. (Often they are on |
70 | | * the stack and so don't need to be freed anyway.) |
71 | | * Closing a data source or destination, if necessary, is the application's |
72 | | * responsibility. |
73 | | */ |
74 | | |
75 | | GLOBAL(void) |
76 | | jpeg_destroy(j_common_ptr cinfo) |
77 | 6.98k | { |
78 | | #ifdef WITH_PROFILE |
79 | | if (cinfo->is_decompressor) { |
80 | | if (((j_decompress_ptr)cinfo)->master->idct_mcoeffs > 0.0) |
81 | | fprintf(stderr, "Inverse DCT: %f Mcoefficients/sec\n", |
82 | | ((j_decompress_ptr)cinfo)->master->idct_mcoeffs / |
83 | | ((j_decompress_ptr)cinfo)->master->idct_elapsed); |
84 | | if (((j_decompress_ptr)cinfo)->master->merged_upsample_mpixels > 0.0) |
85 | | fprintf(stderr, "Merged upsampling: %f Mpixels/sec\n", |
86 | | ((j_decompress_ptr)cinfo)->master->merged_upsample_mpixels / |
87 | | ((j_decompress_ptr)cinfo)->master->merged_upsample_elapsed); |
88 | | if (((j_decompress_ptr)cinfo)->master->upsample_msamples > 0.0) |
89 | | fprintf(stderr, "Upsampling: %f Msamples/sec\n", |
90 | | ((j_decompress_ptr)cinfo)->master->upsample_msamples / |
91 | | ((j_decompress_ptr)cinfo)->master->upsample_elapsed); |
92 | | if (((j_decompress_ptr)cinfo)->master->cconvert_mpixels > 0.0) |
93 | | fprintf(stderr, "Color deconversion: %f Mpixels/sec\n", |
94 | | ((j_decompress_ptr)cinfo)->master->cconvert_mpixels / |
95 | | ((j_decompress_ptr)cinfo)->master->cconvert_elapsed); |
96 | | } else { |
97 | | if (((j_compress_ptr)cinfo)->master->cconvert_mpixels > 0.0) |
98 | | fprintf(stderr, "Color conversion: %f Mpixels/sec\n", |
99 | | ((j_compress_ptr)cinfo)->master->cconvert_mpixels / |
100 | | ((j_compress_ptr)cinfo)->master->cconvert_elapsed); |
101 | | if (((j_compress_ptr)cinfo)->master->downsample_msamples > 0.0) |
102 | | fprintf(stderr, "Downsampling: %f Msamples/sec\n", |
103 | | ((j_compress_ptr)cinfo)->master->downsample_msamples / |
104 | | ((j_compress_ptr)cinfo)->master->downsample_elapsed); |
105 | | if (((j_compress_ptr)cinfo)->master->convsamp_msamples > 0.0) |
106 | | fprintf(stderr, "Sample conversion: %f Msamples/sec\n", |
107 | | ((j_compress_ptr)cinfo)->master->convsamp_msamples / |
108 | | ((j_compress_ptr)cinfo)->master->convsamp_elapsed); |
109 | | if (((j_compress_ptr)cinfo)->master->fdct_mcoeffs > 0.0) |
110 | | fprintf(stderr, "Forward DCT: %f Mcoefficients/sec\n", |
111 | | ((j_compress_ptr)cinfo)->master->fdct_mcoeffs / |
112 | | ((j_compress_ptr)cinfo)->master->fdct_elapsed); |
113 | | if (((j_compress_ptr)cinfo)->master->quantize_mcoeffs > 0.0) |
114 | | fprintf(stderr, "Quantization: %f Mcoefficients/sec\n", |
115 | | ((j_compress_ptr)cinfo)->master->quantize_mcoeffs / |
116 | | ((j_compress_ptr)cinfo)->master->quantize_elapsed); |
117 | | if (((j_compress_ptr)cinfo)->master->entropy_mcoeffs > 0.0) |
118 | | fprintf(stderr, "Entropy encoding: %f Mcoefficients/sec\n", |
119 | | ((j_compress_ptr)cinfo)->master->entropy_mcoeffs / |
120 | | ((j_compress_ptr)cinfo)->master->entropy_elapsed); |
121 | | } |
122 | | #endif |
123 | | |
124 | | /* We need only tell the memory manager to release everything. */ |
125 | | /* NB: mem pointer is NULL if memory mgr failed to initialize. */ |
126 | 6.98k | if (cinfo->mem != NULL) |
127 | 6.98k | (*cinfo->mem->self_destruct) (cinfo); |
128 | 6.98k | cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */ |
129 | 6.98k | cinfo->global_state = 0; /* mark it destroyed */ |
130 | 6.98k | } |
131 | | |
132 | | |
133 | | /* |
134 | | * Convenience routines for allocating quantization and Huffman tables. |
135 | | * (Would jutils.c be a more reasonable place to put these?) |
136 | | */ |
137 | | |
138 | | GLOBAL(JQUANT_TBL *) |
139 | | jpeg_alloc_quant_table(j_common_ptr cinfo) |
140 | 4.04k | { |
141 | 4.04k | JQUANT_TBL *tbl; |
142 | | |
143 | 4.04k | tbl = (JQUANT_TBL *) |
144 | 4.04k | (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JQUANT_TBL)); |
145 | 4.04k | tbl->sent_table = FALSE; /* make sure this is false in any new table */ |
146 | 4.04k | return tbl; |
147 | 4.04k | } |
148 | | |
149 | | |
150 | | GLOBAL(JHUFF_TBL *) |
151 | | jpeg_alloc_huff_table(j_common_ptr cinfo) |
152 | 6.70k | { |
153 | 6.70k | JHUFF_TBL *tbl; |
154 | | |
155 | 6.70k | tbl = (JHUFF_TBL *) |
156 | 6.70k | (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JHUFF_TBL)); |
157 | 6.70k | tbl->sent_table = FALSE; /* make sure this is false in any new table */ |
158 | 6.70k | return tbl; |
159 | 6.70k | } |