2016-03-24 19 views
0

Cでbmp画像を回転して拡大/縮小するという割り当てがあります。画像を反転させるコードが与えられていますが、そうするのは苦労します。Cでbmp画像を反転するコードを理解する

int flip (PIXEL *original, PIXEL **new, int rows, int cols) 
{ 
    int row, col; 

    if ((rows <= 0) || (cols <= 0)) return -1; 

    *new = (PIXEL*)malloc(rows*cols*sizeof(PIXEL)); 

    for (row=0; row < rows; row++) 
    for (col=0; col < cols; col++) { 
     PIXEL* o = original + row*cols + col; 
     PIXEL* n = (*new) + row*cols + (cols-1-col); 
     *n = *o; 
    } 

    return 0; 
} 
+3

私の哀悼の意を。あなたは質問がありましたか? –

+0

マイナーな問題:_large_ピクチャでは、 'sizeof(PIXEL)* rows * cols'を使う方が良いです。私はあなたの写真が<2Gピクセルであると仮定します。 – chux

答えて

0
int flip (PIXEL *original, PIXEL **new, int rows, int cols) 
{ 
    int row, col; 

    if ((rows <= 0) || (cols <= 0)) return -1; 

    *new = (PIXEL*)malloc(rows*cols*sizeof(PIXEL)); // Allocate memory for the flipped image 

    for (row=0; row < rows; row++) 
    for (col=0; col < cols; col++) { 
     PIXEL* o = original + row*cols + col;   // Get a pointer to pixel (row, col) 
                // in original image 

     PIXEL* n = (*new) + row*cols + (cols-1-col); // Get a pointer to pixel 
                // (row, cols - col -1) 
                // in flipped image 


     *n = *o; // Copy pixel from original image to flipped image 
    } 

    return 0; 
} 

あなたは2x2のイメージがあるとします。

Loop 1: Original (0, 0) is copied to flipped (0, 1) 
Loop 2: Original (0, 1) is copied to flipped (0, 0) 
Loop 3: Original (1, 0) is copied to flipped (1, 1) 
Loop 4: Original (1, 1) is copied to flipped (1, 0) 
関連する問題