这基本上是按照我的使用情况来做的一个位图类,里面包括一些ijl库的使用方法,(以前想专门写个关于ijl的文章,可是没时间,加上自己也懒,所以就不写了,自己看吧)只是希望抛砖引玉,结构上没什么说的,直线型结构,如果你的情况不同,请自行修改,希望这个代码能加快你写代码的速度。另外,我主要对合并位图的函数不满意,太零乱了,而且合并多幅位图很麻烦,谁有更好办法,提出来一起讨论吧。另外感谢晓寒教我如何贴代码,效果还不错,^_^.
/H/
#ifndef _LGBITMAP_H_
#define _LGBITMAP_H_
#include " ijl.h "
#include < vfw.h >
namespace lglib
{
class __LGLIB__ lg_Bitmap : public CGdiObject
{
DECLARE_DYNAMIC(lg_Bitmap)
public:
BOOL Draw(HWND hWnd);
static lg_Bitmap* PASCAL FromHandle(HBITMAP hBitmap);
// Constructors
lg_Bitmap();
//加载函数
BOOL LoadJpegBuffer(const uint8* JpgBuffer,uint32 JpgBufLen);
BOOL LoadJPEGFile(LPCTSTR lpszFilename);
//装载没有位图头的数据
BOOL LoadFromMemNoHead(BYTE* pData,int32 width,int32 height,uint16 nChannels);
//装载带有位图头的数据
BOOL LoadFromMem(BYTE* pData,unsigned long length);
//装载位图文件
BOOL LoadBitmap(LPCTSTR lpszFileName);
//创建位图
BOOL CreateBitmap(int nWidth, int nHeight, UINT nBitCount, const void* lpBits);
//
//直接创建位图
BOOL CreateBitmapIndirect(LPBITMAPINFO lpBitmapInfo, const void* lpBits);
// Attributes
operator HBITMAP() const;
int GetBitmap(BITMAP* pBitMap);
protected:
// Attributes
int GetColorNumber(WORD wBitCount);
public:
// Operations
DWORD SetBitmapBits(DWORD dwCount, const void* lpBits);
DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits);
// Implementation
private:
//编码DIB缓冲为JPEG缓冲
BOOL EncodeToJPEGBuffer(
BYTE* lpRgbBuffer,
int32 nWidth,
int32 nHeight,
BYTE** lppJpgBuffer,
DWORD* lpdwJpgBufferSize);
//编码DIB为JPEG文件
BOOL EncodeJPGFileFromDIB(
LPCSTR lpszPathName,
BITMAPINFOHEADER* bmih);
//解码JPEG文件
BOOL DecodeJPGFileToGeneralBuffer(
LPCTSTR lpszPathName,
int32* width,
int32* height,
uint16* nchannels,
BYTE** buffer,DWORD* ImageLen = NULL);
//解码JPEG缓冲
BOOL DecodeFromJPEGBuffer(
BYTE* lpJpgBuffer,
DWORD dwJpgBufferSize,
BYTE** lppRgbBuffer,
int32* lpdwWidth,
int32* lpdwHeight,
uint16* lpdwNumberOfChannels);
//DIB 数据 上下翻转
void OW_Rotate(BYTE *data/*纯DIB数据*/, DWORD scanline/*扫描行*/, DWORD height/*DIB 高度*/);
HDRAWDIB m_hDrawDib;
BITMAPINFO m_bmi;
public:
virtual ~lg_Bitmap();
};
}
#endif
#define _LGBITMAP_H_
#include " ijl.h "
#include < vfw.h >
namespace lglib
{
class __LGLIB__ lg_Bitmap : public CGdiObject
{
DECLARE_DYNAMIC(lg_Bitmap)
public:
BOOL Draw(HWND hWnd);
static lg_Bitmap* PASCAL FromHandle(HBITMAP hBitmap);
// Constructors
lg_Bitmap();
//加载函数
BOOL LoadJpegBuffer(const uint8* JpgBuffer,uint32 JpgBufLen);
BOOL LoadJPEGFile(LPCTSTR lpszFilename);
//装载没有位图头的数据
BOOL LoadFromMemNoHead(BYTE* pData,int32 width,int32 height,uint16 nChannels);
//装载带有位图头的数据
BOOL LoadFromMem(BYTE* pData,unsigned long length);
//装载位图文件
BOOL LoadBitmap(LPCTSTR lpszFileName);
//创建位图
BOOL CreateBitmap(int nWidth, int nHeight, UINT nBitCount, const void* lpBits);
//
//直接创建位图
BOOL CreateBitmapIndirect(LPBITMAPINFO lpBitmapInfo, const void* lpBits);
// Attributes
operator HBITMAP() const;
int GetBitmap(BITMAP* pBitMap);
protected:
// Attributes
int GetColorNumber(WORD wBitCount);
public:
// Operations
DWORD SetBitmapBits(DWORD dwCount, const void* lpBits);
DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits);
// Implementation
private:
//编码DIB缓冲为JPEG缓冲
BOOL EncodeToJPEGBuffer(
BYTE* lpRgbBuffer,
int32 nWidth,
int32 nHeight,
BYTE** lppJpgBuffer,
DWORD* lpdwJpgBufferSize);
//编码DIB为JPEG文件
BOOL EncodeJPGFileFromDIB(
LPCSTR lpszPathName,
BITMAPINFOHEADER* bmih);
//解码JPEG文件
BOOL DecodeJPGFileToGeneralBuffer(
LPCTSTR lpszPathName,
int32* width,
int32* height,
uint16* nchannels,
BYTE** buffer,DWORD* ImageLen = NULL);
//解码JPEG缓冲
BOOL DecodeFromJPEGBuffer(
BYTE* lpJpgBuffer,
DWORD dwJpgBufferSize,
BYTE** lppRgbBuffer,
int32* lpdwWidth,
int32* lpdwHeight,
uint16* lpdwNumberOfChannels);
//DIB 数据 上下翻转
void OW_Rotate(BYTE *data/*纯DIB数据*/, DWORD scanline/*扫描行*/, DWORD height/*DIB 高度*/);
HDRAWDIB m_hDrawDib;
BITMAPINFO m_bmi;
public:
virtual ~lg_Bitmap();
};
}
#endif
CPP///
#include
"
stdafx.h
"
#include " Lg_Bitmap.h "
#include < memory.h >
#pragma comment(lib, " ijl15.lib " )
#pragma comment(lib, " vfw32.lib " )
namespace lglib
{
IMPLEMENT_DYNAMIC(lg_Bitmap,CGdiObject);
//----------------------------------------------------------
// An example using the Intel(R) JPEG Library:
// -- 编码 DIB 为 JPEG 缓冲.
//----------------------------------------------------------
BOOL lg_Bitmap::EncodeToJPEGBuffer(
BYTE* lpRgbBuffer,
int32 nWidth,
int32 nHeight,
BYTE** lppJpgBuffer,
DWORD* lpdwJpgBufferSize)
{
BOOL bres;
IJLERR jerr;
DWORD dwRgbBufferSize;
BYTE* lpTemp;
// Allocate the IJL JPEG_CORE_PROPERTIES structure.
JPEG_CORE_PROPERTIES jcprops;
bres = TRUE;
__try
{
// Initialize the Intel(R) JPEG Library.
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
jcprops.DIBWidth = nWidth;
jcprops.DIBHeight = -nHeight; // Implies a bottom-up DIB.
jcprops.DIBBytes = lpRgbBuffer + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
jcprops.DIBChannels = 3;
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.DIBWidth, jcprops.DIBChannels);
jcprops.DIBColor = IJL_BGR;//;
dwRgbBufferSize = (jcprops.DIBWidth * jcprops.DIBChannels + jcprops.DIBPadBytes) * abs(jcprops.DIBHeight);
lpTemp = new BYTE [dwRgbBufferSize];
if(NULL == lpTemp)
{
bres = FALSE;
__leave;
}
// Set up information to write from the pixel buffer.
jcprops.JPGWidth = jcprops.DIBWidth;
jcprops.JPGHeight = abs(jcprops.DIBHeight);
jcprops.JPGFile = NULL;
jcprops.JPGBytes = lpTemp;
jcprops.JPGSizeBytes = dwRgbBufferSize;
jcprops.JPGChannels = 3;
jcprops.JPGColor = IJL_YCBCR;
jcprops.JPGSubsampling = IJL_411; // 4:1:1 subsampling.
jcprops.jquality = 75; // Select "good" image quality
// Write the actual JPEG image from the pixel buffer.
jerr = ijlWrite(&jcprops,IJL_JBUFF_WRITEWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
} // __try
__finally
{
if(FALSE == bres)
{
if(NULL != lpTemp)
{
delete[] lpTemp;
lpTemp = NULL;
}
}
*lppJpgBuffer = lpTemp;
*lpdwJpgBufferSize = jcprops.JPGSizeBytes;
// Clean up the Intel(R) JPEG Library.
ijlFree(&jcprops);
}
return bres;
} // EncodeToJPEGBuffer()
//----------------------------------------------------------
// An example using the IntelR JPEG Library:
// -- 将 DIB 保存为 JPG 文件.
//----------------------------------------------------------
BOOL lg_Bitmap::EncodeJPGFileFromDIB(
LPCSTR lpszPathName,
BITMAPINFOHEADER* bmih)
{
BOOL bres;
IJLERR jerr;
// Allocate the IJL JPEG_CORE_PROPERTIES structure.
JPEG_CORE_PROPERTIES jcprops;// = new JPEG_CORE_PROPERTIES;
bres = TRUE;
__try
{
// Initialize the IntelR JPEG Library.
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
if(bmih->biBitCount != 24)
{
// not supported palette images
bres = FALSE;
__leave;
}
// Set up information to write from the pixel buffer.
jcprops.DIBWidth = bmih->biWidth;
jcprops.DIBHeight = -bmih->biHeight; // Implies a bottom-up DIB.
jcprops.DIBBytes = reinterpret_cast<BYTE*>(bmih) + sizeof(BITMAPINFOHEADER);
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(bmih->biWidth,3);
// Note: the following are default values and thus
// do not need to be set.
jcprops.DIBChannels = 3;
jcprops.DIBColor = IJL_BGR;
jcprops.JPGFile = const_cast<LPSTR>(lpszPathName);
// Specify JPEG file creation parameters.
jcprops.JPGWidth = bmih->biWidth;
jcprops.JPGHeight = bmih->biHeight;
// Note: the following are default values and thus
// do not need to be set.
jcprops.JPGChannels = 3;
jcprops.JPGColor = IJL_YCBCR;
jcprops.JPGSubsampling = IJL_411; // 4:1:1 subsampling.
jcprops.jquality = 75; // Select "good" image quality
// Write the actual JPEG image from the pixel buffer.
jerr = ijlWrite(&jcprops,IJL_JFILE_WRITEWHOLEIMAGE);//IJL_JFILE_WRITEWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
} // __try
__finally
{
// Clean up the IntelR JPEG Library.
ijlFree(&jcprops);
}
return bres;
} // EncodeJPGFileFromDIB()
//----------------------------------------------------------
// An example using the Intel(R) JPEG Library:
// -- 将 JPEG 缓冲解码为 DIB缓冲
//----------------------------------------------------------
BOOL lg_Bitmap::DecodeFromJPEGBuffer(
BYTE* lpJpgBuffer,
DWORD dwJpgBufferSize,
BYTE** lppRgbBuffer,
int32* lpdwWidth,
int32* lpdwHeight,
uint16* lpdwNumberOfChannels)
{
BOOL bres;
IJLERR jerr;
DWORD dwWholeImageSize;
BYTE* lpTemp = NULL;
// Allocate the IJL JPEG_CORE_PROPERTIES structure.
JPEG_CORE_PROPERTIES jcprops;
bres = TRUE;
__try
{
// Initialize the Intel(R) JPEG Library.
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
// Get information on the JPEG image
// (i.e., width, height, and channels).
jcprops.JPGFile = NULL;
jcprops.JPGBytes = lpJpgBuffer;
jcprops.JPGSizeBytes = dwJpgBufferSize;
jerr = ijlRead(&jcprops, IJL_JBUFF_READPARAMS);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
// Set the JPG color space this will always be
// somewhat of an educated guess at best because JPEG
// is "color blind" (i.e., nothing in the bit stream
// tells you what color space the data was encoded from).
// However, in this example we assume that we are
// reading JPEG files which means that 3 channel images
// are in the YCbCr color space and 1 channel images are
// in the Y color space.
switch(jcprops.JPGChannels)
{
case 1:
{
jcprops.JPGColor = IJL_G;
jcprops.DIBColor = IJL_RGB;
jcprops.DIBChannels = 3;
break;
}
case 3:
{
jcprops.JPGColor = IJL_YCBCR;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBChannels = 3;
break;
}
default:
{
// This catches everything else, but no
// color twist will be performed by the IJL.
jcprops.JPGColor = IJL_OTHER;
jcprops.DIBColor = IJL_OTHER;
jcprops.DIBChannels = jcprops.JPGChannels;
break;
}
}
// Compute size of desired pixel buffer.
jcprops.DIBWidth = jcprops.JPGWidth;
jcprops.DIBHeight = jcprops.JPGHeight;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.JPGWidth, jcprops.DIBChannels);
// Compute size of desired pixel buffer.
dwWholeImageSize = (jcprops.DIBWidth * jcprops.DIBChannels + jcprops.DIBPadBytes) * jcprops.DIBHeight;
lpTemp = new BYTE [dwWholeImageSize];
memset(lpTemp,0,dwWholeImageSize);
if(NULL == lpTemp)
{
bres = FALSE;
__leave;
}
jcprops.DIBBytes = lpTemp;
// Now get the actual JPEG image data into the pixel buffer.
jerr = ijlRead(&jcprops, IJL_JBUFF_READWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
} // __try
__finally
{
if(FALSE == bres)
{
if(NULL != lpTemp)
{
delete [] lpTemp;
lpTemp = NULL;
}
}
// Clean up the Intel(R) JPEG Library.
ijlFree(&jcprops);
*lpdwWidth = jcprops.DIBWidth;
*lpdwHeight = jcprops.DIBHeight;
*lpdwNumberOfChannels = jcprops.DIBChannels;
*lppRgbBuffer = lpTemp;
} // __finally
return bres;
} // DecodeFromJPEGBuffer()
//----------------------------------------------------------
// An example using the Intel(R) JPEG Library:
// -- 解码 JPG文件 为 DIB 缓冲
//----------------------------------------------------------
BOOL lg_Bitmap::DecodeJPGFileToGeneralBuffer(
LPCTSTR lpszPathName,
int32* width,
int32* height,
uint16* nchannels,
BYTE** buffer,DWORD* ImageLen)
{
BOOL bres;
IJLERR jerr;
DWORD wholeimagesize;
BYTE* pixel_buf = NULL;
// Allocate the IJL JPEG_CORE_PROPERTIES structure.
JPEG_CORE_PROPERTIES jcprops;
bres = TRUE;
__try
{
// Initialize the IntelR JPEG Library.
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
// Get information on the JPEG image
// (i.e., width, height, and channels).
jcprops.JPGFile = const_cast<LPSTR>(lpszPathName);
jerr = ijlRead(&jcprops, IJL_JFILE_READPARAMS);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
// Set up local data.
jcprops.DIBWidth = jcprops.JPGWidth;
jcprops.DIBHeight = jcprops.JPGHeight;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.JPGWidth, jcprops.DIBChannels);
wholeimagesize = (jcprops.DIBWidth * jcprops.DIBChannels + jcprops.DIBPadBytes) * jcprops.DIBHeight;
// Compute size of desired pixel buffer.
// Allocate memory to hold the decompressed image data.
pixel_buf = new BYTE [wholeimagesize];
if(NULL == pixel_buf)
{
bres = FALSE;
__leave;
}
// Set up the info on the desired DIB properties.
jcprops.DIBBytes = pixel_buf;
// Set the JPG color space this will always be
// somewhat of an educated guess at best because JPEG
// is "color blind" (i.e., nothing in the bit stream
// tells you what color space the data was encoded from).
// However, in this example we assume that we are
// reading JPEG files which means that 3 channel images
// are in the YCbCr color space and 1 channel images are
// in the Y color space.
switch(jcprops.JPGChannels)
{
case 1:
{
jcprops.JPGColor = IJL_G;
jcprops.DIBChannels = 3;
break;
}
case 3:
{
jcprops.JPGColor = IJL_YCBCR;
jcprops.DIBChannels = 3;
break;
}
default:
{
// This catches everything else, but no
// color twist will be performed by the IJL.
jcprops.DIBColor = (IJL_COLOR)IJL_OTHER;
jcprops.JPGColor = (IJL_COLOR)IJL_OTHER;
jcprops.DIBChannels = jcprops.JPGChannels;
break;
}
}
// Now get the actual JPEG image data into the pixel buffer.
jerr = ijlRead(&jcprops, IJL_JFILE_READWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
} // __try
__finally
{
if(FALSE == bres)
{
if(NULL != pixel_buf)
{
delete [] pixel_buf;
pixel_buf = NULL;
}
}
// Clean up the IntelR JPEG Library.
ijlFree(&jcprops);
*width = jcprops.DIBWidth;
*height = jcprops.DIBHeight;
*nchannels = jcprops.DIBChannels;
*buffer = pixel_buf;
if(NULL != ImageLen)
*ImageLen = wholeimagesize;
} // __finally
return bres;
} // DecodeJPGFileToGeneralBuffer()
lg_Bitmap* PASCAL lg_Bitmap::FromHandle(HBITMAP hBitmap)
{
return (lg_Bitmap*) CGdiObject::FromHandle(hBitmap);
}
//构造
lg_Bitmap::lg_Bitmap()
{
m_hDrawDib = DrawDibOpen();
::ZeroMemory(&m_bmi,sizeof(BITMAPINFO));
}
//析构
lg_Bitmap::~lg_Bitmap()
{
CGdiObject::DeleteObject();
DrawDibClose(m_hDrawDib);
}
//DIB 数据 上下翻转
void lg_Bitmap::OW_Rotate(BYTE *data/*纯DIB数据*/, DWORD scanline/*扫描行*/, DWORD height/*DIB 高度*/)
{
DWORD length = 0;
BYTE* Begin_line_data = new BYTE[scanline];
for(int i=0;(unsigned)i<height/2;i++){
memcpy(Begin_line_data,data + scanline*i,scanline);
memcpy(data + scanline*i,data + (height-i-1)*scanline,scanline);
memcpy(data+(height-i-1)*scanline,Begin_line_data,scanline);
}
delete[] Begin_line_data;
}
//将载入的图像,直接画到指定窗口
BOOL lg_Bitmap::Draw(HWND hWnd)
{
if(::IsWindow(hWnd)){
HDC hDC = ::GetDC(hWnd);
if(!hDC){
::ReleaseDC(hWnd,hDC);
return FALSE;
}
RECT rect;
::GetClientRect(hWnd,&rect);
DWORD dwWinWidth = rect.right - rect.left;
DWORD dwWinHeight = rect.bottom - rect.top;
//解码函数
BITMAPINFOHEADER bih;
memcpy(&bih,&m_bmi.bmiHeader,sizeof(BITMAPINFOHEADER));
bih.biHeight = abs(bih.biHeight);
BITMAP bmp;
GetBitmap(&bmp);
//memcpy(lpBits,bmp.bmBits,dwCount);
DrawDibDraw( m_hDrawDib, hDC,
0,0,dwWinWidth,abs(dwWinHeight),
&bih, (LPVOID)bmp.bmBits,
0,0,bih.biWidth,abs(bih.biHeight),
DDF_BACKGROUNDPAL);
::ReleaseDC(hWnd,hDC);
}
return TRUE;
}
//载入 JPEG 缓冲
BOOL lg_Bitmap::LoadJpegBuffer(const uint8* JpgBuffer,uint32 JpgBufLen)
{
uint16 dwNumberOfChannels;
int32 dwWidth,dwHeight;
uint8* lppRGBBuffer = NULL;
BOOL ret = DecodeFromJPEGBuffer(const_cast<uint8*>(JpgBuffer),JpgBufLen,&lppRGBBuffer,&dwWidth,&dwHeight,&dwNumberOfChannels);
if(!lppRGBBuffer){
return FALSE;
}
ret = LoadFromMemNoHead(lppRGBBuffer,dwWidth,dwHeight,dwNumberOfChannels);
if(lppRGBBuffer){
delete [] lppRGBBuffer;
lppRGBBuffer = NULL;
}
return ret;
}
//将 JPEG 文件载入 Bitmap 对象
BOOL lg_Bitmap::LoadJPEGFile(LPCTSTR lpszFilename)
{
uint16 dwNumberOfChannels;
int32 dwWidth,dwHeight;
uint32 dwWholesize = 0;
BYTE* lppRGBBuffer = NULL;
//解码函数
BOOL ret = DecodeJPGFileToGeneralBuffer(lpszFilename,&dwWidth,&dwHeight,&dwNumberOfChannels,&lppRGBBuffer,&dwWholesize);
if(!lppRGBBuffer){
return FALSE;
}
OW_Rotate(lppRGBBuffer,dwWholesize/dwHeight,dwHeight);
ret = LoadFromMemNoHead(lppRGBBuffer,dwWidth,dwHeight,dwNumberOfChannels);
if(lppRGBBuffer){
delete [] lppRGBBuffer;
lppRGBBuffer = NULL;
}
return ret;
}
//载入纯 DIB 缓冲 ( 没有BITMAPFILEHEADER 和 BITMAPINFOHEADER )
BOOL lg_Bitmap::LoadFromMemNoHead(BYTE* pData,int32 width,int32 height,uint16 nChannels)
{
BITMAPINFOHEADER& bih = m_bmi.bmiHeader;
::ZeroMemory( &bih, sizeof( BITMAPINFOHEADER ));
bih.biSize = sizeof( BITMAPINFOHEADER );
bih.biWidth = width;//(width * nChannels * 8 +31) / 8;
bih.biHeight = -height;
bih.biCompression = BI_RGB;//BI_JPEG;//BI_JPEG
bih.biPlanes = 1;
bih.biBitCount = (nChannels == 3) ? (WORD)24 : (WORD)32;
BOOL bSuccess=CreateBitmapIndirect(&m_bmi, pData);
if(!bSuccess)
return FALSE;
return TRUE;
}
//载入 DIB 缓冲 ( 包含BITMAPFILEHEADER 和 BITMAPINFOHEADER )
BOOL lg_Bitmap::LoadFromMem(BYTE* pData,unsigned long length)
{
LPBITMAPFILEHEADER bfhHeader;
bfhHeader = (LPBITMAPFILEHEADER )pData;
if(bfhHeader->bfType!=((WORD) ('M'<<8)|'B'))
{
return FALSE;
}
if(bfhHeader->bfSize!=length)
{
//MessageBox(NULL,"The BMP file header error!","warning",MB_OK);
return FALSE;
}
UINT uBmpInfoLen=sizeof(BITMAPFILEHEADER);
LPBITMAPINFO lpBitmap = (LPBITMAPINFO)&pData[uBmpInfoLen];
DWORD dwBitlen=bfhHeader->bfOffBits;
LPVOID lpBits = &pData[dwBitlen];
BOOL bSuccess=CreateBitmapIndirect(lpBitmap, lpBits);
if(!bSuccess)
return FALSE;
return TRUE;
}
//载入 DIB 文件
BOOL lg_Bitmap::LoadBitmap(LPCTSTR lpszFileName)
{
CFile file;
if(!file.Open(lpszFileName,CFile::modeRead|CFile::shareDenyWrite))
{
MessageBox(NULL,"BMP file open error!","warning",MB_OK);
return FALSE;
}
BITMAPFILEHEADER bfhHeader;
file.Read(&bfhHeader,sizeof(BITMAPFILEHEADER));
if(bfhHeader.bfType!=((WORD) ('M'<<8)|'B'))
{
MessageBox(NULL,"The file is not a BMP file!","warning",MB_OK);
return FALSE;
}
if(bfhHeader.bfSize!=file.GetLength())
{
MessageBox(NULL,"The BMP file header error!","warning",MB_OK);
return FALSE;
}
UINT uBmpInfoLen=(UINT) bfhHeader.bfOffBits-sizeof(BITMAPFILEHEADER);
LPBITMAPINFO lpBitmap=(LPBITMAPINFO) new BYTE[uBmpInfoLen];
file.Read((LPVOID) lpBitmap,uBmpInfoLen);
if((* (LPDWORD)(lpBitmap))!=sizeof(BITMAPINFOHEADER))
{
MessageBox(NULL,"The BMP is not Windows 3.0 format!","warning",MB_OK);
return FALSE;
}
DWORD dwBitlen=bfhHeader.bfSize - bfhHeader.bfOffBits;
LPVOID lpBits=new BYTE[dwBitlen];
file.ReadHuge(lpBits,dwBitlen);
file.Close();
BOOL bSuccess=CreateBitmapIndirect(lpBitmap, lpBits);
delete lpBitmap;
delete lpBits;
if(!bSuccess)
return FALSE;
return TRUE;
}
//载入纯 DIB 缓冲 ( 没有BITMAPFILEHEADER 和 BITMAPINFOHEADER )
//此函数可以创建特殊颜色的
BOOL lg_Bitmap::CreateBitmap(int nWidth, int nHeight, UINT nBitCount,
const void* lpSrcBits)
{
ASSERT(nBitCount==1||nBitCount==4||nBitCount==8
||nBitCount==16||nBitCount==24||nBitCount==32);
LPBITMAPINFO lpBitmap;
lpBitmap=(BITMAPINFO*) new BYTE[sizeof(BITMAPINFOHEADER) +
GetColorNumber(nBitCount) * sizeof(RGBQUAD)];
lpBitmap->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
lpBitmap->bmiHeader.biWidth=nWidth;
lpBitmap->bmiHeader.biHeight=nHeight;
lpBitmap->bmiHeader.biBitCount=nBitCount;
lpBitmap->bmiHeader.biPlanes=1;
lpBitmap->bmiHeader.biCompression=BI_RGB;
lpBitmap->bmiHeader.biSizeImage=0;
lpBitmap->bmiHeader.biClrUsed=0;
BOOL bSuccess=CreateBitmapIndirect(lpBitmap, lpSrcBits);
delete [] lpBitmap;
if(!bSuccess)
return FALSE;
return TRUE;
}
BOOL lg_Bitmap::CreateBitmapIndirect(LPBITMAPINFO lpBitmapInfo, const void* lpSrcBits)
{
DeleteObject();
LPVOID lpBits;
CDC *dc=new CDC;
dc->CreateCompatibleDC(NULL);
HBITMAP hBitmap=::CreateDIBSection(dc->m_hDC,lpBitmapInfo,DIB_RGB_COLORS,
&lpBits,NULL,0);
if(hBitmap == NULL)
DWORD Err = GetLastError();
ASSERT(hBitmap!=NULL);
delete dc;
Attach(hBitmap);
BITMAP bmp;
GetBitmap(&bmp);
DWORD dwCount=(DWORD) bmp.bmWidthBytes * bmp.bmHeight;
if(SetBitmapBits(dwCount,lpSrcBits)!=dwCount)
{
MessageBox(NULL,"DIB build error!","warning",MB_OK);
return FALSE;
}
return TRUE;
}
lg_Bitmap::operator HBITMAP() const
{
return (HBITMAP)(this == NULL ? NULL : m_hObject);
}
int lg_Bitmap::GetBitmap(BITMAP* pBitMap)
{
ASSERT(m_hObject != NULL);
return ::GetObject(m_hObject, sizeof(BITMAP), pBitMap);
}
int lg_Bitmap::GetColorNumber(WORD wBitCount)
{
ASSERT(wBitCount==1||wBitCount==4||wBitCount==8
||wBitCount==16||wBitCount==24||wBitCount==32);
switch(wBitCount)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
default:
return 0;
}
}
DWORD lg_Bitmap::SetBitmapBits(DWORD dwCount, const void* lpBits)
{
if(lpBits!=NULL)
{
BITMAP bmp;
GetBitmap(&bmp);
memcpy(bmp.bmBits,lpBits,dwCount);
return dwCount;
}
else
return 0;
}
DWORD lg_Bitmap::GetBitmapBits(DWORD dwCount, LPVOID lpBits)
{
if(lpBits!=NULL)
{
BITMAP bmp;
GetBitmap(&bmp);
memcpy(lpBits,bmp.bmBits,dwCount);
return dwCount;
}
else
return 0;
}
}
#include " Lg_Bitmap.h "
#include < memory.h >
#pragma comment(lib, " ijl15.lib " )
#pragma comment(lib, " vfw32.lib " )
namespace lglib
{
IMPLEMENT_DYNAMIC(lg_Bitmap,CGdiObject);
//----------------------------------------------------------
// An example using the Intel(R) JPEG Library:
// -- 编码 DIB 为 JPEG 缓冲.
//----------------------------------------------------------
BOOL lg_Bitmap::EncodeToJPEGBuffer(
BYTE* lpRgbBuffer,
int32 nWidth,
int32 nHeight,
BYTE** lppJpgBuffer,
DWORD* lpdwJpgBufferSize)
{
BOOL bres;
IJLERR jerr;
DWORD dwRgbBufferSize;
BYTE* lpTemp;
// Allocate the IJL JPEG_CORE_PROPERTIES structure.
JPEG_CORE_PROPERTIES jcprops;
bres = TRUE;
__try
{
// Initialize the Intel(R) JPEG Library.
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
jcprops.DIBWidth = nWidth;
jcprops.DIBHeight = -nHeight; // Implies a bottom-up DIB.
jcprops.DIBBytes = lpRgbBuffer + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
jcprops.DIBChannels = 3;
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.DIBWidth, jcprops.DIBChannels);
jcprops.DIBColor = IJL_BGR;//;
dwRgbBufferSize = (jcprops.DIBWidth * jcprops.DIBChannels + jcprops.DIBPadBytes) * abs(jcprops.DIBHeight);
lpTemp = new BYTE [dwRgbBufferSize];
if(NULL == lpTemp)
{
bres = FALSE;
__leave;
}
// Set up information to write from the pixel buffer.
jcprops.JPGWidth = jcprops.DIBWidth;
jcprops.JPGHeight = abs(jcprops.DIBHeight);
jcprops.JPGFile = NULL;
jcprops.JPGBytes = lpTemp;
jcprops.JPGSizeBytes = dwRgbBufferSize;
jcprops.JPGChannels = 3;
jcprops.JPGColor = IJL_YCBCR;
jcprops.JPGSubsampling = IJL_411; // 4:1:1 subsampling.
jcprops.jquality = 75; // Select "good" image quality
// Write the actual JPEG image from the pixel buffer.
jerr = ijlWrite(&jcprops,IJL_JBUFF_WRITEWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
} // __try
__finally
{
if(FALSE == bres)
{
if(NULL != lpTemp)
{
delete[] lpTemp;
lpTemp = NULL;
}
}
*lppJpgBuffer = lpTemp;
*lpdwJpgBufferSize = jcprops.JPGSizeBytes;
// Clean up the Intel(R) JPEG Library.
ijlFree(&jcprops);
}
return bres;
} // EncodeToJPEGBuffer()
//----------------------------------------------------------
// An example using the IntelR JPEG Library:
// -- 将 DIB 保存为 JPG 文件.
//----------------------------------------------------------
BOOL lg_Bitmap::EncodeJPGFileFromDIB(
LPCSTR lpszPathName,
BITMAPINFOHEADER* bmih)
{
BOOL bres;
IJLERR jerr;
// Allocate the IJL JPEG_CORE_PROPERTIES structure.
JPEG_CORE_PROPERTIES jcprops;// = new JPEG_CORE_PROPERTIES;
bres = TRUE;
__try
{
// Initialize the IntelR JPEG Library.
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
if(bmih->biBitCount != 24)
{
// not supported palette images
bres = FALSE;
__leave;
}
// Set up information to write from the pixel buffer.
jcprops.DIBWidth = bmih->biWidth;
jcprops.DIBHeight = -bmih->biHeight; // Implies a bottom-up DIB.
jcprops.DIBBytes = reinterpret_cast<BYTE*>(bmih) + sizeof(BITMAPINFOHEADER);
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(bmih->biWidth,3);
// Note: the following are default values and thus
// do not need to be set.
jcprops.DIBChannels = 3;
jcprops.DIBColor = IJL_BGR;
jcprops.JPGFile = const_cast<LPSTR>(lpszPathName);
// Specify JPEG file creation parameters.
jcprops.JPGWidth = bmih->biWidth;
jcprops.JPGHeight = bmih->biHeight;
// Note: the following are default values and thus
// do not need to be set.
jcprops.JPGChannels = 3;
jcprops.JPGColor = IJL_YCBCR;
jcprops.JPGSubsampling = IJL_411; // 4:1:1 subsampling.
jcprops.jquality = 75; // Select "good" image quality
// Write the actual JPEG image from the pixel buffer.
jerr = ijlWrite(&jcprops,IJL_JFILE_WRITEWHOLEIMAGE);//IJL_JFILE_WRITEWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
} // __try
__finally
{
// Clean up the IntelR JPEG Library.
ijlFree(&jcprops);
}
return bres;
} // EncodeJPGFileFromDIB()
//----------------------------------------------------------
// An example using the Intel(R) JPEG Library:
// -- 将 JPEG 缓冲解码为 DIB缓冲
//----------------------------------------------------------
BOOL lg_Bitmap::DecodeFromJPEGBuffer(
BYTE* lpJpgBuffer,
DWORD dwJpgBufferSize,
BYTE** lppRgbBuffer,
int32* lpdwWidth,
int32* lpdwHeight,
uint16* lpdwNumberOfChannels)
{
BOOL bres;
IJLERR jerr;
DWORD dwWholeImageSize;
BYTE* lpTemp = NULL;
// Allocate the IJL JPEG_CORE_PROPERTIES structure.
JPEG_CORE_PROPERTIES jcprops;
bres = TRUE;
__try
{
// Initialize the Intel(R) JPEG Library.
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
// Get information on the JPEG image
// (i.e., width, height, and channels).
jcprops.JPGFile = NULL;
jcprops.JPGBytes = lpJpgBuffer;
jcprops.JPGSizeBytes = dwJpgBufferSize;
jerr = ijlRead(&jcprops, IJL_JBUFF_READPARAMS);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
// Set the JPG color space this will always be
// somewhat of an educated guess at best because JPEG
// is "color blind" (i.e., nothing in the bit stream
// tells you what color space the data was encoded from).
// However, in this example we assume that we are
// reading JPEG files which means that 3 channel images
// are in the YCbCr color space and 1 channel images are
// in the Y color space.
switch(jcprops.JPGChannels)
{
case 1:
{
jcprops.JPGColor = IJL_G;
jcprops.DIBColor = IJL_RGB;
jcprops.DIBChannels = 3;
break;
}
case 3:
{
jcprops.JPGColor = IJL_YCBCR;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBChannels = 3;
break;
}
default:
{
// This catches everything else, but no
// color twist will be performed by the IJL.
jcprops.JPGColor = IJL_OTHER;
jcprops.DIBColor = IJL_OTHER;
jcprops.DIBChannels = jcprops.JPGChannels;
break;
}
}
// Compute size of desired pixel buffer.
jcprops.DIBWidth = jcprops.JPGWidth;
jcprops.DIBHeight = jcprops.JPGHeight;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.JPGWidth, jcprops.DIBChannels);
// Compute size of desired pixel buffer.
dwWholeImageSize = (jcprops.DIBWidth * jcprops.DIBChannels + jcprops.DIBPadBytes) * jcprops.DIBHeight;
lpTemp = new BYTE [dwWholeImageSize];
memset(lpTemp,0,dwWholeImageSize);
if(NULL == lpTemp)
{
bres = FALSE;
__leave;
}
jcprops.DIBBytes = lpTemp;
// Now get the actual JPEG image data into the pixel buffer.
jerr = ijlRead(&jcprops, IJL_JBUFF_READWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
} // __try
__finally
{
if(FALSE == bres)
{
if(NULL != lpTemp)
{
delete [] lpTemp;
lpTemp = NULL;
}
}
// Clean up the Intel(R) JPEG Library.
ijlFree(&jcprops);
*lpdwWidth = jcprops.DIBWidth;
*lpdwHeight = jcprops.DIBHeight;
*lpdwNumberOfChannels = jcprops.DIBChannels;
*lppRgbBuffer = lpTemp;
} // __finally
return bres;
} // DecodeFromJPEGBuffer()
//----------------------------------------------------------
// An example using the Intel(R) JPEG Library:
// -- 解码 JPG文件 为 DIB 缓冲
//----------------------------------------------------------
BOOL lg_Bitmap::DecodeJPGFileToGeneralBuffer(
LPCTSTR lpszPathName,
int32* width,
int32* height,
uint16* nchannels,
BYTE** buffer,DWORD* ImageLen)
{
BOOL bres;
IJLERR jerr;
DWORD wholeimagesize;
BYTE* pixel_buf = NULL;
// Allocate the IJL JPEG_CORE_PROPERTIES structure.
JPEG_CORE_PROPERTIES jcprops;
bres = TRUE;
__try
{
// Initialize the IntelR JPEG Library.
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
// Get information on the JPEG image
// (i.e., width, height, and channels).
jcprops.JPGFile = const_cast<LPSTR>(lpszPathName);
jerr = ijlRead(&jcprops, IJL_JFILE_READPARAMS);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
// Set up local data.
jcprops.DIBWidth = jcprops.JPGWidth;
jcprops.DIBHeight = jcprops.JPGHeight;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.JPGWidth, jcprops.DIBChannels);
wholeimagesize = (jcprops.DIBWidth * jcprops.DIBChannels + jcprops.DIBPadBytes) * jcprops.DIBHeight;
// Compute size of desired pixel buffer.
// Allocate memory to hold the decompressed image data.
pixel_buf = new BYTE [wholeimagesize];
if(NULL == pixel_buf)
{
bres = FALSE;
__leave;
}
// Set up the info on the desired DIB properties.
jcprops.DIBBytes = pixel_buf;
// Set the JPG color space this will always be
// somewhat of an educated guess at best because JPEG
// is "color blind" (i.e., nothing in the bit stream
// tells you what color space the data was encoded from).
// However, in this example we assume that we are
// reading JPEG files which means that 3 channel images
// are in the YCbCr color space and 1 channel images are
// in the Y color space.
switch(jcprops.JPGChannels)
{
case 1:
{
jcprops.JPGColor = IJL_G;
jcprops.DIBChannels = 3;
break;
}
case 3:
{
jcprops.JPGColor = IJL_YCBCR;
jcprops.DIBChannels = 3;
break;
}
default:
{
// This catches everything else, but no
// color twist will be performed by the IJL.
jcprops.DIBColor = (IJL_COLOR)IJL_OTHER;
jcprops.JPGColor = (IJL_COLOR)IJL_OTHER;
jcprops.DIBChannels = jcprops.JPGChannels;
break;
}
}
// Now get the actual JPEG image data into the pixel buffer.
jerr = ijlRead(&jcprops, IJL_JFILE_READWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
} // __try
__finally
{
if(FALSE == bres)
{
if(NULL != pixel_buf)
{
delete [] pixel_buf;
pixel_buf = NULL;
}
}
// Clean up the IntelR JPEG Library.
ijlFree(&jcprops);
*width = jcprops.DIBWidth;
*height = jcprops.DIBHeight;
*nchannels = jcprops.DIBChannels;
*buffer = pixel_buf;
if(NULL != ImageLen)
*ImageLen = wholeimagesize;
} // __finally
return bres;
} // DecodeJPGFileToGeneralBuffer()
lg_Bitmap* PASCAL lg_Bitmap::FromHandle(HBITMAP hBitmap)
{
return (lg_Bitmap*) CGdiObject::FromHandle(hBitmap);
}
//构造
lg_Bitmap::lg_Bitmap()
{
m_hDrawDib = DrawDibOpen();
::ZeroMemory(&m_bmi,sizeof(BITMAPINFO));
}
//析构
lg_Bitmap::~lg_Bitmap()
{
CGdiObject::DeleteObject();
DrawDibClose(m_hDrawDib);
}
//DIB 数据 上下翻转
void lg_Bitmap::OW_Rotate(BYTE *data/*纯DIB数据*/, DWORD scanline/*扫描行*/, DWORD height/*DIB 高度*/)
{
DWORD length = 0;
BYTE* Begin_line_data = new BYTE[scanline];
for(int i=0;(unsigned)i<height/2;i++){
memcpy(Begin_line_data,data + scanline*i,scanline);
memcpy(data + scanline*i,data + (height-i-1)*scanline,scanline);
memcpy(data+(height-i-1)*scanline,Begin_line_data,scanline);
}
delete[] Begin_line_data;
}
//将载入的图像,直接画到指定窗口
BOOL lg_Bitmap::Draw(HWND hWnd)
{
if(::IsWindow(hWnd)){
HDC hDC = ::GetDC(hWnd);
if(!hDC){
::ReleaseDC(hWnd,hDC);
return FALSE;
}
RECT rect;
::GetClientRect(hWnd,&rect);
DWORD dwWinWidth = rect.right - rect.left;
DWORD dwWinHeight = rect.bottom - rect.top;
//解码函数
BITMAPINFOHEADER bih;
memcpy(&bih,&m_bmi.bmiHeader,sizeof(BITMAPINFOHEADER));
bih.biHeight = abs(bih.biHeight);
BITMAP bmp;
GetBitmap(&bmp);
//memcpy(lpBits,bmp.bmBits,dwCount);
DrawDibDraw( m_hDrawDib, hDC,
0,0,dwWinWidth,abs(dwWinHeight),
&bih, (LPVOID)bmp.bmBits,
0,0,bih.biWidth,abs(bih.biHeight),
DDF_BACKGROUNDPAL);
::ReleaseDC(hWnd,hDC);
}
return TRUE;
}
//载入 JPEG 缓冲
BOOL lg_Bitmap::LoadJpegBuffer(const uint8* JpgBuffer,uint32 JpgBufLen)
{
uint16 dwNumberOfChannels;
int32 dwWidth,dwHeight;
uint8* lppRGBBuffer = NULL;
BOOL ret = DecodeFromJPEGBuffer(const_cast<uint8*>(JpgBuffer),JpgBufLen,&lppRGBBuffer,&dwWidth,&dwHeight,&dwNumberOfChannels);
if(!lppRGBBuffer){
return FALSE;
}
ret = LoadFromMemNoHead(lppRGBBuffer,dwWidth,dwHeight,dwNumberOfChannels);
if(lppRGBBuffer){
delete [] lppRGBBuffer;
lppRGBBuffer = NULL;
}
return ret;
}
//将 JPEG 文件载入 Bitmap 对象
BOOL lg_Bitmap::LoadJPEGFile(LPCTSTR lpszFilename)
{
uint16 dwNumberOfChannels;
int32 dwWidth,dwHeight;
uint32 dwWholesize = 0;
BYTE* lppRGBBuffer = NULL;
//解码函数
BOOL ret = DecodeJPGFileToGeneralBuffer(lpszFilename,&dwWidth,&dwHeight,&dwNumberOfChannels,&lppRGBBuffer,&dwWholesize);
if(!lppRGBBuffer){
return FALSE;
}
OW_Rotate(lppRGBBuffer,dwWholesize/dwHeight,dwHeight);
ret = LoadFromMemNoHead(lppRGBBuffer,dwWidth,dwHeight,dwNumberOfChannels);
if(lppRGBBuffer){
delete [] lppRGBBuffer;
lppRGBBuffer = NULL;
}
return ret;
}
//载入纯 DIB 缓冲 ( 没有BITMAPFILEHEADER 和 BITMAPINFOHEADER )
BOOL lg_Bitmap::LoadFromMemNoHead(BYTE* pData,int32 width,int32 height,uint16 nChannels)
{
BITMAPINFOHEADER& bih = m_bmi.bmiHeader;
::ZeroMemory( &bih, sizeof( BITMAPINFOHEADER ));
bih.biSize = sizeof( BITMAPINFOHEADER );
bih.biWidth = width;//(width * nChannels * 8 +31) / 8;
bih.biHeight = -height;
bih.biCompression = BI_RGB;//BI_JPEG;//BI_JPEG
bih.biPlanes = 1;
bih.biBitCount = (nChannels == 3) ? (WORD)24 : (WORD)32;
BOOL bSuccess=CreateBitmapIndirect(&m_bmi, pData);
if(!bSuccess)
return FALSE;
return TRUE;
}
//载入 DIB 缓冲 ( 包含BITMAPFILEHEADER 和 BITMAPINFOHEADER )
BOOL lg_Bitmap::LoadFromMem(BYTE* pData,unsigned long length)
{
LPBITMAPFILEHEADER bfhHeader;
bfhHeader = (LPBITMAPFILEHEADER )pData;
if(bfhHeader->bfType!=((WORD) ('M'<<8)|'B'))
{
return FALSE;
}
if(bfhHeader->bfSize!=length)
{
//MessageBox(NULL,"The BMP file header error!","warning",MB_OK);
return FALSE;
}
UINT uBmpInfoLen=sizeof(BITMAPFILEHEADER);
LPBITMAPINFO lpBitmap = (LPBITMAPINFO)&pData[uBmpInfoLen];
DWORD dwBitlen=bfhHeader->bfOffBits;
LPVOID lpBits = &pData[dwBitlen];
BOOL bSuccess=CreateBitmapIndirect(lpBitmap, lpBits);
if(!bSuccess)
return FALSE;
return TRUE;
}
//载入 DIB 文件
BOOL lg_Bitmap::LoadBitmap(LPCTSTR lpszFileName)
{
CFile file;
if(!file.Open(lpszFileName,CFile::modeRead|CFile::shareDenyWrite))
{
MessageBox(NULL,"BMP file open error!","warning",MB_OK);
return FALSE;
}
BITMAPFILEHEADER bfhHeader;
file.Read(&bfhHeader,sizeof(BITMAPFILEHEADER));
if(bfhHeader.bfType!=((WORD) ('M'<<8)|'B'))
{
MessageBox(NULL,"The file is not a BMP file!","warning",MB_OK);
return FALSE;
}
if(bfhHeader.bfSize!=file.GetLength())
{
MessageBox(NULL,"The BMP file header error!","warning",MB_OK);
return FALSE;
}
UINT uBmpInfoLen=(UINT) bfhHeader.bfOffBits-sizeof(BITMAPFILEHEADER);
LPBITMAPINFO lpBitmap=(LPBITMAPINFO) new BYTE[uBmpInfoLen];
file.Read((LPVOID) lpBitmap,uBmpInfoLen);
if((* (LPDWORD)(lpBitmap))!=sizeof(BITMAPINFOHEADER))
{
MessageBox(NULL,"The BMP is not Windows 3.0 format!","warning",MB_OK);
return FALSE;
}
DWORD dwBitlen=bfhHeader.bfSize - bfhHeader.bfOffBits;
LPVOID lpBits=new BYTE[dwBitlen];
file.ReadHuge(lpBits,dwBitlen);
file.Close();
BOOL bSuccess=CreateBitmapIndirect(lpBitmap, lpBits);
delete lpBitmap;
delete lpBits;
if(!bSuccess)
return FALSE;
return TRUE;
}
//载入纯 DIB 缓冲 ( 没有BITMAPFILEHEADER 和 BITMAPINFOHEADER )
//此函数可以创建特殊颜色的
BOOL lg_Bitmap::CreateBitmap(int nWidth, int nHeight, UINT nBitCount,
const void* lpSrcBits)
{
ASSERT(nBitCount==1||nBitCount==4||nBitCount==8
||nBitCount==16||nBitCount==24||nBitCount==32);
LPBITMAPINFO lpBitmap;
lpBitmap=(BITMAPINFO*) new BYTE[sizeof(BITMAPINFOHEADER) +
GetColorNumber(nBitCount) * sizeof(RGBQUAD)];
lpBitmap->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
lpBitmap->bmiHeader.biWidth=nWidth;
lpBitmap->bmiHeader.biHeight=nHeight;
lpBitmap->bmiHeader.biBitCount=nBitCount;
lpBitmap->bmiHeader.biPlanes=1;
lpBitmap->bmiHeader.biCompression=BI_RGB;
lpBitmap->bmiHeader.biSizeImage=0;
lpBitmap->bmiHeader.biClrUsed=0;
BOOL bSuccess=CreateBitmapIndirect(lpBitmap, lpSrcBits);
delete [] lpBitmap;
if(!bSuccess)
return FALSE;
return TRUE;
}
BOOL lg_Bitmap::CreateBitmapIndirect(LPBITMAPINFO lpBitmapInfo, const void* lpSrcBits)
{
DeleteObject();
LPVOID lpBits;
CDC *dc=new CDC;
dc->CreateCompatibleDC(NULL);
HBITMAP hBitmap=::CreateDIBSection(dc->m_hDC,lpBitmapInfo,DIB_RGB_COLORS,
&lpBits,NULL,0);
if(hBitmap == NULL)
DWORD Err = GetLastError();
ASSERT(hBitmap!=NULL);
delete dc;
Attach(hBitmap);
BITMAP bmp;
GetBitmap(&bmp);
DWORD dwCount=(DWORD) bmp.bmWidthBytes * bmp.bmHeight;
if(SetBitmapBits(dwCount,lpSrcBits)!=dwCount)
{
MessageBox(NULL,"DIB build error!","warning",MB_OK);
return FALSE;
}
return TRUE;
}
lg_Bitmap::operator HBITMAP() const
{
return (HBITMAP)(this == NULL ? NULL : m_hObject);
}
int lg_Bitmap::GetBitmap(BITMAP* pBitMap)
{
ASSERT(m_hObject != NULL);
return ::GetObject(m_hObject, sizeof(BITMAP), pBitMap);
}
int lg_Bitmap::GetColorNumber(WORD wBitCount)
{
ASSERT(wBitCount==1||wBitCount==4||wBitCount==8
||wBitCount==16||wBitCount==24||wBitCount==32);
switch(wBitCount)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
default:
return 0;
}
}
DWORD lg_Bitmap::SetBitmapBits(DWORD dwCount, const void* lpBits)
{
if(lpBits!=NULL)
{
BITMAP bmp;
GetBitmap(&bmp);
memcpy(bmp.bmBits,lpBits,dwCount);
return dwCount;
}
else
return 0;
}
DWORD lg_Bitmap::GetBitmapBits(DWORD dwCount, LPVOID lpBits)
{
if(lpBits!=NULL)
{
BITMAP bmp;
GetBitmap(&bmp);
memcpy(lpBits,bmp.bmBits,dwCount);
return dwCount;
}
else
return 0;
}
}