QuickView.cpp
上传用户:gzboli
上传日期:2013-04-10
资源大小:471k
文件大小:7k
- // MatchNavView.cpp : implementation file
- //
- #include "stdafx.h"
- #include "QuickImage.h"
- #include "QuickView.h"
- #include "dibapi.h"
- #include "FileView.h"
- #include "DIBDoc.h"
- #include "raw.h"
- #include "importsjpegfile.h"
- #include "importsgifapi.h"
- #include "MainFrm.h"
- #include "global.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CQuickView
- IMPLEMENT_DYNCREATE(CQuickView, CView)
- CQuickView::CQuickView()
- {
- m_rcDIB = m_rcDC = CRect(0,0,0,0);
- m_HDIB = NULL;
- *m_lpszFileName = ' ';
- }
- CQuickView::~CQuickView()
- {
- if(NULL != m_HDIB)
- {
- GlobalFree(m_HDIB);
- m_HDIB = NULL;
- }
- *m_lpszFileName = ' ';
- }
- BEGIN_MESSAGE_MAP(CQuickView, CView)
- //{{AFX_MSG_MAP(CQuickView)
- ON_WM_ERASEBKGND()
- ON_WM_SIZE()
- ON_WM_LBUTTONDOWN()
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONUP()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CQuickView drawing
- void CQuickView::OnDraw(CDC* pDC)
- {
- // CDocument* pDoc = GetDocument();
- // TODO: add draw code her
- // HDIB hDIB = GetDocument()->GetHDIB();
- if (m_HDIB != NULL)
- {
- ::PaintDIB(pDC->GetSafeHdc(), &m_rcDC, m_HDIB, &m_rcDIB, NULL);
- }
- else
- {
- CPoint pt = m_rcDC.CenterPoint();
- int iOldBkMode = pDC->SetBkMode(TRANSPARENT);
- int iOldAlign = pDC->SetTextAlign(TA_CENTER);
- pDC->TextOut(pt.x, pt.y, _T("No Preview!"));
-
- pDC->SetTextAlign(iOldAlign);
- pDC->SetBkMode(iOldBkMode);
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CQuickView diagnostics
- #ifdef _DEBUG
- void CQuickView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CQuickView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CQuickView message handlers
- BOOL CQuickView::OnEraseBkgnd(CDC* pDC)
- {
- CBrush brBack;
- // brBack.CreateSolidBrush((COLORREF) GetSysColor(COLOR_DESKTOP));
- COLORREF bkColor = GetSysColor(COLOR_BACKGROUND);
- if(RGB(0, 0, 0) == bkColor)
- {
- bkColor = RGB(192, 192, 192);
- }
- brBack.CreateSolidBrush(bkColor);
- CBrush* pOldBrush = pDC->SelectObject(&brBack);
- CRect rect;
- pDC->GetClipBox(&rect); // 擦除所需的区域
- pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
- pDC->SelectObject(pOldBrush);
- return TRUE;
- // return CView::OnEraseBkgnd(pDC);
- }
- void CQuickView::OnInitialUpdate()
- {
- CView::OnInitialUpdate();
- /*
- HDIB hDIB = GetDocument()->GetHDIB();
- if (hDIB != NULL)
- {
- LPSTR lpbi = (LPSTR)::GlobalLock((HGLOBAL) hDIB);;
- m_rcDIB.right = (int) ::DIBWidth((LPSTR)lpbi); // Size of DIB - x
- m_rcDIB.bottom = (int) abs(::DIBHeight((LPSTR)lpbi)); // Size of DIB - y
- CalcDCRect();
-
- ::GlobalUnlock((HGLOBAL) hDIB);
- }*/
- GetClientRect(&m_rcDC);
- }
- void CQuickView::OnSize(UINT nType, int cx, int cy)
- {
- CView::OnSize(nType, cx, cy);
-
- if((m_rcDIB.Width() > 0) && (m_rcDIB.Height() > 0))
- {
- CalcRects();
- // Invalidate();
- }
- }
- void CQuickView::CalcRects()
- {
- ASSERT(NULL != m_HDIB);
- LPSTR lpbi = (LPSTR)::GlobalLock((HGLOBAL) m_HDIB);;
- m_rcDIB.right = (int) ::DIBWidth((LPSTR)lpbi); // Size of DIB - x
- m_rcDIB.bottom = (int) abs(::DIBHeight((LPSTR)lpbi));
- ::GlobalUnlock((HGLOBAL) m_HDIB);
- //calcu m_rcDIB
- GetClientRect(&m_rcDC);
- m_rcDC.InflateRect(-2, -2);
- double dx = (double)m_rcDC.Width() / m_rcDIB.Width();
- double dy = (double)m_rcDC.Height() / m_rcDIB.Height();
- POINT pt = m_rcDC.CenterPoint();
- int iHalf;
- if((dx > 1.0) && (dy > 1.0))
- {
- iHalf = int((double)m_rcDIB.Width() / 2.0 + 0.5);
- m_rcDC.left = pt.x - iHalf;
- m_rcDC.right = pt.x + iHalf;
- iHalf = int((double)m_rcDIB.Height() / 2.0 + 0.5);
- m_rcDC.top = pt.y - iHalf;
- m_rcDC.bottom = pt.y + iHalf;
- }
- else if(dx > dy)
- {
- iHalf = int(dy * (double)m_rcDIB.Width() / 2.0);
- m_rcDC.left = pt.x - iHalf;
- m_rcDC.right = pt.x + iHalf;
- }
- else
- {
- iHalf = int(dx * (double)m_rcDIB.Height() / 2.0);
- m_rcDC.top = pt.y - iHalf;
- m_rcDC.bottom = pt.y + iHalf;
- }
- }
- CDIBDoc* CQuickView::GetDocument()
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDIBDoc)));
- return (CDIBDoc*)m_pDocument;
- }
- BOOL CQuickView::DrawIMG(void)
- {
- ASSERT(strlen(m_lpszFileName) > 0);
- char *pExt = strrchr(m_lpszFileName, '.');
- if(NULL == pExt)
- return FALSE;
- CString strExt;
- strExt.Format("%s", ++pExt);
- strExt.MakeUpper();
- HDIB hDIB = NULL;
- if((0 == strExt.Compare("BMP"))
- || (0 == strExt.Compare("DIB"))
- || (0 == strExt.Compare("DDB")))
- {
- CFile file;
- CFileException fe;
- if (!file.Open(m_lpszFileName, CFile::modeRead | CFile::shareDenyWrite, &fe))
- {
- fe.ReportError();
- return NULL;
- }
- // replace calls to Serialize with ReadDIBFile function
- TRY
- {
- hDIB = ::ReadDIBFile(file);
- }
- CATCH (CFileException, eLoad)
- {
- file.Abort(); // will not throw an exception
- eLoad->ReportError();
- if(NULL != hDIB)
- {
- GlobalFree(hDIB);
- hDIB = NULL;
- }
- return FALSE;
- }
- END_CATCH
- if (hDIB == NULL)
- {
- // may not be DIB format
- CString strMsg;
- strMsg.LoadString(IDS_CANNOT_LOAD_DIB);
- MessageBox(strMsg, NULL, MB_ICONINFORMATION | MB_OK);
- return FALSE;
- }
- }
- else if((0 == strExt.Compare("JPG"))
- || (0 == strExt.Compare("JPEG")))
- {
- UINT height,width;
- BYTE *pData = NULL;
- pData = JpegFile::JpegFileToRGB(m_lpszFileName, &width, &height);
- if(NULL == pData)return FALSE;
- JpegFile::BGRFromRGB(pData, width, height);
- JpegFile::VertFlipBuf(pData, width * 3, height);
-
- UINT widthDW;
- BYTE *pDIB = JpegFile::MakeDwordAlignedBuf(pData, width, height, &widthDW);
-
- delete []pData;
- if(NULL == pDIB)
- {
- char strErr[100];
- sprintf(strErr, "Error: %d", GetLastError());
- MessageBox(strErr, "Read File Error",
- MB_OK|MB_ICONERROR);
- return FALSE;
- }
- hDIB = ::ShowJPEG(width, height, pDIB);
- delete []pDIB;
- }
- else if(0 == strExt.Compare("GIF"))
- {
- hDIB = ReadGIF(m_lpszFileName);
- }
- else if(0 == strExt.Compare("PCX"))
- {
- hDIB = ::ReadPCX256(m_lpszFileName);
- }
- /* else if(0 == strExt.Compare("RAW"))
- {
- CRaw raw;
- if(!raw.ReadFromFile(m_lpszFileName))
- {
- return FALSE;
- }
- hDIB = raw.Show();
- }
- */ else
- {
- return FALSE;
- }
- ASSERT(NULL == m_HDIB);
- m_HDIB = hDIB;
- return TRUE;
- }
- void CQuickView::SetFileName(LPCTSTR lpszFileName)
- {
- CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
- ASSERT(NULL != pMainWnd);
- if(NULL == lpszFileName)
- {
- *m_lpszFileName = ' ';
- if(NULL != m_HDIB)
- {
- GlobalFree(m_HDIB);
- }
- m_HDIB = NULL;
-
- pMainWnd->SetStatusText(0);
- Invalidate();
- }
- else if(0 != stricmp(lpszFileName, m_lpszFileName))
- {
- strcpy(m_lpszFileName, lpszFileName);
-
- if(CGlobal::IsImgFile(m_lpszFileName))
- {
- if(NULL != m_HDIB)
- {
- GlobalFree(m_HDIB);
- }
- m_HDIB = NULL;
- DrawIMG();
- CalcRects();
-
- CString strText;
- strText.Format("Image Size: %d,%d", m_rcDIB.Width(), m_rcDIB.Height());
- pMainWnd->SetStatusText(1, (LPCTSTR)strText);
-
- strText.Format("Zoom: %d%%",
- int((double)m_rcDC.Width() / m_rcDIB.Width() * 100.0));
- pMainWnd->SetStatusText(2, (LPCTSTR)strText);
- }
- else
- {
- if(NULL != m_HDIB)
- {
- GlobalFree(m_HDIB);
- }
- m_HDIB = NULL;
-
- pMainWnd->SetStatusText(0);
- }
- Invalidate();
- }
- }