CeXDib.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include "stdafx.h"
  2. #include "CeXDib.h"
  3. #ifdef _DEBUG
  4. #undef THIS_FILE
  5. static char THIS_FILE[]=__FILE__;
  6. #define new DEBUG_NEW
  7. #endif
  8. CCeXDib::CCeXDib()
  9. {
  10. m_hDib = NULL;
  11. m_dwLineWidth = 0;
  12. m_wColors = 0;
  13. m_hMemDC = NULL;
  14. m_hBitmap = NULL;
  15. m_lpBits = NULL;
  16. FreeResources();
  17. }
  18. CCeXDib::~CCeXDib()
  19. {
  20. FreeResources();
  21. }
  22. void CCeXDib::FreeResources()
  23. {
  24. if (m_hMemDC) ::DeleteDC(m_hMemDC);
  25. if (m_hBitmap) ::DeleteObject(m_hBitmap);
  26. if (m_hDib) delete m_hDib;
  27. m_hDib = NULL;
  28. m_hMemDC = NULL;
  29. m_hBitmap = NULL;
  30. m_lpBits = NULL;
  31. memset(&m_bi, 0, sizeof(m_bi));
  32. } // End of FreeResources
  33. HDIB CCeXDib::Create(DWORD dwWidth, DWORD dwHeight, WORD wBitCount)
  34. {
  35. LPBITMAPINFOHEADER lpbi = NULL; // Pointer to BITMAPINFOHEADER
  36. DWORD dwLen = 0; // Size of memory block
  37. FreeResources();
  38. // Following <switch> is taken from
  39. // CDIBSectionLite class by Chris Maunder
  40. switch (wBitCount)
  41. {
  42. case 1: m_wColors = 2; break;
  43. #ifdef _WIN32_WCE
  44. case 2: m_wColors = 4; break; // winCE only
  45. #endif
  46. case 4: m_wColors = 16; break;
  47. case 8: m_wColors = 256; break;
  48. case 16:
  49. case 24:
  50. case 32: m_wColors = 0; break; // 16,24 or 32 bpp have no color table
  51. default:
  52. m_wColors = 0;
  53. } // switch
  54. /*
  55. // Make sure bits per pixel is valid
  56. if (wBitCount <= 1) wBitCount = 1;
  57. else if (wBitCount <= 4) wBitCount = 4;
  58. else if (wBitCount <= 8) wBitCount = 8;
  59. else wBitCount = 24;
  60. switch (wBitCount)
  61. {
  62. case 1:
  63. m_wColors = 2;
  64. break;
  65. case 4:
  66. m_wColors = 16;
  67. break;
  68. case 8:
  69. m_wColors = 256;
  70. break;
  71. default:
  72. m_wColors = 0;
  73. break;
  74. } // switch
  75. */
  76. m_dwLineWidth = WIDTHBYTES(wBitCount * dwWidth);
  77. // Initialize BITMAPINFOHEADER
  78. m_bi.biSize = sizeof(BITMAPINFOHEADER);
  79. m_bi.biWidth = dwWidth; // fill in width from parameter
  80. m_bi.biHeight = dwHeight; // fill in height from parameter
  81. m_bi.biPlanes = 1; // must be 1
  82. m_bi.biBitCount = wBitCount; // from parameter
  83. m_bi.biCompression = BI_RGB;
  84. m_bi.biSizeImage = m_dwLineWidth * dwHeight;
  85. m_bi.biXPelsPerMeter = 0;
  86. m_bi.biYPelsPerMeter = 0;
  87. m_bi.biClrUsed = 0;
  88. m_bi.biClrImportant = 0;
  89. // Calculate size of memory block required to store the DIB. This
  90. // block should be big enough to hold the BITMAPINFOHEADER, the color
  91. // table, and the bits.
  92. dwLen = GetSize();
  93. m_hDib = new HDIB[dwLen]; // Allocate memory block to store our bitmap
  94. if (m_hDib == NULL) return NULL;
  95. // Use our bitmap info structure to fill in first part of
  96. // our DIB with the BITMAPINFOHEADER
  97. lpbi = (LPBITMAPINFOHEADER)(m_hDib);
  98. *lpbi = m_bi;
  99. return m_hDib; // Return handle to the DIB
  100. } // End of Create
  101. DWORD CCeXDib::GetSize()
  102. {
  103. return m_bi.biSize + m_bi.biSizeImage + GetPaletteSize();
  104. } // End of GetSize
  105. DWORD CCeXDib::GetPaletteSize()
  106. {
  107. return (m_wColors * sizeof(RGBQUAD));
  108. } // End of GetPaletteSize
  109. LPBYTE CCeXDib::GetBits()
  110. {
  111. if (m_hDib) return ((LPBYTE)m_hDib + *(LPDWORD)m_hDib + GetPaletteSize());
  112. return NULL;
  113. } // End of GetBits
  114. DWORD CCeXDib::GetWidth()
  115. {
  116. return m_bi.biWidth;
  117. } // End of GetWidth
  118. DWORD CCeXDib::GetHeight()
  119. {
  120. return m_bi.biHeight;
  121. } // End of GetHeight
  122. DWORD CCeXDib::GetLineWidth()
  123. {
  124. return m_dwLineWidth;
  125. } // End of GetLineWidth
  126. void CCeXDib::BlendPalette(COLORREF crColor, DWORD dwPerc)
  127. {
  128. if (m_hDib == NULL || m_wColors == 0) return;
  129. LPBYTE iDst = (LPBYTE)(m_hDib) + sizeof(BITMAPINFOHEADER);
  130. long i,r,g,b;
  131. RGBQUAD* pPal = (RGBQUAD*)iDst;
  132. r = GetRValue(crColor);
  133. g = GetGValue(crColor);
  134. b = GetBValue(crColor);
  135. if (dwPerc > 100) dwPerc = 100;
  136. for (i = 0; i < m_wColors; i++)
  137. {
  138. pPal[i].rgbBlue = (BYTE)((pPal[i].rgbBlue * (100 - dwPerc) + b * dwPerc) / 100);
  139. pPal[i].rgbGreen = (BYTE)((pPal[i].rgbGreen * (100 - dwPerc) + g * dwPerc) / 100);
  140. pPal[i].rgbRed = (BYTE)((pPal[i].rgbRed * (100 - dwPerc) + r * dwPerc) / 100);
  141. } // for
  142. } // End of BlendPalette
  143. void CCeXDib::Clear(BYTE byVal)
  144. {
  145. if (m_hDib) memset(GetBits(), byVal, m_bi.biSizeImage);
  146. } // End of Clear
  147. void CCeXDib::SetPixelIndex(DWORD dwX, DWORD dwY, BYTE byI)
  148. {
  149. if ((m_hDib == NULL) || (m_wColors == 0) ||
  150. ((long)dwX < 0) || ((long)dwY < 0) || (dwX >= (DWORD)m_bi.biWidth) || (dwY >= (DWORD)m_bi.biHeight)) return;
  151. LPBYTE iDst = GetBits();
  152. iDst[(m_bi.biHeight - dwY - 1) * m_dwLineWidth + dwX] = byI;
  153. } // End of SetPixelIndex
  154. void CCeXDib::Clone(CCeXDib* src)
  155. {
  156. Create(src->GetWidth(), src->GetHeight(), src->GetBitCount());
  157. if (m_hDib) memcpy(m_hDib, src->m_hDib, GetSize());
  158. } // End of Clone
  159. WORD CCeXDib::GetBitCount()
  160. {
  161. return m_bi.biBitCount;
  162. } // End of GetBitCount
  163. void CCeXDib::SetPaletteIndex(BYTE byIdx, BYTE byR, BYTE byG, BYTE byB)
  164. {
  165. if (m_hDib && m_wColors)
  166. {
  167. LPBYTE iDst = (LPBYTE)(m_hDib) + sizeof(BITMAPINFOHEADER);
  168. if ((byIdx >= 0) && (byIdx < m_wColors))
  169. {
  170. long ldx = byIdx * sizeof(RGBQUAD);
  171. iDst[ldx++] = (BYTE)byB;
  172. iDst[ldx++] = (BYTE)byG;
  173. iDst[ldx++] = (BYTE)byR;
  174. iDst[ldx] = (BYTE)0;
  175. } // if
  176. } // if
  177. } // End of SetPaletteIndex
  178. void CCeXDib::Draw(HDC hDC, DWORD dwX, DWORD dwY)
  179. {
  180. HBITMAP hBitmap = NULL;
  181. HBITMAP hOldBitmap = NULL;
  182. HDC hMemDC = NULL;
  183. if (m_hBitmap == NULL)
  184. {
  185. m_hBitmap = CreateDIBSection(hDC, (BITMAPINFO*)m_hDib, DIB_RGB_COLORS, &m_lpBits, NULL, 0);
  186. if (m_hBitmap == NULL) return;
  187. if (m_lpBits == NULL)
  188. {
  189. ::DeleteObject(m_hBitmap);
  190. m_hBitmap = NULL;
  191. return;
  192. } // if
  193. } // if
  194. memcpy(m_lpBits, GetBits(), m_bi.biSizeImage);
  195. if (m_hMemDC == NULL)
  196. {
  197. m_hMemDC = CreateCompatibleDC(hDC);
  198. if (m_hMemDC == NULL) return;
  199. } // if
  200. hOldBitmap = (HBITMAP)SelectObject(m_hMemDC, m_hBitmap);
  201. BitBlt(hDC, dwX, dwY, m_bi.biWidth, m_bi.biHeight, m_hMemDC, 0, 0, SRCCOPY);
  202. SelectObject(m_hMemDC, hOldBitmap);
  203. } // End of Draw
  204. void CCeXDib::SetGrayPalette()
  205. {
  206. RGBQUAD pal[256];
  207. RGBQUAD* ppal;
  208. LPBYTE iDst;
  209. int ni;
  210. if (m_hDib == NULL || m_wColors == 0) return;
  211. ppal = (RGBQUAD*)&pal[0];
  212. iDst = (LPBYTE)(m_hDib) + sizeof(BITMAPINFOHEADER);
  213. for (ni = 0; ni < m_wColors; ni++)
  214. {
  215. pal[ni] = RGB2RGBQUAD(RGB(ni,ni,ni));
  216. } // for
  217. pal[0] = RGB2RGBQUAD(RGB(0,0,0));
  218. pal[m_wColors-1] = RGB2RGBQUAD(RGB(255,255,255));
  219. memcpy(iDst, ppal, GetPaletteSize());
  220. } // End of SetGrayPalette
  221. RGBQUAD CCeXDib::RGB2RGBQUAD(COLORREF cr)
  222. {
  223. RGBQUAD c;
  224. c.rgbRed = GetRValue(cr); /* get R, G, and B out of DWORD */
  225. c.rgbGreen = GetGValue(cr);
  226. c.rgbBlue = GetBValue(cr);
  227. c.rgbReserved=0;
  228. return c;
  229. } // End of RGB2RGBQUAD
  230. WORD CCeXDib::GetNumColors()
  231. {
  232. return m_wColors;
  233. } // End of GetNumColors
  234. BOOL CCeXDib::WriteBMP(LPCTSTR bmpFileName)
  235. {
  236. BITMAPFILEHEADER hdr;
  237. HANDLE hFile;
  238. DWORD nByteWrite;
  239. if (*bmpFileName == _T('\0') || m_hDib == 0) return 0;
  240. hFile=CreateFile( // open if exist ini file
  241. bmpFileName, // pointer to name of the file
  242. GENERIC_WRITE, // access mode
  243. 0, // share mode
  244. NULL, // pointer to security descriptor
  245. CREATE_ALWAYS, // how to create
  246. FILE_ATTRIBUTE_NORMAL, // file attributes
  247. NULL // handle to file with attributes to copy
  248. );
  249. if (hFile == INVALID_HANDLE_VALUE) return FALSE;
  250. // Fill in the fields of the file header
  251. hdr.bfType = BFT_BITMAP;
  252. hdr.bfSize = GetSize() + sizeof(BITMAPFILEHEADER);
  253. hdr.bfReserved1 = hdr.bfReserved2 = 0;
  254. hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER)+
  255. m_bi.biSize + GetPaletteSize();
  256. // Write the file header
  257. WriteFile( // write ini (sync mode <-> no overlapped)
  258. hFile, // handle of file to write
  259. (LPSTR) &hdr, // address of buffer that contains data
  260. sizeof(BITMAPFILEHEADER), // number of bytes to write
  261. &nByteWrite, // address of number of bytes written
  262. NULL // address of structure for data
  263. );
  264. // Write the DIB header and the bits
  265. WriteFile( // write ini (sync mode <-> no overlapped)
  266. hFile, // handle of file to write
  267. (LPSTR) m_hDib, // address of buffer that contains data
  268. GetSize(), // number of bytes to write
  269. &nByteWrite, // address of number of bytes written
  270. NULL // address of structure for data
  271. );
  272. CloseHandle(hFile); // free file handle
  273. return TRUE;
  274. } // End of WriteBMP