下面的二维快速逆离散余弦变换是在Thomas G. Lane 的代码的基础上,修改而成。可直接用于图像或视频处理。
测试代码是玄机逸士写的。
// fidct.h
void fidct(short *const block);
void fidct_init();
// fidct.cpp
/*****************************************************************************
*
* XVID MPEG-4 VIDEO CODEC
* - Inverse DCT -
*
* These routines are from Independent JPEG Group's free JPEG software
* Copyright (C) 1991-1998, Thomas G. Lane (see the file README.IJG)
****************************************************************************/
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
#include "fidct.h"
#define W1 2841 /* 2048*sqrt(2)*cos(1*pi/16) */
#define W2 2676 /* 2048*sqrt(2)*cos(2*pi/16) */
#define W3 2408 /* 2048*sqrt(2)*cos(3*pi/16) */
#define W5 1609 /* 2048*sqrt(2)*cos(5*pi/16) */
#define W6 1108 /* 2048*sqrt(2)*cos(6*pi/16) */
#define W7 565 /* 2048*sqrt(2)*cos(7*pi/16) */
/* private data */
static short iclip[1024]; /* clipping table */
static short *iclp;
/* two dimensional inverse discrete cosine transform */
// fidct_init() MUST BE CALLED BEOFRE THE FIRST CALL TO THIS FUNCTION!
void fidct(short *const block)
{
static short *blk;
static long i;
static long X0, X1, X2, X3, X4, X5, X6, X7, X8;
for (i = 0; i < 8; i++) /* idct rows */
{
blk = block + (i << 3);
if (!((X1 = blk[4] << 11) | (X2 = blk[6]) | (X3 = blk[2]) | (X4 = blk[1]) |
(X5 = blk[7]) | (X6 = blk[5]) | (X7 = blk[3])))
{
blk[0] = blk[1] = blk[2] = blk[3] = blk[4] = blk[5] = blk[6] = blk[7] = blk[0] << 3;
continue;
}
X0 = (blk[0] << 11) + 128; /* for proper rounding in the fourth stage */
/* first stage */
X8 = W7 * (X4 + X5);
X4 = X8 + (W1 - W7) * X4;
X5 = X8 - (W1 + W7) * X5;
X8 = W3 * (X6 + X7);
X6 = X8 - (W3 - W5) * X6;
X7 = X8 - (W3 + W5) * X7;
/* second stage */
X8 = X0 + X1;
X0 -= X1;
X1 = W6 * (X3 + X2);
X2 = X1 - (W2 + W6) * X2;
X3 = X1 + (W2 - W6) * X3;
X1 = X4 + X6;
X4 -= X6;
X6 = X5 + X7;
X5 -= X7;
/* third stage */
X7 = X8 + X3;
X8 -= X3;
X3 = X0 + X2;
X0 -= X2;
X2 = (181 * (X4 + X5) + 128) >> 8;
X4 = (181 * (X4 - X5) + 128) >> 8;
/* fourth stage */
blk[0] = (short) ((X7 + X1) >> 8);
blk[1] = (short) ((X3 + X2) >> 8);
blk[2] = (short) ((X0 + X4) >> 8);
blk[3] = (short) ((X8 + X6) >> 8);
blk[4] = (short) ((X8 - X6) >> 8);
blk[5] = (short) ((X0 - X4) >> 8);
blk[6] = (short) ((X3 - X2) >> 8);
blk[7] = (short) ((X7 - X1) >> 8);
} /* end for ( i = 0; i < 8; ++i ) IDCT-rows */
for (i = 0; i < 8; i++) /* idct columns */
{
blk = block + i;
/* shortcut */
if (!
((X1 = (blk[8 * 4] << 8)) | (X2 = blk[8 * 6]) | (X3 = blk[8 * 2]) | (X4 = blk[8 *1])
| (X5 = blk[8 * 7]) | (X6 = blk[8 * 5]) | (X7 = blk[8 * 3])))
{
blk[8 * 0] = blk[8 * 1] = blk[8 * 2] = blk[8 * 3] = blk[8 * 4] =
blk[8 * 5] = blk[8 * 6] = blk[8 * 7] = iclp[(blk[8 * 0] + 32) >> 6];
continue;
}
X0 = (blk[8 * 0] << 8) + 8192;
/* first stage */
X8 = W7 * (X4 + X5) + 4;
X4 = (X8 + (W1 - W7) * X4) >> 3;
X5 = (X8 - (W1 + W7) * X5) >> 3;
X8 = W3 * (X6 + X7) + 4;
X6 = (X8 - (W3 - W5) * X6) >> 3;
X7 = (X8 - (W3 + W5) * X7) >> 3;
/* second stage */
X8 = X0 + X1;
X0 -= X1;
X1 = W6 * (X3 + X2) + 4;
X2 = (X1 - (W2 + W6) * X2) >> 3;
X3 = (X1 + (W2 - W6) * X3) >> 3;
X1 = X4 + X6;
X4 -= X6;
X6 = X5 + X7;
X5 -= X7;
/* third stage */
X7 = X8 + X3;
X8 -= X3;
X3 = X0 + X2;
X0 -= X2;
X2 = (181 * (X4 + X5) + 128) >> 8;
X4 = (181 * (X4 - X5) + 128) >> 8;
/* fourth stage */
blk[8 * 0] = iclp[(X7 + X1) >> 14];
blk[8 * 1] = iclp[(X3 + X2) >> 14];
blk[8 * 2] = iclp[(X0 + X4) >> 14];
blk[8 * 3] = iclp[(X8 + X6) >> 14];
blk[8 * 4] = iclp[(X8 - X6) >> 14];
blk[8 * 5] = iclp[(X0 - X4) >> 14];
blk[8 * 6] = iclp[(X3 - X2) >> 14];
blk[8 * 7] = iclp[(X7 - X1) >> 14];
}
}
void fidct_init()
{
int i;
iclp = iclip + 512;
for (i = -512; i < 512; i++)
iclp[i] = (i < -256) ? -256 : ((i > 255) ? 255 : i);
}
// 测试代码:calculate.cpp
#include <iostream>
#include "fidct.h"
using namespace std;
#define NUM 8
int main(void)
{
int i, j;
double originaldata[NUM][NUM] =
{
{1125.00, -32.00, -185.00, -7.00, 2.00, -1.00, -2.00, 2.00},
{-22.00, -16.00, 45.00, -3.00, -2.00, 0.00, -1.00, -2.00},
{-165.00, 32.00, 17.00, 2.00, 1.00, -1.00, -3.00, 0.00},
{-7.00, -4.00, 0.00, 2.00, 2.00, -1.00, -1.00, 2.00},
{-2.00, 0.00, 0.00, 3.00, 0.00, 0.00, 2.00, 1.00},
{3.00, 1.00, 1.00, -1.00, -2.00, 1.00, 2.00, 0.00},
{0.00, 0.00, 2.00, -1.00, -1.00, 2.00, 1.00, -1.00},
{0.00, 3.00, 1.00, -1.00, 2.00, 1.00, -2.00, 0.00}
};
short *data = new short[NUM * NUM];
for(i = 0; i < NUM; i++)
{
for(j = 0; j < NUM; j++)
{
data[i * NUM + j] = (short)originaldata[i][j];
}
}
fidct_init();
fidct(data);
for(i = 0; i < NUM; i++)
{
for(j = 0; j < NUM; j++)
{
cout << data[i * NUM + j] << '/t';
}
cout << endl;
}
return 0;
}
运行结果:
89 101 114 125 126 115 105 96
97 115 131 147 149 135 123 113
114 134 159 178 175 164 149 137
121 143 177 196 201 189 165 150
119 141 175 201 207 186 162 144
107 130 165 189 192 171 144 125
96 119 150 171 172 145 116 96
88 107 136 156 155 129 97 75
请对照:
离散余弦正逆变换和快速离散余弦变换代码实现(FDCT)