[問題] C語言讀BMP圖檔後顯示資訊

看板C_and_CPP (C/C++)作者 (假斯汀)時間16年前 (2009/06/16 15:36), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/3 (看更多)
以下是讀檔的程式碼 #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
倒立圖->正立圖 BGR->RGB
06/16 19:45, 1F

06/16 21:23, , 2F
你就把header那54byte想讀的地方copy出來 就有了
06/16 21:23, 2F
文章代碼(AID): #1ADqkEsF (C_and_CPP)
文章代碼(AID): #1ADqkEsF (C_and_CPP)