home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jddctmgr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  8.9 KB  |  275 lines

  1. //-------------------------------------------------------------------------//
  2. //          Windows Graphics Programming: Win32 GDI and DirectDraw         //
  3. //                        ISBN  0-13-086985-6                              //
  4. //                                                                         //
  5. //  Modified by: Yuan, Feng                             www.fengyuan.com   //
  6. //  Changes    : C++, exception, in-memory source, BGR byte order          //
  7. //  Version    : 1.00.000, May 31, 2000                                    //
  8. //-------------------------------------------------------------------------//
  9.  
  10. /*
  11.  * jddctmgr.c
  12.  *
  13.  * Copyright (C) 1994-1996, Thomas G. Lane.
  14.  * This file is part of the Independent JPEG Group's software.
  15.  * For conditions of distribution and use, see the accompanying README file.
  16.  *
  17.  * This file contains the inverse-DCT management logic.
  18.  * This code selects a particular IDCT implementation to be used,
  19.  * and it performs related housekeeping chores.  No code in this file
  20.  * is executed per IDCT step, only during output pass setup.
  21.  *
  22.  * Note that the IDCT routines are responsible for performing coefficient
  23.  * dequantization as well as the IDCT proper.  This module sets up the
  24.  * dequantization multiplier table needed by the IDCT routine.
  25.  */
  26.  
  27. #define JPEG_INTERNALS
  28. #include "jinclude.h"
  29. #include "jpeglib.h"
  30. #include "jdct.h"        /* Private declarations for DCT subsystem */
  31.  
  32.  
  33. /*
  34.  * The decompressor input side (jdinput.c) saves away the appropriate
  35.  * quantization table for each component at the start of the first scan
  36.  * involving that component.  (This is necessary in order to correctly
  37.  * decode files that reuse Q-table slots.)
  38.  * When we are ready to make an output pass, the saved Q-table is converted
  39.  * to a multiplier table that will actually be used by the IDCT routine.
  40.  * The multiplier table contents are IDCT-method-dependent.  To support
  41.  * application changes in IDCT method between scans, we can remake the
  42.  * multiplier tables if necessary.
  43.  * In buffered-image mode, the first output pass may occur before any data
  44.  * has been seen for some components, and thus before their Q-tables have
  45.  * been saved away.  To handle this case, multiplier tables are preset
  46.  * to zeroes; the result of the IDCT will be a neutral gray level.
  47.  */
  48.  
  49.  
  50. /* Private subobject for this module */
  51.  
  52. typedef struct {
  53.   struct jpeg_inverse_dct pub;    /* public fields */
  54.  
  55.   /* This array contains the IDCT method code that each multiplier table
  56.    * is currently set up for, or -1 if it's not yet set up.
  57.    * The actual multiplier tables are pointed to by dct_table in the
  58.    * per-component comp_info structures.
  59.    */
  60.   int cur_method[MAX_COMPONENTS];
  61. } my_idct_controller;
  62.  
  63. typedef my_idct_controller * my_idct_ptr;
  64.  
  65.  
  66. /* Allocated multiplier tables: big enough for any supported variant */
  67.  
  68. typedef union {
  69.   ISLOW_MULT_TYPE islow_array[DCTSIZE2];
  70. #ifdef DCT_IFAST_SUPPORTED
  71.   IFAST_MULT_TYPE ifast_array[DCTSIZE2];
  72. #endif
  73. #ifdef DCT_FLOAT_SUPPORTED
  74.   FLOAT_MULT_TYPE float_array[DCTSIZE2];
  75. #endif
  76. } multiplier_table;
  77.  
  78.  
  79. /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
  80.  * so be sure to compile that code if either ISLOW or SCALING is requested.
  81.  */
  82. #ifdef DCT_ISLOW_SUPPORTED
  83. #define PROVIDE_ISLOW_TABLES
  84. #else
  85. #ifdef IDCT_SCALING_SUPPORTED
  86. #define PROVIDE_ISLOW_TABLES
  87. #endif
  88. #endif
  89.  
  90.  
  91. /*
  92.  * Prepare for an output pass.
  93.  * Here we select the proper IDCT routine for each component and build
  94.  * a matching multiplier table.
  95.  */
  96.  
  97. void start_pass (j_decompress_ptr cinfo)
  98. {
  99.   my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
  100.   int ci, i;
  101.   jpeg_component_info *compptr;
  102.   int method = 0;
  103.   inverse_DCT_method_ptr method_ptr = NULL;
  104.   JQUANT_TBL * qtbl;
  105.  
  106.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  107.        ci++, compptr++) {
  108.     /* Select the proper IDCT routine for this component's scaling */
  109.     switch (compptr->DCT_scaled_size) {
  110. #ifdef IDCT_SCALING_SUPPORTED
  111.     case 1:
  112.       method_ptr = jpeg_idct_1x1;
  113.       method = JDCT_ISLOW;    /* jidctred uses islow-style table */
  114.       break;
  115.     case 2:
  116.       method_ptr = jpeg_idct_2x2;
  117.       method = JDCT_ISLOW;    /* jidctred uses islow-style table */
  118.       break;
  119.     case 4:
  120.       method_ptr = jpeg_idct_4x4;
  121.       method = JDCT_ISLOW;    /* jidctred uses islow-style table */
  122.       break;
  123. #endif
  124.     case DCTSIZE:
  125.       switch (cinfo->dct_method) {
  126. #ifdef DCT_ISLOW_SUPPORTED
  127.       case JDCT_ISLOW:
  128.     method_ptr = jpeg_idct_islow;
  129.     method = JDCT_ISLOW;
  130.     break;
  131. #endif
  132. #ifdef DCT_IFAST_SUPPORTED
  133.       case JDCT_IFAST:
  134.     method_ptr = jpeg_idct_ifast;
  135.     method = JDCT_IFAST;
  136.     break;
  137. #endif
  138. #ifdef DCT_FLOAT_SUPPORTED
  139.       case JDCT_FLOAT:
  140.     method_ptr = jpeg_idct_float;
  141.     method = JDCT_FLOAT;
  142.     break;
  143. #endif
  144.       default:
  145.     cinfo->ERREXIT(JERR_NOT_COMPILED);
  146.     break;
  147.       }
  148.       break;
  149.     default:
  150.       cinfo->ERREXIT1(JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
  151.       break;
  152.     }
  153.     idct->pub.inverse_DCT[ci] = method_ptr;
  154.     /* Create multiplier table from quant table.
  155.      * However, we can skip this if the component is uninteresting
  156.      * or if we already built the table.  Also, if no quant table
  157.      * has yet been saved for the component, we leave the
  158.      * multiplier table all-zero; we'll be reading zeroes from the
  159.      * coefficient controller's buffer anyway.
  160.      */
  161.     if (! compptr->component_needed || idct->cur_method[ci] == method)
  162.       continue;
  163.     qtbl = compptr->quant_table;
  164.     if (qtbl == NULL)        /* happens if no data yet for component */
  165.       continue;
  166.     idct->cur_method[ci] = method;
  167.     switch (method) {
  168. #ifdef PROVIDE_ISLOW_TABLES
  169.     case JDCT_ISLOW:
  170.       {
  171.     /* For LL&M IDCT method, multipliers are equal to raw quantization
  172.      * coefficients, but are stored as ints to ensure access efficiency.
  173.      */
  174.     ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
  175.     for (i = 0; i < DCTSIZE2; i++) {
  176.       ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
  177.     }
  178.       }
  179.       break;
  180. #endif
  181. #ifdef DCT_IFAST_SUPPORTED
  182.     case JDCT_IFAST:
  183.       {
  184.     /* For AA&N IDCT method, multipliers are equal to quantization
  185.      * coefficients scaled by scalefactor[row]*scalefactor[col], where
  186.      *   scalefactor[0] = 1
  187.      *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  188.      * For integer operation, the multiplier table is to be scaled by
  189.      * IFAST_SCALE_BITS.
  190.      */
  191.     IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
  192. #define CONST_BITS 14
  193.     static const INT16 aanscales[DCTSIZE2] = {
  194.       /* precomputed values scaled up by 14 bits */
  195.       16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  196.       22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
  197.       21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
  198.       19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
  199.       16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  200.       12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
  201.        8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
  202.        4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
  203.     };
  204.     SHIFT_TEMPS
  205.  
  206.     for (i = 0; i < DCTSIZE2; i++) {
  207.       ifmtbl[i] = (IFAST_MULT_TYPE)
  208.         DESCALE((long) qtbl->quantval[i] * (long) aanscales[i],
  209.             CONST_BITS-IFAST_SCALE_BITS);
  210.     }
  211.       }
  212.       break;
  213. #endif
  214. #ifdef DCT_FLOAT_SUPPORTED
  215.     case JDCT_FLOAT:
  216.       {
  217.     /* For float AA&N IDCT method, multipliers are equal to quantization
  218.      * coefficients scaled by scalefactor[row]*scalefactor[col], where
  219.      *   scalefactor[0] = 1
  220.      *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  221.      */
  222.     FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
  223.     int row, col;
  224.     static const double aanscalefactor[DCTSIZE] = {
  225.       1.0, 1.387039845, 1.306562965, 1.175875602,
  226.       1.0, 0.785694958, 0.541196100, 0.275899379
  227.     };
  228.  
  229.     i = 0;
  230.     for (row = 0; row < DCTSIZE; row++) {
  231.       for (col = 0; col < DCTSIZE; col++) {
  232.         fmtbl[i] = (FLOAT_MULT_TYPE)
  233.           ((double) qtbl->quantval[i] *
  234.            aanscalefactor[row] * aanscalefactor[col]);
  235.         i++;
  236.       }
  237.     }
  238.       }
  239.       break;
  240. #endif
  241.     default:
  242.       cinfo->ERREXIT(JERR_NOT_COMPILED);
  243.       break;
  244.     }
  245.   }
  246. }
  247.  
  248.  
  249. /*
  250.  * Initialize IDCT manager.
  251.  */
  252.  
  253. GLOBAL(void)
  254. jinit_inverse_dct (j_decompress_ptr cinfo)
  255. {
  256.   my_idct_ptr idct;
  257.   int ci;
  258.   jpeg_component_info *compptr;
  259.  
  260.   idct = (my_idct_ptr)
  261.     cinfo->mem->alloc_small(JPOOL_IMAGE, sizeof(my_idct_controller));
  262.   cinfo->idct = (struct jpeg_inverse_dct *) idct;
  263.   idct->pub.start_pass = start_pass;
  264.  
  265.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  266.        ci++, compptr++) {
  267.     /* Allocate and pre-zero a multiplier table for each component */
  268.     compptr->dct_table =
  269.       cinfo->mem->alloc_small(JPOOL_IMAGE, sizeof(multiplier_table));
  270.     memset(compptr->dct_table, 0, sizeof(multiplier_table));
  271.     /* Mark multiplier table not yet set up for any method */
  272.     idct->cur_method[ci] = -1;
  273.   }
  274. }
  275.