4 * Copyright 1993,1994 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
23 * Information in the "Undocumented Windows" is incorrect.
36 #include "gdi_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(palette);
41 typedef BOOL (CDECL *unrealize_function)(HPALETTE);
43 typedef struct tagPALETTEOBJ
46 unrealize_function unrealize;
47 WORD version; /* palette version */
48 WORD count; /* count of palette entries */
49 PALETTEENTRY *entries;
52 static INT PALETTE_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
53 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle );
54 static BOOL PALETTE_DeleteObject( HGDIOBJ handle );
56 static const struct gdi_obj_funcs palette_funcs =
58 NULL, /* pSelectObject */
59 PALETTE_GetObject, /* pGetObjectA */
60 PALETTE_GetObject, /* pGetObjectW */
61 PALETTE_UnrealizeObject, /* pUnrealizeObject */
62 PALETTE_DeleteObject /* pDeleteObject */
65 /* Pointers to USER implementation of SelectPalette/RealizePalette */
66 /* they will be patched by USER on startup */
67 HPALETTE (WINAPI *pfnSelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = GDISelectPalette;
68 UINT (WINAPI *pfnRealizePalette)(HDC hdc) = GDIRealizePalette;
70 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
72 static HPALETTE hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
73 static HPALETTE hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
75 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
77 static const PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS] =
79 /* first 10 entries in the system palette */
80 /* red green blue flags */
81 { 0x00, 0x00, 0x00, 0 },
82 { 0x80, 0x00, 0x00, 0 },
83 { 0x00, 0x80, 0x00, 0 },
84 { 0x80, 0x80, 0x00, 0 },
85 { 0x00, 0x00, 0x80, 0 },
86 { 0x80, 0x00, 0x80, 0 },
87 { 0x00, 0x80, 0x80, 0 },
88 { 0xc0, 0xc0, 0xc0, 0 },
89 { 0xc0, 0xdc, 0xc0, 0 },
90 { 0xa6, 0xca, 0xf0, 0 },
92 /* ... c_min/2 dynamic colorcells */
94 /* ... gap (for sparse palettes) */
96 /* ... c_min/2 dynamic colorcells */
98 { 0xff, 0xfb, 0xf0, 0 },
99 { 0xa0, 0xa0, 0xa4, 0 },
100 { 0x80, 0x80, 0x80, 0 },
101 { 0xff, 0x00, 0x00, 0 },
102 { 0x00, 0xff, 0x00, 0 },
103 { 0xff, 0xff, 0x00, 0 },
104 { 0x00, 0x00, 0xff, 0 },
105 { 0xff, 0x00, 0xff, 0 },
106 { 0x00, 0xff, 0xff, 0 },
107 { 0xff, 0xff, 0xff, 0 } /* last 10 */
110 /***********************************************************************
113 * Create the system palette.
115 HPALETTE PALETTE_Init(void)
120 /* create default palette (20 system colors) */
122 palPtr = HeapAlloc( GetProcessHeap(), 0,
123 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
124 if (!palPtr) return FALSE;
126 palPtr->palVersion = 0x300;
127 palPtr->palNumEntries = NB_RESERVED_COLORS;
128 memcpy( palPtr->palPalEntry, sys_pal_template, sizeof(sys_pal_template) );
129 hpalette = CreatePalette( palPtr );
130 HeapFree( GetProcessHeap(), 0, palPtr );
135 /***********************************************************************
136 * CreatePalette [GDI32.@]
138 * Creates a logical color palette.
141 * Success: Handle to logical palette
144 HPALETTE WINAPI CreatePalette(
145 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
147 PALETTEOBJ * palettePtr;
151 if (!palette) return 0;
152 TRACE("entries=%i\n", palette->palNumEntries);
154 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
156 if (!(palettePtr = HeapAlloc( GetProcessHeap(), 0, sizeof(*palettePtr) ))) return 0;
157 palettePtr->unrealize = NULL;
158 palettePtr->version = palette->palVersion;
159 palettePtr->count = palette->palNumEntries;
160 size = palettePtr->count * sizeof(*palettePtr->entries);
161 if (!(palettePtr->entries = HeapAlloc( GetProcessHeap(), 0, size )))
163 HeapFree( GetProcessHeap(), 0, palettePtr );
166 memcpy( palettePtr->entries, palette->palPalEntry, size );
167 if (!(hpalette = alloc_gdi_handle( &palettePtr->header, OBJ_PAL, &palette_funcs )))
169 HeapFree( GetProcessHeap(), 0, palettePtr->entries );
170 HeapFree( GetProcessHeap(), 0, palettePtr );
172 TRACE(" returning %p\n", hpalette);
177 /***********************************************************************
178 * CreateHalftonePalette [GDI32.@]
180 * Creates a halftone palette.
183 * Success: Handle to logical halftone palette
186 * FIXME: This simply creates the halftone palette derived from running
187 * tests on a windows NT machine. This is assuming a color depth
188 * of greater that 256 color. On a 256 color device the halftone
189 * palette will be different and this function will be incorrect
191 HPALETTE WINAPI CreateHalftonePalette(
192 HDC hdc) /* [in] Handle to device context */
197 WORD NumberOfEntries;
198 PALETTEENTRY aEntries[256];
201 Palette.Version = 0x300;
202 Palette.NumberOfEntries = 256;
203 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
205 Palette.NumberOfEntries = 20;
207 for (i = 0; i < Palette.NumberOfEntries; i++)
209 Palette.aEntries[i].peRed=0xff;
210 Palette.aEntries[i].peGreen=0xff;
211 Palette.aEntries[i].peBlue=0xff;
212 Palette.aEntries[i].peFlags=0x00;
215 Palette.aEntries[0].peRed=0x00;
216 Palette.aEntries[0].peBlue=0x00;
217 Palette.aEntries[0].peGreen=0x00;
220 for (i=1; i <= 6; i++)
222 Palette.aEntries[i].peRed=(i%2)?0x80:0;
223 Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
224 Palette.aEntries[i].peBlue=(i>3)?0x80:0;
227 for (i=7; i <= 12; i++)
232 Palette.aEntries[i].peRed=0xc0;
233 Palette.aEntries[i].peBlue=0xc0;
234 Palette.aEntries[i].peGreen=0xc0;
237 Palette.aEntries[i].peRed=0xc0;
238 Palette.aEntries[i].peGreen=0xdc;
239 Palette.aEntries[i].peBlue=0xc0;
242 Palette.aEntries[i].peRed=0xa6;
243 Palette.aEntries[i].peGreen=0xca;
244 Palette.aEntries[i].peBlue=0xf0;
247 Palette.aEntries[i].peRed=0xff;
248 Palette.aEntries[i].peGreen=0xfb;
249 Palette.aEntries[i].peBlue=0xf0;
252 Palette.aEntries[i].peRed=0xa0;
253 Palette.aEntries[i].peGreen=0xa0;
254 Palette.aEntries[i].peBlue=0xa4;
257 Palette.aEntries[i].peRed=0x80;
258 Palette.aEntries[i].peGreen=0x80;
259 Palette.aEntries[i].peBlue=0x80;
263 for (i=13; i <= 18; i++)
265 Palette.aEntries[i].peRed=(i%2)?0xff:0;
266 Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
267 Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
270 return CreatePalette((LOGPALETTE *)&Palette);
274 /***********************************************************************
275 * GetPaletteEntries [GDI32.@]
277 * Retrieves palette entries.
280 * Success: Number of entries from logical palette
283 UINT WINAPI GetPaletteEntries(
284 HPALETTE hpalette, /* [in] Handle of logical palette */
285 UINT start, /* [in] First entry to receive */
286 UINT count, /* [in] Number of entries to receive */
287 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
292 TRACE("hpal = %p, count=%i\n", hpalette, count );
294 palPtr = GDI_GetObjPtr( hpalette, OBJ_PAL );
295 if (!palPtr) return 0;
297 /* NOTE: not documented but test show this to be the case */
300 count = palPtr->count;
304 numEntries = palPtr->count;
305 if (start+count > numEntries) count = numEntries - start;
308 if (start >= numEntries) count = 0;
309 else memcpy( entries, &palPtr->entries[start], count * sizeof(PALETTEENTRY) );
313 GDI_ReleaseObj( hpalette );
318 /***********************************************************************
319 * SetPaletteEntries [GDI32.@]
321 * Sets color values for range in palette.
324 * Success: Number of entries that were set
327 UINT WINAPI SetPaletteEntries(
328 HPALETTE hpalette, /* [in] Handle of logical palette */
329 UINT start, /* [in] Index of first entry to set */
330 UINT count, /* [in] Number of entries to set */
331 const PALETTEENTRY *entries) /* [in] Address of array of structures */
336 TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
338 if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
339 palPtr = GDI_GetObjPtr( hpalette, OBJ_PAL );
340 if (!palPtr) return 0;
342 numEntries = palPtr->count;
343 if (start >= numEntries)
345 GDI_ReleaseObj( hpalette );
348 if (start+count > numEntries) count = numEntries - start;
349 memcpy( &palPtr->entries[start], entries, count * sizeof(PALETTEENTRY) );
350 GDI_ReleaseObj( hpalette );
351 UnrealizeObject( hpalette );
356 /***********************************************************************
357 * ResizePalette [GDI32.@]
359 * Resizes logical palette.
365 BOOL WINAPI ResizePalette(
366 HPALETTE hPal, /* [in] Handle of logical palette */
367 UINT cEntries) /* [in] Number of entries in logical palette */
369 PALETTEOBJ * palPtr = GDI_GetObjPtr( hPal, OBJ_PAL );
370 PALETTEENTRY *entries;
372 if( !palPtr ) return FALSE;
373 TRACE("hpal = %p, prev = %i, new = %i\n", hPal, palPtr->count, cEntries );
375 if (!(entries = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
376 palPtr->entries, cEntries * sizeof(*palPtr->entries) )))
378 GDI_ReleaseObj( hPal );
381 palPtr->entries = entries;
382 palPtr->count = cEntries;
384 GDI_ReleaseObj( hPal );
385 PALETTE_UnrealizeObject( hPal );
390 /***********************************************************************
391 * AnimatePalette [GDI32.@]
393 * Replaces entries in logical palette.
400 * Should use existing mapping when animating a primary palette
402 BOOL WINAPI AnimatePalette(
403 HPALETTE hPal, /* [in] Handle to logical palette */
404 UINT StartIndex, /* [in] First entry in palette */
405 UINT NumEntries, /* [in] Count of entries in palette */
406 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
408 TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
410 if( hPal != GetStockObject(DEFAULT_PALETTE) )
414 const PALETTEENTRY *pptr = PaletteColors;
416 palPtr = GDI_GetObjPtr( hPal, OBJ_PAL );
417 if (!palPtr) return 0;
419 pal_entries = palPtr->count;
420 if (StartIndex >= pal_entries)
422 GDI_ReleaseObj( hPal );
425 if (StartIndex+NumEntries > pal_entries) NumEntries = pal_entries - StartIndex;
427 for (NumEntries += StartIndex; StartIndex < NumEntries; StartIndex++, pptr++) {
428 /* According to MSDN, only animate PC_RESERVED colours */
429 if (palPtr->entries[StartIndex].peFlags & PC_RESERVED) {
430 TRACE("Animating colour (%d,%d,%d) to (%d,%d,%d)\n",
431 palPtr->entries[StartIndex].peRed,
432 palPtr->entries[StartIndex].peGreen,
433 palPtr->entries[StartIndex].peBlue,
434 pptr->peRed, pptr->peGreen, pptr->peBlue);
435 palPtr->entries[StartIndex] = *pptr;
437 TRACE("Not animating entry %d -- not PC_RESERVED\n", StartIndex);
440 GDI_ReleaseObj( hPal );
441 /* FIXME: check for palette selected in active window */
447 /***********************************************************************
448 * SetSystemPaletteUse [GDI32.@]
450 * Specify whether the system palette contains 2 or 20 static colors.
453 * Success: Previous system palette
454 * Failure: SYSPAL_ERROR
456 UINT WINAPI SetSystemPaletteUse(
457 HDC hdc, /* [in] Handle of device context */
458 UINT use) /* [in] Palette-usage flag */
460 UINT old = SystemPaletteUse;
462 /* Device doesn't support colour palettes */
463 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
468 case SYSPAL_NOSTATIC:
469 case SYSPAL_NOSTATIC256: /* WINVER >= 0x0500 */
471 SystemPaletteUse = use;
479 /***********************************************************************
480 * GetSystemPaletteUse [GDI32.@]
482 * Gets state of system palette.
485 * Current state of system palette
487 UINT WINAPI GetSystemPaletteUse(
488 HDC hdc) /* [in] Handle of device context */
490 return SystemPaletteUse;
494 /***********************************************************************
495 * GetSystemPaletteEntries [GDI32.@]
497 * Gets range of palette entries.
500 * Success: Number of entries retrieved from palette
503 UINT WINAPI GetSystemPaletteEntries(
504 HDC hdc, /* [in] Handle of device context */
505 UINT start, /* [in] Index of first entry to be retrieved */
506 UINT count, /* [in] Number of entries to be retrieved */
507 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
512 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
514 if ((dc = get_dc_ptr( hdc )))
516 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetSystemPaletteEntries );
517 ret = physdev->funcs->pGetSystemPaletteEntries( physdev, start, count, entries );
518 release_dc_ptr( dc );
524 /***********************************************************************
525 * GetNearestPaletteIndex [GDI32.@]
527 * Gets palette index for color.
530 * Should index be initialized to CLR_INVALID instead of 0?
533 * Success: Index of entry in logical palette
534 * Failure: CLR_INVALID
536 UINT WINAPI GetNearestPaletteIndex(
537 HPALETTE hpalette, /* [in] Handle of logical color palette */
538 COLORREF color) /* [in] Color to be matched */
540 PALETTEOBJ* palObj = GDI_GetObjPtr( hpalette, OBJ_PAL );
545 int i, diff = 0x7fffffff;
547 PALETTEENTRY* entry = palObj->entries;
549 for( i = 0; i < palObj->count && diff ; i++, entry++)
551 r = entry->peRed - GetRValue(color);
552 g = entry->peGreen - GetGValue(color);
553 b = entry->peBlue - GetBValue(color);
557 if( r < diff ) { index = i; diff = r; }
559 GDI_ReleaseObj( hpalette );
561 TRACE("(%p,%06x): returning %d\n", hpalette, color, index );
566 /* null driver fallback implementation for GetNearestColor */
567 COLORREF CDECL nulldrv_GetNearestColor( PHYSDEV dev, COLORREF color )
569 unsigned char spec_type;
571 if (!(GetDeviceCaps( dev->hdc, RASTERCAPS ) & RC_PALETTE)) return color;
573 spec_type = color >> 24;
574 if (spec_type == 1 || spec_type == 2)
576 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
579 HPALETTE hpal = GetCurrentObject( dev->hdc, OBJ_PAL );
581 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
582 if (spec_type == 2) /* PALETTERGB */
583 index = GetNearestPaletteIndex( hpal, color );
584 else /* PALETTEINDEX */
585 index = LOWORD(color);
587 if (!GetPaletteEntries( hpal, index, 1, &entry ))
589 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
590 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
592 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
594 return color & 0x00ffffff;
598 /***********************************************************************
599 * GetNearestColor [GDI32.@]
601 * Gets a system color to match.
604 * Success: Color from system palette that corresponds to given color
605 * Failure: CLR_INVALID
607 COLORREF WINAPI GetNearestColor(
608 HDC hdc, /* [in] Handle of device context */
609 COLORREF color) /* [in] Color to be matched */
611 COLORREF nearest = CLR_INVALID;
614 if ((dc = get_dc_ptr( hdc )))
616 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetNearestColor );
617 nearest = physdev->funcs->pGetNearestColor( physdev, color );
618 release_dc_ptr( dc );
624 /***********************************************************************
627 static INT PALETTE_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
629 PALETTEOBJ *palette = GDI_GetObjPtr( handle, OBJ_PAL );
631 if (!palette) return 0;
635 if (count > sizeof(WORD)) count = sizeof(WORD);
636 memcpy( buffer, &palette->count, count );
638 else count = sizeof(WORD);
639 GDI_ReleaseObj( handle );
644 /***********************************************************************
645 * PALETTE_UnrealizeObject
647 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle )
649 PALETTEOBJ *palette = GDI_GetObjPtr( handle, OBJ_PAL );
653 unrealize_function unrealize = palette->unrealize;
654 palette->unrealize = NULL;
655 GDI_ReleaseObj( handle );
656 if (unrealize) unrealize( handle );
659 if (InterlockedCompareExchangePointer( (void **)&hLastRealizedPalette, 0, handle ) == handle)
660 TRACE("unrealizing palette %p\n", handle);
666 /***********************************************************************
667 * PALETTE_DeleteObject
669 static BOOL PALETTE_DeleteObject( HGDIOBJ handle )
673 PALETTE_UnrealizeObject( handle );
674 if (!(obj = free_gdi_handle( handle ))) return FALSE;
675 HeapFree( GetProcessHeap(), 0, obj->entries );
676 return HeapFree( GetProcessHeap(), 0, obj );
680 /***********************************************************************
681 * GDISelectPalette (Not a Windows API)
683 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
688 TRACE("%p %p\n", hdc, hpal );
690 if (GetObjectType(hpal) != OBJ_PAL)
692 WARN("invalid selected palette %p\n",hpal);
695 if ((dc = get_dc_ptr( hdc )))
697 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPalette );
699 if (physdev->funcs->pSelectPalette( physdev, hpal, FALSE ))
702 if (!wBkg) hPrimaryPalette = hpal;
705 release_dc_ptr( dc );
711 /***********************************************************************
712 * GDIRealizePalette (Not a Windows API)
714 UINT WINAPI GDIRealizePalette( HDC hdc )
717 DC* dc = get_dc_ptr( hdc );
721 TRACE("%p...\n", hdc );
723 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
725 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizeDefaultPalette );
726 realized = physdev->funcs->pRealizeDefaultPalette( physdev );
728 else if (InterlockedExchangePointer( (void **)&hLastRealizedPalette, dc->hPalette ) != dc->hPalette)
730 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizePalette );
731 PALETTEOBJ *palPtr = GDI_GetObjPtr( dc->hPalette, OBJ_PAL );
734 realized = physdev->funcs->pRealizePalette( physdev, dc->hPalette,
735 (dc->hPalette == hPrimaryPalette) );
736 palPtr->unrealize = physdev->funcs->pUnrealizePalette;
737 GDI_ReleaseObj( dc->hPalette );
740 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
742 release_dc_ptr( dc );
743 TRACE(" realized %i colors.\n", realized );
748 /***********************************************************************
749 * SelectPalette [GDI32.@]
751 * Selects logical palette into DC.
754 * Success: Previous logical palette
757 HPALETTE WINAPI SelectPalette(
758 HDC hDC, /* [in] Handle of device context */
759 HPALETTE hPal, /* [in] Handle of logical color palette */
760 BOOL bForceBackground) /* [in] Foreground/background mode */
762 return pfnSelectPalette( hDC, hPal, bForceBackground );
766 /***********************************************************************
767 * RealizePalette [GDI32.@]
769 * Maps palette entries to system palette.
772 * Success: Number of entries in logical palette
775 UINT WINAPI RealizePalette(
776 HDC hDC) /* [in] Handle of device context */
778 return pfnRealizePalette( hDC );
782 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
783 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
785 /**********************************************************************
786 * UpdateColors [GDI32.@]
788 * Remaps current colors to logical palette.
794 BOOL WINAPI UpdateColors(
795 HDC hDC) /* [in] Handle of device context */
798 int size = GetDeviceCaps( hDC, SIZEPALETTE );
802 mod = GetModuleHandleA("user32.dll");
805 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
808 HWND hWnd = pWindowFromDC( hDC );
810 /* Docs say that we have to remap current drawable pixel by pixel
811 * but it would take forever given the speed of XGet/PutPixel.
815 RedrawWindow_funcptr pRedrawWindow = (void *)GetProcAddress( mod, "RedrawWindow" );
816 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
823 /*********************************************************************
824 * SetMagicColors (GDI32.@)
826 BOOL WINAPI SetMagicColors(HDC hdc, ULONG u1, ULONG u2)
828 FIXME("(%p 0x%08x 0x%08x): stub\n", hdc, u1, u2);