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
)
118 if (XFindContext( gdi_display
, (XID
)hpal
, palette_context
, (char **)&mapping
)) mapping
= NULL
;
124 /***********************************************************************
125 * palette_set_mapping
127 static void palette_set_mapping( HPALETTE hpal
, int *mapping
)
130 XSaveContext( gdi_display
, (XID
)hpal
, palette_context
, (char *)mapping
);
135 /***********************************************************************
138 * Initialize color management.
140 int X11DRV_PALETTE_Init(void)
142 int mask
, white
, black
;
145 PALETTEENTRY sys_pal_template
[NB_RESERVED_COLORS
];
147 TRACE("initializing palette manager...\n");
150 palette_context
= XUniqueContext();
152 white
= WhitePixel( gdi_display
, DefaultScreen(gdi_display
) );
153 black
= BlackPixel( gdi_display
, DefaultScreen(gdi_display
) );
155 for( mask
= 1; !((white
& mask
)^(black
& mask
)); mask
<<= 1 )
157 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
158 palette_size
= visual
->map_entries
;
160 switch(visual
->class)
163 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
167 if (private_color_map
)
169 XSetWindowAttributes win_attr
;
171 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap( gdi_display
, root_window
,
173 if (X11DRV_PALETTE_PaletteXColormap
)
175 X11DRV_PALETTE_PaletteFlags
|= (X11DRV_PALETTE_PRIVATE
| X11DRV_PALETTE_WHITESET
);
178 for( white
= palette_size
- 1; !(white
& 1); white
>>= 1 )
181 if( root_window
!= DefaultRootWindow(gdi_display
) )
183 win_attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
;
184 XChangeWindowAttributes( gdi_display
, root_window
, CWColormap
, &win_attr
);
188 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
196 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
198 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
199 X11DRV_PALETTE_Graymax
= (1 << screen_depth
)-1;
204 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
206 int *depths
,nrofdepths
;
207 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
211 depths
= XListDepths(gdi_display
,DefaultScreen(gdi_display
),&nrofdepths
);
212 if ((nrofdepths
==2) && ((depths
[0]==4) || depths
[1]==4)) {
214 for( white
= palette_size
- 1; !(white
& 1); white
>>= 1 )
216 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
217 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
222 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
224 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
225 X11DRV_PALETTE_ComputeColorShifts(&X11DRV_PALETTE_default_shifts
, visual
->red_mask
, visual
->green_mask
, visual
->blue_mask
);
233 TRACE(" visual class %i (%i)\n", visual
->class, monoPlane
);
235 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE
), 0, NB_RESERVED_COLORS
, sys_pal_template
);
237 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
243 if ((mapping
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(int) * NB_RESERVED_COLORS
)))
244 palette_set_mapping( GetStockObject(DEFAULT_PALETTE
), mapping
);
246 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
247 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template
);
249 X11DRV_PALETTE_BuildSharedMap( sys_pal_template
);
251 /* Build free list */
253 if( X11DRV_PALETTE_firstFree
!= -1 )
254 X11DRV_PALETTE_FormatSystemPalette();
256 X11DRV_PALETTE_FillDefaultColors( sys_pal_template
);
257 palette_size
= visual
->map_entries
;
263 /***********************************************************************
264 * X11DRV_PALETTE_Cleanup
266 * Free external colors we grabbed in the FillDefaultPalette()
268 void X11DRV_PALETTE_Cleanup(void)
270 if( COLOR_gapFilled
)
273 XFreeColors(gdi_display
, X11DRV_PALETTE_PaletteXColormap
,
274 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel
+ COLOR_gapStart
),
280 /***********************************************************************
281 * X11DRV_PALETTE_ComputeChannelShift
283 * Calculate conversion parameters for a given color mask
285 static void X11DRV_PALETTE_ComputeChannelShift(unsigned long maskbits
, ChannelShift
*physical
, ChannelShift
*to_logical
)
300 for(i
=0;!(maskbits
&1);i
++)
304 physical
->max
= maskbits
;
306 for(i
=0;maskbits
!=0;i
++)
310 if (physical
->scale
>8)
312 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
313 * So we adjust the shifts to also normalize the color fields to
314 * the Win32 standard of 8 bits per color.
316 to_logical
->shift
=physical
->shift
+(physical
->scale
-8);
318 to_logical
->max
=0xff;
320 to_logical
->shift
=physical
->shift
;
321 to_logical
->scale
=physical
->scale
;
322 to_logical
->max
=physical
->max
;
326 /***********************************************************************
327 * X11DRV_PALETTE_ComputeColorShifts
329 * Calculate conversion parameters for a given color
331 void X11DRV_PALETTE_ComputeColorShifts(ColorShifts
*shifts
, unsigned long redMask
, unsigned long greenMask
, unsigned long blueMask
)
333 X11DRV_PALETTE_ComputeChannelShift(redMask
, &shifts
->physicalRed
, &shifts
->logicalRed
);
334 X11DRV_PALETTE_ComputeChannelShift(greenMask
, &shifts
->physicalGreen
, &shifts
->logicalGreen
);
335 X11DRV_PALETTE_ComputeChannelShift(blueMask
, &shifts
->physicalBlue
, &shifts
->logicalBlue
);
338 /***********************************************************************
339 * X11DRV_PALETTE_BuildPrivateMap
341 * Allocate colorcells and initialize mapping tables.
343 static BOOL
X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY
*sys_pal_template
)
345 /* Private colormap - identity mapping */
350 if((COLOR_sysPal
= HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY
)*palette_size
)) == NULL
) {
351 WARN("Unable to allocate the system palette\n");
355 TRACE("Building private map - %i palette entries\n", palette_size
);
357 /* Allocate system palette colors */
360 for( i
=0; i
< palette_size
; i
++ )
362 if( i
< NB_RESERVED_COLORS
/2 )
364 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
365 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
366 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
367 COLOR_sysPal
[i
] = sys_pal_template
[i
];
368 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
370 else if( i
>= palette_size
- NB_RESERVED_COLORS
/2 )
372 int j
= NB_RESERVED_COLORS
+ i
- palette_size
;
373 color
.red
= sys_pal_template
[j
].peRed
* 65535 / 255;
374 color
.green
= sys_pal_template
[j
].peGreen
* 65535 / 255;
375 color
.blue
= sys_pal_template
[j
].peBlue
* 65535 / 255;
376 COLOR_sysPal
[i
] = sys_pal_template
[j
];
377 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
380 color
.flags
= DoRed
| DoGreen
| DoBlue
;
382 XStoreColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
384 /* Set EGA mapping if color is from the first or last eight */
387 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
388 else if (i
>= palette_size
- 8 )
389 X11DRV_PALETTE_mapEGAPixel
[i
- (palette_size
- 16)] = color
.pixel
;
393 X11DRV_PALETTE_XPixelToPalette
= X11DRV_PALETTE_PaletteToXPixel
= NULL
;
395 COLOR_gapStart
= 256; COLOR_gapEnd
= -1;
397 X11DRV_PALETTE_firstFree
= (palette_size
> NB_RESERVED_COLORS
)?NB_RESERVED_COLORS
/2 : -1;
402 /***********************************************************************
403 * X11DRV_PALETTE_BuildSharedMap
405 * Allocate colorcells and initialize mapping tables.
407 static BOOL
X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY
*sys_pal_template
)
410 unsigned long sysPixel
[NB_RESERVED_COLORS
];
411 unsigned long* pixDynMapping
= NULL
;
412 unsigned long plane_masks
[1];
414 int diff
, r
, g
, b
, bp
= 0, wp
= 1;
416 unsigned int max
= 256;
418 XColor defaultColors
[256];
420 /* Copy the first bunch of colors out of the default colormap to prevent
421 * colormap flashing as much as possible. We're likely to get the most
422 * important Window Manager colors, etc in the first 128 colors */
423 defaultCM
= DefaultColormap( gdi_display
, DefaultScreen(gdi_display
) );
425 if (copy_default_colors
> 256) copy_default_colors
= 256;
426 for (i
= 0; i
< copy_default_colors
; i
++)
427 defaultColors
[i
].pixel
= (long) i
;
429 XQueryColors(gdi_display
, defaultCM
, &defaultColors
[0], copy_default_colors
);
430 for (i
= 0; i
< copy_default_colors
; i
++)
431 XAllocColor( gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &defaultColors
[i
] );
433 if (alloc_system_colors
> 256) alloc_system_colors
= 256;
434 else if (alloc_system_colors
< 20) alloc_system_colors
= 20;
435 TRACE("%d colors configured.\n", alloc_system_colors
);
437 TRACE("Building shared map - %i palette entries\n", palette_size
);
439 /* Be nice and allocate system colors as read-only */
441 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
443 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
444 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
445 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
446 color
.flags
= DoRed
| DoGreen
| DoBlue
;
448 if (!XAllocColor( gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
))
454 WARN("Not enough colors for the full system palette.\n");
456 bp
= BlackPixel(gdi_display
, DefaultScreen(gdi_display
));
457 wp
= WhitePixel(gdi_display
, DefaultScreen(gdi_display
));
459 max
= (0xffffffff)>>(32 - screen_depth
);
467 /* reinit color (XAllocColor() may change it)
468 * and map to the best shared colorcell */
470 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
471 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
472 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
474 best
.pixel
= best
.red
= best
.green
= best
.blue
= 0;
475 for( c
.pixel
= 0, diff
= 0x7fffffff; c
.pixel
< max
; c
.pixel
+= step
)
477 XQueryColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &c
);
478 r
= (c
.red
- color
.red
)>>8;
479 g
= (c
.green
- color
.green
)>>8;
480 b
= (c
.blue
- color
.blue
)>>8;
482 if( r
< diff
) { best
= c
; diff
= r
; }
485 if( XAllocColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &best
) )
486 color
.pixel
= best
.pixel
;
487 else color
.pixel
= (i
< NB_RESERVED_COLORS
/2)? bp
: wp
;
490 sysPixel
[i
] = color
.pixel
;
492 TRACE("syscolor(%x) -> pixel %i\n", *(const COLORREF
*)(sys_pal_template
+i
),
495 /* Set EGA mapping if color in the first or last eight */
498 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
499 else if (i
>= NB_RESERVED_COLORS
- 8 )
500 X11DRV_PALETTE_mapEGAPixel
[i
- (NB_RESERVED_COLORS
-16)] = color
.pixel
;
504 /* now allocate changeable set */
506 if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
508 int c_min
= 0, c_max
= palette_size
, c_val
;
510 TRACE("Dynamic colormap...\n");
512 /* let's become the first client that actually follows
513 * X guidelines and does binary search...
516 if((pixDynMapping
= HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size
)) == NULL
) {
517 WARN("Out of memory while building system palette.\n");
522 /* comment this out if you want to debug palette init */
523 XGrabServer(gdi_display
);
525 while( c_max
- c_min
> 0 )
527 c_val
= (c_max
+ c_min
)/2 + (c_max
+ c_min
)%2;
529 if( !XAllocColorCells(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, False
,
530 plane_masks
, 0, pixDynMapping
, c_val
) )
534 XFreeColors(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, pixDynMapping
, c_val
, 0);
539 if( c_min
> alloc_system_colors
- NB_RESERVED_COLORS
)
540 c_min
= alloc_system_colors
- NB_RESERVED_COLORS
;
542 c_min
= (c_min
/2) + (c_min
/2); /* need even set for split palette */
545 if( !XAllocColorCells(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, False
,
546 plane_masks
, 0, pixDynMapping
, c_min
) )
548 WARN("Inexplicable failure during colorcell allocation.\n");
552 palette_size
= c_min
+ NB_RESERVED_COLORS
;
554 XUngrabServer(gdi_display
);
557 TRACE("adjusted size %i colorcells\n", palette_size
);
559 else if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
561 /* virtual colorspace - ToPhysical takes care of
562 * color translations but we have to allocate full palette
563 * to maintain compatibility
566 TRACE("Virtual colorspace - screendepth %i\n", screen_depth
);
568 else palette_size
= NB_RESERVED_COLORS
; /* system palette only - however we can alloc a bunch
569 * of colors and map to them */
571 TRACE("Shared system palette uses %i colors.\n", palette_size
);
573 /* set gap to account for pixel shortage. It has to be right in the center
574 * of the system palette because otherwise raster ops get screwed. */
576 if( palette_size
>= 256 )
577 { COLOR_gapStart
= 256; COLOR_gapEnd
= -1; }
579 { COLOR_gapStart
= palette_size
/2; COLOR_gapEnd
= 255 - palette_size
/2; }
581 X11DRV_PALETTE_firstFree
= ( palette_size
> NB_RESERVED_COLORS
&&
582 (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
|| !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)) )
583 ? NB_RESERVED_COLORS
/2 : -1;
585 COLOR_sysPal
= HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY
)*256);
586 if(COLOR_sysPal
== NULL
) {
587 ERR("Unable to allocate the system palette!\n");
588 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
592 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
594 if (screen_depth
<= 8)
596 X11DRV_PALETTE_XPixelToPalette
= HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
597 if(X11DRV_PALETTE_XPixelToPalette
== NULL
) {
598 ERR("Out of memory: XPixelToPalette!\n");
599 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
602 for( i
= 0; i
< 256; i
++ )
603 X11DRV_PALETTE_XPixelToPalette
[i
] = NB_PALETTE_EMPTY_VALUE
;
606 /* for hicolor visuals PaletteToPixel mapping is used to skip
607 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
610 X11DRV_PALETTE_PaletteToXPixel
= HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
611 if(X11DRV_PALETTE_PaletteToXPixel
== NULL
) {
612 ERR("Out of memory: PaletteToXPixel!\n");
613 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
617 for( i
= j
= 0; i
< 256; i
++ )
619 if( i
>= COLOR_gapStart
&& i
<= COLOR_gapEnd
)
621 X11DRV_PALETTE_PaletteToXPixel
[i
] = NB_PALETTE_EMPTY_VALUE
;
622 COLOR_sysPal
[i
].peFlags
= 0; /* mark as unused */
626 if( i
< NB_RESERVED_COLORS
/2 )
628 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[i
];
629 COLOR_sysPal
[i
] = sys_pal_template
[i
];
630 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
632 else if( i
>= 256 - NB_RESERVED_COLORS
/2 )
634 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[(i
+ NB_RESERVED_COLORS
) - 256];
635 COLOR_sysPal
[i
] = sys_pal_template
[(i
+ NB_RESERVED_COLORS
) - 256];
636 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
638 else if( pixDynMapping
)
639 X11DRV_PALETTE_PaletteToXPixel
[i
] = pixDynMapping
[j
++];
641 X11DRV_PALETTE_PaletteToXPixel
[i
] = i
;
643 TRACE("index %i -> pixel %i\n", i
, X11DRV_PALETTE_PaletteToXPixel
[i
]);
645 if( X11DRV_PALETTE_XPixelToPalette
)
646 X11DRV_PALETTE_XPixelToPalette
[X11DRV_PALETTE_PaletteToXPixel
[i
]] = i
;
649 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
654 /***********************************************************************
655 * Colormap Initialization
657 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY
*sys_pal_template
)
659 /* initialize unused entries to what Windows uses as a color
660 * cube - based on Greg Kreider's code.
664 int red
, no_r
, inc_r
;
665 int green
, no_g
, inc_g
;
666 int blue
, no_b
, inc_b
;
668 if (palette_size
<= NB_RESERVED_COLORS
)
670 while (i
*i
*i
< (palette_size
- NB_RESERVED_COLORS
)) i
++;
671 no_r
= no_g
= no_b
= --i
;
672 if ((no_r
* (no_g
+1) * no_b
) < (palette_size
- NB_RESERVED_COLORS
)) no_g
++;
673 if ((no_r
* no_g
* (no_b
+1)) < (palette_size
- NB_RESERVED_COLORS
)) no_b
++;
674 inc_r
= (255 - NB_COLORCUBE_START_INDEX
)/no_r
;
675 inc_g
= (255 - NB_COLORCUBE_START_INDEX
)/no_g
;
676 inc_b
= (255 - NB_COLORCUBE_START_INDEX
)/no_b
;
680 idx
= X11DRV_PALETTE_firstFree
;
683 for (blue
= NB_COLORCUBE_START_INDEX
; blue
< 256 && idx
; blue
+= inc_b
)
684 for (green
= NB_COLORCUBE_START_INDEX
; green
< 256 && idx
; green
+= inc_g
)
685 for (red
= NB_COLORCUBE_START_INDEX
; red
< 256 && idx
; red
+= inc_r
)
689 if( red
== NB_COLORCUBE_START_INDEX
&& green
== red
&& blue
== green
) continue;
691 COLOR_sysPal
[idx
].peRed
= red
;
692 COLOR_sysPal
[idx
].peGreen
= green
;
693 COLOR_sysPal
[idx
].peBlue
= blue
;
697 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
699 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
700 if (shifts
->physicalRed
.max
!= 255) no_r
= (red
* shifts
->physicalRed
.max
) / 255;
701 if (shifts
->physicalGreen
.max
!= 255) no_g
= (green
* shifts
->physicalGreen
.max
) / 255;
702 if (shifts
->physicalBlue
.max
!= 255) no_b
= (blue
* shifts
->physicalBlue
.max
) / 255;
704 X11DRV_PALETTE_PaletteToXPixel
[idx
] = (no_r
<< shifts
->physicalRed
.shift
) | (no_g
<< shifts
->physicalGreen
.shift
) | (no_b
<< shifts
->physicalBlue
.shift
);
706 else if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
709 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[idx
] : idx
;
710 color
.red
= COLOR_sysPal
[idx
].peRed
<< 8;
711 color
.green
= COLOR_sysPal
[idx
].peGreen
<< 8;
712 color
.blue
= COLOR_sysPal
[idx
].peBlue
<< 8;
713 color
.flags
= DoRed
| DoGreen
| DoBlue
;
714 XStoreColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
716 idx
= X11DRV_PALETTE_freeList
[idx
];
719 /* try to fill some entries in the "gap" with
720 * what's already in the colormap - they will be
721 * mappable to but not changeable. */
723 if( COLOR_gapStart
< COLOR_gapEnd
&& X11DRV_PALETTE_XPixelToPalette
)
728 max
= alloc_system_colors
- (256 - (COLOR_gapEnd
- COLOR_gapStart
));
729 for ( i
= 0, idx
= COLOR_gapStart
; i
< 256 && idx
<= COLOR_gapEnd
; i
++ )
730 if( X11DRV_PALETTE_XPixelToPalette
[i
] == NB_PALETTE_EMPTY_VALUE
)
734 XQueryColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &xc
);
735 r
= xc
.red
>>8; g
= xc
.green
>>8; b
= xc
.blue
>>8;
737 if( xc
.pixel
< 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template
, RGB(r
, g
, b
)) &&
738 XAllocColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &xc
) )
740 X11DRV_PALETTE_XPixelToPalette
[xc
.pixel
] = idx
;
741 X11DRV_PALETTE_PaletteToXPixel
[idx
] = xc
.pixel
;
742 *(COLORREF
*)(COLOR_sysPal
+ idx
) = RGB(r
, g
, b
);
743 COLOR_sysPal
[idx
++].peFlags
|= PC_SYS_USED
;
744 if( --max
<= 0 ) break;
747 COLOR_gapFilled
= idx
- COLOR_gapStart
;
753 /***********************************************************************
754 * X11DRV_IsSolidColor
756 * Check whether 'color' can be represented with a solid color.
758 BOOL
X11DRV_IsSolidColor( COLORREF color
)
761 const PALETTEENTRY
*pEntry
= COLOR_sysPal
;
763 if (color
& 0xff000000) return TRUE
; /* indexed color */
765 if (!color
|| (color
== 0xffffff)) return TRUE
; /* black or white */
767 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
) return TRUE
; /* no palette */
769 EnterCriticalSection( &palette_cs
);
770 for (i
= 0; i
< palette_size
; i
++, pEntry
++)
772 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
773 if ((GetRValue(color
) == pEntry
->peRed
) &&
774 (GetGValue(color
) == pEntry
->peGreen
) &&
775 (GetBValue(color
) == pEntry
->peBlue
))
777 LeaveCriticalSection( &palette_cs
);
781 LeaveCriticalSection( &palette_cs
);
786 /***********************************************************************
787 * X11DRV_PALETTE_ToLogical
789 * Return RGB color for given X pixel.
791 COLORREF
X11DRV_PALETTE_ToLogical(X11DRV_PDEVICE
*physDev
, int pixel
)
796 /* truecolor visual */
798 if (screen_depth
>= 24) return pixel
;
801 /* check for hicolor visuals first */
803 if ( (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) && !X11DRV_PALETTE_Graymax
)
805 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
807 if(physDev
->color_shifts
)
808 shifts
= physDev
->color_shifts
;
810 color
.red
= (pixel
>> shifts
->logicalRed
.shift
) & shifts
->logicalRed
.max
;
811 if (shifts
->logicalRed
.scale
<8)
812 color
.red
= color
.red
<< (8-shifts
->logicalRed
.scale
) |
813 color
.red
>> (2*shifts
->logicalRed
.scale
-8);
814 color
.green
= (pixel
>> shifts
->logicalGreen
.shift
) & shifts
->logicalGreen
.max
;
815 if (shifts
->logicalGreen
.scale
<8)
816 color
.green
=color
.green
<< (8-shifts
->logicalGreen
.scale
) |
817 color
.green
>> (2*shifts
->logicalGreen
.scale
-8);
818 color
.blue
= (pixel
>> shifts
->logicalBlue
.shift
) & shifts
->logicalBlue
.max
;
819 if (shifts
->logicalBlue
.scale
<8)
820 color
.blue
= color
.blue
<< (8-shifts
->logicalBlue
.scale
) |
821 color
.blue
>> (2*shifts
->logicalBlue
.scale
-8);
822 return RGB(color
.red
,color
.green
,color
.blue
);
825 /* check if we can bypass X */
827 if ((screen_depth
<= 8) && (pixel
< 256) &&
828 !(X11DRV_PALETTE_PaletteFlags
& (X11DRV_PALETTE_VIRTUAL
| X11DRV_PALETTE_FIXED
)) ) {
830 EnterCriticalSection( &palette_cs
);
831 ret
= *(COLORREF
*)(COLOR_sysPal
+ (X11DRV_PALETTE_XPixelToPalette
? X11DRV_PALETTE_XPixelToPalette
[pixel
]: pixel
)) & 0x00ffffff;
832 LeaveCriticalSection( &palette_cs
);
838 XQueryColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
840 return RGB(color
.red
>> 8, color
.green
>> 8, color
.blue
>> 8);
844 /***********************************************************************
845 * X11DRV_SysPaletteLookupPixel
847 static int X11DRV_SysPaletteLookupPixel( COLORREF col
, BOOL skipReserved
)
849 int i
, best
= 0, diff
= 0x7fffffff;
852 for( i
= 0; i
< palette_size
&& diff
; i
++ )
854 if( !(COLOR_sysPal
[i
].peFlags
& PC_SYS_USED
) ||
855 (skipReserved
&& COLOR_sysPal
[i
].peFlags
& PC_SYS_RESERVED
) )
858 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
859 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
860 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
864 if( r
< diff
) { best
= i
; diff
= r
; }
870 static inline BOOL
colour_is_brighter(RGBQUAD c1
, RGBQUAD c2
)
872 return (c1
.rgbRed
* c1
.rgbRed
+ c1
.rgbGreen
* c1
.rgbGreen
+ c1
.rgbBlue
* c1
.rgbBlue
) >
873 (c2
.rgbRed
* c2
.rgbRed
+ c2
.rgbGreen
* c2
.rgbGreen
+ c2
.rgbBlue
* c2
.rgbBlue
);
876 /***********************************************************************
877 * X11DRV_PALETTE_GetColor
879 * Resolve PALETTEINDEX/PALETTERGB/DIBINDEX COLORREFs to an RGB COLORREF.
881 COLORREF
X11DRV_PALETTE_GetColor( X11DRV_PDEVICE
*physDev
, COLORREF color
)
883 HPALETTE hPal
= GetCurrentObject(physDev
->dev
.hdc
, OBJ_PAL
);
884 unsigned char spec_type
= color
>> 24;
885 unsigned idx
= color
& 0xffff;
891 case 2: /* PALETTERGB */
892 idx
= GetNearestPaletteIndex( hPal
, color
);
893 /* fall through to PALETTEINDEX */
895 case 1: /* PALETTEINDEX */
896 if (!GetPaletteEntries( hPal
, idx
, 1, &entry
))
898 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color
, idx
);
901 return RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
903 case 0x10: /* DIBINDEX */
904 if( GetDIBColorTable( physDev
->dev
.hdc
, idx
, 1, &quad
) != 1 ) {
905 WARN("DIBINDEX(%x) : idx %d is out of bounds, assuming black\n", color
, idx
);
908 return RGB( quad
.rgbRed
, quad
.rgbGreen
, quad
.rgbBlue
);
912 /* fall through to RGB */
919 /***********************************************************************
920 * X11DRV_PALETTE_ToPhysical
922 * Return the physical color closest to 'color'.
924 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE
*physDev
, COLORREF color
)
927 HPALETTE hPal
= GetCurrentObject(physDev
->dev
.hdc
, OBJ_PAL
);
928 unsigned char spec_type
= color
>> 24;
929 int *mapping
= palette_get_mapping( hPal
);
931 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
933 if(physDev
->color_shifts
)
934 shifts
= physDev
->color_shifts
;
936 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
938 /* there is no colormap limitation; we are going to have to compute
939 * the pixel value from the visual information stored earlier
942 unsigned long red
, green
, blue
;
943 unsigned idx
= color
& 0xffff;
947 case 0x10: /* DIBINDEX */
948 color
= X11DRV_PALETTE_GetColor( physDev
, color
);
951 case 1: /* PALETTEINDEX */
952 if (!GetPaletteEntries( hPal
, idx
, 1, &entry
))
954 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color
, idx
);
957 if (mapping
) return mapping
[idx
];
958 color
= RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
963 /* fall through to RGB */
966 if (physDev
->depth
== 1)
971 if (GetDIBColorTable( physDev
->dev
.hdc
, 0, 2, table
) == 2)
973 if(!colour_is_brighter(table
[1], table
[0])) white
= 0;
975 return (((color
>> 16) & 0xff) +
976 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? white
: 1 - white
;
981 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
983 if (X11DRV_PALETTE_Graymax
)
985 /* grayscale only; return scaled value */
986 return ( (red
* 30 + green
* 59 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
990 /* scale each individually and construct the TrueColor pixel value */
991 if (shifts
->physicalRed
.scale
< 8)
992 red
= red
>> (8-shifts
->physicalRed
.scale
);
993 else if (shifts
->physicalRed
.scale
> 8)
994 red
= red
<< (shifts
->physicalRed
.scale
-8) |
995 red
>> (16-shifts
->physicalRed
.scale
);
996 if (shifts
->physicalGreen
.scale
< 8)
997 green
= green
>> (8-shifts
->physicalGreen
.scale
);
998 else if (shifts
->physicalGreen
.scale
> 8)
999 green
= green
<< (shifts
->physicalGreen
.scale
-8) |
1000 green
>> (16-shifts
->physicalGreen
.scale
);
1001 if (shifts
->physicalBlue
.scale
< 8)
1002 blue
= blue
>> (8-shifts
->physicalBlue
.scale
);
1003 else if (shifts
->physicalBlue
.scale
> 8)
1004 blue
= blue
<< (shifts
->physicalBlue
.scale
-8) |
1005 blue
>> (16-shifts
->physicalBlue
.scale
);
1007 return (red
<< shifts
->physicalRed
.shift
) | (green
<< shifts
->physicalGreen
.shift
) | (blue
<< shifts
->physicalBlue
.shift
);
1013 WARN("Palette %p is not realized\n", hPal
);
1015 switch(spec_type
) /* we have to peruse DC and system palette */
1019 /* fall through to RGB */
1022 if (physDev
->depth
== 1)
1027 if (GetDIBColorTable( physDev
->dev
.hdc
, 0, 2, table
) == 2)
1029 if(!colour_is_brighter(table
[1], table
[0]))
1032 return (((color
>> 16) & 0xff) +
1033 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? white
: 1 - white
;
1036 EnterCriticalSection( &palette_cs
);
1037 index
= X11DRV_SysPaletteLookupPixel( color
, FALSE
);
1038 if (X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1039 LeaveCriticalSection( &palette_cs
);
1041 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
1044 case 1: /* PALETTEINDEX */
1045 index
= color
& 0xffff;
1046 if (!GetPaletteEntries( hPal
, index
, 1, &entry
))
1047 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color
, index
);
1048 else if (mapping
) index
= mapping
[index
];
1050 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
1053 case 2: /* PALETTERGB */
1054 index
= GetNearestPaletteIndex( hPal
, color
);
1055 if (mapping
) index
= mapping
[index
];
1056 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
1064 /***********************************************************************
1065 * X11DRV_PALETTE_LookupPixel
1067 int X11DRV_PALETTE_LookupPixel(ColorShifts
*shifts
, COLORREF color
)
1069 unsigned char spec_type
= color
>> 24;
1071 /* Only accept RGB which has spec_type = 0 */
1077 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
1079 unsigned long red
, green
, blue
;
1080 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
1082 if (X11DRV_PALETTE_Graymax
)
1084 /* grayscale only; return scaled value */
1085 return ( (red
* 30 + green
* 59 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
1089 /* No shifts are set in case of 1-bit */
1090 if(!shifts
) shifts
= &X11DRV_PALETTE_default_shifts
;
1092 /* scale each individually and construct the TrueColor pixel value */
1093 if (shifts
->physicalRed
.scale
< 8)
1094 red
= red
>> (8-shifts
->physicalRed
.scale
);
1095 else if (shifts
->physicalRed
.scale
> 8)
1096 red
= red
<< (shifts
->physicalRed
.scale
-8) |
1097 red
>> (16-shifts
->physicalRed
.scale
);
1098 if (shifts
->physicalGreen
.scale
< 8)
1099 green
= green
>> (8-shifts
->physicalGreen
.scale
);
1100 else if (shifts
->physicalGreen
.scale
> 8)
1101 green
= green
<< (shifts
->physicalGreen
.scale
-8) |
1102 green
>> (16-shifts
->physicalGreen
.scale
);
1103 if (shifts
->physicalBlue
.scale
< 8)
1104 blue
= blue
>> (8-shifts
->physicalBlue
.scale
);
1105 else if (shifts
->physicalBlue
.scale
> 8)
1106 blue
= blue
<< (shifts
->physicalBlue
.scale
-8) |
1107 blue
>> (16-shifts
->physicalBlue
.scale
);
1109 return (red
<< shifts
->physicalRed
.shift
) | (green
<< shifts
->physicalGreen
.shift
) | (blue
<< shifts
->physicalBlue
.shift
);
1115 HPALETTE hPal
= GetStockObject(DEFAULT_PALETTE
);
1116 int *mapping
= palette_get_mapping( hPal
);
1119 WARN("Palette %p is not realized\n", hPal
);
1121 EnterCriticalSection( &palette_cs
);
1122 index
= X11DRV_SysPaletteLookupPixel( color
, FALSE
);
1123 if (X11DRV_PALETTE_PaletteToXPixel
)
1124 index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1125 LeaveCriticalSection( &palette_cs
);
1131 /***********************************************************************
1132 * X11DRV_PALETTE_LookupSystemXPixel
1134 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
)
1136 int i
, best
= 0, diff
= 0x7fffffff;
1137 int size
= palette_size
;
1140 for( i
= 0; i
< size
&& diff
; i
++ )
1142 if( i
== NB_RESERVED_COLORS
/2 )
1144 int newi
= size
- NB_RESERVED_COLORS
/2;
1148 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
1149 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
1150 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
1152 r
= r
*r
+ g
*g
+ b
*b
;
1154 if( r
< diff
) { best
= i
; diff
= r
; }
1157 return (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[best
] : best
;
1160 /***********************************************************************
1161 * X11DRV_PALETTE_FormatSystemPalette
1163 static void X11DRV_PALETTE_FormatSystemPalette(void)
1165 /* Build free list so we'd have an easy way to find
1166 * out if there are any available colorcells.
1169 int i
, j
= X11DRV_PALETTE_firstFree
= NB_RESERVED_COLORS
/2;
1171 COLOR_sysPal
[j
].peFlags
= 0;
1172 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
1173 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
1175 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
1176 X11DRV_PALETTE_freeList
[j
] = i
; /* next */
1179 X11DRV_PALETTE_freeList
[j
] = 0;
1182 /***********************************************************************
1183 * X11DRV_PALETTE_CheckSysColor
1185 static BOOL
X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY
*sys_pal_template
, COLORREF c
)
1188 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
1189 if( c
== (*(const COLORREF
*)(sys_pal_template
+ i
) & 0x00ffffff) )
1195 /***********************************************************************
1196 * X11DRV_LookupSysPaletteExact
1198 static int X11DRV_LookupSysPaletteExact( BYTE r
, BYTE g
, BYTE b
)
1201 for( i
= 0; i
< palette_size
; i
++ )
1203 if( COLOR_sysPal
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
1204 if( COLOR_sysPal
[i
].peRed
== r
&&
1205 COLOR_sysPal
[i
].peGreen
== g
&&
1206 COLOR_sysPal
[i
].peBlue
== b
)
1213 /***********************************************************************
1214 * RealizePalette (X11DRV.@)
1216 UINT
X11DRV_RealizePalette( PHYSDEV dev
, HPALETTE hpal
, BOOL primary
)
1218 X11DRV_PDEVICE
*physDev
= get_x11drv_dev( dev
);
1221 UINT i
, iRemapped
= 0;
1222 int *prev_mapping
, *mapping
;
1223 PALETTEENTRY entries
[256];
1226 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
) return 0;
1228 if (!GetObjectW( hpal
, sizeof(num_entries
), &num_entries
)) return 0;
1230 /* initialize palette mapping table */
1231 prev_mapping
= palette_get_mapping( hpal
);
1233 mapping
= HeapReAlloc( GetProcessHeap(), 0, prev_mapping
, sizeof(int)*num_entries
);
1235 mapping
= HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries
);
1237 if(mapping
== NULL
) {
1238 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1241 palette_set_mapping( hpal
, mapping
);
1243 if (num_entries
> 256)
1245 FIXME( "more than 256 entries not supported\n" );
1248 if (!(num_entries
= GetPaletteEntries( hpal
, 0, num_entries
, entries
))) return 0;
1250 /* reset dynamic system palette entries */
1252 EnterCriticalSection( &palette_cs
);
1253 if( primary
&& X11DRV_PALETTE_firstFree
!= -1)
1254 X11DRV_PALETTE_FormatSystemPalette();
1256 for (i
= 0; i
< num_entries
; i
++)
1261 /* Even though the docs say that only one flag is to be set,
1262 * they are a bitmask. At least one app sets more than one at
1264 if ( entries
[i
].peFlags
& PC_EXPLICIT
) {
1265 /* palette entries are indices into system palette */
1266 index
= *(WORD
*)&entries
[i
];
1267 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
1269 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
1272 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1274 if ( entries
[i
].peFlags
& PC_RESERVED
) {
1275 /* forbid future mappings to this entry */
1276 flag
|= PC_SYS_RESERVED
;
1279 if (! (entries
[i
].peFlags
& PC_NOCOLLAPSE
) ) {
1280 /* try to collapse identical colors */
1281 index
= X11DRV_LookupSysPaletteExact( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
);
1286 if( X11DRV_PALETTE_firstFree
> 0 )
1289 index
= X11DRV_PALETTE_firstFree
; /* ought to be available */
1290 X11DRV_PALETTE_firstFree
= X11DRV_PALETTE_freeList
[index
];
1292 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
) ? X11DRV_PALETTE_PaletteToXPixel
[index
] : index
;
1293 color
.red
= entries
[i
].peRed
<< 8;
1294 color
.green
= entries
[i
].peGreen
<< 8;
1295 color
.blue
= entries
[i
].peBlue
<< 8;
1296 color
.flags
= DoRed
| DoGreen
| DoBlue
;
1298 XStoreColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
1299 wine_tsx11_unlock();
1301 COLOR_sysPal
[index
] = entries
[i
];
1302 COLOR_sysPal
[index
].peFlags
= flag
;
1303 X11DRV_PALETTE_freeList
[index
] = 0;
1305 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1307 else if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
1309 index
= X11DRV_PALETTE_LookupPixel( physDev
->color_shifts
, RGB( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
));
1312 /* we have to map to existing entry in the system palette */
1314 index
= X11DRV_SysPaletteLookupPixel( RGB( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
),
1318 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1321 if( !prev_mapping
|| mapping
[i
] != index
) iRemapped
++;
1324 TRACE("entry %i (%x) -> pixel %i\n", i
, *(COLORREF
*)&entries
[i
], index
);
1327 LeaveCriticalSection( &palette_cs
);
1332 /***********************************************************************
1333 * UnrealizePalette (X11DRV.@)
1335 BOOL
X11DRV_UnrealizePalette( HPALETTE hpal
)
1337 int *mapping
= palette_get_mapping( hpal
);
1342 XDeleteContext( gdi_display
, (XID
)hpal
, palette_context
);
1343 wine_tsx11_unlock();
1344 HeapFree( GetProcessHeap(), 0, mapping
);
1350 /***********************************************************************
1351 * GetSystemPaletteEntries (X11DRV.@)
1353 UINT
X11DRV_GetSystemPaletteEntries( PHYSDEV dev
, UINT start
, UINT count
, LPPALETTEENTRY entries
)
1357 if (!entries
) return palette_size
;
1358 if (start
>= palette_size
) return 0;
1359 if (start
+ count
>= palette_size
) count
= palette_size
- start
;
1361 EnterCriticalSection( &palette_cs
);
1362 for (i
= 0; i
< count
; i
++)
1364 entries
[i
].peRed
= COLOR_sysPal
[start
+ i
].peRed
;
1365 entries
[i
].peGreen
= COLOR_sysPal
[start
+ i
].peGreen
;
1366 entries
[i
].peBlue
= COLOR_sysPal
[start
+ i
].peBlue
;
1367 entries
[i
].peFlags
= 0;
1368 TRACE("\tidx(%02x) -> RGB(%08x)\n", start
+ i
, *(COLORREF
*)(entries
+ i
) );
1370 LeaveCriticalSection( &palette_cs
);
1375 /***********************************************************************
1376 * GetNearestColor (X11DRV.@)
1378 COLORREF
X11DRV_GetNearestColor( PHYSDEV dev
, COLORREF color
)
1380 unsigned char spec_type
= color
>> 24;
1383 if (!palette_size
) return color
;
1385 if (spec_type
== 1 || spec_type
== 2)
1387 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1391 HPALETTE hpal
= GetCurrentObject( dev
->hdc
, OBJ_PAL
);
1393 if (!hpal
) hpal
= GetStockObject( DEFAULT_PALETTE
);
1395 if (spec_type
== 2) /* PALETTERGB */
1396 index
= GetNearestPaletteIndex( hpal
, color
);
1397 else /* PALETTEINDEX */
1398 index
= LOWORD(color
);
1400 if (!GetPaletteEntries( hpal
, index
, 1, &entry
))
1402 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color
, index
);
1403 if (!GetPaletteEntries( hpal
, 0, 1, &entry
)) return CLR_INVALID
;
1405 color
= RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
1407 color
&= 0x00ffffff;
1408 EnterCriticalSection( &palette_cs
);
1409 nearest
= (0x00ffffff & *(COLORREF
*)(COLOR_sysPal
+ X11DRV_SysPaletteLookupPixel(color
, FALSE
)));
1410 LeaveCriticalSection( &palette_cs
);
1412 TRACE("(%06x): returning %06x\n", color
, nearest
);
1417 /***********************************************************************
1418 * RealizeDefaultPalette (X11DRV.@)
1420 UINT
X11DRV_RealizeDefaultPalette( PHYSDEV dev
)
1424 if (palette_size
&& GetObjectType(dev
->hdc
) != OBJ_MEMDC
)
1426 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1427 int i
, index
, *mapping
= palette_get_mapping( GetStockObject(DEFAULT_PALETTE
) );
1428 PALETTEENTRY entries
[NB_RESERVED_COLORS
];
1430 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE
), 0, NB_RESERVED_COLORS
, entries
);
1431 EnterCriticalSection( &palette_cs
);
1432 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
1434 index
= X11DRV_PALETTE_LookupSystemXPixel( RGB(entries
[i
].peRed
,
1436 entries
[i
].peBlue
) );
1437 /* mapping is allocated in COLOR_InitPalette() */
1438 if( index
!= mapping
[i
] )
1444 LeaveCriticalSection( &palette_cs
);