2 * X11DRV OEM bitmap objects
4 * Copyright 1994, 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(palette
);
34 /* Palette indexed mode:
35 * logical palette -> mapping -> pixel
38 * Windows needs contiguous color space ( from 0 to n ) but
39 * it is possible only with the private colormap. Otherwise we
40 * have to map DC palette indices to real pixel values. With
41 * private colormaps it boils down to the identity mapping. The
42 * other special case is when we have a fixed color visual with
43 * the screendepth > 8 - we abandon palette mappings altogether
44 * because pixel values can be calculated without X server
48 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
50 #define PC_SYS_USED 0x80 /* palentry is used (both system and logical) */
51 #define PC_SYS_RESERVED 0x40 /* system palentry is not to be mapped to */
53 static PALETTEENTRY
*COLOR_sysPal
; /* current system palette */
55 static int COLOR_gapStart
= 256;
56 static int COLOR_gapEnd
= -1;
57 static int COLOR_gapFilled
= 0;
59 Colormap X11DRV_PALETTE_PaletteXColormap
= 0;
60 UINT16 X11DRV_PALETTE_PaletteFlags
= 0;
62 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
63 ColorShifts X11DRV_PALETTE_default_shifts
= { {0,0,0,}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
64 static int X11DRV_PALETTE_Graymax
= 0;
66 static int palette_size
;
68 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
69 static int X11DRV_PALETTE_firstFree
= 0;
70 static unsigned char X11DRV_PALETTE_freeList
[256];
72 static XContext palette_context
; /* X context to associate a color mapping to a palette */
74 static CRITICAL_SECTION palette_cs
;
75 static CRITICAL_SECTION_DEBUG critsect_debug
=
78 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
79 0, 0, { (DWORD_PTR
)(__FILE__
": palette_cs") }
81 static CRITICAL_SECTION palette_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
83 /**********************************************************************/
85 /* Map an EGA index (0..15) to a pixel value in the system color space. */
87 int X11DRV_PALETTE_mapEGAPixel
[16];
89 /**********************************************************************/
91 #define NB_COLORCUBE_START_INDEX 63
92 #define NB_PALETTE_EMPTY_VALUE -1
94 /* Maps entry in the system palette to X pixel value */
95 int *X11DRV_PALETTE_PaletteToXPixel
= NULL
;
97 /* Maps pixel to the entry in the system palette */
98 int *X11DRV_PALETTE_XPixelToPalette
= NULL
;
100 /**********************************************************************/
102 static BOOL
X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY
*sys_pal_template
);
103 static BOOL
X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY
*sys_pal_template
);
104 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY
*sys_pal_template
);
105 static void X11DRV_PALETTE_FormatSystemPalette(void);
106 static BOOL
X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY
*sys_pal_template
, COLORREF c
);
107 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
);
110 /***********************************************************************
111 * palette_get_mapping
113 static int *palette_get_mapping( HPALETTE hpal
)
117 if (XFindContext( gdi_display
, (XID
)hpal
, palette_context
, (char **)&mapping
)) mapping
= NULL
;
122 /***********************************************************************
123 * palette_set_mapping
125 static void palette_set_mapping( HPALETTE hpal
, int *mapping
)
127 XSaveContext( gdi_display
, (XID
)hpal
, palette_context
, (char *)mapping
);
131 /***********************************************************************
134 * Initialize color management.
136 int X11DRV_PALETTE_Init(void)
138 int mask
, white
, black
;
141 PALETTEENTRY sys_pal_template
[NB_RESERVED_COLORS
];
143 TRACE("initializing palette manager...\n");
145 palette_context
= XUniqueContext();
146 white
= WhitePixel( gdi_display
, DefaultScreen(gdi_display
) );
147 black
= BlackPixel( gdi_display
, DefaultScreen(gdi_display
) );
149 for( mask
= 1; !((white
& mask
)^(black
& mask
)); mask
<<= 1 )
151 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
152 palette_size
= visual
->map_entries
;
154 switch(visual
->class)
157 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
161 if (private_color_map
)
163 XSetWindowAttributes win_attr
;
165 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap( gdi_display
, root_window
,
167 if (X11DRV_PALETTE_PaletteXColormap
)
169 X11DRV_PALETTE_PaletteFlags
|= (X11DRV_PALETTE_PRIVATE
| X11DRV_PALETTE_WHITESET
);
172 for( white
= palette_size
- 1; !(white
& 1); white
>>= 1 )
175 if( root_window
!= DefaultRootWindow(gdi_display
) )
177 win_attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
;
178 XChangeWindowAttributes( gdi_display
, root_window
, CWColormap
, &win_attr
);
182 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
189 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
191 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
192 X11DRV_PALETTE_Graymax
= (1 << screen_depth
)-1;
196 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
198 int *depths
,nrofdepths
;
199 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
203 depths
= XListDepths(gdi_display
,DefaultScreen(gdi_display
),&nrofdepths
);
204 if ((nrofdepths
==2) && ((depths
[0]==4) || depths
[1]==4)) {
206 for( white
= palette_size
- 1; !(white
& 1); white
>>= 1 )
208 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
209 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
214 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
216 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
217 X11DRV_PALETTE_ComputeColorShifts(&X11DRV_PALETTE_default_shifts
, visual
->red_mask
, visual
->green_mask
, visual
->blue_mask
);
225 TRACE(" visual class %i (%i)\n", visual
->class, monoPlane
);
227 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE
), 0, NB_RESERVED_COLORS
, sys_pal_template
);
229 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
235 if ((mapping
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(int) * NB_RESERVED_COLORS
)))
236 palette_set_mapping( GetStockObject(DEFAULT_PALETTE
), mapping
);
238 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
239 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template
);
241 X11DRV_PALETTE_BuildSharedMap( sys_pal_template
);
243 /* Build free list */
245 if( X11DRV_PALETTE_firstFree
!= -1 )
246 X11DRV_PALETTE_FormatSystemPalette();
248 X11DRV_PALETTE_FillDefaultColors( sys_pal_template
);
249 palette_size
= visual
->map_entries
;
255 /***********************************************************************
256 * X11DRV_PALETTE_Cleanup
258 * Free external colors we grabbed in the FillDefaultPalette()
260 void X11DRV_PALETTE_Cleanup(void)
262 if( COLOR_gapFilled
)
264 XFreeColors(gdi_display
, X11DRV_PALETTE_PaletteXColormap
,
265 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel
+ COLOR_gapStart
),
268 DeleteCriticalSection(&palette_cs
);
271 /***********************************************************************
272 * X11DRV_PALETTE_ComputeChannelShift
274 * Calculate conversion parameters for a given color mask
276 static void X11DRV_PALETTE_ComputeChannelShift(unsigned long maskbits
, ChannelShift
*physical
, ChannelShift
*to_logical
)
291 for(i
=0;!(maskbits
&1);i
++)
295 physical
->max
= maskbits
;
297 for(i
=0;maskbits
!=0;i
++)
301 if (physical
->scale
>8)
303 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
304 * So we adjust the shifts to also normalize the color fields to
305 * the Win32 standard of 8 bits per color.
307 to_logical
->shift
=physical
->shift
+(physical
->scale
-8);
309 to_logical
->max
=0xff;
311 to_logical
->shift
=physical
->shift
;
312 to_logical
->scale
=physical
->scale
;
313 to_logical
->max
=physical
->max
;
317 /***********************************************************************
318 * X11DRV_PALETTE_ComputeColorShifts
320 * Calculate conversion parameters for a given color
322 void X11DRV_PALETTE_ComputeColorShifts(ColorShifts
*shifts
, unsigned long redMask
, unsigned long greenMask
, unsigned long blueMask
)
324 X11DRV_PALETTE_ComputeChannelShift(redMask
, &shifts
->physicalRed
, &shifts
->logicalRed
);
325 X11DRV_PALETTE_ComputeChannelShift(greenMask
, &shifts
->physicalGreen
, &shifts
->logicalGreen
);
326 X11DRV_PALETTE_ComputeChannelShift(blueMask
, &shifts
->physicalBlue
, &shifts
->logicalBlue
);
329 /***********************************************************************
330 * X11DRV_PALETTE_BuildPrivateMap
332 * Allocate colorcells and initialize mapping tables.
334 static BOOL
X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY
*sys_pal_template
)
336 /* Private colormap - identity mapping */
341 if((COLOR_sysPal
= HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY
)*palette_size
)) == NULL
) {
342 WARN("Unable to allocate the system palette\n");
346 TRACE("Building private map - %i palette entries\n", palette_size
);
348 /* Allocate system palette colors */
351 for( i
=0; i
< palette_size
; i
++ )
353 if( i
< NB_RESERVED_COLORS
/2 )
355 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
356 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
357 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
358 COLOR_sysPal
[i
] = sys_pal_template
[i
];
359 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
361 else if( i
>= palette_size
- NB_RESERVED_COLORS
/2 )
363 int j
= NB_RESERVED_COLORS
+ i
- palette_size
;
364 color
.red
= sys_pal_template
[j
].peRed
* 65535 / 255;
365 color
.green
= sys_pal_template
[j
].peGreen
* 65535 / 255;
366 color
.blue
= sys_pal_template
[j
].peBlue
* 65535 / 255;
367 COLOR_sysPal
[i
] = sys_pal_template
[j
];
368 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
371 color
.flags
= DoRed
| DoGreen
| DoBlue
;
373 XStoreColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
375 /* Set EGA mapping if color is from the first or last eight */
378 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
379 else if (i
>= palette_size
- 8 )
380 X11DRV_PALETTE_mapEGAPixel
[i
- (palette_size
- 16)] = color
.pixel
;
384 X11DRV_PALETTE_XPixelToPalette
= X11DRV_PALETTE_PaletteToXPixel
= NULL
;
386 COLOR_gapStart
= 256; COLOR_gapEnd
= -1;
388 X11DRV_PALETTE_firstFree
= (palette_size
> NB_RESERVED_COLORS
)?NB_RESERVED_COLORS
/2 : -1;
393 /***********************************************************************
394 * X11DRV_PALETTE_BuildSharedMap
396 * Allocate colorcells and initialize mapping tables.
398 static BOOL
X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY
*sys_pal_template
)
401 unsigned long sysPixel
[NB_RESERVED_COLORS
];
402 unsigned long* pixDynMapping
= NULL
;
403 unsigned long plane_masks
[1];
405 int diff
, r
, g
, b
, bp
= 0, wp
= 1;
407 unsigned int max
= 256;
409 XColor defaultColors
[256];
411 /* Copy the first bunch of colors out of the default colormap to prevent
412 * colormap flashing as much as possible. We're likely to get the most
413 * important Window Manager colors, etc in the first 128 colors */
414 defaultCM
= DefaultColormap( gdi_display
, DefaultScreen(gdi_display
) );
416 if (copy_default_colors
> 256) copy_default_colors
= 256;
417 for (i
= 0; i
< copy_default_colors
; i
++)
418 defaultColors
[i
].pixel
= (long) i
;
420 XQueryColors(gdi_display
, defaultCM
, &defaultColors
[0], copy_default_colors
);
421 for (i
= 0; i
< copy_default_colors
; i
++)
422 XAllocColor( gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &defaultColors
[i
] );
424 if (alloc_system_colors
> 256) alloc_system_colors
= 256;
425 else if (alloc_system_colors
< 20) alloc_system_colors
= 20;
426 TRACE("%d colors configured.\n", alloc_system_colors
);
428 TRACE("Building shared map - %i palette entries\n", palette_size
);
430 /* Be nice and allocate system colors as read-only */
432 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
434 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
435 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
436 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
437 color
.flags
= DoRed
| DoGreen
| DoBlue
;
439 if (!XAllocColor( gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
))
445 WARN("Not enough colors for the full system palette.\n");
447 bp
= BlackPixel(gdi_display
, DefaultScreen(gdi_display
));
448 wp
= WhitePixel(gdi_display
, DefaultScreen(gdi_display
));
450 max
= (0xffffffff)>>(32 - screen_depth
);
458 /* reinit color (XAllocColor() may change it)
459 * and map to the best shared colorcell */
461 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
462 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
463 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
465 best
.pixel
= best
.red
= best
.green
= best
.blue
= 0;
466 for( c
.pixel
= 0, diff
= 0x7fffffff; c
.pixel
< max
; c
.pixel
+= step
)
468 XQueryColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &c
);
469 r
= (c
.red
- color
.red
)>>8;
470 g
= (c
.green
- color
.green
)>>8;
471 b
= (c
.blue
- color
.blue
)>>8;
473 if( r
< diff
) { best
= c
; diff
= r
; }
476 if( XAllocColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &best
) )
477 color
.pixel
= best
.pixel
;
478 else color
.pixel
= (i
< NB_RESERVED_COLORS
/2)? bp
: wp
;
481 sysPixel
[i
] = color
.pixel
;
483 TRACE("syscolor(%x) -> pixel %i\n", *(const COLORREF
*)(sys_pal_template
+i
),
486 /* Set EGA mapping if color in the first or last eight */
489 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
490 else if (i
>= NB_RESERVED_COLORS
- 8 )
491 X11DRV_PALETTE_mapEGAPixel
[i
- (NB_RESERVED_COLORS
-16)] = color
.pixel
;
495 /* now allocate changeable set */
497 if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
499 int c_min
= 0, c_max
= palette_size
, c_val
;
501 TRACE("Dynamic colormap...\n");
503 /* let's become the first client that actually follows
504 * X guidelines and does binary search...
507 if((pixDynMapping
= HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size
)) == NULL
) {
508 WARN("Out of memory while building system palette.\n");
513 /* comment this out if you want to debug palette init */
514 XGrabServer(gdi_display
);
516 while( c_max
- c_min
> 0 )
518 c_val
= (c_max
+ c_min
)/2 + (c_max
+ c_min
)%2;
520 if( !XAllocColorCells(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, False
,
521 plane_masks
, 0, pixDynMapping
, c_val
) )
525 XFreeColors(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, pixDynMapping
, c_val
, 0);
530 if( c_min
> alloc_system_colors
- NB_RESERVED_COLORS
)
531 c_min
= alloc_system_colors
- NB_RESERVED_COLORS
;
533 c_min
= (c_min
/2) + (c_min
/2); /* need even set for split palette */
536 if( !XAllocColorCells(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, False
,
537 plane_masks
, 0, pixDynMapping
, c_min
) )
539 WARN("Inexplicable failure during colorcell allocation.\n");
543 palette_size
= c_min
+ NB_RESERVED_COLORS
;
545 XUngrabServer(gdi_display
);
548 TRACE("adjusted size %i colorcells\n", palette_size
);
550 else if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
552 /* virtual colorspace - ToPhysical takes care of
553 * color translations but we have to allocate full palette
554 * to maintain compatibility
557 TRACE("Virtual colorspace - screendepth %i\n", screen_depth
);
559 else palette_size
= NB_RESERVED_COLORS
; /* system palette only - however we can alloc a bunch
560 * of colors and map to them */
562 TRACE("Shared system palette uses %i colors.\n", palette_size
);
564 /* set gap to account for pixel shortage. It has to be right in the center
565 * of the system palette because otherwise raster ops get screwed. */
567 if( palette_size
>= 256 )
568 { COLOR_gapStart
= 256; COLOR_gapEnd
= -1; }
570 { COLOR_gapStart
= palette_size
/2; COLOR_gapEnd
= 255 - palette_size
/2; }
572 X11DRV_PALETTE_firstFree
= ( palette_size
> NB_RESERVED_COLORS
&&
573 (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
|| !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)) )
574 ? NB_RESERVED_COLORS
/2 : -1;
576 COLOR_sysPal
= HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY
)*256);
577 if(COLOR_sysPal
== NULL
) {
578 ERR("Unable to allocate the system palette!\n");
579 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
583 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
585 if (screen_depth
<= 8)
587 X11DRV_PALETTE_XPixelToPalette
= HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
588 if(X11DRV_PALETTE_XPixelToPalette
== NULL
) {
589 ERR("Out of memory: XPixelToPalette!\n");
590 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
593 for( i
= 0; i
< 256; i
++ )
594 X11DRV_PALETTE_XPixelToPalette
[i
] = NB_PALETTE_EMPTY_VALUE
;
597 /* for hicolor visuals PaletteToPixel mapping is used to skip
598 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
601 X11DRV_PALETTE_PaletteToXPixel
= HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
602 if(X11DRV_PALETTE_PaletteToXPixel
== NULL
) {
603 ERR("Out of memory: PaletteToXPixel!\n");
604 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
608 for( i
= j
= 0; i
< 256; i
++ )
610 if( i
>= COLOR_gapStart
&& i
<= COLOR_gapEnd
)
612 X11DRV_PALETTE_PaletteToXPixel
[i
] = NB_PALETTE_EMPTY_VALUE
;
613 COLOR_sysPal
[i
].peFlags
= 0; /* mark as unused */
617 if( i
< NB_RESERVED_COLORS
/2 )
619 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[i
];
620 COLOR_sysPal
[i
] = sys_pal_template
[i
];
621 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
623 else if( i
>= 256 - NB_RESERVED_COLORS
/2 )
625 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[(i
+ NB_RESERVED_COLORS
) - 256];
626 COLOR_sysPal
[i
] = sys_pal_template
[(i
+ NB_RESERVED_COLORS
) - 256];
627 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
629 else if( pixDynMapping
)
630 X11DRV_PALETTE_PaletteToXPixel
[i
] = pixDynMapping
[j
++];
632 X11DRV_PALETTE_PaletteToXPixel
[i
] = i
;
634 TRACE("index %i -> pixel %i\n", i
, X11DRV_PALETTE_PaletteToXPixel
[i
]);
636 if( X11DRV_PALETTE_XPixelToPalette
)
637 X11DRV_PALETTE_XPixelToPalette
[X11DRV_PALETTE_PaletteToXPixel
[i
]] = i
;
640 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
645 /***********************************************************************
646 * Colormap Initialization
648 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY
*sys_pal_template
)
650 /* initialize unused entries to what Windows uses as a color
651 * cube - based on Greg Kreider's code.
655 int red
, no_r
, inc_r
;
656 int green
, no_g
, inc_g
;
657 int blue
, no_b
, inc_b
;
659 if (palette_size
<= NB_RESERVED_COLORS
)
661 while (i
*i
*i
< (palette_size
- NB_RESERVED_COLORS
)) i
++;
662 no_r
= no_g
= no_b
= --i
;
663 if ((no_r
* (no_g
+1) * no_b
) < (palette_size
- NB_RESERVED_COLORS
)) no_g
++;
664 if ((no_r
* no_g
* (no_b
+1)) < (palette_size
- NB_RESERVED_COLORS
)) no_b
++;
665 inc_r
= (255 - NB_COLORCUBE_START_INDEX
)/no_r
;
666 inc_g
= (255 - NB_COLORCUBE_START_INDEX
)/no_g
;
667 inc_b
= (255 - NB_COLORCUBE_START_INDEX
)/no_b
;
671 idx
= X11DRV_PALETTE_firstFree
;
674 for (blue
= NB_COLORCUBE_START_INDEX
; blue
< 256 && idx
; blue
+= inc_b
)
675 for (green
= NB_COLORCUBE_START_INDEX
; green
< 256 && idx
; green
+= inc_g
)
676 for (red
= NB_COLORCUBE_START_INDEX
; red
< 256 && idx
; red
+= inc_r
)
680 if( red
== NB_COLORCUBE_START_INDEX
&& green
== red
&& blue
== green
) continue;
682 COLOR_sysPal
[idx
].peRed
= red
;
683 COLOR_sysPal
[idx
].peGreen
= green
;
684 COLOR_sysPal
[idx
].peBlue
= blue
;
688 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
690 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
691 if (shifts
->physicalRed
.max
!= 255) no_r
= (red
* shifts
->physicalRed
.max
) / 255;
692 if (shifts
->physicalGreen
.max
!= 255) no_g
= (green
* shifts
->physicalGreen
.max
) / 255;
693 if (shifts
->physicalBlue
.max
!= 255) no_b
= (blue
* shifts
->physicalBlue
.max
) / 255;
695 X11DRV_PALETTE_PaletteToXPixel
[idx
] = (no_r
<< shifts
->physicalRed
.shift
) | (no_g
<< shifts
->physicalGreen
.shift
) | (no_b
<< shifts
->physicalBlue
.shift
);
697 else if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
700 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[idx
] : idx
;
701 color
.red
= COLOR_sysPal
[idx
].peRed
<< 8;
702 color
.green
= COLOR_sysPal
[idx
].peGreen
<< 8;
703 color
.blue
= COLOR_sysPal
[idx
].peBlue
<< 8;
704 color
.flags
= DoRed
| DoGreen
| DoBlue
;
705 XStoreColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
707 idx
= X11DRV_PALETTE_freeList
[idx
];
710 /* try to fill some entries in the "gap" with
711 * what's already in the colormap - they will be
712 * mappable to but not changeable. */
714 if( COLOR_gapStart
< COLOR_gapEnd
&& X11DRV_PALETTE_XPixelToPalette
)
719 max
= alloc_system_colors
- (256 - (COLOR_gapEnd
- COLOR_gapStart
));
720 for ( i
= 0, idx
= COLOR_gapStart
; i
< 256 && idx
<= COLOR_gapEnd
; i
++ )
721 if( X11DRV_PALETTE_XPixelToPalette
[i
] == NB_PALETTE_EMPTY_VALUE
)
725 XQueryColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &xc
);
726 r
= xc
.red
>>8; g
= xc
.green
>>8; b
= xc
.blue
>>8;
728 if( xc
.pixel
< 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template
, RGB(r
, g
, b
)) &&
729 XAllocColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &xc
) )
731 X11DRV_PALETTE_XPixelToPalette
[xc
.pixel
] = idx
;
732 X11DRV_PALETTE_PaletteToXPixel
[idx
] = xc
.pixel
;
733 *(COLORREF
*)(COLOR_sysPal
+ idx
) = RGB(r
, g
, b
);
734 COLOR_sysPal
[idx
++].peFlags
|= PC_SYS_USED
;
735 if( --max
<= 0 ) break;
738 COLOR_gapFilled
= idx
- COLOR_gapStart
;
744 /***********************************************************************
745 * X11DRV_IsSolidColor
747 * Check whether 'color' can be represented with a solid color.
749 BOOL
X11DRV_IsSolidColor( COLORREF color
)
752 const PALETTEENTRY
*pEntry
= COLOR_sysPal
;
754 if (color
& 0xff000000) return TRUE
; /* indexed color */
756 if (!color
|| (color
== 0xffffff)) return TRUE
; /* black or white */
758 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
) return TRUE
; /* no palette */
760 EnterCriticalSection( &palette_cs
);
761 for (i
= 0; i
< palette_size
; i
++, pEntry
++)
763 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
764 if ((GetRValue(color
) == pEntry
->peRed
) &&
765 (GetGValue(color
) == pEntry
->peGreen
) &&
766 (GetBValue(color
) == pEntry
->peBlue
))
768 LeaveCriticalSection( &palette_cs
);
772 LeaveCriticalSection( &palette_cs
);
777 /***********************************************************************
778 * X11DRV_PALETTE_ToLogical
780 * Return RGB color for given X pixel.
782 COLORREF
X11DRV_PALETTE_ToLogical(X11DRV_PDEVICE
*physDev
, int pixel
)
787 /* truecolor visual */
789 if (screen_depth
>= 24) return pixel
;
792 /* check for hicolor visuals first */
794 if ( (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) && !X11DRV_PALETTE_Graymax
)
796 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
798 if(physDev
->color_shifts
)
799 shifts
= physDev
->color_shifts
;
801 color
.red
= (pixel
>> shifts
->logicalRed
.shift
) & shifts
->logicalRed
.max
;
802 if (shifts
->logicalRed
.scale
<8)
803 color
.red
= color
.red
<< (8-shifts
->logicalRed
.scale
) |
804 color
.red
>> (2*shifts
->logicalRed
.scale
-8);
805 color
.green
= (pixel
>> shifts
->logicalGreen
.shift
) & shifts
->logicalGreen
.max
;
806 if (shifts
->logicalGreen
.scale
<8)
807 color
.green
=color
.green
<< (8-shifts
->logicalGreen
.scale
) |
808 color
.green
>> (2*shifts
->logicalGreen
.scale
-8);
809 color
.blue
= (pixel
>> shifts
->logicalBlue
.shift
) & shifts
->logicalBlue
.max
;
810 if (shifts
->logicalBlue
.scale
<8)
811 color
.blue
= color
.blue
<< (8-shifts
->logicalBlue
.scale
) |
812 color
.blue
>> (2*shifts
->logicalBlue
.scale
-8);
813 return RGB(color
.red
,color
.green
,color
.blue
);
816 /* check if we can bypass X */
818 if ((screen_depth
<= 8) && (pixel
< 256) &&
819 !(X11DRV_PALETTE_PaletteFlags
& (X11DRV_PALETTE_VIRTUAL
| X11DRV_PALETTE_FIXED
)) ) {
821 EnterCriticalSection( &palette_cs
);
822 ret
= *(COLORREF
*)(COLOR_sysPal
+ (X11DRV_PALETTE_XPixelToPalette
? X11DRV_PALETTE_XPixelToPalette
[pixel
]: pixel
)) & 0x00ffffff;
823 LeaveCriticalSection( &palette_cs
);
828 XQueryColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
829 return RGB(color
.red
>> 8, color
.green
>> 8, color
.blue
>> 8);
833 /***********************************************************************
834 * X11DRV_SysPaletteLookupPixel
836 static int X11DRV_SysPaletteLookupPixel( COLORREF col
, BOOL skipReserved
)
838 int i
, best
= 0, diff
= 0x7fffffff;
841 for( i
= 0; i
< palette_size
&& diff
; i
++ )
843 if( !(COLOR_sysPal
[i
].peFlags
& PC_SYS_USED
) ||
844 (skipReserved
&& COLOR_sysPal
[i
].peFlags
& PC_SYS_RESERVED
) )
847 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
848 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
849 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
853 if( r
< diff
) { best
= i
; diff
= r
; }
859 static inline BOOL
colour_is_brighter(RGBQUAD c1
, RGBQUAD c2
)
861 return (c1
.rgbRed
* c1
.rgbRed
+ c1
.rgbGreen
* c1
.rgbGreen
+ c1
.rgbBlue
* c1
.rgbBlue
) >
862 (c2
.rgbRed
* c2
.rgbRed
+ c2
.rgbGreen
* c2
.rgbGreen
+ c2
.rgbBlue
* c2
.rgbBlue
);
865 /***********************************************************************
866 * X11DRV_PALETTE_GetColor
868 * Resolve PALETTEINDEX/PALETTERGB/DIBINDEX COLORREFs to an RGB COLORREF.
870 COLORREF
X11DRV_PALETTE_GetColor( X11DRV_PDEVICE
*physDev
, COLORREF color
)
872 HPALETTE hPal
= GetCurrentObject(physDev
->dev
.hdc
, OBJ_PAL
);
875 if (color
& (1 << 24)) /* PALETTEINDEX */
877 unsigned int idx
= LOWORD(color
);
878 if (!GetPaletteEntries( hPal
, idx
, 1, &entry
)) return 0;
879 return RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
882 if (color
>> 24 == 2) /* PALETTERGB */
884 unsigned int idx
= GetNearestPaletteIndex( hPal
, color
& 0xffffff );
885 if (!GetPaletteEntries( hPal
, idx
, 1, &entry
)) return 0;
886 return RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
889 if (color
>> 16 == 0x10ff) /* DIBINDEX */
892 return color
& 0xffffff;
895 /***********************************************************************
896 * X11DRV_PALETTE_ToPhysical
898 * Return the physical color closest to 'color'.
900 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE
*physDev
, COLORREF color
)
903 HPALETTE hPal
= GetCurrentObject(physDev
->dev
.hdc
, OBJ_PAL
);
904 int *mapping
= palette_get_mapping( hPal
);
906 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
908 if(physDev
->color_shifts
)
909 shifts
= physDev
->color_shifts
;
911 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
913 /* there is no colormap limitation; we are going to have to compute
914 * the pixel value from the visual information stored earlier
916 unsigned long red
, green
, blue
;
918 if (color
& (1 << 24)) /* PALETTEINDEX */
920 unsigned int idx
= LOWORD( color
);
922 if (!GetPaletteEntries( hPal
, idx
, 1, &entry
))
924 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color
, idx
);
927 if (mapping
) return mapping
[idx
];
929 green
= entry
.peGreen
;
932 else if (color
>> 16 == 0x10ff) /* DIBINDEX */
938 if (physDev
->depth
== 1)
939 return (((color
>> 16) & 0xff) +
940 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
941 red
= GetRValue( color
);
942 green
= GetGValue( color
);
943 blue
= GetBValue( color
);
946 if (X11DRV_PALETTE_Graymax
)
948 /* grayscale only; return scaled value */
949 return ( (red
* 30 + green
* 59 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
953 /* scale each individually and construct the TrueColor pixel value */
954 if (shifts
->physicalRed
.scale
< 8)
955 red
= red
>> (8-shifts
->physicalRed
.scale
);
956 else if (shifts
->physicalRed
.scale
> 8)
957 red
= red
<< (shifts
->physicalRed
.scale
-8) |
958 red
>> (16-shifts
->physicalRed
.scale
);
959 if (shifts
->physicalGreen
.scale
< 8)
960 green
= green
>> (8-shifts
->physicalGreen
.scale
);
961 else if (shifts
->physicalGreen
.scale
> 8)
962 green
= green
<< (shifts
->physicalGreen
.scale
-8) |
963 green
>> (16-shifts
->physicalGreen
.scale
);
964 if (shifts
->physicalBlue
.scale
< 8)
965 blue
= blue
>> (8-shifts
->physicalBlue
.scale
);
966 else if (shifts
->physicalBlue
.scale
> 8)
967 blue
= blue
<< (shifts
->physicalBlue
.scale
-8) |
968 blue
>> (16-shifts
->physicalBlue
.scale
);
970 return (red
<< shifts
->physicalRed
.shift
) | (green
<< shifts
->physicalGreen
.shift
) | (blue
<< shifts
->physicalBlue
.shift
);
976 WARN("Palette %p is not realized\n", hPal
);
978 if (color
& (1 << 24)) /* PALETTEINDEX */
980 index
= LOWORD( color
);
981 if (!GetPaletteEntries( hPal
, index
, 1, &entry
))
982 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color
, index
);
983 else if (mapping
) index
= mapping
[index
];
985 else if (color
>> 24 == 2) /* PALETTERGB */
987 index
= GetNearestPaletteIndex( hPal
, color
);
988 if (mapping
) index
= mapping
[index
];
990 else if (color
>> 16 == 0x10ff) /* DIBINDEX */
996 if (physDev
->depth
== 1)
997 return (((color
>> 16) & 0xff) +
998 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
1000 EnterCriticalSection( &palette_cs
);
1001 index
= X11DRV_SysPaletteLookupPixel( color
& 0xffffff, FALSE
);
1002 if (X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1003 LeaveCriticalSection( &palette_cs
);
1009 /***********************************************************************
1010 * X11DRV_PALETTE_LookupPixel
1012 int X11DRV_PALETTE_LookupPixel(ColorShifts
*shifts
, COLORREF color
)
1014 unsigned char spec_type
= color
>> 24;
1016 /* Only accept RGB which has spec_type = 0 */
1022 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
1024 unsigned long red
, green
, blue
;
1025 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
1027 if (X11DRV_PALETTE_Graymax
)
1029 /* grayscale only; return scaled value */
1030 return ( (red
* 30 + green
* 59 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
1034 /* No shifts are set in case of 1-bit */
1035 if(!shifts
) shifts
= &X11DRV_PALETTE_default_shifts
;
1037 /* scale each individually and construct the TrueColor pixel value */
1038 if (shifts
->physicalRed
.scale
< 8)
1039 red
= red
>> (8-shifts
->physicalRed
.scale
);
1040 else if (shifts
->physicalRed
.scale
> 8)
1041 red
= red
<< (shifts
->physicalRed
.scale
-8) |
1042 red
>> (16-shifts
->physicalRed
.scale
);
1043 if (shifts
->physicalGreen
.scale
< 8)
1044 green
= green
>> (8-shifts
->physicalGreen
.scale
);
1045 else if (shifts
->physicalGreen
.scale
> 8)
1046 green
= green
<< (shifts
->physicalGreen
.scale
-8) |
1047 green
>> (16-shifts
->physicalGreen
.scale
);
1048 if (shifts
->physicalBlue
.scale
< 8)
1049 blue
= blue
>> (8-shifts
->physicalBlue
.scale
);
1050 else if (shifts
->physicalBlue
.scale
> 8)
1051 blue
= blue
<< (shifts
->physicalBlue
.scale
-8) |
1052 blue
>> (16-shifts
->physicalBlue
.scale
);
1054 return (red
<< shifts
->physicalRed
.shift
) | (green
<< shifts
->physicalGreen
.shift
) | (blue
<< shifts
->physicalBlue
.shift
);
1060 HPALETTE hPal
= GetStockObject(DEFAULT_PALETTE
);
1061 int *mapping
= palette_get_mapping( hPal
);
1064 WARN("Palette %p is not realized\n", hPal
);
1066 EnterCriticalSection( &palette_cs
);
1067 index
= X11DRV_SysPaletteLookupPixel( color
, FALSE
);
1068 if (X11DRV_PALETTE_PaletteToXPixel
)
1069 index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1070 LeaveCriticalSection( &palette_cs
);
1076 /***********************************************************************
1077 * X11DRV_PALETTE_LookupSystemXPixel
1079 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
)
1081 int i
, best
= 0, diff
= 0x7fffffff;
1082 int size
= palette_size
;
1085 for( i
= 0; i
< size
&& diff
; i
++ )
1087 if( i
== NB_RESERVED_COLORS
/2 )
1089 int newi
= size
- NB_RESERVED_COLORS
/2;
1093 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
1094 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
1095 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
1097 r
= r
*r
+ g
*g
+ b
*b
;
1099 if( r
< diff
) { best
= i
; diff
= r
; }
1102 return (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[best
] : best
;
1105 /***********************************************************************
1106 * X11DRV_PALETTE_FormatSystemPalette
1108 static void X11DRV_PALETTE_FormatSystemPalette(void)
1110 /* Build free list so we'd have an easy way to find
1111 * out if there are any available colorcells.
1114 int i
, j
= X11DRV_PALETTE_firstFree
= NB_RESERVED_COLORS
/2;
1116 COLOR_sysPal
[j
].peFlags
= 0;
1117 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
1118 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
1120 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
1121 X11DRV_PALETTE_freeList
[j
] = i
; /* next */
1124 X11DRV_PALETTE_freeList
[j
] = 0;
1127 /***********************************************************************
1128 * X11DRV_PALETTE_CheckSysColor
1130 static BOOL
X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY
*sys_pal_template
, COLORREF c
)
1133 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
1134 if( c
== (*(const COLORREF
*)(sys_pal_template
+ i
) & 0x00ffffff) )
1140 /***********************************************************************
1141 * X11DRV_LookupSysPaletteExact
1143 static int X11DRV_LookupSysPaletteExact( BYTE r
, BYTE g
, BYTE b
)
1146 for( i
= 0; i
< palette_size
; i
++ )
1148 if( COLOR_sysPal
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
1149 if( COLOR_sysPal
[i
].peRed
== r
&&
1150 COLOR_sysPal
[i
].peGreen
== g
&&
1151 COLOR_sysPal
[i
].peBlue
== b
)
1158 /***********************************************************************
1159 * RealizePalette (X11DRV.@)
1161 UINT
X11DRV_RealizePalette( PHYSDEV dev
, HPALETTE hpal
, BOOL primary
)
1163 X11DRV_PDEVICE
*physDev
= get_x11drv_dev( dev
);
1166 UINT i
, iRemapped
= 0;
1167 int *prev_mapping
, *mapping
;
1168 PALETTEENTRY entries
[256];
1171 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
) return 0;
1173 if (!GetObjectW( hpal
, sizeof(num_entries
), &num_entries
)) return 0;
1175 /* initialize palette mapping table */
1176 prev_mapping
= palette_get_mapping( hpal
);
1178 mapping
= HeapReAlloc( GetProcessHeap(), 0, prev_mapping
, sizeof(int)*num_entries
);
1180 mapping
= HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries
);
1182 if(mapping
== NULL
) {
1183 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1186 palette_set_mapping( hpal
, mapping
);
1188 if (num_entries
> 256)
1190 FIXME( "more than 256 entries not supported\n" );
1193 if (!(num_entries
= GetPaletteEntries( hpal
, 0, num_entries
, entries
))) return 0;
1195 /* reset dynamic system palette entries */
1197 EnterCriticalSection( &palette_cs
);
1198 if( primary
&& X11DRV_PALETTE_firstFree
!= -1)
1199 X11DRV_PALETTE_FormatSystemPalette();
1201 for (i
= 0; i
< num_entries
; i
++)
1206 /* Even though the docs say that only one flag is to be set,
1207 * they are a bitmask. At least one app sets more than one at
1209 if ( entries
[i
].peFlags
& PC_EXPLICIT
) {
1210 /* palette entries are indices into system palette */
1211 index
= *(WORD
*)&entries
[i
];
1212 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
1214 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
1217 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1219 if ( entries
[i
].peFlags
& PC_RESERVED
) {
1220 /* forbid future mappings to this entry */
1221 flag
|= PC_SYS_RESERVED
;
1224 if (! (entries
[i
].peFlags
& PC_NOCOLLAPSE
) ) {
1225 /* try to collapse identical colors */
1226 index
= X11DRV_LookupSysPaletteExact( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
);
1231 if( X11DRV_PALETTE_firstFree
> 0 )
1234 index
= X11DRV_PALETTE_firstFree
; /* ought to be available */
1235 X11DRV_PALETTE_firstFree
= X11DRV_PALETTE_freeList
[index
];
1237 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
) ? X11DRV_PALETTE_PaletteToXPixel
[index
] : index
;
1238 color
.red
= entries
[i
].peRed
<< 8;
1239 color
.green
= entries
[i
].peGreen
<< 8;
1240 color
.blue
= entries
[i
].peBlue
<< 8;
1241 color
.flags
= DoRed
| DoGreen
| DoBlue
;
1242 XStoreColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
1244 COLOR_sysPal
[index
] = entries
[i
];
1245 COLOR_sysPal
[index
].peFlags
= flag
;
1246 X11DRV_PALETTE_freeList
[index
] = 0;
1248 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1250 else if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
1252 index
= X11DRV_PALETTE_LookupPixel( physDev
->color_shifts
, RGB( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
));
1255 /* we have to map to existing entry in the system palette */
1257 index
= X11DRV_SysPaletteLookupPixel( RGB( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
),
1261 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1264 if( !prev_mapping
|| mapping
[i
] != index
) iRemapped
++;
1267 TRACE("entry %i (%x) -> pixel %i\n", i
, *(COLORREF
*)&entries
[i
], index
);
1270 LeaveCriticalSection( &palette_cs
);
1275 /***********************************************************************
1276 * UnrealizePalette (X11DRV.@)
1278 BOOL
X11DRV_UnrealizePalette( HPALETTE hpal
)
1280 int *mapping
= palette_get_mapping( hpal
);
1284 XDeleteContext( gdi_display
, (XID
)hpal
, palette_context
);
1285 HeapFree( GetProcessHeap(), 0, mapping
);
1291 /***********************************************************************
1292 * GetSystemPaletteEntries (X11DRV.@)
1294 UINT
X11DRV_GetSystemPaletteEntries( PHYSDEV dev
, UINT start
, UINT count
, LPPALETTEENTRY entries
)
1298 if (!entries
) return palette_size
;
1299 if (start
>= palette_size
) return 0;
1300 if (start
+ count
>= palette_size
) count
= palette_size
- start
;
1302 EnterCriticalSection( &palette_cs
);
1303 for (i
= 0; i
< count
; i
++)
1305 entries
[i
].peRed
= COLOR_sysPal
[start
+ i
].peRed
;
1306 entries
[i
].peGreen
= COLOR_sysPal
[start
+ i
].peGreen
;
1307 entries
[i
].peBlue
= COLOR_sysPal
[start
+ i
].peBlue
;
1308 entries
[i
].peFlags
= 0;
1309 TRACE("\tidx(%02x) -> RGB(%08x)\n", start
+ i
, *(COLORREF
*)(entries
+ i
) );
1311 LeaveCriticalSection( &palette_cs
);
1316 /***********************************************************************
1317 * GetNearestColor (X11DRV.@)
1319 COLORREF
X11DRV_GetNearestColor( PHYSDEV dev
, COLORREF color
)
1321 unsigned char spec_type
= color
>> 24;
1324 if (!palette_size
) return color
;
1326 if (spec_type
== 1 || spec_type
== 2)
1328 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1332 HPALETTE hpal
= GetCurrentObject( dev
->hdc
, OBJ_PAL
);
1334 if (!hpal
) hpal
= GetStockObject( DEFAULT_PALETTE
);
1336 if (spec_type
== 2) /* PALETTERGB */
1337 index
= GetNearestPaletteIndex( hpal
, color
);
1338 else /* PALETTEINDEX */
1339 index
= LOWORD(color
);
1341 if (!GetPaletteEntries( hpal
, index
, 1, &entry
))
1343 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color
, index
);
1344 if (!GetPaletteEntries( hpal
, 0, 1, &entry
)) return CLR_INVALID
;
1346 color
= RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
1348 color
&= 0x00ffffff;
1349 EnterCriticalSection( &palette_cs
);
1350 nearest
= (0x00ffffff & *(COLORREF
*)(COLOR_sysPal
+ X11DRV_SysPaletteLookupPixel(color
, FALSE
)));
1351 LeaveCriticalSection( &palette_cs
);
1353 TRACE("(%06x): returning %06x\n", color
, nearest
);
1358 /***********************************************************************
1359 * RealizeDefaultPalette (X11DRV.@)
1361 UINT
X11DRV_RealizeDefaultPalette( PHYSDEV dev
)
1365 if (palette_size
&& GetObjectType(dev
->hdc
) != OBJ_MEMDC
)
1367 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1368 int i
, index
, *mapping
= palette_get_mapping( GetStockObject(DEFAULT_PALETTE
) );
1369 PALETTEENTRY entries
[NB_RESERVED_COLORS
];
1371 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE
), 0, NB_RESERVED_COLORS
, entries
);
1372 EnterCriticalSection( &palette_cs
);
1373 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
1375 index
= X11DRV_PALETTE_LookupSystemXPixel( RGB(entries
[i
].peRed
,
1377 entries
[i
].peBlue
) );
1378 /* mapping is allocated in COLOR_InitPalette() */
1379 if( index
!= mapping
[i
] )
1385 LeaveCriticalSection( &palette_cs
);