Path: blob/master/src/java.desktop/share/native/libjavajpeg/jcdctmgr.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jcdctmgr.c6*7* Copyright (C) 1994-1996, 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 the forward-DCT management logic.12* This code selects a particular DCT implementation to be used,13* and it performs related housekeeping chores including coefficient14* quantization.15*/1617#define JPEG_INTERNALS18#include "jinclude.h"19#include "jpeglib.h"20#include "jdct.h" /* Private declarations for DCT subsystem */212223/* Private subobject for this module */2425typedef struct {26struct jpeg_forward_dct pub; /* public fields */2728/* Pointer to the DCT routine actually in use */29forward_DCT_method_ptr do_dct;3031/* The actual post-DCT divisors --- not identical to the quant table32* entries, because of scaling (especially for an unnormalized DCT).33* Each table is given in normal array order.34*/35DCTELEM * divisors[NUM_QUANT_TBLS];3637#ifdef DCT_FLOAT_SUPPORTED38/* Same as above for the floating-point case. */39float_DCT_method_ptr do_float_dct;40FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];41#endif42} my_fdct_controller;4344typedef my_fdct_controller * my_fdct_ptr;454647/*48* Initialize for a processing pass.49* Verify that all referenced Q-tables are present, and set up50* the divisor table for each one.51* In the current implementation, DCT of all components is done during52* the first pass, even if only some components will be output in the53* first scan. Hence all components should be examined here.54*/5556METHODDEF(void)57start_pass_fdctmgr (j_compress_ptr cinfo)58{59my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;60int ci, qtblno, i;61jpeg_component_info *compptr;62JQUANT_TBL * qtbl;63DCTELEM * dtbl;6465for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;66ci++, compptr++) {67qtblno = compptr->quant_tbl_no;68/* Make sure specified quantization table is present */69if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||70cinfo->quant_tbl_ptrs[qtblno] == NULL)71ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);72qtbl = cinfo->quant_tbl_ptrs[qtblno];73/* Compute divisors for this quant table */74/* We may do this more than once for same table, but it's not a big deal */75switch (cinfo->dct_method) {76#ifdef DCT_ISLOW_SUPPORTED77case JDCT_ISLOW:78/* For LL&M IDCT method, divisors are equal to raw quantization79* coefficients multiplied by 8 (to counteract scaling).80*/81if (fdct->divisors[qtblno] == NULL) {82fdct->divisors[qtblno] = (DCTELEM *)83(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,84DCTSIZE2 * SIZEOF(DCTELEM));85}86dtbl = fdct->divisors[qtblno];87for (i = 0; i < DCTSIZE2; i++) {88dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;89}90break;91#endif92#ifdef DCT_IFAST_SUPPORTED93case JDCT_IFAST:94{95/* For AA&N IDCT method, divisors are equal to quantization96* coefficients scaled by scalefactor[row]*scalefactor[col], where97* scalefactor[0] = 198* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..799* We apply a further scale factor of 8.100*/101#define CONST_BITS 14102static const INT16 aanscales[DCTSIZE2] = {103/* precomputed values scaled up by 14 bits */10416384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,10522725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,10621407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,10719266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,10816384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,10912873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,1108867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,1114520, 6270, 5906, 5315, 4520, 3552, 2446, 1247112};113SHIFT_TEMPS114115if (fdct->divisors[qtblno] == NULL) {116fdct->divisors[qtblno] = (DCTELEM *)117(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,118DCTSIZE2 * SIZEOF(DCTELEM));119}120dtbl = fdct->divisors[qtblno];121for (i = 0; i < DCTSIZE2; i++) {122dtbl[i] = (DCTELEM)123DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],124(INT32) aanscales[i]),125CONST_BITS-3);126}127}128break;129#endif130#ifdef DCT_FLOAT_SUPPORTED131case JDCT_FLOAT:132{133/* For float AA&N IDCT method, divisors are equal to quantization134* coefficients scaled by scalefactor[row]*scalefactor[col], where135* scalefactor[0] = 1136* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7137* We apply a further scale factor of 8.138* What's actually stored is 1/divisor so that the inner loop can139* use a multiplication rather than a division.140*/141FAST_FLOAT * fdtbl;142int row, col;143static const double aanscalefactor[DCTSIZE] = {1441.0, 1.387039845, 1.306562965, 1.175875602,1451.0, 0.785694958, 0.541196100, 0.275899379146};147148if (fdct->float_divisors[qtblno] == NULL) {149fdct->float_divisors[qtblno] = (FAST_FLOAT *)150(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,151DCTSIZE2 * SIZEOF(FAST_FLOAT));152}153fdtbl = fdct->float_divisors[qtblno];154i = 0;155for (row = 0; row < DCTSIZE; row++) {156for (col = 0; col < DCTSIZE; col++) {157fdtbl[i] = (FAST_FLOAT)158(1.0 / (((double) qtbl->quantval[i] *159aanscalefactor[row] * aanscalefactor[col] * 8.0)));160i++;161}162}163}164break;165#endif166default:167ERREXIT(cinfo, JERR_NOT_COMPILED);168break;169}170}171}172173174/*175* Perform forward DCT on one or more blocks of a component.176*177* The input samples are taken from the sample_data[] array starting at178* position start_row/start_col, and moving to the right for any additional179* blocks. The quantized coefficients are returned in coef_blocks[].180*/181182METHODDEF(void)183forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,184JSAMPARRAY sample_data, JBLOCKROW coef_blocks,185JDIMENSION start_row, JDIMENSION start_col,186JDIMENSION num_blocks)187/* This version is used for integer DCT implementations. */188{189/* This routine is heavily used, so it's worth coding it tightly. */190my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;191forward_DCT_method_ptr do_dct = fdct->do_dct;192DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];193DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */194JDIMENSION bi;195196sample_data += start_row; /* fold in the vertical offset once */197198for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {199/* Load data into workspace, applying unsigned->signed conversion */200{ register DCTELEM *workspaceptr;201register JSAMPROW elemptr;202register int elemr;203204workspaceptr = workspace;205for (elemr = 0; elemr < DCTSIZE; elemr++) {206elemptr = sample_data[elemr] + start_col;207#if DCTSIZE == 8 /* unroll the inner loop */208*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;209*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;210*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;211*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;212*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;213*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;214*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;215*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;216#else217{ register int elemc;218for (elemc = DCTSIZE; elemc > 0; elemc--) {219*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;220}221}222#endif223}224}225226/* Perform the DCT */227(*do_dct) (workspace);228229/* Quantize/descale the coefficients, and store into coef_blocks[] */230{ register DCTELEM temp, qval;231register int i;232register JCOEFPTR output_ptr = coef_blocks[bi];233234for (i = 0; i < DCTSIZE2; i++) {235qval = divisors[i];236temp = workspace[i];237/* Divide the coefficient value by qval, ensuring proper rounding.238* Since C does not specify the direction of rounding for negative239* quotients, we have to force the dividend positive for portability.240*241* In most files, at least half of the output values will be zero242* (at default quantization settings, more like three-quarters...)243* so we should ensure that this case is fast. On many machines,244* a comparison is enough cheaper than a divide to make a special test245* a win. Since both inputs will be nonnegative, we need only test246* for a < b to discover whether a/b is 0.247* If your machine's division is fast enough, define FAST_DIVIDE.248*/249#ifdef FAST_DIVIDE250#define DIVIDE_BY(a,b) a /= b251#else252#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0253#endif254if (temp < 0) {255temp = -temp;256temp += qval>>1; /* for rounding */257DIVIDE_BY(temp, qval);258temp = -temp;259} else {260temp += qval>>1; /* for rounding */261DIVIDE_BY(temp, qval);262}263output_ptr[i] = (JCOEF) temp;264}265}266}267}268269270#ifdef DCT_FLOAT_SUPPORTED271272METHODDEF(void)273forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,274JSAMPARRAY sample_data, JBLOCKROW coef_blocks,275JDIMENSION start_row, JDIMENSION start_col,276JDIMENSION num_blocks)277/* This version is used for floating-point DCT implementations. */278{279/* This routine is heavily used, so it's worth coding it tightly. */280my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;281float_DCT_method_ptr do_dct = fdct->do_float_dct;282FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];283FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */284JDIMENSION bi;285286sample_data += start_row; /* fold in the vertical offset once */287288for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {289/* Load data into workspace, applying unsigned->signed conversion */290{ register FAST_FLOAT *workspaceptr;291register JSAMPROW elemptr;292register int elemr;293294workspaceptr = workspace;295for (elemr = 0; elemr < DCTSIZE; elemr++) {296elemptr = sample_data[elemr] + start_col;297#if DCTSIZE == 8 /* unroll the inner loop */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*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);303*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);304*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);305*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);306#else307{ register int elemc;308for (elemc = DCTSIZE; elemc > 0; elemc--) {309*workspaceptr++ = (FAST_FLOAT)310(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);311}312}313#endif314}315}316317/* Perform the DCT */318(*do_dct) (workspace);319320/* Quantize/descale the coefficients, and store into coef_blocks[] */321{ register FAST_FLOAT temp;322register int i;323register JCOEFPTR output_ptr = coef_blocks[bi];324325for (i = 0; i < DCTSIZE2; i++) {326/* Apply the quantization and scaling factor */327temp = workspace[i] * divisors[i];328/* Round to nearest integer.329* Since C does not specify the direction of rounding for negative330* quotients, we have to force the dividend positive for portability.331* The maximum coefficient size is +-16K (for 12-bit data), so this332* code should work for either 16-bit or 32-bit ints.333*/334output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);335}336}337}338}339340#endif /* DCT_FLOAT_SUPPORTED */341342343/*344* Initialize FDCT manager.345*/346347GLOBAL(void)348jinit_forward_dct (j_compress_ptr cinfo)349{350my_fdct_ptr fdct;351int i;352353fdct = (my_fdct_ptr)354(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,355SIZEOF(my_fdct_controller));356cinfo->fdct = (struct jpeg_forward_dct *) fdct;357fdct->pub.start_pass = start_pass_fdctmgr;358359switch (cinfo->dct_method) {360#ifdef DCT_ISLOW_SUPPORTED361case JDCT_ISLOW:362fdct->pub.forward_DCT = forward_DCT;363fdct->do_dct = jpeg_fdct_islow;364break;365#endif366#ifdef DCT_IFAST_SUPPORTED367case JDCT_IFAST:368fdct->pub.forward_DCT = forward_DCT;369fdct->do_dct = jpeg_fdct_ifast;370break;371#endif372#ifdef DCT_FLOAT_SUPPORTED373case JDCT_FLOAT:374fdct->pub.forward_DCT = forward_DCT_float;375fdct->do_float_dct = jpeg_fdct_float;376break;377#endif378default:379ERREXIT(cinfo, JERR_NOT_COMPILED);380break;381}382383/* Mark divisor tables unallocated */384for (i = 0; i < NUM_QUANT_TBLS; i++) {385fdct->divisors[i] = NULL;386#ifdef DCT_FLOAT_SUPPORTED387fdct->float_divisors[i] = NULL;388#endif389}390}391392393