簡體   English   中英

從C中的2D陣列讀取的鏡像

[英]Mirror image read from 2D array in C

我正在用C寫一個程序,其中我正在讀取帶有2D數組的bmp圖像。 我應該有作為輸出的原始圖像+逆旁邊對方。

我這里的代碼應該這樣做,但圖像在彼此的頂部打印。 如果有人知道如何解決該問題,我將為您提供幫助。

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:我正在GCC上運行

您可以在此處找到BMP文件格式

關於支持不同版本的BMP,檢查分辨率,色深,調色板外觀等,您的裝載機並不完整。

但是對於您要使用它的目的,您可能有一個固定的輸入,總是相同的分辨率等。

但是您希望得到的圖像寬度是原來的兩倍。 因此,標頭中的一些細節必須更改:

“文件大小”
“位圖寬度(以像素為單位)(有符號整數)”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM