284 lines
6.5 KiB
C++
284 lines
6.5 KiB
C++
#include "ximage.h"
|
|
#include "cximage.h"
|
|
#include "ximabmp.h"
|
|
|
|
#define IS_WIN30_DIB(lpbi) ((*(LPDWORD)(lpbi)) == sizeof(BITMAPINFOHEADER))
|
|
|
|
bool CxImage::Save( FILE* fOut, DWORD imagetype )
|
|
{
|
|
if (!fOut)
|
|
return false;
|
|
bool bOK = Encode(fOut,imagetype);
|
|
fclose(fOut);
|
|
return bOK;
|
|
}
|
|
|
|
CxImage::CxImage(const TCHAR * filename, DWORD imagetype)
|
|
{
|
|
Startup(imagetype);
|
|
Load(filename,imagetype);
|
|
}
|
|
|
|
bool CxImage::Load(const TCHAR * filename, DWORD imagetype)
|
|
{
|
|
bool bOK = false;
|
|
if ( GetTypeIndexFromId(imagetype) ){
|
|
FILE* hFile; //file handle to read the image
|
|
|
|
if ((hFile=_tfopen(filename,_T("rb")))==NULL) return false; // For UNICODE support
|
|
|
|
bOK = Decode(hFile,imagetype);
|
|
fclose(hFile);
|
|
if (bOK) return bOK;
|
|
}
|
|
|
|
char szError[256];
|
|
strcpy(szError,info.szLastError); //save the first error
|
|
|
|
FILE* hFile;
|
|
|
|
if ((hFile=_tfopen(filename,_T("rb")))==NULL) return false; // For UNICODE support
|
|
|
|
bOK = Decode(hFile,CXIMAGE_FORMAT_UNKNOWN);
|
|
fclose(hFile);
|
|
|
|
if (!bOK && imagetype > 0) strcpy(info.szLastError,szError); //restore the first error
|
|
|
|
return bOK;
|
|
}
|
|
|
|
bool CxImage::Save(const TCHAR * filename, DWORD imagetype)
|
|
{
|
|
FILE* hFile; //file handle to write the image
|
|
|
|
if ((hFile=_tfopen(filename,_T("wb")))==NULL) return false; // For UNICODE support
|
|
|
|
bool bOK = Encode(hFile,imagetype);
|
|
fclose(hFile);
|
|
return bOK;
|
|
}
|
|
|
|
DWORD CTangentImage::GetImageType(const TCHAR* pszName)
|
|
{
|
|
TCHAR dir[_MAX_DIR];
|
|
TCHAR ext[_MAX_EXT];
|
|
_tsplitpath(pszName, dir, dir, dir, ext);
|
|
DWORD dwType = 0; // unkown
|
|
// buildin type
|
|
if (_tcsicmp(ext, _T(".ICO")) == 0)
|
|
dwType = CXIMAGE_FORMAT_ICO;
|
|
else if(_tcsicmp(ext, _T(".PCX")) == 0)
|
|
dwType = CXIMAGE_FORMAT_PCX;
|
|
else if(_tcsicmp(ext, _T(".WBMP")) == 0)
|
|
dwType = CXIMAGE_FORMAT_WBMP;
|
|
else if(_tcsicmp(ext, _T(".WMF")) == 0)
|
|
dwType = CXIMAGE_FORMAT_WMF;
|
|
else if(_tcsicmp(ext, _T(".SKA")) == 0)
|
|
dwType = CXIMAGE_FORMAT_SKA;
|
|
else if(_tcsicmp(ext, _T(".PNG")) == 0)
|
|
dwType = CXIMAGE_FORMAT_PNG;
|
|
else if(_tcsicmp(ext, _T(".RAW")) == 0)
|
|
dwType = CXIMAGE_FORMAT_RAW;
|
|
else if(_tcsicmp(ext, _T(".MNG")) == 0)
|
|
dwType = CXIMAGE_FORMAT_MNG;
|
|
// else if(_tcsicmp(ext, _T(".JBG")) == 0)
|
|
// dwType = CXIMAGE_FORMAT_JBG;
|
|
else if(_tcsicmp(ext, _T(".PNM")) == 0)
|
|
dwType = CXIMAGE_FORMAT_PNM;
|
|
else if(_tcsicmp(ext, _T(".RAS")) == 0)
|
|
dwType = CXIMAGE_FORMAT_RAS;
|
|
else if(_tcsicmp(ext, _T(".JP2")) == 0)
|
|
dwType = CXIMAGE_FORMAT_JP2;
|
|
else if(_tcsicmp(ext, _T(".JPC")) == 0)
|
|
dwType = CXIMAGE_FORMAT_JPC;
|
|
else if(_tcsicmp(ext, _T(".PGX")) == 0)
|
|
dwType = CXIMAGE_FORMAT_PGX;
|
|
else if(_tcsicmp(ext, _T(".tif")) == 0)
|
|
dwType = CXIMAGE_FORMAT_TIF;
|
|
else if(_tcsicmp(ext, _T(".gif")) == 0)
|
|
dwType = CXIMAGE_FORMAT_GIF;
|
|
else if(_tcsicmp(ext, _T(".bmp")) == 0)
|
|
dwType = CXIMAGE_FORMAT_BMP;
|
|
else if(_tcsicmp(ext, _T(".jpg")) == 0)
|
|
dwType = CXIMAGE_FORMAT_JPG;
|
|
return dwType;
|
|
}
|
|
|
|
HANDLE CTangentImage::ReadJpeg(const TCHAR *pszInput)
|
|
{
|
|
return GetDibHandle(pszInput, CXIMAGE_FORMAT_JPG);
|
|
}
|
|
|
|
HANDLE CTangentImage::ReadJpeg( FILE *Input )
|
|
{
|
|
return GetDibHandle(Input, CXIMAGE_FORMAT_JPG);
|
|
}
|
|
|
|
DWORD CTangentImage::ReadJpeg( const TCHAR *pszInput, BITMAPINFOHEADER &header, char* &pData )
|
|
{
|
|
return GetBmpInfoByDib(GetDibHandle(pszInput, CXIMAGE_FORMAT_JPG), header, pData);
|
|
}
|
|
|
|
DWORD CTangentImage::ReadJpeg( FILE *Input, BITMAPINFOHEADER &header, char* &pData )
|
|
{
|
|
return GetBmpInfoByDib(GetDibHandle(Input, CXIMAGE_FORMAT_JPG), header, pData);
|
|
}
|
|
|
|
int CTangentImage::WriteJpeg( const TCHAR *pszInput, const TCHAR *pszOutput, int nType, float fQuality)
|
|
{
|
|
if (!nType)
|
|
return 0;
|
|
|
|
|
|
CxImage *pImg = new CxImage(pszInput, nType);
|
|
if (!pImg)
|
|
return 0;
|
|
pImg->SetJpegQualityF(fQuality);
|
|
pImg->Save(pszOutput, CXIMAGE_FORMAT_JPG);
|
|
delete pImg;
|
|
pImg = NULL;
|
|
return 1;
|
|
}
|
|
|
|
int CTangentImage::WriteJpeg( FILE *Input, FILE *Output, int nType, float fQuality)
|
|
{
|
|
if (!nType)
|
|
return 0;
|
|
|
|
CxImage *pImg = new CxImage(Input, nType);
|
|
if (!pImg)
|
|
return 0;
|
|
pImg->SetJpegQualityF(fQuality);
|
|
pImg->Save(Output, CXIMAGE_FORMAT_JPG);
|
|
delete pImg;
|
|
pImg = NULL;
|
|
return 1;
|
|
}
|
|
HANDLE CTangentImage::GetCopyToHandle( const TCHAR *pszInput, int nType)
|
|
{
|
|
return GetDibHandle(pszInput, nType);
|
|
}
|
|
|
|
HANDLE CTangentImage::GetCopyToHandle( FILE *Input, int nType )
|
|
{
|
|
return GetDibHandle(Input, nType);
|
|
}
|
|
|
|
WORD CTangentImage::PaletteSize( LPSTR lpbi )
|
|
{
|
|
if (IS_WIN30_DIB (lpbi))
|
|
return (WORD)(DIBNumColors(lpbi) * sizeof(RGBQUAD));
|
|
else
|
|
return (WORD)(DIBNumColors(lpbi) * sizeof(RGBTRIPLE));
|
|
}
|
|
|
|
WORD CTangentImage::DIBNumColors( LPSTR lpbi )
|
|
{
|
|
WORD wBitCount;
|
|
|
|
if (IS_WIN30_DIB(lpbi))
|
|
{
|
|
DWORD dwClrUsed;
|
|
|
|
dwClrUsed = ((LPBITMAPINFOHEADER)lpbi)->biClrUsed;
|
|
if (dwClrUsed != 0)
|
|
return (WORD)dwClrUsed;
|
|
}
|
|
|
|
if (IS_WIN30_DIB(lpbi))
|
|
wBitCount = ((LPBITMAPINFOHEADER)lpbi)->biBitCount;
|
|
else
|
|
wBitCount = ((LPBITMAPCOREHEADER)lpbi)->bcBitCount;
|
|
|
|
switch (wBitCount)
|
|
{
|
|
case 1:
|
|
return 2;
|
|
case 4:
|
|
return 16;
|
|
case 8:
|
|
return 256;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
char* CTangentImage::FindDIBBits( LPSTR lpbi )
|
|
{
|
|
return (lpbi + *(LPDWORD)lpbi + PaletteSize(lpbi));
|
|
}
|
|
|
|
HANDLE CTangentImage::ReadOtherPicFile( const TCHAR *pszInput )
|
|
{
|
|
return GetDibHandle(pszInput, GetImageType(pszInput));
|
|
}
|
|
|
|
DWORD CTangentImage::ReadOtherPicFile( const TCHAR *pszInput, BITMAPINFOHEADER &header, char* &pData )
|
|
{
|
|
return GetBmpInfoByDib(ReadOtherPicFile(pszInput), header, pData);
|
|
}
|
|
|
|
DWORD CTangentImage::GetBmpInfoByDib( HANDLE hDib , BITMAPINFOHEADER &header, char* &pData )
|
|
{
|
|
if (!hDib)
|
|
return 0;
|
|
char *lpDib = (LPSTR)::GlobalLock((HGLOBAL) hDib);
|
|
header = *(LPBITMAPINFOHEADER)lpDib;
|
|
DWORD dwBit = DibSizeImage(&header);
|
|
|
|
pData = new char [dwBit];
|
|
if (!pData)
|
|
return 0;
|
|
|
|
memset(pData, 0, dwBit);
|
|
memcpy(pData, FindDIBBits(lpDib), dwBit);
|
|
|
|
::GlobalUnlock((HGLOBAL)hDib);
|
|
return dwBit;
|
|
}
|
|
|
|
HANDLE CTangentImage::GetDibHandle( const TCHAR *pszInput, DWORD dwType)
|
|
{
|
|
if (!dwType)
|
|
return NULL;
|
|
|
|
CxImage *pImg = new CxImage(pszInput, dwType);
|
|
if(!pImg)
|
|
return 0;
|
|
HANDLE hRet = NULL;
|
|
hRet = pImg->CopyToHandle();
|
|
delete pImg;
|
|
pImg = NULL;
|
|
return hRet;
|
|
}
|
|
|
|
HANDLE CTangentImage::GetDibHandle( FILE *Input, DWORD dwType)
|
|
{
|
|
if (!dwType)
|
|
return NULL;
|
|
|
|
CxImage *pImg = new CxImage(Input, dwType);
|
|
if(!pImg)
|
|
return 0;
|
|
HANDLE hRet = NULL;
|
|
hRet = pImg->CopyToHandle();
|
|
delete pImg;
|
|
pImg = NULL;
|
|
return hRet;
|
|
}
|
|
|
|
int CTangentImage::OtherPicToJpeg( const TCHAR *pszInput, const TCHAR *pszOutput, float fQuality)
|
|
{
|
|
DWORD dwType = GetImageType(pszInput);
|
|
if (!dwType)
|
|
return 0;
|
|
|
|
CxImage *pImg = new CxImage(pszInput, dwType);
|
|
if(!pImg)
|
|
return 0;
|
|
pImg->SetJpegQualityF(fQuality);
|
|
pImg->Save(pszOutput, CXIMAGE_FORMAT_JPG);
|
|
delete pImg;
|
|
pImg = NULL;
|
|
return 1;
|
|
} |