Overhauled GRUB menus to reduce number of entries, mainly by making use
[cake.git] / compiler / libjpeg / main / jctrans.c
blob26bb45c97040384c826c59ada7392dec61632c5e
1 /*
2 $Id$
3 */
5 /*
6 * jctrans.c
8 * Copyright (C) 1995-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 library routines for transcoding compression,
13 * that is, writing raw DCT coefficient arrays to an output JPEG file.
14 * The routines in jcapimin.c will also be needed by a transcoder.
17 #define JPEG_INTERNALS
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jlossy.h" /* Private declarations for lossy codec */
23 /* Forward declarations */
24 LOCAL(void) transencode_master_selection
25 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
26 LOCAL(void) transencode_codec
27 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
28 LOCAL(void) transencode_coef_controller
29 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
33 * Compression initialization for writing raw-coefficient data.
34 * Before calling this, all parameters and a data destination must be set up.
35 * Call jpeg_finish_compress() to actually write the data.
37 * The number of passed virtual arrays must match cinfo->num_components.
38 * Note that the virtual arrays need not be filled or even realized at
39 * the time write_coefficients is called; indeed, if the virtual arrays
40 * were requested from this compression object's memory manager, they
41 * typically will be realized during this routine and filled afterwards.
44 JGLOBAL(void)
45 jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
47 if (cinfo->global_state != CSTATE_START)
48 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
49 /* Mark all tables to be written */
50 jpeg_suppress_tables(cinfo, FALSE);
51 /* (Re)initialize error mgr and destination modules */
52 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
53 (*cinfo->dest->init_destination) (cinfo);
54 /* Perform master selection of active modules */
55 transencode_master_selection(cinfo, coef_arrays);
56 /* Wait for jpeg_finish_compress() call */
57 cinfo->next_scanline = 0; /* so jpeg_write_marker works */
58 cinfo->global_state = CSTATE_WRCOEFS;
63 * Initialize the compression object with default parameters,
64 * then copy from the source object all parameters needed for lossless
65 * transcoding. Parameters that can be varied without loss (such as
66 * scan script and Huffman optimization) are left in their default states.
69 JGLOBAL(void)
70 jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
71 j_compress_ptr dstinfo)
73 JQUANT_TBL ** qtblptr;
74 jpeg_component_info *incomp, *outcomp;
75 JQUANT_TBL *c_quant, *slot_quant;
76 int tblno, ci, coefi;
78 /* Safety check to ensure start_compress not called yet. */
79 if (dstinfo->global_state != CSTATE_START)
80 ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
81 /* Copy fundamental image dimensions */
82 dstinfo->image_width = srcinfo->image_width;
83 dstinfo->image_height = srcinfo->image_height;
84 dstinfo->input_components = srcinfo->num_components;
85 dstinfo->in_color_space = srcinfo->jpeg_color_space;
86 /* Initialize all parameters to default values */
87 jpeg_set_defaults(dstinfo);
88 /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
89 * Fix it to get the right header markers for the image colorspace.
91 jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
92 dstinfo->data_precision = srcinfo->data_precision;
93 dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
94 /* Copy the source's quantization tables. */
95 for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
96 if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
97 qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
98 if (*qtblptr == NULL)
99 *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
100 MEMCOPY((*qtblptr)->quantval,
101 srcinfo->quant_tbl_ptrs[tblno]->quantval,
102 SIZEOF((*qtblptr)->quantval));
103 (*qtblptr)->sent_table = FALSE;
106 /* Copy the source's per-component info.
107 * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
109 dstinfo->num_components = srcinfo->num_components;
110 if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
111 ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
112 MAX_COMPONENTS);
113 for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
114 ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
115 outcomp->component_id = incomp->component_id;
116 outcomp->h_samp_factor = incomp->h_samp_factor;
117 outcomp->v_samp_factor = incomp->v_samp_factor;
118 outcomp->quant_tbl_no = incomp->quant_tbl_no;
119 /* Make sure saved quantization table for component matches the qtable
120 * slot. If not, the input file re-used this qtable slot.
121 * IJG encoder currently cannot duplicate this.
123 tblno = outcomp->quant_tbl_no;
124 if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
125 srcinfo->quant_tbl_ptrs[tblno] == NULL)
126 ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
127 slot_quant = srcinfo->quant_tbl_ptrs[tblno];
128 c_quant = incomp->quant_table;
129 if (c_quant != NULL) {
130 for (coefi = 0; coefi < DCTSIZE2; coefi++) {
131 if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
132 ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
135 /* Note: we do not copy the source's Huffman table assignments;
136 * instead we rely on jpeg_set_colorspace to have made a suitable choice.
139 /* Also copy JFIF version and resolution information, if available.
140 * Strictly speaking this isn't "critical" info, but it's nearly
141 * always appropriate to copy it if available. In particular,
142 * if the application chooses to copy JFIF 1.02 extension markers from
143 * the source file, we need to copy the version to make sure we don't
144 * emit a file that has 1.02 extensions but a claimed version of 1.01.
145 * We will *not*, however, copy version info from mislabeled "2.01" files.
147 if (srcinfo->saw_JFIF_marker) {
148 if (srcinfo->JFIF_major_version == 1) {
149 dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
150 dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
152 dstinfo->density_unit = srcinfo->density_unit;
153 dstinfo->X_density = srcinfo->X_density;
154 dstinfo->Y_density = srcinfo->Y_density;
160 * Master selection of compression modules for transcoding.
161 * This substitutes for jcinit.c's initialization of the full compressor.
164 LOCAL(void)
165 transencode_master_selection (j_compress_ptr cinfo,
166 jvirt_barray_ptr * coef_arrays)
168 cinfo->data_unit = DCTSIZE;
169 /* Although we don't actually use input_components for transcoding,
170 * jcmaster.c's initial_setup will complain if input_components is 0.
172 cinfo->input_components = 1;
173 /* Initialize master control (includes parameter checking/processing) */
174 jinit_c_master_control(cinfo, TRUE /* transcode only */);
176 /* We need a special compression codec. */
177 transencode_codec(cinfo, coef_arrays);
179 jinit_marker_writer(cinfo);
181 /* We can now tell the memory manager to allocate virtual arrays. */
182 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
184 /* Write the datastream header (SOI, JFIF) immediately.
185 * Frame and scan headers are postponed till later.
186 * This lets application insert special markers after the SOI.
188 (*cinfo->marker->write_file_header) (cinfo);
193 * The rest of this file is a special implementation of the coefficient
194 * buffer controller. This is similar to jccoefct.c, but it handles only
195 * output from presupplied virtual arrays. Furthermore, we generate any
196 * dummy padding blocks on-the-fly rather than expecting them to be present
197 * in the arrays.
200 /* Private buffer controller object */
202 typedef struct {
203 JDIMENSION iMCU_row_num; /* iMCU row # within image */
204 JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
205 int MCU_vert_offset; /* counts MCU rows within iMCU row */
206 int MCU_rows_per_iMCU_row; /* number of such rows needed */
208 /* Virtual block array for each component. */
209 jvirt_barray_ptr * whole_image;
211 /* Workspace for constructing dummy blocks at right/bottom edges. */
212 JBLOCKROW dummy_buffer[C_MAX_DATA_UNITS_IN_MCU];
213 } c_coef_controller;
215 typedef c_coef_controller * c_coef_ptr;
218 LOCAL(void)
219 start_iMCU_row (j_compress_ptr cinfo)
220 /* Reset within-iMCU-row counters for a new row */
222 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
223 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
225 /* In an interleaved scan, an MCU row is the same as an iMCU row.
226 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
227 * But at the bottom of the image, process only what's left.
229 if (cinfo->comps_in_scan > 1) {
230 coef->MCU_rows_per_iMCU_row = 1;
231 } else {
232 if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
233 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
234 else
235 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
238 coef->mcu_ctr = 0;
239 coef->MCU_vert_offset = 0;
244 * Initialize for a processing pass.
247 METHODDEF(void)
248 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
250 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
251 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
253 if (pass_mode != JBUF_CRANK_DEST)
254 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
256 coef->iMCU_row_num = 0;
257 start_iMCU_row(cinfo);
262 * Process some data.
263 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
264 * per call, ie, v_samp_factor block rows for each component in the scan.
265 * The data is obtained from the virtual arrays and fed to the entropy coder.
266 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
268 * NB: input_buf is ignored; it is likely to be a NULL pointer.
271 METHODDEF(boolean)
272 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
274 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
275 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
276 JDIMENSION MCU_col_num; /* index of current MCU within row */
277 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
278 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
279 int blkn, ci, xindex, yindex, yoffset, blockcnt;
280 JDIMENSION start_col;
281 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
282 JBLOCKROW MCU_buffer[C_MAX_DATA_UNITS_IN_MCU];
283 JBLOCKROW buffer_ptr;
284 jpeg_component_info *compptr;
286 /* Align the virtual buffers for the components used in this scan. */
287 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
288 compptr = cinfo->cur_comp_info[ci];
289 buffer[ci] = (*cinfo->mem->access_virt_barray)
290 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
291 coef->iMCU_row_num * compptr->v_samp_factor,
292 (JDIMENSION) compptr->v_samp_factor, FALSE);
295 /* Loop to process one whole iMCU row */
296 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
297 yoffset++) {
298 for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
299 MCU_col_num++) {
300 /* Construct list of pointers to DCT blocks belonging to this MCU */
301 blkn = 0; /* index of current DCT block within MCU */
302 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
303 compptr = cinfo->cur_comp_info[ci];
304 start_col = MCU_col_num * compptr->MCU_width;
305 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
306 : compptr->last_col_width;
307 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
308 if (coef->iMCU_row_num < last_iMCU_row ||
309 yindex+yoffset < compptr->last_row_height) {
310 /* Fill in pointers to real blocks in this row */
311 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
312 for (xindex = 0; xindex < blockcnt; xindex++)
313 MCU_buffer[blkn++] = buffer_ptr++;
314 } else {
315 /* At bottom of image, need a whole row of dummy blocks */
316 xindex = 0;
318 /* Fill in any dummy blocks needed in this row.
319 * Dummy blocks are filled in the same way as in jccoefct.c:
320 * all zeroes in the AC entries, DC entries equal to previous
321 * block's DC value. The init routine has already zeroed the
322 * AC entries, so we need only set the DC entries correctly.
324 for (; xindex < compptr->MCU_width; xindex++) {
325 MCU_buffer[blkn] = coef->dummy_buffer[blkn];
326 MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
327 blkn++;
331 /* Try to write the MCU. */
332 if (! (*lossyc->entropy_encode_mcu) (cinfo, MCU_buffer)) {
333 /* Suspension forced; update state counters and exit */
334 coef->MCU_vert_offset = yoffset;
335 coef->mcu_ctr = MCU_col_num;
336 return FALSE;
339 /* Completed an MCU row, but perhaps not an iMCU row */
340 coef->mcu_ctr = 0;
342 /* Completed the iMCU row, advance counters for next one */
343 coef->iMCU_row_num++;
344 start_iMCU_row(cinfo);
345 return TRUE;
350 * Initialize coefficient buffer controller.
352 * Each passed coefficient array must be the right size for that
353 * coefficient: width_in_data_units wide and height_in_data_units high,
354 * with unitheight at least v_samp_factor.
357 LOCAL(void)
358 transencode_coef_controller (j_compress_ptr cinfo,
359 jvirt_barray_ptr * coef_arrays)
361 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
362 c_coef_ptr coef;
363 JBLOCKROW buffer;
364 int i;
366 coef = (c_coef_ptr)
367 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
368 SIZEOF(c_coef_controller));
369 lossyc->coef_private = (struct jpeg_c_coef_controller *) coef;
371 /* Save pointer to virtual arrays */
372 coef->whole_image = coef_arrays;
374 /* Allocate and pre-zero space for dummy DCT blocks. */
375 buffer = (JBLOCKROW)
376 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
377 C_MAX_DATA_UNITS_IN_MCU * SIZEOF(JBLOCK));
378 jzero_far((void FAR *) buffer, C_MAX_DATA_UNITS_IN_MCU * SIZEOF(JBLOCK));
379 for (i = 0; i < C_MAX_DATA_UNITS_IN_MCU; i++) {
380 coef->dummy_buffer[i] = buffer + i;
386 * Initialize the transencoer codec.
387 * This is called only once, during master selection.
390 LOCAL(void)
391 transencode_codec (j_compress_ptr cinfo,
392 jvirt_barray_ptr * coef_arrays)
394 j_lossy_c_ptr lossyc;
396 /* Create subobject in permanent pool */
397 lossyc = (j_lossy_c_ptr)
398 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
399 SIZEOF(jpeg_lossy_c_codec));
400 cinfo->codec = (struct jpeg_c_codec *) lossyc;
402 /* Initialize sub-modules */
404 /* Entropy encoding: either Huffman or arithmetic coding. */
405 if (cinfo->arith_code) {
406 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
407 } else {
408 if (cinfo->process == JPROC_PROGRESSIVE) {
409 #ifdef C_PROGRESSIVE_SUPPORTED
410 jinit_phuff_encoder(cinfo);
411 #else
412 ERREXIT(cinfo, JERR_NOT_COMPILED);
413 #endif
414 } else
415 jinit_shuff_encoder(cinfo);
418 /* We need a special coefficient buffer controller. */
419 transencode_coef_controller(cinfo, coef_arrays);
421 /* Initialize method pointers */
422 lossyc->pub.start_pass = start_pass_coef;
423 lossyc->pub.compress_data = compress_output;