Leaked source code of windows server 2003
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.

224 lines
7.2 KiB

  1. #ifndef _CDIBUTIL
  2. #define _CDIBUTIL
  3. class CDIBUtil {
  4. public:
  5. CDIBUtil();
  6. ~CDIBUtil();
  7. BOOL SetupBmpHeader(BITMAPINFOHEADER *pbiHeader, int width, int height, int bits, BOOL TopDown, int Resolution)
  8. {
  9. BOOL bSuccess = TRUE;
  10. int WidthBytes = 0;
  11. pbiHeader->biSize = sizeof(BITMAPINFOHEADER);
  12. pbiHeader->biWidth = width;
  13. pbiHeader->biHeight = height * ((TopDown==TRUE)?-1:1);
  14. pbiHeader->biPlanes = 1;
  15. pbiHeader->biBitCount = (BYTE)bits;
  16. pbiHeader->biCompression = 0;
  17. pbiHeader->biXPelsPerMeter = int(Resolution * 39.37);
  18. pbiHeader->biYPelsPerMeter = int(Resolution * 39.37);
  19. pbiHeader->biClrUsed = 0;
  20. pbiHeader->biClrImportant = 0;
  21. switch (bits) {
  22. case 1:
  23. WidthBytes = (width+7)/8;
  24. pbiHeader->biClrUsed = 2;
  25. break;
  26. case 8:
  27. WidthBytes = width;
  28. pbiHeader->biClrUsed = 256;
  29. break;
  30. case 24:
  31. WidthBytes = width*3;
  32. pbiHeader->biClrUsed = 0;
  33. break;
  34. default:
  35. WidthBytes = 0;
  36. bSuccess = FALSE;
  37. break;
  38. }
  39. pbiHeader->biSizeImage = WidthToDIBWidth(WidthBytes)*height; // not used for compression
  40. return bSuccess;
  41. }
  42. void FillPalette (int NumPaletteEntries, tagRGBQUAD* pPal)
  43. {
  44. if (NumPaletteEntries>1) {
  45. for (int i=0; i<NumPaletteEntries; i++) {
  46. int Val = i*255/(NumPaletteEntries-1);
  47. pPal->rgbRed = (BYTE)Val;
  48. pPal->rgbGreen = (BYTE)Val;
  49. pPal->rgbBlue = (BYTE)Val;
  50. pPal->rgbReserved = 0;
  51. pPal++;
  52. }
  53. }
  54. }
  55. void FillLogPalette (int NumPaletteEntries, LOGPALETTE* pPal)
  56. {
  57. if (NumPaletteEntries>2) {
  58. for (int i=0; i<NumPaletteEntries; i++) {
  59. pPal->palPalEntry[i].peRed = (BYTE)i;
  60. pPal->palPalEntry[i].peGreen = (BYTE)i;
  61. pPal->palPalEntry[i].peBlue = (BYTE)i;
  62. pPal->palPalEntry[i].peFlags = 0;
  63. }
  64. } else { // B/W Palette
  65. pPal->palPalEntry[0].peRed = (BYTE)255;
  66. pPal->palPalEntry[0].peGreen = (BYTE)255;
  67. pPal->palPalEntry[0].peBlue = (BYTE)255;
  68. pPal->palPalEntry[0].peFlags = 0;
  69. pPal->palPalEntry[1].peRed = (BYTE)0;
  70. pPal->palPalEntry[1].peGreen = (BYTE)0;
  71. pPal->palPalEntry[1].peBlue = (BYTE)0;
  72. pPal->palPalEntry[1].peFlags = 0;
  73. }
  74. }
  75. BOOL RawColorToDIB(int BitsPerPixel, BYTE* pDest, BYTE* pSrc, int Width, int Height)
  76. {
  77. BOOL bSuccess = FALSE;
  78. switch (BitsPerPixel) {
  79. case 1:
  80. bSuccess = RawColorToBinaryDIB (pDest, pSrc, Width, Height);
  81. break;
  82. case 8:
  83. bSuccess = RawColorToGrayDIB (pDest, pSrc, Width, Height);
  84. break;
  85. case 24:
  86. bSuccess = RawColorToColorDIB(pDest, pSrc, Width, Height);
  87. break;
  88. default:
  89. break;
  90. }
  91. return bSuccess;
  92. }
  93. BOOL RawColorToBinaryDIB(BYTE* pDest, BYTE* pSrc, int Width, int Height)
  94. {
  95. BOOL bSuccess = TRUE;
  96. BYTE* ptDest = NULL;
  97. BYTE* ptSrc = NULL;
  98. int BitIdx = 0;
  99. BYTE Bits = 0;
  100. BYTE GrayByte = 0;
  101. for (int i=0; i < Height; i++) {
  102. ptDest = pDest + (i*WidthToDIBWidth((Width+7)/8));
  103. ptSrc = pSrc + i*Width*3;
  104. BitIdx = 0;
  105. Bits = 0;
  106. for (int j=0; j < Width; j++) {
  107. GrayByte = (BYTE)((ptSrc[0] * 11 + ptSrc[1] * 59 + ptSrc[2] * 30)/100);
  108. Bits *= 2;
  109. if (GrayByte > 128) Bits += 1;
  110. BitIdx++;
  111. if (BitIdx >= 8) {
  112. BitIdx = 0;
  113. *ptDest++ = Bits;
  114. }
  115. ptSrc += 3;
  116. }
  117. // Write out the last byte if matters
  118. if (BitIdx)
  119. *ptDest = Bits;
  120. }
  121. return bSuccess;
  122. }
  123. BOOL RawColorToGrayDIB(BYTE* pDest, BYTE* pSrc, int Width, int Height)
  124. {
  125. BOOL bSuccess = TRUE;
  126. BYTE* ptDest = NULL;
  127. BYTE* ptSrc = NULL;
  128. for (int i=0; i<Height; i++) {
  129. ptDest = pDest + (i*WidthToDIBWidth(Width));
  130. ptSrc = pSrc + i*Width*3;
  131. for (int j=0; j<Width; j++) {
  132. *ptDest++ = (BYTE)((ptSrc[0] * 11 + ptSrc[1] * 59 + ptSrc[2] * 30)/100);
  133. ptSrc += 3;
  134. }
  135. }
  136. return bSuccess;
  137. }
  138. BOOL RawColorToColorDIB(BYTE* pDest, BYTE* pSrc, int Width, int Height)
  139. {
  140. BOOL bSuccess = TRUE;
  141. BYTE* ptDest = NULL;
  142. BYTE* ptSrc = NULL;
  143. for (int i=0; i<Height; i++) {
  144. ptDest = pDest + (i*WidthToDIBWidth(Width*3));
  145. ptSrc = pSrc + i*Width*3;
  146. memcpy (ptDest, ptSrc, Width*3);
  147. }
  148. return bSuccess;
  149. }
  150. int WidthToDIBWidth(int Width)
  151. {
  152. return (Width+3)&0xfffffffc;
  153. }
  154. BOOL WriteDIBToBMP(LPTSTR strFileName, HANDLE hDIB, int nNumPaletteEntries)
  155. {
  156. return FALSE; // force exit for now.
  157. BOOL bSuccess = FALSE;
  158. // lock memory
  159. BYTE* pDIB = (BYTE*)GlobalLock(hDIB);
  160. if (pDIB) {
  161. LPBITMAPINFO pBitmap = (LPBITMAPINFO)pDIB;
  162. // write BMP file header
  163. BITMAPFILEHEADER bmfHeader;
  164. bmfHeader.bfType = 0x4d42;
  165. bmfHeader.bfReserved1 = bmfHeader.bfReserved2 = 0;
  166. bmfHeader.bfSize = pBitmap->bmiHeader.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*nNumPaletteEntries;
  167. bmfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*nNumPaletteEntries;
  168. // write to disk
  169. // Write(&bmfHeader,sizeof(BITMAPFILEHEADER));
  170. // Write(pDIB,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*nNumPaletteEntries+pBitmap->bmiHeader.biSizeImage);
  171. // unlock memory
  172. GlobalUnlock(hDIB);
  173. bSuccess = TRUE;
  174. }
  175. return bSuccess;
  176. }
  177. void GrayTo24bitColorDIB(BYTE* pSrc, int WidthPixels, int Height)
  178. {
  179. BYTE* pTemp = NULL;
  180. DWORD dwSizeBits = (WidthPixels * Height);
  181. pTemp = (BYTE*)GlobalAlloc(GPTR,dwSizeBits + 1024);
  182. if (pTemp) {
  183. CopyMemory(pTemp,pSrc,dwSizeBits);
  184. DWORD MemIndex = 0;
  185. DWORD SrcIndex = 0;
  186. while (MemIndex <= dwSizeBits) {
  187. for (int i = 0;i<=2;i++) {
  188. pSrc[SrcIndex] = pTemp[MemIndex];
  189. SrcIndex++;
  190. }
  191. MemIndex++;
  192. }
  193. GlobalFree(pTemp);
  194. }
  195. }
  196. private:
  197. };
  198. #endif