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
;
19 const PALETTEENTRY g_sysPalTemplate
[NB_RESERVED_COLORS
] =
21 // first 10 entries in the system palette
22 // red green blue flags
23 { 0x00, 0x00, 0x00, PC_SYS_USED
},
24 { 0x80, 0x00, 0x00, PC_SYS_USED
},
25 { 0x00, 0x80, 0x00, PC_SYS_USED
},
26 { 0x80, 0x80, 0x00, PC_SYS_USED
},
27 { 0x00, 0x00, 0x80, PC_SYS_USED
},
28 { 0x80, 0x00, 0x80, PC_SYS_USED
},
29 { 0x00, 0x80, 0x80, PC_SYS_USED
},
30 { 0xc0, 0xc0, 0xc0, PC_SYS_USED
},
31 { 0xc0, 0xdc, 0xc0, PC_SYS_USED
},
32 { 0xa6, 0xca, 0xf0, PC_SYS_USED
},
34 // ... c_min/2 dynamic colorcells
35 // ... gap (for sparse palettes)
36 // ... c_min/2 dynamic colorcells
38 { 0xff, 0xfb, 0xf0, PC_SYS_USED
},
39 { 0xa0, 0xa0, 0xa4, PC_SYS_USED
},
40 { 0x80, 0x80, 0x80, PC_SYS_USED
},
41 { 0xff, 0x00, 0x00, PC_SYS_USED
},
42 { 0x00, 0xff, 0x00, PC_SYS_USED
},
43 { 0xff, 0xff, 0x00, PC_SYS_USED
},
44 { 0x00, 0x00, 0xff, PC_SYS_USED
},
45 { 0xff, 0x00, 0xff, PC_SYS_USED
},
46 { 0x00, 0xff, 0xff, PC_SYS_USED
},
47 { 0xff, 0xff, 0xff, PC_SYS_USED
} // last 10
50 unsigned short GetNumberOfBits(unsigned int dwMask
)
53 for (wBits
= 0; dwMask
; dwMask
= dwMask
& (dwMask
- 1))
58 // Create the system palette
59 HPALETTE FASTCALL
PALETTE_Init(VOID
)
68 // create default palette (20 system colors)
69 palPtr
= ExAllocatePoolWithTag(PagedPool
,
71 (NB_RESERVED_COLORS
* sizeof(PALETTEENTRY
)),
73 if (!palPtr
) return FALSE
;
75 palPtr
->palVersion
= 0x300;
76 palPtr
->palNumEntries
= NB_RESERVED_COLORS
;
77 for (i
=0; i
<NB_RESERVED_COLORS
; i
++)
79 palPtr
->palPalEntry
[i
].peRed
= g_sysPalTemplate
[i
].peRed
;
80 palPtr
->palPalEntry
[i
].peGreen
= g_sysPalTemplate
[i
].peGreen
;
81 palPtr
->palPalEntry
[i
].peBlue
= g_sysPalTemplate
[i
].peBlue
;
82 palPtr
->palPalEntry
[i
].peFlags
= 0;
85 hpalette
= NtGdiCreatePaletteInternal(palPtr
,NB_RESERVED_COLORS
);
86 ExFreePoolWithTag(palPtr
, TAG_PALETTE
);
89 palObj
= (PALOBJ
*)PALETTE_LockPalette(hpalette
);
92 if (!(palObj
->mapping
= ExAllocatePool(PagedPool
, sizeof(int) * 20)))
94 DbgPrint("Win32k: Can not create palette mapping -- out of memory!");
97 PALETTE_UnlockPalette(palObj
);
101 /* palette_size = visual->map_entries; */
103 gpalRGB
.Mode
= PAL_RGB
;
104 gpalRGB
.RedMask
= RGB(0xFF, 0x00, 0x00);
105 gpalRGB
.GreenMask
= RGB(0x00, 0xFF, 0x00);
106 gpalRGB
.BlueMask
= RGB(0x00, 0x00, 0xFF);
107 gpalRGB
.BaseObject
.ulShareCount
= 0;
108 gpalRGB
.BaseObject
.BaseFlags
= 0 ;
110 gpalBGR
.Mode
= PAL_BGR
;
111 gpalBGR
.RedMask
= RGB(0x00, 0x00, 0xFF);
112 gpalBGR
.GreenMask
= RGB(0x00, 0xFF, 0x00);
113 gpalBGR
.BlueMask
= RGB(0xFF, 0x00, 0x00);
114 gpalBGR
.BaseObject
.ulShareCount
= 0;
115 gpalBGR
.BaseObject
.BaseFlags
= 0 ;
117 gpalRGB555
.Mode
= PAL_RGB16_555
| PAL_BITFIELDS
;
118 gpalRGB555
.RedMask
= 0x7C00;
119 gpalRGB555
.GreenMask
= 0x3E0;
120 gpalRGB555
.BlueMask
= 0x1F;
121 gpalRGB555
.BaseObject
.ulShareCount
= 0;
122 gpalRGB555
.BaseObject
.BaseFlags
= 0 ;
124 gpalRGB565
.Mode
= PAL_RGB16_565
| PAL_BITFIELDS
;
125 gpalRGB565
.RedMask
= 0xF800;
126 gpalRGB565
.GreenMask
= 0x7E0;
127 gpalRGB565
.BlueMask
= 0x1F;
128 gpalRGB565
.BaseObject
.ulShareCount
= 0;
129 gpalRGB565
.BaseObject
.BaseFlags
= 0 ;
131 memset(&gpalMono
, 0, sizeof(PALETTE
));
132 gpalMono
.Mode
= PAL_MONOCHROME
;
133 gpalMono
.BaseObject
.ulShareCount
= 0;
134 gpalMono
.BaseObject
.BaseFlags
= 0 ;
139 VOID FASTCALL
PALETTE_ValidateFlags(PALETTEENTRY
* lpPalE
, INT size
)
143 lpPalE
[i
].peFlags
= PC_SYS_USED
| (lpPalE
[i
].peFlags
& 0x07);
148 PALETTE_AllocPalette(ULONG Mode
,
158 PalGDI
= (PPALETTE
)GDIOBJ_AllocObjWithHandle(GDI_OBJECT_TYPE_PALETTE
);
164 NewPalette
= PalGDI
->BaseObject
.hHmgr
;
166 PalGDI
->Self
= NewPalette
;
171 PalGDI
->IndexedColors
= ExAllocatePoolWithTag(PagedPool
,
172 sizeof(PALETTEENTRY
) * NumColors
,
174 if (NULL
== PalGDI
->IndexedColors
)
176 PALETTE_UnlockPalette(PalGDI
);
177 PALETTE_FreePaletteByHandle(NewPalette
);
180 RtlCopyMemory(PalGDI
->IndexedColors
, Colors
, sizeof(PALETTEENTRY
) * NumColors
);
183 if (PAL_INDEXED
== Mode
)
185 PalGDI
->NumColors
= NumColors
;
187 else if (PAL_BITFIELDS
== Mode
)
189 PalGDI
->RedMask
= Red
;
190 PalGDI
->GreenMask
= Green
;
191 PalGDI
->BlueMask
= Blue
;
193 if (Red
== 0x7c00 && Green
== 0x3E0 && Blue
== 0x1F)
194 PalGDI
->Mode
|= PAL_RGB16_555
;
195 else if (Red
== 0xF800 && Green
== 0x7E0 && Blue
== 0x1F)
196 PalGDI
->Mode
|= PAL_RGB16_565
;
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
->Mode
= 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 ExFreePool(pPal
->IndexedColors
);
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
->Mode
& 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
->Mode
& PAL_INDEXED
|| ppal
->Mode
& 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
->Mode
& 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
->Mode
& 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
->Mode
& 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("waring 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
->Mode
& PAL_INDEXED
)
672 index
= PALETTE_ulGetNearestPaletteIndex(palGDI
, Color
);
673 nearest
= PALETTE_ulGetRGBColorFromIndex(palGDI
, index
);
675 else if (palGDI
->Mode
& PAL_RGB
|| palGDI
->Mode
& PAL_BGR
)
679 else if (palGDI
->Mode
& 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
->Mode
& 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
;
732 USHORT sysMode
, palMode
;
737 systemPalette
= NtGdiGetStockObject(DEFAULT_PALETTE
);
738 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
742 DPRINT1("IntGdiRealizePalette(): palGDI is NULL, exiting\n");
747 sysGDI
= PALETTE_LockPalette(systemPalette
);
751 DPRINT1("IntGdiRealizePalette(): sysGDI is NULL, exiting\n");
752 PALETTE_UnlockPalette(palGDI
);
757 // The RealizePalette function modifies the palette for the device associated with the specified device context. If the
758 // device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device
759 // context is a display DC, the physical palette for that device is modified.
760 if(dc
->dctype
== DC_TYPE_MEMORY
)
763 DPRINT1("RealizePalette unimplemented for memory managed DCs\n");
766 DPRINT1("RealizePalette unimplemented for device DCs\n");
769 // need to pass this to IntEngCreateXlate with palettes unlocked
770 sysMode
= sysGDI
->Mode
;
771 palMode
= palGDI
->Mode
;
772 PALETTE_UnlockPalette(sysGDI
);
773 PALETTE_UnlockPalette(palGDI
);
781 IntAnimatePalette(HPALETTE hPal
,
784 CONST PPALETTEENTRY PaletteColors
)
788 if( hPal
!= NtGdiGetStockObject(DEFAULT_PALETTE
) )
795 const PALETTEENTRY
*pptr
= PaletteColors
;
797 palPtr
= (PPALETTE
)PALETTE_LockPalette(hPal
);
798 if (!palPtr
) return FALSE
;
800 pal_entries
= palPtr
->NumColors
;
801 if (StartIndex
>= pal_entries
)
803 PALETTE_UnlockPalette(palPtr
);
806 if (StartIndex
+NumEntries
> pal_entries
) NumEntries
= pal_entries
- StartIndex
;
808 for (NumEntries
+= StartIndex
; StartIndex
< NumEntries
; StartIndex
++, pptr
++)
810 /* According to MSDN, only animate PC_RESERVED colours */
811 if (palPtr
->IndexedColors
[StartIndex
].peFlags
& PC_RESERVED
)
813 memcpy( &palPtr
->IndexedColors
[StartIndex
], pptr
,
814 sizeof(PALETTEENTRY
) );
816 PALETTE_ValidateFlags(&palPtr
->IndexedColors
[StartIndex
], 1);
820 PALETTE_UnlockPalette(palPtr
);
822 /* Immediately apply the new palette if current window uses it */
823 Wnd
= UserGetDesktopWindow();
824 hDC
= UserGetWindowDC(Wnd
);
828 if (dc
->dclevel
.hpal
== hPal
)
831 IntGdiRealizePalette(hDC
);
836 UserReleaseDC(Wnd
,hDC
, FALSE
);
842 IntGetPaletteEntries(
851 palGDI
= (PPALETTE
) PALETTE_LockPalette(hpal
);
857 numEntries
= palGDI
->NumColors
;
860 if (numEntries
< StartIndex
+ Entries
)
862 Entries
= numEntries
- StartIndex
;
864 if (numEntries
<= StartIndex
)
866 PALETTE_UnlockPalette(palGDI
);
869 memcpy(pe
, palGDI
->IndexedColors
+ StartIndex
, Entries
* sizeof(PALETTEENTRY
));
873 Entries
= numEntries
;
876 PALETTE_UnlockPalette(palGDI
);
881 IntGetSystemPaletteEntries(HDC hDC
,
886 PPALETTE palGDI
= NULL
;
888 UINT EntriesSize
= 0;
893 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
899 EntriesSize
= Entries
* sizeof(pe
[0]);
900 if (Entries
!= EntriesSize
/ sizeof(pe
[0]))
902 /* Integer overflow! */
903 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
908 if (!(dc
= DC_LockDc(hDC
)))
910 SetLastWin32Error(ERROR_INVALID_HANDLE
);
914 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
919 if (StartIndex
>= palGDI
->NumColors
)
921 else if (Entries
> palGDI
->NumColors
- StartIndex
)
922 Entries
= palGDI
->NumColors
- StartIndex
;
925 palGDI
->IndexedColors
+ StartIndex
,
926 Entries
* sizeof(pe
[0]));
932 Ret
= dc
->ppdev
->gdiinfo
.ulNumPalReg
;
937 PALETTE_UnlockPalette(palGDI
);
947 IntSetPaletteEntries(
951 CONST LPPALETTEENTRY pe
)
956 if ((UINT
)hpal
& GDI_HANDLE_STOCK_MASK
)
961 palGDI
= PALETTE_LockPalette(hpal
);
962 if (!palGDI
) return 0;
964 numEntries
= palGDI
->NumColors
;
965 if (Start
>= numEntries
)
967 PALETTE_UnlockPalette(palGDI
);
970 if (numEntries
< Start
+ Entries
)
972 Entries
= numEntries
- Start
;
974 memcpy(palGDI
->IndexedColors
+ Start
, pe
, Entries
* sizeof(PALETTEENTRY
));
975 PALETTE_UnlockPalette(palGDI
);
987 IN LPVOID pUnsafeEntries
,
992 LPVOID pEntries
= NULL
;
994 /* FIXME: Handle bInbound correctly */
997 (pUnsafeEntries
== NULL
|| cEntries
== 0))
1004 pEntries
= ExAllocatePool(PagedPool
, cEntries
* sizeof(PALETTEENTRY
));
1011 ProbeForRead(pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
), 1);
1012 memcpy(pEntries
, pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
));
1014 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1016 ExFreePool(pEntries
);
1017 _SEH2_YIELD(return 0);
1028 ret
= IntAnimatePalette((HPALETTE
)hObj
, iStart
, cEntries
, (CONST PPALETTEENTRY
)pEntries
);
1031 case GdiPalSetEntries
:
1033 ret
= IntSetPaletteEntries((HPALETTE
)hObj
, iStart
, cEntries
, (CONST LPPALETTEENTRY
)pEntries
);
1036 case GdiPalGetEntries
:
1037 ret
= IntGetPaletteEntries((HPALETTE
)hObj
, iStart
, cEntries
, (LPPALETTEENTRY
)pEntries
);
1040 case GdiPalGetSystemEntries
:
1041 ret
= IntGetSystemPaletteEntries((HDC
)hObj
, iStart
, cEntries
, (LPPALETTEENTRY
)pEntries
);
1044 case GdiPalSetColorTable
:
1046 ret
= IntSetDIBColorTable((HDC
)hObj
, iStart
, cEntries
, (RGBQUAD
*)pEntries
);
1049 case GdiPalGetColorTable
:
1051 ret
= IntGetDIBColorTable((HDC
)hObj
, iStart
, cEntries
, (RGBQUAD
*)pEntries
);
1061 ProbeForWrite(pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
), 1);
1062 memcpy(pUnsafeEntries
, pEntries
, cEntries
* sizeof(PALETTEENTRY
));
1064 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1070 ExFreePool(pEntries
);
1077 NtGdiSetSystemPaletteUse(HDC hDC
, UINT Usage
)
1079 UINT old
= SystemPaletteUse
;
1081 /* Device doesn't support colour palettes */
1082 if (!(NtGdiGetDeviceCaps(hDC
, RASTERCAPS
) & RC_PALETTE
)) {
1083 return SYSPAL_ERROR
;
1088 case SYSPAL_NOSTATIC
:
1089 case SYSPAL_NOSTATIC256
:
1091 SystemPaletteUse
= Usage
;
1104 NtGdiGetSystemPaletteUse(HDC hDC
)
1106 return SystemPaletteUse
;
1111 NtGdiUpdateColors(HDC hDC
)
1114 BOOL calledFromUser
, ret
;
1115 USER_REFERENCE_ENTRY Ref
;
1117 calledFromUser
= UserIsEntered();
1119 if (!calledFromUser
){
1120 UserEnterExclusive();
1123 Wnd
= UserGetWindowObject(IntWindowFromDC(hDC
));
1126 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
1128 if (!calledFromUser
){
1135 UserRefObjectCo(Wnd
, &Ref
);
1136 ret
= co_UserRedrawWindow(Wnd
, NULL
, 0, RDW_INVALIDATE
);
1137 UserDerefObjectCo(Wnd
);
1139 if (!calledFromUser
){
1148 NtGdiUnrealizeObject(HGDIOBJ hgdiobj
)
1154 ((UINT
)hgdiobj
& GDI_HANDLE_STOCK_MASK
) ||
1155 !GDI_HANDLE_IS_TYPE(hgdiobj
, GDI_OBJECT_TYPE_PALETTE
) )
1158 palGDI
= PALETTE_LockPalette(hgdiobj
);
1159 if (!palGDI
) return FALSE
;
1162 // Need to do something!!!
1163 // Zero out Current and Old Translated pointers?
1166 PALETTE_UnlockPalette(palGDI
);