windows游戏编程大师第七章一个位图读取函数的问题

woshi420 2010-03-26 08:02:23
书本里面第七章有个读取位图的函数int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)
这个函数用来读取书本光盘里面的图片没问题
但是我网上下载的图片就读取出错 会弹出内存不能为read

int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)
{
// this function opens a bitmap file and loads the data into bitmap

int file_handle, // the file handle
index; // looping index

UCHAR *temp_buffer = NULL; // used to convert 24 bit images to 16 bit
OFSTRUCT file_data; // the file data information

// open the file if it exists
if ((file_handle = OpenFile(filename,&file_data,OF_READ))==-1)
return(0);

// now load the bitmap file header
_lread(file_handle, &bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));

// test if this is a bitmap file
if (bitmap->bitmapfileheader.bfType!=BITMAP_ID)
{
// close the file
_lclose(file_handle);

// return error
return(0);
} // end if

// now we know this is a bitmap, so read in all the sections

// first the bitmap infoheader

// now load the bitmap file header
_lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));

// now load the color palette if there is one
if (bitmap->bitmapinfoheader.biBitCount == 8)
{
_lread(file_handle, &bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));

// now set all the flags in the palette correctly and fix the reversed
// BGR RGBQUAD data format
for (index=0; index < MAX_COLORS_PALETTE; index++)
{
// reverse the red and green fields
int temp_color = bitmap->palette[index].peRed;
bitmap->palette[index].peRed = bitmap->palette[index].peBlue;
bitmap->palette[index].peBlue = temp_color;

// always set the flags word to this
bitmap->palette[index].peFlags = PC_NOCOLLAPSE;
} // end for index

} // end if

// finally the image data itself
_lseek(file_handle,-(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);

// now read in the image, if the image is 8 or 16 bit then simply read it
// but if its 24 bit then read it into a temporary area and then convert
// it to a 16 bit image

if (bitmap->bitmapinfoheader.biBitCount==8 || bitmap->bitmapinfoheader.biBitCount==16 ||
bitmap->bitmapinfoheader.biBitCount==24)
{
// delete the last image if there was one
if (bitmap->buffer)
free(bitmap->buffer);

// allocate the memory for the image
if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
{
// close the file
_lclose(file_handle);

// return error
return(0);
} // end if

// now read it in
_lread(file_handle,bitmap->buffer,bitmap->bitmapinfoheader.biSizeImage);

} // end if
else
{
// serious problem
return(0);

} // end else
}


要是把
_lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));
这一行去掉的话就不会弹出内存不能为read的警告 不过图片是显示不了的
...全文
165 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
m261614329 2010-11-14
  • 打赏
  • 举报
回复
把图片放到photoshop里转换成8位位图,并且索取颜色使颜色少于256就可以了,应该使你的图片颜色太多了
nicklisir 2010-11-14
  • 打赏
  • 举报
回复
_lread只能用于16位文件的读取,在32位中会出错;可以用_open,_read取代
我以前用哪个也是,把工程改成UNICODE也一样,后来把函数改了就可以了
harleypang 2010-11-14
  • 打赏
  • 举报
回复
位图的格式不对耶!

按楼上那位所说的用ps或附件中画图把图的格式改一下,应该就没问题了
just_run_run_run 2010-11-14
  • 打赏
  • 举报
回复
if (bitmap->buffer)
free(bitmap->buffer);
还有这2句也注释掉
just_run_run_run 2010-11-14
  • 打赏
  • 举报
回复
_lseek(file_handle,-(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);

把这句注释掉!我也在看这本书啊,有空交流下,LZ给个QQ啊
huang0596 2010-08-07
  • 打赏
  • 举报
回复
图片是bmp格式的吗
dx62588183 2010-08-07
  • 打赏
  • 举报
回复
很简单的问题 那是因为你那个开辟的内存是UCHAR也就是一个字节的呀 如果不是8位图片 你却用1个字节位来储存RGB值的话 当然会内存泄露的哟
shtianhai 2010-03-26
  • 打赏
  • 举报
回复
内存不能为read的警告,不知是哪里的,要跟一下才可以,不是你把这行
_lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));
去掉的问题,这行是读取bitmap的头部信息,即bitmapinfoheader,其中包含了位图的基本信息,具体为BITMAP数据结构和COLOR调色板,BITMAP中有宽高,色深,压缩方式等基本信息。很有看你和你下载的图片有关系。其实并非所有的位图均为fileheader部分和infoheader头部的。LZ可以看看windows中关于DIB和DDB的讲解(windows程序设计一书中有),读取位图也可以用windows中现有的函数读取分析。LZ也可以看看关于bitmap位图的文件格式的解析。
woshi420 2010-03-26
  • 打赏
  • 举报
回复
是位图 8位
dubiousway 2010-03-26
  • 打赏
  • 举报
回复
但是我网上下载的图片就读取出错 会弹出内存不能为read

你下载的图片是位图吗。(就是扩展名是bmp的)。jpeg 是不能读的

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧