Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/vncdll/winvnc/libjpeg/jctrans.c
Views: 11784
/*1* jctrans.c2*3* Copyright (C) 1995-1998, Thomas G. Lane.4* This file is part of the Independent JPEG Group's software.5* For conditions of distribution and use, see the accompanying README file.6*7* This file contains library routines for transcoding compression,8* that is, writing raw DCT coefficient arrays to an output JPEG file.9* The routines in jcapimin.c will also be needed by a transcoder.10*/1112#define JPEG_INTERNALS13#include "jinclude.h"14#include "jpeglib.h"151617/* Forward declarations */18LOCAL(void) transencode_master_selection19JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));20LOCAL(void) transencode_coef_controller21JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));222324/*25* Compression initialization for writing raw-coefficient data.26* Before calling this, all parameters and a data destination must be set up.27* Call jpeg_finish_compress() to actually write the data.28*29* The number of passed virtual arrays must match cinfo->num_components.30* Note that the virtual arrays need not be filled or even realized at31* the time write_coefficients is called; indeed, if the virtual arrays32* were requested from this compression object's memory manager, they33* typically will be realized during this routine and filled afterwards.34*/3536GLOBAL(void)37jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)38{39if (cinfo->global_state != CSTATE_START)40ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);41/* Mark all tables to be written */42jpeg_suppress_tables(cinfo, FALSE);43/* (Re)initialize error mgr and destination modules */44(*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);45(*cinfo->dest->init_destination) (cinfo);46/* Perform master selection of active modules */47transencode_master_selection(cinfo, coef_arrays);48/* Wait for jpeg_finish_compress() call */49cinfo->next_scanline = 0; /* so jpeg_write_marker works */50cinfo->global_state = CSTATE_WRCOEFS;51}525354/*55* Initialize the compression object with default parameters,56* then copy from the source object all parameters needed for lossless57* transcoding. Parameters that can be varied without loss (such as58* scan script and Huffman optimization) are left in their default states.59*/6061GLOBAL(void)62jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,63j_compress_ptr dstinfo)64{65JQUANT_TBL ** qtblptr;66jpeg_component_info *incomp, *outcomp;67JQUANT_TBL *c_quant, *slot_quant;68int tblno, ci, coefi;6970/* Safety check to ensure start_compress not called yet. */71if (dstinfo->global_state != CSTATE_START)72ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);73/* Copy fundamental image dimensions */74dstinfo->image_width = srcinfo->image_width;75dstinfo->image_height = srcinfo->image_height;76dstinfo->input_components = srcinfo->num_components;77dstinfo->in_color_space = srcinfo->jpeg_color_space;78/* Initialize all parameters to default values */79jpeg_set_defaults(dstinfo);80/* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.81* Fix it to get the right header markers for the image colorspace.82*/83jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);84dstinfo->data_precision = srcinfo->data_precision;85dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;86/* Copy the source's quantization tables. */87for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {88if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {89qtblptr = & dstinfo->quant_tbl_ptrs[tblno];90if (*qtblptr == NULL)91*qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);92MEMCOPY((*qtblptr)->quantval,93srcinfo->quant_tbl_ptrs[tblno]->quantval,94SIZEOF((*qtblptr)->quantval));95(*qtblptr)->sent_table = FALSE;96}97}98/* Copy the source's per-component info.99* Note we assume jpeg_set_defaults has allocated the dest comp_info array.100*/101dstinfo->num_components = srcinfo->num_components;102if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)103ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,104MAX_COMPONENTS);105for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;106ci < dstinfo->num_components; ci++, incomp++, outcomp++) {107outcomp->component_id = incomp->component_id;108outcomp->h_samp_factor = incomp->h_samp_factor;109outcomp->v_samp_factor = incomp->v_samp_factor;110outcomp->quant_tbl_no = incomp->quant_tbl_no;111/* Make sure saved quantization table for component matches the qtable112* slot. If not, the input file re-used this qtable slot.113* IJG encoder currently cannot duplicate this.114*/115tblno = outcomp->quant_tbl_no;116if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||117srcinfo->quant_tbl_ptrs[tblno] == NULL)118ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);119slot_quant = srcinfo->quant_tbl_ptrs[tblno];120c_quant = incomp->quant_table;121if (c_quant != NULL) {122for (coefi = 0; coefi < DCTSIZE2; coefi++) {123if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])124ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);125}126}127/* Note: we do not copy the source's Huffman table assignments;128* instead we rely on jpeg_set_colorspace to have made a suitable choice.129*/130}131/* Also copy JFIF version and resolution information, if available.132* Strictly speaking this isn't "critical" info, but it's nearly133* always appropriate to copy it if available. In particular,134* if the application chooses to copy JFIF 1.02 extension markers from135* the source file, we need to copy the version to make sure we don't136* emit a file that has 1.02 extensions but a claimed version of 1.01.137* We will *not*, however, copy version info from mislabeled "2.01" files.138*/139if (srcinfo->saw_JFIF_marker) {140if (srcinfo->JFIF_major_version == 1) {141dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;142dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;143}144dstinfo->density_unit = srcinfo->density_unit;145dstinfo->X_density = srcinfo->X_density;146dstinfo->Y_density = srcinfo->Y_density;147}148}149150151/*152* Master selection of compression modules for transcoding.153* This substitutes for jcinit.c's initialization of the full compressor.154*/155156LOCAL(void)157transencode_master_selection (j_compress_ptr cinfo,158jvirt_barray_ptr * coef_arrays)159{160/* Although we don't actually use input_components for transcoding,161* jcmaster.c's initial_setup will complain if input_components is 0.162*/163cinfo->input_components = 1;164/* Initialize master control (includes parameter checking/processing) */165jinit_c_master_control(cinfo, TRUE /* transcode only */);166167/* Entropy encoding: either Huffman or arithmetic coding. */168if (cinfo->arith_code) {169ERREXIT(cinfo, JERR_ARITH_NOTIMPL);170} else {171if (cinfo->progressive_mode) {172#ifdef C_PROGRESSIVE_SUPPORTED173jinit_phuff_encoder(cinfo);174#else175ERREXIT(cinfo, JERR_NOT_COMPILED);176#endif177} else178jinit_huff_encoder(cinfo);179}180181/* We need a special coefficient buffer controller. */182transencode_coef_controller(cinfo, coef_arrays);183184jinit_marker_writer(cinfo);185186/* We can now tell the memory manager to allocate virtual arrays. */187(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);188189/* Write the datastream header (SOI, JFIF) immediately.190* Frame and scan headers are postponed till later.191* This lets application insert special markers after the SOI.192*/193(*cinfo->marker->write_file_header) (cinfo);194}195196197/*198* The rest of this file is a special implementation of the coefficient199* buffer controller. This is similar to jccoefct.c, but it handles only200* output from presupplied virtual arrays. Furthermore, we generate any201* dummy padding blocks on-the-fly rather than expecting them to be present202* in the arrays.203*/204205/* Private buffer controller object */206207typedef struct {208struct jpeg_c_coef_controller pub; /* public fields */209210JDIMENSION iMCU_row_num; /* iMCU row # within image */211JDIMENSION mcu_ctr; /* counts MCUs processed in current row */212int MCU_vert_offset; /* counts MCU rows within iMCU row */213int MCU_rows_per_iMCU_row; /* number of such rows needed */214215/* Virtual block array for each component. */216jvirt_barray_ptr * whole_image;217218/* Workspace for constructing dummy blocks at right/bottom edges. */219JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];220} my_coef_controller;221222typedef my_coef_controller * my_coef_ptr;223224225LOCAL(void)226start_iMCU_row (j_compress_ptr cinfo)227/* Reset within-iMCU-row counters for a new row */228{229my_coef_ptr coef = (my_coef_ptr) cinfo->coef;230231/* In an interleaved scan, an MCU row is the same as an iMCU row.232* In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.233* But at the bottom of the image, process only what's left.234*/235if (cinfo->comps_in_scan > 1) {236coef->MCU_rows_per_iMCU_row = 1;237} else {238if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))239coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;240else241coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;242}243244coef->mcu_ctr = 0;245coef->MCU_vert_offset = 0;246}247248249/*250* Initialize for a processing pass.251*/252253METHODDEF(void)254start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)255{256my_coef_ptr coef = (my_coef_ptr) cinfo->coef;257258if (pass_mode != JBUF_CRANK_DEST)259ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);260261coef->iMCU_row_num = 0;262start_iMCU_row(cinfo);263}264265266/*267* Process some data.268* We process the equivalent of one fully interleaved MCU row ("iMCU" row)269* per call, ie, v_samp_factor block rows for each component in the scan.270* The data is obtained from the virtual arrays and fed to the entropy coder.271* Returns TRUE if the iMCU row is completed, FALSE if suspended.272*273* NB: input_buf is ignored; it is likely to be a NULL pointer.274*/275276METHODDEF(boolean)277compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)278{279my_coef_ptr coef = (my_coef_ptr) cinfo->coef;280JDIMENSION MCU_col_num; /* index of current MCU within row */281JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;282JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;283int blkn, ci, xindex, yindex, yoffset, blockcnt;284JDIMENSION start_col;285JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];286JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];287JBLOCKROW buffer_ptr;288jpeg_component_info *compptr;289290/* Align the virtual buffers for the components used in this scan. */291for (ci = 0; ci < cinfo->comps_in_scan; ci++) {292compptr = cinfo->cur_comp_info[ci];293buffer[ci] = (*cinfo->mem->access_virt_barray)294((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],295coef->iMCU_row_num * compptr->v_samp_factor,296(JDIMENSION) compptr->v_samp_factor, FALSE);297}298299/* Loop to process one whole iMCU row */300for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;301yoffset++) {302for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;303MCU_col_num++) {304/* Construct list of pointers to DCT blocks belonging to this MCU */305blkn = 0; /* index of current DCT block within MCU */306for (ci = 0; ci < cinfo->comps_in_scan; ci++) {307compptr = cinfo->cur_comp_info[ci];308start_col = MCU_col_num * compptr->MCU_width;309blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width310: compptr->last_col_width;311for (yindex = 0; yindex < compptr->MCU_height; yindex++) {312if (coef->iMCU_row_num < last_iMCU_row ||313yindex+yoffset < compptr->last_row_height) {314/* Fill in pointers to real blocks in this row */315buffer_ptr = buffer[ci][yindex+yoffset] + start_col;316for (xindex = 0; xindex < blockcnt; xindex++)317MCU_buffer[blkn++] = buffer_ptr++;318} else {319/* At bottom of image, need a whole row of dummy blocks */320xindex = 0;321}322/* Fill in any dummy blocks needed in this row.323* Dummy blocks are filled in the same way as in jccoefct.c:324* all zeroes in the AC entries, DC entries equal to previous325* block's DC value. The init routine has already zeroed the326* AC entries, so we need only set the DC entries correctly.327*/328for (; xindex < compptr->MCU_width; xindex++) {329MCU_buffer[blkn] = coef->dummy_buffer[blkn];330MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];331blkn++;332}333}334}335/* Try to write the MCU. */336if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {337/* Suspension forced; update state counters and exit */338coef->MCU_vert_offset = yoffset;339coef->mcu_ctr = MCU_col_num;340return FALSE;341}342}343/* Completed an MCU row, but perhaps not an iMCU row */344coef->mcu_ctr = 0;345}346/* Completed the iMCU row, advance counters for next one */347coef->iMCU_row_num++;348start_iMCU_row(cinfo);349return TRUE;350}351352353/*354* Initialize coefficient buffer controller.355*356* Each passed coefficient array must be the right size for that357* coefficient: width_in_blocks wide and height_in_blocks high,358* with unitheight at least v_samp_factor.359*/360361LOCAL(void)362transencode_coef_controller (j_compress_ptr cinfo,363jvirt_barray_ptr * coef_arrays)364{365my_coef_ptr coef;366JBLOCKROW buffer;367int i;368369coef = (my_coef_ptr)370(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,371SIZEOF(my_coef_controller));372cinfo->coef = (struct jpeg_c_coef_controller *) coef;373coef->pub.start_pass = start_pass_coef;374coef->pub.compress_data = compress_output;375376/* Save pointer to virtual arrays */377coef->whole_image = coef_arrays;378379/* Allocate and pre-zero space for dummy DCT blocks. */380buffer = (JBLOCKROW)381(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,382C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));383jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));384for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {385coef->dummy_buffer[i] = buffer + i;386}387}388389390