/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows(Class Library) - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(Memory)------------------------------------------------------------- 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 file (name passed as a command line parameter) into allocated memory, 2. load the image from memory to a bitmap with LMemoryFile::LoadMemory(), 3. display the loaded image in a window. Usage: MEMORY --------------------------------------------------------------------------*/ // LTLMView.cpp : implementation of the CLTLodMemView class // #include "stdafx.h" #include "LTLodMem.h" #include "LTLMDoc.h" #include "LTLMView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CLTLodMemView IMPLEMENT_DYNCREATE(CLTLodMemView, CView) BEGIN_MESSAGE_MAP(CLTLodMemView, CView) //{{AFX_MSG_MAP(CLTLodMemView) ON_WM_SIZE() ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CLTLodMemView construction/destruction CLTLodMemView::CLTLodMemView() { } CLTLodMemView::~CLTLodMemView() { } BOOL CLTLodMemView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CLTLodMemView drawing void CLTLodMemView::OnDraw(CDC* pDC) { HPALETTE oldPalette = NULL; CLTLodMemDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // Parepare to paint m_LBitmapBase.Paint()->SetDC(pDC->m_hDC); if(m_LBitmapBase.GetPalette()) { oldPalette = SelectPalette(pDC->m_hDC, m_LBitmapBase.GetPalette(), TRUE); RealizePalette(pDC->m_hDC); } // Paint the bitmap m_LBitmapBase.Paint()->PaintDC(); if(oldPalette) SelectPalette(pDC->m_hDC, oldPalette, TRUE); } ///////////////////////////////////////////////////////////////////////////// // CLTLodMemView diagnostics #ifdef _DEBUG void CLTLodMemView::AssertValid() const { CView::AssertValid(); } void CLTLodMemView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CLTLodMemDoc* CLTLodMemView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLTLodMemDoc))); return (CLTLodMemDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CLTLodMemView message handlers void CLTLodMemView::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); RECT rcClient; GetClientRect(&rcClient); m_LBitmapBase.SetDstRect(&rcClient); } int CLTLodMemView::OnCreate(LPCREATESTRUCT lpCreateStruct) { CFile ImageFile; CLTLodMemApp* pTheApp = (CLTLodMemApp* )AfxGetApp(); L_INT nRetCode; L_TCHAR szErrMsg[256] = _T(""); if (CView::OnCreate(lpCreateStruct) == -1) return -1; if(pTheApp->m_strCmdLine == "\0") { LBase::DisplayError(GetSafeHwnd(), _T("No file to be opened.")); return FALSE; } // Open file with read only mode if(!ImageFile.Open(pTheApp->m_strCmdLine, CFile::modeRead | CFile::shareDenyNone)) { wsprintf(szErrMsg, _T("Can't open file: %s"), pTheApp->m_strCmdLine); LBase::DisplayError(GetSafeHwnd(), szErrMsg); return -1; } // Get the file size to Allocate the buffer L_INT32 lFileSize = ImageFile.GetLength(); if((nRetCode=m_LeadBuffer.Reallocate(lFileSize)) != SUCCESS) { ImageFile.Close(); LBase::DisplayError(GetSafeHwnd(),nRetCode); return -1; } L_INT32 lSizeToRead = 0; L_INT32 lActualRead = 0; // The actual size of read chunk L_UCHAR *pMoveBuffer = NULL; if((pMoveBuffer = (L_UCHAR *)m_LeadBuffer.Lock()) == NULL) { LBase::DisplayErrorFromList(GetSafeHwnd()); return -1; } // Read the file not as one chunk but breaked into 32k while(lSizeToRead < lFileSize) { TRY { lActualRead = ImageFile.Read(pMoveBuffer, min(lFileSize - lSizeToRead, 32000)); lSizeToRead += lActualRead; pMoveBuffer += lActualRead; // Advance the pointer buffer } CATCH(CFileException, e) { ImageFile.Close(); wsprintf(szErrMsg, _T("Error while reading file: %s"), pTheApp->m_strCmdLine); LBase::DisplayError(GetSafeHwnd(), szErrMsg); } END_CATCH } m_LeadBuffer.Unlock(); ImageFile.Close(); FILEINFO fi; memset(&fi, 0, sizeof(FILEINFO)); // (1) Get image info. from buffer // (2) Load the image bitmap with info. from step (1) // If failed display error message fi.uStructSize = sizeof(FILEINFO); nRetCode = m_LBitmapBase.MemoryFile()->GetInfo(m_LeadBuffer, &fi, sizeof(FILEINFO)); if(nRetCode == SUCCESS) { /* Init. the bitmap to the image width, height, and bits per pixel. */ m_LBitmapBase.Initialize(fi.Width,fi.Height,fi.BitsPerPixel); LOADFILEOPTION LoadFileOption; LBaseFile::GetDefaultLoadFileOption(&LoadFileOption, sizeof(LOADFILEOPTION)); LoadFileOption.Flags |= ELO_ROTATED; /* Load the bitmap, using the image bits per pixel. */ nRetCode = m_LBitmapBase.MemoryFile()->LoadMemory(m_LeadBuffer,fi.BitsPerPixel,ORDER_BGR, LOADFILE_ALLOCATE|LOADFILE_STORE,&LoadFileOption); } if(nRetCode != SUCCESS) { LBase::DisplayErrorFromList(GetSafeHwnd()); return -1; } return 0; } void CLTLodMemView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) { if(bActivate && pActivateView == this) HandlePalette(WM_QUERYNEWPALETTE,m_hWnd); CView::OnActivateView(bActivate, pActivateView, pDeactiveView); } L_BOOL CLTLodMemView::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_LBitmapBase.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; }