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.

283 lines
6.7 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: MemoryDC.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of MemoryDC.cpp
  13. //----------------------------------------------------------------------------
  14. // MemDC.cpp: implementation of the CMemDC class.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "..\Version.h"
  19. #include "MemoryDC.h"
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char THIS_FILE[]=__FILE__;
  23. #define new DEBUG_NEW
  24. #endif
  25. CLgMemoryDC::CLgMemoryDC()
  26. {
  27. m_pBitmap = NULL;
  28. m_Size.cx = m_Size.cy = NULL;
  29. // log information
  30. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgMemoryDC::CLgMemoryDC" );
  31. }
  32. CLgMemoryDC::~CLgMemoryDC()
  33. {
  34. if( GetSafeHdc() )
  35. DeleteDC();
  36. // log information
  37. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgMemoryDC::~CLgMemoryDC" );
  38. }
  39. BOOL CLgMemoryDC::Create( HBITMAP hBitmap, BOOL bCopy )
  40. {
  41. DIBSECTION DibSection;
  42. if( !::GetObject( hBitmap, sizeof( DibSection ), &DibSection ) )
  43. return FALSE;
  44. int cx = DibSection.dsBm.bmWidth;
  45. int cy = DibSection.dsBm.bmHeight;
  46. if( m_Size.cx != cx && m_Size.cy != cy )
  47. {
  48. if( GetSafeHdc() )
  49. DeleteDC();
  50. m_Size.cx = cx;
  51. m_Size.cy = cy;
  52. CDC *pDC = CWnd::GetDesktopWindow()->GetDC();
  53. if( pDC )
  54. {
  55. CreateCompatibleDC( pDC );
  56. if( bCopy )
  57. {
  58. m_Bitmap.CreateCompatibleBitmap( pDC, m_Size.cx, m_Size.cy );
  59. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::Create", "created %p", &m_Bitmap );
  60. }
  61. else
  62. {
  63. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::Create", "attached %p handle", hBitmap );
  64. m_Bitmap.Attach( hBitmap );
  65. }
  66. m_pBitmap = SelectObject( &m_Bitmap );
  67. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::Create", "select return %p", m_pBitmap );
  68. CWnd::GetDesktopWindow()->ReleaseDC( pDC );
  69. }
  70. if( bCopy )
  71. {
  72. CDC *pDC = CWnd::GetDesktopWindow()->GetDC();
  73. if( pDC )
  74. {
  75. CDC dc;
  76. dc.CreateCompatibleDC( pDC );
  77. CBitmap bmp;
  78. bmp.Attach( hBitmap );
  79. CBitmap *pOldBmp = dc.SelectObject( &bmp );
  80. BitBlt( 0, 0, m_Size.cx, m_Size.cy, &dc, 0, 0, SRCCOPY );
  81. dc.SelectObject( pOldBmp );
  82. bmp.Detach();
  83. CWnd::GetDesktopWindow()->ReleaseDC( pDC );
  84. }
  85. }
  86. }
  87. return TRUE;
  88. }
  89. BOOL CLgMemoryDC::Create( int cx, int cy, COLORREF color )
  90. {
  91. if( m_Size.cx != cx || m_Size.cy != cy )
  92. {
  93. if( GetSafeHdc() )
  94. DeleteDC();
  95. m_Size.cx = cx;
  96. m_Size.cy = cy;
  97. CDC *pDC = CWnd::GetDesktopWindow()->GetDC();
  98. if( pDC )
  99. {
  100. CreateCompatibleDC( pDC );
  101. m_Bitmap.CreateCompatibleBitmap( pDC, m_Size.cx, m_Size.cy );
  102. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::Create", "created %p", &m_Bitmap );
  103. m_pBitmap = SelectObject( &m_Bitmap );
  104. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::Create", "select return %p", m_pBitmap );
  105. CWnd::GetDesktopWindow()->ReleaseDC( pDC );
  106. }
  107. }
  108. if( GetSafeHdc() )
  109. {
  110. CBrush brush( color );
  111. CRect rect( 0, 0, m_Size.cx, m_Size.cy );
  112. FillRect( &rect, &brush );
  113. }
  114. return TRUE;
  115. }
  116. BOOL CLgMemoryDC::DeleteDC()
  117. {
  118. if( m_pBitmap )
  119. {
  120. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::DeleteDC", "select %p", m_pBitmap );
  121. SelectObject( m_pBitmap );
  122. m_pBitmap = NULL;
  123. }
  124. if( m_Bitmap.GetSafeHandle() )
  125. {
  126. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMemoryDC::DeleteDC", "DeleteObject for %p", &m_Bitmap );
  127. m_Bitmap.DeleteObject();
  128. }
  129. return CDC::DeleteDC();
  130. }
  131. #define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
  132. BOOL CLgMemoryDC::SaveDIB( LPCTSTR pFileName, INT nBitCount )
  133. {
  134. BOOL bResult = FALSE;
  135. INT nBiSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256;
  136. // prepare BitmapInfo structure
  137. LPBITMAPINFO pBI = (LPBITMAPINFO)malloc( nBiSize );
  138. if( !pBI )
  139. return FALSE;
  140. memset( pBI, 0, nBiSize );
  141. pBI->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
  142. // unselect bitmap in dc
  143. CBitmap *pBitmap = SelectObject( m_pBitmap );
  144. // get GDI bitmap handle
  145. HBITMAP hBitmap = (HBITMAP)m_Bitmap.GetSafeHandle();
  146. if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, 0, NULL, pBI, DIB_RGB_COLORS ) )
  147. {
  148. switch( nBitCount )
  149. {
  150. case 1:
  151. case 2:
  152. case 4:
  153. case 8:
  154. case 16:
  155. case 24:
  156. pBI->bmiHeader.biBitCount = nBitCount;
  157. pBI->bmiHeader.biSizeImage = WIDTHBYTES( pBI->bmiHeader.biBitCount * pBI->bmiHeader.biWidth ) * pBI->bmiHeader.biHeight;
  158. break;
  159. case ORIGINAL_BIT_COUNT:
  160. break;
  161. default:
  162. // delete BitmapInfo structure
  163. free( pBI );
  164. // select bitmap into dc
  165. SelectObject( pBitmap );
  166. return FALSE;
  167. }
  168. // nessessary uncompressed bitmap
  169. pBI->bmiHeader.biCompression = 0;
  170. PBYTE pByte = (PBYTE)malloc( pBI->bmiHeader.biSizeImage );
  171. if( pByte )
  172. {
  173. if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, pBI->bmiHeader.biHeight, pByte, pBI, DIB_RGB_COLORS ) )
  174. {
  175. bResult = SaveDIB( pBI, pByte, pFileName );
  176. }
  177. delete pByte;
  178. }
  179. delete pBI;
  180. }
  181. // select bitmap into dc
  182. SelectObject( pBitmap );
  183. return bResult;
  184. }
  185. BOOL CLgMemoryDC::SaveDIB( LPBITMAPINFO pBitmapInfo, LPBYTE pByte, LPCTSTR pFileName )
  186. {
  187. if( !pBitmapInfo || !pByte || !pFileName )
  188. return FALSE;
  189. // open file
  190. HANDLE hFile = ::CreateFile( pFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL );
  191. if( hFile == INVALID_HANDLE_VALUE )
  192. return FALSE;
  193. // generate palette size
  194. int nPaletteSize = sizeof(RGBQUAD) * ( ( pBitmapInfo->bmiHeader.biBitCount > 8 ) ? 0 : ( 1 << pBitmapInfo->bmiHeader.biBitCount ) );
  195. // write file header
  196. BITMAPFILEHEADER bfh;
  197. bfh.bfType = MAKEWORD( 'B', 'M' );
  198. bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nPaletteSize + pBitmapInfo->bmiHeader.biSizeImage;
  199. bfh.bfReserved1 = 0;
  200. bfh.bfReserved2 = 0;
  201. bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nPaletteSize;
  202. DWORD dwWrite;
  203. ::WriteFile( hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwWrite, NULL );
  204. // write bitmap info
  205. ::WriteFile( hFile, pBitmapInfo, sizeof(BITMAPINFOHEADER) + nPaletteSize, &dwWrite, NULL );
  206. // write bits
  207. ::WriteFile( hFile, pByte, pBitmapInfo->bmiHeader.biSizeImage, &dwWrite, NULL );
  208. // close file
  209. ::CloseHandle( hFile );
  210. return TRUE;
  211. }
  212. BOOL CLgMemoryDC::ConvertToDIB( CLgImageDIB &dib )
  213. {
  214. SelectObject( m_pBitmap );
  215. BOOL bResult = dib.Create( (HBITMAP)m_Bitmap.GetSafeHandle() );
  216. SelectObject( &m_Bitmap );
  217. return bResult;
  218. }