2017-04-09 42 views
0

下面是代碼:這個2D DCT代碼是如何工作的?

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]; 
    } 
} 

它是從https://unix4lyfe.org/dct/

我只有1個問題找到listing2.c拍攝,我們填寫的行,行[J] [I],但隨後讀出來out rows [i] [j]。根據2D DCT公式,我們轉置DCT矩陣而不是實際數據。爲什麼實際數據正在轉換?

+0

我補充一下:

通過變量i,並在第一forj簡單交換,同樣的代碼可以如下這將使物理意義,因爲你想(見註釋)被改寫如果你必須做大量的DCT,這是非常低效的代碼。 – user3344003

+0

是的,據我所知在實踐中會使用一些快速的DCT算法,但是如果在硬件中使用類似於FPGA的DCT,這種方法可能會很好 – quantum231

+0

你個人推薦什麼? – quantum231

回答

1

如果我假設xpos爲水平索引而ypos爲垂直索引,則以下情況屬實。

dct_1d(*,*,*);適用於僅1-d陣列(這裏inout)中的作用。你得到的結果是indexing笨拙二維陣列C在這裏(特別是rows這裏)。

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 */ 
    } 
}