home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jidctint.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  15.0 KB  |  381 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.  * jidctint.c
  12.  *
  13.  * Copyright (C) 1991-1998, 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 a slow-but-accurate integer implementation of the
  18.  * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
  19.  * must also perform dequantization of the input coefficients.
  20.  *
  21.  * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  22.  * on each row (or vice versa, but it's more convenient to emit a row at
  23.  * a time).  Direct algorithms are also available, but they are much more
  24.  * complex and seem not to be any faster when reduced to code.
  25.  *
  26.  * This implementation is based on an algorithm described in
  27.  *   C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
  28.  *   Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
  29.  *   Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
  30.  * The primary algorithm described there uses 11 multiplies and 29 adds.
  31.  * We use their alternate method with 12 multiplies and 32 adds.
  32.  * The advantage of this method is that no data path contains more than one
  33.  * multiplication; this allows a very simple and accurate implementation in
  34.  * scaled fixed-point arithmetic, with a minimal number of shifts.
  35.  */
  36.  
  37. #define JPEG_INTERNALS
  38. #include "jinclude.h"
  39. #include "jpeglib.h"
  40. #include "jdct.h"        /* Private declarations for DCT subsystem */
  41.  
  42. #ifdef DCT_ISLOW_SUPPORTED
  43.  
  44.  
  45. /*
  46.  * This module is specialized to the case DCTSIZE = 8.
  47.  */
  48.  
  49. #if DCTSIZE != 8
  50.   Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  51. #endif
  52.  
  53.  
  54. /*
  55.  * The poop on this scaling stuff is as follows:
  56.  *
  57.  * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
  58.  * larger than the true IDCT outputs.  The final outputs are therefore
  59.  * a factor of N larger than desired; since N=8 this can be cured by
  60.  * a simple right shift at the end of the algorithm.  The advantage of
  61.  * this arrangement is that we save two multiplications per 1-D IDCT,
  62.  * because the y0 and y4 inputs need not be divided by sqrt(N).
  63.  *
  64.  * We have to do addition and subtraction of the integer inputs, which
  65.  * is no problem, and multiplication by fractional constants, which is
  66.  * a problem to do in integer arithmetic.  We multiply all the constants
  67.  * by CONST_SCALE and convert them to integer constants (thus retaining
  68.  * CONST_BITS bits of precision in the constants).  After doing a
  69.  * multiplication we have to divide the product by CONST_SCALE, with proper
  70.  * rounding, to produce the correct output.  This division can be done
  71.  * cheaply as a right shift of CONST_BITS bits.  We postpone shifting
  72.  * as long as possible so that partial sums can be added together with
  73.  * full fractional precision.
  74.  *
  75.  * The outputs of the first pass are scaled up by PASS1_BITS bits so that
  76.  * they are represented to better-than-integral precision.  These outputs
  77.  * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
  78.  * with the recommended scaling.  (To scale up 12-bit sample data further, an
  79.  * intermediate long array would be needed.)
  80.  *
  81.  * To avoid overflow of the 32-bit intermediate results in pass 2, we must
  82.  * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26.  Error analysis
  83.  * shows that the values given below are the most effective.
  84.  */
  85.  
  86. #if BITS_IN_JSAMPLE == 8
  87. #define CONST_BITS  13
  88. #define PASS1_BITS  2
  89. #else
  90. #define CONST_BITS  13
  91. #define PASS1_BITS  1        /* lose a little precision to avoid overflow */
  92. #endif
  93.  
  94. /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  95.  * causing a lot of useless floating-point operations at run time.
  96.  * To get around this we use the following pre-calculated constants.
  97.  * If you change CONST_BITS you may want to add appropriate values.
  98.  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
  99.  */
  100.  
  101. #if CONST_BITS == 13
  102. #define FIX_0_298631336  ((long)  2446)    /* FIX(0.298631336) */
  103. #define FIX_0_390180644  ((long)  3196)    /* FIX(0.390180644) */
  104. #define FIX_0_541196100  ((long)  4433)    /* FIX(0.541196100) */
  105. #define FIX_0_765366865  ((long)  6270)    /* FIX(0.765366865) */
  106. #define FIX_0_899976223  ((long)  7373)    /* FIX(0.899976223) */
  107. #define FIX_1_175875602  ((long)  9633)    /* FIX(1.175875602) */
  108. #define FIX_1_501321110  ((long)  12299)    /* FIX(1.501321110) */
  109. #define FIX_1_847759065  ((long)  15137)    /* FIX(1.847759065) */
  110. #define FIX_1_961570560  ((long)  16069)    /* FIX(1.961570560) */
  111. #define FIX_2_053119869  ((long)  16819)    /* FIX(2.053119869) */
  112. #define FIX_2_562915447  ((long)  20995)    /* FIX(2.562915447) */
  113. #define FIX_3_072711026  ((long)  25172)    /* FIX(3.072711026) */
  114. #else
  115. #define FIX_0_298631336  FIX(0.298631336)
  116. #define FIX_0_390180644  FIX(0.390180644)
  117. #define FIX_0_541196100  FIX(0.541196100)
  118. #define FIX_0_765366865  FIX(0.765366865)
  119. #define FIX_0_899976223  FIX(0.899976223)
  120. #define FIX_1_175875602  FIX(1.175875602)
  121. #define FIX_1_501321110  FIX(1.501321110)
  122. #define FIX_1_847759065  FIX(1.847759065)
  123. #define FIX_1_961570560  FIX(1.961570560)
  124. #define FIX_2_053119869  FIX(2.053119869)
  125. #define FIX_2_562915447  FIX(2.562915447)
  126. #define FIX_3_072711026  FIX(3.072711026)
  127. #endif
  128.  
  129.  
  130. /* Multiply an long variable by an long constant to yield an long result.
  131.  * For 8-bit samples with the recommended scaling, all the variable
  132.  * and constant values involved are no more than 16 bits wide, so a
  133.  * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  134.  * For 12-bit samples, a full 32-bit multiplication will be needed.
  135.  */
  136.  
  137. #define MULTIPLY(var,const)  ((var) * (const))
  138.  
  139. /* Dequantize a coefficient by multiplying it by the multiplier-table
  140.  * entry; produce an int result.  In this module, both inputs and result
  141.  * are 16 bits or less, so either int or short multiply will work.
  142.  */
  143.  
  144. #define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
  145.  
  146.  
  147. /*
  148.  * Perform dequantization and inverse DCT on one block of coefficients.
  149.  */
  150.  
  151. GLOBAL(void)
  152. jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  153.          JCOEFPTR coef_block,
  154.          JSAMPARRAY output_buf, JDIMENSION output_col)
  155. {
  156.   JCOEFPTR inptr;
  157.   ISLOW_MULT_TYPE * quantptr;
  158.   int * wsptr;
  159.   JSAMPROW outptr;
  160.   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  161.   
  162.   int workspace[DCTSIZE2];    /* buffers data between passes */
  163.   SHIFT_TEMPS
  164.  
  165.   /* Pass 1: process columns from input, store into work array. */
  166.   /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
  167.   /* furthermore, we scale the results by 2**PASS1_BITS. */
  168.  
  169.   inptr = coef_block;
  170.   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  171.   wsptr = workspace;
  172.     
  173.     for (int ctr = DCTSIZE; ctr > 0; ctr--) 
  174.     {
  175.         /* Due to quantization, we will usually find that many of the input
  176.         * coefficients are zero, especially the AC terms.  We can exploit this
  177.         * by short-circuiting the IDCT calculation for any column in which all
  178.         * the AC terms are zero.  In that case each output is equal to the
  179.         * DC coefficient (with scale factor as needed).
  180.         * With typical images and quantization tables, half or more of the
  181.         * column DCT calculations can be simplified this way.
  182.         */
  183.     
  184.         if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
  185.             inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
  186.             inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
  187.             inptr[DCTSIZE*7] == 0) 
  188.             {
  189.                 /* AC terms all zero */
  190.                 int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  191.       
  192.                 wsptr[DCTSIZE*0] = dcval;
  193.                 wsptr[DCTSIZE*1] = dcval;
  194.                 wsptr[DCTSIZE*2] = dcval;
  195.                 wsptr[DCTSIZE*3] = dcval;
  196.                 wsptr[DCTSIZE*4] = dcval;
  197.                 wsptr[DCTSIZE*5] = dcval;
  198.                 wsptr[DCTSIZE*6] = dcval;
  199.                 wsptr[DCTSIZE*7] = dcval;
  200.       
  201.                 inptr++;            /* advance pointers to next column */
  202.                 quantptr++;
  203.                 wsptr++;
  204.             continue;
  205.         }
  206.     
  207.         /* Even part: reverse the even part of the forward DCT. */
  208.         /* The rotator is sqrt(2)*c(-6). */
  209.     
  210.         long z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  211.         long z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  212.         long z1 = (z2 + z3) * FIX_0_541196100;
  213.     
  214.         long tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  215.         long tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  216.     
  217.         z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  218.         z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  219.  
  220.         long tmp0 = (z2 + z3) << CONST_BITS;
  221.         long tmp1 = (z2 - z3) << CONST_BITS;
  222.     
  223.         long tmp10 = tmp0 + tmp3;
  224.         long tmp13 = tmp0 - tmp3;
  225.         long tmp11 = tmp1 + tmp2;
  226.         long tmp12 = tmp1 - tmp2;
  227.     
  228.         /* Odd part per figure 8; the matrix is unitary and hence its
  229.         * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
  230.         */
  231.     
  232.         tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  233.         tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  234.         tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  235.         tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  236.     
  237.         z1 = tmp0 + tmp3;
  238.         z2 = tmp1 + tmp2;
  239.         z3 = tmp0 + tmp2;
  240.         long z4 = tmp1 + tmp3;
  241.         long z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  242.     
  243.         tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  244.         tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  245.         tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  246.         tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  247.         z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  248.         z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  249.         z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  250.         z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  251.     
  252.         z3 += z5;
  253.         z4 += z5;
  254.     
  255.         tmp0 += z1 + z3;
  256.         tmp1 += z2 + z4;
  257.         tmp2 += z2 + z3;
  258.         tmp3 += z1 + z4;
  259.     
  260.         /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  261.     
  262.         wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
  263.         wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
  264.         wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
  265.         wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
  266.         wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
  267.         wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
  268.         wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
  269.         wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
  270.     
  271.         inptr++;            /* advance pointers to next column */
  272.         quantptr++;
  273.         wsptr++;
  274.     }
  275.   
  276.     /* Pass 2: process rows from work array, store into output array. */
  277.     /* Note that we must descale the results by a factor of 8 == 2**3, */
  278.     /* and also undo the PASS1_BITS scaling. */
  279.  
  280.     wsptr = workspace;
  281.     for (ctr = 0; ctr < DCTSIZE; ctr++) 
  282.     {
  283.         outptr = output_buf[ctr] + output_col;
  284.         /* Rows of zeroes can be exploited in the same way as we did with columns.
  285.         * However, the column calculation has created many nonzero AC terms, so
  286.         * the simplification applies less often (typically 5% to 10% of the time).
  287.         * On machines with very fast multiplication, it's possible that the
  288.         * test takes more time than it's worth.  In that case this section
  289.         * may be commented out.
  290.         */
  291.     
  292. #ifndef NO_ZERO_ROW_TEST
  293.         if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
  294.             wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) 
  295.         {
  296.             /* AC terms all zero */
  297.             JSAMPLE dcval = range_limit[(int) DESCALE((long) wsptr[0], PASS1_BITS+3)
  298.                   & RANGE_MASK];
  299.       
  300.             outptr[0] = dcval;
  301.             outptr[1] = dcval;
  302.             outptr[2] = dcval;
  303.             outptr[3] = dcval;
  304.             outptr[4] = dcval;
  305.             outptr[5] = dcval;
  306.             outptr[6] = dcval;
  307.             outptr[7] = dcval;
  308.  
  309.             wsptr += DCTSIZE;        /* advance pointer to next row */
  310.             continue;
  311.         }
  312. #endif
  313.     
  314.         /* Even part: reverse the even part of the forward DCT. */
  315.         /* The rotator is sqrt(2)*c(-6). */
  316.     
  317.         long z2 = (long) wsptr[2];
  318.         long z3 = (long) wsptr[6];
  319.         long z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  320.     
  321.         long tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  322.         long tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  323.     
  324.         long tmp0 = ((long) wsptr[0] + (long) wsptr[4]) << CONST_BITS;
  325.         long tmp1 = ((long) wsptr[0] - (long) wsptr[4]) << CONST_BITS;
  326.     
  327.         long tmp10 = tmp0 + tmp3;
  328.         long tmp13 = tmp0 - tmp3;
  329.         long tmp11 = tmp1 + tmp2;
  330.         long tmp12 = tmp1 - tmp2;
  331.     
  332.         /* Odd part per figure 8; the matrix is unitary and hence its
  333.         * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
  334.         */
  335.     
  336.         tmp0 = (long) wsptr[7];
  337.         tmp1 = (long) wsptr[5];
  338.         tmp2 = (long) wsptr[3];
  339.         tmp3 = (long) wsptr[1];
  340.     
  341.         z1 = tmp0 + tmp3;
  342.         z2 = tmp1 + tmp2;
  343.         z3 = tmp0 + tmp2;
  344.         long z4 = tmp1 + tmp3;
  345.         long z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  346.     
  347.         tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  348.         tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  349.         tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  350.         tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  351.     
  352.         z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  353.         z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  354.         z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  355.         z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  356.     
  357.         z3 += z5;
  358.         z4 += z5;
  359.     
  360.         tmp0 += z1 + z3;
  361.         tmp1 += z2 + z4;
  362.         tmp2 += z2 + z3;
  363.         tmp3 += z1 + z4;
  364.     
  365.         /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  366.     
  367.         outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3, CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
  368.         outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3, CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
  369.         outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2, CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
  370.         outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2, CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
  371.         outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1, CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
  372.         outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1, CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
  373.         outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0, CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
  374.         outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0, CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
  375.     
  376.         wsptr += DCTSIZE;        /* advance pointer to next row */
  377.     }
  378. }
  379.  
  380. #endif /* DCT_ISLOW_SUPPORTED */
  381.