KJpegIdct2d.c
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:3k
源码类别:

模拟服务器

开发平台:

C/C++

  1. //---------------------------------------------------------------------------
  2. // Sword3 Engine (c) 1999-2000 by Kingsoft
  3. //
  4. // File: KJpegDeocode.cpp
  5. // Date: 2000.08.08
  6. // Code: Daniel Wang
  7. // Desc: Jpeg 解码 C 版 2D IDCT
  8. // From: Cloud Wu's JPEG Decoder
  9. //---------------------------------------------------------------------------
  10. #include <windows.h>
  11. #include "KJpegLib.h"
  12. /****************************************************************************
  13.  2D IDCT 公式
  14.           { 1//8 当 u=0 时
  15.   a(u) =  {
  16.           { 1/2 其他情况
  17.            7   7                         2*x+1                2*y+1
  18. f(x,y) =  sum sum a(u)*a(v)*F(u,v)*cos (------- *u*PI)* cos (------ *v*PI)
  19.           u=0 v=0                         16                   16
  20.  x, y = 0 , 1...7
  21. ****************************************************************************/
  22. #define FIX(a) ((long)((a)*256+0.5))
  23. #define c0  FIX(0.7071068)
  24. #define c1  FIX(0.4903926)
  25. #define c2  FIX(0.4619398)
  26. #define c3  FIX(0.4157348)
  27. #define c4  FIX(0.3535534)
  28. #define c5  FIX(0.2777851)
  29. #define c6  FIX(0.1913417)
  30. #define c7  FIX(0.0975452)
  31. #define FIXMUL(a,b) (((a)>>8)*(b))
  32. //---------------------------------------------------------------------------
  33. // 函数: jpeg_idct_1d
  34. // 功能: C 版本 1D IDCT
  35. // 参数: coef
  36. // result
  37. // 返回: void
  38. //---------------------------------------------------------------------------
  39. void jpeg_idct_1d(long *coef, long *result)
  40. {
  41. int  j, j1;
  42. long buf2[8];
  43. long flt1 = FIXMUL(coef[1],c7) - FIXMUL(coef[7],c1);
  44. long flt2 = FIXMUL(coef[7],c7) + FIXMUL(coef[1],c1);
  45. long flt3 = FIXMUL(coef[5],c3) - FIXMUL(coef[3],c5);
  46. long flt4 = FIXMUL(coef[3],c3) + FIXMUL(coef[5],c5);
  47. buf2[0] = FIXMUL((coef[0] + coef[4]),c4);
  48. buf2[1] = FIXMUL((coef[0] - coef[4]),c4);
  49. buf2[2] = FIXMUL(coef[2],c6) - FIXMUL(coef[6],c2);
  50. buf2[3] = FIXMUL(coef[6],c6) + FIXMUL(coef[2],c2);
  51. coef[4] = flt1 + flt3;
  52. buf2[5] = flt1 - flt3;
  53. buf2[6] = flt2 - flt4;
  54. coef[7] = flt2 + flt4;
  55. coef[5] = FIXMUL((buf2[6] - buf2[5]),c0);
  56. coef[6] = FIXMUL((buf2[6] + buf2[5]),c0);
  57. coef[0] = buf2[0] + buf2[3];
  58. coef[1] = buf2[1] + buf2[2];
  59. coef[2] = buf2[1] - buf2[2];
  60. coef[3] = buf2[0] - buf2[3];
  61. for (j = 0; j < 4; j++)
  62. {
  63. j1 = 7 - j;
  64. result[j] = coef[j] + coef[j1];
  65. result[j1] = coef[j] - coef[j1];
  66. }
  67. }
  68. //---------------------------------------------------------------------------
  69. // 函数: jpeg_idct_2d
  70. // 功能: C 版本 2D IDCT
  71. // 参数: buf 解码后的缓存
  72. // 返回: void
  73. //---------------------------------------------------------------------------
  74. void jpeg_idct_2d(short *buf)
  75. {
  76. int  i, j;
  77. long buf1[8];
  78. long buf2[8];
  79. long coef[64];
  80. signed char *result;
  81. short tmp;
  82. for (i = 0; i < 64; i++)
  83. {
  84. coef[i] = (long)buf[i]<<12;
  85. }
  86. result = (signed char *)buf;
  87. for(i = 0; i < 8; i++)
  88. {
  89. jpeg_idct_1d(coef + 8 * i, buf1);
  90. for(j = 0; j < 8; j++)
  91. coef[i * 8 + j] = buf1[j];
  92. }
  93. for(i = 0; i < 8; i++)
  94. {
  95. for(j = 0; j < 8; j++)
  96. {
  97. buf1[j] = coef[j * 8 + i];
  98. }
  99. jpeg_idct_1d(buf1, buf2);
  100. for (j = 0; j < 8; j++)
  101. {
  102. tmp = (buf2[j] + 0x8000)>>16;
  103. if (tmp < -128)
  104. tmp = -128;
  105. else if (tmp > 127)
  106. tmp=127;
  107. result[i * 8 + j] = (signed char)tmp;
  108. }
  109. }
  110. }
  111. //---------------------------------------------------------------------------