/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows(Class Library) - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(CompCB)--------------------------------------------------------------- We have made the assumption that the user has the knowledge of Object Oriented programing of C under Windows. This example will: 1. load the image from a file, whose name is sent through the command line, to a bitmap as 24 bits per pixel (LMemoryFile::CompressBuffer() requires data to be 24 bit color or 8 bit grayscale), 2. display the image, 3. compress the bitmap to file using the call back feature of LEADTOOLS, 4. clear the bitmap, 5. load the compressed image, 6. display the newly loaded image. Usage: COMPCB --------------------------------------------------------------------------*/ // CpCBView.cpp : implementation of the CCompCBView class // #include "stdafx.h" #include "CompCB.h" #include #include "UMemFile.h" #include "CmpCBDoc.h" #include "CpCBView.h" #include "CompCB.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define USER_COMPRESS 1 #define USER_RELOAD 2 #define USER_NONE 3 ///////////////////////////////////////////////////////////////////////////// // CCompCBView IMPLEMENT_DYNCREATE(CCompCBView, CView) BEGIN_MESSAGE_MAP(CCompCBView, CView) //{{AFX_MSG_MAP(CCompCBView) ON_WM_CREATE() ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCompCBView construction/destruction CCompCBView::CCompCBView() { // TODO: add construction code here CCompCBApp* ptheApp = (CCompCBApp*)AfxGetApp(); m_strSrcFile = ptheApp->m_strSrcFile; m_strDstFile = ptheApp->m_strDstFile; } CCompCBView::~CCompCBView() { } ///////////////////////////////////////////////////////////////////////////// // CCompCBView drawing void CCompCBView::OnDraw(CDC* pDC) { CCompCBDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here HDC hdc; HPALETTE hOldPal = NULL; hdc = pDC->GetSafeHdc(); if (m_LeadBitmap.GetPalette() ) hOldPal = SelectPalette (hdc, m_LeadBitmap.GetPalette(), TRUE); m_LeadBitmap.Paint()->SetDC( hdc ); m_LeadBitmap.Paint()->PaintDC (); if (hOldPal ) SelectPalette (hdc, hOldPal, TRUE); m_bFirst = FALSE; } ///////////////////////////////////////////////////////////////////////////// // CCompCBView diagnostics #ifdef _DEBUG void CCompCBView::AssertValid() const { CView::AssertValid(); } void CCompCBView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CCompCBDoc* CCompCBView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCompCBDoc))); return (CCompCBDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CCompCBView message handlers int CCompCBView::OnCreate(LPCREATESTRUCT lpCreateStruct) { // CCompCBApp* CApplication; if (CView::OnCreate(lpCreateStruct) == -1) return -1; m_bFirst = TRUE; m_nOperation = WM_USER + USER_COMPRESS; m_LeadBitmap.SetFileName((L_TCHAR*)(LPCTSTR)m_strSrcFile); L_INT nRet = m_LeadBitmap.Load(); if(nRet!=SUCCESS) { m_LeadBitmap.DisplayErrorFromList(NULL); return -1; } if(m_LeadBitmap.GetViewPerspective() != TOP_LEFT) m_LeadBitmap.ChangeViewPerspective(TOP_LEFT); m_nBytesPerLine =(L_INT32) ((L_UINT) m_LeadBitmap.GetWidth() ) * (m_LeadBitmap.GetBitsPerPixel() / 8); m_nTimer = SetTimer (1, 1000, 0); return 0; } void CCompCBView::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if (m_bFirst) return ; static HCURSOR hOrigCursor=NULL; m_nCount++; // Increment the Counter. // Has the image been displayed about 3 seconds? (this is reset in the // function Window_OnPaint) if (m_nCount == 1) { hOrigCursor = SetCursor (LoadCursor (NULL, IDC_SIZENS)); return; } else if (m_nCount == 2) { SetCursor (LoadCursor (NULL, IDC_SIZENESW)); return; } else if (m_nCount == 3) { // Kill the timer if m_nOperation is USER_NONE. if (m_nOperation == WM_USER + USER_RELOAD) KillTimer ( m_nTimer); SetCursor (hOrigCursor); // m_nOperation is set in Window_OnCreate and Window_OnCommand. PostMessage(WM_COMMAND, m_nOperation ); } m_nCount = 0; CView::OnTimer(nIDEvent); } BOOL CCompCBView::OnCommand(WPARAM wParam, LPARAM lParam) { // TODO: Add your specialized code here and/or call the base class L_INT i, j, nRet; HANDLE hFileOpen; HCURSOR hCursor; LBuffer LeadInBuffer; LBuffer LeadRowBuffer; L_UCHAR *lpInBuffer; L_UCHAR *lpRowBuffer; CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->GetMainWnd(); switch (wParam) { case (WM_USER + USER_COMPRESS): // Set the operation to nothing. If something goes wrong, we don't // want to enter this loop again! m_nOperation = WM_USER + USER_NONE; AfxGetMainWnd()->SetWindowText ( _T("Compressing Image...")); hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); // Create the file to store the compressed image hFileOpen = CreateFile ( m_strDstFile.GetBuffer( m_strDstFile.GetLength() ), GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); m_strDstFile.ReleaseBuffer(); if(hFileOpen== NULL || hFileOpen == INVALID_HANDLE_VALUE) { // Failure creating the file. AfxMessageBox ( _T("Error opening the output file!")); AfxGetMainWnd()->SetWindowText ( _T("Error Opening Output, Example Is Complete (Class library)")); return FALSE; } LeadInBuffer.Reallocate((L_UINT32) NLINES * m_nBytesPerLine); lpInBuffer = (L_UCHAR *) LeadInBuffer.Lock(); LeadRowBuffer.Reallocate(m_nBytesPerLine); lpRowBuffer = (L_UCHAR *) LeadRowBuffer.Lock(); m_LeadMemFile.m_hFD = hFileOpen; m_LeadMemFile.SetBitmap(&m_LeadBitmap); nRet = m_LeadMemFile.EnableCallBack(TRUE); // Initialize the compression engine with all needed parameters. nRet = m_LeadMemFile.StartCompressBuffer ((L_UINT32) NLINES * m_nBytesPerLine, BUFFER_SIZE, LEAD, PQ1);//-1 if(nRet != SUCCESS) { CloseHandle (hFileOpen); LBase::DisplayError(AfxGetMainWnd()->GetSafeHwnd(),nRet); PostQuitMessage(0); return FALSE; } for (i = 0; i < m_LeadBitmap.GetHeight();) // i is incremented at the end! { L_UCHAR * lpDataBuffer = lpInBuffer; // Compression of the NLINE line chunk starts here. for (j = 0; (i + j) < m_LeadBitmap.GetHeight() && j < NLINES; j++) { // Gets the one line at time nRet = m_LeadBitmap.GetRow(&LeadRowBuffer, i + j) ; L_INT32 BytesPerLine = (L_INT32) m_LeadBitmap.GetBytesPerLine(); if ( nRet!= BytesPerLine) { CloseHandle (hFileOpen); return FALSE; } memmove(lpDataBuffer,lpRowBuffer,m_nBytesPerLine); // Move the pointer to next line. lpDataBuffer+=m_nBytesPerLine; } // This is the main function that will do the actual Compression. if ( (nRet = m_LeadMemFile.CompressBuffer (&LeadInBuffer)) != SUCCESS) { CloseHandle (hFileOpen); LBase::DisplayErrorList(m_hWnd); return FALSE; } // Do the next NLINE line chunk. i += NLINES; } // Compression is done. Reset The Compression Engine. m_LeadMemFile.EndCompressBuffer (); LeadRowBuffer.Unlock(); LeadInBuffer.Unlock(); CloseHandle (hFileOpen); AfxGetMainWnd()->SetWindowText ( _T("Image Compressed - Clearing Bitmap...")); // Clear the bitmap. m_LeadBitmap.Clear (); HandlePalette(WM_QUERYNEWPALETTE, m_hWnd); // Set m_nOperation to reload the image. m_nOperation = WM_USER + USER_RELOAD; SetCursor (hCursor); AfxGetMainWnd()->SetWindowText ( _T("Image Compressed - Bitmap Cleared")); m_nCount = 0; break; case (WM_USER + USER_RELOAD): // Set the operation to nothing. If something goes wrong, we don't // want to enter this loop again! m_nOperation = WM_USER + USER_NONE; AfxGetMainWnd()->SetWindowText (_T("Loading Compressed Image...")); hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); // Load the image to the bitmap. nRet = m_LeadBitmap.Load(m_strDstFile.GetBuffer( m_strDstFile.GetLength() ), 24, ORDER_BGR); m_strDstFile.ReleaseBuffer(); SetCursor (hCursor); if (nRet != SUCCESS) { LBase::DisplayError(AfxGetMainWnd()->GetSafeHwnd(),nRet); // Failure in load, so kill the application. PostQuitMessage(0); } else AfxGetMainWnd()->SetWindowText (_T("Compressed Image Loaded, Example Is Complete (Class library)")); m_nCount = 0; HandlePalette(WM_QUERYNEWPALETTE, m_hWnd); break; } return CView::OnCommand(wParam, lParam); } void CCompCBView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) { CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->GetMainWnd(); // 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 CCompCBView::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; }