623 lines
11 KiB
C++
623 lines
11 KiB
C++
//-------------------------------------------------------------------------------------------------------+
|
|
// Copyright (C), 1998-2007, SH Software Co. Ltd.
|
|
// = FileName : CImageDC 类
|
|
// = Version : ver2.0
|
|
// = Author : zjq
|
|
// = CreateDate : 2002-09-09
|
|
// = Description: CImageDC 定义
|
|
// = Maintainers:
|
|
//
|
|
//-------------------------------------------------------------------------------------------------------+
|
|
#include "StdAfx.h"
|
|
#include "CImageDC.h"
|
|
#include "XJpegLib.h"
|
|
#include "XBitmapLib.h"
|
|
#include "XPathParser.h"
|
|
#include "io.h"
|
|
#include "XPushMemVar.h"
|
|
|
|
#include "AString.h"
|
|
#include "XGlobalFunc.h"
|
|
#include "comdef.h"
|
|
|
|
CImageDC::CImageDC()
|
|
: m_bAutoScale(TRUE), m_bUniformScale(TRUE), m_bHighlight(FALSE)
|
|
{
|
|
m_pDC = NULL;
|
|
m_palDIB = NULL;
|
|
m_hDIB = NULL;
|
|
m_nIT = IT_NONE;
|
|
m_bkColor = RGB(0,0,0);
|
|
m_nRotation = 0.0;
|
|
}
|
|
|
|
CImageDC::CImageDC(CDC *pDC, const CRect &rect, COLORREF bkcolor)
|
|
: m_pDC(pDC), m_rectDC(rect), m_bAutoScale(TRUE), m_bUniformScale(TRUE), m_bHighlight(FALSE)
|
|
{
|
|
m_palDIB = NULL;
|
|
m_hDIB = NULL;
|
|
m_nIT = IT_NONE;
|
|
m_bkColor = bkcolor;
|
|
m_nRotation = 0.0;
|
|
}
|
|
|
|
CImageDC::~CImageDC()
|
|
{
|
|
if(m_palDIB)
|
|
{
|
|
delete m_palDIB;
|
|
}
|
|
|
|
if(m_hDIB)
|
|
{
|
|
::GlobalUnlock((HGLOBAL) m_hDIB);
|
|
::GlobalFree((HGLOBAL)m_hDIB);
|
|
}
|
|
}
|
|
|
|
void CImageDC::SetAutoScale(BOOL bAuto)
|
|
{
|
|
m_bAutoScale = bAuto;
|
|
}
|
|
|
|
void CImageDC::SetUniformScale(BOOL bTrue)
|
|
{
|
|
m_bUniformScale = bTrue;
|
|
}
|
|
|
|
void CImageDC::GetRectDC(CRect &rectDC) const
|
|
{
|
|
rectDC = m_rectDC;
|
|
}
|
|
void CImageDC::SetRectDC(const CRect &rect)
|
|
{
|
|
m_rectDC = rect;
|
|
}
|
|
void CImageDC::SetDC(CDC *pDc)
|
|
{
|
|
m_pDC = pDc;
|
|
}
|
|
void CImageDC::SetRotation(double nRot)
|
|
{
|
|
m_nRotation = nRot;
|
|
}
|
|
void CImageDC::SetBackColor(COLORREF co)
|
|
{
|
|
m_bkColor = co;
|
|
}
|
|
|
|
BOOL CImageDC::IsImageLoaded()
|
|
{
|
|
return (m_hDIB ? TRUE : FALSE);
|
|
}
|
|
|
|
BOOL CImageDC::IsHighlight() const
|
|
{
|
|
return m_bHighlight;
|
|
}
|
|
|
|
void CImageDC::SetHighlight(BOOL bTrue)
|
|
{
|
|
m_bHighlight = bTrue;
|
|
}
|
|
|
|
void CImageDC::InitDC(CDC *pDC, const CRect &rect, COLORREF bkcolor/*=RGB(0,0,0)*/)
|
|
{
|
|
m_pDC = pDC;
|
|
m_rectDC = rect;
|
|
m_bkColor = bkcolor;
|
|
}
|
|
|
|
COLORREF CImageDC::GetBkColor()
|
|
{
|
|
return m_bkColor;
|
|
}
|
|
|
|
BOOL CImageDC::PaintJpeg(const TCHAR *pJpgFile)
|
|
{
|
|
m_nIT = IT_BITMAP;
|
|
|
|
if(m_palDIB)
|
|
{
|
|
delete m_palDIB;
|
|
}
|
|
|
|
if(m_hDIB)
|
|
{
|
|
::GlobalUnlock((HGLOBAL) m_hDIB);
|
|
::GlobalFree((HGLOBAL)m_hDIB);
|
|
}
|
|
m_palDIB = NULL;
|
|
m_hDIB = NULL;
|
|
|
|
if(!pJpgFile || !pJpgFile[0])
|
|
{
|
|
Fill(RGB(192,192,192));
|
|
return TRUE;
|
|
}
|
|
XString sLib, sJpg;
|
|
if(!SplitLibraryPath(pJpgFile, sLib, sJpg))
|
|
{
|
|
return PaintImageOnly(pJpgFile);
|
|
}
|
|
|
|
XString strFilePath(256);
|
|
strFilePath << sLib << _T(".JPB");
|
|
|
|
XJpegLib lib(strFilePath, _ios::in|_ios::binary);
|
|
XString sTempPath(256);
|
|
GetTempPath(255, sTempPath);
|
|
|
|
int nStrLen=_tcslen(sTempPath);
|
|
if(nStrLen && sTempPath[nStrLen-1] != _T('\\'))
|
|
{
|
|
|
|
sTempPath << _T("\\");
|
|
}
|
|
|
|
sTempPath << sJpg;
|
|
int nRet = lib.QuickCopy(sTempPath);
|
|
|
|
if(nRet)
|
|
{
|
|
sTempPath << _T(".JPG");
|
|
}
|
|
else
|
|
{
|
|
Fill(m_bkColor);
|
|
return FALSE;
|
|
}
|
|
|
|
BOOL bRet = PaintImageOnly(sTempPath);
|
|
_tremove(sTempPath);
|
|
return bRet;
|
|
}
|
|
|
|
// pszImageFile FullPath of Image file, like:
|
|
// <Path>A.BMP, A.JPG, A(a1)
|
|
// Support BMP, JPG format (both stand along or from LIB)
|
|
// if pszImageFile == NULL, then clear it
|
|
BOOL CImageDC::PaintImage(const TCHAR *pszImageFile)
|
|
{
|
|
m_nIT = IT_BITMAP;
|
|
|
|
if(m_palDIB)
|
|
{
|
|
delete m_palDIB;
|
|
}
|
|
|
|
if(m_hDIB)
|
|
{
|
|
::GlobalUnlock((HGLOBAL) m_hDIB);
|
|
::GlobalFree((HGLOBAL)m_hDIB);
|
|
}
|
|
m_palDIB = NULL;
|
|
m_hDIB = NULL;
|
|
|
|
if(!pszImageFile || !pszImageFile[0])
|
|
{
|
|
Fill(RGB(192,192,192));
|
|
return TRUE;
|
|
}
|
|
|
|
XString sLib, sJpg;
|
|
if(!SplitLibraryPath(pszImageFile, sLib, sJpg))
|
|
{
|
|
return PaintImageOnly(pszImageFile);
|
|
}
|
|
|
|
XString strFilePath(256);
|
|
strFilePath << sLib << _T(".BMB");
|
|
|
|
XBitmapLib lib(strFilePath, _ios::in|_ios::binary);
|
|
XString sTempPath(256);
|
|
GetTempPath(255, sTempPath);
|
|
|
|
int nStrLen = _tcslen(sTempPath);
|
|
if(nStrLen && sTempPath[nStrLen-1] != _T('\\'))
|
|
{
|
|
sTempPath << _T("\\");
|
|
}
|
|
sTempPath << sJpg;
|
|
int nRet = lib.QuickCopy(sTempPath);
|
|
|
|
|
|
if(nRet == 1)
|
|
{
|
|
sTempPath << _T(".BMP");
|
|
}
|
|
else if(nRet == 2)
|
|
{
|
|
sTempPath << _T(".JPG");
|
|
}
|
|
else
|
|
{
|
|
Fill(m_bkColor);
|
|
return FALSE;
|
|
}
|
|
|
|
BOOL bRet = PaintImageOnly(sTempPath);
|
|
_tremove(sTempPath);
|
|
return bRet;
|
|
}
|
|
|
|
BOOL CImageDC::PaintDIB(HDIB hDib) // DIB is keep here
|
|
{
|
|
if(m_palDIB)
|
|
{
|
|
delete m_palDIB;
|
|
}
|
|
if(m_hDIB)
|
|
{
|
|
::GlobalUnlock((HGLOBAL) m_hDIB);
|
|
::GlobalFree((HGLOBAL)m_hDIB);
|
|
}
|
|
m_hDIB=hDib;
|
|
m_palDIB = new CPalette;
|
|
::CreateDIBPalette(m_hDIB, m_palDIB);
|
|
|
|
m_nIT = IT_BITMAP;
|
|
return PaintImage();
|
|
}
|
|
|
|
// Paint Image, not include from LIB
|
|
BOOL CImageDC::PaintImageOnly(const TCHAR *pszFile)
|
|
{
|
|
m_nIT = IT_BITMAP;
|
|
if(m_palDIB)
|
|
{
|
|
delete m_palDIB;
|
|
}
|
|
|
|
if(m_hDIB)
|
|
{
|
|
::GlobalUnlock((HGLOBAL) m_hDIB);
|
|
::GlobalFree((HGLOBAL)m_hDIB);
|
|
}
|
|
m_palDIB = NULL;
|
|
m_hDIB = NULL;
|
|
|
|
|
|
XPathParser parser(pszFile);
|
|
const TCHAR *pszExt = parser.m_sExt;
|
|
if(!pszExt[0])
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
if(_tcsicmp(pszExt, _T(".BMP")) == 0)
|
|
{
|
|
CFile file;
|
|
if (!file.Open(pszFile, CFile::modeRead | CFile::shareDenyWrite))
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
m_hDIB = ::ReadDIBFile(file);
|
|
}
|
|
else if(_tcsicmp(pszExt, _T(".JPG")) == 0)
|
|
{
|
|
/*CXImageFile ximageFile;
|
|
_bstr_t bst(pszFile);
|
|
m_hDIB = (HDIB)ximageFile.ReadJpeg((const char*)bst);
|
|
*/
|
|
}
|
|
else
|
|
{
|
|
//...其他格式
|
|
/*CXImageFile ximageFile;
|
|
_bstr_t bst(pszFile);
|
|
m_hDIB = (HDIB)ximageFile.ReadJpeg((const char*)bst);
|
|
*/
|
|
}
|
|
|
|
m_palDIB = new CPalette;
|
|
::CreateDIBPalette(m_hDIB, m_palDIB);
|
|
|
|
return PaintImage();
|
|
}
|
|
|
|
BOOL CImageDC::GetImageSize(CSize &sizeImage) const
|
|
{
|
|
if(!m_hDIB || !m_palDIB)
|
|
return FALSE;
|
|
|
|
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
|
|
sizeImage.cx = (int) ::DIBWidth(lpDIB); // Size of DIB - x
|
|
sizeImage.cy = (int) ::DIBHeight(lpDIB); // Size of DIB - y
|
|
::GlobalUnlock((HGLOBAL) m_hDIB);
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CImageDC::PaintImage()
|
|
{
|
|
if(!m_pDC)
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
m_nIT = IT_BITMAP;
|
|
|
|
if(!m_hDIB || !m_palDIB)
|
|
{
|
|
Fill(RGB(192,192,192));
|
|
return FALSE;
|
|
}
|
|
Fill(m_bkColor);
|
|
|
|
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
|
|
int cxDIB = (int) ::DIBWidth(lpDIB); // Size of DIB - x
|
|
int cyDIB = (int) ::DIBHeight(lpDIB); // Size of DIB - y
|
|
::GlobalUnlock((HGLOBAL) m_hDIB);
|
|
|
|
CRect rcDIB;
|
|
rcDIB.top = rcDIB.left = 0;
|
|
rcDIB.right = cxDIB;
|
|
rcDIB.bottom = cyDIB;
|
|
if(!m_bAutoScale)
|
|
{
|
|
CRect rectDest = m_rectDC;
|
|
rectDest.right = rectDest.left + cxDIB;
|
|
rectDest.bottom = rectDest.top + cyDIB;
|
|
::PaintDIB(m_pDC->m_hDC, &rectDest, m_hDIB, &rcDIB, m_palDIB);
|
|
}
|
|
else if(m_bUniformScale)
|
|
{
|
|
// Do uniform scale, so adjust rectDC
|
|
CRect rectDC = m_rectDC;
|
|
double nWidthDIB = rcDIB.Width(),
|
|
nWidthDC=m_rectDC.Width(),
|
|
nHeightDIB = rcDIB.Height(),
|
|
nHeightDC = m_rectDC.Height();
|
|
|
|
double nScale = min(nWidthDC / nWidthDIB, nHeightDC / nHeightDIB);
|
|
|
|
// 平移到中心的距离
|
|
double nMoveX=0, nMoveY=0;
|
|
nMoveX = (m_rectDC.Width() - nScale * rcDIB.Width()) / 2.0;
|
|
nMoveY = (m_rectDC.Height() - nScale * rcDIB.Height()) / 2.0;
|
|
|
|
rectDC.DeflateRect(int(nMoveX), int(nMoveY));
|
|
::PaintDIB(m_pDC->m_hDC, &rectDC, m_hDIB, &rcDIB, m_palDIB);
|
|
}
|
|
else
|
|
{ // not uniform scale
|
|
CRect rectDest=m_rectDC;
|
|
::PaintDIB(m_pDC->m_hDC, &rectDest, m_hDIB, &rcDIB, m_palDIB);
|
|
}
|
|
|
|
if(m_bHighlight)
|
|
{
|
|
CRect rect = m_rectDC;
|
|
m_pDC->Draw3dRect(rect,RGB(128,128,128),RGB(240,240,240)); //3D
|
|
rect.DeflateRect(1, 1);
|
|
m_pDC->Draw3dRect(rect,RGB(128,128,128),RGB(240,240,240)); //3D
|
|
rect.DeflateRect(1,1);
|
|
|
|
CBrush brush(RGB(255,0,0));
|
|
m_pDC->FrameRect(&rect, &brush);
|
|
rect.DeflateRect(1,1);
|
|
m_pDC->FrameRect(&rect, &brush);
|
|
}
|
|
else
|
|
{
|
|
CBrush brush(GetBkColor());
|
|
CRect rect = m_rectDC;
|
|
m_pDC->FrameRect(&rect, &brush);
|
|
rect.DeflateRect(1,1);
|
|
m_pDC->FrameRect(&rect, &brush);
|
|
rect.DeflateRect(1,1);
|
|
m_pDC->FrameRect(&rect, &brush);
|
|
rect.DeflateRect(1,1);
|
|
m_pDC->FrameRect(&rect, &brush);
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CImageDC::PaintSlide(const TCHAR *pszSlideFile)
|
|
{
|
|
if(!m_pDC)
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
m_nIT = IT_SLIDE;
|
|
m_slide.Reset();
|
|
if(!pszSlideFile || !pszSlideFile[0])
|
|
{
|
|
Fill(RGB(192,192,192));
|
|
return TRUE;
|
|
}
|
|
|
|
if(m_slide.FromSlide(pszSlideFile) != RTNORM)
|
|
{
|
|
Fill(RGB(192,192,192));
|
|
return FALSE;
|
|
}
|
|
return PaintSlide();
|
|
}
|
|
|
|
BOOL CImageDC::PaintSlide()
|
|
{
|
|
if(!m_pDC)
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
if(!m_slide)
|
|
{
|
|
Fill(RGB(192,192,192));
|
|
return FALSE;
|
|
}
|
|
|
|
if(m_bHighlight)
|
|
{
|
|
Fill(RGB(0,128,192));
|
|
}
|
|
else
|
|
{
|
|
Fill(m_bkColor);
|
|
}
|
|
|
|
XImageBufList lstVectors;
|
|
lstVectors = m_slide;
|
|
if(fabs(m_nRotation) > 1.0E-6)
|
|
{
|
|
ads_matrix mat;
|
|
XPoint().GetRotationMatrix(m_nRotation, mat);
|
|
for(int n = 0; n < lstVectors.Length(); n++)
|
|
{
|
|
XImageBuf &buf = *lstVectors[n];
|
|
XPoint ptStart = buf.GetStartPoint(),
|
|
ptEnd = buf.GetEndPoint();
|
|
|
|
ptStart.TransformBy(mat);
|
|
ptEnd.TransformBy(mat);
|
|
|
|
buf.SetStartPoint(ptStart);
|
|
buf.SetEndPoint(ptEnd);
|
|
}
|
|
|
|
for(int n = 0; n < lstVectors.m_lstSolid.Length(); n++)
|
|
{
|
|
XColorSolid &buf = *lstVectors.m_lstSolid[n];
|
|
buf.TransUCS(mat);
|
|
}
|
|
}
|
|
|
|
lstVectors.WcsToDcs(m_rectDC.Width(), m_rectDC.Height(), 5);
|
|
for(int i = lstVectors.Length() - 1; i >= 0; i--)
|
|
{
|
|
XImageBuf &buf=*lstVectors[i];
|
|
CPoint ptStart(m_rectDC.left + AS_INT(buf.GetStartPoint().x), m_rectDC.top + AS_INT(buf.GetStartPoint().y));
|
|
CPoint ptEnd(m_rectDC.left + AS_INT(buf.GetEndPoint().x), m_rectDC.top + AS_INT(buf.GetEndPoint().y));
|
|
int nR, nG, nB;
|
|
ColorToRGB(buf.nColorIndex, &nR, &nG, &nB);
|
|
|
|
if(m_bkColor == RGB(255,255,255))
|
|
{//白底
|
|
nR = 255 - nR;
|
|
nG = 255 - nG;
|
|
nB = 255 - nB;
|
|
}
|
|
|
|
CPen pen(PS_SOLID, 1, RGB(nR, nG, nB));
|
|
CPen *pOld = m_pDC->SelectObject(&pen);
|
|
|
|
m_pDC->MoveTo(ptStart);
|
|
if(ptStart == ptEnd)
|
|
{
|
|
ptEnd.x += 1;
|
|
ptEnd.y += 1;
|
|
}
|
|
m_pDC->LineTo(ptEnd);
|
|
m_pDC->SelectObject(pOld);
|
|
}
|
|
|
|
for(int i = 0; i < lstVectors.m_lstSolid.Length(); i++)
|
|
{
|
|
XColorSolid &ln = *lstVectors.m_lstSolid[i];
|
|
int nR, nG, nB;
|
|
ColorToRGB(ln.m_nColor, &nR, &nG, &nB);
|
|
if(m_bkColor == RGB(255,255,255))
|
|
{//白底
|
|
nR = 255 - nR;
|
|
nG = 255 - nG;
|
|
nB = 255 - nB;
|
|
}
|
|
|
|
CBrush brush;
|
|
brush.CreateSolidBrush(RGB(nR,nG,nB));
|
|
|
|
CBrush *pOld = m_pDC->SelectObject(&brush);
|
|
|
|
CPoint *verts = new CPoint[ln.Length()];
|
|
for(int j = 0; j < ln.Length(); j++)
|
|
{
|
|
XPoint pt = *ln[j];
|
|
verts[j].x = m_rectDC.left + AS_INT(pt.x);
|
|
verts[j].y = m_rectDC.top + AS_INT(pt.y);
|
|
}
|
|
|
|
CRgn rgn;
|
|
rgn.CreatePolygonRgn(verts,ln.Length(),ALTERNATE);
|
|
|
|
m_pDC->FillRgn(&rgn,&brush);
|
|
m_pDC->SelectObject(pOld);
|
|
brush.DeleteObject();
|
|
}
|
|
|
|
m_nIT = IT_SLIDE;
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CImageDC::PaintBitmap(CBitmap &bmp)
|
|
{
|
|
if(!m_pDC || !bmp.GetSafeHandle())
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
BITMAP bmpInfo;
|
|
bmp.GetBitmap(&bmpInfo);
|
|
|
|
CDC dcTmp;
|
|
dcTmp.CreateCompatibleDC(m_pDC);
|
|
dcTmp.SelectObject(&bmp);
|
|
|
|
m_pDC->StretchBlt(m_rectDC.left, m_rectDC.top, m_rectDC.Width(), m_rectDC.Height(),
|
|
&dcTmp, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
// Refresh the image
|
|
BOOL CImageDC::Redraw()
|
|
{
|
|
//Fill(m_bkColor);
|
|
if(m_nIT == IT_SLIDE)
|
|
{
|
|
|
|
return PaintSlide();
|
|
}
|
|
|
|
else if(m_nIT == IT_BITMAP)
|
|
{
|
|
return PaintImage();
|
|
}
|
|
else
|
|
{
|
|
Fill(RGB(192,192,192));
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
void CImageDC::Fill(COLORREF cref)
|
|
{
|
|
if(!m_pDC)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if(!cref)
|
|
{
|
|
m_pDC->FillSolidRect(&m_rectDC, m_bkColor);
|
|
}
|
|
else
|
|
{
|
|
m_pDC->FillSolidRect(&m_rectDC, cref);
|
|
}
|
|
}
|
|
|
|
BOOL CImageDC::PaintImageNormal(const TCHAR * pszImageFile)
|
|
{
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CImageDC::PaintImageOnlyNormal(const TCHAR * pszImageFile)
|
|
{
|
|
return TRUE;
|
|
|
|
}
|