2017-04-09 11 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/

で発見listing2.cから取られ、私はちょうど1質問があり、私たちは、[I]行[J]として行に記入しますが、それを読んでout行[i] [j]を含む。 2D DCTの式に従って、実際のデータではなくDCT行列を転置します。実際のデータが転記されるのはなぜですか?

+0

私が追加多くのDCTを行う必要がある場合は、これは非常に非効率的なコードです。 – user3344003

+0

はい、私が知っている限り、いくつかの高速DCTアルゴリズムは実際に使用されますが、FPGAのようなハードウェアでDCTを行う場合、この方法はちょうど良いかもしれません – quantum231

+0

あなたは個人的に何をお勧めしますか? – quantum231

答えて

1

水平インデックスがxpos、垂直インデックスがyposと仮定すると、次のようになります。

関数(ここでinout)のみ1-Dアレイdct_1d(*,*,*);作品。あなたが奪い取っているのは、2次元アレイのCここ(特にここではrows)のindexingジャグラーです。単純な変数のスワップiと最初forブロック内jことで

は、同じコードを使用すると、(コメントを参照)しようとしているなどの物理的意味をなすであろう次のように書き換えることができます。

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 */ 
    } 
} 
関連する問題