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