Path: blob/master/3rdparty/libjpeg-turbo/src/jctrans.c
16337 views
/*1* jctrans.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1995-1998, Thomas G. Lane.5* Modified 2000-2009 by Guido Vollbeding.6* It was modified by The libjpeg-turbo Project to include only code relevant7* to libjpeg-turbo.8* For conditions of distribution and use, see the accompanying README.ijg9* 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_selection23(j_compress_ptr cinfo, jvirt_barray_ptr *coef_arrays);24LOCAL(void) transencode_coef_controller25(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#if JPEG_LIB_VERSION >= 7083dstinfo->jpeg_width = srcinfo->output_width;84dstinfo->jpeg_height = srcinfo->output_height;85dstinfo->min_DCT_h_scaled_size = srcinfo->min_DCT_h_scaled_size;86dstinfo->min_DCT_v_scaled_size = srcinfo->min_DCT_v_scaled_size;87#endif88/* Initialize all parameters to default values */89jpeg_set_defaults(dstinfo);90/* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.91* Fix it to get the right header markers for the image colorspace.92*/93jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);94dstinfo->data_precision = srcinfo->data_precision;95dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;96/* Copy the source's quantization tables. */97for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {98if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {99qtblptr = & dstinfo->quant_tbl_ptrs[tblno];100if (*qtblptr == NULL)101*qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);102MEMCOPY((*qtblptr)->quantval,103srcinfo->quant_tbl_ptrs[tblno]->quantval,104sizeof((*qtblptr)->quantval));105(*qtblptr)->sent_table = FALSE;106}107}108/* Copy the source's per-component info.109* Note we assume jpeg_set_defaults has allocated the dest comp_info array.110*/111dstinfo->num_components = srcinfo->num_components;112if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)113ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,114MAX_COMPONENTS);115for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;116ci < dstinfo->num_components; ci++, incomp++, outcomp++) {117outcomp->component_id = incomp->component_id;118outcomp->h_samp_factor = incomp->h_samp_factor;119outcomp->v_samp_factor = incomp->v_samp_factor;120outcomp->quant_tbl_no = incomp->quant_tbl_no;121/* Make sure saved quantization table for component matches the qtable122* slot. If not, the input file re-used this qtable slot.123* IJG encoder currently cannot duplicate this.124*/125tblno = outcomp->quant_tbl_no;126if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||127srcinfo->quant_tbl_ptrs[tblno] == NULL)128ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);129slot_quant = srcinfo->quant_tbl_ptrs[tblno];130c_quant = incomp->quant_table;131if (c_quant != NULL) {132for (coefi = 0; coefi < DCTSIZE2; coefi++) {133if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])134ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);135}136}137/* Note: we do not copy the source's Huffman table assignments;138* instead we rely on jpeg_set_colorspace to have made a suitable choice.139*/140}141/* Also copy JFIF version and resolution information, if available.142* Strictly speaking this isn't "critical" info, but it's nearly143* always appropriate to copy it if available. In particular,144* if the application chooses to copy JFIF 1.02 extension markers from145* the source file, we need to copy the version to make sure we don't146* emit a file that has 1.02 extensions but a claimed version of 1.01.147* We will *not*, however, copy version info from mislabeled "2.01" files.148*/149if (srcinfo->saw_JFIF_marker) {150if (srcinfo->JFIF_major_version == 1) {151dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;152dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;153}154dstinfo->density_unit = srcinfo->density_unit;155dstinfo->X_density = srcinfo->X_density;156dstinfo->Y_density = srcinfo->Y_density;157}158}159160161/*162* Master selection of compression modules for transcoding.163* This substitutes for jcinit.c's initialization of the full compressor.164*/165166LOCAL(void)167transencode_master_selection (j_compress_ptr cinfo,168jvirt_barray_ptr *coef_arrays)169{170/* Although we don't actually use input_components for transcoding,171* jcmaster.c's initial_setup will complain if input_components is 0.172*/173cinfo->input_components = 1;174/* Initialize master control (includes parameter checking/processing) */175jinit_c_master_control(cinfo, TRUE /* transcode only */);176177/* Entropy encoding: either Huffman or arithmetic coding. */178if (cinfo->arith_code) {179#ifdef C_ARITH_CODING_SUPPORTED180jinit_arith_encoder(cinfo);181#else182ERREXIT(cinfo, JERR_ARITH_NOTIMPL);183#endif184} else {185if (cinfo->progressive_mode) {186#ifdef C_PROGRESSIVE_SUPPORTED187jinit_phuff_encoder(cinfo);188#else189ERREXIT(cinfo, JERR_NOT_COMPILED);190#endif191} else192jinit_huff_encoder(cinfo);193}194195/* We need a special coefficient buffer controller. */196transencode_coef_controller(cinfo, coef_arrays);197198jinit_marker_writer(cinfo);199200/* We can now tell the memory manager to allocate virtual arrays. */201(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);202203/* Write the datastream header (SOI, JFIF) immediately.204* Frame and scan headers are postponed till later.205* This lets application insert special markers after the SOI.206*/207(*cinfo->marker->write_file_header) (cinfo);208}209210211/*212* The rest of this file is a special implementation of the coefficient213* buffer controller. This is similar to jccoefct.c, but it handles only214* output from presupplied virtual arrays. Furthermore, we generate any215* dummy padding blocks on-the-fly rather than expecting them to be present216* in the arrays.217*/218219/* Private buffer controller object */220221typedef struct {222struct jpeg_c_coef_controller pub; /* public fields */223224JDIMENSION iMCU_row_num; /* iMCU row # within image */225JDIMENSION mcu_ctr; /* counts MCUs processed in current row */226int MCU_vert_offset; /* counts MCU rows within iMCU row */227int MCU_rows_per_iMCU_row; /* number of such rows needed */228229/* Virtual block array for each component. */230jvirt_barray_ptr *whole_image;231232/* Workspace for constructing dummy blocks at right/bottom edges. */233JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];234} my_coef_controller;235236typedef my_coef_controller *my_coef_ptr;237238239LOCAL(void)240start_iMCU_row (j_compress_ptr cinfo)241/* Reset within-iMCU-row counters for a new row */242{243my_coef_ptr coef = (my_coef_ptr) cinfo->coef;244245/* In an interleaved scan, an MCU row is the same as an iMCU row.246* In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.247* But at the bottom of the image, process only what's left.248*/249if (cinfo->comps_in_scan > 1) {250coef->MCU_rows_per_iMCU_row = 1;251} else {252if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))253coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;254else255coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;256}257258coef->mcu_ctr = 0;259coef->MCU_vert_offset = 0;260}261262263/*264* Initialize for a processing pass.265*/266267METHODDEF(void)268start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)269{270my_coef_ptr coef = (my_coef_ptr) cinfo->coef;271272if (pass_mode != JBUF_CRANK_DEST)273ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);274275coef->iMCU_row_num = 0;276start_iMCU_row(cinfo);277}278279280/*281* Process some data.282* We process the equivalent of one fully interleaved MCU row ("iMCU" row)283* per call, ie, v_samp_factor block rows for each component in the scan.284* The data is obtained from the virtual arrays and fed to the entropy coder.285* Returns TRUE if the iMCU row is completed, FALSE if suspended.286*287* NB: input_buf is ignored; it is likely to be a NULL pointer.288*/289290METHODDEF(boolean)291compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)292{293my_coef_ptr coef = (my_coef_ptr) cinfo->coef;294JDIMENSION MCU_col_num; /* index of current MCU within row */295JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;296JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;297int blkn, ci, xindex, yindex, yoffset, blockcnt;298JDIMENSION start_col;299JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];300JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];301JBLOCKROW buffer_ptr;302jpeg_component_info *compptr;303304/* Align the virtual buffers for the components used in this scan. */305for (ci = 0; ci < cinfo->comps_in_scan; ci++) {306compptr = cinfo->cur_comp_info[ci];307buffer[ci] = (*cinfo->mem->access_virt_barray)308((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],309coef->iMCU_row_num * compptr->v_samp_factor,310(JDIMENSION) compptr->v_samp_factor, FALSE);311}312313/* Loop to process one whole iMCU row */314for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;315yoffset++) {316for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;317MCU_col_num++) {318/* Construct list of pointers to DCT blocks belonging to this MCU */319blkn = 0; /* index of current DCT block within MCU */320for (ci = 0; ci < cinfo->comps_in_scan; ci++) {321compptr = cinfo->cur_comp_info[ci];322start_col = MCU_col_num * compptr->MCU_width;323blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width324: compptr->last_col_width;325for (yindex = 0; yindex < compptr->MCU_height; yindex++) {326if (coef->iMCU_row_num < last_iMCU_row ||327yindex+yoffset < compptr->last_row_height) {328/* Fill in pointers to real blocks in this row */329buffer_ptr = buffer[ci][yindex+yoffset] + start_col;330for (xindex = 0; xindex < blockcnt; xindex++)331MCU_buffer[blkn++] = buffer_ptr++;332} else {333/* At bottom of image, need a whole row of dummy blocks */334xindex = 0;335}336/* Fill in any dummy blocks needed in this row.337* Dummy blocks are filled in the same way as in jccoefct.c:338* all zeroes in the AC entries, DC entries equal to previous339* block's DC value. The init routine has already zeroed the340* AC entries, so we need only set the DC entries correctly.341*/342for (; xindex < compptr->MCU_width; xindex++) {343MCU_buffer[blkn] = coef->dummy_buffer[blkn];344MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];345blkn++;346}347}348}349/* Try to write the MCU. */350if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {351/* Suspension forced; update state counters and exit */352coef->MCU_vert_offset = yoffset;353coef->mcu_ctr = MCU_col_num;354return FALSE;355}356}357/* Completed an MCU row, but perhaps not an iMCU row */358coef->mcu_ctr = 0;359}360/* Completed the iMCU row, advance counters for next one */361coef->iMCU_row_num++;362start_iMCU_row(cinfo);363return TRUE;364}365366367/*368* Initialize coefficient buffer controller.369*370* Each passed coefficient array must be the right size for that371* coefficient: width_in_blocks wide and height_in_blocks high,372* with unitheight at least v_samp_factor.373*/374375LOCAL(void)376transencode_coef_controller (j_compress_ptr cinfo,377jvirt_barray_ptr *coef_arrays)378{379my_coef_ptr coef;380JBLOCKROW buffer;381int i;382383coef = (my_coef_ptr)384(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,385sizeof(my_coef_controller));386cinfo->coef = (struct jpeg_c_coef_controller *) coef;387coef->pub.start_pass = start_pass_coef;388coef->pub.compress_data = compress_output;389390/* Save pointer to virtual arrays */391coef->whole_image = coef_arrays;392393/* Allocate and pre-zero space for dummy DCT blocks. */394buffer = (JBLOCKROW)395(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,396C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));397jzero_far((void *) buffer, C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));398for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {399coef->dummy_buffer[i] = buffer + i;400}401}402403404