/*[]=====================================================================[]*/ /*[] 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. colorres the image to 8 bits per pixel, but only the best 50 colors, in a new bitmap handle, and re-display the image, 4. flip and re-display the image, 5. lighten and re-display the image, 6. resize and re-display the image, 7. rotate and re-display the image. Usage: FEATURE1 --------------------------------------------------------------------------*/ // Ftr1View.cpp : implementation of the CFeature1View class // #include "stdafx.h" #include "Feature1.h" #include "Fetr1Doc.h" #include "Ftr1View.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CFeature1View IMPLEMENT_DYNCREATE(CFeature1View, CView) BEGIN_MESSAGE_MAP(CFeature1View, CView) //{{AFX_MSG_MAP(CFeature1View) ON_WM_TIMER() ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CFeature1View construction/destruction CFeature1View::CFeature1View() { // TODO: add construction code here m_bFirst = TRUE; m_bResetCount = TRUE; m_ProcessNum = DO_COLORRES; } CFeature1View::~CFeature1View() { } ///////////////////////////////////////////////////////////////////////////// // CFeature1View drawing void CFeature1View::OnDraw(CDC* pDC) { CFeature1Doc* 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; return; } ///////////////////////////////////////////////////////////////////////////// // CFeature1View diagnostics #ifdef _DEBUG void CFeature1View::AssertValid() const { CView::AssertValid(); } void CFeature1View::Dump(CDumpContext& dc) const { CView::Dump(dc); } CFeature1Doc* CFeature1View::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFeature1Doc))); return (CFeature1Doc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CFeature1View message handlers void CFeature1View::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) { hOrigCursor = SetCursor (LoadCursor (NULL, IDC_SIZENS)); return; } else if (m_nCount == 2) { SetCursor (LoadCursor (NULL, IDC_SIZENESW)); return; } SetCursor (LoadCursor (NULL, IDC_WAIT)); /* Let OnDraw reset the counter. */ m_bResetCount = TRUE; switch (m_ProcessNum) { case DO_COLORRES: /* Make NewBitmap contain the best 50 colors in an 8-bit bitmap. */ AfxGetMainWnd()->SetWindowText (_T("Optimizing Image To 50 Colors With Burkes Dithering...")); nRet = m_LeadBitmap.ColorRes (8, CRF_OPTIMIZEDPALETTE | CRF_BURKESDITHERING); if (nRet == SUCCESS) { AfxGetMainWnd()->SetWindowText (_T("Image Is Optimized")); HandlePalette(WM_QUERYNEWPALETTE, m_hWnd); } else AfxGetMainWnd()->SetWindowText (_T("ERROR Optimizing The Image")); m_ProcessNum = DO_FLIP; // Next operation is Flip. break; case DO_FLIP: // Flip the image top to bottom. AfxGetMainWnd()->SetWindowText (_T("Flipping Image...")); nRet = m_LeadBitmap.Flip (); if (nRet == SUCCESS) AfxGetMainWnd()->SetWindowText (_T("Image Is Flipped")); else AfxGetMainWnd()->SetWindowText (_T("ERROR Flipping The Image")); m_ProcessNum = DO_INTENSITY; // Next operation is Lighten. * break; case DO_INTENSITY: // Lighten the bitmap. AfxGetMainWnd()->SetWindowText (_T("Lightening Image...")); nRet = m_LeadBitmap.ChangeIntensity (100); if (nRet == SUCCESS) { AfxGetMainWnd()->SetWindowText (_T("Image Is Lightened")); HandlePalette(WM_QUERYNEWPALETTE, m_hWnd); } else AfxGetMainWnd()->SetWindowText (_T("ERROR Lightening The Image")); m_ProcessNum = DO_RESIZE; // Next operation is Resize. break; case DO_RESIZE: AfxGetMainWnd()->SetWindowText (_T("Resizing Image...")); 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")); } else AfxGetMainWnd()->SetWindowText (_T("ERROR Resizing The Image")); m_ProcessNum = DO_ROTATE; // Next operation is Rotate. break; case DO_ROTATE: // Rotate the image 33 degrees clockwise, with resizing. AfxGetMainWnd()->SetWindowText (_T("Rotating Image...")); nRet = m_LeadBitmap.Rotate (3300); 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 Rotated, Example Is Complete (Class library)")); } else AfxGetMainWnd()->SetWindowText (_T("ERROR Rotating 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 m_ProcessNum is DO_QUIT. if (m_ProcessNum == DO_QUIT) KillTimer (m_nTimer); SetCursor (hOrigCursor); CView::OnTimer(nIDEvent); } int CFeature1View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here CFeature1App* pCApp = (CFeature1App*)AfxGetApp(); L_INT nRet = m_LeadBitmap.Load((L_TCHAR*)(LPCTSTR)pCApp->m_strFileName); if ( nRet != SUCCESS ) LBase::DisplayError(AfxGetMainWnd()->m_hWnd, nRet); m_nCount = 0; m_nTimer = SetTimer ( 1, 1000, NULL); return 0; } void CFeature1View::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 CFeature1View::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; }