Path: blob/master/src/java.desktop/share/native/libjavajpeg/jddctmgr.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jddctmgr.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 inverse-DCT management logic.12* This code selects a particular IDCT implementation to be used,13* and it performs related housekeeping chores. No code in this file14* is executed per IDCT step, only during output pass setup.15*16* Note that the IDCT routines are responsible for performing coefficient17* dequantization as well as the IDCT proper. This module sets up the18* dequantization multiplier table needed by the IDCT routine.19*/2021#define JPEG_INTERNALS22#include "jinclude.h"23#include "jpeglib.h"24#include "jdct.h" /* Private declarations for DCT subsystem */252627/*28* The decompressor input side (jdinput.c) saves away the appropriate29* quantization table for each component at the start of the first scan30* involving that component. (This is necessary in order to correctly31* decode files that reuse Q-table slots.)32* When we are ready to make an output pass, the saved Q-table is converted33* to a multiplier table that will actually be used by the IDCT routine.34* The multiplier table contents are IDCT-method-dependent. To support35* application changes in IDCT method between scans, we can remake the36* multiplier tables if necessary.37* In buffered-image mode, the first output pass may occur before any data38* has been seen for some components, and thus before their Q-tables have39* been saved away. To handle this case, multiplier tables are preset40* to zeroes; the result of the IDCT will be a neutral gray level.41*/424344/* Private subobject for this module */4546typedef struct {47struct jpeg_inverse_dct pub; /* public fields */4849/* This array contains the IDCT method code that each multiplier table50* is currently set up for, or -1 if it's not yet set up.51* The actual multiplier tables are pointed to by dct_table in the52* per-component comp_info structures.53*/54int cur_method[MAX_COMPONENTS];55} my_idct_controller;5657typedef my_idct_controller * my_idct_ptr;585960/* Allocated multiplier tables: big enough for any supported variant */6162typedef union {63ISLOW_MULT_TYPE islow_array[DCTSIZE2];64#ifdef DCT_IFAST_SUPPORTED65IFAST_MULT_TYPE ifast_array[DCTSIZE2];66#endif67#ifdef DCT_FLOAT_SUPPORTED68FLOAT_MULT_TYPE float_array[DCTSIZE2];69#endif70} multiplier_table;717273/* The current scaled-IDCT routines require ISLOW-style multiplier tables,74* so be sure to compile that code if either ISLOW or SCALING is requested.75*/76#ifdef DCT_ISLOW_SUPPORTED77#define PROVIDE_ISLOW_TABLES78#else79#ifdef IDCT_SCALING_SUPPORTED80#define PROVIDE_ISLOW_TABLES81#endif82#endif838485/*86* Prepare for an output pass.87* Here we select the proper IDCT routine for each component and build88* a matching multiplier table.89*/9091METHODDEF(void)92start_pass (j_decompress_ptr cinfo)93{94my_idct_ptr idct = (my_idct_ptr) cinfo->idct;95int ci, i;96jpeg_component_info *compptr;97int method = 0;98inverse_DCT_method_ptr method_ptr = NULL;99JQUANT_TBL * qtbl;100101for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;102ci++, compptr++) {103/* Select the proper IDCT routine for this component's scaling */104switch (compptr->DCT_scaled_size) {105#ifdef IDCT_SCALING_SUPPORTED106case 1:107method_ptr = jpeg_idct_1x1;108method = JDCT_ISLOW; /* jidctred uses islow-style table */109break;110case 2:111method_ptr = jpeg_idct_2x2;112method = JDCT_ISLOW; /* jidctred uses islow-style table */113break;114case 4:115method_ptr = jpeg_idct_4x4;116method = JDCT_ISLOW; /* jidctred uses islow-style table */117break;118#endif119case DCTSIZE:120switch (cinfo->dct_method) {121#ifdef DCT_ISLOW_SUPPORTED122case JDCT_ISLOW:123method_ptr = jpeg_idct_islow;124method = JDCT_ISLOW;125break;126#endif127#ifdef DCT_IFAST_SUPPORTED128case JDCT_IFAST:129method_ptr = jpeg_idct_ifast;130method = JDCT_IFAST;131break;132#endif133#ifdef DCT_FLOAT_SUPPORTED134case JDCT_FLOAT:135method_ptr = jpeg_idct_float;136method = JDCT_FLOAT;137break;138#endif139default:140ERREXIT(cinfo, JERR_NOT_COMPILED);141break;142}143break;144default:145ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);146break;147}148idct->pub.inverse_DCT[ci] = method_ptr;149/* Create multiplier table from quant table.150* However, we can skip this if the component is uninteresting151* or if we already built the table. Also, if no quant table152* has yet been saved for the component, we leave the153* multiplier table all-zero; we'll be reading zeroes from the154* coefficient controller's buffer anyway.155*/156if (! compptr->component_needed || idct->cur_method[ci] == method)157continue;158qtbl = compptr->quant_table;159if (qtbl == NULL) /* happens if no data yet for component */160continue;161idct->cur_method[ci] = method;162switch (method) {163#ifdef PROVIDE_ISLOW_TABLES164case JDCT_ISLOW:165{166/* For LL&M IDCT method, multipliers are equal to raw quantization167* coefficients, but are stored as ints to ensure access efficiency.168*/169ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;170for (i = 0; i < DCTSIZE2; i++) {171ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];172}173}174break;175#endif176#ifdef DCT_IFAST_SUPPORTED177case JDCT_IFAST:178{179/* For AA&N IDCT method, multipliers are equal to quantization180* coefficients scaled by scalefactor[row]*scalefactor[col], where181* scalefactor[0] = 1182* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7183* For integer operation, the multiplier table is to be scaled by184* IFAST_SCALE_BITS.185*/186IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;187#define CONST_BITS 14188static const INT16 aanscales[DCTSIZE2] = {189/* precomputed values scaled up by 14 bits */19016384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,19122725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,19221407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,19319266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,19416384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,19512873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,1968867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,1974520, 6270, 5906, 5315, 4520, 3552, 2446, 1247198};199SHIFT_TEMPS200201for (i = 0; i < DCTSIZE2; i++) {202ifmtbl[i] = (IFAST_MULT_TYPE)203DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],204(INT32) aanscales[i]),205CONST_BITS-IFAST_SCALE_BITS);206}207}208break;209#endif210#ifdef DCT_FLOAT_SUPPORTED211case JDCT_FLOAT:212{213/* For float AA&N IDCT method, multipliers are equal to quantization214* coefficients scaled by scalefactor[row]*scalefactor[col], where215* scalefactor[0] = 1216* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7217*/218FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;219int row, col;220static const double aanscalefactor[DCTSIZE] = {2211.0, 1.387039845, 1.306562965, 1.175875602,2221.0, 0.785694958, 0.541196100, 0.275899379223};224225i = 0;226for (row = 0; row < DCTSIZE; row++) {227for (col = 0; col < DCTSIZE; col++) {228fmtbl[i] = (FLOAT_MULT_TYPE)229((double) qtbl->quantval[i] *230aanscalefactor[row] * aanscalefactor[col]);231i++;232}233}234}235break;236#endif237default:238ERREXIT(cinfo, JERR_NOT_COMPILED);239break;240}241}242}243244245/*246* Initialize IDCT manager.247*/248249GLOBAL(void)250jinit_inverse_dct (j_decompress_ptr cinfo)251{252my_idct_ptr idct;253int ci;254jpeg_component_info *compptr;255256idct = (my_idct_ptr)257(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,258SIZEOF(my_idct_controller));259cinfo->idct = (struct jpeg_inverse_dct *) idct;260idct->pub.start_pass = start_pass;261262for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;263ci++, compptr++) {264/* Allocate and pre-zero a multiplier table for each component */265compptr->dct_table =266(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,267SIZEOF(multiplier_table));268MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));269/* Mark multiplier table not yet set up for any method */270idct->cur_method[ci] = -1;271}272}273274275