[C語言][24位元BMP][如何存取每個像素的RGB值]

以下是我各方搜尋外加自己撰寫後的程式碼

他能夠讀取800*600大小的BMP檔並複製出一個一模一樣的BMP檔出來

但好笑的是BMP的每個像素顏色竟然只能是在256色內的色點才行

為什麼這樣說呢? 原因是因為我一開始先開啟windows的小畫家隨手塗鴉

並以24位元方式儲存,再執行程式後也順利複製出一個BMP檔案

但是當我去下載一個一樣800*600的風景畫後再執行程式時既然離奇的只

會跑出空白的out.bmp檔,最後研究發現我必須先將風景畫開小畫家手動

存成256色BMP檔後再轉存為24位元BMP檔以上動作後才能夠順利複製

BMP圖片,請問到底是什麼原因一定要逼得我圖片失真(從一開始24位元

色轉成256色)後才能做這樣的轉換。 感恩

------------------------------------------------------------------------------------------------------

#include <cstdio>

#define DUMP 54

#define Height 800

#define Width 600

main(){

FILE *inputBMP = fopen( "in.bmp", "r" ),

*outputBMP = fopen( "out.bmp", "w" );

int i, j, k ;

char chr ;

char BGR[3];

//將指標過濾到像素的第一位

for( i = 0 ; i < DUMP ; i++ )

{

chr = fgetc ( inputBMP );

fputc( chr, outputBMP );

}

for( i = 0 ; i < Height ; i++ )

{

for( j = 0 ; j < Width ; j++ )

{

for( k = 0 ; k < 3 ; k++ )

{

BGR[k] = fgetc( inputBMP );

fputc( (unsigned char)BGR[k], outputBMP );

}

}

}

fclose( inputBMP );

fclose( outputBMP );}

-------------------------------------------------------------------------------

已更新項目:

拜託 我是來問程式的 我知道有些BMP是以4為一組,但是我不知道程式碼要怎麼寫啊....

2 個解答

評分
  • Chen
    Lv 6
    7 年前
    最佳解答

    Color processing is basic function for image processing.

    1. jpg file is converted into bmp file or Bmp file is converted into jpg file.

    2. 24bit colors file is converted into 16 bit colors file

    3 24bit colors file is converted into 12 bit colors file ( for tracking )

    4. 24bit colors file is converted into 8 bit colors file ( for example )

    5. 24bit colors file is converted into 8 bit grey level file

    6. 24bit colors file is converted into 4 bit grey level file

    7. 24bit colors file is converted into binary white & black file

    BMP format :

    file header ( 14) + info header( 40 ) + color palettes(1024) + image data/index

    image data is that data for 24 bit colors, but it is only index for 8 bit colors or less 8 colors.

    for example:

    original lena.bmp (24 bit colors bmp file)

    圖片參考:https://s.yimg.com/rk/AC08187637/o/89161970.bmp

    after my program( octree.exe ) lena8.bmp( 256 colors bmp file )

    圖片參考:https://s.yimg.com/rk/AC08187637/o/800385413.jpg

    2014-03-16 03:51:10 補充:

    Processing data in 24bit colors being converted into 256 colors adopts

    distance process, It creates many aliasing ( like MSPAINT package softare), I adopts tree method ( no aliasing ).

    2014-03-16 03:55:29 補充:

    attention 2thing:

    1. Your memory must be 4 multiple boundary, or skew picture or can't read or write in virtual memory.

    2.data of BMP format is from down up to top approach, JPEG(other)

    format is from top to down approach. so processing data must do inverse

    of data of BMP format.

  • 7 年前

    我記得.bmp有header.

    十年沒碰, 不要問我哪是啥.

    2014-03-19 08:44:22 補充:

    就跟你說bmp有header, 你就去查啊,

    不查怎麼知道程式怎麼寫.

還有問題?馬上發問,尋求解答。