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