//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: LgImageDIB.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgImageDIB.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "Version.h" #include "LgLogger.h" #include "LgImageDIB.h" #include "LgImageJPEG.h" #include "LgLogger.h" #ifndef WIDTHBYTES #define WIDTHBYTES(bits) (((bits + 31) >> 5) << 2) #endif CLgImageDIB::CLgImageDIB() { // reset members m_pBitmapInfoHeader = NULL; m_pBytes = NULL; m_pBitmapFileHeader = NULL; LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgImageDIB::CLgImageDIB" ); } CLgImageDIB::CLgImageDIB( const CLgImageDIB& rDIB ) { // reset members m_pBitmapInfoHeader = NULL; m_pBytes = NULL; m_pBitmapFileHeader = NULL; LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgImageDIB::CLgImageDIB(const CLgImageDIB&)" ); // init bitmap Create( (LPBITMAPINFO)rDIB.m_pBitmapInfoHeader, rDIB.m_pBytes ); } CLgImageDIB::~CLgImageDIB() { // destroy members Destroy(); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgImageDIB::~CLgImageDIB" ); } CLgImageDIB& CLgImageDIB::operator = ( const CLgImageDIB& rDIB ) { // init bitmap Create( (LPBITMAPINFO)rDIB.m_pBitmapInfoHeader, rDIB.m_pBytes ); return *this; } BOOL CLgImageDIB::Create( INT nWidth, INT nHeight, INT nBitCount ) { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); BITMAPINFOHEADER bih; bih.biSize = sizeof(bih); bih.biHeight = nHeight; bih.biWidth = nWidth; bih.biPlanes = 1; bih.biBitCount = nBitCount; bih.biCompression = 0; bih.biSizeImage = nHeight * WIDTHBYTES( nWidth * nBitCount ); bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrImportant = 0; bih.biClrUsed = 0; return Create( &bih, NULL ); } BOOL CLgImageDIB::Create( LPBITMAPINFO pBitmapInfo, PBYTE pBytes ) { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); // check entered values if( !pBitmapInfo ) return FALSE; // calculate palette size INT nPaletteSize = pBitmapInfo->bmiHeader.biBitCount > 8 ? 0 : (1 << pBitmapInfo->bmiHeader.biBitCount); // allocate memory if nessessary if( (!m_pBitmapFileHeader) || m_pBitmapInfoHeader->biBitCount != pBitmapInfo->bmiHeader.biBitCount || m_pBitmapInfoHeader->biSizeImage < pBitmapInfo->bmiHeader.biSizeImage ) { // calculate full size INT nSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + pBitmapInfo->bmiHeader.biSizeImage + nPaletteSize; m_pBitmapFileHeader = (LPBITMAPFILEHEADER)realloc( m_pBitmapFileHeader, sizeof(BYTE) * nSize ); if( !m_pBitmapFileHeader ) { // clear attributes m_pBitmapInfoHeader = NULL; m_pBytes = NULL; return FALSE; } // fill file attributes m_pBitmapFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); m_pBitmapFileHeader->bfReserved1 = 0; m_pBitmapFileHeader->bfReserved2 = 0; m_pBitmapFileHeader->bfSize = nSize; m_pBitmapFileHeader->bfType = MAKEWORD( 'B', 'M' ); // initialize pointers m_pBitmapInfoHeader = (LPBITMAPINFOHEADER)( (LPBYTE)m_pBitmapFileHeader + sizeof(BITMAPFILEHEADER) ); m_pBytes = (LPBYTE)m_pBitmapFileHeader + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nPaletteSize; } // copy dib bits if( pBytes ) memcpy( m_pBytes, pBytes, pBitmapInfo->bmiHeader.biSizeImage ); else memset( m_pBytes, 0, pBitmapInfo->bmiHeader.biSizeImage ); // copy attributes + palette memcpy( m_pBitmapInfoHeader, pBitmapInfo, sizeof(BITMAPINFOHEADER) + nPaletteSize ); return TRUE; } BOOL CLgImageDIB::Create( LPBITMAPINFOHEADER pBitmapInfoHeader, PBYTE pBytes ) { return Create( (LPBITMAPINFO)pBitmapInfoHeader, pBytes ); } BOOL CLgImageDIB::Create( HBITMAP hBitmap, LPBITMAPINFO pBitmapInfo ) { if( !hBitmap ) return FALSE; BOOL bResult = FALSE; if( pBitmapInfo ) { DIBSECTION DibSection; if( !::GetObject( hBitmap, sizeof(DibSection), &DibSection ) ) return FALSE; // get refference dc HDC hDC = ::GetDC( GetDesktopWindow() ); if( !hDC ) return FALSE; // prepare dib section PBYTE pByte = NULL; HBITMAP hSection = ::CreateDIBSection( hDC, pBitmapInfo, DIB_RGB_COLORS, (void**)&pByte, NULL, 0 ); if( hSection ) { HDC hSrcDC = ::CreateCompatibleDC( hDC ); if( hSrcDC ) { HDC hDstDC = ::CreateCompatibleDC( hDC ); if( hDstDC ) { HBITMAP hSrcBitmap = (HBITMAP)::SelectObject( hSrcDC, hBitmap ); HBITMAP hDstBitmap = (HBITMAP)::SelectObject( hDstDC, hSection ); INT32 nStretchMode = ::SetStretchBltMode( hDstDC, COLORONCOLOR ); // stretch image according to incoming parameters ::StretchBlt( hDstDC, 0, 0, pBitmapInfo->bmiHeader.biWidth, pBitmapInfo->bmiHeader.biHeight, hSrcDC, 0, 0, DibSection.dsBm.bmWidth, DibSection.dsBm.bmHeight, SRCCOPY ); ::SetStretchBltMode( hDstDC, nStretchMode ); ::SelectObject( hSrcDC, hSrcBitmap ); ::SelectObject( hDstDC, hDstBitmap ); bResult = Create( pBitmapInfo, pByte ); ::DeleteDC( hDstDC ); } ::DeleteDC( hSrcDC ); } ::DeleteObject( hSection ); } ::ReleaseDC( GetDesktopWindow(), hDC ); } else { BITMAPINFO bmi; memset( &bmi, 0, sizeof(bmi) ); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); HDC hDC = ::GetDC( 0 ); if( !hDC ) return FALSE; if( !::GetDIBits( hDC, hBitmap, 0, 0, NULL, &bmi, DIB_RGB_COLORS) ) { ::ReleaseDC( 0, hDC ); return FALSE; } bmi.bmiHeader.biCompression = 0; bmi.bmiHeader.biBitCount = 24; bmi.bmiHeader.biSizeImage = WIDTHBYTES( bmi.bmiHeader.biWidth * bmi.bmiHeader.biBitCount ) * bmi.bmiHeader.biHeight; LPBYTE pByte = new BYTE[bmi.bmiHeader.biSizeImage]; if( !pByte ) { ::ReleaseDC( 0, hDC ); return FALSE; } if( !::GetDIBits( hDC, hBitmap, 0, bmi.bmiHeader.biHeight, pByte, &bmi, DIB_RGB_COLORS) ) { delete [] pByte; ::ReleaseDC( 0, hDC ); return FALSE; } bResult = Create( (LPBITMAPINFO)&bmi, pByte ); delete[] pByte; ::ReleaseDC( 0, hDC ); } return bResult; } void CLgImageDIB::Destroy() { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); // clear memory if( m_pBitmapFileHeader ) { free( m_pBitmapFileHeader ); m_pBitmapFileHeader = NULL; } // clear attributes m_pBitmapInfoHeader = NULL; m_pBytes = NULL; } BOOL CLgImageDIB::Draw( HDC hDC, PRECT pRect, BOOL bStretch, BOOL bFlipVertical ) { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); if( !m_pBytes ) return FALSE; RECT rect = { 0, 0, m_pBitmapInfoHeader->biWidth, m_pBitmapInfoHeader->biHeight }; if( !pRect ) pRect = ▭ LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CLgImageDIB::Draw", "%p,%p(%d,%d,%d,%d),%d", hDC, pRect, pRect->left, pRect->top, pRect->right, pRect->bottom, bStretch ); INT nWidth = pRect->right - pRect->left; INT nHeight = pRect->bottom - pRect->top; BOOL bDrawResult = FALSE; if( bFlipVertical ) m_pBitmapInfoHeader->biHeight =- m_pBitmapInfoHeader->biHeight; if( bStretch ) { INT nMode = ::SetStretchBltMode( hDC, ( GetVersion() & 0x80000000UL ) ? COLORONCOLOR : HALFTONE ); bDrawResult = ::StretchDIBits( hDC, pRect->left, pRect->top, nWidth, nHeight, 0, 0, m_pBitmapInfoHeader->biWidth, abs(m_pBitmapInfoHeader->biHeight), m_pBytes, (LPBITMAPINFO)m_pBitmapInfoHeader, DIB_RGB_COLORS, SRCCOPY ); ::SetStretchBltMode( hDC, nMode ); } else { POINT vPos = { (pRect->right - m_pBitmapInfoHeader->biWidth) >> 1, (pRect->bottom - abs(m_pBitmapInfoHeader->biHeight)) >> 1 }; bDrawResult = ::SetDIBitsToDevice( hDC, pRect->left + vPos.x, pRect->top + vPos.y, m_pBitmapInfoHeader->biWidth, abs(m_pBitmapInfoHeader->biHeight), 0, 0, 0, abs(m_pBitmapInfoHeader->biHeight), m_pBytes, (LPBITMAPINFO)m_pBitmapInfoHeader, DIB_RGB_COLORS ); } if( bFlipVertical ) m_pBitmapInfoHeader->biHeight =- m_pBitmapInfoHeader->biHeight; if( !bDrawResult ) LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgImageDIB::Draw", "GetLastError=%d, Bytes: %p, Bmi: %p(W:%d,H:%d,BC:%d,SI:%d,C:%d,P:%d,S:%d,CI:%d,CU:%d,PXPM:%d,PXPM:%d)", GetLastError(), m_pBytes, m_pBitmapInfoHeader, m_pBitmapInfoHeader->biWidth, m_pBitmapInfoHeader->biHeight, m_pBitmapInfoHeader->biBitCount, m_pBitmapInfoHeader->biSizeImage, m_pBitmapInfoHeader->biCompression, m_pBitmapInfoHeader->biPlanes, m_pBitmapInfoHeader->biSize, m_pBitmapInfoHeader->biClrImportant, m_pBitmapInfoHeader->biClrUsed, m_pBitmapInfoHeader->biXPelsPerMeter, m_pBitmapInfoHeader->biYPelsPerMeter ); else LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CLgImageDIB::Draw", "Bytes: %p, Bmi: %p(W:%d,H:%d,BC:%d,SI:%d,C:%d,P:%d,S:%d,CI:%d,CU:%d,PXPM:%d,PXPM:%d)", m_pBytes, m_pBitmapInfoHeader, m_pBitmapInfoHeader->biWidth, m_pBitmapInfoHeader->biHeight, m_pBitmapInfoHeader->biBitCount, m_pBitmapInfoHeader->biSizeImage, m_pBitmapInfoHeader->biCompression, m_pBitmapInfoHeader->biPlanes, m_pBitmapInfoHeader->biSize, m_pBitmapInfoHeader->biClrImportant, m_pBitmapInfoHeader->biClrUsed, m_pBitmapInfoHeader->biXPelsPerMeter, m_pBitmapInfoHeader->biYPelsPerMeter ); return TRUE; } BOOL CLgImageDIB::Draw( HDC hDC, PRECT pDstRect, PRECT pSrcRect, BOOL bFlipVertical ) { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); if( !m_pBytes ) return FALSE; BOOL bDrawResult = FALSE; if( bFlipVertical ) { } INT nMode = ::SetStretchBltMode( hDC, COLORONCOLOR ); bDrawResult = ::StretchDIBits( hDC, pDstRect->left, pDstRect->top, pDstRect->right - pDstRect->left, pDstRect->bottom - pDstRect->top, pSrcRect->left, pSrcRect->top, pSrcRect->right - pSrcRect->left, pSrcRect->bottom - pSrcRect->top, m_pBytes, (LPBITMAPINFO)m_pBitmapInfoHeader, DIB_RGB_COLORS, SRCCOPY ); ::SetStretchBltMode( hDC, nMode ); return TRUE; } /* HBITMAP CLgImageDIB::CreateBitmap() { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); if( !m_pBytes ) return FALSE; // get dc of entire screen HDC hDC = ::GetDC( NULL ); if( !hDC ) return NULL; // create dib bitmap HBITMAP hBitmap = ::CreateDIBitmap( hDC, m_pBitmapInfoHeader, CBM_INIT, m_pBytes, (LPBITMAPINFO)m_pBitmapInfoHeader, DIB_RGB_COLORS ); // relase dc ::ReleaseDC( NULL, hDC ); return hBitmap; } */ HBITMAP CLgImageDIB::CreateBitmap( LPBITMAPINFO *ppBitmapInfo, LPBYTE *ppBytes ) { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); if( !m_pBytes ) return FALSE; // get dc of entire screen HDC hDC = ::GetDC( NULL ); if( !hDC ) return NULL; // create dib bitmap HBITMAP hBitmap = ::CreateDIBitmap( hDC, m_pBitmapInfoHeader, CBM_INIT, m_pBytes, (LPBITMAPINFO)m_pBitmapInfoHeader, DIB_RGB_COLORS ); // relase dc ::ReleaseDC( NULL, hDC ); // return format if( ppBitmapInfo ) *ppBitmapInfo = (LPBITMAPINFO)m_pBitmapInfoHeader; if( ppBytes ) *ppBytes = m_pBytes; return hBitmap; } BOOL CLgImageDIB::Save( LPCTSTR pFileName ) { if( !m_pBitmapFileHeader ) return FALSE; HANDLE hFile = ::CreateFile( pFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, 0, NULL ); if( hFile == INVALID_HANDLE_VALUE ) return FALSE; // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); DWORD dwWritten = 0; BOOL bResult = ::WriteFile( hFile, m_pBitmapFileHeader, m_pBitmapFileHeader->bfSize, &dwWritten, NULL ); ::CloseHandle( hFile ); return bResult ? (dwWritten == m_pBitmapFileHeader->bfSize) : FALSE; } BOOL CLgImageDIB::Load( LPCTSTR pFileName, BOOL bConvertToRGB24 ) { if( !pFileName ) return FALSE; if( bConvertToRGB24 ) { HANDLE hFile = ::CreateFile( pFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); if( hFile == INVALID_HANDLE_VALUE ) return FALSE; DWORD dwFileSize = GetFileSize( hFile, NULL ); DWORD dwRead = 0; // read file header BITMAPFILEHEADER FileHeader; ::ReadFile( hFile, &FileHeader, sizeof(FileHeader), &dwRead, NULL ); // allocate bitmap info INT nBitmapInfoSize = FileHeader.bfOffBits - sizeof(FileHeader); LPBITMAPINFO pBitmapInfo = (LPBITMAPINFO)malloc( nBitmapInfoSize ); if( !pBitmapInfo ) { ::CloseHandle( hFile ); return FALSE; } // read bitmap info ::ReadFile( hFile, pBitmapInfo, nBitmapInfoSize, &dwRead, NULL ); // if impossible to perform easy conversion if( pBitmapInfo->bmiHeader.biBitCount != 8 ) { // close file free( pBitmapInfo ); ::CloseHandle( hFile ); // perform convertion by Windows API HBITMAP hBitmap = (HBITMAP)::LoadImage( 0, pFileName, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE ); if( hBitmap ) { BOOL bResult = Create( hBitmap ); ::DeleteObject( hBitmap ); return bResult; } return FALSE; } // prepare new bitmap info size INT nDstLineSize = WIDTHBYTES( pBitmapInfo->bmiHeader.biWidth * 24 ); INT nDstImageSize = nDstLineSize * pBitmapInfo->bmiHeader.biHeight; INT nDstFullSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nDstImageSize; // check sizes if( (!m_pBitmapFileHeader) || m_pBitmapInfoHeader->biBitCount != 24 || m_pBitmapInfoHeader->biSizeImage < nDstImageSize ) { // alloocate additional space m_pBitmapFileHeader = (LPBITMAPFILEHEADER)realloc( m_pBitmapFileHeader, sizeof(BYTE) * nDstFullSize ); if( !m_pBitmapFileHeader ) { // clear attributes m_pBitmapInfoHeader = NULL; m_pBytes = NULL; // close file data free( pBitmapInfo ); ::CloseHandle( hFile ); return FALSE; } // fill file attributes m_pBitmapFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); m_pBitmapFileHeader->bfReserved1 = 0; m_pBitmapFileHeader->bfReserved2 = 0; m_pBitmapFileHeader->bfSize = nDstFullSize; m_pBitmapFileHeader->bfType = MAKEWORD( 'B', 'M' ); // initialize pointers m_pBitmapInfoHeader = (LPBITMAPINFOHEADER)( (LPBYTE)m_pBitmapFileHeader + sizeof(BITMAPFILEHEADER) ); m_pBytes = (LPBYTE)m_pBitmapFileHeader + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); } // copy attributes memcpy( m_pBitmapInfoHeader, pBitmapInfo, sizeof(BITMAPINFOHEADER) ); // adjust attributes m_pBitmapInfoHeader->biBitCount = 24; m_pBitmapInfoHeader->biSizeImage = nDstImageSize; // allocate buffer for source bytes PBYTE pSrcBytes = (PBYTE)malloc( pBitmapInfo->bmiHeader.biSizeImage ); if( !pSrcBytes ) { // close file data free( pBitmapInfo ); ::CloseHandle( hFile ); return FALSE; } // read bitmap data ::ReadFile( hFile, pSrcBytes, pBitmapInfo->bmiHeader.biSizeImage, &dwRead, NULL ); // perform conversion PBYTE pDstLine = m_pBytes; PBYTE pSrcLine = pSrcBytes; INT nSrcLineSize = WIDTHBYTES( pBitmapInfo->bmiHeader.biWidth * 8 ); for( INT y = 0; y < pBitmapInfo->bmiHeader.biHeight; y++ ) { RGBTRIPLE *pTriple = (RGBTRIPLE*)pDstLine; for( INT x = 0; x < pBitmapInfo->bmiHeader.biWidth; x++ ) { pTriple[x] = (RGBTRIPLE&)pBitmapInfo->bmiColors[ pSrcLine[x] ]; } pDstLine += nDstLineSize; pSrcLine += nSrcLineSize; } // close file data free( pBitmapInfo ); free( pSrcBytes ); ::CloseHandle( hFile ); return TRUE; } else { HANDLE hFile = ::CreateFile( pFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); if( hFile == INVALID_HANDLE_VALUE ) return FALSE; DWORD dwFileSize = GetFileSize( hFile, NULL ); // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); m_pBitmapFileHeader = (LPBITMAPFILEHEADER)realloc( m_pBitmapFileHeader, dwFileSize ); if( !m_pBitmapFileHeader ) { m_pBitmapFileHeader = NULL; m_pBitmapInfoHeader = NULL; m_pBytes = NULL; ::CloseHandle( hFile ); return FALSE; } DWORD dwRead = 0; BOOL bResult = ::ReadFile( hFile, m_pBitmapFileHeader, dwFileSize, &dwRead, NULL ); ::CloseHandle( hFile ); if( bResult && ( m_pBitmapFileHeader->bfSize <= dwRead ) ) { m_pBitmapInfoHeader = (LPBITMAPINFOHEADER)( (PBYTE)m_pBitmapFileHeader + sizeof(BITMAPFILEHEADER) ); m_pBytes = (PBYTE)m_pBitmapFileHeader + m_pBitmapFileHeader->bfOffBits; return TRUE; } } return FALSE; } BOOL CLgImageDIB::Create( CLgImageJPEG *pImageJPEG ) { if( !pImageJPEG ) return FALSE; // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); return pImageJPEG->ConvertToDIB( m_pBitmapFileHeader, m_pBitmapInfoHeader, m_pBytes ); } BOOL CLgImageDIB::Create( LPBYTE pJpegData, INT nSize ) { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); return CLgImageJPEG::ConvertToDIB( pJpegData, nSize, m_pBitmapFileHeader, m_pBitmapInfoHeader, m_pBytes ); } BOOL CLgImageDIB::Erase() { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); if( !m_pBitmapInfoHeader || !m_pBytes ) return TRUE; memset( m_pBytes, 0, m_pBitmapInfoHeader->biSizeImage ); return TRUE; }