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.

323 lines
8.7 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: CFile.cpp
  3. *
  4. * This file contains the code to support the functionality test harness
  5. * for GDI+. This includes menu options and calling the appropriate
  6. * functions for execution.
  7. *
  8. * Created: 05-May-2000 - Jeff Vezina [t-jfvez]
  9. *
  10. * Copyright (c) 2000 Microsoft Corporation
  11. *
  12. \**************************************************************************/
  13. #include "CFile.h"
  14. #include "CFuncTest.h"
  15. extern CFuncTest g_FuncTest;
  16. CFile::CFile(BOOL bRegression,int nBits)
  17. {
  18. sprintf(m_szName,"File %d bits",nBits);
  19. m_nBits=nBits;
  20. m_hDC=NULL;
  21. m_hBM=NULL;
  22. m_hBMOld=NULL;
  23. InitPalettes();
  24. m_bRegression=bRegression;
  25. }
  26. CFile::~CFile()
  27. {
  28. }
  29. Graphics *CFile::PreDraw(int &nOffsetX,int &nOffsetY)
  30. {
  31. Graphics *g=NULL;
  32. PVOID pvBits = NULL;
  33. HDC hdcWnd=GetDC(g_FuncTest.m_hWndMain);
  34. // these combined give BITMAPINFO structure.
  35. struct {
  36. BITMAPINFOHEADER bmih;
  37. union {
  38. RGBQUAD1 rgbquad1;
  39. RGBQUAD2 rgbquad2;
  40. RGBQUAD4 rgbquad4;
  41. RGBQUAD8 rgbquad8;
  42. RGBQUAD16 rgbquad16;
  43. RGBQUAD24 rgbquad24;
  44. RGBQUAD32 rgbquad32;
  45. };
  46. } bmi;
  47. bmi.bmih.biSize = sizeof(BITMAPINFOHEADER);
  48. bmi.bmih.biWidth = (int)TESTAREAWIDTH;
  49. bmi.bmih.biHeight = (int)TESTAREAHEIGHT;
  50. bmi.bmih.biPlanes = 1;
  51. bmi.bmih.biBitCount = (WORD)m_nBits;
  52. bmi.bmih.biCompression = BI_RGB;
  53. bmi.bmih.biSizeImage = 0;
  54. bmi.bmih.biXPelsPerMeter = 0;
  55. bmi.bmih.biYPelsPerMeter = 0;
  56. bmi.bmih.biClrUsed = 0; // only used for <= 16bpp
  57. bmi.bmih.biClrImportant = 0;
  58. // create appropriate rgb table.
  59. switch (m_nBits)
  60. {
  61. case 1: memcpy(bmi.rgbquad1, m_rgbQuad1, sizeof(m_rgbQuad1));
  62. break;
  63. case 2: memcpy(bmi.rgbquad2, m_rgbQuad2, sizeof(m_rgbQuad2));
  64. break;
  65. case 4: memcpy(bmi.rgbquad4, m_rgbQuad4, sizeof(m_rgbQuad4));
  66. break;
  67. case 8: memcpy(bmi.rgbquad8, m_rgbQuad8, sizeof(m_rgbQuad8));
  68. break;
  69. case 16: memcpy(bmi.rgbquad16, m_rgbQuad16, sizeof(m_rgbQuad16));
  70. break;
  71. case 24: memcpy(bmi.rgbquad24, m_rgbQuad24, sizeof(m_rgbQuad24));
  72. break;
  73. case 32: memcpy(bmi.rgbquad32, m_rgbQuad32, sizeof(m_rgbQuad32));
  74. break;
  75. }
  76. m_hBM=CreateDIBSection(hdcWnd,(BITMAPINFO*)&bmi,DIB_RGB_COLORS,&pvBits,NULL,0);
  77. if (!m_hBM)
  78. {
  79. MessageBoxA(NULL,"Can't create DIB Section.","",MB_OK);
  80. return NULL;
  81. }
  82. // create DC for our DIB
  83. m_hDC=CreateCompatibleDC(hdcWnd);
  84. m_hBMOld=(HBITMAP)SelectObject(m_hDC,m_hBM);
  85. // Set the background to main window background
  86. BitBlt(m_hDC, 0, 0, (int)TESTAREAWIDTH, (int)TESTAREAHEIGHT, hdcWnd, nOffsetX, nOffsetY, SRCCOPY);
  87. // PatBlt(m_hDC, 0, 0, (int)TESTAREAWIDTH, (int)TESTAREAHEIGHT, WHITENESS);
  88. // if (m_nBits == 32)
  89. // g = Graphics::FromDib32(pvBits, rClient.right, rClient.bottom);
  90. // else
  91. g = Graphics::FromHDC(m_hDC);
  92. // Since we are doing the test on another surface
  93. nOffsetX=0;
  94. nOffsetY=0;
  95. ReleaseDC(g_FuncTest.m_hWndMain,hdcWnd);
  96. return g;
  97. }
  98. void CFile::PostDraw(RECT rTestArea)
  99. {
  100. char *szTitle="TestDIB.bmp";
  101. HDC hdcOrig = GetDC(g_FuncTest.m_hWndMain);
  102. // blit from the DIB to screen so we see the results, we use
  103. // GDI for this.
  104. BitBlt(hdcOrig, rTestArea.left, rTestArea.top, (int)TESTAREAWIDTH, (int)TESTAREAHEIGHT, m_hDC, 0, 0, SRCCOPY);
  105. BOOL success = WriteBitmap(szTitle, m_hBM, (int)TESTAREAWIDTH, (int)TESTAREAHEIGHT);
  106. ReleaseDC(g_FuncTest.m_hWndMain,hdcOrig);
  107. SelectObject(m_hDC,m_hBMOld);
  108. DeleteDC(m_hDC);
  109. DeleteObject(m_hBM);
  110. }
  111. void CFile::InitPalettes()
  112. {
  113. BYTE red=0,green=0,blue=0;
  114. RGBQUAD1 rgbQuad1 = { { 0, 0, 0, 0 },
  115. { 0xff, 0xff, 0xff, 0 } };
  116. RGBQUAD2 rgbQuad2 = { { 0x80, 0x80, 0x80, 0 },
  117. { 0x80, 0, 0, 0 },
  118. { 0, 0x80, 0, 0 },
  119. { 0, 0, 0x80, 0 } };
  120. RGBQUAD4 rgbQuad4 = { // B G R
  121. { 0, 0, 0, 0 }, // 0
  122. { 0, 0, 0x80,0 }, // 1
  123. { 0, 0x80,0, 0 }, // 2
  124. { 0, 0x80,0x80,0 }, // 3
  125. { 0x80,0, 0, 0 }, // 4
  126. { 0x80,0, 0x80,0 }, // 5
  127. { 0x80,0x80,0, 0 }, // 6
  128. { 0x80,0x80,0x80,0 }, // 7
  129. { 0xC0,0xC0,0xC0,0 }, // 8
  130. { 0, 0, 0xFF,0 }, // 9
  131. { 0, 0xFF,0, 0 }, // 10
  132. { 0, 0xFF,0xFF,0 }, // 11
  133. { 0xFF,0, 0, 0 }, // 12
  134. { 0xFF,0, 0xFF,0 }, // 13
  135. { 0xFF,0xFF,0, 0 }, // 14
  136. { 0xFF,0xFF,0xFF,0 } }; // 15
  137. RGBQUAD16 rgbQuad16 = { { 0, 0x7c, 0, 0 }, { 0xe0, 03, 0, 0 }, { 0x1f, 0, 0, 0 } };
  138. RGBQUAD24 rgbQuad24 = { { 0, 0, 0xff, 0 }, { 0, 0xff, 0, 0 }, { 0xff, 0, 0, 0 } };
  139. RGBQUAD32 rgbQuad32 = { { 0, 0, 0xff, 0 }, { 0, 0xff, 0, 0 }, { 0xff, 0, 0 ,0 } };
  140. memcpy(m_rgbQuad1,rgbQuad1,sizeof(rgbQuad1));
  141. memcpy(m_rgbQuad2,rgbQuad2,sizeof(rgbQuad2));
  142. memcpy(m_rgbQuad4,rgbQuad4,sizeof(rgbQuad4));
  143. memcpy(m_rgbQuad16,rgbQuad16,sizeof(rgbQuad16));
  144. memcpy(m_rgbQuad24,rgbQuad24,sizeof(rgbQuad24));
  145. memcpy(m_rgbQuad32,rgbQuad32,sizeof(rgbQuad32));
  146. ZeroMemory(m_rgbQuad8,sizeof(m_rgbQuad8));
  147. for (INT i=0; i<256; i++)
  148. {
  149. m_rgbQuad8[i].rgbRed=red;
  150. m_rgbQuad8[i].rgbGreen=green;
  151. m_rgbQuad8[i].rgbBlue=blue;
  152. if (!(red+=32))
  153. {
  154. if (!(green+=32))
  155. {
  156. blue+=64;
  157. }
  158. }
  159. }
  160. // for system colors (last 20), use those from 4 bpp palette table.
  161. for (INT j=248; j<256; j++)
  162. {
  163. m_rgbQuad8[j].rgbRed=m_rgbQuad4[j-240].rgbRed;
  164. m_rgbQuad8[j].rgbGreen=m_rgbQuad4[j-240].rgbGreen;
  165. m_rgbQuad8[j].rgbBlue=m_rgbQuad4[j-240].rgbBlue;
  166. }
  167. }
  168. BOOL CFile::WriteBitmap(char *szTitle, HBITMAP hbitmap, INT width, INT height)
  169. {
  170. BITMAPFILEHEADER fileHeader;
  171. BITMAPINFOHEADER infoHeader;
  172. memset(&fileHeader, 0, sizeof(fileHeader));
  173. memset(&infoHeader, 0, sizeof(infoHeader));
  174. fileHeader.bfType = 0x4d42;
  175. fileHeader.bfOffBits = sizeof(fileHeader) + sizeof(infoHeader);
  176. infoHeader.biSize = sizeof(infoHeader);
  177. infoHeader.biPlanes = 1;
  178. infoHeader.biWidth = width;
  179. infoHeader.biHeight = height;
  180. infoHeader.biBitCount = 24;
  181. infoHeader.biXPelsPerMeter = 3780;
  182. infoHeader.biYPelsPerMeter = 3780;
  183. BITMAPINFO bmInfo;
  184. bmInfo.bmiHeader = infoHeader;
  185. VOID* bits = malloc(infoHeader.biWidth*infoHeader.biHeight*3);
  186. BOOL success = FALSE;
  187. if(bits)
  188. {
  189. HDC hdc = CreateCompatibleDC(NULL);
  190. INT n = GetDIBits(hdc, hbitmap, 0, height,
  191. bits, &bmInfo, DIB_RGB_COLORS);
  192. infoHeader.biHeight = n;
  193. INT bitsSize = infoHeader.biHeight*infoHeader.biWidth*3;
  194. fileHeader.bfSize = sizeof(fileHeader) + sizeof(infoHeader)
  195. + bitsSize;
  196. DeleteDC(hdc);
  197. HANDLE hFile = CreateFileA(szTitle,
  198. GENERIC_WRITE,
  199. 0,
  200. NULL,
  201. CREATE_ALWAYS,
  202. FILE_ATTRIBUTE_NORMAL,
  203. NULL);
  204. BOOL deleteFile = FALSE;
  205. DWORD dwPtr;
  206. DWORD dwError;
  207. if(hFile == INVALID_HANDLE_VALUE)
  208. goto cleanUp2;
  209. dwPtr = SetFilePointer (hFile, 0, NULL, FILE_BEGIN) ;
  210. dwError;
  211. if(dwPtr == 0xFFFFFFFF && (dwError = GetLastError()) != NO_ERROR)
  212. {
  213. deleteFile = TRUE;
  214. goto cleanUp1;
  215. }
  216. DWORD bytesToWrite;
  217. DWORD bytesWritten;
  218. bytesToWrite = sizeof(fileHeader);
  219. success = WriteFile(
  220. hFile,
  221. &fileHeader,
  222. bytesToWrite,
  223. &bytesWritten,
  224. NULL);
  225. if(!success || bytesWritten != bytesToWrite)
  226. {
  227. success = FALSE;
  228. deleteFile = TRUE;
  229. goto cleanUp1;
  230. }
  231. bytesToWrite = sizeof(infoHeader);
  232. success = WriteFile(
  233. hFile,
  234. &infoHeader,
  235. bytesToWrite,
  236. &bytesWritten,
  237. NULL);
  238. if(!success || bytesWritten != bytesToWrite)
  239. {
  240. success = FALSE;
  241. deleteFile = TRUE;
  242. goto cleanUp1;
  243. }
  244. bytesToWrite = bitsSize;
  245. success = WriteFile(
  246. hFile,
  247. bits,
  248. bytesToWrite,
  249. &bytesWritten,
  250. NULL);
  251. if(!success || bytesWritten != bytesToWrite)
  252. {
  253. success = FALSE;
  254. deleteFile = TRUE;
  255. goto cleanUp1;
  256. }
  257. success = TRUE;
  258. cleanUp1:
  259. CloseHandle(hFile);
  260. if(deleteFile)
  261. DeleteFileA(szTitle);
  262. cleanUp2:
  263. free(bits);
  264. }
  265. return success;
  266. }