4 * Copyright (C) 1995-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.
8 * This file contains library routines for transcoding compression,
9 * that is, writing raw DCT coefficient arrays to an output JPEG file.
10 * The routines in jcapimin.c will also be needed by a transcoder.
13 #define JPEG_INTERNALS
16 #include "jlossy.h" /* Private declarations for lossy codec */
19 /* Forward declarations */
20 LOCAL(void) transencode_master_selection
21 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
22 LOCAL(void) transencode_codec
23 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
24 LOCAL(void) transencode_coef_controller
25 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
29 * Compression initialization for writing raw-coefficient data.
30 * Before calling this, all parameters and a data destination must be set up.
31 * Call jpeg_finish_compress() to actually write the data.
33 * The number of passed virtual arrays must match cinfo->num_components.
34 * Note that the virtual arrays need not be filled or even realized at
35 * the time write_coefficients is called; indeed, if the virtual arrays
36 * were requested from this compression object's memory manager, they
37 * typically will be realized during this routine and filled afterwards.
41 jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
43 if (cinfo->global_state != CSTATE_START)
44 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
45 /* Mark all tables to be written */
46 jpeg_suppress_tables(cinfo, FALSE);
47 /* (Re)initialize error mgr and destination modules */
48 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
49 (*cinfo->dest->init_destination) (cinfo);
50 /* Perform master selection of active modules */
51 transencode_master_selection(cinfo, coef_arrays);
52 /* Wait for jpeg_finish_compress() call */
53 cinfo->next_scanline = 0; /* so jpeg_write_marker works */
54 cinfo->global_state = CSTATE_WRCOEFS;
59 * Initialize the compression object with default parameters,
60 * then copy from the source object all parameters needed for lossless
61 * transcoding. Parameters that can be varied without loss (such as
62 * scan script and Huffman optimization) are left in their default states.
66 jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
67 j_compress_ptr dstinfo)
69 JQUANT_TBL ** qtblptr;
70 jpeg_component_info *incomp, *outcomp;
71 JQUANT_TBL *c_quant, *slot_quant;
74 /* Safety check to ensure start_compress not called yet. */
75 if (dstinfo->global_state != CSTATE_START)
76 ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
77 /* Copy fundamental image dimensions */
78 dstinfo->image_width = srcinfo->image_width;
79 dstinfo->image_height = srcinfo->image_height;
80 dstinfo->input_components = srcinfo->num_components;
81 dstinfo->in_color_space = srcinfo->jpeg_color_space;
82 /* Initialize all parameters to default values */
83 jpeg_set_defaults(dstinfo);
84 /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
85 * Fix it to get the right header markers for the image colorspace.
87 jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
88 dstinfo->data_precision = srcinfo->data_precision;
89 dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
90 /* Copy the source's quantization tables. */
91 for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
92 if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
93 qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
95 *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
96 MEMCOPY((*qtblptr)->quantval,
97 srcinfo->quant_tbl_ptrs[tblno]->quantval,
98 SIZEOF((*qtblptr)->quantval));
99 (*qtblptr)->sent_table = FALSE;
102 /* Copy the source's per-component info.
103 * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
105 dstinfo->num_components = srcinfo->num_components;
106 if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
107 ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
109 for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
110 ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
111 outcomp->component_id = incomp->component_id;
112 outcomp->h_samp_factor = incomp->h_samp_factor;
113 outcomp->v_samp_factor = incomp->v_samp_factor;
114 outcomp->quant_tbl_no = incomp->quant_tbl_no;
115 /* Make sure saved quantization table for component matches the qtable
116 * slot. If not, the input file re-used this qtable slot.
117 * IJG encoder currently cannot duplicate this.
119 tblno = outcomp->quant_tbl_no;
120 if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
121 srcinfo->quant_tbl_ptrs[tblno] == NULL)
122 ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
123 slot_quant = srcinfo->quant_tbl_ptrs[tblno];
124 c_quant = incomp->quant_table;
125 if (c_quant != NULL) {
126 for (coefi = 0; coefi < DCTSIZE2; coefi++) {
127 if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
128 ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
131 /* Note: we do not copy the source's Huffman table assignments;
132 * instead we rely on jpeg_set_colorspace to have made a suitable choice.
135 /* Also copy JFIF version and resolution information, if available.
136 * Strictly speaking this isn't "critical" info, but it's nearly
137 * always appropriate to copy it if available. In particular,
138 * if the application chooses to copy JFIF 1.02 extension markers from
139 * the source file, we need to copy the version to make sure we don't
140 * emit a file that has 1.02 extensions but a claimed version of 1.01.
141 * We will *not*, however, copy version info from mislabeled "2.01" files.
143 if (srcinfo->saw_JFIF_marker) {
144 if (srcinfo->JFIF_major_version == 1) {
145 dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
146 dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
148 dstinfo->density_unit = srcinfo->density_unit;
149 dstinfo->X_density = srcinfo->X_density;
150 dstinfo->Y_density = srcinfo->Y_density;
156 * Master selection of compression modules for transcoding.
157 * This substitutes for jcinit.c's initialization of the full compressor.
161 transencode_master_selection (j_compress_ptr cinfo,
162 jvirt_barray_ptr * coef_arrays)
164 cinfo->data_unit = DCTSIZE;
165 /* Although we don't actually use input_components for transcoding,
166 * jcmaster.c's initial_setup will complain if input_components is 0.
168 cinfo->input_components = 1;
169 /* Initialize master control (includes parameter checking/processing) */
170 jinit_c_master_control(cinfo, TRUE /* transcode only */);
172 /* We need a special compression codec. */
173 transencode_codec(cinfo, coef_arrays);
175 jinit_marker_writer(cinfo);
177 /* We can now tell the memory manager to allocate virtual arrays. */
178 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
180 /* Write the datastream header (SOI, JFIF) immediately.
181 * Frame and scan headers are postponed till later.
182 * This lets application insert special markers after the SOI.
184 (*cinfo->marker->write_file_header) (cinfo);
189 * The rest of this file is a special implementation of the coefficient
190 * buffer controller. This is similar to jccoefct.c, but it handles only
191 * output from presupplied virtual arrays. Furthermore, we generate any
192 * dummy padding blocks on-the-fly rather than expecting them to be present
196 /* Private buffer controller object */
199 JDIMENSION iMCU_row_num; /* iMCU row # within image */
200 JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
201 int MCU_vert_offset; /* counts MCU rows within iMCU row */
202 int MCU_rows_per_iMCU_row; /* number of such rows needed */
204 /* Virtual block array for each component. */
205 jvirt_barray_ptr * whole_image;
207 /* Workspace for constructing dummy blocks at right/bottom edges. */
208 JBLOCKROW dummy_buffer[C_MAX_DATA_UNITS_IN_MCU];
211 typedef c_coef_controller * c_coef_ptr;
215 start_iMCU_row (j_compress_ptr cinfo)
216 /* Reset within-iMCU-row counters for a new row */
218 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
219 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
221 /* In an interleaved scan, an MCU row is the same as an iMCU row.
222 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
223 * But at the bottom of the image, process only what's left.
225 if (cinfo->comps_in_scan > 1) {
226 coef->MCU_rows_per_iMCU_row = 1;
228 if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
229 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
231 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
235 coef->MCU_vert_offset = 0;
240 * Initialize for a processing pass.
244 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
246 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
247 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
249 if (pass_mode != JBUF_CRANK_DEST)
250 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
252 coef->iMCU_row_num = 0;
253 start_iMCU_row(cinfo);
259 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
260 * per call, ie, v_samp_factor block rows for each component in the scan.
261 * The data is obtained from the virtual arrays and fed to the entropy coder.
262 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
264 * NB: input_buf is ignored; it is likely to be a NULL pointer.
268 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
270 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
271 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
272 JDIMENSION MCU_col_num; /* index of current MCU within row */
273 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
274 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
275 int blkn, ci, xindex, yindex, yoffset, blockcnt;
276 JDIMENSION start_col;
277 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
278 JBLOCKROW MCU_buffer[C_MAX_DATA_UNITS_IN_MCU];
279 JBLOCKROW buffer_ptr;
280 jpeg_component_info *compptr;
283 /* Align the virtual buffers for the components used in this scan. */
284 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
285 compptr = cinfo->cur_comp_info[ci];
286 buffer[ci] = (*cinfo->mem->access_virt_barray)
287 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
288 coef->iMCU_row_num * compptr->v_samp_factor,
289 (JDIMENSION) compptr->v_samp_factor, FALSE);
292 /* Loop to process one whole iMCU row */
293 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
295 for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
297 /* Construct list of pointers to DCT blocks belonging to this MCU */
298 blkn = 0; /* index of current DCT block within MCU */
299 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
300 compptr = cinfo->cur_comp_info[ci];
301 start_col = MCU_col_num * compptr->MCU_width;
302 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
303 : compptr->last_col_width;
304 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
305 if (coef->iMCU_row_num < last_iMCU_row ||
306 yindex+yoffset < compptr->last_row_height) {
307 /* Fill in pointers to real blocks in this row */
308 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
309 for (xindex = 0; xindex < blockcnt; xindex++)
310 MCU_buffer[blkn++] = buffer_ptr++;
312 /* At bottom of image, need a whole row of dummy blocks */
315 /* Fill in any dummy blocks needed in this row.
316 * Dummy blocks are filled in the same way as in jccoefct.c:
317 * all zeroes in the AC entries, DC entries equal to previous
318 * block's DC value. The init routine has already zeroed the
319 * AC entries, so we need only set the DC entries correctly.
321 for (; xindex < compptr->MCU_width; xindex++) {
322 MCU_buffer[blkn] = coef->dummy_buffer[blkn];
323 MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
328 /* Try to write the MCU. */
329 if (! (*lossyc->entropy_encode_mcu) (cinfo, MCU_buffer)) {
330 /* Suspension forced; update state counters and exit */
331 coef->MCU_vert_offset = yoffset;
332 coef->mcu_ctr = MCU_col_num;
336 /* Completed an MCU row, but perhaps not an iMCU row */
339 /* Completed the iMCU row, advance counters for next one */
340 coef->iMCU_row_num++;
341 start_iMCU_row(cinfo);
347 * Initialize coefficient buffer controller.
349 * Each passed coefficient array must be the right size for that
350 * coefficient: width_in_data_units wide and height_in_data_units high,
351 * with unitheight at least v_samp_factor.
355 transencode_coef_controller (j_compress_ptr cinfo,
356 jvirt_barray_ptr * coef_arrays)
358 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
364 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
365 SIZEOF(c_coef_controller));
366 lossyc->coef_private = (struct jpeg_c_coef_controller *) coef;
368 /* Save pointer to virtual arrays */
369 coef->whole_image = coef_arrays;
371 /* Allocate and pre-zero space for dummy DCT blocks. */
373 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
374 C_MAX_DATA_UNITS_IN_MCU * SIZEOF(JBLOCK));
375 jzero_far((void FAR *) buffer, C_MAX_DATA_UNITS_IN_MCU * SIZEOF(JBLOCK));
376 for (i = 0; i < C_MAX_DATA_UNITS_IN_MCU; i++) {
377 coef->dummy_buffer[i] = buffer + i;
383 * Initialize the transencoer codec.
384 * This is called only once, during master selection.
388 transencode_codec (j_compress_ptr cinfo,
389 jvirt_barray_ptr * coef_arrays)
391 j_lossy_c_ptr lossyc;
393 /* Create subobject in permanent pool */
394 lossyc = (j_lossy_c_ptr)
395 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
396 SIZEOF(jpeg_lossy_c_codec));
397 cinfo->codec = (struct jpeg_c_codec *) lossyc;
399 /* Initialize sub-modules */
401 /* Entropy encoding: either Huffman or arithmetic coding. */
402 if (cinfo->arith_code) {
403 jinit_arith_encoder(cinfo);
405 if (cinfo->process == JPROC_PROGRESSIVE) {
406 #ifdef C_PROGRESSIVE_SUPPORTED
407 jinit_phuff_encoder(cinfo);
409 ERREXIT(cinfo, JERR_NOT_COMPILED);
412 jinit_shuff_encoder(cinfo);
415 /* We need a special coefficient buffer controller. */
416 transencode_coef_controller(cinfo, coef_arrays);
418 /* Initialize method pointers */
419 lossyc->pub.start_pass = start_pass_coef;
420 lossyc->pub.compress_data = compress_output;