2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: GDI Palette Functions
5 * FILE: subsys/win32k/eng/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
60 HPALETTE FASTCALL
PALETTE_Init(VOID
)
66 // create default palette (20 system colors)
67 palPtr
= ExAllocatePoolWithTag(PagedPool
,
69 (NB_RESERVED_COLORS
* sizeof(PALETTEENTRY
)),
71 if (!palPtr
) return FALSE
;
73 palPtr
->palVersion
= 0x300;
74 palPtr
->palNumEntries
= NB_RESERVED_COLORS
;
75 for (i
=0; i
<NB_RESERVED_COLORS
; i
++)
77 palPtr
->palPalEntry
[i
].peRed
= g_sysPalTemplate
[i
].peRed
;
78 palPtr
->palPalEntry
[i
].peGreen
= g_sysPalTemplate
[i
].peGreen
;
79 palPtr
->palPalEntry
[i
].peBlue
= g_sysPalTemplate
[i
].peBlue
;
80 palPtr
->palPalEntry
[i
].peFlags
= 0;
83 hpalette
= NtGdiCreatePaletteInternal(palPtr
,NB_RESERVED_COLORS
);
84 ExFreePoolWithTag(palPtr
, TAG_PALETTE
);
86 /* palette_size = visual->map_entries; */
88 gpalRGB
.Mode
= PAL_RGB
;
89 gpalRGB
.RedMask
= RGB(0xFF, 0x00, 0x00);
90 gpalRGB
.GreenMask
= RGB(0x00, 0xFF, 0x00);
91 gpalRGB
.BlueMask
= RGB(0x00, 0x00, 0xFF);
92 gpalRGB
.BaseObject
.ulShareCount
= 0;
93 gpalRGB
.BaseObject
.BaseFlags
= 0 ;
95 gpalBGR
.Mode
= PAL_BGR
;
96 gpalBGR
.RedMask
= RGB(0x00, 0x00, 0xFF);
97 gpalBGR
.GreenMask
= RGB(0x00, 0xFF, 0x00);
98 gpalBGR
.BlueMask
= RGB(0xFF, 0x00, 0x00);
99 gpalBGR
.BaseObject
.ulShareCount
= 0;
100 gpalBGR
.BaseObject
.BaseFlags
= 0 ;
102 gpalRGB555
.Mode
= PAL_RGB16_555
| PAL_BITFIELDS
;
103 gpalRGB555
.RedMask
= 0x7C00;
104 gpalRGB555
.GreenMask
= 0x3E0;
105 gpalRGB555
.BlueMask
= 0x1F;
106 gpalRGB555
.BaseObject
.ulShareCount
= 0;
107 gpalRGB555
.BaseObject
.BaseFlags
= 0 ;
109 gpalRGB565
.Mode
= PAL_RGB16_565
| PAL_BITFIELDS
;
110 gpalRGB565
.RedMask
= 0xF800;
111 gpalRGB565
.GreenMask
= 0x7E0;
112 gpalRGB565
.BlueMask
= 0x1F;
113 gpalRGB565
.BaseObject
.ulShareCount
= 0;
114 gpalRGB565
.BaseObject
.BaseFlags
= 0 ;
116 memset(&gpalMono
, 0, sizeof(PALETTE
));
117 gpalMono
.Mode
= PAL_MONOCHROME
;
118 gpalMono
.BaseObject
.ulShareCount
= 0;
119 gpalMono
.BaseObject
.BaseFlags
= 0 ;
121 /* Initialize default surface palettes */
122 gppalDefault
= PALETTE_ShareLockPalette(hpalette
);
123 appalSurfaceDefault
[BMF_1BPP
] = &gpalMono
;
124 appalSurfaceDefault
[BMF_4BPP
] = gppalDefault
;
125 appalSurfaceDefault
[BMF_8BPP
] = gppalDefault
;
126 appalSurfaceDefault
[BMF_16BPP
] = &gpalRGB565
;
127 appalSurfaceDefault
[BMF_24BPP
] = &gpalRGB
;
128 appalSurfaceDefault
[BMF_32BPP
] = &gpalRGB
;
129 appalSurfaceDefault
[BMF_4RLE
] = gppalDefault
;
130 appalSurfaceDefault
[BMF_8RLE
] = gppalDefault
;
131 appalSurfaceDefault
[BMF_JPEG
] = &gpalRGB
;
132 appalSurfaceDefault
[BMF_PNG
] = &gpalRGB
;
137 VOID FASTCALL
PALETTE_ValidateFlags(PALETTEENTRY
* lpPalE
, INT size
)
141 lpPalE
[i
].peFlags
= PC_SYS_USED
| (lpPalE
[i
].peFlags
& 0x07);
146 PALETTE_AllocPalette(ULONG Mode
,
156 PalGDI
= (PPALETTE
)GDIOBJ_AllocObjWithHandle(GDI_OBJECT_TYPE_PALETTE
);
162 NewPalette
= PalGDI
->BaseObject
.hHmgr
;
164 PalGDI
->Self
= NewPalette
;
169 PalGDI
->IndexedColors
= ExAllocatePoolWithTag(PagedPool
,
170 sizeof(PALETTEENTRY
) * NumColors
,
172 if (NULL
== PalGDI
->IndexedColors
)
174 PALETTE_UnlockPalette(PalGDI
);
175 PALETTE_FreePaletteByHandle(NewPalette
);
178 RtlCopyMemory(PalGDI
->IndexedColors
, Colors
, sizeof(PALETTEENTRY
) * NumColors
);
181 if (PAL_INDEXED
== Mode
)
183 PalGDI
->NumColors
= NumColors
;
185 else if (PAL_BITFIELDS
== Mode
)
187 PalGDI
->RedMask
= Red
;
188 PalGDI
->GreenMask
= Green
;
189 PalGDI
->BlueMask
= Blue
;
191 if (Red
== 0x7c00 && Green
== 0x3E0 && Blue
== 0x1F)
192 PalGDI
->Mode
|= PAL_RGB16_555
;
193 else if (Red
== 0xF800 && Green
== 0x7E0 && Blue
== 0x1F)
194 PalGDI
->Mode
|= PAL_RGB16_565
;
197 PALETTE_UnlockPalette(PalGDI
);
204 PALETTE_AllocPaletteIndexedRGB(ULONG NumColors
,
205 CONST RGBQUAD
*Colors
)
211 PalGDI
= (PPALETTE
)GDIOBJ_AllocObjWithHandle(GDI_OBJECT_TYPE_PALETTE
);
217 NewPalette
= PalGDI
->BaseObject
.hHmgr
;
219 PalGDI
->Self
= NewPalette
;
220 PalGDI
->Mode
= PAL_INDEXED
;
222 PalGDI
->IndexedColors
= ExAllocatePoolWithTag(PagedPool
,
223 sizeof(PALETTEENTRY
) * NumColors
,
225 if (NULL
== PalGDI
->IndexedColors
)
227 PALETTE_UnlockPalette(PalGDI
);
228 PALETTE_FreePaletteByHandle(NewPalette
);
232 for (i
= 0; i
< NumColors
; i
++)
234 PalGDI
->IndexedColors
[i
].peRed
= Colors
[i
].rgbRed
;
235 PalGDI
->IndexedColors
[i
].peGreen
= Colors
[i
].rgbGreen
;
236 PalGDI
->IndexedColors
[i
].peBlue
= Colors
[i
].rgbBlue
;
237 PalGDI
->IndexedColors
[i
].peFlags
= 0;
240 PalGDI
->NumColors
= NumColors
;
242 PALETTE_UnlockPalette(PalGDI
);
248 PALETTE_Cleanup(PVOID ObjectBody
)
250 PPALETTE pPal
= (PPALETTE
)ObjectBody
;
251 if (NULL
!= pPal
->IndexedColors
)
253 ExFreePoolWithTag(pPal
->IndexedColors
, TAG_PALETTE
);
260 PALETTE_GetObject(PPALETTE ppal
, INT cbCount
, LPLOGBRUSH lpBuffer
)
267 if ((UINT
)cbCount
< sizeof(WORD
)) return 0;
268 *((WORD
*)lpBuffer
) = (WORD
)ppal
->NumColors
;
274 PALETTE_ulGetNearestPaletteIndex(PALETTE
* ppal
, ULONG iColor
)
276 ULONG ulDiff
, ulColorDiff
, ulMinimalDiff
= 0xFFFFFF;
277 ULONG i
, ulBestIndex
= 0;
278 PALETTEENTRY peColor
= *(PPALETTEENTRY
)&iColor
;
280 /* Loop all palette entries, break on exact match */
281 for (i
= 0; i
< ppal
->NumColors
&& ulMinimalDiff
!= 0; i
++)
283 /* Calculate distance in the color cube */
284 ulDiff
= peColor
.peRed
- ppal
->IndexedColors
[i
].peRed
;
285 ulColorDiff
= ulDiff
* ulDiff
;
286 ulDiff
= peColor
.peGreen
- ppal
->IndexedColors
[i
].peGreen
;
287 ulColorDiff
+= ulDiff
* ulDiff
;
288 ulDiff
= peColor
.peBlue
- ppal
->IndexedColors
[i
].peBlue
;
289 ulColorDiff
+= ulDiff
* ulDiff
;
291 /* Check for a better match */
292 if (ulColorDiff
< ulMinimalDiff
)
295 ulMinimalDiff
= ulColorDiff
;
304 PALETTE_ulGetNearestBitFieldsIndex(PALETTE
* ppal
, ULONG ulColor
)
308 // FIXME: HACK, should be stored already
309 ppal
->ulRedShift
= CalculateShift(RGB(0xff,0,0), ppal
->RedMask
);
310 ppal
->ulGreenShift
= CalculateShift(RGB(0,0xff,0), ppal
->GreenMask
);
311 ppal
->ulBlueShift
= CalculateShift(RGB(0,0,0xff), ppal
->BlueMask
);
313 ulNewColor
= _rotl(ulColor
, ppal
->ulRedShift
) & ppal
->RedMask
;
314 ulNewColor
|= _rotl(ulColor
, ppal
->ulGreenShift
) & ppal
->GreenMask
;
315 ulNewColor
|= _rotl(ulColor
, ppal
->ulBlueShift
) & ppal
->BlueMask
;
322 PALETTE_ulGetNearestIndex(PALETTE
* ppal
, ULONG ulColor
)
324 if (ppal
->Mode
& PAL_INDEXED
) // use fl & PALINDEXED
325 return PALETTE_ulGetNearestPaletteIndex(ppal
, ulColor
);
327 return PALETTE_ulGetNearestBitFieldsIndex(ppal
, ulColor
);
332 PALETTE_vGetBitMasks(PPALETTE ppal
, PULONG pulColors
)
336 if (ppal
->Mode
& PAL_INDEXED
|| ppal
->Mode
& PAL_RGB
)
338 pulColors
[0] = RGB(0xFF, 0x00, 0x00);
339 pulColors
[1] = RGB(0x00, 0xFF, 0x00);
340 pulColors
[2] = RGB(0x00, 0x00, 0xFF);
342 else if (ppal
->Mode
& PAL_BGR
)
344 pulColors
[0] = RGB(0x00, 0x00, 0xFF);
345 pulColors
[1] = RGB(0x00, 0xFF, 0x00);
346 pulColors
[2] = RGB(0xFF, 0x00, 0x00);
348 else if (ppal
->Mode
& PAL_BITFIELDS
)
350 pulColors
[0] = ppal
->RedMask
;
351 pulColors
[1] = ppal
->GreenMask
;
352 pulColors
[2] = ppal
->BlueMask
;
358 ColorCorrection(PPALETTE PalGDI
, PPALETTEENTRY PaletteEntry
, ULONG Colors
)
360 PPDEVOBJ ppdev
= (PPDEVOBJ
)PalGDI
->hPDev
;
364 if (ppdev
->flFlags
& PDEV_GAMMARAMP_TABLE
)
367 PGAMMARAMP GammaRamp
= (PGAMMARAMP
)ppdev
->pvGammaRamp
;
368 for ( i
= 0; i
< Colors
; i
++)
370 PaletteEntry
[i
].peRed
+= GammaRamp
->Red
[i
];
371 PaletteEntry
[i
].peGreen
+= GammaRamp
->Green
[i
];
372 PaletteEntry
[i
].peBlue
+= GammaRamp
->Blue
[i
];
378 /** Display Driver Interface **************************************************/
395 Palette
= PALETTE_AllocPalette(Mode
, NumColors
, Colors
, Red
, Green
, Blue
);
398 GDIOBJ_SetOwnership(Palette
, NULL
);
409 EngDeletePalette(IN HPALETTE Palette
)
411 GDIOBJ_SetOwnership(Palette
, PsGetCurrentProcess());
413 return PALETTE_FreePaletteByHandle(Palette
);
421 PALOBJ_cGetColors(PALOBJ
*PalObj
, ULONG Start
, ULONG Colors
, ULONG
*PaletteEntry
)
425 PalGDI
= (PALETTE
*)PalObj
;
426 /* PalGDI = (PALETTE*)AccessInternalObjectFromUserObject(PalObj); */
428 if (Start
>= PalGDI
->NumColors
)
431 Colors
= min(Colors
, PalGDI
->NumColors
- Start
);
433 /* NOTE: PaletteEntry ULONGs are in the same order as PALETTEENTRY. */
434 RtlCopyMemory(PaletteEntry
, PalGDI
->IndexedColors
+ Start
, sizeof(ULONG
) * Colors
);
436 if (PalGDI
->Mode
& PAL_GAMMACORRECTION
)
437 ColorCorrection(PalGDI
, (PPALETTEENTRY
)PaletteEntry
, Colors
);
443 /** Systemcall Interface ******************************************************/
449 NtGdiCreatePaletteInternal ( IN LPLOGPALETTE pLogPal
, IN UINT cEntries
)
454 pLogPal
->palNumEntries
= cEntries
;
455 NewPalette
= PALETTE_AllocPalette( PAL_INDEXED
,
457 (PULONG
)pLogPal
->palPalEntry
,
460 if (NewPalette
== NULL
)
465 PalGDI
= (PPALETTE
) PALETTE_LockPalette(NewPalette
);
468 PALETTE_ValidateFlags(PalGDI
->IndexedColors
, PalGDI
->NumColors
);
469 PALETTE_UnlockPalette(PalGDI
);
473 /* FIXME - Handle PalGDI == NULL!!!! */
474 DPRINT1("PalGDI is NULL\n");
479 HPALETTE APIENTRY
NtGdiCreateHalftonePalette(HDC hDC
)
484 WORD NumberOfEntries
;
485 PALETTEENTRY aEntries
[256];
488 Palette
.Version
= 0x300;
489 Palette
.NumberOfEntries
= 256;
490 if (IntGetSystemPaletteEntries(hDC
, 0, 256, Palette
.aEntries
) == 0)
492 /* from wine, more that 256 color math */
493 Palette
.NumberOfEntries
= 20;
494 for (i
= 0; i
< Palette
.NumberOfEntries
; i
++)
496 Palette
.aEntries
[i
].peRed
=0xff;
497 Palette
.aEntries
[i
].peGreen
=0xff;
498 Palette
.aEntries
[i
].peBlue
=0xff;
499 Palette
.aEntries
[i
].peFlags
=0x00;
502 Palette
.aEntries
[0].peRed
=0x00;
503 Palette
.aEntries
[0].peBlue
=0x00;
504 Palette
.aEntries
[0].peGreen
=0x00;
507 for (i
=1; i
<= 6; i
++)
509 Palette
.aEntries
[i
].peRed
=(i
%2)?0x80:0;
510 Palette
.aEntries
[i
].peGreen
=(i
==2)?0x80:(i
==3)?0x80:(i
==6)?0x80:0;
511 Palette
.aEntries
[i
].peBlue
=(i
>3)?0x80:0;
514 for (i
=7; i
<= 12; i
++)
519 Palette
.aEntries
[i
].peRed
=0xc0;
520 Palette
.aEntries
[i
].peBlue
=0xc0;
521 Palette
.aEntries
[i
].peGreen
=0xc0;
524 Palette
.aEntries
[i
].peRed
=0xc0;
525 Palette
.aEntries
[i
].peGreen
=0xdc;
526 Palette
.aEntries
[i
].peBlue
=0xc0;
529 Palette
.aEntries
[i
].peRed
=0xa6;
530 Palette
.aEntries
[i
].peGreen
=0xca;
531 Palette
.aEntries
[i
].peBlue
=0xf0;
534 Palette
.aEntries
[i
].peRed
=0xff;
535 Palette
.aEntries
[i
].peGreen
=0xfb;
536 Palette
.aEntries
[i
].peBlue
=0xf0;
539 Palette
.aEntries
[i
].peRed
=0xa0;
540 Palette
.aEntries
[i
].peGreen
=0xa0;
541 Palette
.aEntries
[i
].peBlue
=0xa4;
544 Palette
.aEntries
[i
].peRed
=0x80;
545 Palette
.aEntries
[i
].peGreen
=0x80;
546 Palette
.aEntries
[i
].peBlue
=0x80;
550 for (i
=13; i
<= 18; i
++)
552 Palette
.aEntries
[i
].peRed
=(i
%2)?0xff:0;
553 Palette
.aEntries
[i
].peGreen
=(i
==14)?0xff:(i
==15)?0xff:(i
==18)?0xff:0;
554 Palette
.aEntries
[i
].peBlue
=(i
>15)?0xff:0x00;
559 /* 256 color table */
560 for (r
= 0; r
< 6; r
++)
561 for (g
= 0; g
< 6; g
++)
562 for (b
= 0; b
< 6; b
++)
564 i
= r
+ g
*6 + b
*36 + 10;
565 Palette
.aEntries
[i
].peRed
= r
* 51;
566 Palette
.aEntries
[i
].peGreen
= g
* 51;
567 Palette
.aEntries
[i
].peBlue
= b
* 51;
570 for (i
= 216; i
< 246; i
++)
572 int v
= (i
- 216) << 3;
573 Palette
.aEntries
[i
].peRed
= v
;
574 Palette
.aEntries
[i
].peGreen
= v
;
575 Palette
.aEntries
[i
].peBlue
= v
;
579 return NtGdiCreatePaletteInternal((LOGPALETTE
*)&Palette
, Palette
.NumberOfEntries
);
588 /* PALOBJ *palPtr = (PALOBJ*)AccessUserObject(hPal);
589 UINT cPrevEnt, prevVer;
590 INT prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
591 XLATEOBJ *XlateObj = NULL;
593 if(!palPtr) return FALSE;
594 cPrevEnt = palPtr->logpalette->palNumEntries;
595 prevVer = palPtr->logpalette->palVersion;
596 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) + sizeof(int*) + sizeof(GDIOBJHDR);
597 size += sizeof(int*) + sizeof(GDIOBJHDR);
598 XlateObj = palPtr->logicalToSystem;
600 if (!(palPtr = GDI_ReallocObject(size, hPal, palPtr))) return FALSE;
604 XLATEOBJ *NewXlateObj = (int*) HeapReAlloc(GetProcessHeap(), 0, XlateObj, cEntries * sizeof(int));
605 if(NewXlateObj == NULL)
607 ERR("Can not resize logicalToSystem -- out of memory!");
608 GDI_ReleaseObj( hPal );
611 palPtr->logicalToSystem = NewXlateObj;
614 if(cEntries > cPrevEnt)
616 if(XlateObj) memset(palPtr->logicalToSystem + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
617 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
618 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize), cEntries - cPrevEnt );
620 palPtr->logpalette->palNumEntries = cEntries;
621 palPtr->logpalette->palVersion = prevVer;
622 // GDI_ReleaseObj( hPal );
631 NtGdiGetColorAdjustment(
633 LPCOLORADJUSTMENT pca
)
641 NtGdiSetColorAdjustment(
643 LPCOLORADJUSTMENT pca
)
649 COLORREF APIENTRY
NtGdiGetNearestColor(HDC hDC
, COLORREF Color
)
651 COLORREF nearest
= CLR_INVALID
;
654 LONG RBits
, GBits
, BBits
;
659 HPALETTE hpal
= dc
->dclevel
.hpal
;
660 palGDI
= (PPALETTE
) PALETTE_LockPalette(hpal
);
667 if (palGDI
->Mode
& PAL_INDEXED
)
670 index
= PALETTE_ulGetNearestPaletteIndex(palGDI
, Color
);
671 nearest
= PALETTE_ulGetRGBColorFromIndex(palGDI
, index
);
673 else if (palGDI
->Mode
& PAL_RGB
|| palGDI
->Mode
& PAL_BGR
)
677 else if (palGDI
->Mode
& PAL_BITFIELDS
)
679 RBits
= 8 - GetNumberOfBits(palGDI
->RedMask
);
680 GBits
= 8 - GetNumberOfBits(palGDI
->GreenMask
);
681 BBits
= 8 - GetNumberOfBits(palGDI
->BlueMask
);
683 (GetRValue(Color
) >> RBits
) << RBits
,
684 (GetGValue(Color
) >> GBits
) << GBits
,
685 (GetBValue(Color
) >> BBits
) << BBits
);
687 PALETTE_UnlockPalette(palGDI
);
696 NtGdiGetNearestPaletteIndex(
700 PPALETTE ppal
= (PPALETTE
) PALETTE_LockPalette(hpal
);
705 if (ppal
->Mode
& PAL_INDEXED
)
707 /* Return closest match for the given RGB color */
708 index
= PALETTE_ulGetNearestPaletteIndex(ppal
, crColor
);
710 // else SetLastError ?
711 PALETTE_UnlockPalette(ppal
);
719 IntGdiRealizePalette(HDC hDC
)
722 * This function doesn't do any real work now and there's plenty
726 PPALETTE palGDI
, sysGDI
;
729 HPALETTE systemPalette
;
730 USHORT sysMode
, palMode
;
735 systemPalette
= NtGdiGetStockObject(DEFAULT_PALETTE
);
736 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
740 DPRINT1("IntGdiRealizePalette(): palGDI is NULL, exiting\n");
745 sysGDI
= PALETTE_LockPalette(systemPalette
);
749 DPRINT1("IntGdiRealizePalette(): sysGDI is NULL, exiting\n");
750 PALETTE_UnlockPalette(palGDI
);
755 // The RealizePalette function modifies the palette for the device associated with the specified device context. If the
756 // device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device
757 // context is a display DC, the physical palette for that device is modified.
758 if(dc
->dctype
== DC_TYPE_MEMORY
)
761 DPRINT1("RealizePalette unimplemented for memory managed DCs\n");
764 DPRINT1("RealizePalette unimplemented for device DCs\n");
767 // need to pass this to IntEngCreateXlate with palettes unlocked
768 sysMode
= sysGDI
->Mode
;
769 palMode
= palGDI
->Mode
;
770 PALETTE_UnlockPalette(sysGDI
);
771 PALETTE_UnlockPalette(palGDI
);
779 IntAnimatePalette(HPALETTE hPal
,
782 CONST PPALETTEENTRY PaletteColors
)
786 if( hPal
!= NtGdiGetStockObject(DEFAULT_PALETTE
) )
793 const PALETTEENTRY
*pptr
= PaletteColors
;
795 palPtr
= (PPALETTE
)PALETTE_LockPalette(hPal
);
796 if (!palPtr
) return FALSE
;
798 pal_entries
= palPtr
->NumColors
;
799 if (StartIndex
>= pal_entries
)
801 PALETTE_UnlockPalette(palPtr
);
804 if (StartIndex
+NumEntries
> pal_entries
) NumEntries
= pal_entries
- StartIndex
;
806 for (NumEntries
+= StartIndex
; StartIndex
< NumEntries
; StartIndex
++, pptr
++)
808 /* According to MSDN, only animate PC_RESERVED colours */
809 if (palPtr
->IndexedColors
[StartIndex
].peFlags
& PC_RESERVED
)
811 memcpy( &palPtr
->IndexedColors
[StartIndex
], pptr
,
812 sizeof(PALETTEENTRY
) );
814 PALETTE_ValidateFlags(&palPtr
->IndexedColors
[StartIndex
], 1);
818 PALETTE_UnlockPalette(palPtr
);
820 /* Immediately apply the new palette if current window uses it */
821 Wnd
= UserGetDesktopWindow();
822 hDC
= UserGetWindowDC(Wnd
);
826 if (dc
->dclevel
.hpal
== hPal
)
829 IntGdiRealizePalette(hDC
);
834 UserReleaseDC(Wnd
,hDC
, FALSE
);
840 IntGetPaletteEntries(
849 palGDI
= (PPALETTE
) PALETTE_LockPalette(hpal
);
855 numEntries
= palGDI
->NumColors
;
858 if (numEntries
< StartIndex
+ Entries
)
860 Entries
= numEntries
- StartIndex
;
862 if (numEntries
<= StartIndex
)
864 PALETTE_UnlockPalette(palGDI
);
867 memcpy(pe
, palGDI
->IndexedColors
+ StartIndex
, Entries
* sizeof(PALETTEENTRY
));
871 Entries
= numEntries
;
874 PALETTE_UnlockPalette(palGDI
);
879 IntGetSystemPaletteEntries(HDC hDC
,
884 PPALETTE palGDI
= NULL
;
886 UINT EntriesSize
= 0;
891 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
897 EntriesSize
= Entries
* sizeof(pe
[0]);
898 if (Entries
!= EntriesSize
/ sizeof(pe
[0]))
900 /* Integer overflow! */
901 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
906 if (!(dc
= DC_LockDc(hDC
)))
908 SetLastWin32Error(ERROR_INVALID_HANDLE
);
912 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
917 if (StartIndex
>= palGDI
->NumColors
)
919 else if (Entries
> palGDI
->NumColors
- StartIndex
)
920 Entries
= palGDI
->NumColors
- StartIndex
;
923 palGDI
->IndexedColors
+ StartIndex
,
924 Entries
* sizeof(pe
[0]));
930 Ret
= dc
->ppdev
->gdiinfo
.ulNumPalReg
;
935 PALETTE_UnlockPalette(palGDI
);
945 IntSetPaletteEntries(
949 CONST LPPALETTEENTRY pe
)
954 if ((UINT
)hpal
& GDI_HANDLE_STOCK_MASK
)
959 palGDI
= PALETTE_LockPalette(hpal
);
960 if (!palGDI
) return 0;
962 numEntries
= palGDI
->NumColors
;
963 if (Start
>= numEntries
)
965 PALETTE_UnlockPalette(palGDI
);
968 if (numEntries
< Start
+ Entries
)
970 Entries
= numEntries
- Start
;
972 memcpy(palGDI
->IndexedColors
+ Start
, pe
, Entries
* sizeof(PALETTEENTRY
));
973 PALETTE_UnlockPalette(palGDI
);
985 IN LPVOID pUnsafeEntries
,
990 LPVOID pEntries
= NULL
;
992 /* FIXME: Handle bInbound correctly */
995 (pUnsafeEntries
== NULL
|| cEntries
== 0))
1002 pEntries
= ExAllocatePoolWithTag(PagedPool
, cEntries
* sizeof(PALETTEENTRY
), TAG_PALETTE
);
1009 ProbeForRead(pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
), 1);
1010 memcpy(pEntries
, pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
));
1012 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1014 ExFreePoolWithTag(pEntries
, TAG_PALETTE
);
1015 _SEH2_YIELD(return 0);
1026 ret
= IntAnimatePalette((HPALETTE
)hObj
, iStart
, cEntries
, (CONST PPALETTEENTRY
)pEntries
);
1029 case GdiPalSetEntries
:
1031 ret
= IntSetPaletteEntries((HPALETTE
)hObj
, iStart
, cEntries
, (CONST LPPALETTEENTRY
)pEntries
);
1034 case GdiPalGetEntries
:
1035 ret
= IntGetPaletteEntries((HPALETTE
)hObj
, iStart
, cEntries
, (LPPALETTEENTRY
)pEntries
);
1038 case GdiPalGetSystemEntries
:
1039 ret
= IntGetSystemPaletteEntries((HDC
)hObj
, iStart
, cEntries
, (LPPALETTEENTRY
)pEntries
);
1042 case GdiPalSetColorTable
:
1044 ret
= IntSetDIBColorTable((HDC
)hObj
, iStart
, cEntries
, (RGBQUAD
*)pEntries
);
1047 case GdiPalGetColorTable
:
1049 ret
= IntGetDIBColorTable((HDC
)hObj
, iStart
, cEntries
, (RGBQUAD
*)pEntries
);
1059 ProbeForWrite(pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
), 1);
1060 memcpy(pUnsafeEntries
, pEntries
, cEntries
* sizeof(PALETTEENTRY
));
1062 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1068 ExFreePoolWithTag(pEntries
, TAG_PALETTE
);
1075 NtGdiSetSystemPaletteUse(HDC hDC
, UINT Usage
)
1077 UINT old
= SystemPaletteUse
;
1079 /* Device doesn't support colour palettes */
1080 if (!(NtGdiGetDeviceCaps(hDC
, RASTERCAPS
) & RC_PALETTE
)) {
1081 return SYSPAL_ERROR
;
1086 case SYSPAL_NOSTATIC
:
1087 case SYSPAL_NOSTATIC256
:
1089 SystemPaletteUse
= Usage
;
1102 NtGdiGetSystemPaletteUse(HDC hDC
)
1104 return SystemPaletteUse
;
1109 NtGdiUpdateColors(HDC hDC
)
1112 BOOL calledFromUser
, ret
;
1113 USER_REFERENCE_ENTRY Ref
;
1115 calledFromUser
= UserIsEntered();
1117 if (!calledFromUser
){
1118 UserEnterExclusive();
1121 Wnd
= UserGetWindowObject(IntWindowFromDC(hDC
));
1124 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
1126 if (!calledFromUser
){
1133 UserRefObjectCo(Wnd
, &Ref
);
1134 ret
= co_UserRedrawWindow(Wnd
, NULL
, 0, RDW_INVALIDATE
);
1135 UserDerefObjectCo(Wnd
);
1137 if (!calledFromUser
){
1146 NtGdiUnrealizeObject(HGDIOBJ hgdiobj
)
1152 ((UINT
)hgdiobj
& GDI_HANDLE_STOCK_MASK
) ||
1153 !GDI_HANDLE_IS_TYPE(hgdiobj
, GDI_OBJECT_TYPE_PALETTE
) )
1156 palGDI
= PALETTE_LockPalette(hgdiobj
);
1157 if (!palGDI
) return FALSE
;
1160 // Need to do something!!!
1161 // Zero out Current and Old Translated pointers?
1164 PALETTE_UnlockPalette(palGDI
);