2016-03-24 5 views
0

Ich habe eine Aufgabe zu drehen und skalieren ein BMP-Bild in C. Der Code zum Spiegeln eines Bildes wurde uns gegeben, um uns zu verstehen, wie es funktioniert, aber ich habe eine schwere Zeit damit.Code zum Umdrehen eines BMP-Bildes in C

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

Mein Beileid. Hattest du eine Frage? –

+0

Kleinere Ausgabe: Für _large_ Bilder besser 'sizeof (PIXEL) * rows * cols'. Ich nehme an, deine Bilder sind <2G Pixel. – chux

Antwort

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

Angenommen, Sie ein 2x2 Bild haben:

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)