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
.flFlags
= 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
.flFlags
= 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
.flFlags
= 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
.flFlags
= 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
.flFlags
= 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
] = &gpalBGR
;
128 appalSurfaceDefault
[BMF_32BPP
] = &gpalBGR
;
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
;
165 PalGDI
->flFlags
= Mode
;
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 (Mode
& PAL_INDEXED
)
183 PalGDI
->NumColors
= NumColors
;
185 else if (Mode
& PAL_BITFIELDS
)
187 PalGDI
->RedMask
= Red
;
188 PalGDI
->GreenMask
= Green
;
189 PalGDI
->BlueMask
= Blue
;
191 if (Red
== 0x7c00 && Green
== 0x3E0 && Blue
== 0x1F)
192 PalGDI
->flFlags
|= PAL_RGB16_555
;
193 else if (Red
== 0xF800 && Green
== 0x7E0 && Blue
== 0x1F)
194 PalGDI
->flFlags
|= PAL_RGB16_565
;
195 else if (Red
== 0xFF0000 && Green
== 0xFF00 && Blue
== 0xFF)
196 PalGDI
->flFlags
|= PAL_BGR
;
199 PALETTE_UnlockPalette(PalGDI
);
206 PALETTE_AllocPaletteIndexedRGB(ULONG NumColors
,
207 CONST RGBQUAD
*Colors
)
213 PalGDI
= (PPALETTE
)GDIOBJ_AllocObjWithHandle(GDI_OBJECT_TYPE_PALETTE
);
219 NewPalette
= PalGDI
->BaseObject
.hHmgr
;
221 PalGDI
->Self
= NewPalette
;
222 PalGDI
->flFlags
= PAL_INDEXED
;
224 PalGDI
->IndexedColors
= ExAllocatePoolWithTag(PagedPool
,
225 sizeof(PALETTEENTRY
) * NumColors
,
227 if (NULL
== PalGDI
->IndexedColors
)
229 PALETTE_UnlockPalette(PalGDI
);
230 PALETTE_FreePaletteByHandle(NewPalette
);
234 for (i
= 0; i
< NumColors
; i
++)
236 PalGDI
->IndexedColors
[i
].peRed
= Colors
[i
].rgbRed
;
237 PalGDI
->IndexedColors
[i
].peGreen
= Colors
[i
].rgbGreen
;
238 PalGDI
->IndexedColors
[i
].peBlue
= Colors
[i
].rgbBlue
;
239 PalGDI
->IndexedColors
[i
].peFlags
= 0;
242 PalGDI
->NumColors
= NumColors
;
244 PALETTE_UnlockPalette(PalGDI
);
250 PALETTE_Cleanup(PVOID ObjectBody
)
252 PPALETTE pPal
= (PPALETTE
)ObjectBody
;
253 if (NULL
!= pPal
->IndexedColors
)
255 ExFreePoolWithTag(pPal
->IndexedColors
, TAG_PALETTE
);
262 PALETTE_GetObject(PPALETTE ppal
, INT cbCount
, LPLOGBRUSH lpBuffer
)
269 if ((UINT
)cbCount
< sizeof(WORD
)) return 0;
270 *((WORD
*)lpBuffer
) = (WORD
)ppal
->NumColors
;
276 PALETTE_ulGetNearestPaletteIndex(PALETTE
* ppal
, ULONG iColor
)
278 ULONG ulDiff
, ulColorDiff
, ulMinimalDiff
= 0xFFFFFF;
279 ULONG i
, ulBestIndex
= 0;
280 PALETTEENTRY peColor
= *(PPALETTEENTRY
)&iColor
;
282 /* Loop all palette entries, break on exact match */
283 for (i
= 0; i
< ppal
->NumColors
&& ulMinimalDiff
!= 0; i
++)
285 /* Calculate distance in the color cube */
286 ulDiff
= peColor
.peRed
- ppal
->IndexedColors
[i
].peRed
;
287 ulColorDiff
= ulDiff
* ulDiff
;
288 ulDiff
= peColor
.peGreen
- ppal
->IndexedColors
[i
].peGreen
;
289 ulColorDiff
+= ulDiff
* ulDiff
;
290 ulDiff
= peColor
.peBlue
- ppal
->IndexedColors
[i
].peBlue
;
291 ulColorDiff
+= ulDiff
* ulDiff
;
293 /* Check for a better match */
294 if (ulColorDiff
< ulMinimalDiff
)
297 ulMinimalDiff
= ulColorDiff
;
306 PALETTE_ulGetNearestBitFieldsIndex(PALETTE
* ppal
, ULONG ulColor
)
310 // FIXME: HACK, should be stored already
311 ppal
->ulRedShift
= CalculateShift(RGB(0xff,0,0), ppal
->RedMask
);
312 ppal
->ulGreenShift
= CalculateShift(RGB(0,0xff,0), ppal
->GreenMask
);
313 ppal
->ulBlueShift
= CalculateShift(RGB(0,0,0xff), ppal
->BlueMask
);
315 ulNewColor
= _rotl(ulColor
, ppal
->ulRedShift
) & ppal
->RedMask
;
316 ulNewColor
|= _rotl(ulColor
, ppal
->ulGreenShift
) & ppal
->GreenMask
;
317 ulNewColor
|= _rotl(ulColor
, ppal
->ulBlueShift
) & ppal
->BlueMask
;
324 PALETTE_ulGetNearestIndex(PALETTE
* ppal
, ULONG ulColor
)
326 if (ppal
->flFlags
& PAL_INDEXED
) // use fl & PALINDEXED
327 return PALETTE_ulGetNearestPaletteIndex(ppal
, ulColor
);
329 return PALETTE_ulGetNearestBitFieldsIndex(ppal
, ulColor
);
334 PALETTE_vGetBitMasks(PPALETTE ppal
, PULONG pulColors
)
338 if (ppal
->flFlags
& PAL_INDEXED
|| ppal
->flFlags
& PAL_RGB
)
340 pulColors
[0] = RGB(0xFF, 0x00, 0x00);
341 pulColors
[1] = RGB(0x00, 0xFF, 0x00);
342 pulColors
[2] = RGB(0x00, 0x00, 0xFF);
344 else if (ppal
->flFlags
& PAL_BGR
)
346 pulColors
[0] = RGB(0x00, 0x00, 0xFF);
347 pulColors
[1] = RGB(0x00, 0xFF, 0x00);
348 pulColors
[2] = RGB(0xFF, 0x00, 0x00);
350 else if (ppal
->flFlags
& PAL_BITFIELDS
)
352 pulColors
[0] = ppal
->RedMask
;
353 pulColors
[1] = ppal
->GreenMask
;
354 pulColors
[2] = ppal
->BlueMask
;
360 ColorCorrection(PPALETTE PalGDI
, PPALETTEENTRY PaletteEntry
, ULONG Colors
)
362 PPDEVOBJ ppdev
= (PPDEVOBJ
)PalGDI
->hPDev
;
366 if (ppdev
->flFlags
& PDEV_GAMMARAMP_TABLE
)
369 PGAMMARAMP GammaRamp
= (PGAMMARAMP
)ppdev
->pvGammaRamp
;
370 for ( i
= 0; i
< Colors
; i
++)
372 PaletteEntry
[i
].peRed
+= GammaRamp
->Red
[i
];
373 PaletteEntry
[i
].peGreen
+= GammaRamp
->Green
[i
];
374 PaletteEntry
[i
].peBlue
+= GammaRamp
->Blue
[i
];
380 /** Display Driver Interface **************************************************/
397 Palette
= PALETTE_AllocPalette(Mode
, NumColors
, Colors
, Red
, Green
, Blue
);
400 GDIOBJ_SetOwnership(Palette
, NULL
);
411 EngDeletePalette(IN HPALETTE Palette
)
413 GDIOBJ_SetOwnership(Palette
, PsGetCurrentProcess());
415 return PALETTE_FreePaletteByHandle(Palette
);
423 PALOBJ_cGetColors(PALOBJ
*PalObj
, ULONG Start
, ULONG Colors
, ULONG
*PaletteEntry
)
427 PalGDI
= (PALETTE
*)PalObj
;
428 /* PalGDI = (PALETTE*)AccessInternalObjectFromUserObject(PalObj); */
430 if (Start
>= PalGDI
->NumColors
)
433 Colors
= min(Colors
, PalGDI
->NumColors
- Start
);
435 /* NOTE: PaletteEntry ULONGs are in the same order as PALETTEENTRY. */
436 RtlCopyMemory(PaletteEntry
, PalGDI
->IndexedColors
+ Start
, sizeof(ULONG
) * Colors
);
438 if (PalGDI
->flFlags
& PAL_GAMMACORRECTION
)
439 ColorCorrection(PalGDI
, (PPALETTEENTRY
)PaletteEntry
, Colors
);
445 /** Systemcall Interface ******************************************************/
451 NtGdiCreatePaletteInternal ( IN LPLOGPALETTE pLogPal
, IN UINT cEntries
)
456 pLogPal
->palNumEntries
= cEntries
;
457 NewPalette
= PALETTE_AllocPalette( PAL_INDEXED
,
459 (PULONG
)pLogPal
->palPalEntry
,
462 if (NewPalette
== NULL
)
467 PalGDI
= (PPALETTE
) PALETTE_LockPalette(NewPalette
);
470 PALETTE_ValidateFlags(PalGDI
->IndexedColors
, PalGDI
->NumColors
);
471 PALETTE_UnlockPalette(PalGDI
);
475 /* FIXME - Handle PalGDI == NULL!!!! */
476 DPRINT1("PalGDI is NULL\n");
481 HPALETTE APIENTRY
NtGdiCreateHalftonePalette(HDC hDC
)
486 WORD NumberOfEntries
;
487 PALETTEENTRY aEntries
[256];
490 Palette
.Version
= 0x300;
491 Palette
.NumberOfEntries
= 256;
492 if (IntGetSystemPaletteEntries(hDC
, 0, 256, Palette
.aEntries
) == 0)
494 /* from wine, more that 256 color math */
495 Palette
.NumberOfEntries
= 20;
496 for (i
= 0; i
< Palette
.NumberOfEntries
; i
++)
498 Palette
.aEntries
[i
].peRed
=0xff;
499 Palette
.aEntries
[i
].peGreen
=0xff;
500 Palette
.aEntries
[i
].peBlue
=0xff;
501 Palette
.aEntries
[i
].peFlags
=0x00;
504 Palette
.aEntries
[0].peRed
=0x00;
505 Palette
.aEntries
[0].peBlue
=0x00;
506 Palette
.aEntries
[0].peGreen
=0x00;
509 for (i
=1; i
<= 6; i
++)
511 Palette
.aEntries
[i
].peRed
=(i
%2)?0x80:0;
512 Palette
.aEntries
[i
].peGreen
=(i
==2)?0x80:(i
==3)?0x80:(i
==6)?0x80:0;
513 Palette
.aEntries
[i
].peBlue
=(i
>3)?0x80:0;
516 for (i
=7; i
<= 12; i
++)
521 Palette
.aEntries
[i
].peRed
=0xc0;
522 Palette
.aEntries
[i
].peBlue
=0xc0;
523 Palette
.aEntries
[i
].peGreen
=0xc0;
526 Palette
.aEntries
[i
].peRed
=0xc0;
527 Palette
.aEntries
[i
].peGreen
=0xdc;
528 Palette
.aEntries
[i
].peBlue
=0xc0;
531 Palette
.aEntries
[i
].peRed
=0xa6;
532 Palette
.aEntries
[i
].peGreen
=0xca;
533 Palette
.aEntries
[i
].peBlue
=0xf0;
536 Palette
.aEntries
[i
].peRed
=0xff;
537 Palette
.aEntries
[i
].peGreen
=0xfb;
538 Palette
.aEntries
[i
].peBlue
=0xf0;
541 Palette
.aEntries
[i
].peRed
=0xa0;
542 Palette
.aEntries
[i
].peGreen
=0xa0;
543 Palette
.aEntries
[i
].peBlue
=0xa4;
546 Palette
.aEntries
[i
].peRed
=0x80;
547 Palette
.aEntries
[i
].peGreen
=0x80;
548 Palette
.aEntries
[i
].peBlue
=0x80;
552 for (i
=13; i
<= 18; i
++)
554 Palette
.aEntries
[i
].peRed
=(i
%2)?0xff:0;
555 Palette
.aEntries
[i
].peGreen
=(i
==14)?0xff:(i
==15)?0xff:(i
==18)?0xff:0;
556 Palette
.aEntries
[i
].peBlue
=(i
>15)?0xff:0x00;
561 /* 256 color table */
562 for (r
= 0; r
< 6; r
++)
563 for (g
= 0; g
< 6; g
++)
564 for (b
= 0; b
< 6; b
++)
566 i
= r
+ g
*6 + b
*36 + 10;
567 Palette
.aEntries
[i
].peRed
= r
* 51;
568 Palette
.aEntries
[i
].peGreen
= g
* 51;
569 Palette
.aEntries
[i
].peBlue
= b
* 51;
572 for (i
= 216; i
< 246; i
++)
574 int v
= (i
- 216) << 3;
575 Palette
.aEntries
[i
].peRed
= v
;
576 Palette
.aEntries
[i
].peGreen
= v
;
577 Palette
.aEntries
[i
].peBlue
= v
;
581 return NtGdiCreatePaletteInternal((LOGPALETTE
*)&Palette
, Palette
.NumberOfEntries
);
590 /* PALOBJ *palPtr = (PALOBJ*)AccessUserObject(hPal);
591 UINT cPrevEnt, prevVer;
592 INT prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
593 XLATEOBJ *XlateObj = NULL;
595 if(!palPtr) return FALSE;
596 cPrevEnt = palPtr->logpalette->palNumEntries;
597 prevVer = palPtr->logpalette->palVersion;
598 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) + sizeof(int*) + sizeof(GDIOBJHDR);
599 size += sizeof(int*) + sizeof(GDIOBJHDR);
600 XlateObj = palPtr->logicalToSystem;
602 if (!(palPtr = GDI_ReallocObject(size, hPal, palPtr))) return FALSE;
606 XLATEOBJ *NewXlateObj = (int*) HeapReAlloc(GetProcessHeap(), 0, XlateObj, cEntries * sizeof(int));
607 if(NewXlateObj == NULL)
609 ERR("Can not resize logicalToSystem -- out of memory!");
610 GDI_ReleaseObj( hPal );
613 palPtr->logicalToSystem = NewXlateObj;
616 if(cEntries > cPrevEnt)
618 if(XlateObj) memset(palPtr->logicalToSystem + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
619 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
620 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize), cEntries - cPrevEnt );
622 palPtr->logpalette->palNumEntries = cEntries;
623 palPtr->logpalette->palVersion = prevVer;
624 // GDI_ReleaseObj( hPal );
633 NtGdiGetColorAdjustment(
635 LPCOLORADJUSTMENT pca
)
643 NtGdiSetColorAdjustment(
645 LPCOLORADJUSTMENT pca
)
651 COLORREF APIENTRY
NtGdiGetNearestColor(HDC hDC
, COLORREF Color
)
653 COLORREF nearest
= CLR_INVALID
;
656 LONG RBits
, GBits
, BBits
;
661 HPALETTE hpal
= dc
->dclevel
.hpal
;
662 palGDI
= (PPALETTE
) PALETTE_LockPalette(hpal
);
669 if (palGDI
->flFlags
& PAL_INDEXED
)
672 index
= PALETTE_ulGetNearestPaletteIndex(palGDI
, Color
);
673 nearest
= PALETTE_ulGetRGBColorFromIndex(palGDI
, index
);
675 else if (palGDI
->flFlags
& PAL_RGB
|| palGDI
->flFlags
& PAL_BGR
)
679 else if (palGDI
->flFlags
& PAL_BITFIELDS
)
681 RBits
= 8 - GetNumberOfBits(palGDI
->RedMask
);
682 GBits
= 8 - GetNumberOfBits(palGDI
->GreenMask
);
683 BBits
= 8 - GetNumberOfBits(palGDI
->BlueMask
);
685 (GetRValue(Color
) >> RBits
) << RBits
,
686 (GetGValue(Color
) >> GBits
) << GBits
,
687 (GetBValue(Color
) >> BBits
) << BBits
);
689 PALETTE_UnlockPalette(palGDI
);
698 NtGdiGetNearestPaletteIndex(
702 PPALETTE ppal
= (PPALETTE
) PALETTE_LockPalette(hpal
);
707 if (ppal
->flFlags
& PAL_INDEXED
)
709 /* Return closest match for the given RGB color */
710 index
= PALETTE_ulGetNearestPaletteIndex(ppal
, crColor
);
712 // else SetLastError ?
713 PALETTE_UnlockPalette(ppal
);
721 IntGdiRealizePalette(HDC hDC
)
724 * This function doesn't do any real work now and there's plenty
728 PPALETTE palGDI
, sysGDI
;
731 HPALETTE systemPalette
;
736 systemPalette
= NtGdiGetStockObject(DEFAULT_PALETTE
);
737 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
741 DPRINT1("IntGdiRealizePalette(): palGDI is NULL, exiting\n");
746 sysGDI
= PALETTE_LockPalette(systemPalette
);
750 DPRINT1("IntGdiRealizePalette(): sysGDI is NULL, exiting\n");
751 PALETTE_UnlockPalette(palGDI
);
756 // The RealizePalette function modifies the palette for the device associated with the specified device context. If the
757 // device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device
758 // context is a display DC, the physical palette for that device is modified.
759 if(dc
->dctype
== DC_TYPE_MEMORY
)
762 DPRINT1("RealizePalette unimplemented for memory managed DCs\n");
765 DPRINT1("RealizePalette unimplemented for device DCs\n");
768 // need to pass this to IntEngCreateXlate with palettes unlocked
769 PALETTE_UnlockPalette(sysGDI
);
770 PALETTE_UnlockPalette(palGDI
);
778 IntAnimatePalette(HPALETTE hPal
,
781 CONST PPALETTEENTRY PaletteColors
)
785 if( hPal
!= NtGdiGetStockObject(DEFAULT_PALETTE
) )
792 const PALETTEENTRY
*pptr
= PaletteColors
;
794 palPtr
= (PPALETTE
)PALETTE_LockPalette(hPal
);
795 if (!palPtr
) return FALSE
;
797 pal_entries
= palPtr
->NumColors
;
798 if (StartIndex
>= pal_entries
)
800 PALETTE_UnlockPalette(palPtr
);
803 if (StartIndex
+NumEntries
> pal_entries
) NumEntries
= pal_entries
- StartIndex
;
805 for (NumEntries
+= StartIndex
; StartIndex
< NumEntries
; StartIndex
++, pptr
++)
807 /* According to MSDN, only animate PC_RESERVED colours */
808 if (palPtr
->IndexedColors
[StartIndex
].peFlags
& PC_RESERVED
)
810 memcpy( &palPtr
->IndexedColors
[StartIndex
], pptr
,
811 sizeof(PALETTEENTRY
) );
813 PALETTE_ValidateFlags(&palPtr
->IndexedColors
[StartIndex
], 1);
817 PALETTE_UnlockPalette(palPtr
);
819 /* Immediately apply the new palette if current window uses it */
820 Wnd
= UserGetDesktopWindow();
821 hDC
= UserGetWindowDC(Wnd
);
825 if (dc
->dclevel
.hpal
== hPal
)
828 IntGdiRealizePalette(hDC
);
833 UserReleaseDC(Wnd
,hDC
, FALSE
);
839 IntGetPaletteEntries(
848 palGDI
= (PPALETTE
) PALETTE_LockPalette(hpal
);
854 numEntries
= palGDI
->NumColors
;
857 if (numEntries
< StartIndex
+ Entries
)
859 Entries
= numEntries
- StartIndex
;
861 if (numEntries
<= StartIndex
)
863 PALETTE_UnlockPalette(palGDI
);
866 memcpy(pe
, palGDI
->IndexedColors
+ StartIndex
, Entries
* sizeof(PALETTEENTRY
));
870 Entries
= numEntries
;
873 PALETTE_UnlockPalette(palGDI
);
878 IntGetSystemPaletteEntries(HDC hDC
,
883 PPALETTE palGDI
= NULL
;
885 UINT EntriesSize
= 0;
890 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
896 EntriesSize
= Entries
* sizeof(pe
[0]);
897 if (Entries
!= EntriesSize
/ sizeof(pe
[0]))
899 /* Integer overflow! */
900 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
905 if (!(dc
= DC_LockDc(hDC
)))
907 SetLastWin32Error(ERROR_INVALID_HANDLE
);
911 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
916 if (StartIndex
>= palGDI
->NumColors
)
918 else if (Entries
> palGDI
->NumColors
- StartIndex
)
919 Entries
= palGDI
->NumColors
- StartIndex
;
922 palGDI
->IndexedColors
+ StartIndex
,
923 Entries
* sizeof(pe
[0]));
929 Ret
= dc
->ppdev
->gdiinfo
.ulNumPalReg
;
934 PALETTE_UnlockPalette(palGDI
);
944 IntSetPaletteEntries(
948 CONST LPPALETTEENTRY pe
)
953 if ((UINT
)hpal
& GDI_HANDLE_STOCK_MASK
)
958 palGDI
= PALETTE_LockPalette(hpal
);
959 if (!palGDI
) return 0;
961 numEntries
= palGDI
->NumColors
;
962 if (Start
>= numEntries
)
964 PALETTE_UnlockPalette(palGDI
);
967 if (numEntries
< Start
+ Entries
)
969 Entries
= numEntries
- Start
;
971 memcpy(palGDI
->IndexedColors
+ Start
, pe
, Entries
* sizeof(PALETTEENTRY
));
972 PALETTE_UnlockPalette(palGDI
);
984 IN LPVOID pUnsafeEntries
,
989 LPVOID pEntries
= NULL
;
991 /* FIXME: Handle bInbound correctly */
994 (pUnsafeEntries
== NULL
|| cEntries
== 0))
1001 pEntries
= ExAllocatePoolWithTag(PagedPool
, cEntries
* sizeof(PALETTEENTRY
), TAG_PALETTE
);
1008 ProbeForRead(pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
), 1);
1009 memcpy(pEntries
, pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
));
1011 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1013 ExFreePoolWithTag(pEntries
, TAG_PALETTE
);
1014 _SEH2_YIELD(return 0);
1025 ret
= IntAnimatePalette((HPALETTE
)hObj
, iStart
, cEntries
, (CONST PPALETTEENTRY
)pEntries
);
1028 case GdiPalSetEntries
:
1030 ret
= IntSetPaletteEntries((HPALETTE
)hObj
, iStart
, cEntries
, (CONST LPPALETTEENTRY
)pEntries
);
1033 case GdiPalGetEntries
:
1034 ret
= IntGetPaletteEntries((HPALETTE
)hObj
, iStart
, cEntries
, (LPPALETTEENTRY
)pEntries
);
1037 case GdiPalGetSystemEntries
:
1038 ret
= IntGetSystemPaletteEntries((HDC
)hObj
, iStart
, cEntries
, (LPPALETTEENTRY
)pEntries
);
1041 case GdiPalSetColorTable
:
1043 ret
= IntSetDIBColorTable((HDC
)hObj
, iStart
, cEntries
, (RGBQUAD
*)pEntries
);
1046 case GdiPalGetColorTable
:
1048 ret
= IntGetDIBColorTable((HDC
)hObj
, iStart
, cEntries
, (RGBQUAD
*)pEntries
);
1058 ProbeForWrite(pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
), 1);
1059 memcpy(pUnsafeEntries
, pEntries
, cEntries
* sizeof(PALETTEENTRY
));
1061 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1067 ExFreePoolWithTag(pEntries
, TAG_PALETTE
);
1074 NtGdiSetSystemPaletteUse(HDC hDC
, UINT Usage
)
1076 UINT old
= SystemPaletteUse
;
1078 /* Device doesn't support colour palettes */
1079 if (!(NtGdiGetDeviceCaps(hDC
, RASTERCAPS
) & RC_PALETTE
)) {
1080 return SYSPAL_ERROR
;
1085 case SYSPAL_NOSTATIC
:
1086 case SYSPAL_NOSTATIC256
:
1088 SystemPaletteUse
= Usage
;
1101 NtGdiGetSystemPaletteUse(HDC hDC
)
1103 return SystemPaletteUse
;
1108 NtGdiUpdateColors(HDC hDC
)
1111 BOOL calledFromUser
, ret
;
1112 USER_REFERENCE_ENTRY Ref
;
1114 calledFromUser
= UserIsEntered();
1116 if (!calledFromUser
){
1117 UserEnterExclusive();
1120 Wnd
= UserGetWindowObject(IntWindowFromDC(hDC
));
1123 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
1125 if (!calledFromUser
){
1132 UserRefObjectCo(Wnd
, &Ref
);
1133 ret
= co_UserRedrawWindow(Wnd
, NULL
, 0, RDW_INVALIDATE
);
1134 UserDerefObjectCo(Wnd
);
1136 if (!calledFromUser
){
1145 NtGdiUnrealizeObject(HGDIOBJ hgdiobj
)
1151 ((UINT
)hgdiobj
& GDI_HANDLE_STOCK_MASK
) ||
1152 !GDI_HANDLE_IS_TYPE(hgdiobj
, GDI_OBJECT_TYPE_PALETTE
) )
1155 palGDI
= PALETTE_LockPalette(hgdiobj
);
1156 if (!palGDI
) return FALSE
;
1159 // Need to do something!!!
1160 // Zero out Current and Old Translated pointers?
1163 PALETTE_UnlockPalette(palGDI
);