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
;
160 if (private_color_map
)
162 XSetWindowAttributes win_attr
;
164 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap( gdi_display
, root_window
,
166 if (X11DRV_PALETTE_PaletteXColormap
)
168 X11DRV_PALETTE_PaletteFlags
|= (X11DRV_PALETTE_PRIVATE
| X11DRV_PALETTE_WHITESET
);
171 for( white
= palette_size
- 1; !(white
& 1); white
>>= 1 )
174 if( root_window
!= DefaultRootWindow(gdi_display
) )
176 win_attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
;
177 XChangeWindowAttributes( gdi_display
, root_window
, CWColormap
, &win_attr
);
181 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
187 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
189 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
190 X11DRV_PALETTE_Graymax
= (1 << screen_depth
)-1;
194 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
196 int *depths
,nrofdepths
;
197 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
200 depths
= XListDepths(gdi_display
,DefaultScreen(gdi_display
),&nrofdepths
);
201 if ((nrofdepths
==2) && ((depths
[0]==4) || depths
[1]==4)) {
203 for( white
= palette_size
- 1; !(white
& 1); white
>>= 1 )
205 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
206 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
211 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
213 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
214 X11DRV_PALETTE_ComputeColorShifts(&X11DRV_PALETTE_default_shifts
, visual
->red_mask
, visual
->green_mask
, visual
->blue_mask
);
221 TRACE(" visual class %i (%i)\n", visual
->class, monoPlane
);
223 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE
), 0, NB_RESERVED_COLORS
, sys_pal_template
);
225 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
231 if ((mapping
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(int) * NB_RESERVED_COLORS
)))
232 palette_set_mapping( GetStockObject(DEFAULT_PALETTE
), mapping
);
234 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
235 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template
);
237 X11DRV_PALETTE_BuildSharedMap( sys_pal_template
);
239 /* Build free list */
241 if( X11DRV_PALETTE_firstFree
!= -1 )
242 X11DRV_PALETTE_FormatSystemPalette();
244 X11DRV_PALETTE_FillDefaultColors( sys_pal_template
);
245 palette_size
= visual
->map_entries
;
251 /***********************************************************************
252 * X11DRV_PALETTE_ComputeChannelShift
254 * Calculate conversion parameters for a given color mask
256 static void X11DRV_PALETTE_ComputeChannelShift(unsigned long maskbits
, ChannelShift
*physical
, ChannelShift
*to_logical
)
271 for(i
=0;!(maskbits
&1);i
++)
275 physical
->max
= maskbits
;
277 for(i
=0;maskbits
!=0;i
++)
281 if (physical
->scale
>8)
283 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
284 * So we adjust the shifts to also normalize the color fields to
285 * the Win32 standard of 8 bits per color.
287 to_logical
->shift
=physical
->shift
+(physical
->scale
-8);
289 to_logical
->max
=0xff;
291 to_logical
->shift
=physical
->shift
;
292 to_logical
->scale
=physical
->scale
;
293 to_logical
->max
=physical
->max
;
297 /***********************************************************************
298 * X11DRV_PALETTE_ComputeColorShifts
300 * Calculate conversion parameters for a given color
302 void X11DRV_PALETTE_ComputeColorShifts(ColorShifts
*shifts
, unsigned long redMask
, unsigned long greenMask
, unsigned long blueMask
)
304 X11DRV_PALETTE_ComputeChannelShift(redMask
, &shifts
->physicalRed
, &shifts
->logicalRed
);
305 X11DRV_PALETTE_ComputeChannelShift(greenMask
, &shifts
->physicalGreen
, &shifts
->logicalGreen
);
306 X11DRV_PALETTE_ComputeChannelShift(blueMask
, &shifts
->physicalBlue
, &shifts
->logicalBlue
);
309 /***********************************************************************
310 * X11DRV_PALETTE_BuildPrivateMap
312 * Allocate colorcells and initialize mapping tables.
314 static BOOL
X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY
*sys_pal_template
)
316 /* Private colormap - identity mapping */
321 if((COLOR_sysPal
= HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY
)*palette_size
)) == NULL
) {
322 WARN("Unable to allocate the system palette\n");
326 TRACE("Building private map - %i palette entries\n", palette_size
);
328 /* Allocate system palette colors */
330 for( i
=0; i
< palette_size
; i
++ )
332 if( i
< NB_RESERVED_COLORS
/2 )
334 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
335 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
336 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
337 COLOR_sysPal
[i
] = sys_pal_template
[i
];
338 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
340 else if( i
>= palette_size
- NB_RESERVED_COLORS
/2 )
342 int j
= NB_RESERVED_COLORS
+ i
- palette_size
;
343 color
.red
= sys_pal_template
[j
].peRed
* 65535 / 255;
344 color
.green
= sys_pal_template
[j
].peGreen
* 65535 / 255;
345 color
.blue
= sys_pal_template
[j
].peBlue
* 65535 / 255;
346 COLOR_sysPal
[i
] = sys_pal_template
[j
];
347 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
350 color
.flags
= DoRed
| DoGreen
| DoBlue
;
352 XStoreColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
354 /* Set EGA mapping if color is from the first or last eight */
357 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
358 else if (i
>= palette_size
- 8 )
359 X11DRV_PALETTE_mapEGAPixel
[i
- (palette_size
- 16)] = color
.pixel
;
362 X11DRV_PALETTE_XPixelToPalette
= X11DRV_PALETTE_PaletteToXPixel
= NULL
;
364 COLOR_gapStart
= 256; COLOR_gapEnd
= -1;
366 X11DRV_PALETTE_firstFree
= (palette_size
> NB_RESERVED_COLORS
)?NB_RESERVED_COLORS
/2 : -1;
371 /***********************************************************************
372 * X11DRV_PALETTE_BuildSharedMap
374 * Allocate colorcells and initialize mapping tables.
376 static BOOL
X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY
*sys_pal_template
)
379 unsigned long sysPixel
[NB_RESERVED_COLORS
];
380 unsigned long* pixDynMapping
= NULL
;
381 unsigned long plane_masks
[1];
383 int diff
, r
, g
, b
, bp
= 0, wp
= 1;
385 unsigned int max
= 256;
387 XColor defaultColors
[256];
389 /* Copy the first bunch of colors out of the default colormap to prevent
390 * colormap flashing as much as possible. We're likely to get the most
391 * important Window Manager colors, etc in the first 128 colors */
392 defaultCM
= DefaultColormap( gdi_display
, DefaultScreen(gdi_display
) );
394 if (copy_default_colors
> 256) copy_default_colors
= 256;
395 for (i
= 0; i
< copy_default_colors
; i
++)
396 defaultColors
[i
].pixel
= (long) i
;
397 XQueryColors(gdi_display
, defaultCM
, &defaultColors
[0], copy_default_colors
);
398 for (i
= 0; i
< copy_default_colors
; i
++)
399 XAllocColor( gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &defaultColors
[i
] );
401 if (alloc_system_colors
> 256) alloc_system_colors
= 256;
402 else if (alloc_system_colors
< 20) alloc_system_colors
= 20;
403 TRACE("%d colors configured.\n", alloc_system_colors
);
405 TRACE("Building shared map - %i palette entries\n", palette_size
);
407 /* Be nice and allocate system colors as read-only */
409 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
411 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
412 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
413 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
414 color
.flags
= DoRed
| DoGreen
| DoBlue
;
416 if (!XAllocColor( gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
))
422 WARN("Not enough colors for the full system palette.\n");
424 bp
= BlackPixel(gdi_display
, DefaultScreen(gdi_display
));
425 wp
= WhitePixel(gdi_display
, DefaultScreen(gdi_display
));
427 max
= (0xffffffff)>>(32 - screen_depth
);
435 /* reinit color (XAllocColor() may change it)
436 * and map to the best shared colorcell */
438 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
439 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
440 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
442 best
.pixel
= best
.red
= best
.green
= best
.blue
= 0;
443 for( c
.pixel
= 0, diff
= 0x7fffffff; c
.pixel
< max
; c
.pixel
+= step
)
445 XQueryColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &c
);
446 r
= (c
.red
- color
.red
)>>8;
447 g
= (c
.green
- color
.green
)>>8;
448 b
= (c
.blue
- color
.blue
)>>8;
450 if( r
< diff
) { best
= c
; diff
= r
; }
453 if( XAllocColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &best
) )
454 color
.pixel
= best
.pixel
;
455 else color
.pixel
= (i
< NB_RESERVED_COLORS
/2)? bp
: wp
;
458 sysPixel
[i
] = color
.pixel
;
460 TRACE("syscolor(%x) -> pixel %i\n", *(const COLORREF
*)(sys_pal_template
+i
),
463 /* Set EGA mapping if color in the first or last eight */
466 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
467 else if (i
>= NB_RESERVED_COLORS
- 8 )
468 X11DRV_PALETTE_mapEGAPixel
[i
- (NB_RESERVED_COLORS
-16)] = color
.pixel
;
471 /* now allocate changeable set */
473 if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
475 int c_min
= 0, c_max
= palette_size
, c_val
;
477 TRACE("Dynamic colormap...\n");
479 /* let's become the first client that actually follows
480 * X guidelines and does binary search...
483 if((pixDynMapping
= HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size
)) == NULL
) {
484 WARN("Out of memory while building system palette.\n");
488 /* comment this out if you want to debug palette init */
489 XGrabServer(gdi_display
);
491 while( c_max
- c_min
> 0 )
493 c_val
= (c_max
+ c_min
)/2 + (c_max
+ c_min
)%2;
495 if( !XAllocColorCells(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, False
,
496 plane_masks
, 0, pixDynMapping
, c_val
) )
500 XFreeColors(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, pixDynMapping
, c_val
, 0);
505 if( c_min
> alloc_system_colors
- NB_RESERVED_COLORS
)
506 c_min
= alloc_system_colors
- NB_RESERVED_COLORS
;
508 c_min
= (c_min
/2) + (c_min
/2); /* need even set for split palette */
511 if( !XAllocColorCells(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, False
,
512 plane_masks
, 0, pixDynMapping
, c_min
) )
514 WARN("Inexplicable failure during colorcell allocation.\n");
518 palette_size
= c_min
+ NB_RESERVED_COLORS
;
520 XUngrabServer(gdi_display
);
522 TRACE("adjusted size %i colorcells\n", palette_size
);
524 else if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
526 /* virtual colorspace - ToPhysical takes care of
527 * color translations but we have to allocate full palette
528 * to maintain compatibility
531 TRACE("Virtual colorspace - screendepth %i\n", screen_depth
);
533 else palette_size
= NB_RESERVED_COLORS
; /* system palette only - however we can alloc a bunch
534 * of colors and map to them */
536 TRACE("Shared system palette uses %i colors.\n", palette_size
);
538 /* set gap to account for pixel shortage. It has to be right in the center
539 * of the system palette because otherwise raster ops get screwed. */
541 if( palette_size
>= 256 )
542 { COLOR_gapStart
= 256; COLOR_gapEnd
= -1; }
544 { COLOR_gapStart
= palette_size
/2; COLOR_gapEnd
= 255 - palette_size
/2; }
546 X11DRV_PALETTE_firstFree
= ( palette_size
> NB_RESERVED_COLORS
&&
547 (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
|| !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)) )
548 ? NB_RESERVED_COLORS
/2 : -1;
550 COLOR_sysPal
= HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY
)*256);
551 if(COLOR_sysPal
== NULL
) {
552 ERR("Unable to allocate the system palette!\n");
553 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
557 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
559 if (screen_depth
<= 8)
561 X11DRV_PALETTE_XPixelToPalette
= HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
562 if(X11DRV_PALETTE_XPixelToPalette
== NULL
) {
563 ERR("Out of memory: XPixelToPalette!\n");
564 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
567 for( i
= 0; i
< 256; i
++ )
568 X11DRV_PALETTE_XPixelToPalette
[i
] = NB_PALETTE_EMPTY_VALUE
;
571 /* for hicolor visuals PaletteToPixel mapping is used to skip
572 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
575 X11DRV_PALETTE_PaletteToXPixel
= HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
576 if(X11DRV_PALETTE_PaletteToXPixel
== NULL
) {
577 ERR("Out of memory: PaletteToXPixel!\n");
578 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
582 for( i
= j
= 0; i
< 256; i
++ )
584 if( i
>= COLOR_gapStart
&& i
<= COLOR_gapEnd
)
586 X11DRV_PALETTE_PaletteToXPixel
[i
] = NB_PALETTE_EMPTY_VALUE
;
587 COLOR_sysPal
[i
].peFlags
= 0; /* mark as unused */
591 if( i
< NB_RESERVED_COLORS
/2 )
593 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[i
];
594 COLOR_sysPal
[i
] = sys_pal_template
[i
];
595 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
597 else if( i
>= 256 - NB_RESERVED_COLORS
/2 )
599 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[(i
+ NB_RESERVED_COLORS
) - 256];
600 COLOR_sysPal
[i
] = sys_pal_template
[(i
+ NB_RESERVED_COLORS
) - 256];
601 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
603 else if( pixDynMapping
)
604 X11DRV_PALETTE_PaletteToXPixel
[i
] = pixDynMapping
[j
++];
606 X11DRV_PALETTE_PaletteToXPixel
[i
] = i
;
608 TRACE("index %i -> pixel %i\n", i
, X11DRV_PALETTE_PaletteToXPixel
[i
]);
610 if( X11DRV_PALETTE_XPixelToPalette
)
611 X11DRV_PALETTE_XPixelToPalette
[X11DRV_PALETTE_PaletteToXPixel
[i
]] = i
;
614 HeapFree(GetProcessHeap(), 0, pixDynMapping
);
619 /***********************************************************************
620 * Colormap Initialization
622 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY
*sys_pal_template
)
624 /* initialize unused entries to what Windows uses as a color
625 * cube - based on Greg Kreider's code.
629 int red
, no_r
, inc_r
;
630 int green
, no_g
, inc_g
;
631 int blue
, no_b
, inc_b
;
633 if (palette_size
<= NB_RESERVED_COLORS
)
635 while (i
*i
*i
< (palette_size
- NB_RESERVED_COLORS
)) i
++;
636 no_r
= no_g
= no_b
= --i
;
637 if ((no_r
* (no_g
+1) * no_b
) < (palette_size
- NB_RESERVED_COLORS
)) no_g
++;
638 if ((no_r
* no_g
* (no_b
+1)) < (palette_size
- NB_RESERVED_COLORS
)) no_b
++;
639 inc_r
= (255 - NB_COLORCUBE_START_INDEX
)/no_r
;
640 inc_g
= (255 - NB_COLORCUBE_START_INDEX
)/no_g
;
641 inc_b
= (255 - NB_COLORCUBE_START_INDEX
)/no_b
;
643 idx
= X11DRV_PALETTE_firstFree
;
646 for (blue
= NB_COLORCUBE_START_INDEX
; blue
< 256 && idx
; blue
+= inc_b
)
647 for (green
= NB_COLORCUBE_START_INDEX
; green
< 256 && idx
; green
+= inc_g
)
648 for (red
= NB_COLORCUBE_START_INDEX
; red
< 256 && idx
; red
+= inc_r
)
652 if( red
== NB_COLORCUBE_START_INDEX
&& green
== red
&& blue
== green
) continue;
654 COLOR_sysPal
[idx
].peRed
= red
;
655 COLOR_sysPal
[idx
].peGreen
= green
;
656 COLOR_sysPal
[idx
].peBlue
= blue
;
660 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
662 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
663 if (shifts
->physicalRed
.max
!= 255) no_r
= (red
* shifts
->physicalRed
.max
) / 255;
664 if (shifts
->physicalGreen
.max
!= 255) no_g
= (green
* shifts
->physicalGreen
.max
) / 255;
665 if (shifts
->physicalBlue
.max
!= 255) no_b
= (blue
* shifts
->physicalBlue
.max
) / 255;
667 X11DRV_PALETTE_PaletteToXPixel
[idx
] = (no_r
<< shifts
->physicalRed
.shift
) | (no_g
<< shifts
->physicalGreen
.shift
) | (no_b
<< shifts
->physicalBlue
.shift
);
669 else if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
672 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[idx
] : idx
;
673 color
.red
= COLOR_sysPal
[idx
].peRed
<< 8;
674 color
.green
= COLOR_sysPal
[idx
].peGreen
<< 8;
675 color
.blue
= COLOR_sysPal
[idx
].peBlue
<< 8;
676 color
.flags
= DoRed
| DoGreen
| DoBlue
;
677 XStoreColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
679 idx
= X11DRV_PALETTE_freeList
[idx
];
682 /* try to fill some entries in the "gap" with
683 * what's already in the colormap - they will be
684 * mappable to but not changeable. */
686 if( COLOR_gapStart
< COLOR_gapEnd
&& X11DRV_PALETTE_XPixelToPalette
)
691 max
= alloc_system_colors
- (256 - (COLOR_gapEnd
- COLOR_gapStart
));
692 for ( i
= 0, idx
= COLOR_gapStart
; i
< 256 && idx
<= COLOR_gapEnd
; i
++ )
693 if( X11DRV_PALETTE_XPixelToPalette
[i
] == NB_PALETTE_EMPTY_VALUE
)
697 XQueryColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &xc
);
698 r
= xc
.red
>>8; g
= xc
.green
>>8; b
= xc
.blue
>>8;
700 if( xc
.pixel
< 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template
, RGB(r
, g
, b
)) &&
701 XAllocColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &xc
) )
703 X11DRV_PALETTE_XPixelToPalette
[xc
.pixel
] = idx
;
704 X11DRV_PALETTE_PaletteToXPixel
[idx
] = xc
.pixel
;
705 *(COLORREF
*)(COLOR_sysPal
+ idx
) = RGB(r
, g
, b
);
706 COLOR_sysPal
[idx
++].peFlags
|= PC_SYS_USED
;
707 if( --max
<= 0 ) break;
710 COLOR_gapFilled
= idx
- COLOR_gapStart
;
715 /***********************************************************************
716 * X11DRV_IsSolidColor
718 * Check whether 'color' can be represented with a solid color.
720 BOOL
X11DRV_IsSolidColor( COLORREF color
)
723 const PALETTEENTRY
*pEntry
= COLOR_sysPal
;
725 if (color
& 0xff000000) return TRUE
; /* indexed color */
727 if (!color
|| (color
== 0xffffff)) return TRUE
; /* black or white */
729 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
) return TRUE
; /* no palette */
731 EnterCriticalSection( &palette_cs
);
732 for (i
= 0; i
< palette_size
; i
++, pEntry
++)
734 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
735 if ((GetRValue(color
) == pEntry
->peRed
) &&
736 (GetGValue(color
) == pEntry
->peGreen
) &&
737 (GetBValue(color
) == pEntry
->peBlue
))
739 LeaveCriticalSection( &palette_cs
);
743 LeaveCriticalSection( &palette_cs
);
748 /***********************************************************************
749 * X11DRV_PALETTE_ToLogical
751 * Return RGB color for given X pixel.
753 COLORREF
X11DRV_PALETTE_ToLogical(X11DRV_PDEVICE
*physDev
, int pixel
)
758 /* truecolor visual */
760 if (screen_depth
>= 24) return pixel
;
763 /* check for hicolor visuals first */
765 if ( (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) && !X11DRV_PALETTE_Graymax
)
767 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
769 if(physDev
->color_shifts
)
770 shifts
= physDev
->color_shifts
;
772 color
.red
= (pixel
>> shifts
->logicalRed
.shift
) & shifts
->logicalRed
.max
;
773 if (shifts
->logicalRed
.scale
<8)
774 color
.red
= color
.red
<< (8-shifts
->logicalRed
.scale
) |
775 color
.red
>> (2*shifts
->logicalRed
.scale
-8);
776 color
.green
= (pixel
>> shifts
->logicalGreen
.shift
) & shifts
->logicalGreen
.max
;
777 if (shifts
->logicalGreen
.scale
<8)
778 color
.green
=color
.green
<< (8-shifts
->logicalGreen
.scale
) |
779 color
.green
>> (2*shifts
->logicalGreen
.scale
-8);
780 color
.blue
= (pixel
>> shifts
->logicalBlue
.shift
) & shifts
->logicalBlue
.max
;
781 if (shifts
->logicalBlue
.scale
<8)
782 color
.blue
= color
.blue
<< (8-shifts
->logicalBlue
.scale
) |
783 color
.blue
>> (2*shifts
->logicalBlue
.scale
-8);
784 return RGB(color
.red
,color
.green
,color
.blue
);
787 /* check if we can bypass X */
789 if ((screen_depth
<= 8) && (pixel
< 256) &&
790 !(X11DRV_PALETTE_PaletteFlags
& (X11DRV_PALETTE_VIRTUAL
| X11DRV_PALETTE_FIXED
)) ) {
792 EnterCriticalSection( &palette_cs
);
793 ret
= *(COLORREF
*)(COLOR_sysPal
+ (X11DRV_PALETTE_XPixelToPalette
? X11DRV_PALETTE_XPixelToPalette
[pixel
]: pixel
)) & 0x00ffffff;
794 LeaveCriticalSection( &palette_cs
);
799 XQueryColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
800 return RGB(color
.red
>> 8, color
.green
>> 8, color
.blue
>> 8);
804 /***********************************************************************
805 * X11DRV_SysPaletteLookupPixel
807 static int X11DRV_SysPaletteLookupPixel( COLORREF col
, BOOL skipReserved
)
809 int i
, best
= 0, diff
= 0x7fffffff;
812 for( i
= 0; i
< palette_size
&& diff
; i
++ )
814 if( !(COLOR_sysPal
[i
].peFlags
& PC_SYS_USED
) ||
815 (skipReserved
&& COLOR_sysPal
[i
].peFlags
& PC_SYS_RESERVED
) )
818 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
819 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
820 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
824 if( r
< diff
) { best
= i
; diff
= r
; }
830 static inline BOOL
colour_is_brighter(RGBQUAD c1
, RGBQUAD c2
)
832 return (c1
.rgbRed
* c1
.rgbRed
+ c1
.rgbGreen
* c1
.rgbGreen
+ c1
.rgbBlue
* c1
.rgbBlue
) >
833 (c2
.rgbRed
* c2
.rgbRed
+ c2
.rgbGreen
* c2
.rgbGreen
+ c2
.rgbBlue
* c2
.rgbBlue
);
836 /***********************************************************************
837 * X11DRV_PALETTE_GetColor
839 * Resolve PALETTEINDEX/PALETTERGB/DIBINDEX COLORREFs to an RGB COLORREF.
841 COLORREF
X11DRV_PALETTE_GetColor( X11DRV_PDEVICE
*physDev
, COLORREF color
)
843 HPALETTE hPal
= GetCurrentObject(physDev
->dev
.hdc
, OBJ_PAL
);
846 if (color
& (1 << 24)) /* PALETTEINDEX */
848 unsigned int idx
= LOWORD(color
);
849 if (!GetPaletteEntries( hPal
, idx
, 1, &entry
)) return 0;
850 return RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
853 if (color
>> 24 == 2) /* PALETTERGB */
855 unsigned int idx
= GetNearestPaletteIndex( hPal
, color
& 0xffffff );
856 if (!GetPaletteEntries( hPal
, idx
, 1, &entry
)) return 0;
857 return RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
860 if (color
>> 16 == 0x10ff) /* DIBINDEX */
863 return color
& 0xffffff;
866 /***********************************************************************
867 * X11DRV_PALETTE_ToPhysical
869 * Return the physical color closest to 'color'.
871 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE
*physDev
, COLORREF color
)
874 HPALETTE hPal
= GetCurrentObject(physDev
->dev
.hdc
, OBJ_PAL
);
875 int *mapping
= palette_get_mapping( hPal
);
877 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
879 if(physDev
->color_shifts
)
880 shifts
= physDev
->color_shifts
;
882 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
884 /* there is no colormap limitation; we are going to have to compute
885 * the pixel value from the visual information stored earlier
887 unsigned long red
, green
, blue
;
889 if (color
& (1 << 24)) /* PALETTEINDEX */
891 unsigned int idx
= LOWORD( color
);
893 if (!GetPaletteEntries( hPal
, idx
, 1, &entry
))
895 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color
, idx
);
898 if (mapping
) return mapping
[idx
];
900 green
= entry
.peGreen
;
903 else if (color
>> 16 == 0x10ff) /* DIBINDEX */
909 if (physDev
->depth
== 1)
910 return (((color
>> 16) & 0xff) +
911 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
912 red
= GetRValue( color
);
913 green
= GetGValue( color
);
914 blue
= GetBValue( color
);
917 if (X11DRV_PALETTE_Graymax
)
919 /* grayscale only; return scaled value */
920 return ( (red
* 30 + green
* 59 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
924 /* scale each individually and construct the TrueColor pixel value */
925 if (shifts
->physicalRed
.scale
< 8)
926 red
= red
>> (8-shifts
->physicalRed
.scale
);
927 else if (shifts
->physicalRed
.scale
> 8)
928 red
= red
<< (shifts
->physicalRed
.scale
-8) |
929 red
>> (16-shifts
->physicalRed
.scale
);
930 if (shifts
->physicalGreen
.scale
< 8)
931 green
= green
>> (8-shifts
->physicalGreen
.scale
);
932 else if (shifts
->physicalGreen
.scale
> 8)
933 green
= green
<< (shifts
->physicalGreen
.scale
-8) |
934 green
>> (16-shifts
->physicalGreen
.scale
);
935 if (shifts
->physicalBlue
.scale
< 8)
936 blue
= blue
>> (8-shifts
->physicalBlue
.scale
);
937 else if (shifts
->physicalBlue
.scale
> 8)
938 blue
= blue
<< (shifts
->physicalBlue
.scale
-8) |
939 blue
>> (16-shifts
->physicalBlue
.scale
);
941 return (red
<< shifts
->physicalRed
.shift
) | (green
<< shifts
->physicalGreen
.shift
) | (blue
<< shifts
->physicalBlue
.shift
);
947 WARN("Palette %p is not realized\n", hPal
);
949 if (color
& (1 << 24)) /* PALETTEINDEX */
951 index
= LOWORD( color
);
952 if (!GetPaletteEntries( hPal
, index
, 1, &entry
))
953 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color
, index
);
954 else if (mapping
) index
= mapping
[index
];
956 else if (color
>> 24 == 2) /* PALETTERGB */
958 index
= GetNearestPaletteIndex( hPal
, color
);
959 if (mapping
) index
= mapping
[index
];
961 else if (color
>> 16 == 0x10ff) /* DIBINDEX */
967 if (physDev
->depth
== 1)
968 return (((color
>> 16) & 0xff) +
969 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
971 EnterCriticalSection( &palette_cs
);
972 index
= X11DRV_SysPaletteLookupPixel( color
& 0xffffff, FALSE
);
973 if (X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
974 LeaveCriticalSection( &palette_cs
);
980 /***********************************************************************
981 * X11DRV_PALETTE_LookupPixel
983 int X11DRV_PALETTE_LookupPixel(ColorShifts
*shifts
, COLORREF color
)
985 unsigned char spec_type
= color
>> 24;
987 /* Only accept RGB which has spec_type = 0 */
993 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
995 unsigned long red
, green
, blue
;
996 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
998 if (X11DRV_PALETTE_Graymax
)
1000 /* grayscale only; return scaled value */
1001 return ( (red
* 30 + green
* 59 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
1005 /* No shifts are set in case of 1-bit */
1006 if(!shifts
) shifts
= &X11DRV_PALETTE_default_shifts
;
1008 /* scale each individually and construct the TrueColor pixel value */
1009 if (shifts
->physicalRed
.scale
< 8)
1010 red
= red
>> (8-shifts
->physicalRed
.scale
);
1011 else if (shifts
->physicalRed
.scale
> 8)
1012 red
= red
<< (shifts
->physicalRed
.scale
-8) |
1013 red
>> (16-shifts
->physicalRed
.scale
);
1014 if (shifts
->physicalGreen
.scale
< 8)
1015 green
= green
>> (8-shifts
->physicalGreen
.scale
);
1016 else if (shifts
->physicalGreen
.scale
> 8)
1017 green
= green
<< (shifts
->physicalGreen
.scale
-8) |
1018 green
>> (16-shifts
->physicalGreen
.scale
);
1019 if (shifts
->physicalBlue
.scale
< 8)
1020 blue
= blue
>> (8-shifts
->physicalBlue
.scale
);
1021 else if (shifts
->physicalBlue
.scale
> 8)
1022 blue
= blue
<< (shifts
->physicalBlue
.scale
-8) |
1023 blue
>> (16-shifts
->physicalBlue
.scale
);
1025 return (red
<< shifts
->physicalRed
.shift
) | (green
<< shifts
->physicalGreen
.shift
) | (blue
<< shifts
->physicalBlue
.shift
);
1031 HPALETTE hPal
= GetStockObject(DEFAULT_PALETTE
);
1032 int *mapping
= palette_get_mapping( hPal
);
1035 WARN("Palette %p is not realized\n", hPal
);
1037 EnterCriticalSection( &palette_cs
);
1038 index
= X11DRV_SysPaletteLookupPixel( color
, FALSE
);
1039 if (X11DRV_PALETTE_PaletteToXPixel
)
1040 index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1041 LeaveCriticalSection( &palette_cs
);
1047 /***********************************************************************
1048 * X11DRV_PALETTE_LookupSystemXPixel
1050 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
)
1052 int i
, best
= 0, diff
= 0x7fffffff;
1053 int size
= palette_size
;
1056 for( i
= 0; i
< size
&& diff
; i
++ )
1058 if( i
== NB_RESERVED_COLORS
/2 )
1060 int newi
= size
- NB_RESERVED_COLORS
/2;
1064 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
1065 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
1066 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
1068 r
= r
*r
+ g
*g
+ b
*b
;
1070 if( r
< diff
) { best
= i
; diff
= r
; }
1073 return (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[best
] : best
;
1076 /***********************************************************************
1077 * X11DRV_PALETTE_FormatSystemPalette
1079 static void X11DRV_PALETTE_FormatSystemPalette(void)
1081 /* Build free list so we'd have an easy way to find
1082 * out if there are any available colorcells.
1085 int i
, j
= X11DRV_PALETTE_firstFree
= NB_RESERVED_COLORS
/2;
1087 COLOR_sysPal
[j
].peFlags
= 0;
1088 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
1089 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
1091 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
1092 X11DRV_PALETTE_freeList
[j
] = i
; /* next */
1095 X11DRV_PALETTE_freeList
[j
] = 0;
1098 /***********************************************************************
1099 * X11DRV_PALETTE_CheckSysColor
1101 static BOOL
X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY
*sys_pal_template
, COLORREF c
)
1104 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
1105 if( c
== (*(const COLORREF
*)(sys_pal_template
+ i
) & 0x00ffffff) )
1111 /***********************************************************************
1112 * X11DRV_LookupSysPaletteExact
1114 static int X11DRV_LookupSysPaletteExact( BYTE r
, BYTE g
, BYTE b
)
1117 for( i
= 0; i
< palette_size
; i
++ )
1119 if( COLOR_sysPal
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
1120 if( COLOR_sysPal
[i
].peRed
== r
&&
1121 COLOR_sysPal
[i
].peGreen
== g
&&
1122 COLOR_sysPal
[i
].peBlue
== b
)
1129 /***********************************************************************
1130 * RealizePalette (X11DRV.@)
1132 UINT
X11DRV_RealizePalette( PHYSDEV dev
, HPALETTE hpal
, BOOL primary
)
1134 X11DRV_PDEVICE
*physDev
= get_x11drv_dev( dev
);
1137 UINT i
, iRemapped
= 0;
1138 int *prev_mapping
, *mapping
;
1139 PALETTEENTRY entries
[256];
1142 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
) return 0;
1144 if (!GetObjectW( hpal
, sizeof(num_entries
), &num_entries
)) return 0;
1146 /* initialize palette mapping table */
1147 prev_mapping
= palette_get_mapping( hpal
);
1149 mapping
= HeapReAlloc( GetProcessHeap(), 0, prev_mapping
, sizeof(int)*num_entries
);
1151 mapping
= HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries
);
1153 if(mapping
== NULL
) {
1154 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1157 palette_set_mapping( hpal
, mapping
);
1159 if (num_entries
> 256)
1161 FIXME( "more than 256 entries not supported\n" );
1164 if (!(num_entries
= GetPaletteEntries( hpal
, 0, num_entries
, entries
))) return 0;
1166 /* reset dynamic system palette entries */
1168 EnterCriticalSection( &palette_cs
);
1169 if( primary
&& X11DRV_PALETTE_firstFree
!= -1)
1170 X11DRV_PALETTE_FormatSystemPalette();
1172 for (i
= 0; i
< num_entries
; i
++)
1177 /* Even though the docs say that only one flag is to be set,
1178 * they are a bitmask. At least one app sets more than one at
1180 if ( entries
[i
].peFlags
& PC_EXPLICIT
) {
1181 /* palette entries are indices into system palette */
1182 index
= *(WORD
*)&entries
[i
];
1183 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
1185 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
1188 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1190 if ( entries
[i
].peFlags
& PC_RESERVED
) {
1191 /* forbid future mappings to this entry */
1192 flag
|= PC_SYS_RESERVED
;
1195 if (! (entries
[i
].peFlags
& PC_NOCOLLAPSE
) ) {
1196 /* try to collapse identical colors */
1197 index
= X11DRV_LookupSysPaletteExact( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
);
1202 if( X11DRV_PALETTE_firstFree
> 0 )
1205 index
= X11DRV_PALETTE_firstFree
; /* ought to be available */
1206 X11DRV_PALETTE_firstFree
= X11DRV_PALETTE_freeList
[index
];
1208 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
) ? X11DRV_PALETTE_PaletteToXPixel
[index
] : index
;
1209 color
.red
= entries
[i
].peRed
<< 8;
1210 color
.green
= entries
[i
].peGreen
<< 8;
1211 color
.blue
= entries
[i
].peBlue
<< 8;
1212 color
.flags
= DoRed
| DoGreen
| DoBlue
;
1213 XStoreColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
1215 COLOR_sysPal
[index
] = entries
[i
];
1216 COLOR_sysPal
[index
].peFlags
= flag
;
1217 X11DRV_PALETTE_freeList
[index
] = 0;
1219 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1221 else if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
1223 index
= X11DRV_PALETTE_LookupPixel( physDev
->color_shifts
, RGB( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
));
1226 /* we have to map to existing entry in the system palette */
1228 index
= X11DRV_SysPaletteLookupPixel( RGB( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
),
1232 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1235 if( !prev_mapping
|| mapping
[i
] != index
) iRemapped
++;
1238 TRACE("entry %i (%x) -> pixel %i\n", i
, *(COLORREF
*)&entries
[i
], index
);
1241 LeaveCriticalSection( &palette_cs
);
1246 /***********************************************************************
1247 * UnrealizePalette (X11DRV.@)
1249 BOOL
X11DRV_UnrealizePalette( HPALETTE hpal
)
1251 int *mapping
= palette_get_mapping( hpal
);
1255 XDeleteContext( gdi_display
, (XID
)hpal
, palette_context
);
1256 HeapFree( GetProcessHeap(), 0, mapping
);
1262 /***********************************************************************
1263 * GetSystemPaletteEntries (X11DRV.@)
1265 UINT
X11DRV_GetSystemPaletteEntries( PHYSDEV dev
, UINT start
, UINT count
, LPPALETTEENTRY entries
)
1269 if (!entries
) return palette_size
;
1270 if (start
>= palette_size
) return 0;
1271 if (start
+ count
>= palette_size
) count
= palette_size
- start
;
1273 EnterCriticalSection( &palette_cs
);
1274 for (i
= 0; i
< count
; i
++)
1276 entries
[i
].peRed
= COLOR_sysPal
[start
+ i
].peRed
;
1277 entries
[i
].peGreen
= COLOR_sysPal
[start
+ i
].peGreen
;
1278 entries
[i
].peBlue
= COLOR_sysPal
[start
+ i
].peBlue
;
1279 entries
[i
].peFlags
= 0;
1280 TRACE("\tidx(%02x) -> RGB(%08x)\n", start
+ i
, *(COLORREF
*)(entries
+ i
) );
1282 LeaveCriticalSection( &palette_cs
);
1287 /***********************************************************************
1288 * GetNearestColor (X11DRV.@)
1290 COLORREF
X11DRV_GetNearestColor( PHYSDEV dev
, COLORREF color
)
1292 unsigned char spec_type
= color
>> 24;
1295 if (!palette_size
) return color
;
1297 if (spec_type
== 1 || spec_type
== 2)
1299 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1303 HPALETTE hpal
= GetCurrentObject( dev
->hdc
, OBJ_PAL
);
1305 if (!hpal
) hpal
= GetStockObject( DEFAULT_PALETTE
);
1307 if (spec_type
== 2) /* PALETTERGB */
1308 index
= GetNearestPaletteIndex( hpal
, color
);
1309 else /* PALETTEINDEX */
1310 index
= LOWORD(color
);
1312 if (!GetPaletteEntries( hpal
, index
, 1, &entry
))
1314 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color
, index
);
1315 if (!GetPaletteEntries( hpal
, 0, 1, &entry
)) return CLR_INVALID
;
1317 color
= RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
1319 color
&= 0x00ffffff;
1320 EnterCriticalSection( &palette_cs
);
1321 nearest
= (0x00ffffff & *(COLORREF
*)(COLOR_sysPal
+ X11DRV_SysPaletteLookupPixel(color
, FALSE
)));
1322 LeaveCriticalSection( &palette_cs
);
1324 TRACE("(%06x): returning %06x\n", color
, nearest
);
1329 /***********************************************************************
1330 * RealizeDefaultPalette (X11DRV.@)
1332 UINT
X11DRV_RealizeDefaultPalette( PHYSDEV dev
)
1336 if (palette_size
&& GetObjectType(dev
->hdc
) != OBJ_MEMDC
)
1338 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1339 int i
, index
, *mapping
= palette_get_mapping( GetStockObject(DEFAULT_PALETTE
) );
1340 PALETTEENTRY entries
[NB_RESERVED_COLORS
];
1342 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE
), 0, NB_RESERVED_COLORS
, entries
);
1343 EnterCriticalSection( &palette_cs
);
1344 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
1346 index
= X11DRV_PALETTE_LookupSystemXPixel( RGB(entries
[i
].peRed
,
1348 entries
[i
].peBlue
) );
1349 /* mapping is allocated in COLOR_InitPalette() */
1350 if( index
!= mapping
[i
] )
1356 LeaveCriticalSection( &palette_cs
);