gpt4 book ai didi

c - 这个二维 DCT 代码实际上是如何工作的?

转载 作者:行者123 更新时间:2023-11-30 14:58:29 25 4
gpt4 key购买 nike

这是代码:

void dct(const tga_image *tga, double data[8][8],
const int xpos, const int ypos)
{
int i,j;
double in[8], out[8], rows[8][8];

/* transform rows */
for (j=0; j<8; j++)
{
for (i=0; i<8; i++)
in[i] = (double) pixel(tga, xpos+i, ypos+j);
dct_1d(in, out, 8);
for (i=0; i<8; i++) rows[j][i] = out[i];
}

/* transform columns */
for (j=0; j<8; j++)
{
for (i=0; i<8; i++)
in[i] = rows[i][j];
dct_1d(in, out, 8);
for (i=0; i<8; i++) data[i][j] = out[i];
}
}

它取自listing2.c,位于 https://unix4lyfe.org/dct/

我只有 1 个问题,我们将行填充为 rows[j][i],然后读出 rows[i][j]。根据 2D DCT 公式,我们转置 DCT 矩阵而不是实际数据。为什么要转置实际数据?

最佳答案

如果我假设 xpos 作为水平索引,ypos 作为垂直索引,则以下情况成立。

函数dct_1d(*,*,*);仅适用于一维数组(此处inout )。您所迷惑的原因是这里的C二维数组索引杂技(特别是) > 此处)。

通过在第一个 for block 中简单交换变量 ij,可以将相同的代码重写如下,这将使得您正在尝试的物理感觉(请参阅评论):

void dct(const tga_image *tga, double data[8][8],
const int xpos, const int ypos)
{
int i,j; /* as in matrix[i][j] */
double in[8], out[8], rows[8][8];

/* transform rows (each is running horizontally with j) */
for (i=0; i<8; i++)
{
for (j=0; j<8; j++)
in[j] = (double) pixel(tga, xpos+j, ypos+i); /* fill current row i */
/* Note above xpos in an image is horizontal as j is in a matrix[i][j] in c and
vice versa. (The fallacy that you will make is the following: You will think that
xpos corresponds to i and ypos corresponds to j, which is incorrect.) */
dct_1d(in, out, 8); /* transform current row i */
for (j=0; j<8; j++) rows[i][j] = out[j]; /* copy back current row i */
}

/* transform columns (each is running vertically with i) */
for (j=0; j<8; j++)
{
for (i=0; i<8; i++)
in[i] = rows[i][j]; /* fill current column j */
dct_1d(in, out, 8); /* transform current column j */
for (i=0; i<8; i++) data[i][j] = out[i]; /* copy back current column j */
}
}

关于c - 这个二维 DCT 代码实际上是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43309717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com