Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

358 lines
8.8 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: LgMemoryDC.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of LgMemoryDC.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "LgMemoryDC.h"
#include "..\LgLib\LgImageJPEG.h"
#include "..\LgLib\Version.h"
CLgMemoryDC::CLgMemoryDC()
{
m_Size.cx = 0;
m_Size.cy = 0;
m_hDC = NULL;
m_hOldBitmap= 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 ) )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMemoryDC::Create"), _T("Can't get bitmap dimensions") );
return FALSE;
}
DeleteDC();
m_Size.cx = DibSection.dsBm.bmWidth;
m_Size.cy = DibSection.dsBm.bmHeight;
HWND hWnd = ::GetDesktopWindow();
HDC hDC = ::GetDC( hWnd );
if( hDC )
{
m_hDC = ::CreateCompatibleDC( hDC );
m_hBitmap = bCopy ? ::CreateCompatibleBitmap( hDC, m_Size.cx, m_Size.cy ) : hBitmap;
m_hOldBitmap = (HBITMAP)::SelectObject( m_hDC, m_hBitmap );
LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgMemoryDC::Create"), _T("DC %p, BITMAP %p, OLDBITMAP %p"), m_hDC, m_hBitmap, m_hOldBitmap );
::ReleaseDC( hWnd, hDC );
}
if( !bCopy )
return TRUE;
if( !m_hDC || !m_hBitmap )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMemoryDC::Create"), _T("Can't copy: DC %p, BITMAP %p, OLDBITMAP %p"), m_hDC, m_hBitmap, m_hOldBitmap );
return FALSE;
}
hDC = ::GetDC( hWnd );
if( hDC )
{
HDC hTempDC = ::CreateCompatibleDC( hDC );
if( hTempDC )
{
HBITMAP hOldTempBitmap = (HBITMAP )::SelectObject( hTempDC, hBitmap );
::BitBlt( m_hDC, 0, 0, m_Size.cx, m_Size.cy, hTempDC, 0, 0, SRCCOPY );
::SelectObject( hTempDC, hOldTempBitmap );
::DeleteDC( hTempDC );
}
::ReleaseDC( hWnd, hDC );
}
return TRUE;
}
BOOL CLgMemoryDC::Create( int cx, int cy, COLORREF color )
{
if( m_Size.cx != cx || m_Size.cy != cy )
{
DeleteDC();
m_Size.cx = cx;
m_Size.cy = cy;
HWND hWnd = ::GetDesktopWindow();
HDC hDC = ::GetDC( hWnd );
if( hDC )
{
m_hDC = ::CreateCompatibleDC( hDC );
m_hBitmap = ::CreateCompatibleBitmap( hDC, m_Size.cx, m_Size.cy );
m_hOldBitmap = (HBITMAP)::SelectObject( m_hDC, m_hBitmap );
::ReleaseDC( hWnd, hDC );
}
}
if( m_hDC && m_hBitmap )
{
HBRUSH hBrush = ::CreateSolidBrush( color );
if( hBrush )
{
RECT rect = { 0, 0, m_Size.cx, m_Size.cy };
::FillRect( m_hDC, &rect, hBrush );
::DeleteObject( hBrush );
}
}
return TRUE;
}
BOOL CLgMemoryDC::DeleteDC()
{
if( m_hOldBitmap )
{
::SelectObject( m_hDC, m_hOldBitmap );
m_hOldBitmap = NULL;
}
if( m_hBitmap )
{
::DeleteObject( m_hBitmap );
m_hBitmap = NULL;
}
if( m_hDC )
{
::DeleteDC( m_hDC );
m_hDC = NULL;
}
return TRUE;
}
//#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( sizeof(BYTE) * nBiSize );
if( !pBI )
return FALSE;
memset( pBI, 0, nBiSize );
pBI->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
// unselect bitmap in dc (actually it is m_hBitmap)
HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hDC, m_hOldBitmap );
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
delete [] (PBYTE)pBI;
// select bitmap into dc
SelectObject( m_hDC, hBitmap );
return FALSE;
}
// nessessary uncompressed bitmap
pBI->bmiHeader.biCompression = 0;
PBYTE pByte = (PBYTE)malloc( sizeof(BYTE) * pBI->bmiHeader.biSizeImage );
if( pByte )
{
if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, pBI->bmiHeader.biHeight, pByte, pBI, DIB_RGB_COLORS ) )
{
bResult = SaveDIB( pBI, pByte, pFileName );
}
free( pByte );
}
free( pBI );
}
// select bitmap into dc
::SelectObject( m_hDC, hBitmap );
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::SaveJPEG( LPCTSTR pFileName, INT nQuality )
{
BOOL bResult = FALSE;
INT nBiSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256;
// prepare BitmapInfo structure
LPBITMAPINFO pBI = (LPBITMAPINFO)malloc( sizeof(BYTE) * nBiSize );
if( !pBI )
return FALSE;
memset( pBI, 0, nBiSize );
pBI->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
// unselect bitmap in dc (actually it is m_hBitmap)
HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hDC, m_hOldBitmap );
if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, 0, NULL, pBI, DIB_RGB_COLORS ) )
{
// nessessary uncompressed bitmap
pBI->bmiHeader.biCompression = 0;
pBI->bmiHeader.biBitCount = 24;
pBI->bmiHeader.biSizeImage = pBI->bmiHeader.biHeight * WIDTHBYTES( pBI->bmiHeader.biWidth * 24 );
PBYTE pByte = (PBYTE)malloc( sizeof(BYTE) * pBI->bmiHeader.biSizeImage );
if( pByte )
{
if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, pBI->bmiHeader.biHeight, pByte, pBI, DIB_RGB_COLORS ) )
{
CLgImageJPEG jpeg;
bResult = jpeg.Create( pBI, pByte, nQuality ) && jpeg.Save( pFileName );
}
free( pByte );
}
free( pBI );
}
// select bitmap into dc
::SelectObject( m_hDC, hBitmap );
return bResult;
}
BOOL CLgMemoryDC::GetDIB( CLgImageDIB *pDIB )
{
if( !m_hDC || !m_hOldBitmap )
return FALSE;
BOOL bResult = FALSE;
HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hDC, m_hOldBitmap );
if( hBitmap )
{
bResult = pDIB->Create( hBitmap );
::SelectObject( m_hDC, hBitmap );
}
return bResult;
}
HBITMAP CLgMemoryDC::CopyImage()
{
HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hDC, m_hOldBitmap );
HBITMAP hResult = (HBITMAP)::CopyImage( m_hBitmap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
::SelectObject( m_hDC, hBitmap );
return hResult;
}
BOOL CLgMemoryDC::Ellipse( int nLeftRect, int nTopRect, int nRightRect, int nBottomRect )
{
return ::Ellipse( m_hDC, nLeftRect, nTopRect, nRightRect, nBottomRect );
}
BOOL CLgMemoryDC::LineTo( int x, int y )
{
return ::LineTo( m_hDC, x, y );
}
BOOL CLgMemoryDC::MoveTo( int x, int y )
{
return ::MoveToEx( m_hDC, x, y, NULL );
}
int CLgMemoryDC::DrawText( LPCTSTR lpString, int nCount, LPRECT lpRect, UINT uFormat )
{
return ::DrawText( m_hDC, lpString, nCount, lpRect, uFormat );
}
BOOL CLgMemoryDC::FillSolidRect( LPCRECT lpRect, COLORREF clr )
{
HBRUSH hBrush = ::CreateSolidBrush( clr );
if( !hBrush )
return FALSE;
BOOL bResult = ::FillRect( m_hDC, lpRect, hBrush );
::DeleteObject( hBrush );
return bResult;
}
BOOL CLgMemoryDC::DrawIcon( int X, int Y, HICON hIcon )
{
return ::DrawIcon( m_hDC, X, Y, hIcon );
}