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.

337 lines
9.5 KiB

  1. /******************************Module*Header*******************************\
  2. *
  3. * *******************
  4. * * GDI SAMPLE CODE *
  5. * *******************
  6. *
  7. * Module Name: palette.c
  8. *
  9. * Palette support.
  10. *
  11. * Copyright (c) 1992-1998 Microsoft Corporation
  12. \**************************************************************************/
  13. #include "driver.h"
  14. // Global Table defining the 20 Window Default Colors. For 256 color
  15. // palettes the first 10 must be put at the beginning of the palette
  16. // and the last 10 at the end of the palette.
  17. const PALETTEENTRY BASEPALETTE[20] =
  18. {
  19. { 0, 0, 0, 0 }, // 0
  20. { 0x80,0, 0, 0 }, // 1
  21. { 0, 0x80,0, 0 }, // 2
  22. { 0x80,0x80,0, 0 }, // 3
  23. { 0, 0, 0x80,0 }, // 4
  24. { 0x80,0, 0x80,0 }, // 5
  25. { 0, 0x80,0x80,0 }, // 6
  26. { 0xC0,0xC0,0xC0,0 }, // 7
  27. { 192, 220, 192, 0 }, // 8
  28. { 166, 202, 240, 0 }, // 9
  29. { 255, 251, 240, 0 }, // 10
  30. { 160, 160, 164, 0 }, // 11
  31. { 0x80,0x80,0x80,0 }, // 12
  32. { 0xFF,0, 0 ,0 }, // 13
  33. { 0, 0xFF,0 ,0 }, // 14
  34. { 0xFF,0xFF,0 ,0 }, // 15
  35. { 0 ,0, 0xFF,0 }, // 16
  36. { 0xFF,0, 0xFF,0 }, // 17
  37. { 0, 0xFF,0xFF,0 }, // 18
  38. { 0xFF,0xFF,0xFF,0 }, // 19
  39. };
  40. BOOL bInitDefaultPalette(PPDEV ppdev, DEVINFO *pDevInfo);
  41. /******************************Public*Routine******************************\
  42. * bInitPaletteInfo
  43. *
  44. * Initializes the palette information for this PDEV.
  45. *
  46. * Called by DrvEnablePDEV.
  47. *
  48. \**************************************************************************/
  49. BOOL bInitPaletteInfo(PPDEV ppdev, DEVINFO *pDevInfo)
  50. {
  51. if (!bInitDefaultPalette(ppdev, pDevInfo))
  52. return(FALSE);
  53. return(TRUE);
  54. }
  55. /******************************Public*Routine******************************\
  56. * vDisablePalette
  57. *
  58. * Frees resources allocated by bInitPaletteInfo.
  59. *
  60. \**************************************************************************/
  61. VOID vDisablePalette(PPDEV ppdev)
  62. {
  63. // Delete the default palette if we created one.
  64. if (ppdev->hpalDefault)
  65. {
  66. EngDeletePalette(ppdev->hpalDefault);
  67. ppdev->hpalDefault = (HPALETTE) 0;
  68. }
  69. if (ppdev->pPal != (PPALETTEENTRY)NULL)
  70. EngFreeMem((PVOID)ppdev->pPal);
  71. }
  72. /******************************Public*Routine******************************\
  73. * bInitDefaultPalette
  74. *
  75. * Initializes default palette for PDEV.
  76. *
  77. \**************************************************************************/
  78. BOOL bInitDefaultPalette(PPDEV ppdev, DEVINFO *pDevInfo)
  79. {
  80. if (ppdev->ulBitCount == 8)
  81. {
  82. ULONG ulLoop;
  83. BYTE jRed,jGre,jBlu;
  84. //
  85. // Allocate our palette
  86. //
  87. ppdev->pPal = (PPALETTEENTRY)EngAllocMem(0, sizeof(PALETTEENTRY) * 256,
  88. ALLOC_TAG);
  89. if ((ppdev->pPal) == NULL) {
  90. DISPDBG((0, "DISP bInitDefaultPalette() failed EngAllocMem\n"));
  91. return(FALSE);
  92. }
  93. //
  94. // Generate 256 (8*4*4) RGB combinations to fill the palette
  95. //
  96. jRed = jGre = jBlu = 0;
  97. for (ulLoop = 0; ulLoop < 256; ulLoop++)
  98. {
  99. ppdev->pPal[ulLoop].peRed = jRed;
  100. ppdev->pPal[ulLoop].peGreen = jGre;
  101. ppdev->pPal[ulLoop].peBlue = jBlu;
  102. ppdev->pPal[ulLoop].peFlags = (BYTE)0;
  103. if (!(jRed += 32))
  104. if (!(jGre += 32))
  105. jBlu += 64;
  106. }
  107. //
  108. // Fill in Windows Reserved Colors from the WIN 3.0 DDK
  109. // The Window Manager reserved the first and last 10 colors for
  110. // painting windows borders and for non-palette managed applications.
  111. //
  112. for (ulLoop = 0; ulLoop < 10; ulLoop++)
  113. {
  114. //
  115. // First 10
  116. //
  117. ppdev->pPal[ulLoop] = BASEPALETTE[ulLoop];
  118. //
  119. // Last 10
  120. //
  121. ppdev->pPal[246 + ulLoop] = BASEPALETTE[ulLoop+10];
  122. }
  123. //
  124. // Create handle for palette.
  125. //
  126. ppdev->hpalDefault =
  127. pDevInfo->hpalDefault = EngCreatePalette(PAL_INDEXED,
  128. 256,
  129. (PULONG) ppdev->pPal,
  130. 0,0,0);
  131. if (ppdev->hpalDefault == (HPALETTE) 0)
  132. {
  133. DISPDBG((0, "DISP bInitDefaultPalette failed EngCreatePalette\n"));
  134. EngFreeMem(ppdev->pPal);
  135. return(FALSE);
  136. }
  137. //
  138. // Initialize the hardware with the initial palette.
  139. //
  140. return(TRUE);
  141. } else {
  142. ppdev->hpalDefault =
  143. pDevInfo->hpalDefault = EngCreatePalette(PAL_BITFIELDS,
  144. 0,(PULONG) NULL,
  145. ppdev->flRed,
  146. ppdev->flGreen,
  147. ppdev->flBlue);
  148. if (ppdev->hpalDefault == (HPALETTE) 0)
  149. {
  150. DISPDBG((0, "DISP bInitDefaultPalette failed EngCreatePalette\n"));
  151. return(FALSE);
  152. }
  153. }
  154. return(TRUE);
  155. }
  156. /******************************Public*Routine******************************\
  157. * bInit256ColorPalette
  158. *
  159. * Initialize the hardware's palette registers.
  160. *
  161. \**************************************************************************/
  162. BOOL bInit256ColorPalette(PPDEV ppdev)
  163. {
  164. BYTE ajClutSpace[MAX_CLUT_SIZE];
  165. PVIDEO_CLUT pScreenClut;
  166. ULONG ulReturnedDataLength;
  167. ULONG cColors;
  168. PVIDEO_CLUTDATA pScreenClutData;
  169. if (ppdev->ulBitCount == 8)
  170. {
  171. //
  172. // Fill in pScreenClut header info:
  173. //
  174. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  175. pScreenClut->NumEntries = 256;
  176. pScreenClut->FirstEntry = 0;
  177. //
  178. // Copy colours in:
  179. //
  180. cColors = 256;
  181. pScreenClutData = (PVIDEO_CLUTDATA) (&(pScreenClut->LookupTable[0]));
  182. while(cColors--)
  183. {
  184. pScreenClutData[cColors].Red = ppdev->pPal[cColors].peRed >>
  185. ppdev->cPaletteShift;
  186. pScreenClutData[cColors].Green = ppdev->pPal[cColors].peGreen >>
  187. ppdev->cPaletteShift;
  188. pScreenClutData[cColors].Blue = ppdev->pPal[cColors].peBlue >>
  189. ppdev->cPaletteShift;
  190. pScreenClutData[cColors].Unused = 0;
  191. }
  192. //
  193. // Set palette registers:
  194. //
  195. if (EngDeviceIoControl(ppdev->hDriver,
  196. IOCTL_VIDEO_SET_COLOR_REGISTERS,
  197. pScreenClut,
  198. MAX_CLUT_SIZE,
  199. NULL,
  200. 0,
  201. &ulReturnedDataLength))
  202. {
  203. DISPDBG((0, "Failed bEnablePalette"));
  204. return(FALSE);
  205. }
  206. }
  207. DISPDBG((5, "Passed bEnablePalette"));
  208. return(TRUE);
  209. }
  210. /******************************Public*Routine******************************\
  211. * DrvSetPalette
  212. *
  213. * DDI entry point for manipulating the palette.
  214. *
  215. \**************************************************************************/
  216. BOOL DrvSetPalette(
  217. DHPDEV dhpdev,
  218. PALOBJ* ppalo,
  219. FLONG fl,
  220. ULONG iStart,
  221. ULONG cColors)
  222. {
  223. BYTE ajClutSpace[MAX_CLUT_SIZE];
  224. PVIDEO_CLUT pScreenClut;
  225. PVIDEO_CLUTDATA pScreenClutData;
  226. PDEV* ppdev;
  227. UNREFERENCED_PARAMETER(fl);
  228. ppdev = (PDEV*) dhpdev;
  229. //
  230. // Fill in pScreenClut header info:
  231. //
  232. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  233. pScreenClut->NumEntries = (USHORT) cColors;
  234. pScreenClut->FirstEntry = (USHORT) iStart;
  235. pScreenClutData = (PVIDEO_CLUTDATA) (&(pScreenClut->LookupTable[0]));
  236. if (cColors != PALOBJ_cGetColors(ppalo, iStart, cColors,
  237. (ULONG*) pScreenClutData))
  238. {
  239. DISPDBG((0, "DrvSetPalette failed PALOBJ_cGetColors\n"));
  240. return (FALSE);
  241. }
  242. //
  243. // Set the high reserved byte in each palette entry to 0.
  244. // Do the appropriate palette shifting to fit in the DAC.
  245. //
  246. if (ppdev->cPaletteShift)
  247. {
  248. while(cColors--)
  249. {
  250. pScreenClutData[cColors].Red >>= ppdev->cPaletteShift;
  251. pScreenClutData[cColors].Green >>= ppdev->cPaletteShift;
  252. pScreenClutData[cColors].Blue >>= ppdev->cPaletteShift;
  253. pScreenClutData[cColors].Unused = 0;
  254. }
  255. }
  256. else
  257. {
  258. while(cColors--)
  259. {
  260. pScreenClutData[cColors].Unused = 0;
  261. }
  262. }
  263. //
  264. // Set palette registers
  265. //
  266. if (EngDeviceIoControl(ppdev->hDriver,
  267. IOCTL_VIDEO_SET_COLOR_REGISTERS,
  268. pScreenClut,
  269. MAX_CLUT_SIZE,
  270. NULL,
  271. 0,
  272. &cColors))
  273. {
  274. DISPDBG((0, "DrvSetPalette failed EngDeviceIoControl\n"));
  275. return (FALSE);
  276. }
  277. return(TRUE);
  278. }