上次的位图类,比较基本这回又增加了一些功能,从一幅图(BMP或者JPEG)中取得指定矩形区域,拼合(左右,上下),等功能,希望能帮到你,依旧希望大家的支持,能指出其中的BUG,谢谢。
//////////////////////////////////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);
//将载入的图像,直接画到指定DC(固定大小)
void Draw(HDC hDC,int Width,int Height);
// Constructors
lg_Bitmap();

// Implementation
//检查位图头是否合法
LPBITMAPFILEHEADER CheckBmpFileHead(BYTE* pBuf,uint32 dwLen);
//载入位图的一部分内容
BOOL LoadBitmapWithHeadPart(BYTE* lpRgbBuffer,uint32 dwBufLen,RECT rectPart,int32 nExten = 0);
//根据输入图像调整矩形位置
BOOL AdjustRect(RECT Rect,RECT &AdRect,int32 nWidth,int32 nHeight,int32 nExten = 0);
//载入位图的一部分内容(纯数据)
BOOL LoadBitmapNoHeadPart(BYTE* lpRgbBuffer,int32 nWidth,int32 nHeight,uint16 nChannels,uint32 dwBufLen,RECT rectPart,int32 nExten = 0);
//载入JPEG的一部分内容
BOOL LoadJPEGPart(BYTE* lpJpgBuffer,uint32 dwBufLen,RECT rectPart,int32 nExten = 0);
//载入 JPEG 缓冲
BOOL LoadJpegBuffer(const uint8* JpgBuffer,uint32 JpgBufLen);
//将 JPEG 文件载入 Bitmap 对象
BOOL LoadJPEGFile(LPCTSTR lpszFilename);
//载入纯 DIB 缓冲 ( 没有BITMAPFILEHEADER 和 BITMAPINFOHEADER )
BOOL LoadFromMemNoHead(BYTE* pData,int32 width,int32 height,uint16 nChannels);
//载入 DIB 缓冲 ( 包含BITMAPFILEHEADER 和 BITMAPINFOHEADER )
BOOL LoadFromMem(BYTE* pData,uint32 length);
//在根据id在资源中载入DIB
BOOL LoadBitmap(UINT nIDResource);
//载入 DIB 文件
BOOL LoadBitmap(LPCTSTR lpszFileName);
//合并两个位图,左右排列
BOOL ConnectTwoBmp(BYTE* pData1,DWORD Len1,BYTE* pData2,DWORD Len2,DWORD & WholeSize);
//合并两个位图,上下排列 下位置 上位置
BOOL MergeTwoBmp(BYTE* pData2,DWORD Len2,BYTE* pData1,DWORD Len1,DWORD & WholeSize);
//合并两个位图,上下排列 下位置 上位置
BOOL MergeTwoBmpNoHead(BYTE* pData1,DWORD Len1,BYTE* pData2,DWORD Len2,int Width,int Height);
//创建位图
BOOL CreateBitmap(int nWidth, int nHeight, UINT nBitCount, const void* lpBits);
//
//直接创建位图
BOOL CreateBitmapIndirect(LPBITMAPINFO lpBitmapInfo, const void* lpBits);

//编码DIB缓冲为JPEG缓冲
BOOL EncodeToJPEGBuffer(
BYTE* lpRgbBuffer,
int32 nWidth,
int32 nHeight,
BYTE** lppJpgBuffer,
DWORD* lpdwJpgBufferSize);
//解码JPEG缓冲
BOOL DecodeFromJPEGBuffer(
BYTE* lpJpgBuffer,
DWORD dwJpgBufferSize,
BYTE** lppRgbBuffer,
int32* lpdwWidth,
int32* lpdwHeight,
uint16* lpdwNumberOfChannels,
DWORD* ImageLen);
//编码DIB为JPEG文件
BOOL EncodeJPGFileFromDIB(
LPCSTR lpszPathName,
BITMAPINFOHEADER* bmih);
//解码JPEG文件
BOOL DecodeJPGFileToGeneralBuffer(
LPCTSTR lpszPathName,
int32* width,
int32* height,
uint16* nchannels,
BYTE** buffer,DWORD* ImageLen = NULL);
// Attributes
operator HBITMAP() const;
int GetBitmap(BITMAP* pBitMap);
// Operations
static lg_Bitmap* PASCAL FromHandle(HBITMAP hBitmap);
DWORD SetBitmapBits(DWORD dwCount, const void* lpBits);
DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits);

protected:
// Attributes
int GetColorNumber(WORD wBitCount);
private:


//DIB 数据 上下翻转

void OW_Rotate(BYTE *data/**//*纯DIB数据*/, DWORD scanline/**//*扫描行*/, DWORD height/**//*DIB 高度*/);
void Bitfield2RGB(BYTE *src, WORD redmask, WORD greenmask, WORD bluemask, BYTE bpp,LONG Width,LONG Height,DWORD EffWidth);
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,
DWORD* ImageLen)

{
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
&n