作者wcmein (浪漫鐵人8號 )
看板MacDev
標題[問題] 讀取bmp影像檔
時間Thu Jul 26 12:38:59 2007
請問一下
我利用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)
1F:推 anpig:我很懷疑這樣compile會過嗎? 07/26 14:06
2F:→ wcmein:過也 不過這是readbmp.cpp 的檔 我沒列出readbmp.h的檔 因 07/26 14:20
3F:→ wcmein:為這樣會很長 主要我是要拿來貼圖的 因此我需要load bmp 07/26 14:22
4F:推 atst:有trace過code嗎? 07/26 14:33
5F:→ atst:要不要先試著在每個return的地方設一下break point 07/26 14:33
6F:→ atst:看看是不是真的如你所預期的? 07/26 14:34
7F:推 jclin:如果是PowerPC CPU, 是不是讀檔內容跟 endian 轉換的問題 07/26 17:09
8F:推 wcmein:compiler過了 可是檢查MB那段 內容不是MB 然後程式就退出 07/26 17:17
9F:→ wcmein:檢查後是這樣 該怎辦哩 07/26 17:18
10F:推 HalfLucifer:ppc or x86 ? 07/26 17:59
11F:推 atst:問題很明確,這段code是給特定格式的bmp檔用的 07/27 01:11
12F:→ atst:印象中,bmp檔也有不同的格式與header 07/27 01:13
13F:→ atst:從你的描述中看來,應該是所讀取的header的內容, 07/27 01:14
14F:→ atst:與原本windows所要求的不同 07/27 01:14
15F:→ atst:建議1. 檢查檔案的header,確定是此一問題導致 07/27 01:15
16F:→ atst:2.若是,重寫header的剖析,以及讀檔時的各項判斷式 07/27 01:16
17F:→ atst:加油囉.. 07/27 01:17
18F:→ wcmein:是ppc 重寫header的剖析 我查查範例了 07/27 09:23
19F:推 Blueshiva:header.bfType != 'MB' -> header.bfType != 'BM' 07/27 11:14
20F:推 wcmein:改成BM也不行 檢查後bits=0因此讀取失敗 請問大家 有讀取 07/27 11:31
21F:→ wcmein:影像檔的source code 或libarary嗎 在mac下的 07/27 11:33
22F:→ wcmein:想放棄這個source code了 07/27 11:35
23F:推 atst:建議你,在header.bfType != "MB" 07/27 12:31
24F:→ atst:設中斷點檢查一下. 07/27 12:32
25F:→ atst:header.bfType的型態,值各為何? 07/27 12:33
26F:→ atst:若改用別的API當然也可以,但這樣的話, 07/27 12:34
27F:→ atst:你對Debug的方式不會有進步 07/27 12:35
28F:→ atst:還是試著使用Debug工具一步步把問題找出來吧... 07/27 12:35
30F:推 wcmein:謝了 我在試試看好了 07/28 08:24