Source code of Windows XP (NT5)
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.

317 lines
8.6 KiB

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