0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jddctmgr.c
0N/A *
0N/A * Copyright (C) 1994-1996, Thomas G. Lane.
0N/A * This file is part of the Independent JPEG Group's software.
0N/A * For conditions of distribution and use, see the accompanying README file.
0N/A *
0N/A * This file contains the inverse-DCT management logic.
0N/A * This code selects a particular IDCT implementation to be used,
0N/A * and it performs related housekeeping chores. No code in this file
0N/A * is executed per IDCT step, only during output pass setup.
0N/A *
0N/A * Note that the IDCT routines are responsible for performing coefficient
0N/A * dequantization as well as the IDCT proper. This module sets up the
0N/A * dequantization multiplier table needed by the IDCT routine.
0N/A */
0N/A
0N/A#define JPEG_INTERNALS
0N/A#include "jinclude.h"
0N/A#include "jpeglib.h"
0N/A#include "jdct.h" /* Private declarations for DCT subsystem */
0N/A
0N/A
0N/A/*
0N/A * The decompressor input side (jdinput.c) saves away the appropriate
0N/A * quantization table for each component at the start of the first scan
0N/A * involving that component. (This is necessary in order to correctly
0N/A * decode files that reuse Q-table slots.)
0N/A * When we are ready to make an output pass, the saved Q-table is converted
0N/A * to a multiplier table that will actually be used by the IDCT routine.
0N/A * The multiplier table contents are IDCT-method-dependent. To support
0N/A * application changes in IDCT method between scans, we can remake the
0N/A * multiplier tables if necessary.
0N/A * In buffered-image mode, the first output pass may occur before any data
0N/A * has been seen for some components, and thus before their Q-tables have
0N/A * been saved away. To handle this case, multiplier tables are preset
0N/A * to zeroes; the result of the IDCT will be a neutral gray level.
0N/A */
0N/A
0N/A
0N/A/* Private subobject for this module */
0N/A
0N/Atypedef struct {
0N/A struct jpeg_inverse_dct pub; /* public fields */
0N/A
0N/A /* This array contains the IDCT method code that each multiplier table
0N/A * is currently set up for, or -1 if it's not yet set up.
0N/A * The actual multiplier tables are pointed to by dct_table in the
0N/A * per-component comp_info structures.
0N/A */
0N/A int cur_method[MAX_COMPONENTS];
0N/A} my_idct_controller;
0N/A
0N/Atypedef my_idct_controller * my_idct_ptr;
0N/A
0N/A
0N/A/* Allocated multiplier tables: big enough for any supported variant */
0N/A
0N/Atypedef union {
0N/A ISLOW_MULT_TYPE islow_array[DCTSIZE2];
0N/A#ifdef DCT_IFAST_SUPPORTED
0N/A IFAST_MULT_TYPE ifast_array[DCTSIZE2];
0N/A#endif
0N/A#ifdef DCT_FLOAT_SUPPORTED
0N/A FLOAT_MULT_TYPE float_array[DCTSIZE2];
0N/A#endif
0N/A} multiplier_table;
0N/A
0N/A
0N/A/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
0N/A * so be sure to compile that code if either ISLOW or SCALING is requested.
0N/A */
0N/A#ifdef DCT_ISLOW_SUPPORTED
0N/A#define PROVIDE_ISLOW_TABLES
0N/A#else
0N/A#ifdef IDCT_SCALING_SUPPORTED
0N/A#define PROVIDE_ISLOW_TABLES
0N/A#endif
0N/A#endif
0N/A
0N/A
0N/A/*
0N/A * Prepare for an output pass.
0N/A * Here we select the proper IDCT routine for each component and build
0N/A * a matching multiplier table.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Astart_pass (j_decompress_ptr cinfo)
0N/A{
0N/A my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
0N/A int ci, i;
0N/A jpeg_component_info *compptr;
0N/A int method = 0;
0N/A inverse_DCT_method_ptr method_ptr = NULL;
0N/A JQUANT_TBL * qtbl;
0N/A
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A /* Select the proper IDCT routine for this component's scaling */
0N/A switch (compptr->DCT_scaled_size) {
0N/A#ifdef IDCT_SCALING_SUPPORTED
0N/A case 1:
0N/A method_ptr = jpeg_idct_1x1;
0N/A method = JDCT_ISLOW; /* jidctred uses islow-style table */
0N/A break;
0N/A case 2:
0N/A method_ptr = jpeg_idct_2x2;
0N/A method = JDCT_ISLOW; /* jidctred uses islow-style table */
0N/A break;
0N/A case 4:
0N/A method_ptr = jpeg_idct_4x4;
0N/A method = JDCT_ISLOW; /* jidctred uses islow-style table */
0N/A break;
0N/A#endif
0N/A case DCTSIZE:
0N/A switch (cinfo->dct_method) {
0N/A#ifdef DCT_ISLOW_SUPPORTED
0N/A case JDCT_ISLOW:
0N/A method_ptr = jpeg_idct_islow;
0N/A method = JDCT_ISLOW;
0N/A break;
0N/A#endif
0N/A#ifdef DCT_IFAST_SUPPORTED
0N/A case JDCT_IFAST:
0N/A method_ptr = jpeg_idct_ifast;
0N/A method = JDCT_IFAST;
0N/A break;
0N/A#endif
0N/A#ifdef DCT_FLOAT_SUPPORTED
0N/A case JDCT_FLOAT:
0N/A method_ptr = jpeg_idct_float;
0N/A method = JDCT_FLOAT;
0N/A break;
0N/A#endif
0N/A default:
0N/A ERREXIT(cinfo, JERR_NOT_COMPILED);
0N/A break;
0N/A }
0N/A break;
0N/A default:
0N/A ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
0N/A break;
0N/A }
0N/A idct->pub.inverse_DCT[ci] = method_ptr;
0N/A /* Create multiplier table from quant table.
0N/A * However, we can skip this if the component is uninteresting
0N/A * or if we already built the table. Also, if no quant table
0N/A * has yet been saved for the component, we leave the
0N/A * multiplier table all-zero; we'll be reading zeroes from the
0N/A * coefficient controller's buffer anyway.
0N/A */
0N/A if (! compptr->component_needed || idct->cur_method[ci] == method)
0N/A continue;
0N/A qtbl = compptr->quant_table;
0N/A if (qtbl == NULL) /* happens if no data yet for component */
0N/A continue;
0N/A idct->cur_method[ci] = method;
0N/A switch (method) {
0N/A#ifdef PROVIDE_ISLOW_TABLES
0N/A case JDCT_ISLOW:
0N/A {
0N/A /* For LL&M IDCT method, multipliers are equal to raw quantization
0N/A * coefficients, but are stored as ints to ensure access efficiency.
0N/A */
0N/A ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
0N/A for (i = 0; i < DCTSIZE2; i++) {
0N/A ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
0N/A }
0N/A }
0N/A break;
0N/A#endif
0N/A#ifdef DCT_IFAST_SUPPORTED
0N/A case JDCT_IFAST:
0N/A {
0N/A /* For AA&N IDCT method, multipliers are equal to quantization
0N/A * coefficients scaled by scalefactor[row]*scalefactor[col], where
0N/A * scalefactor[0] = 1
0N/A * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
0N/A * For integer operation, the multiplier table is to be scaled by
0N/A * IFAST_SCALE_BITS.
0N/A */
0N/A IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
0N/A#define CONST_BITS 14
0N/A static const INT16 aanscales[DCTSIZE2] = {
0N/A /* precomputed values scaled up by 14 bits */
0N/A 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
0N/A 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
0N/A 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
0N/A 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
0N/A 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
0N/A 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
0N/A 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
0N/A 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
0N/A };
0N/A SHIFT_TEMPS
0N/A
0N/A for (i = 0; i < DCTSIZE2; i++) {
0N/A ifmtbl[i] = (IFAST_MULT_TYPE)
0N/A DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
0N/A (INT32) aanscales[i]),
0N/A CONST_BITS-IFAST_SCALE_BITS);
0N/A }
0N/A }
0N/A break;
0N/A#endif
0N/A#ifdef DCT_FLOAT_SUPPORTED
0N/A case JDCT_FLOAT:
0N/A {
0N/A /* For float AA&N IDCT method, multipliers are equal to quantization
0N/A * coefficients scaled by scalefactor[row]*scalefactor[col], where
0N/A * scalefactor[0] = 1
0N/A * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
0N/A */
0N/A FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
0N/A int row, col;
0N/A static const double aanscalefactor[DCTSIZE] = {
0N/A 1.0, 1.387039845, 1.306562965, 1.175875602,
0N/A 1.0, 0.785694958, 0.541196100, 0.275899379
0N/A };
0N/A
0N/A i = 0;
0N/A for (row = 0; row < DCTSIZE; row++) {
0N/A for (col = 0; col < DCTSIZE; col++) {
0N/A fmtbl[i] = (FLOAT_MULT_TYPE)
0N/A ((double) qtbl->quantval[i] *
0N/A aanscalefactor[row] * aanscalefactor[col]);
0N/A i++;
0N/A }
0N/A }
0N/A }
0N/A break;
0N/A#endif
0N/A default:
0N/A ERREXIT(cinfo, JERR_NOT_COMPILED);
0N/A break;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Initialize IDCT manager.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajinit_inverse_dct (j_decompress_ptr cinfo)
0N/A{
0N/A my_idct_ptr idct;
0N/A int ci;
0N/A jpeg_component_info *compptr;
0N/A
0N/A idct = (my_idct_ptr)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(my_idct_controller));
0N/A cinfo->idct = (struct jpeg_inverse_dct *) idct;
0N/A idct->pub.start_pass = start_pass;
0N/A
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A /* Allocate and pre-zero a multiplier table for each component */
0N/A compptr->dct_table =
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(multiplier_table));
0N/A MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
0N/A /* Mark multiplier table not yet set up for any method */
0N/A idct->cur_method[ci] = -1;
0N/A }
0N/A}