home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jcdctmgr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  13.0 KB  |  390 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.  * jcdctmgr.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 forward-DCT management logic.
  18.  * This code selects a particular DCT implementation to be used,
  19.  * and it performs related housekeeping chores including coefficient
  20.  * quantization.
  21.  */
  22.  
  23. #define JPEG_INTERNALS
  24. #include "jinclude.h"
  25. #include "jpeglib.h"
  26. #include "jdct.h"        /* Private declarations for DCT subsystem */
  27.  
  28.  
  29. /* Private subobject for this module */
  30.  
  31. typedef struct {
  32.   struct jpeg_forward_dct pub;    /* public fields */
  33.  
  34.   /* Pointer to the DCT routine actually in use */
  35.   forward_DCT_method_ptr do_dct;
  36.  
  37.   /* The actual post-DCT divisors --- not identical to the quant table
  38.    * entries, because of scaling (especially for an unnormalized DCT).
  39.    * Each table is given in normal array order.
  40.    */
  41.   DCTELEM * divisors[NUM_QUANT_TBLS];
  42.  
  43. #ifdef DCT_FLOAT_SUPPORTED
  44.   /* Same as above for the floating-point case. */
  45.   float_DCT_method_ptr do_float_dct;
  46.   FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
  47. #endif
  48. } my_fdct_controller;
  49.  
  50. typedef my_fdct_controller * my_fdct_ptr;
  51.  
  52.  
  53. /*
  54.  * Initialize for a processing pass.
  55.  * Verify that all referenced Q-tables are present, and set up
  56.  * the divisor table for each one.
  57.  * In the current implementation, DCT of all components is done during
  58.  * the first pass, even if only some components will be output in the
  59.  * first scan.  Hence all components should be examined here.
  60.  */
  61.  
  62. void start_pass_fdctmgr (j_compress_ptr cinfo)
  63. {
  64.   my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  65.   int ci, qtblno, i;
  66.   jpeg_component_info *compptr;
  67.   JQUANT_TBL * qtbl;
  68.   DCTELEM * dtbl;
  69.  
  70.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  71.        ci++, compptr++) {
  72.     qtblno = compptr->quant_tbl_no;
  73.     /* Make sure specified quantization table is present */
  74.     if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
  75.     cinfo->quant_tbl_ptrs[qtblno] == NULL)
  76.       cinfo->ERREXIT1(JERR_NO_QUANT_TABLE, qtblno);
  77.     qtbl = cinfo->quant_tbl_ptrs[qtblno];
  78.     /* Compute divisors for this quant table */
  79.     /* We may do this more than once for same table, but it's not a big deal */
  80.     switch (cinfo->dct_method) {
  81. #ifdef DCT_ISLOW_SUPPORTED
  82.     case JDCT_ISLOW:
  83.       /* For LL&M IDCT method, divisors are equal to raw quantization
  84.        * coefficients multiplied by 8 (to counteract scaling).
  85.        */
  86.       if (fdct->divisors[qtblno] == NULL) {
  87.     fdct->divisors[qtblno] = (DCTELEM *)
  88.       cinfo->mem->alloc_small (JPOOL_IMAGE, DCTSIZE2 * sizeof(DCTELEM));
  89.       }
  90.       dtbl = fdct->divisors[qtblno];
  91.       for (i = 0; i < DCTSIZE2; i++) {
  92.     dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
  93.       }
  94.       break;
  95. #endif
  96. #ifdef DCT_IFAST_SUPPORTED
  97.     case JDCT_IFAST:
  98.       {
  99.     /* For AA&N IDCT method, divisors are equal to quantization
  100.      * coefficients scaled by scalefactor[row]*scalefactor[col], where
  101.      *   scalefactor[0] = 1
  102.      *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  103.      * We apply a further scale factor of 8.
  104.      */
  105. #define CONST_BITS 14
  106.     static const INT16 aanscales[DCTSIZE2] = {
  107.       /* precomputed values scaled up by 14 bits */
  108.       16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  109.       22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
  110.       21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
  111.       19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
  112.       16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  113.       12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
  114.        8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
  115.        4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
  116.     };
  117.     SHIFT_TEMPS
  118.  
  119.     if (fdct->divisors[qtblno] == NULL) {
  120.       fdct->divisors[qtblno] = (DCTELEM *)
  121.         cinfo->mem->alloc_small (JPOOL_IMAGE, DCTSIZE2 * sizeof(DCTELEM));
  122.     }
  123.     dtbl = fdct->divisors[qtblno];
  124.     for (i = 0; i < DCTSIZE2; i++) {
  125.       dtbl[i] = (DCTELEM)
  126.         DESCALE((long) qtbl->quantval[i] * (long) aanscales[i],
  127.             CONST_BITS-3);
  128.     }
  129.       }
  130.       break;
  131. #endif
  132. #ifdef DCT_FLOAT_SUPPORTED
  133.     case JDCT_FLOAT:
  134.       {
  135.     /* For float AA&N IDCT method, divisors are equal to quantization
  136.      * coefficients scaled by scalefactor[row]*scalefactor[col], where
  137.      *   scalefactor[0] = 1
  138.      *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  139.      * We apply a further scale factor of 8.
  140.      * What's actually stored is 1/divisor so that the inner loop can
  141.      * use a multiplication rather than a division.
  142.      */
  143.     FAST_FLOAT * fdtbl;
  144.     int row, col;
  145.     static const double aanscalefactor[DCTSIZE] = {
  146.       1.0, 1.387039845, 1.306562965, 1.175875602,
  147.       1.0, 0.785694958, 0.541196100, 0.275899379
  148.     };
  149.  
  150.     if (fdct->float_divisors[qtblno] == NULL) {
  151.       fdct->float_divisors[qtblno] = (FAST_FLOAT *)
  152.         cinfo->mem->alloc_small (JPOOL_IMAGE,
  153.                     DCTSIZE2 * sizeof(FAST_FLOAT));
  154.     }
  155.     fdtbl = fdct->float_divisors[qtblno];
  156.     i = 0;
  157.     for (row = 0; row < DCTSIZE; row++) {
  158.       for (col = 0; col < DCTSIZE; col++) {
  159.         fdtbl[i] = (FAST_FLOAT)
  160.           (1.0 / (((double) qtbl->quantval[i] *
  161.                aanscalefactor[row] * aanscalefactor[col] * 8.0)));
  162.         i++;
  163.       }
  164.     }
  165.       }
  166.       break;
  167. #endif
  168.     default:
  169.       cinfo->ERREXIT(JERR_NOT_COMPILED);
  170.       break;
  171.     }
  172.   }
  173. }
  174.  
  175.  
  176. /*
  177.  * Perform forward DCT on one or more blocks of a component.
  178.  *
  179.  * The input samples are taken from the sample_data[] array starting at
  180.  * position start_row/start_col, and moving to the right for any additional
  181.  * blocks. The quantized coefficients are returned in coef_blocks[].
  182.  */
  183.  
  184. void forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
  185.          JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  186.          JDIMENSION start_row, JDIMENSION start_col,
  187.          JDIMENSION num_blocks)
  188. /* This version is used for integer DCT implementations. */
  189. {
  190.   /* This routine is heavily used, so it's worth coding it tightly. */
  191.   my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  192.   forward_DCT_method_ptr do_dct = fdct->do_dct;
  193.   DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
  194.   DCTELEM workspace[DCTSIZE2];    /* work area for FDCT subroutine */
  195.   JDIMENSION bi;
  196.  
  197.   sample_data += start_row;    /* fold in the vertical offset once */
  198.  
  199.   for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  200.     /* Load data into workspace, applying unsigned->signed conversion */
  201.     { register DCTELEM *workspaceptr;
  202.       register JSAMPROW elemptr;
  203.       register int elemr;
  204.  
  205.       workspaceptr = workspace;
  206.       for (elemr = 0; elemr < DCTSIZE; elemr++) {
  207.     elemptr = sample_data[elemr] + start_col;
  208. #if DCTSIZE == 8        /* unroll the inner loop */
  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.     *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  217. #else
  218.     { register int elemc;
  219.       for (elemc = DCTSIZE; elemc > 0; elemc--) {
  220.         *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  221.       }
  222.     }
  223. #endif
  224.       }
  225.     }
  226.  
  227.     /* Perform the DCT */
  228.     (*do_dct) (workspace);
  229.  
  230.     /* Quantize/descale the coefficients, and store into coef_blocks[] */
  231.     { register DCTELEM temp, qval;
  232.       register int i;
  233.       register JCOEFPTR output_ptr = coef_blocks[bi];
  234.  
  235.       for (i = 0; i < DCTSIZE2; i++) {
  236.     qval = divisors[i];
  237.     temp = workspace[i];
  238.     /* Divide the coefficient value by qval, ensuring proper rounding.
  239.      * Since C does not specify the direction of rounding for negative
  240.      * quotients, we have to force the dividend positive for portability.
  241.      *
  242.      * In most files, at least half of the output values will be zero
  243.      * (at default quantization settings, more like three-quarters...)
  244.      * so we should ensure that this case is fast.  On many machines,
  245.      * a comparison is enough cheaper than a divide to make a special test
  246.      * a win.  Since both inputs will be nonnegative, we need only test
  247.      * for a < b to discover whether a/b is 0.
  248.      * If your machine's division is fast enough, define FAST_DIVIDE.
  249.      */
  250. #ifdef FAST_DIVIDE
  251. #define DIVIDE_BY(a,b)    a /= b
  252. #else
  253. #define DIVIDE_BY(a,b)    if (a >= b) a /= b; else a = 0
  254. #endif
  255.     if (temp < 0) {
  256.       temp = -temp;
  257.       temp += qval>>1;    /* for rounding */
  258.       DIVIDE_BY(temp, qval);
  259.       temp = -temp;
  260.     } else {
  261.       temp += qval>>1;    /* for rounding */
  262.       DIVIDE_BY(temp, qval);
  263.     }
  264.     output_ptr[i] = (JCOEF) temp;
  265.       }
  266.     }
  267.   }
  268. }
  269.  
  270.  
  271. #ifdef DCT_FLOAT_SUPPORTED
  272.  
  273. void forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
  274.            JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  275.            JDIMENSION start_row, JDIMENSION start_col,
  276.            JDIMENSION 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. */
  280.   my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  281.   float_DCT_method_ptr do_dct = fdct->do_float_dct;
  282.   FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
  283.   FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
  284.   JDIMENSION bi;
  285.  
  286.   sample_data += start_row;    /* fold in the vertical offset once */
  287.  
  288.   for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  289.     /* Load data into workspace, applying unsigned->signed conversion */
  290.     { register FAST_FLOAT *workspaceptr;
  291.       register JSAMPROW elemptr;
  292.       register int elemr;
  293.  
  294.       workspaceptr = workspace;
  295.       for (elemr = 0; elemr < DCTSIZE; elemr++) {
  296.     elemptr = 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. #else
  307.     { register int elemc;
  308.       for (elemc = DCTSIZE; elemc > 0; elemc--) {
  309.         *workspaceptr++ = (FAST_FLOAT)
  310.           (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  311.       }
  312.     }
  313. #endif
  314.       }
  315.     }
  316.  
  317.     /* Perform the DCT */
  318.     (*do_dct) (workspace);
  319.  
  320.     /* Quantize/descale the coefficients, and store into coef_blocks[] */
  321.     { register FAST_FLOAT temp;
  322.       register int i;
  323.       register JCOEFPTR output_ptr = coef_blocks[bi];
  324.  
  325.       for (i = 0; i < DCTSIZE2; i++) {
  326.     /* Apply the quantization and scaling factor */
  327.     temp = workspace[i] * divisors[i];
  328.     /* Round to nearest integer.
  329.      * Since C does not specify the direction of rounding for negative
  330.      * quotients, we have to force the dividend positive for portability.
  331.      * The maximum coefficient size is +-16K (for 12-bit data), so this
  332.      * code should work for either 16-bit or 32-bit ints.
  333.      */
  334.     output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
  335.       }
  336.     }
  337.   }
  338. }
  339.  
  340. #endif /* DCT_FLOAT_SUPPORTED */
  341.  
  342.  
  343. /*
  344.  * Initialize FDCT manager.
  345.  */
  346.  
  347. GLOBAL(void)
  348. jinit_forward_dct (j_compress_ptr cinfo)
  349. {
  350.   my_fdct_ptr fdct;
  351.   int i;
  352.  
  353.   fdct = (my_fdct_ptr)
  354.     cinfo->mem->alloc_small (JPOOL_IMAGE, sizeof(my_fdct_controller));
  355.   cinfo->fdct = (struct jpeg_forward_dct *) fdct;
  356.   fdct->pub.start_pass = start_pass_fdctmgr;
  357.  
  358.   switch (cinfo->dct_method) {
  359. #ifdef DCT_ISLOW_SUPPORTED
  360.   case JDCT_ISLOW:
  361.     fdct->pub.forward_DCT = forward_DCT;
  362.     fdct->do_dct = jpeg_fdct_islow;
  363.     break;
  364. #endif
  365. #ifdef DCT_IFAST_SUPPORTED
  366.   case JDCT_IFAST:
  367.     fdct->pub.forward_DCT = forward_DCT;
  368.     fdct->do_dct = jpeg_fdct_ifast;
  369.     break;
  370. #endif
  371. #ifdef DCT_FLOAT_SUPPORTED
  372.   case JDCT_FLOAT:
  373.     fdct->pub.forward_DCT = forward_DCT_float;
  374.     fdct->do_float_dct = jpeg_fdct_float;
  375.     break;
  376. #endif
  377.   default:
  378.     cinfo->ERREXIT(JERR_NOT_COMPILED);
  379.     break;
  380.   }
  381.  
  382.   /* Mark divisor tables unallocated */
  383.   for (i = 0; i < NUM_QUANT_TBLS; i++) {
  384.     fdct->divisors[i] = NULL;
  385. #ifdef DCT_FLOAT_SUPPORTED
  386.     fdct->float_divisors[i] = NULL;
  387. #endif
  388.   }
  389. }
  390.