home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / dctm.lzh / DCTM / source.lzh / source / cDCT.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2003-02-08  |  3.8 KB  |  188 lines

  1. /*************************************************************************
  2.     cDCT.cpp
  3.  
  4.     03/02/08    Xiaohong
  5. *************************************************************************/
  6. #include "cDCT.h"
  7. #include "macros.h"
  8. #include <math.h>
  9. /**********************************************************
  10.     cDCT é╠âüâôâoè╓Éöé╠ÉΘî╛
  11. **********************************************************/
  12. cDCT::cDCT()
  13. {
  14.     int i, j;
  15.     double s;
  16.     double sTmp;
  17.  
  18.     for(i=0; i<8; i++)
  19.     {
  20.         if(i == 0)
  21.         {
  22.             s = sqrt(0.125);
  23.         }
  24.         else
  25.         {
  26.             s = 0.5;
  27.         }
  28.  
  29.         for (j=0; j<8; j++)
  30.         {
  31.             m_fCoefficients[i][j] = s * cos((PI/8.0)*i*(j+0.5));
  32.             /* save integer value shifted 14 bits to left */
  33.             sTmp = m_fCoefficients[i][j] * 16384;
  34.         }
  35.     }
  36.  
  37.     m_pIclip = m_nIclip+512;
  38.     for (i= -512; i<512; i++)
  39.         m_pIclip[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
  40.  
  41. }
  42. cDCT::~cDCT()
  43. {
  44. }
  45. void cDCT::fDCT(short *pBlock)
  46. {
  47.     int        i, j, k;
  48.     double     s;
  49.     double     tmp[64];
  50.  
  51.  
  52.     for (i=0; i<8; i++)
  53.     {
  54.         for (j=0; j<8; j++)
  55.         {
  56.             s = 0.0;
  57.             for (k=0; k<8; k++)
  58.                 s += m_fCoefficients[j][k] * pBlock[8*i+k];
  59.             tmp[8*i+j] = s;
  60.         }
  61.     }
  62.  
  63.     for (j=0; j<8; j++)
  64.     {
  65.         for (i=0; i<8; i++)
  66.         {
  67.  
  68.             s = 0.0;
  69.             for (k=0; k<8; k++)
  70.                 s += m_fCoefficients[i][k] * tmp[8*k+j];
  71.             pBlock[8*i+j] = (short)(s+0.499999);
  72.         }
  73.     }
  74. }
  75. void cDCT::iDCT(short *pBlock)
  76. {
  77.     int i;
  78.  
  79.     for (i=0; i<8; i++)
  80.         IdctRow(pBlock+8*i);
  81.  
  82.     for (i=0; i<8; i++)
  83.         IdctCol(pBlock+i);
  84. }
  85. inline void cDCT::IdctRow(short* pBlock)
  86. {
  87.     int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  88.  
  89.     /* shortcut */
  90.     if (!((x1 = pBlock[4]<<11) | (x2 = pBlock[6]) | (x3 = pBlock[2]) |
  91.         (x4 = pBlock[1]) | (x5 = pBlock[7]) | (x6 = pBlock[5]) | (x7 = pBlock[3])))
  92.     {
  93.         pBlock[0]=pBlock[1]=pBlock[2]=pBlock[3]=pBlock[4]=pBlock[5]=pBlock[6]=pBlock[7]=pBlock[0]<<3;
  94.         return;
  95.     }
  96.  
  97.     x0 = (pBlock[0]<<11) + 128; /* for proper rounding in the fourth stage */
  98.  
  99.     /* first stage */
  100.     x8 = W7*(x4+x5);
  101.     x4 = x8 + (W1-W7)*x4;
  102.     x5 = x8 - (W1+W7)*x5;
  103.     x8 = W3*(x6+x7);
  104.     x6 = x8 - (W3-W5)*x6;
  105.     x7 = x8 - (W3+W5)*x7;
  106.  
  107.     /* second stage */
  108.     x8 = x0 + x1;
  109.     x0 -= x1;
  110.     x1 = W6*(x3+x2);
  111.     x2 = x1 - (W2+W6)*x2;
  112.     x3 = x1 + (W2-W6)*x3;
  113.     x1 = x4 + x6;
  114.     x4 -= x6;
  115.     x6 = x5 + x7;
  116.     x5 -= x7;
  117.  
  118.     /* third stage */
  119.     x7 = x8 + x3;
  120.     x8 -= x3;
  121.     x3 = x0 + x2;
  122.     x0 -= x2;
  123.     x2 = (181*(x4+x5)+128)>>8;
  124.     x4 = (181*(x4-x5)+128)>>8;
  125.  
  126.     /* fourth stage */
  127.     pBlock[0] = (x7+x1)>>8;
  128.     pBlock[1] = (x3+x2)>>8;
  129.     pBlock[2] = (x0+x4)>>8;
  130.     pBlock[3] = (x8+x6)>>8;
  131.     pBlock[4] = (x8-x6)>>8;
  132.     pBlock[5] = (x0-x4)>>8;
  133.     pBlock[6] = (x3-x2)>>8;
  134.     pBlock[7] = (x7-x1)>>8;
  135. }
  136. inline void cDCT::IdctCol(short* pBlock)
  137. {
  138.     int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  139.  
  140.     /* shortcut */
  141.     if (!((x1 = (pBlock[8*4]<<8)) | (x2 = pBlock[8*6]) | (x3 = pBlock[8*2]) |
  142.         (x4 = pBlock[8*1]) | (x5 = pBlock[8*7]) | (x6 = pBlock[8*5]) | (x7 = pBlock[8*3])))
  143.     {
  144.         pBlock[8*0]=pBlock[8*1]=pBlock[8*2]=pBlock[8*3]=pBlock[8*4]=pBlock[8*5]=pBlock[8*6]=pBlock[8*7]=m_pIclip[(pBlock[8*0]+32)>>6];
  145.         return;
  146.     }
  147.  
  148.     x0 = (pBlock[8*0]<<8) + 8192;
  149.  
  150.     /* first stage */
  151.     x8 = W7*(x4+x5) + 4;
  152.     x4 = (x8+(W1-W7)*x4)>>3;
  153.     x5 = (x8-(W1+W7)*x5)>>3;
  154.     x8 = W3*(x6+x7) + 4;
  155.     x6 = (x8-(W3-W5)*x6)>>3;
  156.     x7 = (x8-(W3+W5)*x7)>>3;
  157.  
  158.     /* second stage */
  159.     x8 = x0 + x1;
  160.     x0 -= x1;
  161.     x1 = W6*(x3+x2) + 4;
  162.     x2 = (x1-(W2+W6)*x2)>>3;
  163.     x3 = (x1+(W2-W6)*x3)>>3;
  164.     x1 = x4 + x6;
  165.     x4 -= x6;
  166.     x6 = x5 + x7;
  167.     x5 -= x7;
  168.  
  169.     /* third stage */
  170.     x7 = x8 + x3;
  171.     x8 -= x3;
  172.     x3 = x0 + x2;
  173.     x0 -= x2;
  174.     x2 = (181*(x4+x5)+128)>>8;
  175.     x4 = (181*(x4-x5)+128)>>8;
  176.  
  177.     /* fourth stage */
  178.     pBlock[8*0] = m_pIclip[(x7+x1)>>14];
  179.     pBlock[8*1] = m_pIclip[(x3+x2)>>14];
  180.     pBlock[8*2] = m_pIclip[(x0+x4)>>14];
  181.     pBlock[8*3] = m_pIclip[(x8+x6)>>14];
  182.     pBlock[8*4] = m_pIclip[(x8-x6)>>14];
  183.     pBlock[8*5] = m_pIclip[(x0-x4)>>14];
  184.     pBlock[8*6] = m_pIclip[(x3-x2)>>14];
  185.     pBlock[8*7] = m_pIclip[(x7-x1)>>14];
  186. }
  187.  
  188.