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