[問題] 讀取bmp影像檔

看板MacDev作者 (浪漫鐵人8號 ￾ )時間17年前 (2007/07/26 12:39), 編輯推噓11(11019)
留言30則, 6人參與, 最新討論串1/2 (看更多)
請問一下 我利用C++ 藉由 opengl 讀取影像檔 我在windows的VC++6.0 下可以讀取 可是在mac 的XCode (C++ tool) 下就無法讀取了 請問是何種原因 以下為source code 取材自opengl超級聖經 第二版 請問是哪裏有問題 ///// source code 開始 LoadDIBitmap(const char *filename, /* I - File to load */ BITMAPINFO **info) /* O - Bitmap information */ { FILE *fp; /* Open file pointer */ GLubyte *bits; /* Bitmap pixel bits */ int bitsize; /* Size of bitmap */ int infosize; /* Size of header information */ BITMAPFILEHEADER header; /* File header */ /* Try opening the file; use "rb" mode to read this *binary* file. */ if ((fp = fopen(filename, "rb")) == NULL) return (NULL); /* Read the file header and any following bitmap information... */ if (fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1) { /* Couldn't read the file header - return NULL... */ fclose(fp); return (NULL); } if (header.bfType != 'MB') /* Check for BM reversed... */ { /* Not a bitmap file - return NULL... */ fclose(fp); return (NULL); } infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER); if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL) { /* Couldn't allocate memory for bitmap info - return NULL... */ fclose(fp); return (NULL); } if (fread(*info, 1, infosize, fp) < infosize) { /* Couldn't read the bitmap header - return NULL... */ free(*info); fclose(fp); return (NULL); } /* Now that we have all the header info read in, allocate memory for * * the bitmap and read *it* in... */ if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0) bitsize = ((*info)->bmiHeader.biWidth * (*info)->bmiHeader.biBitCount + 7) / 8 * abs((*info)->bmiHeader.biHeight); if ((bits = malloc(bitsize)) == NULL) { /* Couldn't allocate memory - return NULL! */ free(*info); fclose(fp); return (NULL); } if (fread(bits, 1, bitsize, fp) < bitsize) { /* Couldn't read bitmap - free memory and return NULL! */ free(*info); free(bits); fclose(fp); return (NULL); } /* OK, everything went fine - return the allocated bitmap... */ fclose(fp); return (bits); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.96.77.105 ※ 編輯: wcmein 來自: 140.96.77.105 (07/26 13:11)

07/26 14:06, , 1F
我很懷疑這樣compile會過嗎?
07/26 14:06, 1F

07/26 14:20, , 2F
過也 不過這是readbmp.cpp 的檔 我沒列出readbmp.h的檔 因
07/26 14:20, 2F

07/26 14:22, , 3F
為這樣會很長 主要我是要拿來貼圖的 因此我需要load bmp
07/26 14:22, 3F

07/26 14:33, , 4F
有trace過code嗎?
07/26 14:33, 4F

07/26 14:33, , 5F
要不要先試著在每個return的地方設一下break point
07/26 14:33, 5F

07/26 14:34, , 6F
看看是不是真的如你所預期的?
07/26 14:34, 6F

07/26 17:09, , 7F
如果是PowerPC CPU, 是不是讀檔內容跟 endian 轉換的問題
07/26 17:09, 7F

07/26 17:17, , 8F
compiler過了 可是檢查MB那段 內容不是MB 然後程式就退出
07/26 17:17, 8F

07/26 17:18, , 9F
檢查後是這樣 該怎辦哩
07/26 17:18, 9F

07/26 17:59, , 10F
ppc or x86 ?
07/26 17:59, 10F

07/27 01:11, , 11F
問題很明確,這段code是給特定格式的bmp檔用的
07/27 01:11, 11F

07/27 01:13, , 12F
印象中,bmp檔也有不同的格式與header
07/27 01:13, 12F

07/27 01:14, , 13F
從你的描述中看來,應該是所讀取的header的內容,
07/27 01:14, 13F

07/27 01:14, , 14F
與原本windows所要求的不同
07/27 01:14, 14F

07/27 01:15, , 15F
建議1. 檢查檔案的header,確定是此一問題導致
07/27 01:15, 15F

07/27 01:16, , 16F
2.若是,重寫header的剖析,以及讀檔時的各項判斷式
07/27 01:16, 16F

07/27 01:17, , 17F
加油囉..
07/27 01:17, 17F

07/27 09:23, , 18F
是ppc 重寫header的剖析 我查查範例了
07/27 09:23, 18F

07/27 11:14, , 19F
header.bfType != 'MB' -> header.bfType != 'BM'
07/27 11:14, 19F

07/27 11:31, , 20F
改成BM也不行 檢查後bits=0因此讀取失敗 請問大家 有讀取
07/27 11:31, 20F

07/27 11:33, , 21F
影像檔的source code 或libarary嗎 在mac下的
07/27 11:33, 21F

07/27 11:35, , 22F
想放棄這個source code了
07/27 11:35, 22F

07/27 12:31, , 23F
建議你,在header.bfType != "MB"
07/27 12:31, 23F

07/27 12:32, , 24F
設中斷點檢查一下.
07/27 12:32, 24F

07/27 12:33, , 25F
header.bfType的型態,值各為何?
07/27 12:33, 25F

07/27 12:34, , 26F
若改用別的API當然也可以,但這樣的話,
07/27 12:34, 26F

07/27 12:35, , 27F
你對Debug的方式不會有進步
07/27 12:35, 27F

07/27 12:35, , 28F
還是試著使用Debug工具一步步把問題找出來吧...
07/27 12:35, 28F

07/27 13:25, , 29F
如果只是想讀bmp, 參考http://0rz.tw/2c2V3
07/27 13:25, 29F

07/28 08:24, , 30F
謝了 我在試試看好了
07/28 08:24, 30F
文章代碼(AID): #16g2La11 (MacDev)
文章代碼(AID): #16g2La11 (MacDev)