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