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.

620 lines
18 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: LgImageDIB.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgImageDIB.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "Version.h"
  16. #include "LgLogger.h"
  17. #include "LgImageDIB.h"
  18. #include "LgImageJPEG.h"
  19. #include "LgLogger.h"
  20. #ifndef WIDTHBYTES
  21. #define WIDTHBYTES(bits) (((bits + 31) >> 5) << 2)
  22. #endif
  23. CLgImageDIB::CLgImageDIB()
  24. {
  25. // reset members
  26. m_pBitmapInfoHeader = NULL;
  27. m_pBytes = NULL;
  28. m_pBitmapFileHeader = NULL;
  29. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgImageDIB::CLgImageDIB" );
  30. }
  31. CLgImageDIB::CLgImageDIB( const CLgImageDIB& rDIB )
  32. {
  33. // reset members
  34. m_pBitmapInfoHeader = NULL;
  35. m_pBytes = NULL;
  36. m_pBitmapFileHeader = NULL;
  37. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgImageDIB::CLgImageDIB(const CLgImageDIB&)" );
  38. // init bitmap
  39. Create( (LPBITMAPINFO)rDIB.m_pBitmapInfoHeader, rDIB.m_pBytes );
  40. }
  41. CLgImageDIB::~CLgImageDIB()
  42. {
  43. // destroy members
  44. Destroy();
  45. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgImageDIB::~CLgImageDIB" );
  46. }
  47. CLgImageDIB& CLgImageDIB::operator = ( const CLgImageDIB& rDIB )
  48. {
  49. // init bitmap
  50. Create( (LPBITMAPINFO)rDIB.m_pBitmapInfoHeader, rDIB.m_pBytes );
  51. return *this;
  52. }
  53. BOOL CLgImageDIB::Create( INT nWidth, INT nHeight, INT nBitCount )
  54. {
  55. // protect data from simultaniosly access
  56. CLgLocker Locker( &m_CriticalSection );
  57. BITMAPINFOHEADER bih;
  58. bih.biSize = sizeof(bih);
  59. bih.biHeight = nHeight;
  60. bih.biWidth = nWidth;
  61. bih.biPlanes = 1;
  62. bih.biBitCount = nBitCount;
  63. bih.biCompression = 0;
  64. bih.biSizeImage = nHeight * WIDTHBYTES( nWidth * nBitCount );
  65. bih.biXPelsPerMeter = 0;
  66. bih.biYPelsPerMeter = 0;
  67. bih.biClrImportant = 0;
  68. bih.biClrUsed = 0;
  69. return Create( &bih, NULL );
  70. }
  71. BOOL CLgImageDIB::Create( LPBITMAPINFO pBitmapInfo, PBYTE pBytes )
  72. {
  73. // protect data from simultaniosly access
  74. CLgLocker Locker( &m_CriticalSection );
  75. // check entered values
  76. if( !pBitmapInfo )
  77. return FALSE;
  78. // calculate palette size
  79. INT nPaletteSize = pBitmapInfo->bmiHeader.biBitCount > 8 ? 0 : (1 << pBitmapInfo->bmiHeader.biBitCount);
  80. // allocate memory if nessessary
  81. if( (!m_pBitmapFileHeader) ||
  82. m_pBitmapInfoHeader->biBitCount != pBitmapInfo->bmiHeader.biBitCount ||
  83. m_pBitmapInfoHeader->biSizeImage < pBitmapInfo->bmiHeader.biSizeImage )
  84. {
  85. // calculate full size
  86. INT nSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + pBitmapInfo->bmiHeader.biSizeImage + nPaletteSize;
  87. m_pBitmapFileHeader = (LPBITMAPFILEHEADER)realloc( m_pBitmapFileHeader, sizeof(BYTE) * nSize );
  88. if( !m_pBitmapFileHeader )
  89. {
  90. // clear attributes
  91. m_pBitmapInfoHeader = NULL;
  92. m_pBytes = NULL;
  93. return FALSE;
  94. }
  95. // fill file attributes
  96. m_pBitmapFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  97. m_pBitmapFileHeader->bfReserved1 = 0;
  98. m_pBitmapFileHeader->bfReserved2 = 0;
  99. m_pBitmapFileHeader->bfSize = nSize;
  100. m_pBitmapFileHeader->bfType = MAKEWORD( 'B', 'M' );
  101. // initialize pointers
  102. m_pBitmapInfoHeader = (LPBITMAPINFOHEADER)( (LPBYTE)m_pBitmapFileHeader + sizeof(BITMAPFILEHEADER) );
  103. m_pBytes = (LPBYTE)m_pBitmapFileHeader + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nPaletteSize;
  104. }
  105. // copy dib bits
  106. if( pBytes ) memcpy( m_pBytes, pBytes, pBitmapInfo->bmiHeader.biSizeImage );
  107. else memset( m_pBytes, 0, pBitmapInfo->bmiHeader.biSizeImage );
  108. // copy attributes + palette
  109. memcpy( m_pBitmapInfoHeader, pBitmapInfo, sizeof(BITMAPINFOHEADER) + nPaletteSize );
  110. return TRUE;
  111. }
  112. BOOL CLgImageDIB::Create( LPBITMAPINFOHEADER pBitmapInfoHeader, PBYTE pBytes )
  113. {
  114. return Create( (LPBITMAPINFO)pBitmapInfoHeader, pBytes );
  115. }
  116. BOOL CLgImageDIB::Create( HBITMAP hBitmap, LPBITMAPINFO pBitmapInfo )
  117. {
  118. if( !hBitmap )
  119. return FALSE;
  120. BOOL bResult = FALSE;
  121. if( pBitmapInfo )
  122. {
  123. DIBSECTION DibSection;
  124. if( !::GetObject( hBitmap, sizeof(DibSection), &DibSection ) )
  125. return FALSE;
  126. // get refference dc
  127. HDC hDC = ::GetDC( GetDesktopWindow() );
  128. if( !hDC )
  129. return FALSE;
  130. // prepare dib section
  131. PBYTE pByte = NULL;
  132. HBITMAP hSection = ::CreateDIBSection( hDC, pBitmapInfo, DIB_RGB_COLORS, (void**)&pByte, NULL, 0 );
  133. if( hSection )
  134. {
  135. HDC hSrcDC = ::CreateCompatibleDC( hDC );
  136. if( hSrcDC )
  137. {
  138. HDC hDstDC = ::CreateCompatibleDC( hDC );
  139. if( hDstDC )
  140. {
  141. HBITMAP hSrcBitmap = (HBITMAP)::SelectObject( hSrcDC, hBitmap );
  142. HBITMAP hDstBitmap = (HBITMAP)::SelectObject( hDstDC, hSection );
  143. INT32 nStretchMode = ::SetStretchBltMode( hDstDC, COLORONCOLOR );
  144. // stretch image according to incoming parameters
  145. ::StretchBlt( hDstDC, 0, 0, pBitmapInfo->bmiHeader.biWidth, pBitmapInfo->bmiHeader.biHeight,
  146. hSrcDC, 0, 0, DibSection.dsBm.bmWidth, DibSection.dsBm.bmHeight, SRCCOPY );
  147. ::SetStretchBltMode( hDstDC, nStretchMode );
  148. ::SelectObject( hSrcDC, hSrcBitmap );
  149. ::SelectObject( hDstDC, hDstBitmap );
  150. bResult = Create( pBitmapInfo, pByte );
  151. ::DeleteDC( hDstDC );
  152. }
  153. ::DeleteDC( hSrcDC );
  154. }
  155. ::DeleteObject( hSection );
  156. }
  157. ::ReleaseDC( GetDesktopWindow(), hDC );
  158. }
  159. else
  160. {
  161. BITMAPINFO bmi;
  162. memset( &bmi, 0, sizeof(bmi) );
  163. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  164. HDC hDC = ::GetDC( 0 );
  165. if( !hDC )
  166. return FALSE;
  167. if( !::GetDIBits( hDC, hBitmap, 0, 0, NULL, &bmi, DIB_RGB_COLORS) )
  168. {
  169. ::ReleaseDC( 0, hDC );
  170. return FALSE;
  171. }
  172. bmi.bmiHeader.biCompression = 0;
  173. bmi.bmiHeader.biBitCount = 24;
  174. bmi.bmiHeader.biSizeImage = WIDTHBYTES( bmi.bmiHeader.biWidth * bmi.bmiHeader.biBitCount ) * bmi.bmiHeader.biHeight;
  175. LPBYTE pByte = new BYTE[bmi.bmiHeader.biSizeImage];
  176. if( !pByte )
  177. {
  178. ::ReleaseDC( 0, hDC );
  179. return FALSE;
  180. }
  181. if( !::GetDIBits( hDC, hBitmap, 0, bmi.bmiHeader.biHeight, pByte, &bmi, DIB_RGB_COLORS) )
  182. {
  183. delete [] pByte;
  184. ::ReleaseDC( 0, hDC );
  185. return FALSE;
  186. }
  187. bResult = Create( (LPBITMAPINFO)&bmi, pByte );
  188. delete[] pByte;
  189. ::ReleaseDC( 0, hDC );
  190. }
  191. return bResult;
  192. }
  193. void CLgImageDIB::Destroy()
  194. {
  195. // protect data from simultaniosly access
  196. CLgLocker Locker( &m_CriticalSection );
  197. // clear memory
  198. if( m_pBitmapFileHeader )
  199. {
  200. free( m_pBitmapFileHeader );
  201. m_pBitmapFileHeader = NULL;
  202. }
  203. // clear attributes
  204. m_pBitmapInfoHeader = NULL;
  205. m_pBytes = NULL;
  206. }
  207. BOOL CLgImageDIB::Draw( HDC hDC, PRECT pRect, BOOL bStretch, BOOL bFlipVertical )
  208. {
  209. // protect data from simultaniosly access
  210. CLgLocker Locker( &m_CriticalSection );
  211. if( !m_pBytes )
  212. return FALSE;
  213. RECT rect = { 0, 0, m_pBitmapInfoHeader->biWidth, m_pBitmapInfoHeader->biHeight };
  214. if( !pRect )
  215. pRect = &rect;
  216. 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 );
  217. INT nWidth = pRect->right - pRect->left;
  218. INT nHeight = pRect->bottom - pRect->top;
  219. BOOL bDrawResult = FALSE;
  220. if( bFlipVertical ) m_pBitmapInfoHeader->biHeight =- m_pBitmapInfoHeader->biHeight;
  221. if( bStretch )
  222. {
  223. INT nMode = ::SetStretchBltMode( hDC, ( GetVersion() & 0x80000000UL ) ? COLORONCOLOR : HALFTONE );
  224. 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 );
  225. ::SetStretchBltMode( hDC, nMode );
  226. }
  227. else
  228. {
  229. POINT vPos = { (pRect->right - m_pBitmapInfoHeader->biWidth) >> 1,
  230. (pRect->bottom - abs(m_pBitmapInfoHeader->biHeight)) >> 1 };
  231. 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 );
  232. }
  233. if( bFlipVertical ) m_pBitmapInfoHeader->biHeight =- m_pBitmapInfoHeader->biHeight;
  234. if( !bDrawResult )
  235. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgImageDIB::Draw",
  236. "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)",
  237. GetLastError(), m_pBytes, m_pBitmapInfoHeader,
  238. m_pBitmapInfoHeader->biWidth,
  239. m_pBitmapInfoHeader->biHeight,
  240. m_pBitmapInfoHeader->biBitCount,
  241. m_pBitmapInfoHeader->biSizeImage,
  242. m_pBitmapInfoHeader->biCompression,
  243. m_pBitmapInfoHeader->biPlanes,
  244. m_pBitmapInfoHeader->biSize,
  245. m_pBitmapInfoHeader->biClrImportant,
  246. m_pBitmapInfoHeader->biClrUsed,
  247. m_pBitmapInfoHeader->biXPelsPerMeter,
  248. m_pBitmapInfoHeader->biYPelsPerMeter );
  249. else
  250. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CLgImageDIB::Draw",
  251. "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)",
  252. m_pBytes, m_pBitmapInfoHeader,
  253. m_pBitmapInfoHeader->biWidth,
  254. m_pBitmapInfoHeader->biHeight,
  255. m_pBitmapInfoHeader->biBitCount,
  256. m_pBitmapInfoHeader->biSizeImage,
  257. m_pBitmapInfoHeader->biCompression,
  258. m_pBitmapInfoHeader->biPlanes,
  259. m_pBitmapInfoHeader->biSize,
  260. m_pBitmapInfoHeader->biClrImportant,
  261. m_pBitmapInfoHeader->biClrUsed,
  262. m_pBitmapInfoHeader->biXPelsPerMeter,
  263. m_pBitmapInfoHeader->biYPelsPerMeter );
  264. return TRUE;
  265. }
  266. BOOL CLgImageDIB::Draw( HDC hDC, PRECT pDstRect, PRECT pSrcRect, BOOL bFlipVertical )
  267. {
  268. // protect data from simultaniosly access
  269. CLgLocker Locker( &m_CriticalSection );
  270. if( !m_pBytes )
  271. return FALSE;
  272. BOOL bDrawResult = FALSE;
  273. if( bFlipVertical )
  274. {
  275. }
  276. INT nMode = ::SetStretchBltMode( hDC, COLORONCOLOR );
  277. bDrawResult = ::StretchDIBits( hDC, pDstRect->left, pDstRect->top, pDstRect->right - pDstRect->left, pDstRect->bottom - pDstRect->top,
  278. pSrcRect->left, pSrcRect->top, pSrcRect->right - pSrcRect->left, pSrcRect->bottom - pSrcRect->top,
  279. m_pBytes, (LPBITMAPINFO)m_pBitmapInfoHeader, DIB_RGB_COLORS, SRCCOPY );
  280. ::SetStretchBltMode( hDC, nMode );
  281. return TRUE;
  282. }
  283. /*
  284. HBITMAP CLgImageDIB::CreateBitmap()
  285. {
  286. // protect data from simultaniosly access
  287. CLgLocker Locker( &m_CriticalSection );
  288. if( !m_pBytes )
  289. return FALSE;
  290. // get dc of entire screen
  291. HDC hDC = ::GetDC( NULL );
  292. if( !hDC )
  293. return NULL;
  294. // create dib bitmap
  295. HBITMAP hBitmap = ::CreateDIBitmap( hDC, m_pBitmapInfoHeader, CBM_INIT, m_pBytes, (LPBITMAPINFO)m_pBitmapInfoHeader, DIB_RGB_COLORS );
  296. // relase dc
  297. ::ReleaseDC( NULL, hDC );
  298. return hBitmap;
  299. }
  300. */
  301. HBITMAP CLgImageDIB::CreateBitmap( LPBITMAPINFO *ppBitmapInfo, LPBYTE *ppBytes )
  302. {
  303. // protect data from simultaniosly access
  304. CLgLocker Locker( &m_CriticalSection );
  305. if( !m_pBytes )
  306. return FALSE;
  307. // get dc of entire screen
  308. HDC hDC = ::GetDC( NULL );
  309. if( !hDC )
  310. return NULL;
  311. // create dib bitmap
  312. HBITMAP hBitmap = ::CreateDIBitmap( hDC, m_pBitmapInfoHeader, CBM_INIT, m_pBytes, (LPBITMAPINFO)m_pBitmapInfoHeader, DIB_RGB_COLORS );
  313. // relase dc
  314. ::ReleaseDC( NULL, hDC );
  315. // return format
  316. if( ppBitmapInfo )
  317. *ppBitmapInfo = (LPBITMAPINFO)m_pBitmapInfoHeader;
  318. if( ppBytes )
  319. *ppBytes = m_pBytes;
  320. return hBitmap;
  321. }
  322. BOOL CLgImageDIB::Save( LPCTSTR pFileName )
  323. {
  324. if( !m_pBitmapFileHeader )
  325. return FALSE;
  326. HANDLE hFile = ::CreateFile( pFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, 0, NULL );
  327. if( hFile == INVALID_HANDLE_VALUE )
  328. return FALSE;
  329. // protect data from simultaniosly access
  330. CLgLocker Locker( &m_CriticalSection );
  331. DWORD dwWritten = 0;
  332. BOOL bResult = ::WriteFile( hFile, m_pBitmapFileHeader, m_pBitmapFileHeader->bfSize, &dwWritten, NULL );
  333. ::CloseHandle( hFile );
  334. return bResult ? (dwWritten == m_pBitmapFileHeader->bfSize) : FALSE;
  335. }
  336. BOOL CLgImageDIB::Load( LPCTSTR pFileName, BOOL bConvertToRGB24 )
  337. {
  338. if( !pFileName )
  339. return FALSE;
  340. if( bConvertToRGB24 )
  341. {
  342. HANDLE hFile = ::CreateFile( pFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
  343. if( hFile == INVALID_HANDLE_VALUE )
  344. return FALSE;
  345. DWORD dwFileSize = GetFileSize( hFile, NULL );
  346. DWORD dwRead = 0;
  347. // read file header
  348. BITMAPFILEHEADER FileHeader;
  349. ::ReadFile( hFile, &FileHeader, sizeof(FileHeader), &dwRead, NULL );
  350. // allocate bitmap info
  351. INT nBitmapInfoSize = FileHeader.bfOffBits - sizeof(FileHeader);
  352. LPBITMAPINFO pBitmapInfo = (LPBITMAPINFO)malloc( nBitmapInfoSize );
  353. if( !pBitmapInfo )
  354. {
  355. ::CloseHandle( hFile );
  356. return FALSE;
  357. }
  358. // read bitmap info
  359. ::ReadFile( hFile, pBitmapInfo, nBitmapInfoSize, &dwRead, NULL );
  360. // if impossible to perform easy conversion
  361. if( pBitmapInfo->bmiHeader.biBitCount != 8 )
  362. {
  363. // close file
  364. free( pBitmapInfo );
  365. ::CloseHandle( hFile );
  366. // perform convertion by Windows API
  367. HBITMAP hBitmap = (HBITMAP)::LoadImage( 0, pFileName, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE );
  368. if( hBitmap )
  369. {
  370. BOOL bResult = Create( hBitmap );
  371. ::DeleteObject( hBitmap );
  372. return bResult;
  373. }
  374. return FALSE;
  375. }
  376. // prepare new bitmap info size
  377. INT nDstLineSize = WIDTHBYTES( pBitmapInfo->bmiHeader.biWidth * 24 );
  378. INT nDstImageSize = nDstLineSize * pBitmapInfo->bmiHeader.biHeight;
  379. INT nDstFullSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nDstImageSize;
  380. // check sizes
  381. if( (!m_pBitmapFileHeader) ||
  382. m_pBitmapInfoHeader->biBitCount != 24 ||
  383. m_pBitmapInfoHeader->biSizeImage < nDstImageSize )
  384. {
  385. // alloocate additional space
  386. m_pBitmapFileHeader = (LPBITMAPFILEHEADER)realloc( m_pBitmapFileHeader, sizeof(BYTE) * nDstFullSize );
  387. if( !m_pBitmapFileHeader )
  388. {
  389. // clear attributes
  390. m_pBitmapInfoHeader = NULL;
  391. m_pBytes = NULL;
  392. // close file data
  393. free( pBitmapInfo );
  394. ::CloseHandle( hFile );
  395. return FALSE;
  396. }
  397. // fill file attributes
  398. m_pBitmapFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  399. m_pBitmapFileHeader->bfReserved1 = 0;
  400. m_pBitmapFileHeader->bfReserved2 = 0;
  401. m_pBitmapFileHeader->bfSize = nDstFullSize;
  402. m_pBitmapFileHeader->bfType = MAKEWORD( 'B', 'M' );
  403. // initialize pointers
  404. m_pBitmapInfoHeader = (LPBITMAPINFOHEADER)( (LPBYTE)m_pBitmapFileHeader + sizeof(BITMAPFILEHEADER) );
  405. m_pBytes = (LPBYTE)m_pBitmapFileHeader + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  406. }
  407. // copy attributes
  408. memcpy( m_pBitmapInfoHeader, pBitmapInfo, sizeof(BITMAPINFOHEADER) );
  409. // adjust attributes
  410. m_pBitmapInfoHeader->biBitCount = 24;
  411. m_pBitmapInfoHeader->biSizeImage = nDstImageSize;
  412. // allocate buffer for source bytes
  413. PBYTE pSrcBytes = (PBYTE)malloc( pBitmapInfo->bmiHeader.biSizeImage );
  414. if( !pSrcBytes )
  415. {
  416. // close file data
  417. free( pBitmapInfo );
  418. ::CloseHandle( hFile );
  419. return FALSE;
  420. }
  421. // read bitmap data
  422. ::ReadFile( hFile, pSrcBytes, pBitmapInfo->bmiHeader.biSizeImage, &dwRead, NULL );
  423. // perform conversion
  424. PBYTE pDstLine = m_pBytes;
  425. PBYTE pSrcLine = pSrcBytes;
  426. INT nSrcLineSize = WIDTHBYTES( pBitmapInfo->bmiHeader.biWidth * 8 );
  427. for( INT y = 0; y < pBitmapInfo->bmiHeader.biHeight; y++ )
  428. {
  429. RGBTRIPLE *pTriple = (RGBTRIPLE*)pDstLine;
  430. for( INT x = 0; x < pBitmapInfo->bmiHeader.biWidth; x++ )
  431. {
  432. pTriple[x] = (RGBTRIPLE&)pBitmapInfo->bmiColors[ pSrcLine[x] ];
  433. }
  434. pDstLine += nDstLineSize;
  435. pSrcLine += nSrcLineSize;
  436. }
  437. // close file data
  438. free( pBitmapInfo );
  439. free( pSrcBytes );
  440. ::CloseHandle( hFile );
  441. return TRUE;
  442. }
  443. else
  444. {
  445. HANDLE hFile = ::CreateFile( pFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
  446. if( hFile == INVALID_HANDLE_VALUE )
  447. return FALSE;
  448. DWORD dwFileSize = GetFileSize( hFile, NULL );
  449. // protect data from simultaniosly access
  450. CLgLocker Locker( &m_CriticalSection );
  451. m_pBitmapFileHeader = (LPBITMAPFILEHEADER)realloc( m_pBitmapFileHeader, dwFileSize );
  452. if( !m_pBitmapFileHeader )
  453. {
  454. m_pBitmapFileHeader = NULL;
  455. m_pBitmapInfoHeader = NULL;
  456. m_pBytes = NULL;
  457. ::CloseHandle( hFile );
  458. return FALSE;
  459. }
  460. DWORD dwRead = 0;
  461. BOOL bResult = ::ReadFile( hFile, m_pBitmapFileHeader, dwFileSize, &dwRead, NULL );
  462. ::CloseHandle( hFile );
  463. if( bResult && ( m_pBitmapFileHeader->bfSize <= dwRead ) )
  464. {
  465. m_pBitmapInfoHeader = (LPBITMAPINFOHEADER)( (PBYTE)m_pBitmapFileHeader + sizeof(BITMAPFILEHEADER) );
  466. m_pBytes = (PBYTE)m_pBitmapFileHeader + m_pBitmapFileHeader->bfOffBits;
  467. return TRUE;
  468. }
  469. }
  470. return FALSE;
  471. }
  472. BOOL CLgImageDIB::Create( CLgImageJPEG *pImageJPEG )
  473. {
  474. if( !pImageJPEG )
  475. return FALSE;
  476. // protect data from simultaniosly access
  477. CLgLocker Locker( &m_CriticalSection );
  478. return pImageJPEG->ConvertToDIB( m_pBitmapFileHeader, m_pBitmapInfoHeader, m_pBytes );
  479. }
  480. BOOL CLgImageDIB::Create( LPBYTE pJpegData, INT nSize )
  481. {
  482. // protect data from simultaniosly access
  483. CLgLocker Locker( &m_CriticalSection );
  484. return CLgImageJPEG::ConvertToDIB( pJpegData, nSize, m_pBitmapFileHeader, m_pBitmapInfoHeader, m_pBytes );
  485. }
  486. BOOL CLgImageDIB::Erase()
  487. {
  488. // protect data from simultaniosly access
  489. CLgLocker Locker( &m_CriticalSection );
  490. if( !m_pBitmapInfoHeader || !m_pBytes )
  491. return TRUE;
  492. memset( m_pBytes, 0, m_pBitmapInfoHeader->biSizeImage );
  493. return TRUE;
  494. }