这基本上是按照我的使用情况来做的一个位图类,里面包括一些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


////////////////////////////////////////////////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;
