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.

310 lines
8.1 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(0, sizeof(PALETTEENTRY) * 256, ALLOC_TAG);
  56. if (ppal == NULL)
  57. {
  58. goto ReturnFalse;
  59. }
  60. ppdev->pPal = ppal;
  61. // Generate 256 (8*4*4) RGB combinations to fill the palette
  62. jRed = 0;
  63. jGre = 0;
  64. jBlu = 0;
  65. ppalTmp = ppal;
  66. for (ulLoop = 256; ulLoop != 0; ulLoop--)
  67. {
  68. ppalTmp->peRed = jRed;
  69. ppalTmp->peGreen = jGre;
  70. ppalTmp->peBlue = jBlu;
  71. ppalTmp->peFlags = 0;
  72. ppalTmp++;
  73. if (!(jRed += 32))
  74. if (!(jGre += 32))
  75. jBlu += 64;
  76. }
  77. // Fill in Windows reserved colours from the WIN 3.0 DDK
  78. // The Window Manager reserved the first and last 10 colours for
  79. // painting windows borders and for non-palette managed applications.
  80. for (ulLoop = 0; ulLoop < 10; ulLoop++)
  81. {
  82. // First 10
  83. ppal[ulLoop] = gapalBase[ulLoop];
  84. // Last 10
  85. ppal[246 + ulLoop] = gapalBase[ulLoop+10];
  86. }
  87. // Create handle for palette.
  88. hpal = EngCreatePalette(PAL_INDEXED, 256, (ULONG*) ppal, 0, 0, 0);
  89. }
  90. else
  91. {
  92. ASSERTDD((ppdev->iBitmapFormat == BMF_16BPP) ||
  93. (ppdev->iBitmapFormat == BMF_24BPP),
  94. "This case handles only 16 or 24bpp");
  95. hpal = EngCreatePalette(PAL_BITFIELDS, 0, NULL,
  96. ppdev->flRed, ppdev->flGreen, ppdev->flBlue);
  97. }
  98. ppdev->hpalDefault = hpal;
  99. pdi->hpalDefault = hpal;
  100. if (hpal == 0)
  101. goto ReturnFalse;
  102. return(TRUE);
  103. ReturnFalse:
  104. DISPDBG((0, "Failed bInitializePalette"));
  105. return(FALSE);
  106. }
  107. /******************************Public*Routine******************************\
  108. * VOID vUninitializePalette
  109. *
  110. * Frees resources allocated by bInitializePalette.
  111. *
  112. * Note: In an error case, this may be called before bInitializePalette.
  113. *
  114. \**************************************************************************/
  115. VOID vUninitializePalette(PDEV* ppdev)
  116. {
  117. // Delete the default palette if we created one:
  118. if (ppdev->hpalDefault != 0)
  119. EngDeletePalette(ppdev->hpalDefault);
  120. if (ppdev->pPal != (PALETTEENTRY*) NULL)
  121. EngFreeMem(ppdev->pPal);
  122. }
  123. /******************************Public*Routine******************************\
  124. * BOOL bEnablePalette
  125. *
  126. * Initialize the hardware's 8bpp palette registers.
  127. *
  128. \**************************************************************************/
  129. BOOL bEnablePalette(PDEV* ppdev)
  130. {
  131. BYTE ajClutSpace[MAX_CLUT_SIZE];
  132. PVIDEO_CLUT pScreenClut;
  133. ULONG ulReturnedDataLength;
  134. PALETTEENTRY* ppalFrom;
  135. PALETTEENTRY* ppalTo;
  136. PALETTEENTRY* ppalEnd;
  137. if (ppdev->iBitmapFormat == BMF_8BPP)
  138. {
  139. // Fill in pScreenClut header info:
  140. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  141. pScreenClut->NumEntries = 256;
  142. pScreenClut->FirstEntry = 0;
  143. // Copy Colors in.
  144. ppalFrom = ppdev->pPal;
  145. ppalTo = (PALETTEENTRY*) pScreenClut->LookupTable;
  146. ppalEnd = &ppalTo[256];
  147. for (; ppalTo < ppalEnd; ppalFrom++, ppalTo++)
  148. {
  149. ppalTo->peRed = ppalFrom->peRed >> 2;
  150. ppalTo->peGreen = ppalFrom->peGreen >> 2;
  151. ppalTo->peBlue = ppalFrom->peBlue >> 2;
  152. ppalTo->peFlags = 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. PALETTEENTRY* ppal;
  211. PALETTEENTRY* ppalEnd;
  212. PDEV* ppdev;
  213. UNREFERENCED_PARAMETER(fl);
  214. // Fill in pScreenClut header info:
  215. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  216. pScreenClut->NumEntries = (USHORT) cColors;
  217. pScreenClut->FirstEntry = (USHORT) iStart;
  218. ppal = (PPALETTEENTRY) (pScreenClut->LookupTable);
  219. if (cColors != PALOBJ_cGetColors(ppalo, iStart, cColors, (ULONG*) ppal))
  220. {
  221. DISPDBG((0, "DrvSetPalette failed PALOBJ_cGetColors\n"));
  222. goto ReturnFalse;
  223. }
  224. // Set the high reserved byte in each palette entry to 0.
  225. for (ppalEnd = &ppal[cColors]; ppal < ppalEnd; ppal++)
  226. {
  227. ppal->peRed >>= 2;
  228. ppal->peGreen >>= 2;
  229. ppal->peBlue >>= 2;
  230. ppal->peFlags = 0;
  231. }
  232. // Set palette registers
  233. ppdev = (PDEV*) dhpdev;
  234. if (EngDeviceIoControl(ppdev->hDriver,
  235. IOCTL_VIDEO_SET_COLOR_REGISTERS,
  236. pScreenClut,
  237. MAX_CLUT_SIZE,
  238. NULL,
  239. 0,
  240. &cColors))
  241. {
  242. DISPDBG((0, "DrvSetPalette failed EngDeviceIoControl\n"));
  243. goto ReturnFalse;
  244. }
  245. return(TRUE);
  246. ReturnFalse:
  247. return(FALSE);
  248. }