/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows(Class Library) - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(Offset)------------------------------------------------------------- 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 (name passed as a command line parameter), 2. display the image, 3. save the image to a file, (name passed as a command line parameter), prepending a false header, 4. fill the bitmap with a color and display it, 5. load the image from the "offset" file, 6. display the new image. Usage: OFFSET --------------------------------------------------------------------------*/ // ofstView.cpp : implementation of the COffsetView class // #include "stdafx.h" #include "offset.h" #include #include "ofsetDoc.h" #include "ofstView.h" #define ORIG_LOAD WM_USER+1 #define SAVE_FILE WM_USER+2 #define OFFSET_LOAD WM_USER+3 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // COffsetView IMPLEMENT_DYNCREATE(COffsetView, CView) BEGIN_MESSAGE_MAP(COffsetView, CView) //{{AFX_MSG_MAP(COffsetView) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // COffsetView construction/destruction COffsetView::COffsetView() { // TODO: add construction code here L_INT nPos; L_INT nLength; CString strFileName; COffsetApp* ptheApp = (COffsetApp*)AfxGetApp(); strFileName = ptheApp->m_strFileName; nPos = strFileName.Find(' '); nLength = strFileName.GetLength(); m_strSrcFile = strFileName.Left(nPos); m_strDstFile = strFileName.Right(nLength - nPos - 1); } COffsetView::~COffsetView() { } BOOL COffsetView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // COffsetView drawing void COffsetView::OnDraw(CDC* pDC) { COffsetDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here HPALETTE hOldPalette = NULL; if ( m_LeadBitmap.GetPalette() ) hOldPalette = SelectPalette (pDC->GetSafeHdc() , m_LeadBitmap.GetPalette(), TRUE); m_LeadBitmap.Paint()->SetDC ( pDC->GetSafeHdc() ); m_LeadBitmap.Paint()->PaintDC(); if (hOldPalette) SelectPalette (pDC->GetSafeHdc() ,hOldPalette, TRUE); } ///////////////////////////////////////////////////////////////////////////// // COffsetView diagnostics #ifdef _DEBUG void COffsetView::AssertValid() const { CView::AssertValid(); } void COffsetView::Dump(CDumpContext& dc) const { CView::Dump(dc); } COffsetDoc* COffsetView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COffsetDoc))); return (COffsetDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // COffsetView message handlers BOOL COffsetView::OnCommand(WPARAM wParam, LPARAM lParam) { // TODO: Add your specialized code here and/or call the base class L_INT nRet; L_INT nFd; switch (wParam) { case ORIG_LOAD: m_LeadBitmap.SetFileName( m_strSrcFile.GetBuffer( m_strSrcFile.GetLength() ) ); m_strSrcFile.ReleaseBuffer(); nRet = m_LeadBitmap.Load(); if (nRet != SUCCESS) { LBase::DisplayError(AfxGetMainWnd()->GetSafeHwnd(),nRet); nRet = FALSE; PostQuitMessage(0); } else { HandlePalette(WM_QUERYNEWPALETTE,m_hWnd); PostMessage(WM_COMMAND ,SAVE_FILE ); } break; case SAVE_FILE: #ifdef UNICODE nFd = _wcreat ( m_strDstFile, _S_IREAD | _S_IWRITE ); #else nFd = _lcreat ( m_strDstFile.GetBuffer( m_strDstFile.GetLength() ), _S_IREAD | _S_IWRITE ); m_strDstFile.ReleaseBuffer(); #endif if (nFd == -1) { MessageBox (_T("Error Opening File!"), _T("Error"), MB_OK); return FALSE; } _lwrite (nFd, "Offset is at 15", 15); nRet = m_LeadBitmap.File()->SaveOffset(nFd, 15, &m_nSizeFile, FILE_LEAD, 24, PQ1 ); _lclose (nFd); if (nRet == SUCCESS) { MessageBox (_T("Image written to file with a false header."), _T("SUCCESS"), MB_OK); m_LeadBitmap .Fill (RGB (200, 0, 255) ); InvalidateRect (NULL, TRUE); UpdateWindow(); PostMessage(WM_COMMAND, OFFSET_LOAD ); } break; case OFFSET_LOAD: #if defined(_M_IX86) || defined(_ALPHA_) #ifdef UNICODE { L_INT nSize = ( m_strDstFile.GetLength() + 1 ) * sizeof ( L_TCHAR ) ; L_CHAR *pszTemp = (L_CHAR*) malloc ( nSize ); memset ( pszTemp, 0, nSize ); if ( NULL != pszTemp ) { wcstombs ( pszTemp, m_strDstFile.GetBuffer( m_strDstFile.GetLength() ), nSize ) ; nFd = _lopen (pszTemp, OF_READ | OF_SHARE_DENY_WRITE); free ( pszTemp ) ; } else { MessageBox (TEXT("Error Opening File!"), TEXT("Error"), MB_OK); return FALSE; } } #else nFd = _lopen (m_strDstFile.GetBuffer( m_strDstFile.GetLength() ) , OF_READ | OF_SHARE_DENY_WRITE); #endif m_strDstFile.ReleaseBuffer(); #else nFd = _lopen (m_strDstFile.GetBuffer( m_strDstFile.GetLength() ) , READ); m_strDstFile.ReleaseBuffer(); #endif if (nFd == -1) { MessageBox (_T("Error Opening File!"), _T("Error"), MB_OK); return FALSE; PostQuitMessage(0); } // Load the bitmap as the file's bits per pixel nRet = m_LeadBitmap.File()->LoadOffset (nFd, 15, m_nSizeFile, 0, ORDER_BGR, LOADFILE_ALLOCATE | LOADFILE_STORE ); _lclose (nFd); if (nRet != SUCCESS) { LBase::DisplayError(AfxGetMainWnd()->GetSafeHwnd(), nRet); AfxGetMainWnd()->SetWindowText (_T("Example Is Complete (Class library)")); } else { AfxGetMainWnd()->SetWindowText (_T("Offset File Loaded, Example Is Complete (Class library)")); HandlePalette(WM_QUERYNEWPALETTE,m_hWnd); } break; } return CView::OnCommand(wParam, lParam); } void COffsetView::OnInitialUpdate() { CView::OnInitialUpdate(); // TODO: Add your specialized code here and/or call the base class PostMessage(WM_COMMAND, ORIG_LOAD); } void COffsetView::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 COffsetView::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); InvalidateRect(NULL,false); UpdateWindow(); return TRUE; } return FALSE; }