/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows(Class Library) - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(Feature2)------------------------------------------------------------- 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. colorres the image to 8 bits per pixel with the LEAD fixed palette using Jarvis dithering, and re-display the image, 4. reverse and re-display the image, 5. grayscale and re-display the image, 6. resize and re-display the image. Usage: FEATURE2 --------------------------------------------------------------------------*/ // Ftr2View.cpp : implementation of the CFeature2View class // #include "stdafx.h" #include "Feature2.h" #include "Fetr2Doc.h" #include "Ftr2View.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CFeature2View IMPLEMENT_DYNCREATE(CFeature2View, CView) BEGIN_MESSAGE_MAP(CFeature2View, CView) //{{AFX_MSG_MAP(CFeature2View) ON_WM_TIMER() ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CFeature2View construction/destruction CFeature2View::CFeature2View() { // TODO: add construction code here m_bFirst = TRUE; m_ProcessNum = DO_COLORRES; } CFeature2View::~CFeature2View() { } ///////////////////////////////////////////////////////////////////////////// // CFeature2View drawing void CFeature2View::OnDraw(CDC* pDC) { CFeature2Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here HDC hdc; HPALETTE hOldPalette = NULL; hdc = pDC->GetSafeHdc();// Get DC if(m_LeadBitmap.GetPalette()) hOldPalette = SelectPalette(hdc, m_LeadBitmap.GetPalette(), TRUE); m_LeadBitmap.Paint()->SetDC( hdc ); m_LeadBitmap.Paint()->PaintDC(); if(hOldPalette!=NULL) SelectPalette(hdc, hOldPalette, TRUE); m_bFirst = FALSE; } ///////////////////////////////////////////////////////////////////////////// // CFeature2View diagnostics #ifdef _DEBUG void CFeature2View::AssertValid() const { CView::AssertValid(); } void CFeature2View::Dump(CDumpContext& dc) const { CView::Dump(dc); } CFeature2Doc* CFeature2View::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFeature2Doc))); return (CFeature2Doc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CFeature2View message handlers void CFeature2View::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default L_INT nRet; RECT rcClient; RECT rcWindow; RECT rc; 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_COLORRES: AfxGetMainWnd()->SetWindowText (_T("Dithering Image...")); /* Dither the bitmap down to 256 colors, with the LEAD Fixed Palette using Jarvis Dithering. */ nRet = m_LeadBitmap.ColorRes (8, CRF_FIXEDPALETTE | CRF_JARVISDITHERING); if (nRet == SUCCESS) { AfxGetMainWnd()->SetWindowText (_T("Image Is Dithered")); HandlePalette(WM_QUERYNEWPALETTE,m_hWnd); } else AfxGetMainWnd()->SetWindowText (_T("ERROR Dithering The Image")); m_ProcessNum = DO_REVERSE; break; case DO_REVERSE: AfxGetMainWnd()->SetWindowText (_T("Reversing Image...")); // Flip the image left to right. nRet = m_LeadBitmap.Reverse( ); if (nRet == SUCCESS) AfxGetMainWnd()->SetWindowText (_T("Image Is Reversed")); else AfxGetMainWnd()->SetWindowText (_T("ERROR Reversing The Image")); m_ProcessNum = DO_GRAYSCALE; break; case DO_GRAYSCALE: AfxGetMainWnd()->SetWindowText (_T("Grayscaling Image...")); // Gray scale the bitmap to 8 bit per pixel. nRet = m_LeadBitmap.GrayScale(); if (nRet == SUCCESS) { AfxGetMainWnd()->SetWindowText (_T("Image Is Grayscaled")); HandlePalette(WM_QUERYNEWPALETTE,m_hWnd); } else AfxGetMainWnd()->SetWindowText (_T("ERROR Grayscaling The Image")); m_ProcessNum = DO_SIZE; break; case DO_SIZE: AfxGetMainWnd()->SetWindowText (_T("Resizing Image...")); // This will resize the bitmap to 320 by 200. nRet = m_LeadBitmap.Size(320, 200); if (nRet == SUCCESS) { // Resize the window to accomodate the new image dimensions. AfxGetMainWnd()->GetClientRect (&rcClient); AfxGetMainWnd()->GetWindowRect (&rcWindow); AfxGetMainWnd()->MoveWindow ( rcWindow.left, rcWindow.top, RECTWIDTH (&rcWindow) + m_LeadBitmap.GetWidth() - RECTWIDTH (&rcClient), RECTHEIGHT (&rcWindow) + m_LeadBitmap.GetHeight() - RECTHEIGHT (&rcClient) ); SetRect(&rc, 0, 0, m_LeadBitmap.GetWidth(), m_LeadBitmap.GetHeight() ); m_LeadBitmap.SetDstRect (&rc); AfxGetMainWnd()->SetWindowText (_T("Image Is Resized, Example Is Complete (Class library)")); } else AfxGetMainWnd()->SetWindowText (_T("ERROR Resizing The Image, Example Is Complete (Class library)")); m_ProcessNum = DO_QUIT; break; } m_nCount=0; // Invalidate the entire client region to display the changes. InvalidateRect (NULL); // Kill the timer if ProcessNum is DO_QUIT. if (m_ProcessNum == DO_QUIT) KillTimer (m_nTimer); SetCursor (m_hOrigCursor); CView::OnTimer(nIDEvent); } int CFeature2View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; CFeature2App* pCApp = ( CFeature2App* ) AfxGetApp(); L_INT nRet = m_LeadBitmap.Load((L_TCHAR*)(LPCTSTR)pCApp->m_strFileName); if ( nRet != SUCCESS ) LBase::DisplayError(AfxGetMainWnd()->m_hWnd, nRet); // Reset the counter and start a timer to be used for display delay. m_nCount = 0; m_nTimer = SetTimer ( 1, 1000, NULL); return 0; } void CFeature2View::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 CFeature2View::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; }