[問題] C語言讀BMP圖檔後顯示資訊
以下是讀檔的程式碼
#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
→
06/16 19:45, , 1F
06/16 19:45, 1F
→
06/16 21:23, , 2F
06/16 21:23, 2F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 3 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章