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/jcdctmgr.c
Views: 11784
/*1* jcdctmgr.c2*3* Copyright (C) 1994-1996, 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 the forward-DCT management logic.8* This code selects a particular DCT implementation to be used,9* and it performs related housekeeping chores including coefficient10* quantization.11*/1213#define JPEG_INTERNALS14#include "jinclude.h"15#include "jpeglib.h"16#include "jdct.h" /* Private declarations for DCT subsystem */171819/* Private subobject for this module */2021typedef struct {22struct jpeg_forward_dct pub; /* public fields */2324/* Pointer to the DCT routine actually in use */25forward_DCT_method_ptr do_dct;2627/* The actual post-DCT divisors --- not identical to the quant table28* entries, because of scaling (especially for an unnormalized DCT).29* Each table is given in normal array order.30*/31DCTELEM * divisors[NUM_QUANT_TBLS];3233#ifdef DCT_FLOAT_SUPPORTED34/* Same as above for the floating-point case. */35float_DCT_method_ptr do_float_dct;36FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];37#endif38} my_fdct_controller;3940typedef my_fdct_controller * my_fdct_ptr;414243/*44* Initialize for a processing pass.45* Verify that all referenced Q-tables are present, and set up46* the divisor table for each one.47* In the current implementation, DCT of all components is done during48* the first pass, even if only some components will be output in the49* first scan. Hence all components should be examined here.50*/5152METHODDEF(void)53start_pass_fdctmgr (j_compress_ptr cinfo)54{55my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;56int ci, qtblno, i;57jpeg_component_info *compptr;58JQUANT_TBL * qtbl;59DCTELEM * dtbl;6061for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;62ci++, compptr++) {63qtblno = compptr->quant_tbl_no;64/* Make sure specified quantization table is present */65if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||66cinfo->quant_tbl_ptrs[qtblno] == NULL)67ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);68qtbl = cinfo->quant_tbl_ptrs[qtblno];69/* Compute divisors for this quant table */70/* We may do this more than once for same table, but it's not a big deal */71switch (cinfo->dct_method) {72#ifdef DCT_ISLOW_SUPPORTED73case JDCT_ISLOW:74/* For LL&M IDCT method, divisors are equal to raw quantization75* coefficients multiplied by 8 (to counteract scaling).76*/77if (fdct->divisors[qtblno] == NULL) {78fdct->divisors[qtblno] = (DCTELEM *)79(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,80DCTSIZE2 * SIZEOF(DCTELEM));81}82dtbl = fdct->divisors[qtblno];83for (i = 0; i < DCTSIZE2; i++) {84dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;85}86break;87#endif88#ifdef DCT_IFAST_SUPPORTED89case JDCT_IFAST:90{91/* For AA&N IDCT method, divisors are equal to quantization92* coefficients scaled by scalefactor[row]*scalefactor[col], where93* scalefactor[0] = 194* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..795* We apply a further scale factor of 8.96*/97#define CONST_BITS 1498static const INT16 aanscales[DCTSIZE2] = {99/* precomputed values scaled up by 14 bits */10016384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,10122725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,10221407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,10319266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,10416384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,10512873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,1068867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,1074520, 6270, 5906, 5315, 4520, 3552, 2446, 1247108};109SHIFT_TEMPS110111if (fdct->divisors[qtblno] == NULL) {112fdct->divisors[qtblno] = (DCTELEM *)113(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,114DCTSIZE2 * SIZEOF(DCTELEM));115}116dtbl = fdct->divisors[qtblno];117for (i = 0; i < DCTSIZE2; i++) {118dtbl[i] = (DCTELEM)119DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],120(INT32) aanscales[i]),121CONST_BITS-3);122}123}124break;125#endif126#ifdef DCT_FLOAT_SUPPORTED127case JDCT_FLOAT:128{129/* For float AA&N IDCT method, divisors are equal to quantization130* coefficients scaled by scalefactor[row]*scalefactor[col], where131* scalefactor[0] = 1132* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7133* We apply a further scale factor of 8.134* What's actually stored is 1/divisor so that the inner loop can135* use a multiplication rather than a division.136*/137FAST_FLOAT * fdtbl;138int row, col;139static const double aanscalefactor[DCTSIZE] = {1401.0, 1.387039845, 1.306562965, 1.175875602,1411.0, 0.785694958, 0.541196100, 0.275899379142};143144if (fdct->float_divisors[qtblno] == NULL) {145fdct->float_divisors[qtblno] = (FAST_FLOAT *)146(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,147DCTSIZE2 * SIZEOF(FAST_FLOAT));148}149fdtbl = fdct->float_divisors[qtblno];150i = 0;151for (row = 0; row < DCTSIZE; row++) {152for (col = 0; col < DCTSIZE; col++) {153fdtbl[i] = (FAST_FLOAT)154(1.0 / (((double) qtbl->quantval[i] *155aanscalefactor[row] * aanscalefactor[col] * 8.0)));156i++;157}158}159}160break;161#endif162default:163ERREXIT(cinfo, JERR_NOT_COMPILED);164break;165}166}167}168169170/*171* Perform forward DCT on one or more blocks of a component.172*173* The input samples are taken from the sample_data[] array starting at174* position start_row/start_col, and moving to the right for any additional175* blocks. The quantized coefficients are returned in coef_blocks[].176*/177178METHODDEF(void)179forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,180JSAMPARRAY sample_data, JBLOCKROW coef_blocks,181JDIMENSION start_row, JDIMENSION start_col,182JDIMENSION num_blocks)183/* This version is used for integer DCT implementations. */184{185/* This routine is heavily used, so it's worth coding it tightly. */186my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;187forward_DCT_method_ptr do_dct = fdct->do_dct;188DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];189DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */190JDIMENSION bi;191192sample_data += start_row; /* fold in the vertical offset once */193194for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {195/* Load data into workspace, applying unsigned->signed conversion */196{ register DCTELEM *workspaceptr;197register JSAMPROW elemptr;198register int elemr;199200workspaceptr = workspace;201for (elemr = 0; elemr < DCTSIZE; elemr++) {202elemptr = sample_data[elemr] + start_col;203#if DCTSIZE == 8 /* unroll the inner loop */204*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;205*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;206*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;207*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;208*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;209*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;210*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;211*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;212#else213{ register int elemc;214for (elemc = DCTSIZE; elemc > 0; elemc--) {215*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;216}217}218#endif219}220}221222/* Perform the DCT */223(*do_dct) (workspace);224225/* Quantize/descale the coefficients, and store into coef_blocks[] */226{ register DCTELEM temp, qval;227register int i;228register JCOEFPTR output_ptr = coef_blocks[bi];229230for (i = 0; i < DCTSIZE2; i++) {231qval = divisors[i];232temp = workspace[i];233/* Divide the coefficient value by qval, ensuring proper rounding.234* Since C does not specify the direction of rounding for negative235* quotients, we have to force the dividend positive for portability.236*237* In most files, at least half of the output values will be zero238* (at default quantization settings, more like three-quarters...)239* so we should ensure that this case is fast. On many machines,240* a comparison is enough cheaper than a divide to make a special test241* a win. Since both inputs will be nonnegative, we need only test242* for a < b to discover whether a/b is 0.243* If your machine's division is fast enough, define FAST_DIVIDE.244*/245#ifdef FAST_DIVIDE246#define DIVIDE_BY(a,b) a /= b247#else248#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0249#endif250if (temp < 0) {251temp = -temp;252temp += qval>>1; /* for rounding */253DIVIDE_BY(temp, qval);254temp = -temp;255} else {256temp += qval>>1; /* for rounding */257DIVIDE_BY(temp, qval);258}259output_ptr[i] = (JCOEF) temp;260}261}262}263}264265266#ifdef DCT_FLOAT_SUPPORTED267268METHODDEF(void)269forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,270JSAMPARRAY sample_data, JBLOCKROW coef_blocks,271JDIMENSION start_row, JDIMENSION start_col,272JDIMENSION num_blocks)273/* This version is used for floating-point DCT implementations. */274{275/* This routine is heavily used, so it's worth coding it tightly. */276my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;277float_DCT_method_ptr do_dct = fdct->do_float_dct;278FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];279FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */280JDIMENSION bi;281282sample_data += start_row; /* fold in the vertical offset once */283284for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {285/* Load data into workspace, applying unsigned->signed conversion */286{ register FAST_FLOAT *workspaceptr;287register JSAMPROW elemptr;288register int elemr;289290workspaceptr = workspace;291for (elemr = 0; elemr < DCTSIZE; elemr++) {292elemptr = sample_data[elemr] + start_col;293#if DCTSIZE == 8 /* unroll the inner loop */294*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);295*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);296*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);297*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);298*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);299*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);300*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);301*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);302#else303{ register int elemc;304for (elemc = DCTSIZE; elemc > 0; elemc--) {305*workspaceptr++ = (FAST_FLOAT)306(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);307}308}309#endif310}311}312313/* Perform the DCT */314(*do_dct) (workspace);315316/* Quantize/descale the coefficients, and store into coef_blocks[] */317{ register FAST_FLOAT temp;318register int i;319register JCOEFPTR output_ptr = coef_blocks[bi];320321for (i = 0; i < DCTSIZE2; i++) {322/* Apply the quantization and scaling factor */323temp = workspace[i] * divisors[i];324/* Round to nearest integer.325* Since C does not specify the direction of rounding for negative326* quotients, we have to force the dividend positive for portability.327* The maximum coefficient size is +-16K (for 12-bit data), so this328* code should work for either 16-bit or 32-bit ints.329*/330output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);331}332}333}334}335336#endif /* DCT_FLOAT_SUPPORTED */337338339/*340* Initialize FDCT manager.341*/342343GLOBAL(void)344jinit_forward_dct (j_compress_ptr cinfo)345{346my_fdct_ptr fdct;347int i;348349fdct = (my_fdct_ptr)350(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,351SIZEOF(my_fdct_controller));352cinfo->fdct = (struct jpeg_forward_dct *) fdct;353fdct->pub.start_pass = start_pass_fdctmgr;354355switch (cinfo->dct_method) {356#ifdef DCT_ISLOW_SUPPORTED357case JDCT_ISLOW:358fdct->pub.forward_DCT = forward_DCT;359fdct->do_dct = jpeg_fdct_islow;360break;361#endif362#ifdef DCT_IFAST_SUPPORTED363case JDCT_IFAST:364fdct->pub.forward_DCT = forward_DCT;365fdct->do_dct = jpeg_fdct_ifast;366break;367#endif368#ifdef DCT_FLOAT_SUPPORTED369case JDCT_FLOAT:370fdct->pub.forward_DCT = forward_DCT_float;371fdct->do_float_dct = jpeg_fdct_float;372break;373#endif374default:375ERREXIT(cinfo, JERR_NOT_COMPILED);376break;377}378379/* Mark divisor tables unallocated */380for (i = 0; i < NUM_QUANT_TBLS; i++) {381fdct->divisors[i] = NULL;382#ifdef DCT_FLOAT_SUPPORTED383fdct->float_divisors[i] = NULL;384#endif385}386}387388389