Back to index

plt-scheme  4.2.1
jddctmgr.c
Go to the documentation of this file.
00001 /*
00002  * jddctmgr.c
00003  *
00004  * Copyright (C) 1994-1996, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains the inverse-DCT management logic.
00009  * This code selects a particular IDCT implementation to be used,
00010  * and it performs related housekeeping chores.  No code in this file
00011  * is executed per IDCT step, only during output pass setup.
00012  *
00013  * Note that the IDCT routines are responsible for performing coefficient
00014  * dequantization as well as the IDCT proper.  This module sets up the
00015  * dequantization multiplier table needed by the IDCT routine.
00016  */
00017 
00018 #define JPEG_INTERNALS
00019 #include "jinclude.h"
00020 #include "jpeglib.h"
00021 #include "jdct.h"           /* Private declarations for DCT subsystem */
00022 
00023 
00024 /*
00025  * The decompressor input side (jdinput.c) saves away the appropriate
00026  * quantization table for each component at the start of the first scan
00027  * involving that component.  (This is necessary in order to correctly
00028  * decode files that reuse Q-table slots.)
00029  * When we are ready to make an output pass, the saved Q-table is converted
00030  * to a multiplier table that will actually be used by the IDCT routine.
00031  * The multiplier table contents are IDCT-method-dependent.  To support
00032  * application changes in IDCT method between scans, we can remake the
00033  * multiplier tables if necessary.
00034  * In buffered-image mode, the first output pass may occur before any data
00035  * has been seen for some components, and thus before their Q-tables have
00036  * been saved away.  To handle this case, multiplier tables are preset
00037  * to zeroes; the result of the IDCT will be a neutral gray level.
00038  */
00039 
00040 
00041 /* Private subobject for this module */
00042 
00043 typedef struct {
00044   struct jpeg_inverse_dct pub;     /* public fields */
00045 
00046   /* This array contains the IDCT method code that each multiplier table
00047    * is currently set up for, or -1 if it's not yet set up.
00048    * The actual multiplier tables are pointed to by dct_table in the
00049    * per-component comp_info structures.
00050    */
00051   int cur_method[MAX_COMPONENTS];
00052 } my_idct_controller;
00053 
00054 typedef my_idct_controller * my_idct_ptr;
00055 
00056 
00057 /* Allocated multiplier tables: big enough for any supported variant */
00058 
00059 typedef union {
00060   ISLOW_MULT_TYPE islow_array[DCTSIZE2];
00061 #ifdef DCT_IFAST_SUPPORTED
00062   IFAST_MULT_TYPE ifast_array[DCTSIZE2];
00063 #endif
00064 #ifdef DCT_FLOAT_SUPPORTED
00065   FLOAT_MULT_TYPE float_array[DCTSIZE2];
00066 #endif
00067 } multiplier_table;
00068 
00069 
00070 /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
00071  * so be sure to compile that code if either ISLOW or SCALING is requested.
00072  */
00073 #ifdef DCT_ISLOW_SUPPORTED
00074 #define PROVIDE_ISLOW_TABLES
00075 #else
00076 #ifdef IDCT_SCALING_SUPPORTED
00077 #define PROVIDE_ISLOW_TABLES
00078 #endif
00079 #endif
00080 
00081 
00082 /*
00083  * Prepare for an output pass.
00084  * Here we select the proper IDCT routine for each component and build
00085  * a matching multiplier table.
00086  */
00087 
00088 METHODDEF(void)
00089 start_pass (j_decompress_ptr cinfo)
00090 {
00091   my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
00092   int ci, i;
00093   jpeg_component_info *compptr;
00094   int method = 0;
00095   inverse_DCT_method_ptr method_ptr = NULL;
00096   JQUANT_TBL * qtbl;
00097 
00098   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00099        ci++, compptr++) {
00100     /* Select the proper IDCT routine for this component's scaling */
00101     switch (compptr->DCT_scaled_size) {
00102 #ifdef IDCT_SCALING_SUPPORTED
00103     case 1:
00104       method_ptr = jpeg_idct_1x1;
00105       method = JDCT_ISLOW;  /* jidctred uses islow-style table */
00106       break;
00107     case 2:
00108       method_ptr = jpeg_idct_2x2;
00109       method = JDCT_ISLOW;  /* jidctred uses islow-style table */
00110       break;
00111     case 4:
00112       method_ptr = jpeg_idct_4x4;
00113       method = JDCT_ISLOW;  /* jidctred uses islow-style table */
00114       break;
00115 #endif
00116     case DCTSIZE:
00117       switch (cinfo->dct_method) {
00118 #ifdef DCT_ISLOW_SUPPORTED
00119       case JDCT_ISLOW:
00120        method_ptr = jpeg_idct_islow;
00121        method = JDCT_ISLOW;
00122        break;
00123 #endif
00124 #ifdef DCT_IFAST_SUPPORTED
00125       case JDCT_IFAST:
00126        method_ptr = jpeg_idct_ifast;
00127        method = JDCT_IFAST;
00128        break;
00129 #endif
00130 #ifdef DCT_FLOAT_SUPPORTED
00131       case JDCT_FLOAT:
00132        method_ptr = jpeg_idct_float;
00133        method = JDCT_FLOAT;
00134        break;
00135 #endif
00136       default:
00137        ERREXIT(cinfo, JERR_NOT_COMPILED);
00138        break;
00139       }
00140       break;
00141     default:
00142       ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
00143       break;
00144     }
00145     idct->pub.inverse_DCT[ci] = method_ptr;
00146     /* Create multiplier table from quant table.
00147      * However, we can skip this if the component is uninteresting
00148      * or if we already built the table.  Also, if no quant table
00149      * has yet been saved for the component, we leave the
00150      * multiplier table all-zero; we'll be reading zeroes from the
00151      * coefficient controller's buffer anyway.
00152      */
00153     if (! compptr->component_needed || idct->cur_method[ci] == method)
00154       continue;
00155     qtbl = compptr->quant_table;
00156     if (qtbl == NULL)              /* happens if no data yet for component */
00157       continue;
00158     idct->cur_method[ci] = method;
00159     switch (method) {
00160 #ifdef PROVIDE_ISLOW_TABLES
00161     case JDCT_ISLOW:
00162       {
00163        /* For LL&M IDCT method, multipliers are equal to raw quantization
00164         * coefficients, but are stored as ints to ensure access efficiency.
00165         */
00166        ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
00167        for (i = 0; i < DCTSIZE2; i++) {
00168          ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
00169        }
00170       }
00171       break;
00172 #endif
00173 #ifdef DCT_IFAST_SUPPORTED
00174     case JDCT_IFAST:
00175       {
00176        /* For AA&N IDCT method, multipliers are equal to quantization
00177         * coefficients scaled by scalefactor[row]*scalefactor[col], where
00178         *   scalefactor[0] = 1
00179         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
00180         * For integer operation, the multiplier table is to be scaled by
00181         * IFAST_SCALE_BITS.
00182         */
00183        IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
00184 #define CONST_BITS 14
00185        static const INT16 aanscales[DCTSIZE2] = {
00186          /* precomputed values scaled up by 14 bits */
00187          16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
00188          22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
00189          21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
00190          19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
00191          16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
00192          12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
00193           8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
00194           4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
00195        };
00196        SHIFT_TEMPS
00197 
00198        for (i = 0; i < DCTSIZE2; i++) {
00199          ifmtbl[i] = (IFAST_MULT_TYPE)
00200            DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
00201                               (INT32) aanscales[i]),
00202                   CONST_BITS-IFAST_SCALE_BITS);
00203        }
00204       }
00205       break;
00206 #endif
00207 #ifdef DCT_FLOAT_SUPPORTED
00208     case JDCT_FLOAT:
00209       {
00210        /* For float AA&N IDCT method, multipliers are equal to quantization
00211         * coefficients scaled by scalefactor[row]*scalefactor[col], where
00212         *   scalefactor[0] = 1
00213         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
00214         */
00215        FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
00216        int row, col;
00217        static const double aanscalefactor[DCTSIZE] = {
00218          1.0, 1.387039845, 1.306562965, 1.175875602,
00219          1.0, 0.785694958, 0.541196100, 0.275899379
00220        };
00221 
00222        i = 0;
00223        for (row = 0; row < DCTSIZE; row++) {
00224          for (col = 0; col < DCTSIZE; col++) {
00225            fmtbl[i] = (FLOAT_MULT_TYPE)
00226              ((double) qtbl->quantval[i] *
00227               aanscalefactor[row] * aanscalefactor[col]);
00228            i++;
00229          }
00230        }
00231       }
00232       break;
00233 #endif
00234     default:
00235       ERREXIT(cinfo, JERR_NOT_COMPILED);
00236       break;
00237     }
00238   }
00239 }
00240 
00241 
00242 /*
00243  * Initialize IDCT manager.
00244  */
00245 
00246 GLOBAL(void)
00247 jinit_inverse_dct (j_decompress_ptr cinfo)
00248 {
00249   my_idct_ptr idct;
00250   int ci;
00251   jpeg_component_info *compptr;
00252 
00253   idct = (my_idct_ptr)
00254     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00255                             SIZEOF(my_idct_controller));
00256   cinfo->idct = (struct jpeg_inverse_dct *) idct;
00257   idct->pub.start_pass = start_pass;
00258 
00259   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00260        ci++, compptr++) {
00261     /* Allocate and pre-zero a multiplier table for each component */
00262     compptr->dct_table =
00263       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00264                               SIZEOF(multiplier_table));
00265     MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
00266     /* Mark multiplier table not yet set up for any method */
00267     idct->cur_method[ci] = -1;
00268   }
00269 }