C语言实现BMP图像的写入与图像的清空(256色灰度图)
#include"afx.h"
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)//保证每行数据占用的空间是4的倍数
class CImg
{
public:
//构造函数
CImg();
//析构函数
virtual ~CImg();
//重载“=”操作符来创建新对象
void operator=(CImg& gray);
public:
//判断位图是否有效
BOOL IsValidate() { return m_pBMIH != NULL; }
//将所有图像的值初始化为color
void InitPixels(BYTE color);
// 从文件加载位图
BOOL AttachFromFile(LPCTSTR lpcPathName);
BOOL AttachFromFile(CFile &file);
// 将位图保存到文件
BOOL SaveToFile(LPCTSTR lpcPathName);
BOOL SaveToFile(CFile &file);
//设置像素得值
void SetPixel(int x, int y, COLORREF);
// 获取一行的像素数
int GetWidthPixel();
// 获取高度
int GetHeight();
LPVOID GetColorTable() { return m_lpvColorTable; }
int GetColorTableEntriesNum() { return m_nColorTableEntries; }
private:
void CleanUp();
public:
// 文件数据
BITMAPINFOHEADER *m_pBMIH;
LPBYTE *m_lpData;
protected:
int m_nColorTableEntries;
LPVOID m_lpvColorTable;
};
CImg::CImg()
{
m_pBMIH = NULL;
m_lpData = NULL;
m_lpvColorTable = NULL;
}
CImg::~CImg()
{
CleanUp();//程序退出前清空数据
}
void CImg::operator = (CImg& gray)
{
CleanUp();
m_nColorTableEntries = gray.m_nColorTableEntries;
if (gray.m_pBMIH != NULL)
{
m_pBMIH = (BITMAPINFOHEADER*)new BYTE[sizeof(BITMAPINFOHEADER) + m_nColorTableEntries * 4];
memcpy(m_pBMIH