2016-04-13 13 views
2

私は2D配列のbmpイメージを読み込んでいるC言語のプログラムを書いています。私は元の画像+その逆のの隣に出力として持っているはずです。Cで2D配列からミラーイメージを読み取る

私がここに書いたコードはそれを行うべきですが、画像はの上に印刷されます。誰かが私がそれをどのように修正できるか知っていれば、私は助けに感謝します。

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:私はあなたのローダがBMPのサポート異なるバージョン、解像度、カラー深度を確認し、パレットのpresensなどに関する完全なものではありませんGCC

+0

は、それはあなたのコードで何が起こっているのか伝えるのは難しいです(つまりは、コメントがためのものであるものです)、私はイメージが '160px'の高さと'(3 * 240 * 2)px'の幅を持っていると思いますか?結果として得られる画像の高さは '160px'、幅は' 2 *(3 * 240 * 2)px'でなければなりません。ダブルループの 'char temp 'の目的は何ですか? –

+0

@ジョニー・ヘンリー。ここにコメントを追加する – Shango

+0

これらのコメントはすばらしいです:)しかし、私はまだネストされたforループの 'temp'がどのように使われているのか混乱していますか? 'temp'はあなたのコードの以前のバージョンからの成果物ですか? –

答えて

0

The BMP file format you can find here

でこれを実行しています

しかし、あなたがのためにこれを使用したいもののために、あなたはおそらく固定入力を持って、常に同じ解像度など

富栄結果として得られる画像の幅を2倍にします。したがって、ヘッダー内のいくつかの詳細を変更することがあります。

「ファイルサイズ」
「をピクセル単位でビットマップの幅(符号付き整数)」

関連する問題