作者SiriusJinn (假斯汀)
看板C_and_CPP
標題[問題] C語言讀BMP圖檔後顯示資訊
時間Tue Jun 16 15:36:43 2009
以下是讀檔的程式碼
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int bmp_read(unsigned char *image, int xsize, int ysize, char *filename)
{
FILE *fp;
char fname_bmp[128];
long i, j;
unsigned char *image_buf;
unsigned char header[54];
image_buf = (unsigned char *)malloc((size_t)xsize*ysize*3);
if (image_buf == NULL) return -1;
sprintf(fname_bmp, "%s.bmp", filename);
if ((fp = fopen(fname_bmp, "rb")) == NULL) return -1;
fread(header, sizeof(unsigned char), 54, fp);
fread(image_buf, sizeof(unsigned char), (size_t)(long)xsize*ysize*3, fp);
fclose(fp);
for (i = 0; i < ysize; i++){
for (j = 0; j < xsize; j++){
*(image + xsize*(ysize-i-1) + j)
= *(image_buf + 3*(xsize*i +j) +2);
*(image + xsize*(ysize-i-1) + j + xsize*ysize)
= *(image_buf + 3*(xsize*i +j) +1);
*(image + xsize*(ysize-i-1) + j + xsize*ysize*2)
= *(image_buf + 3*(xsize*i +j) );
}
}
free(image_buf);
return 0;
}
int main()
{
unsigned char *image;
int xsize = 512;
int ysize = 512;
image = (unsigned char *)malloc((size_t)xsize * ysize * 3);
if (image == NULL)
return -1;
bmp_read(image, xsize, ysize, "lena.bmp");
free(image);
}
想請問如果我想把一些資料如 BM size width height 輸出在螢幕的話
printf該怎麼寫呢?我read的部份有把 file-header 跟 information-header
存進來了嗎?
綠色部份的作用是什麼,可否幫解釋一下!
感謝!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.120.90.174
→ Picat:倒立圖->正立圖 BGR->RGB 06/16 19:45
→ typeotoco:你就把header那54byte想讀的地方copy出來 就有了 06/16 21:23