Release 980104
[wine/multimedia.git] / objects / palette.c
bloba1f61531a8619e5272725d362dbae5c9c553cb9e
1 /*
2 * GDI palette objects
4 * Copyright 1993,1994 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
7 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
8 * Information in the "Undocumented Windows" is incorrect.
9 */
11 #include <stdlib.h>
12 #include <string.h>
13 #include <X11/Xlib.h>
15 #include "gdi.h"
16 #include "color.h"
17 #include "palette.h"
18 #include "xmalloc.h"
19 #include "stddebug.h"
20 /* #define DEBUG_PALETTE */
21 #include "debug.h"
23 static UINT32 SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
25 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
26 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
29 /***********************************************************************
30 * PALETTE_Init
32 * Create the system palette.
34 HPALETTE16 PALETTE_Init(void)
36 int i;
37 HPALETTE16 hpalette;
38 LOGPALETTE * palPtr;
39 PALETTEOBJ* palObj;
40 const PALETTEENTRY* __sysPalTemplate = COLOR_GetSystemPaletteTemplate();
42 /* create default palette (20 system colors) */
44 palPtr = HeapAlloc( GetProcessHeap(), 0,
45 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
46 if (!palPtr) return FALSE;
48 palPtr->palVersion = 0x300;
49 palPtr->palNumEntries = NB_RESERVED_COLORS;
50 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
52 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
53 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
54 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
55 palPtr->palPalEntry[i].peFlags = 0;
57 hpalette = CreatePalette16( palPtr );
59 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
61 palObj->mapping = xmalloc( sizeof(int) * 20 );
63 GDI_HEAP_UNLOCK( hpalette );
65 HeapFree( GetProcessHeap(), 0, palPtr );
66 return hpalette;
69 /***********************************************************************
70 * PALETTE_ValidateFlags
72 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
74 int i = 0;
75 for( ; i<size ; i++ )
76 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
80 /***********************************************************************
81 * CreatePalette16 (GDI.360)
83 HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
85 return CreatePalette32( palette );
89 /***********************************************************************
90 * CreatePalette32 (GDI32.53)
92 HPALETTE32 WINAPI CreatePalette32( const LOGPALETTE* palette )
94 PALETTEOBJ * palettePtr;
95 HPALETTE32 hpalette;
96 int size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
98 dprintf_palette(stddeb,"CreatePalette: %i entries, ",
99 palette->palNumEntries);
101 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
102 if (!hpalette) return 0;
104 palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
105 memcpy( &palettePtr->logpalette, palette, size );
106 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
107 palettePtr->logpalette.palNumEntries);
108 palettePtr->mapping = NULL;
109 GDI_HEAP_UNLOCK( hpalette );
111 dprintf_palette(stddeb,"returning %04x\n", hpalette);
112 return hpalette;
115 /***********************************************************************
116 * CreateHalftonePalette (GDI32.47)
118 HPALETTE32 WINAPI CreateHalftonePalette(HDC32 hdc)
120 fprintf(stdnimp,"CreateHalftonePalette: empty stub!\n");
121 return NULL;
124 /***********************************************************************
125 * GetPaletteEntries16 (GDI.363)
127 UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
128 UINT16 count, LPPALETTEENTRY entries )
130 return GetPaletteEntries32( hpalette, start, count, entries );
134 /***********************************************************************
135 * GetPaletteEntries32 (GDI32.209)
137 UINT32 WINAPI GetPaletteEntries32( HPALETTE32 hpalette, UINT32 start,
138 UINT32 count, LPPALETTEENTRY entries )
140 PALETTEOBJ * palPtr;
141 INT32 numEntries;
143 dprintf_palette( stddeb,"GetPaletteEntries: hpal = %04x, %i entries\n",
144 hpalette, count );
146 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
147 if (!palPtr) return 0;
149 numEntries = palPtr->logpalette.palNumEntries;
150 if (start >= numEntries)
152 GDI_HEAP_UNLOCK( hpalette );
153 return 0;
155 if (start+count > numEntries) count = numEntries - start;
156 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
157 count * sizeof(PALETTEENTRY) );
158 for( numEntries = 0; numEntries < count ; numEntries++ )
159 if (entries[numEntries].peFlags & 0xF0)
160 entries[numEntries].peFlags = 0;
161 GDI_HEAP_UNLOCK( hpalette );
162 return count;
166 /***********************************************************************
167 * SetPaletteEntries16 (GDI.364)
169 UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
170 UINT16 count, LPPALETTEENTRY entries )
172 return SetPaletteEntries32( hpalette, start, count, entries );
176 /***********************************************************************
177 * SetPaletteEntries32 (GDI32.326)
179 UINT32 WINAPI SetPaletteEntries32( HPALETTE32 hpalette, UINT32 start,
180 UINT32 count, LPPALETTEENTRY entries )
182 PALETTEOBJ * palPtr;
183 INT32 numEntries;
185 dprintf_palette( stddeb,"SetPaletteEntries: hpal = %04x, %i entries\n",
186 hpalette, count );
188 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
189 if (!palPtr) return 0;
191 numEntries = palPtr->logpalette.palNumEntries;
192 if (start >= numEntries)
194 GDI_HEAP_UNLOCK( hpalette );
195 return 0;
197 if (start+count > numEntries) count = numEntries - start;
198 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
199 count * sizeof(PALETTEENTRY) );
200 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
201 palPtr->logpalette.palNumEntries);
202 free(palPtr->mapping);
203 palPtr->mapping = NULL;
204 GDI_HEAP_UNLOCK( hpalette );
205 return count;
209 /***********************************************************************
210 * ResizePalette16 (GDI.368)
212 BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
214 return ResizePalette32( hPal, cEntries );
218 /***********************************************************************
219 * ResizePalette32 (GDI32.289)
221 BOOL32 WINAPI ResizePalette32( HPALETTE32 hPal, UINT32 cEntries )
223 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
224 UINT32 cPrevEnt, prevVer;
225 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
226 int* mapping = NULL;
228 dprintf_palette(stddeb,"ResizePalette: hpal = %04x, prev = %i, new = %i\n",
229 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
230 cEntries );
231 if( !palPtr ) return FALSE;
232 cPrevEnt = palPtr->logpalette.palNumEntries;
233 prevVer = palPtr->logpalette.palVersion;
234 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
235 sizeof(int*) + sizeof(GDIOBJHDR);
236 size += sizeof(int*) + sizeof(GDIOBJHDR);
237 mapping = palPtr->mapping;
239 GDI_HEAP_UNLOCK( hPal );
241 hPal = GDI_HEAP_REALLOC( hPal, size );
242 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
243 if( !palPtr ) return FALSE;
245 if( mapping )
246 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
247 if( cEntries > cPrevEnt )
249 if( mapping )
250 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
251 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
252 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
253 cEntries - cPrevEnt );
255 palPtr->logpalette.palNumEntries = cEntries;
256 palPtr->logpalette.palVersion = prevVer;
257 GDI_HEAP_UNLOCK( hPal );
258 return TRUE;
262 /***********************************************************************
263 * AnimatePalette16 (GDI.367)
265 void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
266 UINT16 NumEntries, LPPALETTEENTRY PaletteColors)
268 AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
272 /***********************************************************************
273 * AnimatePalette32 (GDI32.6)
275 * FIXME: should use existing mapping when animating a primary palette
277 BOOL32 WINAPI AnimatePalette32( HPALETTE32 hPal, UINT32 StartIndex,
278 UINT32 NumEntries, LPPALETTEENTRY PaletteColors)
280 dprintf_palette(stddeb, "AnimatePalette: %04x (%i - %i)", hPal,
281 StartIndex, StartIndex + NumEntries );
283 if( hPal != STOCK_DEFAULT_PALETTE )
285 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
287 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
289 UINT32 u;
290 for( u = 0; u < NumEntries; u++ )
291 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
292 COLOR_SetMapping(palPtr, StartIndex, NumEntries,
293 hPal != hPrimaryPalette );
294 GDI_HEAP_UNLOCK( hPal );
295 return TRUE;
298 return FALSE;
302 /***********************************************************************
303 * SetSystemPaletteUse16 (GDI.373)
305 UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
307 return SetSystemPaletteUse32( hdc, use );
311 /***********************************************************************
312 * SetSystemPaletteUse32 (GDI32.335)
314 UINT32 WINAPI SetSystemPaletteUse32( HDC32 hdc, UINT32 use )
316 UINT32 old = SystemPaletteUse;
317 fprintf( stdnimp,"SetSystemPaletteUse(%04x,%04x) // empty stub !!!\n",
318 hdc, use );
319 SystemPaletteUse = use;
320 return old;
324 /***********************************************************************
325 * GetSystemPaletteUse16 (GDI.374)
327 UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
329 return SystemPaletteUse;
333 /***********************************************************************
334 * GetSystemPaletteUse32 (GDI32.223)
336 UINT32 WINAPI GetSystemPaletteUse32( HDC32 hdc )
338 return SystemPaletteUse;
342 /***********************************************************************
343 * GetSystemPaletteEntries16 (GDI.375)
345 UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
346 LPPALETTEENTRY entries )
348 return GetSystemPaletteEntries32( hdc, start, count, entries );
352 /***********************************************************************
353 * GetSystemPaletteEntries32 (GDI32.222)
355 UINT32 WINAPI GetSystemPaletteEntries32( HDC32 hdc, UINT32 start, UINT32 count,
356 LPPALETTEENTRY entries )
358 UINT32 i;
359 DC *dc;
361 dprintf_palette(stddeb,"GetSystemPaletteEntries: hdc = %04x, cound = %i", hdc, count );
363 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
364 if (start >= dc->w.devCaps->sizePalette)
366 GDI_HEAP_UNLOCK( hdc );
367 return 0;
369 if (start+count >= dc->w.devCaps->sizePalette)
370 count = dc->w.devCaps->sizePalette - start;
371 for (i = 0; i < count; i++)
373 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
375 dprintf_palette( stddeb,"\tidx(%02x) -> RGB(%08lx)\n",
376 start + i, *(COLORREF*)(entries + i) );
378 GDI_HEAP_UNLOCK( hdc );
379 return count;
383 /***********************************************************************
384 * GetNearestPaletteIndex16 (GDI.370)
386 UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
388 return GetNearestPaletteIndex32( hpalette, color );
392 /***********************************************************************
393 * GetNearestPaletteIndex32 (GDI32.203)
395 UINT32 WINAPI GetNearestPaletteIndex32( HPALETTE32 hpalette, COLORREF color )
397 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
398 UINT32 index = 0;
400 if( palObj )
401 index = COLOR_PaletteLookupPixel( palObj->logpalette.palPalEntry,
402 palObj->logpalette.palNumEntries,
403 NULL, color, FALSE );
405 dprintf_palette(stddeb,"GetNearestPaletteIndex(%04x,%06lx): returning %d\n",
406 hpalette, color, index );
407 GDI_HEAP_UNLOCK( hpalette );
408 return index;
412 /***********************************************************************
413 * GetNearestColor16 (GDI.154)
415 COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
417 return GetNearestColor32( hdc, color );
421 /***********************************************************************
422 * GetNearestColor32 (GDI32.202)
424 COLORREF WINAPI GetNearestColor32( HDC32 hdc, COLORREF color )
426 COLORREF nearest = 0xFADECAFE;
427 DC *dc;
428 PALETTEOBJ *palObj;
430 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
432 palObj = (PALETTEOBJ*)
433 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
434 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
436 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
437 palObj->logpalette.palNumEntries, color );
438 GDI_HEAP_UNLOCK( dc->w.hPalette );
441 dprintf_palette(stddeb,"GetNearestColor(%06lx): returning %06lx\n",
442 color, nearest );
443 GDI_HEAP_UNLOCK( hdc );
444 return nearest;
448 /***********************************************************************
449 * PALETTE_GetObject
451 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
453 if (count > sizeof(WORD)) count = sizeof(WORD);
454 memcpy( buffer, &palette->logpalette.palNumEntries, count );
455 return count;
459 /***********************************************************************
460 * PALETTE_UnrealizeObject
462 BOOL32 PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
464 if (palette->mapping)
466 free( palette->mapping );
467 palette->mapping = NULL;
469 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
470 return TRUE;
474 /***********************************************************************
475 * PALETTE_DeleteObject
477 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
479 free( palette->mapping );
480 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
481 return GDI_FreeObject( hpalette );
485 /***********************************************************************
486 * GDISelectPalette (GDI.361)
488 HPALETTE16 WINAPI GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
490 HPALETTE16 prev;
491 DC *dc;
493 dprintf_palette(stddeb, "GDISelectPalette: %04x %04x\n", hdc, hpal );
495 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
496 if (!dc)
498 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
499 if (!dc) return 0;
501 prev = dc->w.hPalette;
502 dc->w.hPalette = hpal;
503 GDI_HEAP_UNLOCK( hdc );
504 if (!wBkg) hPrimaryPalette = hpal;
505 return prev;
509 /***********************************************************************
510 * GDIRealizePalette (GDI.362)
512 UINT16 WINAPI GDIRealizePalette( HDC16 hdc )
514 PALETTEOBJ* palPtr;
515 int realized = 0;
516 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
517 if (!dc)
519 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
520 if (!dc) return 0;
523 dprintf_palette(stddeb, "GDIRealizePalette: %04x...", hdc );
525 if( dc && dc->w.hPalette != hLastRealizedPalette )
527 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
528 return RealizeDefaultPalette( hdc );
530 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
532 realized = COLOR_SetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
533 (dc->w.hPalette != hPrimaryPalette) ||
534 (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
535 GDI_HEAP_UNLOCK( dc->w.hPalette );
536 hLastRealizedPalette = dc->w.hPalette;
538 else dprintf_palette(stddeb, " skipping (hLastRealizedPalette = %04x) ",
539 hLastRealizedPalette);
541 GDI_HEAP_UNLOCK( hdc );
542 dprintf_palette(stdnimp, " realized %i colors\n", realized );
543 return (UINT16)realized;
547 /***********************************************************************
548 * RealizeDefaultPalette (GDI.365)
550 UINT16 WINAPI RealizeDefaultPalette( HDC16 hdc )
552 DC *dc;
553 PALETTEOBJ* palPtr;
554 int i, index, realized = 0;
556 dprintf_palette(stddeb,"RealizeDefaultPalette: %04x\n", hdc );
558 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
559 if (!dc)
561 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
562 if (!dc) return 0;
565 if ( dc->w.flags & DC_MEMORY )
567 GDI_HEAP_UNLOCK( hdc );
568 return 0;
571 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
572 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
574 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
576 /* lookup is needed to account for SetSystemPaletteUse() stuff */
578 for( i = 0; i < 20; i++ )
580 index = COLOR_LookupSystemPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
582 /* mapping is allocated in COLOR_InitPalette() */
584 if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
586 return realized;
589 /***********************************************************************
590 * IsDCCurrentPalette (GDI.412)
592 BOOL16 WINAPI IsDCCurrentPalette(HDC16 hDC)
594 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
595 if (dc)
597 GDI_HEAP_UNLOCK( hDC );
598 return dc->w.hPalette == hPrimaryPalette;
600 return FALSE;
604 /***********************************************************************
605 * SelectPalette16 (USER.282)
607 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
608 BOOL16 bForceBackground )
610 return SelectPalette32( hDC, hPal, bForceBackground );
614 /***********************************************************************
615 * SelectPalette32 (GDI32.300)
617 HPALETTE32 WINAPI SelectPalette32( HDC32 hDC, HPALETTE32 hPal,
618 BOOL32 bForceBackground )
620 WORD wBkgPalette = 1;
621 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
623 dprintf_palette(stddeb,"SelectPalette: dc %04x pal %04x, force=%i ",
624 hDC, hPal, bForceBackground);
625 if( !lpt ) return 0;
627 dprintf_palette(stddeb," entries = %d\n",
628 lpt->logpalette.palNumEntries);
629 GDI_HEAP_UNLOCK( hPal );
631 if( hPal != STOCK_DEFAULT_PALETTE )
633 HWND32 hWnd = WindowFromDC32( hDC );
634 HWND32 hActive = GetActiveWindow32();
636 /* set primary palette if it's related to current active */
638 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
639 !bForceBackground )
640 wBkgPalette = 0;
642 return GDISelectPalette( hDC, hPal, wBkgPalette);
646 /***********************************************************************
647 * RealizePalette16 (USER.283)
649 UINT16 WINAPI RealizePalette16( HDC16 hDC )
651 return RealizePalette32( hDC );
655 /***********************************************************************
656 * RealizePalette32 (GDI32.280)
658 UINT32 WINAPI RealizePalette32( HDC32 hDC )
660 UINT32 realized = GDIRealizePalette( hDC );
662 /* do not send anything if no colors were changed */
664 if( IsDCCurrentPalette( hDC ) && realized &&
665 !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
667 /* Send palette change notification */
669 HWND32 hWnd;
670 if( (hWnd = WindowFromDC32( hDC )) )
671 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
673 return realized;
677 /**********************************************************************
678 * UpdateColors16 (GDI.366)
680 INT16 WINAPI UpdateColors16( HDC16 hDC )
682 HWND32 hWnd = WindowFromDC32( hDC );
684 /* Docs say that we have to remap current drawable pixel by pixel
685 * but it would take forever given the speed of XGet/PutPixel.
687 if (hWnd && !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
688 InvalidateRect32( hWnd, NULL, FALSE );
689 return 0x666;
693 /**********************************************************************
694 * UpdateColors32 (GDI32.359)
696 BOOL32 WINAPI UpdateColors32( HDC32 hDC )
698 UpdateColors16( hDC );
699 return TRUE;