Path: blob/master/src/java.desktop/share/native/libjavajpeg/jctrans.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jctrans.c6*7* Copyright (C) 1995-1998, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains library routines for transcoding compression,12* that is, writing raw DCT coefficient arrays to an output JPEG file.13* The routines in jcapimin.c will also be needed by a transcoder.14*/1516#define JPEG_INTERNALS17#include "jinclude.h"18#include "jpeglib.h"192021/* Forward declarations */22LOCAL(void) transencode_master_selection23JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));24LOCAL(void) transencode_coef_controller25JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));262728/*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.32*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 at35* the time write_coefficients is called; indeed, if the virtual arrays36* were requested from this compression object's memory manager, they37* typically will be realized during this routine and filled afterwards.38*/3940GLOBAL(void)41jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)42{43if (cinfo->global_state != CSTATE_START)44ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);45/* Mark all tables to be written */46jpeg_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 */51transencode_master_selection(cinfo, coef_arrays);52/* Wait for jpeg_finish_compress() call */53cinfo->next_scanline = 0; /* so jpeg_write_marker works */54cinfo->global_state = CSTATE_WRCOEFS;55}565758/*59* Initialize the compression object with default parameters,60* then copy from the source object all parameters needed for lossless61* transcoding. Parameters that can be varied without loss (such as62* scan script and Huffman optimization) are left in their default states.63*/6465GLOBAL(void)66jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,67j_compress_ptr dstinfo)68{69JQUANT_TBL ** qtblptr;70jpeg_component_info *incomp, *outcomp;71JQUANT_TBL *c_quant, *slot_quant;72int tblno, ci, coefi;7374/* Safety check to ensure start_compress not called yet. */75if (dstinfo->global_state != CSTATE_START)76ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);77/* Copy fundamental image dimensions */78dstinfo->image_width = srcinfo->image_width;79dstinfo->image_height = srcinfo->image_height;80dstinfo->input_components = srcinfo->num_components;81dstinfo->in_color_space = srcinfo->jpeg_color_space;82/* Initialize all parameters to default values */83jpeg_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.86*/87jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);88dstinfo->data_precision = srcinfo->data_precision;89dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;90/* Copy the source's quantization tables. */91for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {92if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {93qtblptr = & dstinfo->quant_tbl_ptrs[tblno];94if (*qtblptr == NULL)95*qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);96MEMCOPY((*qtblptr)->quantval,97srcinfo->quant_tbl_ptrs[tblno]->quantval,98SIZEOF((*qtblptr)->quantval));99(*qtblptr)->sent_table = FALSE;100}101}102/* Copy the source's per-component info.103* Note we assume jpeg_set_defaults has allocated the dest comp_info array.104*/105dstinfo->num_components = srcinfo->num_components;106if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)107ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,108MAX_COMPONENTS);109for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;110ci < dstinfo->num_components; ci++, incomp++, outcomp++) {111outcomp->component_id = incomp->component_id;112outcomp->h_samp_factor = incomp->h_samp_factor;113outcomp->v_samp_factor = incomp->v_samp_factor;114outcomp->quant_tbl_no = incomp->quant_tbl_no;115/* Make sure saved quantization table for component matches the qtable116* slot. If not, the input file re-used this qtable slot.117* IJG encoder currently cannot duplicate this.118*/119tblno = outcomp->quant_tbl_no;120if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||121srcinfo->quant_tbl_ptrs[tblno] == NULL)122ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);123slot_quant = srcinfo->quant_tbl_ptrs[tblno];124c_quant = incomp->quant_table;125if (c_quant != NULL) {126for (coefi = 0; coefi < DCTSIZE2; coefi++) {127if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])128ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);129}130}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.133*/134}135/* Also copy JFIF version and resolution information, if available.136* Strictly speaking this isn't "critical" info, but it's nearly137* always appropriate to copy it if available. In particular,138* if the application chooses to copy JFIF 1.02 extension markers from139* the source file, we need to copy the version to make sure we don't140* 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.142*/143if (srcinfo->saw_JFIF_marker) {144if (srcinfo->JFIF_major_version == 1) {145dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;146dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;147}148dstinfo->density_unit = srcinfo->density_unit;149dstinfo->X_density = srcinfo->X_density;150dstinfo->Y_density = srcinfo->Y_density;151}152}153154155/*156* Master selection of compression modules for transcoding.157* This substitutes for jcinit.c's initialization of the full compressor.158*/159160LOCAL(void)161transencode_master_selection (j_compress_ptr cinfo,162jvirt_barray_ptr * coef_arrays)163{164/* Although we don't actually use input_components for transcoding,165* jcmaster.c's initial_setup will complain if input_components is 0.166*/167cinfo->input_components = 1;168/* Initialize master control (includes parameter checking/processing) */169jinit_c_master_control(cinfo, TRUE /* transcode only */);170171/* Entropy encoding: either Huffman or arithmetic coding. */172if (cinfo->arith_code) {173ERREXIT(cinfo, JERR_ARITH_NOTIMPL);174} else {175if (cinfo->progressive_mode) {176#ifdef C_PROGRESSIVE_SUPPORTED177jinit_phuff_encoder(cinfo);178#else179ERREXIT(cinfo, JERR_NOT_COMPILED);180#endif181} else182jinit_huff_encoder(cinfo);183}184185/* We need a special coefficient buffer controller. */186transencode_coef_controller(cinfo, coef_arrays);187188jinit_marker_writer(cinfo);189190/* We can now tell the memory manager to allocate virtual arrays. */191(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);192193/* Write the datastream header (SOI, JFIF) immediately.194* Frame and scan headers are postponed till later.195* This lets application insert special markers after the SOI.196*/197(*cinfo->marker->write_file_header) (cinfo);198}199200201/*202* The rest of this file is a special implementation of the coefficient203* buffer controller. This is similar to jccoefct.c, but it handles only204* output from presupplied virtual arrays. Furthermore, we generate any205* dummy padding blocks on-the-fly rather than expecting them to be present206* in the arrays.207*/208209/* Private buffer controller object */210211typedef struct {212struct jpeg_c_coef_controller pub; /* public fields */213214JDIMENSION iMCU_row_num; /* iMCU row # within image */215JDIMENSION mcu_ctr; /* counts MCUs processed in current row */216int MCU_vert_offset; /* counts MCU rows within iMCU row */217int MCU_rows_per_iMCU_row; /* number of such rows needed */218219/* Virtual block array for each component. */220jvirt_barray_ptr * whole_image;221222/* Workspace for constructing dummy blocks at right/bottom edges. */223JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];224} my_coef_controller;225226typedef my_coef_controller * my_coef_ptr;227228229LOCAL(void)230start_iMCU_row (j_compress_ptr cinfo)231/* Reset within-iMCU-row counters for a new row */232{233my_coef_ptr coef = (my_coef_ptr) cinfo->coef;234235/* In an interleaved scan, an MCU row is the same as an iMCU row.236* In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.237* But at the bottom of the image, process only what's left.238*/239if (cinfo->comps_in_scan > 1) {240coef->MCU_rows_per_iMCU_row = 1;241} else {242if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))243coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;244else245coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;246}247248coef->mcu_ctr = 0;249coef->MCU_vert_offset = 0;250}251252253/*254* Initialize for a processing pass.255*/256257METHODDEF(void)258start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)259{260my_coef_ptr coef = (my_coef_ptr) cinfo->coef;261262if (pass_mode != JBUF_CRANK_DEST)263ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);264265coef->iMCU_row_num = 0;266start_iMCU_row(cinfo);267}268269270/*271* Process some data.272* We process the equivalent of one fully interleaved MCU row ("iMCU" row)273* per call, ie, v_samp_factor block rows for each component in the scan.274* The data is obtained from the virtual arrays and fed to the entropy coder.275* Returns TRUE if the iMCU row is completed, FALSE if suspended.276*277* NB: input_buf is ignored; it is likely to be a NULL pointer.278*/279280METHODDEF(boolean)281compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)282{283my_coef_ptr coef = (my_coef_ptr) cinfo->coef;284JDIMENSION MCU_col_num; /* index of current MCU within row */285JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;286JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;287int blkn, ci, xindex, yindex, yoffset, blockcnt;288JDIMENSION start_col;289JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];290JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];291JBLOCKROW buffer_ptr;292jpeg_component_info *compptr;293294/* Align the virtual buffers for the components used in this scan. */295for (ci = 0; ci < cinfo->comps_in_scan; ci++) {296compptr = cinfo->cur_comp_info[ci];297buffer[ci] = (*cinfo->mem->access_virt_barray)298((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],299coef->iMCU_row_num * compptr->v_samp_factor,300(JDIMENSION) compptr->v_samp_factor, FALSE);301}302303/* Loop to process one whole iMCU row */304for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;305yoffset++) {306for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;307MCU_col_num++) {308/* Construct list of pointers to DCT blocks belonging to this MCU */309blkn = 0; /* index of current DCT block within MCU */310for (ci = 0; ci < cinfo->comps_in_scan; ci++) {311compptr = cinfo->cur_comp_info[ci];312start_col = MCU_col_num * compptr->MCU_width;313blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width314: compptr->last_col_width;315for (yindex = 0; yindex < compptr->MCU_height; yindex++) {316if (coef->iMCU_row_num < last_iMCU_row ||317yindex+yoffset < compptr->last_row_height) {318/* Fill in pointers to real blocks in this row */319buffer_ptr = buffer[ci][yindex+yoffset] + start_col;320for (xindex = 0; xindex < blockcnt; xindex++)321MCU_buffer[blkn++] = buffer_ptr++;322} else {323/* At bottom of image, need a whole row of dummy blocks */324xindex = 0;325}326/* Fill in any dummy blocks needed in this row.327* Dummy blocks are filled in the same way as in jccoefct.c:328* all zeroes in the AC entries, DC entries equal to previous329* block's DC value. The init routine has already zeroed the330* AC entries, so we need only set the DC entries correctly.331*/332for (; xindex < compptr->MCU_width; xindex++) {333MCU_buffer[blkn] = coef->dummy_buffer[blkn];334MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];335blkn++;336}337}338}339/* Try to write the MCU. */340if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {341/* Suspension forced; update state counters and exit */342coef->MCU_vert_offset = yoffset;343coef->mcu_ctr = MCU_col_num;344return FALSE;345}346}347/* Completed an MCU row, but perhaps not an iMCU row */348coef->mcu_ctr = 0;349}350/* Completed the iMCU row, advance counters for next one */351coef->iMCU_row_num++;352start_iMCU_row(cinfo);353return TRUE;354}355356357/*358* Initialize coefficient buffer controller.359*360* Each passed coefficient array must be the right size for that361* coefficient: width_in_blocks wide and height_in_blocks high,362* with unitheight at least v_samp_factor.363*/364365LOCAL(void)366transencode_coef_controller (j_compress_ptr cinfo,367jvirt_barray_ptr * coef_arrays)368{369my_coef_ptr coef;370JBLOCKROW buffer;371int i;372373coef = (my_coef_ptr)374(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,375SIZEOF(my_coef_controller));376cinfo->coef = (struct jpeg_c_coef_controller *) coef;377coef->pub.start_pass = start_pass_coef;378coef->pub.compress_data = compress_output;379380/* Save pointer to virtual arrays */381coef->whole_image = coef_arrays;382383/* Allocate and pre-zero space for dummy DCT blocks. */384buffer = (JBLOCKROW)385(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,386C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));387jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));388for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {389coef->dummy_buffer[i] = buffer + i;390}391}392393394