msi: Stub Migrate10CachedPackagesW.
[wine/multimedia.git] / dlls / gdi32 / palette.c
blobafb723bb2c0c387630088863c754dca440fd268f
1 /*
2 * GDI palette objects
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
21 * NOTES:
22 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
23 * Information in the "Undocumented Windows" is incorrect.
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "wingdi.h"
34 #include "winuser.h"
36 #include "gdi_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(palette);
41 typedef BOOL (*unrealize_function)(HPALETTE);
43 typedef struct tagPALETTEOBJ
45 GDIOBJHDR header;
46 unrealize_function unrealize;
47 WORD version; /* palette version */
48 WORD count; /* count of palette entries */
49 PALETTEENTRY *entries;
50 } PALETTEOBJ;
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 */
76 /***********************************************************************
77 * PALETTE_Init
79 * Create the system palette.
81 HPALETTE PALETTE_Init(void)
83 const RGBQUAD *entries = get_default_color_table( 8 );
84 char buffer[FIELD_OFFSET( LOGPALETTE, palPalEntry[20] )];
85 LOGPALETTE *palPtr = (LOGPALETTE *)buffer;
86 int i;
88 /* create default palette (20 system colors) */
90 palPtr->palVersion = 0x300;
91 palPtr->palNumEntries = 20;
92 for (i = 0; i < 20; i++)
94 palPtr->palPalEntry[i].peRed = entries[i < 10 ? i : 236 + i].rgbRed;
95 palPtr->palPalEntry[i].peGreen = entries[i < 10 ? i : 236 + i].rgbGreen;
96 palPtr->palPalEntry[i].peBlue = entries[i < 10 ? i : 236 + i].rgbBlue;
97 palPtr->palPalEntry[i].peFlags = 0;
99 return CreatePalette( palPtr );
103 /***********************************************************************
104 * CreatePalette [GDI32.@]
106 * Creates a logical color palette.
108 * RETURNS
109 * Success: Handle to logical palette
110 * Failure: NULL
112 HPALETTE WINAPI CreatePalette(
113 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
115 PALETTEOBJ * palettePtr;
116 HPALETTE hpalette;
117 int size;
119 if (!palette) return 0;
120 TRACE("entries=%i\n", palette->palNumEntries);
122 if (!(palettePtr = HeapAlloc( GetProcessHeap(), 0, sizeof(*palettePtr) ))) return 0;
123 palettePtr->unrealize = NULL;
124 palettePtr->version = palette->palVersion;
125 palettePtr->count = palette->palNumEntries;
126 size = palettePtr->count * sizeof(*palettePtr->entries);
127 if (!(palettePtr->entries = HeapAlloc( GetProcessHeap(), 0, size )))
129 HeapFree( GetProcessHeap(), 0, palettePtr );
130 return 0;
132 memcpy( palettePtr->entries, palette->palPalEntry, size );
133 if (!(hpalette = alloc_gdi_handle( &palettePtr->header, OBJ_PAL, &palette_funcs )))
135 HeapFree( GetProcessHeap(), 0, palettePtr->entries );
136 HeapFree( GetProcessHeap(), 0, palettePtr );
138 TRACE(" returning %p\n", hpalette);
139 return hpalette;
143 /***********************************************************************
144 * CreateHalftonePalette [GDI32.@]
146 * Creates a halftone palette.
148 * RETURNS
149 * Success: Handle to logical halftone palette
150 * Failure: 0
152 * FIXME: This simply creates the halftone palette derived from running
153 * tests on a windows NT machine. This is assuming a color depth
154 * of greater that 256 color. On a 256 color device the halftone
155 * palette will be different and this function will be incorrect
157 HPALETTE WINAPI CreateHalftonePalette(
158 HDC hdc) /* [in] Handle to device context */
160 const RGBQUAD *entries = get_default_color_table( 8 );
161 char buffer[FIELD_OFFSET( LOGPALETTE, palPalEntry[256] )];
162 LOGPALETTE *pal = (LOGPALETTE *)buffer;
163 int i;
165 pal->palVersion = 0x300;
166 pal->palNumEntries = 256;
167 for (i = 0; i < 256; i++)
169 pal->palPalEntry[i].peRed = entries[i].rgbRed;
170 pal->palPalEntry[i].peGreen = entries[i].rgbGreen;
171 pal->palPalEntry[i].peBlue = entries[i].rgbBlue;
172 pal->palPalEntry[i].peFlags = 0;
174 return CreatePalette( pal );
178 /***********************************************************************
179 * GetPaletteEntries [GDI32.@]
181 * Retrieves palette entries.
183 * RETURNS
184 * Success: Number of entries from logical palette
185 * Failure: 0
187 UINT WINAPI GetPaletteEntries(
188 HPALETTE hpalette, /* [in] Handle of logical palette */
189 UINT start, /* [in] First entry to receive */
190 UINT count, /* [in] Number of entries to receive */
191 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
193 PALETTEOBJ * palPtr;
194 UINT numEntries;
196 TRACE("hpal = %p, count=%i\n", hpalette, count );
198 palPtr = GDI_GetObjPtr( hpalette, OBJ_PAL );
199 if (!palPtr) return 0;
201 /* NOTE: not documented but test show this to be the case */
202 if (count == 0)
204 count = palPtr->count;
206 else
208 numEntries = palPtr->count;
209 if (start+count > numEntries) count = numEntries - start;
210 if (entries)
212 if (start >= numEntries) count = 0;
213 else memcpy( entries, &palPtr->entries[start], count * sizeof(PALETTEENTRY) );
217 GDI_ReleaseObj( hpalette );
218 return count;
222 /***********************************************************************
223 * SetPaletteEntries [GDI32.@]
225 * Sets color values for range in palette.
227 * RETURNS
228 * Success: Number of entries that were set
229 * Failure: 0
231 UINT WINAPI SetPaletteEntries(
232 HPALETTE hpalette, /* [in] Handle of logical palette */
233 UINT start, /* [in] Index of first entry to set */
234 UINT count, /* [in] Number of entries to set */
235 const PALETTEENTRY *entries) /* [in] Address of array of structures */
237 PALETTEOBJ * palPtr;
238 UINT numEntries;
240 TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
242 if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
243 palPtr = GDI_GetObjPtr( hpalette, OBJ_PAL );
244 if (!palPtr) return 0;
246 numEntries = palPtr->count;
247 if (start >= numEntries)
249 GDI_ReleaseObj( hpalette );
250 return 0;
252 if (start+count > numEntries) count = numEntries - start;
253 memcpy( &palPtr->entries[start], entries, count * sizeof(PALETTEENTRY) );
254 GDI_ReleaseObj( hpalette );
255 UnrealizeObject( hpalette );
256 return count;
260 /***********************************************************************
261 * ResizePalette [GDI32.@]
263 * Resizes logical palette.
265 * RETURNS
266 * Success: TRUE
267 * Failure: FALSE
269 BOOL WINAPI ResizePalette(
270 HPALETTE hPal, /* [in] Handle of logical palette */
271 UINT cEntries) /* [in] Number of entries in logical palette */
273 PALETTEOBJ * palPtr = GDI_GetObjPtr( hPal, OBJ_PAL );
274 PALETTEENTRY *entries;
276 if( !palPtr ) return FALSE;
277 TRACE("hpal = %p, prev = %i, new = %i\n", hPal, palPtr->count, cEntries );
279 if (!(entries = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
280 palPtr->entries, cEntries * sizeof(*palPtr->entries) )))
282 GDI_ReleaseObj( hPal );
283 return FALSE;
285 palPtr->entries = entries;
286 palPtr->count = cEntries;
288 GDI_ReleaseObj( hPal );
289 PALETTE_UnrealizeObject( hPal );
290 return TRUE;
294 /***********************************************************************
295 * AnimatePalette [GDI32.@]
297 * Replaces entries in logical palette.
299 * RETURNS
300 * Success: TRUE
301 * Failure: FALSE
303 * FIXME
304 * Should use existing mapping when animating a primary palette
306 BOOL WINAPI AnimatePalette(
307 HPALETTE hPal, /* [in] Handle to logical palette */
308 UINT StartIndex, /* [in] First entry in palette */
309 UINT NumEntries, /* [in] Count of entries in palette */
310 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
312 TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
314 if( hPal != GetStockObject(DEFAULT_PALETTE) )
316 PALETTEOBJ * palPtr;
317 UINT pal_entries;
318 const PALETTEENTRY *pptr = PaletteColors;
320 palPtr = GDI_GetObjPtr( hPal, OBJ_PAL );
321 if (!palPtr) return 0;
323 pal_entries = palPtr->count;
324 if (StartIndex >= pal_entries)
326 GDI_ReleaseObj( hPal );
327 return 0;
329 if (StartIndex+NumEntries > pal_entries) NumEntries = pal_entries - StartIndex;
331 for (NumEntries += StartIndex; StartIndex < NumEntries; StartIndex++, pptr++) {
332 /* According to MSDN, only animate PC_RESERVED colours */
333 if (palPtr->entries[StartIndex].peFlags & PC_RESERVED) {
334 TRACE("Animating colour (%d,%d,%d) to (%d,%d,%d)\n",
335 palPtr->entries[StartIndex].peRed,
336 palPtr->entries[StartIndex].peGreen,
337 palPtr->entries[StartIndex].peBlue,
338 pptr->peRed, pptr->peGreen, pptr->peBlue);
339 palPtr->entries[StartIndex] = *pptr;
340 } else {
341 TRACE("Not animating entry %d -- not PC_RESERVED\n", StartIndex);
344 GDI_ReleaseObj( hPal );
345 /* FIXME: check for palette selected in active window */
347 return TRUE;
351 /***********************************************************************
352 * SetSystemPaletteUse [GDI32.@]
354 * Specify whether the system palette contains 2 or 20 static colors.
356 * RETURNS
357 * Success: Previous system palette
358 * Failure: SYSPAL_ERROR
360 UINT WINAPI SetSystemPaletteUse(
361 HDC hdc, /* [in] Handle of device context */
362 UINT use) /* [in] Palette-usage flag */
364 UINT old = SystemPaletteUse;
366 /* Device doesn't support colour palettes */
367 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
368 return SYSPAL_ERROR;
371 switch (use) {
372 case SYSPAL_NOSTATIC:
373 case SYSPAL_NOSTATIC256: /* WINVER >= 0x0500 */
374 case SYSPAL_STATIC:
375 SystemPaletteUse = use;
376 return old;
377 default:
378 return SYSPAL_ERROR;
383 /***********************************************************************
384 * GetSystemPaletteUse [GDI32.@]
386 * Gets state of system palette.
388 * RETURNS
389 * Current state of system palette
391 UINT WINAPI GetSystemPaletteUse(
392 HDC hdc) /* [in] Handle of device context */
394 return SystemPaletteUse;
398 /***********************************************************************
399 * GetSystemPaletteEntries [GDI32.@]
401 * Gets range of palette entries.
403 * RETURNS
404 * Success: Number of entries retrieved from palette
405 * Failure: 0
407 UINT WINAPI GetSystemPaletteEntries(
408 HDC hdc, /* [in] Handle of device context */
409 UINT start, /* [in] Index of first entry to be retrieved */
410 UINT count, /* [in] Number of entries to be retrieved */
411 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
413 UINT ret = 0;
414 DC *dc;
416 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
418 if ((dc = get_dc_ptr( hdc )))
420 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetSystemPaletteEntries );
421 ret = physdev->funcs->pGetSystemPaletteEntries( physdev, start, count, entries );
422 release_dc_ptr( dc );
424 return ret;
428 /***********************************************************************
429 * GetNearestPaletteIndex [GDI32.@]
431 * Gets palette index for color.
433 * NOTES
434 * Should index be initialized to CLR_INVALID instead of 0?
436 * RETURNS
437 * Success: Index of entry in logical palette
438 * Failure: CLR_INVALID
440 UINT WINAPI GetNearestPaletteIndex(
441 HPALETTE hpalette, /* [in] Handle of logical color palette */
442 COLORREF color) /* [in] Color to be matched */
444 PALETTEOBJ* palObj = GDI_GetObjPtr( hpalette, OBJ_PAL );
445 UINT index = 0;
447 if( palObj )
449 int i, diff = 0x7fffffff;
450 int r,g,b;
451 PALETTEENTRY* entry = palObj->entries;
453 for( i = 0; i < palObj->count && diff ; i++, entry++)
455 r = entry->peRed - GetRValue(color);
456 g = entry->peGreen - GetGValue(color);
457 b = entry->peBlue - GetBValue(color);
459 r = r*r + g*g + b*b;
461 if( r < diff ) { index = i; diff = r; }
463 GDI_ReleaseObj( hpalette );
465 TRACE("(%p,%06x): returning %d\n", hpalette, color, index );
466 return index;
470 /* null driver fallback implementation for GetNearestColor */
471 COLORREF nulldrv_GetNearestColor( PHYSDEV dev, COLORREF color )
473 unsigned char spec_type;
475 if (!(GetDeviceCaps( dev->hdc, RASTERCAPS ) & RC_PALETTE)) return color;
477 spec_type = color >> 24;
478 if (spec_type == 1 || spec_type == 2)
480 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
481 UINT index;
482 PALETTEENTRY entry;
483 HPALETTE hpal = GetCurrentObject( dev->hdc, OBJ_PAL );
485 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
486 if (spec_type == 2) /* PALETTERGB */
487 index = GetNearestPaletteIndex( hpal, color );
488 else /* PALETTEINDEX */
489 index = LOWORD(color);
491 if (!GetPaletteEntries( hpal, index, 1, &entry ))
493 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
494 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
496 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
498 return color & 0x00ffffff;
502 /***********************************************************************
503 * GetNearestColor [GDI32.@]
505 * Gets a system color to match.
507 * RETURNS
508 * Success: Color from system palette that corresponds to given color
509 * Failure: CLR_INVALID
511 COLORREF WINAPI GetNearestColor(
512 HDC hdc, /* [in] Handle of device context */
513 COLORREF color) /* [in] Color to be matched */
515 COLORREF nearest = CLR_INVALID;
516 DC *dc;
518 if ((dc = get_dc_ptr( hdc )))
520 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetNearestColor );
521 nearest = physdev->funcs->pGetNearestColor( physdev, color );
522 release_dc_ptr( dc );
524 return nearest;
528 /***********************************************************************
529 * PALETTE_GetObject
531 static INT PALETTE_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
533 PALETTEOBJ *palette = GDI_GetObjPtr( handle, OBJ_PAL );
535 if (!palette) return 0;
537 if (buffer)
539 if (count > sizeof(WORD)) count = sizeof(WORD);
540 memcpy( buffer, &palette->count, count );
542 else count = sizeof(WORD);
543 GDI_ReleaseObj( handle );
544 return count;
548 /***********************************************************************
549 * PALETTE_UnrealizeObject
551 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle )
553 PALETTEOBJ *palette = GDI_GetObjPtr( handle, OBJ_PAL );
555 if (palette)
557 unrealize_function unrealize = palette->unrealize;
558 palette->unrealize = NULL;
559 GDI_ReleaseObj( handle );
560 if (unrealize) unrealize( handle );
563 if (InterlockedCompareExchangePointer( (void **)&hLastRealizedPalette, 0, handle ) == handle)
564 TRACE("unrealizing palette %p\n", handle);
566 return TRUE;
570 /***********************************************************************
571 * PALETTE_DeleteObject
573 static BOOL PALETTE_DeleteObject( HGDIOBJ handle )
575 PALETTEOBJ *obj;
577 PALETTE_UnrealizeObject( handle );
578 if (!(obj = free_gdi_handle( handle ))) return FALSE;
579 HeapFree( GetProcessHeap(), 0, obj->entries );
580 return HeapFree( GetProcessHeap(), 0, obj );
584 /***********************************************************************
585 * GDISelectPalette (Not a Windows API)
587 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
589 HPALETTE ret = 0;
590 DC *dc;
592 TRACE("%p %p\n", hdc, hpal );
594 if (GetObjectType(hpal) != OBJ_PAL)
596 WARN("invalid selected palette %p\n",hpal);
597 return 0;
599 if ((dc = get_dc_ptr( hdc )))
601 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPalette );
602 ret = dc->hPalette;
603 if (physdev->funcs->pSelectPalette( physdev, hpal, FALSE ))
605 dc->hPalette = hpal;
606 if (!wBkg) hPrimaryPalette = hpal;
608 else ret = 0;
609 release_dc_ptr( dc );
611 return ret;
615 /***********************************************************************
616 * GDIRealizePalette (Not a Windows API)
618 UINT WINAPI GDIRealizePalette( HDC hdc )
620 UINT realized = 0;
621 DC* dc = get_dc_ptr( hdc );
623 if (!dc) return 0;
625 TRACE("%p...\n", hdc );
627 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
629 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizeDefaultPalette );
630 realized = physdev->funcs->pRealizeDefaultPalette( physdev );
632 else if (InterlockedExchangePointer( (void **)&hLastRealizedPalette, dc->hPalette ) != dc->hPalette)
634 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizePalette );
635 PALETTEOBJ *palPtr = GDI_GetObjPtr( dc->hPalette, OBJ_PAL );
636 if (palPtr)
638 realized = physdev->funcs->pRealizePalette( physdev, dc->hPalette,
639 (dc->hPalette == hPrimaryPalette) );
640 palPtr->unrealize = physdev->funcs->pUnrealizePalette;
641 GDI_ReleaseObj( dc->hPalette );
644 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
646 release_dc_ptr( dc );
647 TRACE(" realized %i colors.\n", realized );
648 return realized;
652 /***********************************************************************
653 * SelectPalette [GDI32.@]
655 * Selects logical palette into DC.
657 * RETURNS
658 * Success: Previous logical palette
659 * Failure: NULL
661 HPALETTE WINAPI SelectPalette(
662 HDC hDC, /* [in] Handle of device context */
663 HPALETTE hPal, /* [in] Handle of logical color palette */
664 BOOL bForceBackground) /* [in] Foreground/background mode */
666 return pfnSelectPalette( hDC, hPal, bForceBackground );
670 /***********************************************************************
671 * RealizePalette [GDI32.@]
673 * Maps palette entries to system palette.
675 * RETURNS
676 * Success: Number of entries in logical palette
677 * Failure: GDI_ERROR
679 UINT WINAPI RealizePalette(
680 HDC hDC) /* [in] Handle of device context */
682 return pfnRealizePalette( hDC );
686 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
687 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
689 /**********************************************************************
690 * UpdateColors [GDI32.@]
692 * Remaps current colors to logical palette.
694 * RETURNS
695 * Success: TRUE
696 * Failure: FALSE
698 BOOL WINAPI UpdateColors(
699 HDC hDC) /* [in] Handle of device context */
701 HMODULE mod;
702 int size = GetDeviceCaps( hDC, SIZEPALETTE );
704 if (!size) return 0;
706 mod = GetModuleHandleA("user32.dll");
707 if (mod)
709 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
710 if (pWindowFromDC)
712 HWND hWnd = pWindowFromDC( hDC );
714 /* Docs say that we have to remap current drawable pixel by pixel
715 * but it would take forever given the speed of XGet/PutPixel.
717 if (hWnd && size)
719 RedrawWindow_funcptr pRedrawWindow = (void *)GetProcAddress( mod, "RedrawWindow" );
720 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
724 return 0x666;
727 /*********************************************************************
728 * SetMagicColors (GDI32.@)
730 BOOL WINAPI SetMagicColors(HDC hdc, ULONG u1, ULONG u2)
732 FIXME("(%p 0x%08x 0x%08x): stub\n", hdc, u1, u2);
733 return TRUE;