/src/libjpeg-turbo.main/jcapimin.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * jcapimin.c  | 
3  |  |  *  | 
4  |  |  * This file was part of the Independent JPEG Group's software:  | 
5  |  |  * Copyright (C) 1994-1998, Thomas G. Lane.  | 
6  |  |  * Modified 2003-2010 by Guido Vollbeding.  | 
7  |  |  * libjpeg-turbo Modifications:  | 
8  |  |  * Copyright (C) 2022, D. R. Commander.  | 
9  |  |  * For conditions of distribution and use, see the accompanying README.ijg  | 
10  |  |  * file.  | 
11  |  |  *  | 
12  |  |  * This file contains application interface code for the compression half  | 
13  |  |  * of the JPEG library.  These are the "minimum" API routines that may be  | 
14  |  |  * needed in either the normal full-compression case or the transcoding-only  | 
15  |  |  * case.  | 
16  |  |  *  | 
17  |  |  * Most of the routines intended to be called directly by an application  | 
18  |  |  * are in this file or in jcapistd.c.  But also see jcparam.c for  | 
19  |  |  * parameter-setup helper routines, jcomapi.c for routines shared by  | 
20  |  |  * compression and decompression, and jctrans.c for the transcoding case.  | 
21  |  |  */  | 
22  |  |  | 
23  |  | #define JPEG_INTERNALS  | 
24  |  | #include "jinclude.h"  | 
25  |  | #include "jpeglib.h"  | 
26  |  | #include "jcmaster.h"  | 
27  |  |  | 
28  |  |  | 
29  |  | /*  | 
30  |  |  * Initialization of a JPEG compression object.  | 
31  |  |  * The error manager must already be set up (in case memory manager fails).  | 
32  |  |  */  | 
33  |  |  | 
34  |  | GLOBAL(void)  | 
35  |  | jpeg_CreateCompress(j_compress_ptr cinfo, int version, size_t structsize)  | 
36  | 5.51k  | { | 
37  | 5.51k  |   int i;  | 
38  |  |  | 
39  |  |   /* Guard against version mismatches between library and caller. */  | 
40  | 5.51k  |   cinfo->mem = NULL;            /* so jpeg_destroy knows mem mgr not called */  | 
41  | 5.51k  |   if (version != JPEG_LIB_VERSION)  | 
42  | 0  |     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);  | 
43  | 5.51k  |   if (structsize != sizeof(struct jpeg_compress_struct))  | 
44  | 0  |     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,  | 
45  | 5.51k  |              (int)sizeof(struct jpeg_compress_struct), (int)structsize);  | 
46  |  |  | 
47  |  |   /* For debugging purposes, we zero the whole master structure.  | 
48  |  |    * But the application has already set the err pointer, and may have set  | 
49  |  |    * client_data, so we have to save and restore those fields.  | 
50  |  |    * Note: if application hasn't set client_data, tools like Purify may  | 
51  |  |    * complain here.  | 
52  |  |    */  | 
53  | 5.51k  |   { | 
54  | 5.51k  |     struct jpeg_error_mgr *err = cinfo->err;  | 
55  | 5.51k  |     void *client_data = cinfo->client_data; /* ignore Purify complaint here */  | 
56  | 5.51k  |     memset(cinfo, 0, sizeof(struct jpeg_compress_struct));  | 
57  | 5.51k  |     cinfo->err = err;  | 
58  | 5.51k  |     cinfo->client_data = client_data;  | 
59  | 5.51k  |   }  | 
60  | 5.51k  |   cinfo->is_decompressor = FALSE;  | 
61  |  |  | 
62  |  |   /* Initialize a memory manager instance for this object */  | 
63  | 5.51k  |   jinit_memory_mgr((j_common_ptr)cinfo);  | 
64  |  |  | 
65  |  |   /* Zero out pointers to permanent structures. */  | 
66  | 5.51k  |   cinfo->progress = NULL;  | 
67  | 5.51k  |   cinfo->dest = NULL;  | 
68  |  |  | 
69  | 5.51k  |   cinfo->comp_info = NULL;  | 
70  |  |  | 
71  | 27.5k  |   for (i = 0; i < NUM_QUANT_TBLS; i++) { | 
72  | 22.0k  |     cinfo->quant_tbl_ptrs[i] = NULL;  | 
73  |  | #if JPEG_LIB_VERSION >= 70  | 
74  |  |     cinfo->q_scale_factor[i] = 100;  | 
75  |  | #endif  | 
76  | 22.0k  |   }  | 
77  |  |  | 
78  | 27.5k  |   for (i = 0; i < NUM_HUFF_TBLS; i++) { | 
79  | 22.0k  |     cinfo->dc_huff_tbl_ptrs[i] = NULL;  | 
80  | 22.0k  |     cinfo->ac_huff_tbl_ptrs[i] = NULL;  | 
81  | 22.0k  |   }  | 
82  |  |  | 
83  |  | #if JPEG_LIB_VERSION >= 80  | 
84  |  |   /* Must do it here for emit_dqt in case jpeg_write_tables is used */  | 
85  |  |   cinfo->block_size = DCTSIZE;  | 
86  |  |   cinfo->natural_order = jpeg_natural_order;  | 
87  |  |   cinfo->lim_Se = DCTSIZE2 - 1;  | 
88  |  | #endif  | 
89  |  |  | 
90  | 5.51k  |   cinfo->script_space = NULL;  | 
91  |  |  | 
92  | 5.51k  |   cinfo->input_gamma = 1.0;     /* in case application forgets */  | 
93  |  |  | 
94  | 5.51k  |   cinfo->data_precision = BITS_IN_JSAMPLE;  | 
95  |  |  | 
96  |  |   /* OK, I'm ready */  | 
97  | 5.51k  |   cinfo->global_state = CSTATE_START;  | 
98  |  |  | 
99  |  |   /* The master struct is used to store extension parameters, so we allocate it  | 
100  |  |    * here.  | 
101  |  |    */  | 
102  | 5.51k  |   cinfo->master = (struct jpeg_comp_master *)  | 
103  | 5.51k  |       (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,  | 
104  | 5.51k  |                                   sizeof(my_comp_master));  | 
105  | 5.51k  |   memset(cinfo->master, 0, sizeof(my_comp_master));  | 
106  | 5.51k  | }  | 
107  |  |  | 
108  |  |  | 
109  |  | /*  | 
110  |  |  * Destruction of a JPEG compression object  | 
111  |  |  */  | 
112  |  |  | 
113  |  | GLOBAL(void)  | 
114  |  | jpeg_destroy_compress(j_compress_ptr cinfo)  | 
115  | 5.51k  | { | 
116  | 5.51k  |   jpeg_destroy((j_common_ptr)cinfo); /* use common routine */  | 
117  | 5.51k  | }  | 
118  |  |  | 
119  |  |  | 
120  |  | /*  | 
121  |  |  * Abort processing of a JPEG compression operation,  | 
122  |  |  * but don't destroy the object itself.  | 
123  |  |  */  | 
124  |  |  | 
125  |  | GLOBAL(void)  | 
126  |  | jpeg_abort_compress(j_compress_ptr cinfo)  | 
127  | 274  | { | 
128  | 274  |   jpeg_abort((j_common_ptr)cinfo); /* use common routine */  | 
129  | 274  | }  | 
130  |  |  | 
131  |  |  | 
132  |  | /*  | 
133  |  |  * Forcibly suppress or un-suppress all quantization and Huffman tables.  | 
134  |  |  * Marks all currently defined tables as already written (if suppress)  | 
135  |  |  * or not written (if !suppress).  This will control whether they get emitted  | 
136  |  |  * by a subsequent jpeg_start_compress call.  | 
137  |  |  *  | 
138  |  |  * This routine is exported for use by applications that want to produce  | 
139  |  |  * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but  | 
140  |  |  * since it is called by jpeg_start_compress, we put it here --- otherwise  | 
141  |  |  * jcparam.o would be linked whether the application used it or not.  | 
142  |  |  */  | 
143  |  |  | 
144  |  | GLOBAL(void)  | 
145  |  | jpeg_suppress_tables(j_compress_ptr cinfo, boolean suppress)  | 
146  | 7.03k  | { | 
147  | 7.03k  |   int i;  | 
148  | 7.03k  |   JQUANT_TBL *qtbl;  | 
149  | 7.03k  |   JHUFF_TBL *htbl;  | 
150  |  |  | 
151  | 35.1k  |   for (i = 0; i < NUM_QUANT_TBLS; i++) { | 
152  | 28.1k  |     if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)  | 
153  | 14.1k  |       qtbl->sent_table = suppress;  | 
154  | 28.1k  |   }  | 
155  |  |  | 
156  | 35.1k  |   for (i = 0; i < NUM_HUFF_TBLS; i++) { | 
157  | 28.1k  |     if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)  | 
158  | 14.0k  |       htbl->sent_table = suppress;  | 
159  | 28.1k  |     if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)  | 
160  | 14.0k  |       htbl->sent_table = suppress;  | 
161  | 28.1k  |   }  | 
162  | 7.03k  | }  | 
163  |  |  | 
164  |  |  | 
165  |  | /*  | 
166  |  |  * Finish JPEG compression.  | 
167  |  |  *  | 
168  |  |  * If a multipass operating mode was selected, this may do a great deal of  | 
169  |  |  * work including most of the actual output.  | 
170  |  |  */  | 
171  |  |  | 
172  |  | GLOBAL(void)  | 
173  |  | jpeg_finish_compress(j_compress_ptr cinfo)  | 
174  | 7.03k  | { | 
175  | 7.03k  |   JDIMENSION iMCU_row;  | 
176  |  |  | 
177  | 7.03k  |   if (cinfo->global_state == CSTATE_SCANNING ||  | 
178  | 7.03k  |       cinfo->global_state == CSTATE_RAW_OK) { | 
179  |  |     /* Terminate first pass */  | 
180  | 0  |     if (cinfo->next_scanline < cinfo->image_height)  | 
181  | 0  |       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);  | 
182  | 0  |     (*cinfo->master->finish_pass) (cinfo);  | 
183  | 7.03k  |   } else if (cinfo->global_state != CSTATE_WRCOEFS)  | 
184  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
185  |  |   /* Perform any remaining passes */  | 
186  | 60.2k  |   while (!cinfo->master->is_last_pass) { | 
187  | 53.2k  |     (*cinfo->master->prepare_for_pass) (cinfo);  | 
188  | 7.80M  |     for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) { | 
189  | 7.75M  |       if (cinfo->progress != NULL) { | 
190  | 0  |         cinfo->progress->pass_counter = (long)iMCU_row;  | 
191  | 0  |         cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows;  | 
192  | 0  |         (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);  | 
193  | 0  |       }  | 
194  |  |       /* We bypass the main controller and invoke coef controller directly;  | 
195  |  |        * all work is being done from the coefficient buffer.  | 
196  |  |        */  | 
197  | 7.75M  |       if (cinfo->data_precision == 16) { | 
198  | 0  | #ifdef C_LOSSLESS_SUPPORTED  | 
199  | 0  |         if (!(*cinfo->coef->compress_data_16) (cinfo, (J16SAMPIMAGE)NULL))  | 
200  | 0  |           ERREXIT(cinfo, JERR_CANT_SUSPEND);  | 
201  |  | #else  | 
202  |  |         ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);  | 
203  |  | #endif  | 
204  | 7.75M  |       } else if (cinfo->data_precision == 12) { | 
205  | 1.24M  |         if (!(*cinfo->coef->compress_data_12) (cinfo, (J12SAMPIMAGE)NULL))  | 
206  | 0  |           ERREXIT(cinfo, JERR_CANT_SUSPEND);  | 
207  | 6.50M  |       } else { | 
208  | 6.50M  |         if (!(*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE)NULL))  | 
209  | 0  |           ERREXIT(cinfo, JERR_CANT_SUSPEND);  | 
210  | 6.50M  |       }  | 
211  | 7.75M  |     }  | 
212  | 53.2k  |     (*cinfo->master->finish_pass) (cinfo);  | 
213  | 53.2k  |   }  | 
214  |  |   /* Write EOI, do final cleanup */  | 
215  | 7.03k  |   (*cinfo->marker->write_file_trailer) (cinfo);  | 
216  | 7.03k  |   (*cinfo->dest->term_destination) (cinfo);  | 
217  |  |   /* We can use jpeg_abort to release memory and reset global_state */  | 
218  | 7.03k  |   jpeg_abort((j_common_ptr)cinfo);  | 
219  | 7.03k  | }  | 
220  |  |  | 
221  |  |  | 
222  |  | /*  | 
223  |  |  * Write a special marker.  | 
224  |  |  * This is only recommended for writing COM or APPn markers.  | 
225  |  |  * Must be called after jpeg_start_compress() and before  | 
226  |  |  * first call to jpeg_write_scanlines() or jpeg_write_raw_data().  | 
227  |  |  */  | 
228  |  |  | 
229  |  | GLOBAL(void)  | 
230  |  | jpeg_write_marker(j_compress_ptr cinfo, int marker, const JOCTET *dataptr,  | 
231  |  |                   unsigned int datalen)  | 
232  | 292k  | { | 
233  | 292k  |   void (*write_marker_byte) (j_compress_ptr info, int val);  | 
234  |  |  | 
235  | 292k  |   if (cinfo->next_scanline != 0 ||  | 
236  | 292k  |       (cinfo->global_state != CSTATE_SCANNING &&  | 
237  | 292k  |        cinfo->global_state != CSTATE_RAW_OK &&  | 
238  | 292k  |        cinfo->global_state != CSTATE_WRCOEFS))  | 
239  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
240  |  |  | 
241  | 292k  |   (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);  | 
242  | 292k  |   write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */  | 
243  | 19.3M  |   while (datalen--) { | 
244  | 19.0M  |     (*write_marker_byte) (cinfo, *dataptr);  | 
245  | 19.0M  |     dataptr++;  | 
246  | 19.0M  |   }  | 
247  | 292k  | }  | 
248  |  |  | 
249  |  | /* Same, but piecemeal. */  | 
250  |  |  | 
251  |  | GLOBAL(void)  | 
252  |  | jpeg_write_m_header(j_compress_ptr cinfo, int marker, unsigned int datalen)  | 
253  | 0  | { | 
254  | 0  |   if (cinfo->next_scanline != 0 ||  | 
255  | 0  |       (cinfo->global_state != CSTATE_SCANNING &&  | 
256  | 0  |        cinfo->global_state != CSTATE_RAW_OK &&  | 
257  | 0  |        cinfo->global_state != CSTATE_WRCOEFS))  | 
258  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
259  |  | 
  | 
260  | 0  |   (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);  | 
261  | 0  | }  | 
262  |  |  | 
263  |  | GLOBAL(void)  | 
264  |  | jpeg_write_m_byte(j_compress_ptr cinfo, int val)  | 
265  | 0  | { | 
266  | 0  |   (*cinfo->marker->write_marker_byte) (cinfo, val);  | 
267  | 0  | }  | 
268  |  |  | 
269  |  |  | 
270  |  | /*  | 
271  |  |  * Alternate compression function: just write an abbreviated table file.  | 
272  |  |  * Before calling this, all parameters and a data destination must be set up.  | 
273  |  |  *  | 
274  |  |  * To produce a pair of files containing abbreviated tables and abbreviated  | 
275  |  |  * image data, one would proceed as follows:  | 
276  |  |  *  | 
277  |  |  *              initialize JPEG object  | 
278  |  |  *              set JPEG parameters  | 
279  |  |  *              set destination to table file  | 
280  |  |  *              jpeg_write_tables(cinfo);  | 
281  |  |  *              set destination to image file  | 
282  |  |  *              jpeg_start_compress(cinfo, FALSE);  | 
283  |  |  *              write data...  | 
284  |  |  *              jpeg_finish_compress(cinfo);  | 
285  |  |  *  | 
286  |  |  * jpeg_write_tables has the side effect of marking all tables written  | 
287  |  |  * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress  | 
288  |  |  * will not re-emit the tables unless it is passed write_all_tables=TRUE.  | 
289  |  |  */  | 
290  |  |  | 
291  |  | GLOBAL(void)  | 
292  |  | jpeg_write_tables(j_compress_ptr cinfo)  | 
293  | 0  | { | 
294  | 0  |   if (cinfo->global_state != CSTATE_START)  | 
295  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
296  |  |  | 
297  |  |   /* (Re)initialize error mgr and destination modules */  | 
298  | 0  |   (*cinfo->err->reset_error_mgr) ((j_common_ptr)cinfo);  | 
299  | 0  |   (*cinfo->dest->init_destination) (cinfo);  | 
300  |  |   /* Initialize the marker writer ... bit of a crock to do it here. */  | 
301  | 0  |   jinit_marker_writer(cinfo);  | 
302  |  |   /* Write them tables! */  | 
303  | 0  |   (*cinfo->marker->write_tables_only) (cinfo);  | 
304  |  |   /* And clean up. */  | 
305  | 0  |   (*cinfo->dest->term_destination) (cinfo);  | 
306  |  |   /*  | 
307  |  |    * In library releases up through v6a, we called jpeg_abort() here to free  | 
308  |  |    * any working memory allocated by the destination manager and marker  | 
309  |  |    * writer.  Some applications had a problem with that: they allocated space  | 
310  |  |    * of their own from the library memory manager, and didn't want it to go  | 
311  |  |    * away during write_tables.  So now we do nothing.  This will cause a  | 
312  |  |    * memory leak if an app calls write_tables repeatedly without doing a full  | 
313  |  |    * compression cycle or otherwise resetting the JPEG object.  However, that  | 
314  |  |    * seems less bad than unexpectedly freeing memory in the normal case.  | 
315  |  |    * An app that prefers the old behavior can call jpeg_abort for itself after  | 
316  |  |    * each call to jpeg_write_tables().  | 
317  |  |    */  | 
318  | 0  | }  |