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