2016-04-13 5 views
2

Ich schreibe ein Programm in C, in dem ich ein BMP-Bild mit einem 2D-Array lesen. Ich soll als das ursprüngliche Bild + seine inverse rechts nebeneinander ausgegeben haben.Spiegelbild aus 2D-Array in C gelesen

Der Code, den ich hier habe, sollte tun, aber die Bilder drucken auf top voneinander. Ich schätze die Hilfe, wenn jemand weiß, wie ich das beheben kann.

int main(void) { 

    // Opens file to read the image from 
    FILE * infile = fopen("inputImage.bmp", "rb"); 

    // Creates file to write the image to after modifications  
    FILE * outfile = fopen("flip.bmp", "wb"); 

    //Bmp images have headers the next 3 lines of code store the header of the input image 
    unsigned char headersPart1[2]; 
    int filesize;  
    unsigned char headersPart2[48]; 

    // This space[] [] creates the array to which to write the input image AND its inverse right next to each other the image file is 160 * 240 
    // The 3 is for the rgb values.. since its a 2D array we want to use every single //part of the image to work with  
    unsigned char space[160][3*240]; 

    //The array to which to copy the results to (original image + INVERSE) 
    unsigned char mirror[160][3*480]; 

    fread(headersPart1,sizeof(char) ,2,infile); 
    fread(&filesize,sizeof(char) ,4,infile); 
    fread(headersPart2,sizeof(char) ,48,infile); 
    fread(space,sizeof(char) ,filesize-54,infile); 

    // copying what in the original image array (space[][]) into the new //array mirror[][] 
    for (int row = 0; row < 240*3 ; row++) { 
     for (int col = 0 ; col < 160; col++) { 

      char temp = space[col][row]; 
      mirror[col][row] = space[col][row]; 

      //Starts printing the inverse of the original image, starting at the index where the original image will end 
      mirror[col][720+row]= space[col][719-row]; 

      space[col][row] = temp; 

     } 
    } 

    // Puts everything back into the outfile , once i save and run on GCC this gives me an image and its inverse on top of each other 
    fwrite(headersPart1,sizeof(char) ,2,outfile); 
    fwrite(&filesize,sizeof(char) ,4,outfile); 
    fwrite(headersPart2,sizeof(char) ,48,outfile); 

    //sends whats in mirror [][] to the outfile 
    //54 is the size of the header (bmp images)) 
    fwrite(mirror,sizeof(char) ,filesize-54,outfile);  

    return 0; 
} 

PS: Ich bin mit dieser auf GCC

+0

Es ist schwer zu sagen, was in Ihrem Code los ist (das ist, was Kommentare sind für), Ich nehme an, das Bild hat eine Höhe von '160px' und eine Breite von '(3 * 240 * 2) px'? Das resultierende Bild sollte also eine Höhe von 160 px und eine Breite von 2 px (3 * 240 * 2) haben. Und was ist der Zweck von 'char temp' in der Double For-Schleife? –

+0

@Jonny Henly. Ill Kommentare jetzt hinzufügen – Shango

+0

Diese Kommentare sind großartig:) aber ich bin immer noch verwirrt, was 'temp', in der verschachtelten for-Schleife, verwendet wird? Ist "Temp" nur ein Artefakt von einer früheren Version Ihres Codes? –

Antwort

0

The BMP file format you can find here

Ihr Lader nicht vollständig über den Support verschiedene Versionen von BMP, überprüfen Auflösung, Farbtiefe, die presens der Palette usw.

Aber für das, was Sie dafür verwenden wollen, haben Sie wahrscheinlich einen festen Eingang, immer die gleiche Auflösung usw.

Bu t Sie möchten, dass das resultierende Bild doppelt so breit ist. Daher einige Details in den Headern muss sich ändern:

„Dateigröße“
„die Bitmap-Breite in Pixel (Signed Integer)“

Verwandte Themen