home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jfdctfst.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  8.2 KB  |  234 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.  * jfdctfst.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 a fast, not so accurate integer implementation of the
  18.  * forward DCT (Discrete Cosine Transform).
  19.  *
  20.  * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
  21.  * on each column.  Direct algorithms are also available, but they are
  22.  * much more complex and seem not to be any faster when reduced to code.
  23.  *
  24.  * This implementation is based on Arai, Agui, and Nakajima's algorithm for
  25.  * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
  26.  * Japanese, but the algorithm is described in the Pennebaker & Mitchell
  27.  * JPEG textbook (see REFERENCES section in file README).  The following code
  28.  * is based directly on figure 4-8 in P&M.
  29.  * While an 8-point DCT cannot be done in less than 11 multiplies, it is
  30.  * possible to arrange the computation so that many of the multiplies are
  31.  * simple scalings of the final outputs.  These multiplies can then be
  32.  * folded into the multiplications or divisions by the JPEG quantization
  33.  * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
  34.  * to be done in the DCT itself.
  35.  * The primary disadvantage of this method is that with fixed-point math,
  36.  * accuracy is lost due to imprecise representation of the scaled
  37.  * quantization values.  The smaller the quantization table entry, the less
  38.  * precise the scaled value, so this implementation does worse with high-
  39.  * quality-setting files than with low-quality ones.
  40.  */
  41.  
  42. #define JPEG_INTERNALS
  43. #include "jinclude.h"
  44. #include "jpeglib.h"
  45. #include "jdct.h"        /* Private declarations for DCT subsystem */
  46.  
  47. #ifdef DCT_IFAST_SUPPORTED
  48.  
  49.  
  50. /*
  51.  * This module is specialized to the case DCTSIZE = 8.
  52.  */
  53.  
  54. #if DCTSIZE != 8
  55.   Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  56. #endif
  57.  
  58.  
  59. /* Scaling decisions are generally the same as in the LL&M algorithm;
  60.  * see jfdctint.c for more details.  However, we choose to descale
  61.  * (right shift) multiplication products as soon as they are formed,
  62.  * rather than carrying additional fractional bits into subsequent additions.
  63.  * This compromises accuracy slightly, but it lets us save a few shifts.
  64.  * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
  65.  * everywhere except in the multiplications proper; this saves a good deal
  66.  * of work on 16-bit-int machines.
  67.  *
  68.  * Again to save a few shifts, the intermediate results between pass 1 and
  69.  * pass 2 are not upscaled, but are represented only to integral precision.
  70.  *
  71.  * A final compromise is to represent the multiplicative constants to only
  72.  * 8 fractional bits, rather than 13.  This saves some shifting work on some
  73.  * machines, and may also reduce the cost of multiplication (since there
  74.  * are fewer one-bits in the constants).
  75.  */
  76.  
  77. #define CONST_BITS  8
  78.  
  79.  
  80. /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  81.  * causing a lot of useless floating-point operations at run time.
  82.  * To get around this we use the following pre-calculated constants.
  83.  * If you change CONST_BITS you may want to add appropriate values.
  84.  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
  85.  */
  86.  
  87. #if CONST_BITS == 8
  88. #define FIX_0_382683433  ((long)   98)        /* FIX(0.382683433) */
  89. #define FIX_0_541196100  ((long)  139)        /* FIX(0.541196100) */
  90. #define FIX_0_707106781  ((long)  181)        /* FIX(0.707106781) */
  91. #define FIX_1_306562965  ((long)  334)        /* FIX(1.306562965) */
  92. #else
  93. #define FIX_0_382683433  FIX(0.382683433)
  94. #define FIX_0_541196100  FIX(0.541196100)
  95. #define FIX_0_707106781  FIX(0.707106781)
  96. #define FIX_1_306562965  FIX(1.306562965)
  97. #endif
  98.  
  99.  
  100. /* We can gain a little more speed, with a further compromise in accuracy,
  101.  * by omitting the addition in a descaling shift.  This yields an incorrectly
  102.  * rounded result half the time...
  103.  */
  104.  
  105. #ifndef USE_ACCURATE_ROUNDING
  106. #undef DESCALE
  107. #define DESCALE(x,n)  RIGHT_SHIFT(x, n)
  108. #endif
  109.  
  110.  
  111. /* Multiply a DCTELEM variable by an long constant, and immediately
  112.  * descale to yield a DCTELEM result.
  113.  */
  114.  
  115. #define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
  116.  
  117.  
  118. /*
  119.  * Perform the forward DCT on one block of samples.
  120.  */
  121.  
  122. GLOBAL(void)
  123. jpeg_fdct_ifast (DCTELEM * data)
  124. {
  125.   DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  126.   DCTELEM tmp10, tmp11, tmp12, tmp13;
  127.   DCTELEM z1, z2, z3, z4, z5, z11, z13;
  128.   DCTELEM *dataptr;
  129.   int ctr;
  130.   SHIFT_TEMPS
  131.  
  132.   /* Pass 1: process rows. */
  133.  
  134.   dataptr = data;
  135.   for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  136.     tmp0 = dataptr[0] + dataptr[7];
  137.     tmp7 = dataptr[0] - dataptr[7];
  138.     tmp1 = dataptr[1] + dataptr[6];
  139.     tmp6 = dataptr[1] - dataptr[6];
  140.     tmp2 = dataptr[2] + dataptr[5];
  141.     tmp5 = dataptr[2] - dataptr[5];
  142.     tmp3 = dataptr[3] + dataptr[4];
  143.     tmp4 = dataptr[3] - dataptr[4];
  144.     
  145.     /* Even part */
  146.     
  147.     tmp10 = tmp0 + tmp3;    /* phase 2 */
  148.     tmp13 = tmp0 - tmp3;
  149.     tmp11 = tmp1 + tmp2;
  150.     tmp12 = tmp1 - tmp2;
  151.     
  152.     dataptr[0] = tmp10 + tmp11; /* phase 3 */
  153.     dataptr[4] = tmp10 - tmp11;
  154.     
  155.     z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
  156.     dataptr[2] = tmp13 + z1;    /* phase 5 */
  157.     dataptr[6] = tmp13 - z1;
  158.     
  159.     /* Odd part */
  160.  
  161.     tmp10 = tmp4 + tmp5;    /* phase 2 */
  162.     tmp11 = tmp5 + tmp6;
  163.     tmp12 = tmp6 + tmp7;
  164.  
  165.     /* The rotator is modified from fig 4-8 to avoid extra negations. */
  166.     z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
  167.     z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
  168.     z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
  169.     z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
  170.  
  171.     z11 = tmp7 + z3;        /* phase 5 */
  172.     z13 = tmp7 - z3;
  173.  
  174.     dataptr[5] = z13 + z2;    /* phase 6 */
  175.     dataptr[3] = z13 - z2;
  176.     dataptr[1] = z11 + z4;
  177.     dataptr[7] = z11 - z4;
  178.  
  179.     dataptr += DCTSIZE;        /* advance pointer to next row */
  180.   }
  181.  
  182.   /* Pass 2: process columns. */
  183.  
  184.   dataptr = data;
  185.   for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  186.     tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
  187.     tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
  188.     tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
  189.     tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
  190.     tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
  191.     tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
  192.     tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
  193.     tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
  194.     
  195.     /* Even part */
  196.     
  197.     tmp10 = tmp0 + tmp3;    /* phase 2 */
  198.     tmp13 = tmp0 - tmp3;
  199.     tmp11 = tmp1 + tmp2;
  200.     tmp12 = tmp1 - tmp2;
  201.     
  202.     dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
  203.     dataptr[DCTSIZE*4] = tmp10 - tmp11;
  204.     
  205.     z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
  206.     dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
  207.     dataptr[DCTSIZE*6] = tmp13 - z1;
  208.     
  209.     /* Odd part */
  210.  
  211.     tmp10 = tmp4 + tmp5;    /* phase 2 */
  212.     tmp11 = tmp5 + tmp6;
  213.     tmp12 = tmp6 + tmp7;
  214.  
  215.     /* The rotator is modified from fig 4-8 to avoid extra negations. */
  216.     z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
  217.     z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
  218.     z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
  219.     z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
  220.  
  221.     z11 = tmp7 + z3;        /* phase 5 */
  222.     z13 = tmp7 - z3;
  223.  
  224.     dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
  225.     dataptr[DCTSIZE*3] = z13 - z2;
  226.     dataptr[DCTSIZE*1] = z11 + z4;
  227.     dataptr[DCTSIZE*7] = z11 - z4;
  228.  
  229.     dataptr++;            /* advance pointer to next column */
  230.   }
  231. }
  232.  
  233. #endif /* DCT_IFAST_SUPPORTED */
  234.