/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows(Class Library) - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(Feature1)------------------------------------------------------------- We have made the assumption that the user has the knowledge of Object Oriented programing of C under Windows. This example will: 1. load an image from a file, whose name is sent through the command line, to a bitmap as 24 bits per pixel, 2. display the image, 3. copy the data to a different bitmap, half-tone the original image, and re-display the image, 4. restore the original image (by copying the handle of the new bitmap to the original) and re-display the image, 5. fill the bitmap with a solid color and re-display the image. Usage: FEATURE3 --------------------------------------------------------------------------*/ // Ftr3View.cpp : implementation of the CFeature3View class // #include "stdafx.h" #include "Feature3.h" #include "Fetr3Doc.h" #include "Ftr3View.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CFeature3View IMPLEMENT_DYNCREATE(CFeature3View, CView) BEGIN_MESSAGE_MAP(CFeature3View, CView) //{{AFX_MSG_MAP(CFeature3View) ON_WM_TIMER() ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CFeature3View construction/destruction CFeature3View::CFeature3View() { // TODO: add construction code here m_bFirst = TRUE; m_ProcessNum = DO_COPY; } CFeature3View::~CFeature3View() { } ///////////////////////////////////////////////////////////////////////////// // CFeature3View drawing void CFeature3View::OnDraw(CDC* pDC) { CFeature3Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here if(!m_LeadBitmap.IsAllocated()|| m_ProcessNum==DO_PAINTRGNDC) return; HDC hdc = pDC->GetSafeHdc(); HPALETTE hOldPal = NULL; if ( m_LeadBitmap.GetPalette() )//Is there a palette that needs to be selected? hOldPal = SelectPalette (hdc, m_LeadBitmap.GetPalette(), TRUE); // Select it m_LeadBitmap.Paint()->SetDC(hdc); if( m_LeadBitmap.HasRgn() ) { if( m_ProcessNum == DO_PAINTEFFECT ) m_LeadBitmap.Paint()->PaintRgnDC (); else { m_LeadBitmap.PaintEffect()->SetDC(hdc); m_LeadBitmap.PaintEffect()->PaintRgnDCEffect (EFX_EFFECT_WIPE4_L_L_R_R); } } else m_LeadBitmap.Paint()->PaintDC(); if (hOldPal) SelectPalette (hdc,hOldPal , TRUE); m_bFirst = FALSE; } ///////////////////////////////////////////////////////////////////////////// // CFeature3View diagnostics #ifdef _DEBUG void CFeature3View::AssertValid() const { CView::AssertValid(); } void CFeature3View::Dump(CDumpContext& dc) const { CView::Dump(dc); } CFeature3Doc* CFeature3View::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFeature3Doc))); return (CFeature3Doc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CFeature3View message handlers void CFeature3View::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default L_INT nRet; L_TCHAR buf[41]; HCURSOR hCursor; LUserFile* pLeadFile; if (m_bFirst) return ; m_nCount++; //Increment Counter /* Has the image been displayed about 3 seconds? (this is reset in the function OnDraw) */ if (m_nCount == 1) { m_hOrigCursor = SetCursor (LoadCursor (NULL, IDC_SIZENS)); return; } else if (m_nCount == 2) { SetCursor (LoadCursor (NULL, IDC_SIZENESW)); return; } SetCursor (LoadCursor (NULL, IDC_WAIT)); switch (m_ProcessNum) { case DO_COPY: m_ProcessNum = DO_BACK; AfxGetMainWnd()->SetWindowText (_T("Copying and HalfToning Image...")); // Copy the old bitmap handle to the new one. nRet = m_LeadNewBitmap.Copy(m_LeadBitmap); if (nRet == SUCCESS) { if (m_LeadNewBitmap.GetBitsPerPixel() <= 8) m_LeadNewBitmap.SetPalette( m_LeadBitmap.DupPalette() ); // Halftone the bitmap for viewing nRet = m_LeadBitmap.HalfTone(HT_VIEW,0,1, (HBITMAPLIST)NULL); if (nRet == SUCCESS) { AfxGetMainWnd()->SetWindowText (_T("Image Is HalfToned")); HandlePalette(WM_QUERYNEWPALETTE,m_hWnd); } else AfxGetMainWnd()->SetWindowText (_T("ERROR HalfToning The Image")); } else AfxGetMainWnd()->SetWindowText (_T("ERROR Copying The Image Data")); break; case DO_BACK: m_ProcessNum = DO_FILL; if ( m_LeadNewBitmap.IsAllocated() ) { AfxGetMainWnd()->SetWindowText (_T("Restoring Original Image...")); m_LeadBitmap.Free(); // Free the HalfToned bitmap. // Bring back the original bitmap from the new bitmap handle. m_LeadBitmap.Copy(m_LeadNewBitmap); m_LeadNewBitmap.Free(); // Free the temporary bitmap. } AfxGetMainWnd()->SetWindowText (_T("Image Is Restored.")); HandlePalette(WM_QUERYNEWPALETTE,m_hWnd); break; case DO_FILL: AfxGetMainWnd()->SetWindowText (_T("Filling Image...")); /* This will reset the storage (turn all pixel data to zero) currently allocated for the bitmap. */ nRet = m_LeadBitmap.Fill(RGB ( 255, 0, 128) ); if (nRet == SUCCESS) { AfxGetMainWnd()->SetWindowText (_T("Image Is Filled.")); HandlePalette(WM_QUERYNEWPALETTE,m_hWnd); } else AfxGetMainWnd()->SetWindowText (_T("ERROR Filling The Image.")); InvalidateRect (NULL); UpdateWindow(); m_ProcessNum = DO_PAINTRGNDC; break; case DO_PAINTRGNDC: hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); AfxGetMainWnd()->SetWindowText (_T("Now only what is inside the region should be painted.")); // Load the image. m_LeadBitmap.File()->EnableCallBack(TRUE); pLeadFile = (LUserFile*)m_LeadBitmap.File(); pLeadFile->m_hWnd = m_hWnd; RECT rc; SetRect( &rc, 0, 0, m_LeadBitmap.GetWidth(), m_LeadBitmap.GetHeight() ); m_LeadBitmap.Region()->SetRgnEllipse(&rc); nRet = m_LeadBitmap.File()->LoadFile (0, ORDER_BGR, LOADFILE_ALLOCATE | LOADFILE_STORE); if( nRet != SUCCESS ) { LBase::DisplayError( AfxGetMainWnd()->m_hWnd, nRet); break; } HandlePalette(WM_QUERYNEWPALETTE,m_hWnd); m_LeadBitmap.Region()->SetRgnXForm(&pLeadFile->m_XForm); if (m_LeadBitmap.GetViewPerspective() != TOP_LEFT) m_LeadBitmap.ChangeViewPerspective(TOP_LEFT); SetRect(&rc, 0, 0, m_LeadBitmap.GetWidth(), m_LeadBitmap.GetHeight() ); m_LeadBitmap.Region()->SetRgnEllipse(&rc); nRet = m_LeadBitmap.Region()->GetRgnArea( &m_dwRegionArea ); SetCursor (hCursor); if( nRet != SUCCESS ) { LBase::DisplayError( AfxGetMainWnd()->m_hWnd, nRet); CFeature3App* pCApp = ( CFeature3App* ) AfxGetApp(); wsprintf (buf, _T("Error %d getting the region area"), nRet, (L_TCHAR*)(LPCTSTR) pCApp->m_strFileName); AfxGetMainWnd()->SetWindowText(buf); } else { wsprintf( buf, _T("The region area is %ld pixels"), m_dwRegionArea ); AfxGetMainWnd()->SetWindowText(buf); } m_ProcessNum = DO_PAINTEFFECT; break; case DO_PAINTEFFECT: m_ProcessNum = DO_QUIT; AfxGetMainWnd()->SetWindowText(_T("Paint only the region using an effect. Example is complete (Class library)") ); break; } m_nCount=0; // Invalidate the entire client region to display the changes. if(m_ProcessNum!=DO_PAINTRGNDC) InvalidateRect (NULL); // Kill the timer if m_ProcessNum is DO_QUIT. if (m_ProcessNum == DO_QUIT) KillTimer (m_nTimer); SetCursor (m_hOrigCursor); CView::OnTimer(nIDEvent); } int CFeature3View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; CFeature3App* pCApp = ( CFeature3App* ) AfxGetApp(); L_INT nRet = m_LeadBitmap.Load((L_TCHAR*) pCApp->m_lpCmdLine); if ( nRet != SUCCESS ) LBase::DisplayError(AfxGetMainWnd()->m_hWnd, nRet); m_nCount = 0; m_nTimer = SetTimer (1, 1000, NULL); return 0; } void CFeature3View::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) { // TODO: Add your specialized code here and/or call the base class if(bActivate==TRUE&&pActivateView==this) HandlePalette(WM_QUERYNEWPALETTE,m_hWnd); CView::OnActivateView(bActivate, pActivateView, pDeactiveView); } L_BOOL CFeature3View::HandlePalette(UINT uMsg,HWND hWnd) { HDC hDC; switch(uMsg) { case WM_PALETTECHANGED: if(m_hWnd== hWnd) return TRUE; case WM_SYSCOLORCHANGE: case WM_QUERYNEWPALETTE: hDC = ::GetDC(m_hWnd); HPALETTE hPalette = m_LeadBitmap.CreatePaintPalette(hDC); L_UINT uColor=0; if(hPalette) { HPALETTE oldPalette = ::SelectPalette(hDC,hPalette,(uMsg==WM_PALETTECHANGED)); uColor=::RealizePalette(hDC); ::SelectPalette(hDC,oldPalette,TRUE); } ::ReleaseDC(m_hWnd,hDC); if(uColor!=0) { InvalidateRect(NULL,false); UpdateWindow(); return TRUE; } break; } return FALSE; }