Release 970415
[wine/multimedia.git] / objects / palette.c
blob75e11a616e6f2d030b09efca8c07d61ecebc4264
1 /*
2 * GDI palette objects
4 * Copyright 1993,1994 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
7 */
9 #include <stdlib.h>
10 #include <string.h>
11 #include <X11/Xlib.h>
13 #include "gdi.h"
14 #include "color.h"
15 #include "palette.h"
16 #include "xmalloc.h"
17 #include "stddebug.h"
18 /* #define DEBUG_PALETTE */
19 #include "debug.h"
21 /* lookup pixel among static entries of the system palette */
22 extern int COLOR_LookupSystemPixel(COLORREF);
24 static UINT32 SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
26 static HPALETTE16 hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
27 static HPALETTE16 hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
30 /***********************************************************************
31 * PALETTE_ValidateFlags
33 void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
35 int i = 0;
36 for( ; i<size ; i++ )
37 lpPalE[i].peFlags = PC_SYS_USED | (lpPalE[i].peFlags & 0x07);
41 /***********************************************************************
42 * CreatePalette16 (GDI.360)
44 HPALETTE16 CreatePalette16( const LOGPALETTE* palette )
46 return CreatePalette32( palette );
50 /***********************************************************************
51 * CreatePalette32 (GDI32.53)
53 HPALETTE32 CreatePalette32( const LOGPALETTE* palette )
55 PALETTEOBJ * palettePtr;
56 HPALETTE32 hpalette;
57 int size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
59 dprintf_palette(stddeb,"CreatePalette: %i entries, ",
60 palette->palNumEntries);
62 hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
63 if (!hpalette) return 0;
65 palettePtr = (PALETTEOBJ *) GDI_HEAP_LIN_ADDR( hpalette );
66 memcpy( &palettePtr->logpalette, palette, size );
67 PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry,
68 palettePtr->logpalette.palNumEntries);
69 palettePtr->mapping = NULL;
71 dprintf_palette(stddeb,"returning %04x\n", hpalette);
72 return hpalette;
76 /***********************************************************************
77 * GetPaletteEntries16 (GDI.363)
79 UINT16 GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start, UINT16 count,
80 LPPALETTEENTRY entries )
82 return GetPaletteEntries32( hpalette, start, count, entries );
86 /***********************************************************************
87 * GetPaletteEntries32 (GDI32.209)
89 UINT32 GetPaletteEntries32( HPALETTE32 hpalette, UINT32 start, UINT32 count,
90 LPPALETTEENTRY entries )
92 PALETTEOBJ * palPtr;
93 INT32 numEntries;
95 dprintf_palette( stddeb,"GetPaletteEntries: hpal = %04x, %i entries\n",
96 hpalette, count );
98 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
99 if (!palPtr) return 0;
101 numEntries = palPtr->logpalette.palNumEntries;
102 if (start >= numEntries) return 0;
103 if (start+count > numEntries) count = numEntries - start;
104 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
105 count * sizeof(PALETTEENTRY) );
106 for( numEntries = 0; numEntries < count ; numEntries++ )
107 if (entries[numEntries].peFlags & 0xF0)
108 entries[numEntries].peFlags = 0;
109 return count;
113 /***********************************************************************
114 * SetPaletteEntries16 (GDI.364)
116 UINT16 SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start, UINT16 count,
117 LPPALETTEENTRY entries )
119 return SetPaletteEntries32( hpalette, start, count, entries );
123 /***********************************************************************
124 * SetPaletteEntries32 (GDI32.326)
126 UINT32 SetPaletteEntries32( HPALETTE32 hpalette, UINT32 start, UINT32 count,
127 LPPALETTEENTRY entries )
129 PALETTEOBJ * palPtr;
130 INT32 numEntries;
132 dprintf_palette( stddeb,"SetPaletteEntries: hpal = %04x, %i entries\n",
133 hpalette, count );
135 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
136 if (!palPtr) return 0;
138 numEntries = palPtr->logpalette.palNumEntries;
139 if (start >= numEntries) return 0;
140 if (start+count > numEntries) count = numEntries - start;
141 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
142 count * sizeof(PALETTEENTRY) );
143 PALETTE_ValidateFlags(palPtr->logpalette.palPalEntry,
144 palPtr->logpalette.palNumEntries);
145 free(palPtr->mapping);
146 palPtr->mapping = NULL;
148 return count;
152 /***********************************************************************
153 * ResizePalette16 (GDI.368)
155 BOOL16 ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
157 return ResizePalette32( hPal, cEntries );
161 /***********************************************************************
162 * ResizePalette32 (GDI32.289)
164 BOOL32 ResizePalette32( HPALETTE32 hPal, UINT32 cEntries )
166 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
167 UINT32 cPrevEnt, prevVer;
168 int prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
169 int* mapping = NULL;
171 dprintf_palette(stddeb,"ResizePalette: hpal = %04x, prev = %i, new = %i\n",
172 hPal, palPtr ? palPtr->logpalette.palNumEntries : -1,
173 cEntries );
174 if( !palPtr ) return FALSE;
175 cPrevEnt = palPtr->logpalette.palNumEntries;
176 prevVer = palPtr->logpalette.palVersion;
177 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) +
178 sizeof(int*) + sizeof(GDIOBJHDR);
179 size += sizeof(int*) + sizeof(GDIOBJHDR);
180 mapping = palPtr->mapping;
182 hPal = GDI_HEAP_REALLOC( hPal, size );
183 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
184 if( !palPtr ) return FALSE;
186 if( mapping )
187 palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
188 if( cEntries > cPrevEnt )
190 if( mapping )
191 memset(palPtr->mapping + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
192 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
193 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize),
194 cEntries - cPrevEnt );
196 palPtr->logpalette.palNumEntries = cEntries;
197 palPtr->logpalette.palVersion = prevVer;
198 return TRUE;
202 /***********************************************************************
203 * AnimatePalette16 (GDI.367)
205 BOOL16 AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex, UINT16 NumEntries,
206 LPPALETTEENTRY PaletteColors )
208 return AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
212 /***********************************************************************
213 * AnimatePalette32 (GDI32.6)
215 BOOL32 AnimatePalette32( HPALETTE32 hPal, UINT32 StartIndex, UINT32 NumEntries,
216 LPPALETTEENTRY PaletteColors )
218 fprintf(stdnimp,"AnimatePalette: empty stub! \n");
219 return TRUE;
223 /***********************************************************************
224 * SetSystemPaletteUse16 (GDI.373)
226 UINT16 SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
228 return SetSystemPaletteUse32( hdc, use );
232 /***********************************************************************
233 * SetSystemPaletteUse32 (GDI32.335)
235 UINT32 SetSystemPaletteUse32( HDC32 hdc, UINT32 use )
237 UINT32 old = SystemPaletteUse;
238 fprintf( stdnimp,"SetSystemPaletteUse(%04x,%04x) // empty stub !!!\n",
239 hdc, use );
240 SystemPaletteUse = use;
241 return old;
245 /***********************************************************************
246 * GetSystemPaletteUse16 (GDI.374)
248 UINT16 GetSystemPaletteUse16( HDC16 hdc )
250 return SystemPaletteUse;
254 /***********************************************************************
255 * GetSystemPaletteUse32 (GDI32.223)
257 UINT32 GetSystemPaletteUse32( HDC32 hdc )
259 return SystemPaletteUse;
263 /***********************************************************************
264 * GetSystemPaletteEntries16 (GDI.375)
266 UINT16 GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
267 LPPALETTEENTRY entries )
269 return GetSystemPaletteEntries32( hdc, start, count, entries );
273 /***********************************************************************
274 * GetSystemPaletteEntries32 (GDI32.222)
276 UINT32 GetSystemPaletteEntries32( HDC32 hdc, UINT32 start, UINT32 count,
277 LPPALETTEENTRY entries )
279 UINT32 i;
280 DC *dc;
282 dprintf_palette(stddeb,"GetSystemPaletteEntries: hdc = %04x, cound = %i", hdc, count );
284 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
285 if (start >= dc->w.devCaps->sizePalette) return 0;
286 if (start+count >= dc->w.devCaps->sizePalette)
287 count = dc->w.devCaps->sizePalette - start;
288 for (i = 0; i < count; i++)
290 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry( start + i );
292 dprintf_palette( stddeb,"\tidx(%02x) -> RGB(%08lx)\n",
293 start + i, *(COLORREF*)(entries + i) );
295 return count;
299 /***********************************************************************
300 * GetNearestPaletteIndex16 (GDI.370)
302 UINT16 GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
304 return GetNearestPaletteIndex32( hpalette, color );
308 /***********************************************************************
309 * GetNearestPaletteIndex32 (GDI32.203)
311 UINT32 GetNearestPaletteIndex32( HPALETTE32 hpalette, COLORREF color )
313 PALETTEOBJ* palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
314 UINT32 index = 0;
316 if( palObj )
317 index = COLOR_PaletteLookupPixel( palObj->logpalette.palPalEntry,
318 palObj->logpalette.palNumEntries,
319 NULL, color, FALSE );
321 dprintf_palette(stddeb,"GetNearestPaletteIndex(%04x,%06lx): returning %d\n",
322 hpalette, color, index );
323 return index;
327 /***********************************************************************
328 * GetNearestColor16 (GDI.154)
330 COLORREF GetNearestColor16( HDC16 hdc, COLORREF color )
332 return GetNearestColor32( hdc, color );
336 /***********************************************************************
337 * GetNearestColor32 (GDI32.202)
339 COLORREF GetNearestColor32( HDC32 hdc, COLORREF color )
341 COLORREF nearest = 0xFADECAFE;
342 DC *dc;
343 PALETTEOBJ *palObj;
345 if ( (dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )) )
347 palObj = (PALETTEOBJ*)
348 GDI_GetObjPtr( (dc->w.hPalette)? dc->w.hPalette
349 : STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
351 nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
352 palObj->logpalette.palNumEntries, color );
355 dprintf_palette(stddeb,"GetNearestColor(%06lx): returning %06lx\n",
356 color, nearest );
357 return nearest;
361 /***********************************************************************
362 * PALETTE_GetObject
364 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
366 if (count > sizeof(WORD)) count = sizeof(WORD);
367 memcpy( buffer, &palette->logpalette.palNumEntries, count );
368 return count;
372 /***********************************************************************
373 * PALETTE_UnrealizeObject
375 BOOL32 PALETTE_UnrealizeObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
377 if (palette->mapping)
379 free( palette->mapping );
380 palette->mapping = NULL;
382 if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
383 return TRUE;
387 /***********************************************************************
388 * PALETTE_DeleteObject
390 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
392 free( palette->mapping );
393 return GDI_FreeObject( hpalette );
397 /***********************************************************************
398 * GDISelectPalette (GDI.361)
400 HPALETTE16 GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
402 HPALETTE16 prev;
403 DC *dc;
405 dprintf_palette(stddeb, "GDISelectPalette: %04x %04x\n", hdc, hpal );
407 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
408 if (!dc)
410 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
411 if (!dc) return 0;
413 prev = dc->w.hPalette;
414 dc->w.hPalette = hpal;
415 if (!wBkg) hPrimaryPalette = hpal;
416 return prev;
420 /***********************************************************************
421 * GDIRealizePalette (GDI.362)
423 UINT16 GDIRealizePalette( HDC16 hdc )
425 PALETTEOBJ* palPtr;
426 int realized = 0;
427 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
428 if (!dc)
430 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
431 if (!dc) return 0;
434 dprintf_palette(stddeb, "GDIRealizePalette: %04x...", hdc );
436 if( dc && dc->w.hPalette != hLastRealizedPalette )
438 if( dc->w.hPalette == STOCK_DEFAULT_PALETTE )
439 return RealizeDefaultPalette( hdc );
441 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
443 realized = COLOR_SetMapping(palPtr, dc->w.hPalette != hPrimaryPalette
444 || dc->w.hPalette == STOCK_DEFAULT_PALETTE );
445 hLastRealizedPalette = dc->w.hPalette;
447 else dprintf_palette(stddeb, " skipping ");
449 dprintf_palette(stdnimp, " realized %i colors\n", realized );
450 return (UINT16)realized;
454 /***********************************************************************
455 * RealizeDefaultPalette (GDI.365)
457 UINT16 RealizeDefaultPalette( HDC16 hdc )
459 DC *dc;
460 PALETTEOBJ* palPtr;
461 int i, index, realized = 0;
463 dprintf_palette(stddeb,"RealizeDefaultPalette: %04x\n", hdc );
465 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
466 if (!dc)
468 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
469 if (!dc) return 0;
472 if ( dc->w.flags & DC_MEMORY ) return 0;
474 hPrimaryPalette = STOCK_DEFAULT_PALETTE;
475 hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
477 palPtr = (PALETTEOBJ*)GDI_GetObjPtr(STOCK_DEFAULT_PALETTE, PALETTE_MAGIC );
479 /* lookup is needed to account for SetSystemPaletteUse() stuff */
481 for( i = 0; i < 20; i++ )
483 index = COLOR_LookupSystemPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
485 /* mapping is allocated in COLOR_InitPalette() */
487 if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
489 return realized;
492 /***********************************************************************
493 * IsDCCurrentPalette (GDI.412)
495 BOOL16 IsDCCurrentPalette(HDC16 hDC)
497 DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
498 return (dc)?(dc->w.hPalette == hPrimaryPalette):FALSE;
502 /***********************************************************************
503 * SelectPalette16 (USER.282)
505 HPALETTE16 SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
506 BOOL16 bForceBackground )
508 return SelectPalette32( hDC, hPal, bForceBackground );
512 /***********************************************************************
513 * SelectPalette32 (GDI32.300)
515 HPALETTE32 SelectPalette32( HDC32 hDC, HPALETTE32 hPal,
516 BOOL32 bForceBackground )
518 WORD wBkgPalette = 1;
519 PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
521 dprintf_palette(stddeb,"SelectPalette: dc %04x pal %04x, force=%i ",
522 hDC, hPal, bForceBackground);
523 if( !lpt ) return 0;
525 dprintf_palette(stddeb," entries = %d\n",
526 lpt->logpalette.palNumEntries);
528 if( hPal != STOCK_DEFAULT_PALETTE )
530 HWND32 hWnd = WindowFromDC32( hDC );
531 HWND32 hActive = GetActiveWindow32();
533 /* set primary palette if it's related to current active */
535 if((!hWnd || (hActive == hWnd || IsChild16(hActive,hWnd))) &&
536 !bForceBackground )
537 wBkgPalette = 0;
539 return GDISelectPalette( hDC, hPal, wBkgPalette);
543 /***********************************************************************
544 * RealizePalette16 (USER.283)
546 UINT16 RealizePalette16( HDC16 hDC )
548 return RealizePalette32( hDC );
552 /***********************************************************************
553 * RealizePalette32 (GDI32.280)
555 UINT32 RealizePalette32( HDC32 hDC )
557 UINT32 realized = GDIRealizePalette( hDC );
559 /* do not send anything if no colors were changed */
561 if( IsDCCurrentPalette( hDC ) && realized &&
562 !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
564 /* Send palette change notification */
566 HWND32 hWnd;
567 if( (hWnd = WindowFromDC32( hDC )) )
568 SendMessage16( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
570 return realized;
574 /**********************************************************************
575 * UpdateColors16 (GDI.366)
577 INT16 UpdateColors16( HDC16 hDC )
579 HWND32 hWnd = WindowFromDC32( hDC );
581 /* Docs say that we have to remap current drawable pixel by pixel
582 * but it would take forever given the speed of XGet/PutPixel.
584 if (hWnd && !(COLOR_GetSystemPaletteFlags() & COLOR_VIRTUAL) )
585 InvalidateRect32( hWnd, NULL, FALSE );
586 return 0x666;
590 /**********************************************************************
591 * UpdateColors32 (GDI32.359)
593 BOOL32 UpdateColors32( HDC32 hDC )
595 UpdateColors16( hDC );
596 return TRUE;