📄 voimage.cpp
字号:
#include "stdafx.h"
#include "VOImage.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#ifdef _WIN32_WCE
#pragma comment(lib, "imgdecmp.lib")
#endif
//#ifdef DEBUG_TIME_ANALYZE
//#include "../global/timeanalyze.h"
//extern CTimeAnalyze* g_pTimeAnalyze;
//#define TIME_ANALYZE_BEGINMAIN(id) g_pTimeAnalyze->BeginMain(id)
//#define TIME_ANALYZE_ENDMAIN(id) g_pTimeAnalyze->EndMain(id)
//#define TIME_ANALYZE_BEGIN(id) g_pTimeAnalyze->Begin(id)
//#define TIME_ANALYZE_END(id) g_pTimeAnalyze->End(id)
//#else
//#define TIME_ANALYZE_BEGINMAIN(id)
//#define TIME_ANALYZE_ENDMAIN(id)
//#define TIME_ANALYZE_BEGIN(id)
//#define TIME_ANALYZE_END(id)
//#endif
int CVOImage::g_iScale = 100;
int CVOImage::g_iMaxWidth = 10000;
int CVOImage::g_iMaxHeight = 10000;
BOOL CVOImage::g_bStretchBlt = FALSE;
#if !defined(SCREEN_176)
extern int32 g_ScaleWidth;
extern int32 g_ScaleHeight;
#endif
CVOImage::CVOImage() : m_hdc(0), m_hbitmap(0), m_dwWidth(0), m_dwHeight(0), m_hmoduleResource(0), m_dwResourceID(0)
{
}
CVOImage::~CVOImage()
{
if(m_hdc)
DeleteDC(m_hdc);
if(m_hbitmap)
DeleteObject(m_hbitmap);
}
DWORD CALLBACK CVOImage::GetImageDataP(LPSTR szBuffer, DWORD dwBufferMax, LPARAM lParam )
{
FileP * fileP = (FileP *)lParam;
DWORD dwNumberOfBytesRead;
if(dwBufferMax==0) return 0;
SetFilePointer(fileP->hFile, fileP->fileNowPoint, NULL, FILE_BEGIN);
if ( (HANDLE)lParam == INVALID_HANDLE_VALUE )
return 0;
if(dwBufferMax-(fileP->fileNowPoint-fileP->fileStartPoint)>=1024)
{
ReadFile(fileP->hFile, szBuffer, 1024, &dwNumberOfBytesRead, NULL );
fileP->fileNowPoint += 1024;
}
else if(dwBufferMax-(fileP->fileNowPoint-fileP->fileStartPoint)<=0)
{
return 0;
}
else
{
ReadFile(fileP->hFile, szBuffer, dwBufferMax-(fileP->fileNowPoint-fileP->fileStartPoint), &dwNumberOfBytesRead, NULL );
fileP->fileNowPoint += dwBufferMax-(fileP->fileNowPoint-fileP->fileStartPoint);
}
return dwNumberOfBytesRead;
}
DWORD CALLBACK CVOImage::GetImageDataB(LPSTR szBuffer, DWORD dwBufferMax, LPARAM lParam )
{
FileP * fileP = (FileP *)lParam;
DWORD dwNumberOfBytesRead = 1024;
if(dwBufferMax==0) return 0;
char* tmp;
if(dwBufferMax-(fileP->fileNowPoint-fileP->fileStartPoint)>=1024)
{
tmp = (char*)(fileP->hFile);
tmp = tmp+fileP->fileNowPoint;
memcpy(szBuffer,tmp,1024);
fileP->fileNowPoint += 1024;
}
else if(dwBufferMax-(fileP->fileNowPoint-fileP->fileStartPoint)<=0)
{
return 0;
}
else
{
tmp = (char*)(fileP->hFile);
tmp = tmp+fileP->fileNowPoint;
memcpy(szBuffer,tmp,dwBufferMax-(fileP->fileNowPoint-fileP->fileStartPoint));
dwNumberOfBytesRead= dwBufferMax-(fileP->fileNowPoint-fileP->fileStartPoint);
fileP->fileNowPoint += dwBufferMax-(fileP->fileNowPoint-fileP->fileStartPoint);
}
return dwNumberOfBytesRead;
}
BOOL CVOImage::Load(HDC hdc, char* cSrc, DWORD start, DWORD len)
{
if(cSrc==NULL)
return 0;
//TIME_ANALYZE_BEGINMAIN( 153 );
FileP* fileP = (FileP *)malloc(sizeof(FileP));
fileP->fileStartPoint = start;
fileP->hFile = cSrc;
fileP->fileNowPoint = start;
if(m_hbitmap)
{
DeleteObject(m_hbitmap);
}
if(!m_hdc)
{
m_hdc = CreateCompatibleDC(hdc);
}
#if !defined(SCREEN_176)
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, GetDeviceCaps(hdc, HORZRES)*g_ScaleWidth, GetDeviceCaps(hdc, VERTRES)*g_ScaleHeight );
#else
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES) );
#endif
SelectObject(m_hdc, hbitmap);
BYTE szBuffer[1024] = {0};
#ifdef _WIN32_WCE
HRESULT hr;
DecompressImageInfo dii;
// Fill in the 'DecompressImageInfo' structure
dii.dwSize = sizeof( DecompressImageInfo ); // Size of this structure
dii.pbBuffer = szBuffer; // Pointer to the buffer to use for data
dii.dwBufferMax = len; // Size of the buffer
dii.dwBufferCurrent = 0; // The amount of data which is current in the buffer
dii.phBM = &m_hbitmap; // Pointer to the bitmap returned (can be NULL)
dii.ppImageRender = NULL; // Pointer to an IImageRender object (can be NULL)
dii.iBitDepth = GetDeviceCaps(hdc,BITSPIXEL); // Bit depth of the output image
dii.lParam = ( LPARAM ) fileP; // User parameter for callback functions
dii.hdc = m_hdc; // HDC to use for retrieving palettes
dii.iScale = g_iScale; // Scale factor (1 - 100)
dii.iMaxWidth = g_iMaxWidth; // Maximum width of the output image
dii.iMaxHeight = g_iMaxHeight; // Maxumum height of the output image
dii.pfnGetData = GetImageDataB; // Callback function to get image data
dii.pfnImageProgress = ImageProgress; // Callback function to notify caller of progress decoding the image
dii.crTransparentOverride = ( UINT ) -1; // If this color is not (UINT)-1, it will override the
// transparent color in the image with this color. (GIF ONLY)
// Process and decompress the image data
//TIME_ANALYZE_BEGINMAIN( 151 );
hr = DecompressImageIndirect( &dii );
//TIME_ANALYZE_ENDMAIN( 151 );
#endif
// Clean up
HBITMAP hbitmapOld = (HBITMAP)SelectObject(m_hdc, m_hbitmap);
DeleteObject(hbitmapOld);
BITMAP bmp;
GetObject(m_hbitmap, sizeof(BITMAP), &bmp);
m_dwWidth = bmp.bmWidth;
m_dwHeight = bmp.bmHeight;
//m_strFileName = pcszFileName;//注意不能使用GetFileName
m_dwResourceID = 0;
m_hmoduleResource = 0;
free(fileP);
//TIME_ANALYZE_ENDMAIN( 153 );
return TRUE;
}
//BOOL CVOImage::Load(HDC hdc, HANDLE hFile, DWORD start, DWORD len)
//{
// if(hFile==INVALID_HANDLE_VALUE)
// return 0;
//
// //TIME_ANALYZE_BEGINMAIN( 152 );
// FileP* fileP = (FileP *)malloc(sizeof(FileP));
//
// fileP->fileStartPoint = start;
// fileP->hFile = hFile;
// fileP->fileNowPoint = start;
//
// if(m_hbitmap)
// {
// DeleteObject(m_hbitmap);
// }
//
// if(!m_hdc)
// {
// m_hdc = CreateCompatibleDC(hdc);
// }
//
//#if !defined(SCREEN_176)
// HBITMAP hbitmap = CreateCompatibleBitmap(hdc, GetDeviceCaps(hdc, HORZRES)*g_ScaleWidth, GetDeviceCaps(hdc, VERTRES)*g_ScaleHeight);
//#else
// HBITMAP hbitmap = CreateCompatibleBitmap(hdc, GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES));
//#endif
//
// SelectObject(m_hdc, hbitmap);
//
//
// BYTE szBuffer[1024] = {0};
//
//#ifdef _WIN32_WCE
// HRESULT hr;
//
// DecompressImageInfo dii;
//#endif
//
//
//#ifdef _WIN32_WCE
// // Fill in the 'DecompressImageInfo' structure
// dii.dwSize = sizeof( DecompressImageInfo ); // Size of this structure
// dii.pbBuffer = szBuffer; // Pointer to the buffer to use for data
// dii.dwBufferMax = len; // Size of the buffer
// dii.dwBufferCurrent = 0; // The amount of data which is current in the buffer
// dii.phBM = &m_hbitmap; // Pointer to the bitmap returned (can be NULL)
// dii.ppImageRender = NULL; // Pointer to an IImageRender object (can be NULL)
// dii.iBitDepth = GetDeviceCaps(hdc,BITSPIXEL); // Bit depth of the output image
// dii.lParam = ( LPARAM ) fileP; // User parameter for callback functions
// dii.hdc = m_hdc; // HDC to use for retrieving palettes
// dii.iScale = g_iScale; // Scale factor (1 - 100)
// dii.iMaxWidth = g_iMaxWidth; // Maximum width of the output image
// dii.iMaxHeight = g_iMaxHeight; // Maxumum height of the output image
// dii.pfnGetData = GetImageDataP; // Callback function to get image data
// dii.pfnImageProgress = ImageProgress; // Callback function to notify caller of progress decoding the image
// dii.crTransparentOverride = ( UINT ) -1; // If this color is not (UINT)-1, it will override the
// // transparent color in the image with this color. (GIF ONLY)
//
// // Process and decompress the image data
// //TIME_ANALYZE_BEGINMAIN( 130 );
// hr = DecompressImageIndirect( &dii );
// //TIME_ANALYZE_ENDMAIN( 130 );
//#endif
//
// // Clean up
//
// HBITMAP hbitmapOld = (HBITMAP)SelectObject(m_hdc, m_hbitmap);
//
// DeleteObject(hbitmapOld);
//
// BITMAP bmp;
//
// GetObject(m_hbitmap, sizeof(BITMAP), &bmp);
//
// m_dwWidth = bmp.bmWidth;
// m_dwHeight = bmp.bmHeight;
//
// //m_strFileName = pcszFileName;//注意不能使用GetFileName
// m_dwResourceID = 0;
// m_hmoduleResource = 0;
//
// free(fileP);
//
// //TIME_ANALYZE_ENDMAIN( 152 );
// return TRUE;
//}
//
//
BOOL CVOImage::Load(HDC hdc, LPCTSTR pcszFileName)
{
if(m_hbitmap)
{
if(GetFileName().GetLength() && GetFileName() == pcszFileName)
return TRUE; // Already Loaded
DeleteObject(m_hbitmap);
}
//TIME_ANALYZE_BEGINMAIN( 157 );
if(!m_hdc)
{
m_hdc = CreateCompatibleDC(hdc);
}
#if !defined(SCREEN_176)
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, GetDeviceCaps(hdc, HORZRES)*g_ScaleWidth, GetDeviceCaps(hdc, VERTRES)*g_ScaleHeight);
#else
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES));
#endif
SelectObject(m_hdc, hbitmap);
BYTE szBuffer[1024] = {0};
HANDLE hFile = INVALID_HANDLE_VALUE;
#ifdef _WIN32_WCE
HRESULT hr;
DecompressImageInfo dii;
#endif
hFile = CreateFile(pcszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
#ifdef _WIN32_WCE
// Fill in the 'DecompressImageInfo' structure
dii.dwSize = sizeof( DecompressImageInfo ); // Size of this structure
dii.pbBuffer = szBuffer; // Pointer to the buffer to use for data
dii.dwBufferMax = 1024; // Size of the buffer
dii.dwBufferCurrent = 0; // The amount of data which is current in the buffer
dii.phBM = &m_hbitmap; // Pointer to the bitmap returned (can be NULL)
dii.ppImageRender = NULL; // Pointer to an IImageRender object (can be NULL)
dii.iBitDepth = GetDeviceCaps(hdc,BITSPIXEL); // Bit depth of the output image
dii.lParam = ( LPARAM ) hFile; // User parameter for callback functions
dii.hdc = m_hdc; // HDC to use for retrieving palettes
dii.iScale = g_iScale; // Scale factor (1 - 100)
dii.iMaxWidth = g_iMaxWidth; // Maximum width of the output image
dii.iMaxHeight = g_iMaxHeight; // Maxumum height of the output image
dii.pfnGetData = GetImageData; // Callback function to get image data
dii.pfnImageProgress = ImageProgress; // Callback function to notify caller of progress decoding the image
dii.crTransparentOverride = ( UINT ) -1; // If this color is not (UINT)-1, it will override the
// transparent color in the image with this color. (GIF ONLY)
// Process and decompress the image data
//TIME_ANALYZE_BEGINMAIN( 155 );
hr = DecompressImageIndirect( &dii );
//TIME_ANALYZE_ENDMAIN( 155 );
#endif
// Clean up
CloseHandle( hFile );
HBITMAP hbitmapOld = (HBITMAP)SelectObject(m_hdc, m_hbitmap);
DeleteObject(hbitmapOld);
BITMAP bmp;
GetObject(m_hbitmap, sizeof(BITMAP), &bmp);
m_dwWidth = bmp.bmWidth;
m_dwHeight = bmp.bmHeight;
m_strFileName = pcszFileName;
m_dwResourceID = 0;
m_hmoduleResource = 0;
//TIME_ANALYZE_ENDMAIN( 157 );
return TRUE;
}
//HBITMAP CVOImage::Copy(int cx, int cy, int nCanvasWidth, int nCanvasHeight)
//{
// BITMAP bm, bmNew;
// HBITMAP hNew;
// BOOL fStretch = (cx == -1) || (cy == -1);
//
// ::GetObject(m_hbitmap, sizeof(BITMAP), &bm);
//
// HDC hdc = CreateCompatibleDC(m_hdc);
//
// if(cx == -1)
// {
// if(cy == -1)
// { // Both are default.
// cx = bm.bmWidth;
// cy = bm.bmHeight;
// fStretch = FALSE;
// }
// else
// { // Get cx from cy value
// float fltAspect = (float)bm.bmWidth / (float)bm.bmHeight;
//
// cx = (int) ((float)cy * fltAspect);
// }
// }
// else if(cy == -1)
// { // Get cy from cx value
// float fltAspect = (float)bm.bmHeight / (float)bm.bmWidth;
//
// cy = (int) ((float)cx * fltAspect);
// }
//
// if((nCanvasWidth == -1) || (nCanvasHeight == -1))
// {
// if(cx < cy)
// {
// nCanvasWidth = cy;
// nCanvasHeight = cy;
// }
// else
// {
// nCanvasWidth = cx;
// nCanvasHeight = cx;
// }
// }
//
//// hNew = CreateCompatibleBitmap(m_hdc, cx, cy);
//#if !defined(SCREEN_176)
// hNew = CreateCompatibleBitmap(m_hdc, nCanvasWidth*g_ScaleWidth, nCanvasHeight*g_ScaleHeight);
//#else
// hNew = CreateCompatibleBitmap(m_hdc, nCanvasWidth, nCanvasHeight);
//#endif
// SelectObject(hdc, hNew);
//
// RECT rect;
//
// rect.left = 0;
// rect.top = 0;
// rect.right = nCanvasWidth;
// rect.bottom = nCanvasHeight;
//
//// HBRUSH hbrushBG = CreateSolidBrush(GetPixel(0, 0));
//
// FillRect(hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
//// FillRect(hdc, &rect, hbrushBG);
//
//// DeleteObject(hbrushBG);
//
// if(DrawTransparent(hdc, 0, 0, cx, cy, GetPixel(0, 0)) )
// {
// HBITMAP hPrev = (HBITMAP) ::GetObject(hNew, sizeof(BITMAP), &bmNew);
//
// ::SelectObject(hdc, hPrev);
// }
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -