前两天,在使用位图当纹理时候,遇到了问题.就是关于8-bit位图不能派上用场时候,于是就萌生了想法,希望能将它完善下整理出来.于是查了一些关于bitmap的文件结构.关于位图结构等,我就不在此详细叙述了.因为网上已经相当的多 而且很多人也实现了很多读取的方法.可参考此篇博文:位图综述
其实呢.我原先并不知道原来已经有了很强大的一个开源工具..FreeImage ..是个已经支持目前所有格式的图像文件..所以我的基本算是白写了.关于1 4 16 32 的完全可参考其源代码 完善下我以下这个类:
现在仅将我写了一个下午的源代码献上:详细情况不在综述,全部在类里表明了注释:
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
BmpLoader.h
//======================================================================
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**
* @file BmpLoader.h
*
* 文件描述: 载入位图类
* 适用平台: Windows98/2000/NT/XP
*
* 作者: acmiyou
* 电子邮件: acmiyou@qq.com
*
*/
//======================================================================
#include <windows.h>
#include <stdio.h>
#ifndef _BMPLOADER_H_
#define _BMPLOADER_H_
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#define BITMAP_ID 0x4D42 /**< 位图文件的标志 */
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#define BLUE 0
#define GREEN 1
#define RED 2
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#define swapcolor(a,b){ \
(a) ^= (b); \
(b) ^= (a); \
(a) ^= (b); \
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
class BmpLoader
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
BmpLoader();
~BmpLoader();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void Free(); /**//** 释放内存*/
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
BITMAPINFOHEADER* bitInfo; /**//** 记录位图的头信息*/
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
BYTE* image; /**//** 图像数据*/
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
bool loadBitmap(const char* filename); /**//** 载入位图文件*/
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
private:
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void computePaletteSize(); /**//** 计算位图中颜色表项数*/
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int getImageLine(int width,int bitCount); /**//** 计算每行的字节数,结果为4的倍数*/
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
BYTE* getScanLine(BYTE* data ,int pitch,int scanline); /**//** 获得像素数据每行的起始地址*/
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
bool covertTo24bits(BYTE* data); /**//** 强制转换所有类型至24-bit*/
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void covertLine8bTo24b(BYTE* dst,BYTE* src); /**//** 转换8-bit行为24-bit行 */
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
RGBQUAD* p_ColorTable; /**//** 颜色表*/
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#endif
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
BmpLoader.cpp
//======================================================================
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**
* @file BmpLoader.cpp
*
* 文件描述: 载入位图类
* 适用平台: Windows98/2000/NT/XP
*
* 作者: acmiyou
* 电子邮件: acmiyou@qq.com
*
*/
//======================================================================
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include "BmpLoader.h"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
BmpLoader::BmpLoader()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
bitInfo= 0;
image = 0;
p_ColorTable = 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
BmpLoader::~BmpLoader()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
Free();
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**
** 释放占用内存
*/
void BmpLoader::Free()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if(image!=NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
delete[] image;
image = 0;
}
if(bitInfo!=NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
delete[] bitInfo;
bitInfo = 0;
}
if(p_ColorTable !=NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
delete[] p_ColorTable;
p_ColorTable = 0;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
** 载入位图文件
** @param:
** filename 文件名
*/
bool BmpLoader::loadBitmap(const char *filename)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
/** 如果已经存在数据 则释放内存*/
Free(); /** 释放内存*/
FILE* fp= fopen(filename,"rb");
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(fp==NULL) return false; /**//** 打开文件失败*/
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
BITMAPFILEHEADER header;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
fread(&header,sizeof(BITMAPFILEHEADER),1,fp); /**//** 读入bitmap文件头*/
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(header.bfType !=BITMAP_ID) /**//** 若不是 位图类型 直接返回*/
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Free();
fclose(fp);
return false;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
bitInfo=(BITMAPINFOHEADER*)new BYTE[sizeof(BITMAPINFOHEADER)];
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
fread(bitInfo,sizeof(BITMAPINFOHEADER),1,fp); /**//** 位图信息头*/
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(bitInfo->biSize !=40)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Free();
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return false; /**//** 不是RGB格式 而是压缩的 该类不支持 返回读取失败 */
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
computePaletteSize(); /**//** 计算颜色表值*/
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(bitInfo->biClrUsed !=0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
p_ColorTable=(RGBQUAD*)new BYTE[sizeof(RGBQUAD)*bitInfo->biClrUsed];
fread(p_ColorTable,sizeof(RGBQUAD) * bitInfo->biClrUsed,1,fp);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//** 位图像素数据大小 */
bitInfo->biSizeImage=getImageLine(bitInfo->biWidth,bitInfo->biBitCount) * bitInfo->biHeight;
BYTE* imagedata = new BYTE[bitInfo->biSizeImage];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
fseek(fp,header.bfOffBits,SEEK_SET);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//** 读取像素信息*/
fread(imagedata,bitInfo->biSizeImage,1,fp);
fclose(fp);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//** 强制转换成24-bit*/
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(!covertTo24bits(imagedata)) /**//** 不支持 1 4 16 32位格式 则返回*/
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Free();
return false;
}
delete[] imagedata;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//** 转换bgr --> rgb*/
for(int inx= 0;inx < bitInfo->biSizeImage;inx+=3)
swapcolor(image[inx],image[inx+2]);
return true;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
** 计算位图中颜色表项数
** @param: 无
*/
void BmpLoader::computePaletteSize()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//** 仅 1 4 8 存在颜色表*/
if(bitInfo->biClrUsed==0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch(bitInfo->biBitCount)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 1:
bitInfo->biClrUsed=2;break;
case 4:
bitInfo->biClrUsed=16;break;
case 8:
bitInfo->biClrUsed=256;break;
case 16:
case 24:
case 32:
bitInfo->biClrUsed=0;break;
default:
break;
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
** 计算每行的字节数
** @param:
** width 宽度
** bitCount 每个像素的位数
** @return: 行字节数
*/
int BmpLoader::getImageLine(int width,int bitCount)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return ((width * bitCount) + 7) / 8 + 3 & ~3;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//** 获得像素数据每行的起始地址
** @param:
** data 像素数据
** pitch 像素行字节数
** scanline 第几行
** @return: 行起始地址
*/
BYTE* BmpLoader::getScanLine(BYTE* data ,int pitch, int scanline)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return data + pitch * scanline;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
** 强制转换所有类型至24-bit
** @param:
** data 像素数据
*/
bool BmpLoader::covertTo24bits(BYTE *data)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if(bitInfo==NULL || data == NULL) return false;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int lineImage = getImageLine(bitInfo->biWidth,24);
int lineData = getImageLine(bitInfo->biWidth,bitInfo->biBitCount);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
bitInfo->biSizeImage =lineImage * bitInfo->biHeight ;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
image = new BYTE[bitInfo->biSizeImage];
memset(image,0,bitInfo->biSizeImage);
switch(bitInfo->biBitCount)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 1:return false;
case 4:return false;
case 8:
for(int row = 0; row < bitInfo->biHeight; row++)
covertLine8bTo24b(getScanLine(image,lineImage,row),getScanLine(data,lineData,row));
break;
case 16:
return false;
case 24:
memcpy(image,data,bitInfo->biSizeImage);
break;
case 32:
return false;
}
return true;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
** 转换8-bit行为24-bit行
** @param:
** dst 目标地址
** src 源地址
*/
void BmpLoader::covertLine8bTo24b(BYTE *dst,BYTE *src)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
for(int col = 0; col < bitInfo->biWidth ; col++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//** 将索引值转换成颜色表中的颜色*/
dst[BLUE] = p_ColorTable[src[col]].rgbBlue;
dst[GREEN] =p_ColorTable[src[col]].rgbGreen;
dst[RED] =p_ColorTable[src[col]].rgbRed;
dst+=3;
}
}
posted on 2009-07-31 15:30
米游 阅读(2562)
评论(2) 编辑 收藏 引用 所属分类:
OpenGL/OSG