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
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
= 0;
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
= 0;
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
= 0;
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
= 0;
117 gpalRGB565
.BaseObject
.BaseFlags
= 0 ;
119 memset(&gpalMono
, 0, sizeof(PALETTE
));
120 gpalMono
.flFlags
= PAL_MONOCHROME
;
121 gpalMono
.BaseObject
.ulShareCount
= 0;
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_AllocPalette(ULONG Mode
,
159 PalGDI
= (PPALETTE
)GDIOBJ_AllocObjWithHandle(GDI_OBJECT_TYPE_PALETTE
);
165 NewPalette
= PalGDI
->BaseObject
.hHmgr
;
167 PalGDI
->Self
= NewPalette
;
168 PalGDI
->flFlags
= Mode
;
172 PalGDI
->IndexedColors
= ExAllocatePoolWithTag(PagedPool
,
173 sizeof(PALETTEENTRY
) * NumColors
,
175 if (NULL
== PalGDI
->IndexedColors
)
177 PALETTE_UnlockPalette(PalGDI
);
178 PALETTE_FreePaletteByHandle(NewPalette
);
181 RtlCopyMemory(PalGDI
->IndexedColors
, Colors
, sizeof(PALETTEENTRY
) * NumColors
);
184 if (Mode
& PAL_INDEXED
)
186 PalGDI
->NumColors
= NumColors
;
188 else if (Mode
& PAL_BITFIELDS
)
190 PalGDI
->RedMask
= Red
;
191 PalGDI
->GreenMask
= Green
;
192 PalGDI
->BlueMask
= Blue
;
194 if (Red
== 0x7c00 && Green
== 0x3E0 && Blue
== 0x1F)
195 PalGDI
->flFlags
|= PAL_RGB16_555
;
196 else if (Red
== 0xF800 && Green
== 0x7E0 && Blue
== 0x1F)
197 PalGDI
->flFlags
|= PAL_RGB16_565
;
198 else if (Red
== 0xFF0000 && Green
== 0xFF00 && Blue
== 0xFF)
199 PalGDI
->flFlags
|= PAL_BGR
;
200 else if (Red
== 0xFF && Green
== 0xFF00 && Blue
== 0xFF0000)
201 PalGDI
->flFlags
|= PAL_RGB
;
204 PALETTE_UnlockPalette(PalGDI
);
211 PALETTE_AllocPaletteIndexedRGB(ULONG NumColors
,
212 CONST RGBQUAD
*Colors
)
218 PalGDI
= (PPALETTE
)GDIOBJ_AllocObjWithHandle(GDI_OBJECT_TYPE_PALETTE
);
224 NewPalette
= PalGDI
->BaseObject
.hHmgr
;
226 PalGDI
->Self
= NewPalette
;
227 PalGDI
->flFlags
= PAL_INDEXED
;
229 PalGDI
->IndexedColors
= ExAllocatePoolWithTag(PagedPool
,
230 sizeof(PALETTEENTRY
) * NumColors
,
232 if (NULL
== PalGDI
->IndexedColors
)
234 PALETTE_UnlockPalette(PalGDI
);
235 PALETTE_FreePaletteByHandle(NewPalette
);
239 for (i
= 0; i
< NumColors
; i
++)
241 PalGDI
->IndexedColors
[i
].peRed
= Colors
[i
].rgbRed
;
242 PalGDI
->IndexedColors
[i
].peGreen
= Colors
[i
].rgbGreen
;
243 PalGDI
->IndexedColors
[i
].peBlue
= Colors
[i
].rgbBlue
;
244 PalGDI
->IndexedColors
[i
].peFlags
= 0;
247 PalGDI
->NumColors
= NumColors
;
249 PALETTE_UnlockPalette(PalGDI
);
255 PALETTE_Cleanup(PVOID ObjectBody
)
257 PPALETTE pPal
= (PPALETTE
)ObjectBody
;
258 if (NULL
!= pPal
->IndexedColors
)
260 ExFreePoolWithTag(pPal
->IndexedColors
, TAG_PALETTE
);
267 PALETTE_GetObject(PPALETTE ppal
, INT cbCount
, LPLOGBRUSH lpBuffer
)
274 if ((UINT
)cbCount
< sizeof(WORD
)) return 0;
275 *((WORD
*)lpBuffer
) = (WORD
)ppal
->NumColors
;
281 PALETTE_ulGetNearestPaletteIndex(PALETTE
* ppal
, ULONG iColor
)
283 ULONG ulDiff
, ulColorDiff
, ulMinimalDiff
= 0xFFFFFF;
284 ULONG i
, ulBestIndex
= 0;
285 PALETTEENTRY peColor
= *(PPALETTEENTRY
)&iColor
;
287 /* Loop all palette entries, break on exact match */
288 for (i
= 0; i
< ppal
->NumColors
&& ulMinimalDiff
!= 0; i
++)
290 /* Calculate distance in the color cube */
291 ulDiff
= peColor
.peRed
- ppal
->IndexedColors
[i
].peRed
;
292 ulColorDiff
= ulDiff
* ulDiff
;
293 ulDiff
= peColor
.peGreen
- ppal
->IndexedColors
[i
].peGreen
;
294 ulColorDiff
+= ulDiff
* ulDiff
;
295 ulDiff
= peColor
.peBlue
- ppal
->IndexedColors
[i
].peBlue
;
296 ulColorDiff
+= ulDiff
* ulDiff
;
298 /* Check for a better match */
299 if (ulColorDiff
< ulMinimalDiff
)
302 ulMinimalDiff
= ulColorDiff
;
311 PALETTE_ulGetNearestBitFieldsIndex(PALETTE
* ppal
, ULONG ulColor
)
315 // FIXME: HACK, should be stored already
316 ppal
->ulRedShift
= CalculateShift(RGB(0xff,0,0), ppal
->RedMask
);
317 ppal
->ulGreenShift
= CalculateShift(RGB(0,0xff,0), ppal
->GreenMask
);
318 ppal
->ulBlueShift
= CalculateShift(RGB(0,0,0xff), ppal
->BlueMask
);
320 ulNewColor
= _rotl(ulColor
, ppal
->ulRedShift
) & ppal
->RedMask
;
321 ulNewColor
|= _rotl(ulColor
, ppal
->ulGreenShift
) & ppal
->GreenMask
;
322 ulNewColor
|= _rotl(ulColor
, ppal
->ulBlueShift
) & ppal
->BlueMask
;
329 PALETTE_ulGetNearestIndex(PALETTE
* ppal
, ULONG ulColor
)
331 if (ppal
->flFlags
& PAL_INDEXED
) // use fl & PALINDEXED
332 return PALETTE_ulGetNearestPaletteIndex(ppal
, ulColor
);
334 return PALETTE_ulGetNearestBitFieldsIndex(ppal
, ulColor
);
339 PALETTE_vGetBitMasks(PPALETTE ppal
, PULONG pulColors
)
343 if (ppal
->flFlags
& PAL_INDEXED
|| ppal
->flFlags
& PAL_RGB
)
345 pulColors
[0] = RGB(0xFF, 0x00, 0x00);
346 pulColors
[1] = RGB(0x00, 0xFF, 0x00);
347 pulColors
[2] = RGB(0x00, 0x00, 0xFF);
349 else if (ppal
->flFlags
& PAL_BGR
)
351 pulColors
[0] = RGB(0x00, 0x00, 0xFF);
352 pulColors
[1] = RGB(0x00, 0xFF, 0x00);
353 pulColors
[2] = RGB(0xFF, 0x00, 0x00);
355 else if (ppal
->flFlags
& PAL_BITFIELDS
)
357 pulColors
[0] = ppal
->RedMask
;
358 pulColors
[1] = ppal
->GreenMask
;
359 pulColors
[2] = ppal
->BlueMask
;
365 ColorCorrection(PPALETTE PalGDI
, PPALETTEENTRY PaletteEntry
, ULONG Colors
)
367 PPDEVOBJ ppdev
= (PPDEVOBJ
)PalGDI
->hPDev
;
371 if (ppdev
->flFlags
& PDEV_GAMMARAMP_TABLE
)
374 PGAMMARAMP GammaRamp
= (PGAMMARAMP
)ppdev
->pvGammaRamp
;
375 for ( i
= 0; i
< Colors
; i
++)
377 PaletteEntry
[i
].peRed
+= GammaRamp
->Red
[i
];
378 PaletteEntry
[i
].peGreen
+= GammaRamp
->Green
[i
];
379 PaletteEntry
[i
].peBlue
+= GammaRamp
->Blue
[i
];
385 /** Display Driver Interface **************************************************/
402 Palette
= PALETTE_AllocPalette(Mode
, NumColors
, Colors
, Red
, Green
, Blue
);
405 GDIOBJ_SetOwnership(Palette
, NULL
);
416 EngDeletePalette(IN HPALETTE Palette
)
418 GDIOBJ_SetOwnership(Palette
, PsGetCurrentProcess());
420 return PALETTE_FreePaletteByHandle(Palette
);
428 PALOBJ_cGetColors(PALOBJ
*PalObj
, ULONG Start
, ULONG Colors
, ULONG
*PaletteEntry
)
432 PalGDI
= (PALETTE
*)PalObj
;
433 /* PalGDI = (PALETTE*)AccessInternalObjectFromUserObject(PalObj); */
435 if (Start
>= PalGDI
->NumColors
)
438 Colors
= min(Colors
, PalGDI
->NumColors
- Start
);
440 /* NOTE: PaletteEntry ULONGs are in the same order as PALETTEENTRY. */
441 RtlCopyMemory(PaletteEntry
, PalGDI
->IndexedColors
+ Start
, sizeof(ULONG
) * Colors
);
443 if (PalGDI
->flFlags
& PAL_GAMMACORRECTION
)
444 ColorCorrection(PalGDI
, (PPALETTEENTRY
)PaletteEntry
, Colors
);
450 /** Systemcall Interface ******************************************************/
456 NtGdiCreatePaletteInternal ( IN LPLOGPALETTE pLogPal
, IN UINT cEntries
)
461 pLogPal
->palNumEntries
= cEntries
;
462 NewPalette
= PALETTE_AllocPalette( PAL_INDEXED
,
464 (PULONG
)pLogPal
->palPalEntry
,
467 if (NewPalette
== NULL
)
472 PalGDI
= (PPALETTE
) PALETTE_LockPalette(NewPalette
);
475 PALETTE_ValidateFlags(PalGDI
->IndexedColors
, PalGDI
->NumColors
);
476 PALETTE_UnlockPalette(PalGDI
);
480 /* FIXME - Handle PalGDI == NULL!!!! */
481 DPRINT1("PalGDI is NULL\n");
486 HPALETTE APIENTRY
NtGdiCreateHalftonePalette(HDC hDC
)
491 WORD NumberOfEntries
;
492 PALETTEENTRY aEntries
[256];
495 Palette
.Version
= 0x300;
496 Palette
.NumberOfEntries
= 256;
497 if (IntGetSystemPaletteEntries(hDC
, 0, 256, Palette
.aEntries
) == 0)
499 /* from wine, more that 256 color math */
500 Palette
.NumberOfEntries
= 20;
501 for (i
= 0; i
< Palette
.NumberOfEntries
; i
++)
503 Palette
.aEntries
[i
].peRed
=0xff;
504 Palette
.aEntries
[i
].peGreen
=0xff;
505 Palette
.aEntries
[i
].peBlue
=0xff;
506 Palette
.aEntries
[i
].peFlags
=0x00;
509 Palette
.aEntries
[0].peRed
=0x00;
510 Palette
.aEntries
[0].peBlue
=0x00;
511 Palette
.aEntries
[0].peGreen
=0x00;
514 for (i
=1; i
<= 6; i
++)
516 Palette
.aEntries
[i
].peRed
=(i
%2)?0x80:0;
517 Palette
.aEntries
[i
].peGreen
=(i
==2)?0x80:(i
==3)?0x80:(i
==6)?0x80:0;
518 Palette
.aEntries
[i
].peBlue
=(i
>3)?0x80:0;
521 for (i
=7; i
<= 12; i
++)
526 Palette
.aEntries
[i
].peRed
=0xc0;
527 Palette
.aEntries
[i
].peBlue
=0xc0;
528 Palette
.aEntries
[i
].peGreen
=0xc0;
531 Palette
.aEntries
[i
].peRed
=0xc0;
532 Palette
.aEntries
[i
].peGreen
=0xdc;
533 Palette
.aEntries
[i
].peBlue
=0xc0;
536 Palette
.aEntries
[i
].peRed
=0xa6;
537 Palette
.aEntries
[i
].peGreen
=0xca;
538 Palette
.aEntries
[i
].peBlue
=0xf0;
541 Palette
.aEntries
[i
].peRed
=0xff;
542 Palette
.aEntries
[i
].peGreen
=0xfb;
543 Palette
.aEntries
[i
].peBlue
=0xf0;
546 Palette
.aEntries
[i
].peRed
=0xa0;
547 Palette
.aEntries
[i
].peGreen
=0xa0;
548 Palette
.aEntries
[i
].peBlue
=0xa4;
551 Palette
.aEntries
[i
].peRed
=0x80;
552 Palette
.aEntries
[i
].peGreen
=0x80;
553 Palette
.aEntries
[i
].peBlue
=0x80;
557 for (i
=13; i
<= 18; i
++)
559 Palette
.aEntries
[i
].peRed
=(i
%2)?0xff:0;
560 Palette
.aEntries
[i
].peGreen
=(i
==14)?0xff:(i
==15)?0xff:(i
==18)?0xff:0;
561 Palette
.aEntries
[i
].peBlue
=(i
>15)?0xff:0x00;
566 /* 256 color table */
567 for (r
= 0; r
< 6; r
++)
568 for (g
= 0; g
< 6; g
++)
569 for (b
= 0; b
< 6; b
++)
571 i
= r
+ g
*6 + b
*36 + 10;
572 Palette
.aEntries
[i
].peRed
= r
* 51;
573 Palette
.aEntries
[i
].peGreen
= g
* 51;
574 Palette
.aEntries
[i
].peBlue
= b
* 51;
577 for (i
= 216; i
< 246; i
++)
579 int v
= (i
- 216) << 3;
580 Palette
.aEntries
[i
].peRed
= v
;
581 Palette
.aEntries
[i
].peGreen
= v
;
582 Palette
.aEntries
[i
].peBlue
= v
;
586 return NtGdiCreatePaletteInternal((LOGPALETTE
*)&Palette
, Palette
.NumberOfEntries
);
595 /* PALOBJ *palPtr = (PALOBJ*)AccessUserObject(hPal);
596 UINT cPrevEnt, prevVer;
597 INT prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
598 XLATEOBJ *XlateObj = NULL;
600 if(!palPtr) return FALSE;
601 cPrevEnt = palPtr->logpalette->palNumEntries;
602 prevVer = palPtr->logpalette->palVersion;
603 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) + sizeof(int*) + sizeof(GDIOBJHDR);
604 size += sizeof(int*) + sizeof(GDIOBJHDR);
605 XlateObj = palPtr->logicalToSystem;
607 if (!(palPtr = GDI_ReallocObject(size, hPal, palPtr))) return FALSE;
611 XLATEOBJ *NewXlateObj = (int*) HeapReAlloc(GetProcessHeap(), 0, XlateObj, cEntries * sizeof(int));
612 if(NewXlateObj == NULL)
614 ERR("Can not resize logicalToSystem -- out of memory!");
615 GDI_ReleaseObj( hPal );
618 palPtr->logicalToSystem = NewXlateObj;
621 if(cEntries > cPrevEnt)
623 if(XlateObj) memset(palPtr->logicalToSystem + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
624 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
625 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize), cEntries - cPrevEnt );
627 palPtr->logpalette->palNumEntries = cEntries;
628 palPtr->logpalette->palVersion = prevVer;
629 // GDI_ReleaseObj( hPal );
638 NtGdiGetColorAdjustment(
640 LPCOLORADJUSTMENT pca
)
648 NtGdiSetColorAdjustment(
650 LPCOLORADJUSTMENT pca
)
656 COLORREF APIENTRY
NtGdiGetNearestColor(HDC hDC
, COLORREF Color
)
658 COLORREF nearest
= CLR_INVALID
;
661 LONG RBits
, GBits
, BBits
;
666 HPALETTE hpal
= dc
->dclevel
.hpal
;
667 palGDI
= (PPALETTE
) PALETTE_LockPalette(hpal
);
674 if (palGDI
->flFlags
& PAL_INDEXED
)
677 index
= PALETTE_ulGetNearestPaletteIndex(palGDI
, Color
);
678 nearest
= PALETTE_ulGetRGBColorFromIndex(palGDI
, index
);
680 else if (palGDI
->flFlags
& PAL_RGB
|| palGDI
->flFlags
& PAL_BGR
)
684 else if (palGDI
->flFlags
& PAL_BITFIELDS
)
686 RBits
= 8 - GetNumberOfBits(palGDI
->RedMask
);
687 GBits
= 8 - GetNumberOfBits(palGDI
->GreenMask
);
688 BBits
= 8 - GetNumberOfBits(palGDI
->BlueMask
);
690 (GetRValue(Color
) >> RBits
) << RBits
,
691 (GetGValue(Color
) >> GBits
) << GBits
,
692 (GetBValue(Color
) >> BBits
) << BBits
);
694 PALETTE_UnlockPalette(palGDI
);
703 NtGdiGetNearestPaletteIndex(
707 PPALETTE ppal
= (PPALETTE
) PALETTE_LockPalette(hpal
);
712 if (ppal
->flFlags
& PAL_INDEXED
)
714 /* Return closest match for the given RGB color */
715 index
= PALETTE_ulGetNearestPaletteIndex(ppal
, crColor
);
717 // else SetLastError ?
718 PALETTE_UnlockPalette(ppal
);
726 IntGdiRealizePalette(PDC pdc
)
729 PALETTE
*ppalSurf
, *ppalDC
;
731 ppalSurf
= pdc
->dclevel
.pSurface
->ppal
;
732 ppalDC
= pdc
->dclevel
.ppal
;
734 if(!(ppalSurf
->flFlags
& PAL_INDEXED
))
736 // FIXME : set error?
740 ASSERT(ppalDC
->flFlags
& PAL_INDEXED
);
742 // FIXME : should we resize ppalSurf if it's too small?
743 realize
= (ppalDC
->NumColors
< ppalSurf
->NumColors
) ? ppalDC
->NumColors
: ppalSurf
->NumColors
;
745 for(i
=0; i
<realize
; i
++)
747 InterlockedExchange((LONG
*)&ppalSurf
->IndexedColors
[i
], *(LONG
*)&ppalDC
->IndexedColors
[i
]);
754 IntAnimatePalette(HPALETTE hPal
,
757 CONST PPALETTEENTRY PaletteColors
)
761 if( hPal
!= NtGdiGetStockObject(DEFAULT_PALETTE
) )
768 const PALETTEENTRY
*pptr
= PaletteColors
;
770 palPtr
= (PPALETTE
)PALETTE_LockPalette(hPal
);
771 if (!palPtr
) return FALSE
;
773 pal_entries
= palPtr
->NumColors
;
774 if (StartIndex
>= pal_entries
)
776 PALETTE_UnlockPalette(palPtr
);
779 if (StartIndex
+NumEntries
> pal_entries
) NumEntries
= pal_entries
- StartIndex
;
781 for (NumEntries
+= StartIndex
; StartIndex
< NumEntries
; StartIndex
++, pptr
++)
783 /* According to MSDN, only animate PC_RESERVED colours */
784 if (palPtr
->IndexedColors
[StartIndex
].peFlags
& PC_RESERVED
)
786 memcpy( &palPtr
->IndexedColors
[StartIndex
], pptr
,
787 sizeof(PALETTEENTRY
) );
789 PALETTE_ValidateFlags(&palPtr
->IndexedColors
[StartIndex
], 1);
793 PALETTE_UnlockPalette(palPtr
);
795 /* Immediately apply the new palette if current window uses it */
796 Wnd
= UserGetDesktopWindow();
797 hDC
= UserGetWindowDC(Wnd
);
801 if (dc
->dclevel
.hpal
== hPal
)
803 IntGdiRealizePalette(dc
);
807 UserReleaseDC(Wnd
,hDC
, FALSE
);
813 IntGetPaletteEntries(
822 palGDI
= (PPALETTE
) PALETTE_LockPalette(hpal
);
828 numEntries
= palGDI
->NumColors
;
831 if (numEntries
< StartIndex
+ Entries
)
833 Entries
= numEntries
- StartIndex
;
835 if (numEntries
<= StartIndex
)
837 PALETTE_UnlockPalette(palGDI
);
840 memcpy(pe
, palGDI
->IndexedColors
+ StartIndex
, Entries
* sizeof(PALETTEENTRY
));
844 Entries
= numEntries
;
847 PALETTE_UnlockPalette(palGDI
);
852 IntGetSystemPaletteEntries(HDC hDC
,
857 PPALETTE palGDI
= NULL
;
859 UINT EntriesSize
= 0;
864 EngSetLastError(ERROR_INVALID_PARAMETER
);
870 EntriesSize
= Entries
* sizeof(pe
[0]);
871 if (Entries
!= EntriesSize
/ sizeof(pe
[0]))
873 /* Integer overflow! */
874 EngSetLastError(ERROR_INVALID_PARAMETER
);
879 if (!(dc
= DC_LockDc(hDC
)))
881 EngSetLastError(ERROR_INVALID_HANDLE
);
885 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
890 if (StartIndex
>= palGDI
->NumColors
)
892 else if (Entries
> palGDI
->NumColors
- StartIndex
)
893 Entries
= palGDI
->NumColors
- StartIndex
;
896 palGDI
->IndexedColors
+ StartIndex
,
897 Entries
* sizeof(pe
[0]));
903 Ret
= dc
->ppdev
->gdiinfo
.ulNumPalReg
;
908 PALETTE_UnlockPalette(palGDI
);
918 IntSetPaletteEntries(
922 CONST LPPALETTEENTRY pe
)
927 if ((UINT
)hpal
& GDI_HANDLE_STOCK_MASK
)
932 palGDI
= PALETTE_LockPalette(hpal
);
933 if (!palGDI
) return 0;
935 numEntries
= palGDI
->NumColors
;
936 if (Start
>= numEntries
)
938 PALETTE_UnlockPalette(palGDI
);
941 if (numEntries
< Start
+ Entries
)
943 Entries
= numEntries
- Start
;
945 memcpy(palGDI
->IndexedColors
+ Start
, pe
, Entries
* sizeof(PALETTEENTRY
));
946 PALETTE_UnlockPalette(palGDI
);
958 IN LPVOID pUnsafeEntries
,
963 LPVOID pEntries
= NULL
;
965 /* FIXME: Handle bInbound correctly */
968 (pUnsafeEntries
== NULL
|| cEntries
== 0))
975 pEntries
= ExAllocatePoolWithTag(PagedPool
, cEntries
* sizeof(PALETTEENTRY
), TAG_PALETTE
);
982 ProbeForRead(pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
), 1);
983 memcpy(pEntries
, pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
));
985 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
987 ExFreePoolWithTag(pEntries
, TAG_PALETTE
);
988 _SEH2_YIELD(return 0);
999 ret
= IntAnimatePalette((HPALETTE
)hObj
, iStart
, cEntries
, (CONST PPALETTEENTRY
)pEntries
);
1002 case GdiPalSetEntries
:
1004 ret
= IntSetPaletteEntries((HPALETTE
)hObj
, iStart
, cEntries
, (CONST LPPALETTEENTRY
)pEntries
);
1007 case GdiPalGetEntries
:
1008 ret
= IntGetPaletteEntries((HPALETTE
)hObj
, iStart
, cEntries
, (LPPALETTEENTRY
)pEntries
);
1011 case GdiPalGetSystemEntries
:
1012 ret
= IntGetSystemPaletteEntries((HDC
)hObj
, iStart
, cEntries
, (LPPALETTEENTRY
)pEntries
);
1015 case GdiPalSetColorTable
:
1017 ret
= IntSetDIBColorTable((HDC
)hObj
, iStart
, cEntries
, (RGBQUAD
*)pEntries
);
1020 case GdiPalGetColorTable
:
1022 ret
= IntGetDIBColorTable((HDC
)hObj
, iStart
, cEntries
, (RGBQUAD
*)pEntries
);
1032 ProbeForWrite(pUnsafeEntries
, cEntries
* sizeof(PALETTEENTRY
), 1);
1033 memcpy(pUnsafeEntries
, pEntries
, cEntries
* sizeof(PALETTEENTRY
));
1035 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1041 ExFreePoolWithTag(pEntries
, TAG_PALETTE
);
1048 NtGdiSetSystemPaletteUse(HDC hDC
, UINT Usage
)
1050 UINT old
= SystemPaletteUse
;
1052 /* Device doesn't support colour palettes */
1053 if (!(NtGdiGetDeviceCaps(hDC
, RASTERCAPS
) & RC_PALETTE
)) {
1054 return SYSPAL_ERROR
;
1059 case SYSPAL_NOSTATIC
:
1060 case SYSPAL_NOSTATIC256
:
1062 SystemPaletteUse
= Usage
;
1075 NtGdiGetSystemPaletteUse(HDC hDC
)
1077 return SystemPaletteUse
;
1082 NtGdiUpdateColors(HDC hDC
)
1085 BOOL calledFromUser
, ret
;
1086 USER_REFERENCE_ENTRY Ref
;
1088 calledFromUser
= UserIsEntered();
1090 if (!calledFromUser
){
1091 UserEnterExclusive();
1094 Wnd
= UserGetWindowObject(IntWindowFromDC(hDC
));
1097 EngSetLastError(ERROR_INVALID_WINDOW_HANDLE
);
1099 if (!calledFromUser
){
1106 UserRefObjectCo(Wnd
, &Ref
);
1107 ret
= co_UserRedrawWindow(Wnd
, NULL
, 0, RDW_INVALIDATE
);
1108 UserDerefObjectCo(Wnd
);
1110 if (!calledFromUser
){
1119 NtGdiUnrealizeObject(HGDIOBJ hgdiobj
)
1125 ((UINT
)hgdiobj
& GDI_HANDLE_STOCK_MASK
) ||
1126 !GDI_HANDLE_IS_TYPE(hgdiobj
, GDI_OBJECT_TYPE_PALETTE
) )
1129 palGDI
= PALETTE_LockPalette(hgdiobj
);
1130 if (!palGDI
) return FALSE
;
1133 // Need to do something!!!
1134 // Zero out Current and Old Translated pointers?
1137 PALETTE_UnlockPalette(palGDI
);