]> git.saurik.com Git - wxWidgets.git/blob - src/jpeg/jctrans.c
add support for alpha in color dialog on OSX, see #14127
[wxWidgets.git] / src / jpeg / jctrans.c
1 /*
2 * jctrans.c
3 *
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.
7 *
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.
11 */
12
13 #define JPEG_INTERNALS
14 #include "jinclude.h"
15 #include "jpeglib.h"
16
17
18 /* Forward declarations */
19 LOCAL(void) transencode_master_selection
20 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
21 LOCAL(void) transencode_coef_controller
22 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
23
24 #if defined(__VISAGECPP__)
25 /* Visual Age fixups for multiple declarations */
26 # define start_pass_coef start_pass_coef2 /* already in jccoeft.c */
27 # define compress_output compress_output2 /* already in jccoeft.c */
28 #endif
29
30
31 /*
32 * Compression initialization for writing raw-coefficient data.
33 * Before calling this, all parameters and a data destination must be set up.
34 * Call jpeg_finish_compress() to actually write the data.
35 *
36 * The number of passed virtual arrays must match cinfo->num_components.
37 * Note that the virtual arrays need not be filled or even realized at
38 * the time write_coefficients is called; indeed, if the virtual arrays
39 * were requested from this compression object's memory manager, they
40 * typically will be realized during this routine and filled afterwards.
41 */
42
43 GLOBAL(void)
44 jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
45 {
46 if (cinfo->global_state != CSTATE_START)
47 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
48 /* Mark all tables to be written */
49 jpeg_suppress_tables(cinfo, FALSE);
50 /* (Re)initialize error mgr and destination modules */
51 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
52 (*cinfo->dest->init_destination) (cinfo);
53 /* Perform master selection of active modules */
54 transencode_master_selection(cinfo, coef_arrays);
55 /* Wait for jpeg_finish_compress() call */
56 cinfo->next_scanline = 0; /* so jpeg_write_marker works */
57 cinfo->global_state = CSTATE_WRCOEFS;
58 }
59
60
61 /*
62 * Initialize the compression object with default parameters,
63 * then copy from the source object all parameters needed for lossless
64 * transcoding. Parameters that can be varied without loss (such as
65 * scan script and Huffman optimization) are left in their default states.
66 */
67
68 GLOBAL(void)
69 jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
70 j_compress_ptr dstinfo)
71 {
72 JQUANT_TBL ** qtblptr;
73 jpeg_component_info *incomp, *outcomp;
74 JQUANT_TBL *c_quant, *slot_quant;
75 int tblno, ci, coefi;
76
77 /* Safety check to ensure start_compress not called yet. */
78 if (dstinfo->global_state != CSTATE_START)
79 ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
80 /* Copy fundamental image dimensions */
81 dstinfo->image_width = srcinfo->image_width;
82 dstinfo->image_height = srcinfo->image_height;
83 dstinfo->input_components = srcinfo->num_components;
84 dstinfo->in_color_space = srcinfo->jpeg_color_space;
85 /* Initialize all parameters to default values */
86 jpeg_set_defaults(dstinfo);
87 /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
88 * Fix it to get the right header markers for the image colorspace.
89 */
90 jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
91 dstinfo->data_precision = srcinfo->data_precision;
92 dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
93 /* Copy the source's quantization tables. */
94 for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
95 if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
96 qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
97 if (*qtblptr == NULL)
98 *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
99 MEMCOPY((*qtblptr)->quantval,
100 srcinfo->quant_tbl_ptrs[tblno]->quantval,
101 SIZEOF((*qtblptr)->quantval));
102 (*qtblptr)->sent_table = FALSE;
103 }
104 }
105 /* Copy the source's per-component info.
106 * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
107 */
108 dstinfo->num_components = srcinfo->num_components;
109 if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
110 ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
111 MAX_COMPONENTS);
112 for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
113 ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
114 outcomp->component_id = incomp->component_id;
115 outcomp->h_samp_factor = incomp->h_samp_factor;
116 outcomp->v_samp_factor = incomp->v_samp_factor;
117 outcomp->quant_tbl_no = incomp->quant_tbl_no;
118 /* Make sure saved quantization table for component matches the qtable
119 * slot. If not, the input file re-used this qtable slot.
120 * IJG encoder currently cannot duplicate this.
121 */
122 tblno = outcomp->quant_tbl_no;
123 if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
124 srcinfo->quant_tbl_ptrs[tblno] == NULL)
125 ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
126 slot_quant = srcinfo->quant_tbl_ptrs[tblno];
127 c_quant = incomp->quant_table;
128 if (c_quant != NULL) {
129 for (coefi = 0; coefi < DCTSIZE2; coefi++) {
130 if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
131 ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
132 }
133 }
134 /* Note: we do not copy the source's Huffman table assignments;
135 * instead we rely on jpeg_set_colorspace to have made a suitable choice.
136 */
137 }
138 /* Also copy JFIF version and resolution information, if available.
139 * Strictly speaking this isn't "critical" info, but it's nearly
140 * always appropriate to copy it if available. In particular,
141 * if the application chooses to copy JFIF 1.02 extension markers from
142 * the source file, we need to copy the version to make sure we don't
143 * emit a file that has 1.02 extensions but a claimed version of 1.01.
144 * We will *not*, however, copy version info from mislabeled "2.01" files.
145 */
146 if (srcinfo->saw_JFIF_marker) {
147 if (srcinfo->JFIF_major_version == 1) {
148 dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
149 dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
150 }
151 dstinfo->density_unit = srcinfo->density_unit;
152 dstinfo->X_density = srcinfo->X_density;
153 dstinfo->Y_density = srcinfo->Y_density;
154 }
155 }
156
157
158 /*
159 * Master selection of compression modules for transcoding.
160 * This substitutes for jcinit.c's initialization of the full compressor.
161 */
162
163 LOCAL(void)
164 transencode_master_selection (j_compress_ptr cinfo,
165 jvirt_barray_ptr * coef_arrays)
166 {
167 /* Although we don't actually use input_components for transcoding,
168 * jcmaster.c's initial_setup will complain if input_components is 0.
169 */
170 cinfo->input_components = 1;
171 /* Initialize master control (includes parameter checking/processing) */
172 jinit_c_master_control(cinfo, TRUE /* transcode only */);
173
174 /* Entropy encoding: either Huffman or arithmetic coding. */
175 if (cinfo->arith_code) {
176 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
177 } else {
178 if (cinfo->progressive_mode) {
179 #ifdef C_PROGRESSIVE_SUPPORTED
180 jinit_phuff_encoder(cinfo);
181 #else
182 ERREXIT(cinfo, JERR_NOT_COMPILED);
183 #endif
184 } else
185 jinit_huff_encoder(cinfo);
186 }
187
188 /* We need a special coefficient buffer controller. */
189 transencode_coef_controller(cinfo, coef_arrays);
190
191 jinit_marker_writer(cinfo);
192
193 /* We can now tell the memory manager to allocate virtual arrays. */
194 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
195
196 /* Write the datastream header (SOI, JFIF) immediately.
197 * Frame and scan headers are postponed till later.
198 * This lets application insert special markers after the SOI.
199 */
200 (*cinfo->marker->write_file_header) (cinfo);
201 }
202
203
204 /*
205 * The rest of this file is a special implementation of the coefficient
206 * buffer controller. This is similar to jccoefct.c, but it handles only
207 * output from presupplied virtual arrays. Furthermore, we generate any
208 * dummy padding blocks on-the-fly rather than expecting them to be present
209 * in the arrays.
210 */
211
212 /* Private buffer controller object */
213
214 typedef struct {
215 struct jpeg_c_coef_controller pub; /* public fields */
216
217 JDIMENSION iMCU_row_num; /* iMCU row # within image */
218 JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
219 int MCU_vert_offset; /* counts MCU rows within iMCU row */
220 int MCU_rows_per_iMCU_row; /* number of such rows needed */
221
222 /* Virtual block array for each component. */
223 jvirt_barray_ptr * whole_image;
224
225 /* Workspace for constructing dummy blocks at right/bottom edges. */
226 JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
227 } my_coef_controller;
228
229 typedef my_coef_controller * my_coef_ptr;
230
231
232 LOCAL(void)
233 start_iMCU_row (j_compress_ptr cinfo)
234 /* Reset within-iMCU-row counters for a new row */
235 {
236 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
237
238 /* In an interleaved scan, an MCU row is the same as an iMCU row.
239 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
240 * But at the bottom of the image, process only what's left.
241 */
242 if (cinfo->comps_in_scan > 1) {
243 coef->MCU_rows_per_iMCU_row = 1;
244 } else {
245 if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
246 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
247 else
248 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
249 }
250
251 coef->mcu_ctr = 0;
252 coef->MCU_vert_offset = 0;
253 }
254
255 /*
256 * Initialize for a processing pass.
257 */
258
259 METHODDEF(void)
260 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
261 {
262 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
263
264 if (pass_mode != JBUF_CRANK_DEST)
265 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
266
267 coef->iMCU_row_num = 0;
268 start_iMCU_row(cinfo);
269 }
270
271
272 /*
273 * Process some data.
274 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
275 * per call, ie, v_samp_factor block rows for each component in the scan.
276 * The data is obtained from the virtual arrays and fed to the entropy coder.
277 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
278 *
279 * NB: input_buf is ignored; it is likely to be a NULL pointer.
280 */
281
282 METHODDEF(wxjpeg_boolean)
283 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
284 {
285 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
286 JDIMENSION MCU_col_num; /* index of current MCU within row */
287 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
288 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
289 int blkn, ci, xindex, yindex, yoffset, blockcnt;
290 JDIMENSION start_col;
291 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
292 JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
293 JBLOCKROW buffer_ptr;
294 jpeg_component_info *compptr;
295
296 /* Align the virtual buffers for the components used in this scan. */
297 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
298 compptr = cinfo->cur_comp_info[ci];
299 buffer[ci] = (*cinfo->mem->access_virt_barray)
300 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
301 coef->iMCU_row_num * compptr->v_samp_factor,
302 (JDIMENSION) compptr->v_samp_factor, FALSE);
303 }
304
305 /* Loop to process one whole iMCU row */
306 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
307 yoffset++) {
308 for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
309 MCU_col_num++) {
310 /* Construct list of pointers to DCT blocks belonging to this MCU */
311 blkn = 0; /* index of current DCT block within MCU */
312 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
313 compptr = cinfo->cur_comp_info[ci];
314 start_col = MCU_col_num * compptr->MCU_width;
315 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
316 : compptr->last_col_width;
317 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
318 if (coef->iMCU_row_num < last_iMCU_row ||
319 yindex+yoffset < compptr->last_row_height) {
320 /* Fill in pointers to real blocks in this row */
321 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
322 for (xindex = 0; xindex < blockcnt; xindex++)
323 MCU_buffer[blkn++] = buffer_ptr++;
324 } else {
325 /* At bottom of image, need a whole row of dummy blocks */
326 xindex = 0;
327 }
328 /* Fill in any dummy blocks needed in this row.
329 * Dummy blocks are filled in the same way as in jccoefct.c:
330 * all zeroes in the AC entries, DC entries equal to previous
331 * block's DC value. The init routine has already zeroed the
332 * AC entries, so we need only set the DC entries correctly.
333 */
334 for (; xindex < compptr->MCU_width; xindex++) {
335 MCU_buffer[blkn] = coef->dummy_buffer[blkn];
336 MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
337 blkn++;
338 }
339 }
340 }
341 /* Try to write the MCU. */
342 if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
343 /* Suspension forced; update state counters and exit */
344 coef->MCU_vert_offset = yoffset;
345 coef->mcu_ctr = MCU_col_num;
346 return FALSE;
347 }
348 }
349 /* Completed an MCU row, but perhaps not an iMCU row */
350 coef->mcu_ctr = 0;
351 }
352 /* Completed the iMCU row, advance counters for next one */
353 coef->iMCU_row_num++;
354 start_iMCU_row(cinfo);
355 return TRUE;
356 }
357
358
359 /*
360 * Initialize coefficient buffer controller.
361 *
362 * Each passed coefficient array must be the right size for that
363 * coefficient: width_in_blocks wide and height_in_blocks high,
364 * with unitheight at least v_samp_factor.
365 */
366
367 LOCAL(void)
368 transencode_coef_controller (j_compress_ptr cinfo,
369 jvirt_barray_ptr * coef_arrays)
370 {
371 my_coef_ptr coef;
372 JBLOCKROW buffer;
373 int i;
374
375 coef = (my_coef_ptr)
376 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
377 SIZEOF(my_coef_controller));
378 cinfo->coef = (struct jpeg_c_coef_controller *) coef;
379 coef->pub.start_pass = start_pass_coef;
380 coef->pub.compress_data = compress_output;
381
382 /* Save pointer to virtual arrays */
383 coef->whole_image = coef_arrays;
384
385 /* Allocate and pre-zero space for dummy DCT blocks. */
386 buffer = (JBLOCKROW)
387 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
388 C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
389 jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
390 for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
391 coef->dummy_buffer[i] = buffer + i;
392 }
393 }
394
395 #if defined(__VISAGECPP__)
396 # ifdef start_pass_coef2
397 # undef start_pass_coef2
398 # endif
399 # ifdef compress_output2
400 # undef compress_output2
401 # endif
402 #endif