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