2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: GDI Palette Functions
5 * FILE: subsystems/win32/win32k/objects/palette.c
6 * PROGRAMERS: Jason Filby
15 static UINT SystemPaletteUse
= SYSPAL_NOSTATIC
; /* The program need save the pallete and restore it */
17 PALETTE gpalRGB
, gpalBGR
, gpalMono
, gpalRGB555
, gpalRGB565
, *gppalDefault
;
18 PPALETTE appalSurfaceDefault
[11];
20 const PALETTEENTRY g_sysPalTemplate
[NB_RESERVED_COLORS
] =
22 // First 10 entries in the system palette
23 // Red Green Blue Flags
24 { 0x00, 0x00, 0x00, PC_SYS_USED
},
25 { 0x80, 0x00, 0x00, PC_SYS_USED
},
26 { 0x00, 0x80, 0x00, PC_SYS_USED
},
27 { 0x80, 0x80, 0x00, PC_SYS_USED
},
28 { 0x00, 0x00, 0x80, PC_SYS_USED
},
29 { 0x80, 0x00, 0x80, PC_SYS_USED
},
30 { 0x00, 0x80, 0x80, PC_SYS_USED
},
31 { 0xc0, 0xc0, 0xc0, PC_SYS_USED
},
32 { 0xc0, 0xdc, 0xc0, PC_SYS_USED
},
33 { 0xa6, 0xca, 0xf0, PC_SYS_USED
},
35 // ... c_min/2 dynamic colorcells
36 // ... gap (for sparse palettes)
37 // ... c_min/2 dynamic colorcells
39 { 0xff, 0xfb, 0xf0, PC_SYS_USED
},
40 { 0xa0, 0xa0, 0xa4, PC_SYS_USED
},
41 { 0x80, 0x80, 0x80, PC_SYS_USED
},
42 { 0xff, 0x00, 0x00, PC_SYS_USED
},
43 { 0x00, 0xff, 0x00, PC_SYS_USED
},
44 { 0xff, 0xff, 0x00, PC_SYS_USED
},
45 { 0x00, 0x00, 0xff, PC_SYS_USED
},
46 { 0xff, 0x00, 0xff, PC_SYS_USED
},
47 { 0x00, 0xff, 0xff, PC_SYS_USED
},
48 { 0xff, 0xff, 0xff, PC_SYS_USED
} // Last 10
51 unsigned short GetNumberOfBits(unsigned int dwMask
)
54 for (wBits
= 0; dwMask
; dwMask
= dwMask
& (dwMask
- 1))
59 // Create the system palette
69 // Create default palette (20 system colors)
70 palPtr
= ExAllocatePoolWithTag(PagedPool
,
72 (NB_RESERVED_COLORS
* sizeof(PALETTEENTRY
)),
74 if (!palPtr
) return STATUS_NO_MEMORY
;
76 palPtr
->palVersion
= 0x300;
77 palPtr
->palNumEntries
= NB_RESERVED_COLORS
;
78 for (i
=0; i
<NB_RESERVED_COLORS
; i
++)
80 palPtr
->palPalEntry
[i
].peRed
= g_sysPalTemplate
[i
].peRed
;
81 palPtr
->palPalEntry
[i
].peGreen
= g_sysPalTemplate
[i
].peGreen
;
82 palPtr
->palPalEntry
[i
].peBlue
= g_sysPalTemplate
[i
].peBlue
;
83 palPtr
->palPalEntry
[i
].peFlags
= 0;
86 hpalette
= NtGdiCreatePaletteInternal(palPtr
,NB_RESERVED_COLORS
);
87 ExFreePoolWithTag(palPtr
, TAG_PALETTE
);
89 /* palette_size = visual->map_entries; */
91 gpalRGB
.flFlags
= PAL_RGB
;
92 gpalRGB
.RedMask
= RGB(0xFF, 0x00, 0x00);
93 gpalRGB
.GreenMask
= RGB(0x00, 0xFF, 0x00);
94 gpalRGB
.BlueMask
= RGB(0x00, 0x00, 0xFF);
95 gpalRGB
.BaseObject
.ulShareCount
= 1;
96 gpalRGB
.BaseObject
.BaseFlags
= 0 ;
98 gpalBGR
.flFlags
= PAL_BGR
;
99 gpalBGR
.RedMask
= RGB(0x00, 0x00, 0xFF);
100 gpalBGR
.GreenMask
= RGB(0x00, 0xFF, 0x00);
101 gpalBGR
.BlueMask
= RGB(0xFF, 0x00, 0x00);
102 gpalBGR
.BaseObject
.ulShareCount
= 1;
103 gpalBGR
.BaseObject
.BaseFlags
= 0 ;
105 gpalRGB555
.flFlags
= PAL_RGB16_555
| PAL_BITFIELDS
;
106 gpalRGB555
.RedMask
= 0x7C00;
107 gpalRGB555
.GreenMask
= 0x3E0;
108 gpalRGB555
.BlueMask
= 0x1F;
109 gpalRGB555
.BaseObject
.ulShareCount
= 1;
110 gpalRGB555
.BaseObject
.BaseFlags
= 0 ;
112 gpalRGB565
.flFlags
= PAL_RGB16_565
| PAL_BITFIELDS
;
113 gpalRGB565
.RedMask
= 0xF800;
114 gpalRGB565
.GreenMask
= 0x7E0;
115 gpalRGB565
.BlueMask
= 0x1F;
116 gpalRGB565
.BaseObject
.ulShareCount
= 1;
117 gpalRGB565
.BaseObject
.BaseFlags
= 0 ;
119 memset(&gpalMono
, 0, sizeof(PALETTE
));
120 gpalMono
.flFlags
= PAL_MONOCHROME
;
121 gpalMono
.BaseObject
.ulShareCount
= 1;
122 gpalMono
.BaseObject
.BaseFlags
= 0 ;
124 /* Initialize default surface palettes */
125 gppalDefault
= PALETTE_ShareLockPalette(hpalette
);
126 appalSurfaceDefault
[BMF_1BPP
] = &gpalMono
;
127 appalSurfaceDefault
[BMF_4BPP
] = gppalDefault
;
128 appalSurfaceDefault
[BMF_8BPP
] = gppalDefault
;
129 appalSurfaceDefault
[BMF_16BPP
] = &gpalRGB565
;
130 appalSurfaceDefault
[BMF_24BPP
] = &gpalBGR
;
131 appalSurfaceDefault
[BMF_32BPP
] = &gpalBGR
;
132 appalSurfaceDefault
[BMF_4RLE
] = gppalDefault
;
133 appalSurfaceDefault
[BMF_8RLE
] = gppalDefault
;
134 appalSurfaceDefault
[BMF_JPEG
] = &gpalRGB
;
135 appalSurfaceDefault
[BMF_PNG
] = &gpalRGB
;
137 return STATUS_SUCCESS
;
140 VOID FASTCALL
PALETTE_ValidateFlags(PALETTEENTRY
* lpPalE
, INT size
)
144 lpPalE
[i
].peFlags
= PC_SYS_USED
| (lpPalE
[i
].peFlags
& 0x07);
149 PALETTE_AllocPalette2(ULONG Mode
,
158 PalGDI
= (PPALETTE
)GDIOBJ_AllocateObject(GDIObjType_PAL_TYPE
,
163 DPRINT1("Could not allocate a palette.\n");
167 PalGDI
->Self
= PalGDI
->BaseObject
.hHmgr
;
168 PalGDI
->flFlags
= Mode
;
172 PalGDI
->IndexedColors
= ExAllocatePoolWithTag(PagedPool
,
173 sizeof(PALETTEENTRY
) * NumColors
,
175 if (NULL
== PalGDI
->IndexedColors
)
177 GDIOBJ_vDeleteObject(&PalGDI
->BaseObject
);
180 RtlCopyMemory(PalGDI
->IndexedColors
, Colors
, sizeof(PALETTEENTRY
) * NumColors
);
183 if (Mode
& PAL_INDEXED
)
185 PalGDI
->NumColors
= NumColors
;
187 else if (Mode
& PAL_BITFIELDS
)
189 PalGDI
->RedMask
= Red
;
190 PalGDI
->GreenMask
= Green
;
191 PalGDI
->BlueMask
= Blue
;
193 if (Red
== 0x7c00 && Green
== 0x3E0 && Blue
== 0x1F)
194 PalGDI
->flFlags
|= PAL_RGB16_555
;
195 else if (Red
== 0xF800 && Green
== 0x7E0 && Blue
== 0x1F)
196 PalGDI
->flFlags
|= PAL_RGB16_565
;
197 else if (Red
== 0xFF0000 && Green
== 0xFF00 && Blue
== 0xFF)
198 PalGDI
->flFlags
|= PAL_BGR
;
199 else if (Red
== 0xFF && Green
== 0xFF00 && Blue
== 0xFF0000)
200 PalGDI
->flFlags
|= PAL_RGB
;
208 PALETTE_AllocPalette(ULONG Mode
,
218 ppal
= PALETTE_AllocPalette2(Mode
, NumColors
, Colors
, Red
, Green
, Blue
);
219 if (!ppal
) return NULL
;
221 hpal
= GDIOBJ_hInsertObject(&ppal
->BaseObject
, GDI_OBJ_HMGR_POWNED
);
224 DPRINT1("Could not insert palette into handle table.\n");
225 GDIOBJ_vFreeObject(&ppal
->BaseObject
);
229 PALETTE_UnlockPalette(ppal
);
236 PALETTE_AllocPaletteIndexedRGB(ULONG NumColors
,
237 CONST RGBQUAD
*Colors
)
243 PalGDI
= (PPALETTE
)GDIOBJ_AllocateObject(GDIObjType_PAL_TYPE
,
248 DPRINT1("Could not allocate a palette.\n");
252 if (!GDIOBJ_hInsertObject(&PalGDI
->BaseObject
, GDI_OBJ_HMGR_POWNED
))
254 DPRINT1("Could not insert palette into handle table.\n");
255 GDIOBJ_vFreeObject(&PalGDI
->BaseObject
);
259 NewPalette
= PalGDI
->BaseObject
.hHmgr
;
261 PalGDI
->Self
= NewPalette
;
262 PalGDI
->flFlags
= PAL_INDEXED
;
264 PalGDI
->IndexedColors
= ExAllocatePoolWithTag(PagedPool
,
265 sizeof(PALETTEENTRY
) * NumColors
,
267 if (NULL
== PalGDI
->IndexedColors
)
269 GDIOBJ_vDeleteObject(&PalGDI
->BaseObject
);
273 for (i
= 0; i
< NumColors
; i
++)
275 PalGDI
->IndexedColors
[i
].peRed
= Colors
[i
].rgbRed
;
276 PalGDI
->IndexedColors
[i
].peGreen
= Colors
[i
].rgbGreen
;
277 PalGDI
->IndexedColors
[i
].peBlue
= Colors
[i
].rgbBlue
;
278 PalGDI
->IndexedColors
[i
].peFlags
= 0;
281 PalGDI
->NumColors
= NumColors
;
283 PALETTE_UnlockPalette(PalGDI
);
289 PALETTE_Cleanup(PVOID ObjectBody
)
291 PPALETTE pPal
= (PPALETTE
)ObjectBody
;
292 if (NULL
!= pPal
->IndexedColors
)
294 ExFreePoolWithTag(pPal
->IndexedColors
, TAG_PALETTE
);
301 PALETTE_GetObject(PPALETTE ppal
, INT cbCount
, LPLOGBRUSH lpBuffer
)
308 if ((UINT
)cbCount
< sizeof(WORD
)) return 0;
309 *((WORD
*)lpBuffer
) = (WORD
)ppal
->NumColors
;
315 PALETTE_ulGetNearestPaletteIndex(PALETTE
* ppal
, ULONG iColor
)
317 ULONG ulDiff
, ulColorDiff
, ulMinimalDiff
= 0xFFFFFF;
318 ULONG i
, ulBestIndex
= 0;
319 PALETTEENTRY peColor
= *(PPALETTEENTRY
)&iColor
;
321 /* Loop all palette entries, break on exact match */
322 for (i
= 0; i
< ppal
->NumColors
&& ulMinimalDiff
!= 0; i
++)
324 /* Calculate distance in the color cube */
325 ulDiff
= peColor
.peRed
- ppal
->IndexedColors
[i
].peRed
;
326 ulColorDiff
= ulDiff
* ulDiff
;
327 ulDiff
= peColor
.peGreen
- ppal
->IndexedColors
[i
].peGreen
;
328 ulColorDiff
+= ulDiff
* ulDiff
;
329 ulDiff
= peColor
.peBlue
- ppal
->IndexedColors
[i
].peBlue
;
330 ulColorDiff
+= ulDiff
* ulDiff
;
332 /* Check for a better match */
333 if (ulColorDiff
< ulMinimalDiff
)
336 ulMinimalDiff
= ulColorDiff
;
345 PALETTE_ulGetNearestBitFieldsIndex(PALETTE
* ppal
, ULONG ulColor
)
349 // FIXME: HACK, should be stored already
350 ppal
->ulRedShift
= CalculateShift(RGB(0xff,0,0), ppal
->RedMask
);
351 ppal
->ulGreenShift
= CalculateShift(RGB(0,0xff,0), ppal
->GreenMask
);
352 ppal
->ulBlueShift
= CalculateShift(RGB(0,0,0xff), ppal
->BlueMask
);
354 ulNewColor
= _rotl(ulColor
, ppal
->ulRedShift
) & ppal
->RedMask
;
355 ulNewColor
|= _rotl(ulColor
, ppal
->ulGreenShift
) & ppal
->GreenMask
;
356 ulNewColor
|= _rotl(ulColor
, ppal
->ulBlueShift
) & ppal
->BlueMask
;
363 PALETTE_ulGetNearestIndex(PALETTE
* ppal
, ULONG ulColor
)
365 if (ppal
->flFlags
& PAL_INDEXED
) // Use fl & PALINDEXED
366 return PALETTE_ulGetNearestPaletteIndex(ppal
, ulColor
);
368 return PALETTE_ulGetNearestBitFieldsIndex(ppal
, ulColor
);
373 PALETTE_vGetBitMasks(PPALETTE ppal
, PULONG pulColors
)
377 if (ppal
->flFlags
& PAL_INDEXED
|| ppal
->flFlags
& PAL_RGB
)
379 pulColors
[0] = RGB(0xFF, 0x00, 0x00);
380 pulColors
[1] = RGB(0x00, 0xFF, 0x00);
381 pulColors
[2] = RGB(0x00, 0x00, 0xFF);
383 else if (ppal
->flFlags
& PAL_BGR
)
385 pulColors
[0] = RGB(0x00, 0x00, 0xFF);
386 pulColors
[1] = RGB(0x00, 0xFF, 0x00);
387 pulColors
[2] = RGB(0xFF, 0x00, 0x00);
389 else if (ppal
->flFlags
& PAL_BITFIELDS
)
391 pulColors
[0] = ppal
->RedMask
;
392 pulColors
[1] = ppal
->GreenMask
;
393 pulColors
[2] = ppal
->BlueMask
;
399 ColorCorrection(PPALETTE PalGDI
, PPALETTEENTRY PaletteEntry
, ULONG Colors
)
401 PPDEVOBJ ppdev
= (PPDEVOBJ
)PalGDI
->hPDev
;
405 if (ppdev
->flFlags
& PDEV_GAMMARAMP_TABLE
)
408 PGAMMARAMP GammaRamp
= (PGAMMARAMP
)ppdev
->pvGammaRamp
;
409 for ( i
= 0; i
< Colors
; i
++)
411 PaletteEntry
[i
].peRed
+= GammaRamp
->Red
[i
];
412 PaletteEntry
[i
].peGreen
+= GammaRamp
->Green
[i
];
413 PaletteEntry
[i
].peBlue
+= GammaRamp
->Blue
[i
];
419 /** Display Driver Interface **************************************************/
437 ppal
= PALETTE_AllocPalette2(iMode
, cColors
, pulColors
, flRed
, flGreen
, flBlue
);
438 if (!ppal
) return NULL
;
440 hpal
= GDIOBJ_hInsertObject(&ppal
->BaseObject
, GDI_OBJ_HMGR_PUBLIC
);
443 DPRINT1("Could not insert palette into handle table.\n");
444 GDIOBJ_vFreeObject(&ppal
->BaseObject
);
448 PALETTE_UnlockPalette(ppal
);
457 EngDeletePalette(IN HPALETTE hpal
)
461 ppal
= PALETTE_ShareLockPalette(hpal
);
462 if (!ppal
) return FALSE
;
464 GDIOBJ_vDeleteObject(&ppal
->BaseObject
);
474 PALOBJ_cGetColors(PALOBJ
*PalObj
, ULONG Start
, ULONG Colors
, ULONG
*PaletteEntry
)
478 PalGDI
= (PALETTE
*)PalObj
;
479 /* PalGDI = (PALETTE*)AccessInternalObjectFromUserObject(PalObj); */
481 if (Start
>= PalGDI
->NumColors
)
484 Colors
= min(Colors
, PalGDI
->NumColors
- Start
);
486 /* NOTE: PaletteEntry ULONGs are in the same order as PALETTEENTRY. */
487 RtlCopyMemory(PaletteEntry
, PalGDI
->IndexedColors
+ Start
, sizeof(ULONG
) * Colors
);
489 if (PalGDI
->flFlags
& PAL_GAMMACORRECTION
)
490 ColorCorrection(PalGDI
, (PPALETTEENTRY
)PaletteEntry
, Colors
);
496 /** Systemcall Interface ******************************************************/
502 NtGdiCreatePaletteInternal ( IN LPLOGPALETTE pLogPal
, IN UINT cEntries
)
507 pLogPal
->palNumEntries
= cEntries
;
508 NewPalette
= PALETTE_AllocPalette( PAL_INDEXED
,
510 (PULONG
)pLogPal
->palPalEntry
,
513 if (NewPalette
== NULL
)
518 PalGDI
= (PPALETTE
) PALETTE_ShareLockPalette(NewPalette
);
521 PALETTE_ValidateFlags(PalGDI
->IndexedColors
, PalGDI
->NumColors
);
522 PALETTE_ShareUnlockPalette(PalGDI
);
526 /* FIXME: Handle PalGDI == NULL!!!! */
527 DPRINT1("PalGDI is NULL\n");
532 HPALETTE APIENTRY
NtGdiCreateHalftonePalette(HDC hDC
)
537 WORD NumberOfEntries
;
538 PALETTEENTRY aEntries
[256];
541 Palette
.Version
= 0x300;
542 Palette
.NumberOfEntries
= 256;
543 if (IntGetSystemPaletteEntries(hDC
, 0, 256, Palette
.aEntries
) == 0)
545 /* From WINE, more that 256 color math */
546 Palette
.NumberOfEntries
= 20;
547 for (i
= 0; i
< Palette
.NumberOfEntries
; i
++)
549 Palette
.aEntries
[i
].peRed
=0xff;
550 Palette
.aEntries
[i
].peGreen
=0xff;
551 Palette
.aEntries
[i
].peBlue
=0xff;
552 Palette
.aEntries
[i
].peFlags
=0x00;
555 Palette
.aEntries
[0].peRed
=0x00;
556 Palette
.aEntries
[0].peBlue
=0x00;
557 Palette
.aEntries
[0].peGreen
=0x00;
560 for (i
=1; i
<= 6; i
++)
562 Palette
.aEntries
[i
].peRed
=(i
%2)?0x80:0;
563 Palette
.aEntries
[i
].peGreen
=(i
==2)?0x80:(i
==3)?0x80:(i
==6)?0x80:0;
564 Palette
.aEntries
[i
].peBlue
=(i
>3)?0x80:0;
567 for (i
=7; i
<= 12; i
++)
572 Palette
.aEntries
[i
].peRed
=0xc0;
573 Palette
.aEntries
[i
].peBlue
=0xc0;
574 Palette
.aEntries
[i
].peGreen
=0xc0;
577 Palette
.aEntries
[i
].peRed
=0xc0;
578 Palette
.aEntries
[i
].peGreen
=0xdc;
579 Palette
.aEntries
[i
].peBlue
=0xc0;
582 Palette
.aEntries
[i
].peRed
=0xa6;
583 Palette
.aEntries
[i
].peGreen
=0xca;
584 Palette
.aEntries
[i
].peBlue
=0xf0;
587 Palette
.aEntries
[i
].peRed
=0xff;
588 Palette
.aEntries
[i
].peGreen
=0xfb;
589 Palette
.aEntries
[i
].peBlue
=0xf0;
592 Palette
.aEntries
[i
].peRed
=0xa0;
593 Palette
.aEntries
[i
].peGreen
=0xa0;
594 Palette
.aEntries
[i
].peBlue
=0xa4;
597 Palette
.aEntries
[i
].peRed
=0x80;
598 Palette
.aEntries
[i
].peGreen
=0x80;
599 Palette
.aEntries
[i
].peBlue
=0x80;
603 for (i
=13; i
<= 18; i
++)
605 Palette
.aEntries
[i
].peRed
=(i
%2)?0xff:0;
606 Palette
.aEntries
[i
].peGreen
=(i
==14)?0xff:(i
==15)?0xff:(i
==18)?0xff:0;
607 Palette
.aEntries
[i
].peBlue
=(i
>15)?0xff:0x00;
612 /* 256 color table */
613 for (r
= 0; r
< 6; r
++)
614 for (g
= 0; g
< 6; g
++)
615 for (b
= 0; b
< 6; b
++)
617 i
= r
+ g
*6 + b
*36 + 10;
618 Palette
.aEntries
[i
].peRed
= r
* 51;
619 Palette
.aEntries
[i
].peGreen
= g
* 51;
620 Palette
.aEntries
[i
].peBlue
= b
* 51;
623 for (i
= 216; i
< 246; i
++)
625 int v
= (i
- 216) << 3;
626 Palette
.aEntries
[i
].peRed
= v
;
627 Palette
.aEntries
[i
].peGreen
= v
;
628 Palette
.aEntries
[i
].peBlue
= v
;
632 return NtGdiCreatePaletteInternal((LOGPALETTE
*)&Palette
, Palette
.NumberOfEntries
);
641 /* PALOBJ *palPtr = (PALOBJ*)AccessUserObject(hPal);
642 UINT cPrevEnt, prevVer;
643 INT prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
644 XLATEOBJ *XlateObj = NULL;
646 if(!palPtr) return FALSE;
647 cPrevEnt = palPtr->logpalette->palNumEntries;
648 prevVer = palPtr->logpalette->palVersion;
649 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) + sizeof(int*) + sizeof(GDIOBJHDR);
650 size += sizeof(int*) + sizeof(GDIOBJHDR);
651 XlateObj = palPtr->logicalToSystem;
653 if (!(palPtr = GDI_ReallocObject(size, hPal, palPtr))) return FALSE;
657 XLATEOBJ *NewXlateObj = (int*) HeapReAlloc(GetProcessHeap(), 0, XlateObj, cEntries * sizeof(int));
658 if(NewXlateObj == NULL)
660 ERR("Can not resize logicalToSystem -- out of memory!");
661 GDI_ReleaseObj( hPal );
664 palPtr->logicalToSystem = NewXlateObj;
667 if(cEntries > cPrevEnt)
669 if(XlateObj) memset(palPtr->logicalToSystem + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
670 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
671 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize), cEntries - cPrevEnt );
673 palPtr->logpalette->palNumEntries = cEntries;
674 palPtr->logpalette->palVersion = prevVer;
675 // GDI_ReleaseObj( hPal );
684 NtGdiGetColorAdjustment(
686 LPCOLORADJUSTMENT pca
)
694 NtGdiSetColorAdjustment(
696 LPCOLORADJUSTMENT pca
)
702 COLORREF APIENTRY
NtGdiGetNearestColor(HDC hDC
, COLORREF Color
)
704 COLORREF nearest
= CLR_INVALID
;
707 LONG RBits
, GBits
, BBits
;
712 HPALETTE hpal
= dc
->dclevel
.hpal
;
713 palGDI
= PALETTE_ShareLockPalette(hpal
);
720 if (palGDI
->flFlags
& PAL_INDEXED
)
723 index
= PALETTE_ulGetNearestPaletteIndex(palGDI
, Color
);
724 nearest
= PALETTE_ulGetRGBColorFromIndex(palGDI
, index
);
726 else if (palGDI
->flFlags
& PAL_RGB
|| palGDI
->flFlags
& PAL_BGR
)
730 else if (palGDI
->flFlags
& PAL_BITFIELDS
)
732 RBits
= 8 - GetNumberOfBits(palGDI
->RedMask
);
733 GBits
= 8 - GetNumberOfBits(palGDI
->GreenMask
);
734 BBits
= 8 - GetNumberOfBits(palGDI
->BlueMask
);
736 (GetRValue(Color
) >> RBits
) << RBits
,
737 (GetGValue(Color
) >> GBits
) << GBits
,
738 (GetBValue(Color
) >> BBits
) << BBits
);
740 PALETTE_ShareUnlockPalette(palGDI
);
749 NtGdiGetNearestPaletteIndex(
753 PPALETTE ppal
= PALETTE_ShareLockPalette(hpal
);
758 if (ppal
->flFlags
& PAL_INDEXED
)
760 /* Return closest match for the given RGB color */
761 index
= PALETTE_ulGetNearestPaletteIndex(ppal
, crColor
);
763 // else SetLastError ?
764 PALETTE_ShareUnlockPalette(ppal
);
772 IntGdiRealizePalette(HDC hDC
)
776 PALETTE
*ppalSurf
, *ppalDC
;
778 pdc
= DC_LockDc(hDC
);
781 EngSetLastError(ERROR_INVALID_HANDLE
);
785 ppalSurf
= pdc
->dclevel
.pSurface
->ppal
;
786 ppalDC
= pdc
->dclevel
.ppal
;
788 if(!(ppalSurf
->flFlags
& PAL_INDEXED
))
794 ASSERT(ppalDC
->flFlags
& PAL_INDEXED
);
796 // FIXME: Should we resize ppalSurf if it's too small?
797 realize
= (ppalDC
->NumColors
< ppalSurf
->NumColors
) ? ppalDC
->NumColors
: ppalSurf
->NumColors
;
799 for(i
=0; i
<realize
; i
++)
801 InterlockedExchange((LONG
*)&ppalSurf
->IndexedColors
[i
], *(LONG
*)&ppalDC
->IndexedColors
[i
]);
810 IntAnimatePalette(HPALETTE hPal
,
813 CONST PPALETTEENTRY PaletteColors
)
817 if( hPal
!= NtGdiGetStockObject(DEFAULT_PALETTE
) )
824 const PALETTEENTRY
*pptr
= PaletteColors
;
826 palPtr
= PALETTE_ShareLockPalette(hPal
);
827 if (!palPtr
) return FALSE
;
829 pal_entries
= palPtr
->NumColors
;
830 if (StartIndex
>= pal_entries
)
832 PALETTE_ShareUnlockPalette(palPtr
);
835 if (StartIndex
+NumEntries
> pal_entries
) NumEntries
= pal_entries
- StartIndex
;
837 for (NumEntries
+= StartIndex
; StartIndex
< NumEntries
; StartIndex
++, pptr
++)
839 /* According to MSDN, only animate PC_RESERVED colours */
840 if (palPtr
->IndexedColors
[StartIndex
].peFlags
& PC_RESERVED
)
842 memcpy( &palPtr
->IndexedColors
[StartIndex
], pptr
,
843 sizeof(PALETTEENTRY
) );
845 PALETTE_ValidateFlags(&palPtr
->IndexedColors
[StartIndex
], 1);
849 PALETTE_ShareUnlockPalette(palPtr
);
851 /* Immediately apply the new palette if current window uses it */
852 Wnd
= UserGetDesktopWindow();
853 hDC
= UserGetWindowDC(Wnd
);
857 if (dc
->dclevel
.hpal
== hPal
)
860 IntGdiRealizePalette(hDC
);
865 UserReleaseDC(Wnd
,hDC
, FALSE
);
871 IntGetPaletteEntries(
880 palGDI
= (PPALETTE
) PALETTE_ShareLockPalette(hpal
);
886 numEntries
= palGDI
->NumColors
;
889 if (numEntries
< StartIndex
+ Entries
)
891 Entries
= numEntries
- StartIndex
;
893 if (numEntries
<= StartIndex
)
895 PALETTE_ShareUnlockPalette(palGDI
);
898 memcpy(pe
, palGDI
->IndexedColors
+ StartIndex
, Entries
* sizeof(PALETTEENTRY
));
902 Entries
= numEntries
;
905 PALETTE_ShareUnlockPalette(palGDI
);
910 IntGetSystemPaletteEntries(HDC hDC
,
915 PPALETTE palGDI
= NULL
;
917 UINT EntriesSize
= 0;
922 EngSetLastError(ERROR_INVALID_PARAMETER
);
928 EntriesSize
= Entries
* sizeof(pe
[0]);
929 if (Entries
!= EntriesSize
/ sizeof(pe
[0]))
931 /* Integer overflow! */
932 EngSetLastError(ERROR_INVALID_PARAMETER
);
937 if (!(dc
= DC_LockDc(hDC
)))
939 EngSetLastError(ERROR_INVALID_HANDLE
);
943 palGDI
= PALETTE_ShareLockPalette(dc
->dclevel
.hpal
);
948 if (StartIndex
>= palGDI
->NumColors
)
950 else if (Entries
> palGDI
->NumColors
- StartIndex
)
951 Entries
= palGDI
->NumColors
- StartIndex
;
954 palGDI
->IndexedColors
+ StartIndex
,
955 Entries
* sizeof(pe
[0]));
961 Ret
= dc
->ppdev
->gdiinfo
.ulNumPalReg
;
966 PALETTE_ShareUnlockPalette(palGDI
);
976 IntSetPaletteEntries(
980 CONST LPPALETTEENTRY pe
)
985 if ((UINT
)hpal
& GDI_HANDLE_STOCK_MASK
)
990 palGDI
= PALETTE_ShareLockPalette(hpal
);
991 if (!palGDI
) return 0;
993 numEntries
= palGDI
->NumColors
;
994 if (Start
>= numEntries
)
996 PALETTE_ShareUnlockPalette(palGDI
);
999 if (numEntries
< Start
+ Entries
)
1001 Entries
= numEntries
- Start
;
1003 memcpy(palGDI
->IndexedColors
+ Start
, pe
, Entries
* sizeof(PALETTEENTRY
));
1004 PALETTE_ShareUnlockPalette(palGDI
);
1016 IN LPVOID pUnsafeEntries
,
1021 LPVOID pEntries
= NULL
;
1023 /* FIXME: Handle bInbound correctly */
1026 (pUnsafeEntries
== NULL
|| cEntries
== 0))
1033 pEntries
= ExAllocatePoolWithTag(PagedPool
, cEntries
* sizeof(PALETTEENTRY
), TAG_PALETTE
);
1040 ProbeForRead(pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
), 1);
1041 memcpy(pEntries
, pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
));
1043 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1045 ExFreePoolWithTag(pEntries
, TAG_PALETTE
);
1046 _SEH2_YIELD(return 0);
1057 ret
= IntAnimatePalette((HPALETTE
)hObj
, iStart
, cEntries
, (CONST PPALETTEENTRY
)pEntries
);
1060 case GdiPalSetEntries
:
1062 ret
= IntSetPaletteEntries((HPALETTE
)hObj
, iStart
, cEntries
, (CONST LPPALETTEENTRY
)pEntries
);
1065 case GdiPalGetEntries
:
1066 ret
= IntGetPaletteEntries((HPALETTE
)hObj
, iStart
, cEntries
, (LPPALETTEENTRY
)pEntries
);
1069 case GdiPalGetSystemEntries
:
1070 ret
= IntGetSystemPaletteEntries((HDC
)hObj
, iStart
, cEntries
, (LPPALETTEENTRY
)pEntries
);
1073 case GdiPalSetColorTable
:
1075 ret
= IntSetDIBColorTable((HDC
)hObj
, iStart
, cEntries
, (RGBQUAD
*)pEntries
);
1078 case GdiPalGetColorTable
:
1080 ret
= IntGetDIBColorTable((HDC
)hObj
, iStart
, cEntries
, (RGBQUAD
*)pEntries
);
1090 ProbeForWrite(pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
), 1);
1091 memcpy(pUnsafeEntries
, pEntries
, cEntries
* sizeof(PALETTEENTRY
));
1093 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1099 ExFreePoolWithTag(pEntries
, TAG_PALETTE
);
1106 NtGdiSetSystemPaletteUse(HDC hDC
, UINT Usage
)
1108 UINT old
= SystemPaletteUse
;
1110 /* Device doesn't support colour palettes */
1111 if (!(NtGdiGetDeviceCaps(hDC
, RASTERCAPS
) & RC_PALETTE
)) {
1112 return SYSPAL_ERROR
;
1117 case SYSPAL_NOSTATIC
:
1118 case SYSPAL_NOSTATIC256
:
1120 SystemPaletteUse
= Usage
;
1133 NtGdiGetSystemPaletteUse(HDC hDC
)
1135 return SystemPaletteUse
;
1140 NtGdiUpdateColors(HDC hDC
)
1143 BOOL calledFromUser
, ret
;
1144 USER_REFERENCE_ENTRY Ref
;
1146 calledFromUser
= UserIsEntered();
1148 if (!calledFromUser
){
1149 UserEnterExclusive();
1152 Wnd
= UserGetWindowObject(IntWindowFromDC(hDC
));
1155 EngSetLastError(ERROR_INVALID_WINDOW_HANDLE
);
1157 if (!calledFromUser
){
1164 UserRefObjectCo(Wnd
, &Ref
);
1165 ret
= co_UserRedrawWindow(Wnd
, NULL
, 0, RDW_INVALIDATE
);
1166 UserDerefObjectCo(Wnd
);
1168 if (!calledFromUser
){
1177 NtGdiUnrealizeObject(HGDIOBJ hgdiobj
)
1183 ((UINT
)hgdiobj
& GDI_HANDLE_STOCK_MASK
) ||
1184 !GDI_HANDLE_IS_TYPE(hgdiobj
, GDI_OBJECT_TYPE_PALETTE
) )
1187 palGDI
= PALETTE_ShareLockPalette(hgdiobj
);
1188 if (!palGDI
) return FALSE
;
1191 // Need to do something!!!
1192 // Zero out Current and Old Translated pointers?
1195 PALETTE_ShareUnlockPalette(palGDI
);