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.
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
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.
23 #define JPEG_INTERNALS
29 * Initialization of a JPEG compression object.
30 * The error manager must already be set up (in case memory manager fails).
34 jpeg_CreateCompress (j_compress_ptr cinfo
, int version
, size_t structsize
)
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
);
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
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
));
57 cinfo
->client_data
= client_data
;
59 cinfo
->is_decompressor
= FALSE
;
61 /* Initialize a memory manager instance for this object */
62 jinit_memory_mgr((j_common_ptr
) cinfo
);
64 /* Zero out pointers to permanent structures. */
65 cinfo
->progress
= NULL
;
68 cinfo
->comp_info
= NULL
;
70 for (i
= 0; i
< NUM_QUANT_TBLS
; i
++)
71 cinfo
->quant_tbl_ptrs
[i
] = NULL
;
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
;
78 cinfo
->script_space
= NULL
;
80 cinfo
->input_gamma
= 1.0; /* in case application forgets */
83 cinfo
->global_state
= CSTATE_START
;
88 * Destruction of a JPEG compression object
92 jpeg_destroy_compress (j_compress_ptr cinfo
)
94 jpeg_destroy((j_common_ptr
) cinfo
); /* use common routine */
99 * Abort processing of a JPEG compression operation,
100 * but don't destroy the object itself.
104 jpeg_abort_compress (j_compress_ptr cinfo
)
106 jpeg_abort((j_common_ptr
) cinfo
); /* use common routine */
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.
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.
123 jpeg_suppress_tables (j_compress_ptr cinfo
, boolean suppress
)
129 for (i
= 0; i
< NUM_QUANT_TBLS
; i
++) {
130 if ((qtbl
= cinfo
->quant_tbl_ptrs
[i
]) != NULL
)
131 qtbl
->sent_table
= suppress
;
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
;
144 * Finish JPEG compression.
146 * If a multipass operating mode was selected, this may do a great deal of
147 * work including most of the actual output.
151 jpeg_finish_compress (j_compress_ptr cinfo
)
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
);
172 /* We bypass the main controller and invoke coef controller directly;
173 * all work is being done from the coefficient buffer.
175 if (! (*cinfo
->codec
->compress_data
) (cinfo
, (JSAMPIMAGE
) NULL
))
176 ERREXIT(cinfo
, JERR_CANT_SUSPEND
);
178 (*cinfo
->master
->finish_pass
) (cinfo
);
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
);
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().
196 jpeg_write_marker (j_compress_ptr cinfo
, int marker
,
197 const JOCTET
*dataptr
, unsigned int datalen
)
199 JMETHOD(void, write_marker_byte
, (j_compress_ptr info
, int val
));
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
);
207 (*cinfo
->marker
->write_marker_header
) (cinfo
, marker
, datalen
);
208 write_marker_byte
= cinfo
->marker
->write_marker_byte
; /* copy for speed */
210 (*write_marker_byte
) (cinfo
, *dataptr
);
215 /* Same, but piecemeal. */
218 jpeg_write_m_header (j_compress_ptr cinfo
, int marker
, unsigned int datalen
)
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
);
226 (*cinfo
->marker
->write_marker_header
) (cinfo
, marker
, datalen
);
230 jpeg_write_m_byte (j_compress_ptr cinfo
, int val
)
232 (*cinfo
->marker
->write_marker_byte
) (cinfo
, val
);
237 * Alternate compression function: just write an abbreviated table file.
238 * Before calling this, all parameters and a data destination must be set up.
240 * To produce a pair of files containing abbreviated tables and abbreviated
241 * image data, one would proceed as follows:
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);
250 * jpeg_finish_compress(cinfo);
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.
258 jpeg_write_tables (j_compress_ptr cinfo
)
260 if (cinfo
->global_state
!= CSTATE_START
)
261 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
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
);
271 (*cinfo
->dest
->term_destination
) (cinfo
);
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().