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