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.

316 lines
8.6 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: palette.c
  3. *
  4. * Palette support.
  5. *
  6. * Copyright (c) 1992-1995 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. DISPDBG((1, "flRed: %lx flGreen: %lx flBlue: %lx",
  92. ppdev->flRed, ppdev->flGreen, ppdev->flBlue));
  93. hpal = EngCreatePalette(PAL_BITFIELDS, 0, NULL,
  94. ppdev->flRed, ppdev->flGreen, ppdev->flBlue);
  95. }
  96. ppdev->hpalDefault = hpal;
  97. pdi->hpalDefault = hpal;
  98. if (hpal == 0)
  99. goto ReturnFalse;
  100. return(TRUE);
  101. ReturnFalse:
  102. DISPDBG((0, "Failed bInitializePalette"));
  103. return(FALSE);
  104. }
  105. /******************************Public*Routine******************************\
  106. * VOID vUninitializePalette
  107. *
  108. * Frees resources allocated by bInitializePalette.
  109. *
  110. * Note: In an error case, this may be called before bInitializePalette.
  111. *
  112. \**************************************************************************/
  113. VOID vUninitializePalette(PDEV* ppdev)
  114. {
  115. // Delete the default palette if we created one:
  116. if (ppdev->hpalDefault != 0)
  117. EngDeletePalette(ppdev->hpalDefault);
  118. if (ppdev->pPal != (PALETTEENTRY*) NULL)
  119. EngFreeMem(ppdev->pPal);
  120. }
  121. /******************************Public*Routine******************************\
  122. * BOOL bEnablePalette
  123. *
  124. * Initialize the hardware's 8bpp palette registers.
  125. *
  126. \**************************************************************************/
  127. BOOL bEnablePalette(PDEV* ppdev)
  128. {
  129. BYTE ajClutSpace[MAX_CLUT_SIZE];
  130. PVIDEO_CLUT pScreenClut;
  131. ULONG ulReturnedDataLength;
  132. ULONG cColors;
  133. PVIDEO_CLUTDATA pScreenClutData;
  134. if (ppdev->iBitmapFormat == BMF_8BPP)
  135. {
  136. // Fill in pScreenClut header info:
  137. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  138. pScreenClut->NumEntries = 256;
  139. pScreenClut->FirstEntry = 0;
  140. // Copy colours in:
  141. cColors = 256;
  142. pScreenClutData = (PVIDEO_CLUTDATA) (&(pScreenClut->LookupTable[0]));
  143. while(cColors--)
  144. {
  145. pScreenClutData[cColors].Red = ppdev->pPal[cColors].peRed >>
  146. ppdev->cPaletteShift;
  147. pScreenClutData[cColors].Green = ppdev->pPal[cColors].peGreen >>
  148. ppdev->cPaletteShift;
  149. pScreenClutData[cColors].Blue = ppdev->pPal[cColors].peBlue >>
  150. ppdev->cPaletteShift;
  151. pScreenClutData[cColors].Unused = 0;
  152. }
  153. // Set palette registers:
  154. if (EngDeviceIoControl(ppdev->hDriver,
  155. IOCTL_VIDEO_SET_COLOR_REGISTERS,
  156. pScreenClut,
  157. MAX_CLUT_SIZE,
  158. NULL,
  159. 0,
  160. &ulReturnedDataLength))
  161. {
  162. DISPDBG((0, "Failed bEnablePalette"));
  163. return(FALSE);
  164. }
  165. }
  166. DISPDBG((5, "Passed bEnablePalette"));
  167. return(TRUE);
  168. }
  169. /******************************Public*Routine******************************\
  170. * VOID vDisablePalette
  171. *
  172. * Undoes anything done in bEnablePalette.
  173. *
  174. \**************************************************************************/
  175. VOID vDisablePalette(
  176. PDEV* ppdev)
  177. {
  178. // Nothin' to do
  179. }
  180. /******************************Public*Routine******************************\
  181. * VOID vAssertModePalette
  182. *
  183. * Sets/resets the palette in preparation for full-screen/graphics mode.
  184. *
  185. \**************************************************************************/
  186. VOID vAssertModePalette(
  187. PDEV* ppdev,
  188. BOOL bEnable)
  189. {
  190. // USER immediately calls DrvSetPalette after switching out of
  191. // full-screen, so we don't have to worry about resetting the
  192. // palette here.
  193. }
  194. /******************************Public*Routine******************************\
  195. * BOOL DrvSetPalette
  196. *
  197. * DDI entry point for manipulating the palette.
  198. *
  199. \**************************************************************************/
  200. BOOL DrvSetPalette(
  201. DHPDEV dhpdev,
  202. PALOBJ* ppalo,
  203. FLONG fl,
  204. ULONG iStart,
  205. ULONG cColors)
  206. {
  207. BYTE ajClutSpace[MAX_CLUT_SIZE];
  208. PVIDEO_CLUT pScreenClut;
  209. PVIDEO_CLUTDATA pScreenClutData;
  210. PDEV* ppdev;
  211. UNREFERENCED_PARAMETER(fl);
  212. ppdev = (PDEV*) dhpdev;
  213. // Fill in pScreenClut header info:
  214. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  215. pScreenClut->NumEntries = (USHORT) cColors;
  216. pScreenClut->FirstEntry = (USHORT) iStart;
  217. pScreenClutData = (PVIDEO_CLUTDATA) (&(pScreenClut->LookupTable[0]));
  218. if (cColors != PALOBJ_cGetColors(ppalo, iStart, cColors,
  219. (ULONG*) pScreenClutData))
  220. {
  221. DISPDBG((0, "DrvSetPalette failed PALOBJ_cGetColors\n"));
  222. return (FALSE);
  223. }
  224. // Set the high reserved byte in each palette entry to 0.
  225. // Do the appropriate palette shifting to fit in the DAC.
  226. if (ppdev->cPaletteShift)
  227. {
  228. while(cColors--)
  229. {
  230. pScreenClutData[cColors].Red >>= ppdev->cPaletteShift;
  231. pScreenClutData[cColors].Green >>= ppdev->cPaletteShift;
  232. pScreenClutData[cColors].Blue >>= ppdev->cPaletteShift;
  233. pScreenClutData[cColors].Unused = 0;
  234. }
  235. }
  236. else
  237. {
  238. while(cColors--)
  239. {
  240. pScreenClutData[cColors].Unused = 0;
  241. }
  242. }
  243. // Set palette registers
  244. if (EngDeviceIoControl(ppdev->hDriver,
  245. IOCTL_VIDEO_SET_COLOR_REGISTERS,
  246. pScreenClut,
  247. MAX_CLUT_SIZE,
  248. NULL,
  249. 0,
  250. &cColors))
  251. {
  252. DISPDBG((0, "DrvSetPalette failed EngDeviceIoControl\n"));
  253. return (FALSE);
  254. }
  255. return(TRUE);
  256. }