//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: MemoryDC.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of MemoryDC.cpp //---------------------------------------------------------------------------- // MemDC.cpp: implementation of the CMemDC class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "..\Version.h" #include "MemoryDC.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif CLgMemoryDC::CLgMemoryDC() { m_pBitmap = NULL; m_Size.cx = m_Size.cy = NULL; // log information LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgMemoryDC::CLgMemoryDC" ); } CLgMemoryDC::~CLgMemoryDC() { if( GetSafeHdc() ) DeleteDC(); // log information LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgMemoryDC::~CLgMemoryDC" ); } BOOL CLgMemoryDC::Create( HBITMAP hBitmap, BOOL bCopy ) { DIBSECTION DibSection; if( !::GetObject( hBitmap, sizeof( DibSection ), &DibSection ) ) return FALSE; int cx = DibSection.dsBm.bmWidth; int cy = DibSection.dsBm.bmHeight; if( m_Size.cx != cx && m_Size.cy != cy ) { if( GetSafeHdc() ) DeleteDC(); m_Size.cx = cx; m_Size.cy = cy; CDC *pDC = CWnd::GetDesktopWindow()->GetDC(); if( pDC ) { CreateCompatibleDC( pDC ); if( bCopy ) { m_Bitmap.CreateCompatibleBitmap( pDC, m_Size.cx, m_Size.cy ); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::Create", "created %p", &m_Bitmap ); } else { LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::Create", "attached %p handle", hBitmap ); m_Bitmap.Attach( hBitmap ); } m_pBitmap = SelectObject( &m_Bitmap ); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::Create", "select return %p", m_pBitmap ); CWnd::GetDesktopWindow()->ReleaseDC( pDC ); } if( bCopy ) { CDC *pDC = CWnd::GetDesktopWindow()->GetDC(); if( pDC ) { CDC dc; dc.CreateCompatibleDC( pDC ); CBitmap bmp; bmp.Attach( hBitmap ); CBitmap *pOldBmp = dc.SelectObject( &bmp ); BitBlt( 0, 0, m_Size.cx, m_Size.cy, &dc, 0, 0, SRCCOPY ); dc.SelectObject( pOldBmp ); bmp.Detach(); CWnd::GetDesktopWindow()->ReleaseDC( pDC ); } } } return TRUE; } BOOL CLgMemoryDC::Create( int cx, int cy, COLORREF color ) { if( m_Size.cx != cx || m_Size.cy != cy ) { if( GetSafeHdc() ) DeleteDC(); m_Size.cx = cx; m_Size.cy = cy; CDC *pDC = CWnd::GetDesktopWindow()->GetDC(); if( pDC ) { CreateCompatibleDC( pDC ); m_Bitmap.CreateCompatibleBitmap( pDC, m_Size.cx, m_Size.cy ); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::Create", "created %p", &m_Bitmap ); m_pBitmap = SelectObject( &m_Bitmap ); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::Create", "select return %p", m_pBitmap ); CWnd::GetDesktopWindow()->ReleaseDC( pDC ); } } if( GetSafeHdc() ) { CBrush brush( color ); CRect rect( 0, 0, m_Size.cx, m_Size.cy ); FillRect( &rect, &brush ); } return TRUE; } BOOL CLgMemoryDC::DeleteDC() { if( m_pBitmap ) { LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::DeleteDC", "select %p", m_pBitmap ); SelectObject( m_pBitmap ); m_pBitmap = NULL; } if( m_Bitmap.GetSafeHandle() ) { LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::DeleteDC", "DeleteObject for %p", &m_Bitmap ); m_Bitmap.DeleteObject(); } return CDC::DeleteDC(); } #define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4) BOOL CLgMemoryDC::SaveDIB( LPCTSTR pFileName, INT nBitCount ) { BOOL bResult = FALSE; INT nBiSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256; // prepare BitmapInfo structure LPBITMAPINFO pBI = (LPBITMAPINFO)malloc( nBiSize ); if( !pBI ) return FALSE; memset( pBI, 0, nBiSize ); pBI->bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); // unselect bitmap in dc CBitmap *pBitmap = SelectObject( m_pBitmap ); // get GDI bitmap handle HBITMAP hBitmap = (HBITMAP)m_Bitmap.GetSafeHandle(); if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, 0, NULL, pBI, DIB_RGB_COLORS ) ) { switch( nBitCount ) { case 1: case 2: case 4: case 8: case 16: case 24: pBI->bmiHeader.biBitCount = nBitCount; pBI->bmiHeader.biSizeImage = WIDTHBYTES( pBI->bmiHeader.biBitCount * pBI->bmiHeader.biWidth ) * pBI->bmiHeader.biHeight; break; case ORIGINAL_BIT_COUNT: break; default: // delete BitmapInfo structure free( pBI ); // select bitmap into dc SelectObject( pBitmap ); return FALSE; } // nessessary uncompressed bitmap pBI->bmiHeader.biCompression = 0; PBYTE pByte = (PBYTE)malloc( pBI->bmiHeader.biSizeImage ); if( pByte ) { if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, pBI->bmiHeader.biHeight, pByte, pBI, DIB_RGB_COLORS ) ) { bResult = SaveDIB( pBI, pByte, pFileName ); } delete pByte; } delete pBI; } // select bitmap into dc SelectObject( pBitmap ); return bResult; } BOOL CLgMemoryDC::SaveDIB( LPBITMAPINFO pBitmapInfo, LPBYTE pByte, LPCTSTR pFileName ) { if( !pBitmapInfo || !pByte || !pFileName ) return FALSE; // open file HANDLE hFile = ::CreateFile( pFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL ); if( hFile == INVALID_HANDLE_VALUE ) return FALSE; // generate palette size int nPaletteSize = sizeof(RGBQUAD) * ( ( pBitmapInfo->bmiHeader.biBitCount > 8 ) ? 0 : ( 1 << pBitmapInfo->bmiHeader.biBitCount ) ); // write file header BITMAPFILEHEADER bfh; bfh.bfType = MAKEWORD( 'B', 'M' ); bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nPaletteSize + pBitmapInfo->bmiHeader.biSizeImage; bfh.bfReserved1 = 0; bfh.bfReserved2 = 0; bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nPaletteSize; DWORD dwWrite; ::WriteFile( hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwWrite, NULL ); // write bitmap info ::WriteFile( hFile, pBitmapInfo, sizeof(BITMAPINFOHEADER) + nPaletteSize, &dwWrite, NULL ); // write bits ::WriteFile( hFile, pByte, pBitmapInfo->bmiHeader.biSizeImage, &dwWrite, NULL ); // close file ::CloseHandle( hFile ); return TRUE; } BOOL CLgMemoryDC::ConvertToDIB( CLgImageDIB &dib ) { SelectObject( m_pBitmap ); BOOL bResult = dib.Create( (HBITMAP)m_Bitmap.GetSafeHandle() ); SelectObject( &m_Bitmap ); return bResult; }