TImageInfo* CRenderEngine::LoadImage(STRINGorID bitmap, LPCTSTR type, DWORD mask)
{
DuiLib::CDuiString str (bitmap.m_lpstr);
if (0 == _tcscmp(_T(".ico"), (4)))
{
TCHAR szDrive[_MAX_DRIVE] = {0};
TCHAR szDir[_MAX_DIR] = {0};
TCHAR szFname[_MAX_FNAME] = {0};
TCHAR szExt[_MAX_EXT] = {0};
_tsplitpath_s(bitmap.m_lpstr, szDrive, szDir, szFname, szExt);
if (_tcscmp(szExt, _T(".ico")) == 0)
{
CDuiString sFile;
if (_tcscmp(szDrive, _T("")) == 0)
sFile = CPaintManagerUI::GetResourcePath();
sFile += bitmap.m_lpstr;
HICON hIcon = (HICON)::LoadImage(NULL, (), IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
if (NULL != hIcon)
{
TImageInfo* data = new TImageInfo;
data->hBitmap = ConvertIconToBitmap(hIcon, &(data->nX), &(data->nY));
data->alphaChannel = true;
data->dwMask = mask;
return data;
}
}
}
LPBYTE pData = NULL;
DWORD dwSize = 0;
do
{
if( type == NULL ) {
CDuiString sFile = CPaintManagerUI::GetResourcePath();
if( CPaintManagerUI::GetResourceZip().IsEmpty() ) {
sFile += bitmap.m_lpstr;
HANDLE hFile = ::CreateFile((), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
FILE_ATTRIBUTE_NORMAL, NULL);
if( hFile == INVALID_HANDLE_VALUE ) break;
dwSize = ::GetFileSize(hFile, NULL);
if( dwSize == 0 ) break;
DWORD dwRead = 0;
pData = new BYTE[ dwSize ];
::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
::CloseHandle( hFile );
if( dwRead != dwSize ) {
delete[] pData;
pData = NULL;
break;
}
}
else {
sFile += CPaintManagerUI::GetResourceZip();
HZIP hz = NULL;
if( CPaintManagerUI::IsCachedResourceZip() ) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
else hz = OpenZip((void*)(), 0, 2);
if( hz == NULL ) break;
ZIPENTRY ze;
int i;
if( FindZipItem(hz, bitmap.m_lpstr, true, &i, &ze) != 0 ) break;
dwSize = ze.unc_size;
if( dwSize == 0 ) break;
pData = new BYTE[ dwSize ];
int res = UnzipItem(hz, i, pData, dwSize, 3);
if( res != 0x00000000 && res != 0x00000600) {
delete[] pData;
pData = NULL;
if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
break;
}
if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
}
}
else {
HRSRC hResource = ::FindResource(CPaintManagerUI::GetResourceDll(), bitmap.m_lpstr, type);
if( hResource == NULL ) break;
HGLOBAL hGlobal = ::LoadResource(CPaintManagerUI::GetResourceDll(), hResource);
if( hGlobal == NULL ) {
FreeResource(hResource);
break;
}
dwSize = ::SizeofResource(CPaintManagerUI::GetResourceDll(), hResource);
if( dwSize == 0 ) break;
pData = new BYTE[ dwSize ];
::CopyMemory(pData, (LPBYTE)::LockResource(hGlobal), dwSize);
::FreeResource(hResource);
}
} while (0);
while (!pData)
{
HANDLE hFile = ::CreateFile(bitmap.m_lpstr, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
FILE_ATTRIBUTE_NORMAL, NULL);
if( hFile == INVALID_HANDLE_VALUE ) break;
dwSize = ::GetFileSize(hFile, NULL);
if( dwSize == 0 ) break;
DWORD dwRead = 0;
pData = new BYTE[ dwSize ];
::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
::CloseHandle( hFile );
if( dwRead != dwSize ) {
delete[] pData;
pData = NULL;
}
break;
}
if (!pData)
{
return NULL;
}
LPBYTE pImage = NULL;
int x,y,n;
pImage = stbi_load_from_memory(pData, dwSize, &x, &y, &n, 4);
delete[] pData;
if( !pImage ) {
return NULL;
}
BITMAPINFO bmi;
::ZeroMemory(&bmi, sizeof(BITMAPINFO));
= sizeof(BITMAPINFOHEADER);
= x;
= -y;
= 1;
= 32;
= BI_RGB;
= x * y * 4;
bool bAlphaChannel = false;
LPBYTE pDest = NULL;
HBITMAP hBitmap = ::CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void**)&pDest, NULL, 0);
if( !hBitmap ) {
return NULL;
}
for( int i = 0; i < x * y; i++ )
{
pDest[i*4 + 3] = pImage[i*4 + 3];
if( pDest[i*4 + 3] < 255 )
{
pDest[i*4] = (BYTE)(DWORD(pImage[i*4 + 2])*pImage[i*4 + 3]/255);
pDest[i*4 + 1] = (BYTE)(DWORD(pImage[i*4 + 1])*pImage[i*4 + 3]/255);
pDest[i*4 + 2] = (BYTE)(DWORD(pImage[i*4])*pImage[i*4 + 3]/255);
bAlphaChannel = true;
}
else
{
pDest[i*4] = pImage[i*4 + 2];
pDest[i*4 + 1] = pImage[i*4 + 1];
pDest[i*4 + 2] = pImage[i*4];
}
if( *(DWORD*)(&pDest[i*4]) == mask ) {
pDest[i*4] = (BYTE)0;
pDest[i*4 + 1] = (BYTE)0;
pDest[i*4 + 2] = (BYTE)0;
pDest[i*4 + 3] = (BYTE)0;
bAlphaChannel = true;
}
}
stbi_image_free(pImage);
TImageInfo* data = new TImageInfo;
data->hBitmap = hBitmap;
data->nX = x;
data->nY = y;
data->alphaChannel = bAlphaChannel;
return data;
}