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 static 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_ComputeShifts(unsigned long maskbits
, ChannelShift
*physical
, ChannelShift
*to_logical
);
105 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY
*sys_pal_template
);
106 static void X11DRV_PALETTE_FormatSystemPalette(void);
107 static BOOL
X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY
*sys_pal_template
, COLORREF c
);
108 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
);
111 /***********************************************************************
112 * palette_get_mapping
114 static int *palette_get_mapping( HPALETTE hpal
)
119 if (XFindContext( gdi_display
, (XID
)hpal
, palette_context
, (char **)&mapping
)) mapping
= NULL
;
125 /***********************************************************************
126 * palette_set_mapping
128 static void palette_set_mapping( HPALETTE hpal
, int *mapping
)
131 XSaveContext( gdi_display
, (XID
)hpal
, palette_context
, (char *)mapping
);
136 /***********************************************************************
139 * Initialize color management.
141 int X11DRV_PALETTE_Init(void)
143 int mask
, white
, black
;
146 PALETTEENTRY sys_pal_template
[NB_RESERVED_COLORS
];
148 TRACE("initializing palette manager...\n");
151 palette_context
= XUniqueContext();
153 white
= WhitePixel( gdi_display
, DefaultScreen(gdi_display
) );
154 black
= BlackPixel( gdi_display
, DefaultScreen(gdi_display
) );
156 for( mask
= 1; !((white
& mask
)^(black
& mask
)); mask
<<= 1 )
158 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
159 palette_size
= visual
->map_entries
;
161 switch(visual
->class)
164 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
168 if (private_color_map
)
170 XSetWindowAttributes win_attr
;
172 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap( gdi_display
, root_window
,
174 if (X11DRV_PALETTE_PaletteXColormap
)
176 X11DRV_PALETTE_PaletteFlags
|= (X11DRV_PALETTE_PRIVATE
| X11DRV_PALETTE_WHITESET
);
179 for( white
= palette_size
- 1; !(white
& 1); white
>>= 1 )
182 if( root_window
!= DefaultRootWindow(gdi_display
) )
184 win_attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
;
185 XChangeWindowAttributes( gdi_display
, root_window
, CWColormap
, &win_attr
);
189 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
197 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
199 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
200 X11DRV_PALETTE_Graymax
= (1 << screen_depth
)-1;
205 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
207 int *depths
,nrofdepths
;
208 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
212 depths
= XListDepths(gdi_display
,DefaultScreen(gdi_display
),&nrofdepths
);
213 if ((nrofdepths
==2) && ((depths
[0]==4) || depths
[1]==4)) {
215 for( white
= palette_size
- 1; !(white
& 1); white
>>= 1 )
217 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
218 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
223 X11DRV_PALETTE_PaletteXColormap
= XCreateColormap(gdi_display
, root_window
,
225 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
226 X11DRV_PALETTE_ComputeShifts(visual
->red_mask
, &X11DRV_PALETTE_default_shifts
.physicalRed
, &X11DRV_PALETTE_default_shifts
.logicalRed
);
227 X11DRV_PALETTE_ComputeShifts(visual
->green_mask
, &X11DRV_PALETTE_default_shifts
.physicalGreen
, &X11DRV_PALETTE_default_shifts
.logicalGreen
);
228 X11DRV_PALETTE_ComputeShifts(visual
->blue_mask
, &X11DRV_PALETTE_default_shifts
.physicalBlue
, &X11DRV_PALETTE_default_shifts
.logicalBlue
);
236 TRACE(" visual class %i (%i)\n", visual
->class, monoPlane
);
238 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE
), 0, NB_RESERVED_COLORS
, sys_pal_template
);
240 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
246 if ((mapping
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(int) * NB_RESERVED_COLORS
)))
247 palette_set_mapping( GetStockObject(DEFAULT_PALETTE
), mapping
);
249 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
250 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template
);
252 X11DRV_PALETTE_BuildSharedMap( sys_pal_template
);
254 /* Build free list */
256 if( X11DRV_PALETTE_firstFree
!= -1 )
257 X11DRV_PALETTE_FormatSystemPalette();
259 X11DRV_PALETTE_FillDefaultColors( sys_pal_template
);
260 palette_size
= visual
->map_entries
;
266 /***********************************************************************
267 * X11DRV_PALETTE_Cleanup
269 * Free external colors we grabbed in the FillDefaultPalette()
271 void X11DRV_PALETTE_Cleanup(void)
273 if( COLOR_gapFilled
)
276 XFreeColors(gdi_display
, X11DRV_PALETTE_PaletteXColormap
,
277 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel
+ COLOR_gapStart
),
283 /***********************************************************************
284 * X11DRV_PALETTE_ComputeShifts
286 * Calculate conversion parameters for direct mapped visuals
288 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits
, ChannelShift
*physical
, ChannelShift
*to_logical
)
303 for(i
=0;!(maskbits
&1);i
++)
307 physical
->max
= maskbits
;
309 for(i
=0;maskbits
!=0;i
++)
313 if (physical
->scale
>8)
315 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
316 * So we adjust the shifts to also normalize the color fields to
317 * the Win32 standard of 8 bits per color.
319 to_logical
->shift
=physical
->shift
+(physical
->scale
-8);
321 to_logical
->max
=0xff;
323 to_logical
->shift
=physical
->shift
;
324 to_logical
->scale
=physical
->scale
;
325 to_logical
->max
=physical
->max
;
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(int pixel
)
785 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
788 /* truecolor visual */
790 if (screen_depth
>= 24) return pixel
;
793 /* check for hicolor visuals first */
795 if ( (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) && !X11DRV_PALETTE_Graymax
)
797 color
.red
= (pixel
>> shifts
->logicalRed
.shift
) & shifts
->logicalRed
.max
;
798 if (shifts
->logicalRed
.scale
<8)
799 color
.red
= color
.red
<< (8-shifts
->logicalRed
.scale
) |
800 color
.red
>> (2*shifts
->logicalRed
.scale
-8);
801 color
.green
= (pixel
>> shifts
->logicalGreen
.shift
) & shifts
->logicalGreen
.max
;
802 if (shifts
->logicalGreen
.scale
<8)
803 color
.green
=color
.green
<< (8-shifts
->logicalGreen
.scale
) |
804 color
.green
>> (2*shifts
->logicalGreen
.scale
-8);
805 color
.blue
= (pixel
>> shifts
->logicalBlue
.shift
) & shifts
->logicalBlue
.max
;
806 if (shifts
->logicalBlue
.scale
<8)
807 color
.blue
= color
.blue
<< (8-shifts
->logicalBlue
.scale
) |
808 color
.blue
>> (2*shifts
->logicalBlue
.scale
-8);
809 return RGB(color
.red
,color
.green
,color
.blue
);
812 /* check if we can bypass X */
814 if ((screen_depth
<= 8) && (pixel
< 256) &&
815 !(X11DRV_PALETTE_PaletteFlags
& (X11DRV_PALETTE_VIRTUAL
| X11DRV_PALETTE_FIXED
)) ) {
817 EnterCriticalSection( &palette_cs
);
818 ret
= *(COLORREF
*)(COLOR_sysPal
+ (X11DRV_PALETTE_XPixelToPalette
? X11DRV_PALETTE_XPixelToPalette
[pixel
]: pixel
)) & 0x00ffffff;
819 LeaveCriticalSection( &palette_cs
);
825 XQueryColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
827 return RGB(color
.red
>> 8, color
.green
>> 8, color
.blue
>> 8);
831 /***********************************************************************
832 * X11DRV_SysPaletteLookupPixel
834 static int X11DRV_SysPaletteLookupPixel( COLORREF col
, BOOL skipReserved
)
836 int i
, best
= 0, diff
= 0x7fffffff;
839 for( i
= 0; i
< palette_size
&& diff
; i
++ )
841 if( !(COLOR_sysPal
[i
].peFlags
& PC_SYS_USED
) ||
842 (skipReserved
&& COLOR_sysPal
[i
].peFlags
& PC_SYS_RESERVED
) )
845 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
846 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
847 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
851 if( r
< diff
) { best
= i
; diff
= r
; }
857 static inline BOOL
colour_is_brighter(RGBQUAD c1
, RGBQUAD c2
)
859 return (c1
.rgbRed
* c1
.rgbRed
+ c1
.rgbGreen
* c1
.rgbGreen
+ c1
.rgbBlue
* c1
.rgbBlue
) >
860 (c2
.rgbRed
* c2
.rgbRed
+ c2
.rgbGreen
* c2
.rgbGreen
+ c2
.rgbBlue
* c2
.rgbBlue
);
863 /***********************************************************************
864 * X11DRV_PALETTE_ToPhysical
866 * Return the physical color closest to 'color'.
868 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE
*physDev
, COLORREF color
)
871 HPALETTE hPal
= physDev
? GetCurrentObject(physDev
->hdc
, OBJ_PAL
) : GetStockObject(DEFAULT_PALETTE
);
872 unsigned char spec_type
= color
>> 24;
873 int *mapping
= palette_get_mapping( hPal
);
875 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
877 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
879 /* there is no colormap limitation; we are going to have to compute
880 * the pixel value from the visual information stored earlier
883 unsigned long red
, green
, blue
;
884 unsigned idx
= color
& 0xffff;
889 case 0x10: /* DIBINDEX */
890 if( GetDIBColorTable( physDev
->hdc
, idx
, 1, &quad
) != 1 ) {
891 WARN("DIBINDEX(%x) : idx %d is out of bounds, assuming black\n", color
, idx
);
894 color
= RGB( quad
.rgbRed
, quad
.rgbGreen
, quad
.rgbBlue
);
897 case 1: /* PALETTEINDEX */
898 if (!GetPaletteEntries( hPal
, idx
, 1, &entry
))
900 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color
, idx
);
903 if (mapping
) return mapping
[idx
];
904 color
= RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
909 /* fall through to RGB */
912 if (physDev
&& (physDev
->depth
== 1) )
917 if (GetDIBColorTable( physDev
->hdc
, 0, 2, table
) == 2)
919 if(!colour_is_brighter(table
[1], table
[0])) white
= 0;
921 return (((color
>> 16) & 0xff) +
922 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? white
: 1 - white
;
927 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
929 if (X11DRV_PALETTE_Graymax
)
931 /* grayscale only; return scaled value */
932 return ( (red
* 30 + green
* 59 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
936 /* scale each individually and construct the TrueColor pixel value */
937 if (shifts
->physicalRed
.scale
< 8)
938 red
= red
>> (8-shifts
->physicalRed
.scale
);
939 else if (shifts
->physicalRed
.scale
> 8)
940 red
= red
<< (shifts
->physicalRed
.scale
-8) |
941 red
>> (16-shifts
->physicalRed
.scale
);
942 if (shifts
->physicalGreen
.scale
< 8)
943 green
= green
>> (8-shifts
->physicalGreen
.scale
);
944 else if (shifts
->physicalGreen
.scale
> 8)
945 green
= green
<< (shifts
->physicalGreen
.scale
-8) |
946 green
>> (16-shifts
->physicalGreen
.scale
);
947 if (shifts
->physicalBlue
.scale
< 8)
948 blue
= blue
>> (8-shifts
->physicalBlue
.scale
);
949 else if (shifts
->physicalBlue
.scale
> 8)
950 blue
= blue
<< (shifts
->physicalBlue
.scale
-8) |
951 blue
>> (16-shifts
->physicalBlue
.scale
);
953 return (red
<< shifts
->physicalRed
.shift
) | (green
<< shifts
->physicalGreen
.shift
) | (blue
<< shifts
->physicalBlue
.shift
);
959 WARN("Palette %p is not realized\n", hPal
);
961 switch(spec_type
) /* we have to peruse DC and system palette */
965 /* fall through to RGB */
968 if (physDev
&& (physDev
->depth
== 1) )
973 if (GetDIBColorTable( physDev
->hdc
, 0, 2, table
) == 2)
975 if(!colour_is_brighter(table
[1], table
[0]))
978 return (((color
>> 16) & 0xff) +
979 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? white
: 1 - white
;
982 EnterCriticalSection( &palette_cs
);
983 index
= X11DRV_SysPaletteLookupPixel( color
, FALSE
);
984 if (X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
985 LeaveCriticalSection( &palette_cs
);
987 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
990 case 1: /* PALETTEINDEX */
991 index
= color
& 0xffff;
992 if (!GetPaletteEntries( hPal
, index
, 1, &entry
))
993 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color
, index
);
994 else if (mapping
) index
= mapping
[index
];
996 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
999 case 2: /* PALETTERGB */
1000 index
= GetNearestPaletteIndex( hPal
, color
);
1001 if (mapping
) index
= mapping
[index
];
1002 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
1010 /***********************************************************************
1011 * X11DRV_PALETTE_LookupPixel
1013 int X11DRV_PALETTE_LookupPixel(COLORREF color
)
1015 unsigned char spec_type
= color
>> 24;
1017 /* Only accept RGB which has spec_type = 0 */
1023 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
1025 unsigned long red
, green
, blue
;
1026 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
1028 if (X11DRV_PALETTE_Graymax
)
1030 /* grayscale only; return scaled value */
1031 return ( (red
* 30 + green
* 59 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
1035 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
1036 /* scale each individually and construct the TrueColor pixel value */
1037 if (shifts
->physicalRed
.scale
< 8)
1038 red
= red
>> (8-shifts
->physicalRed
.scale
);
1039 else if (shifts
->physicalRed
.scale
> 8)
1040 red
= red
<< (shifts
->physicalRed
.scale
-8) |
1041 red
>> (16-shifts
->physicalRed
.scale
);
1042 if (shifts
->physicalGreen
.scale
< 8)
1043 green
= green
>> (8-shifts
->physicalGreen
.scale
);
1044 else if (shifts
->physicalGreen
.scale
> 8)
1045 green
= green
<< (shifts
->physicalGreen
.scale
-8) |
1046 green
>> (16-shifts
->physicalGreen
.scale
);
1047 if (shifts
->physicalBlue
.scale
< 8)
1048 blue
= blue
>> (8-shifts
->physicalBlue
.scale
);
1049 else if (shifts
->physicalBlue
.scale
> 8)
1050 blue
= blue
<< (shifts
->physicalBlue
.scale
-8) |
1051 blue
>> (16-shifts
->physicalBlue
.scale
);
1053 return (red
<< shifts
->physicalRed
.shift
) | (green
<< shifts
->physicalGreen
.shift
) | (blue
<< shifts
->physicalBlue
.shift
);
1059 HPALETTE hPal
= GetStockObject(DEFAULT_PALETTE
);
1060 int *mapping
= palette_get_mapping( hPal
);
1063 WARN("Palette %p is not realized\n", hPal
);
1065 EnterCriticalSection( &palette_cs
);
1066 index
= X11DRV_SysPaletteLookupPixel( color
, FALSE
);
1067 if (X11DRV_PALETTE_PaletteToXPixel
)
1068 index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1069 LeaveCriticalSection( &palette_cs
);
1075 /***********************************************************************
1076 * X11DRV_PALETTE_LookupSystemXPixel
1078 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
)
1080 int i
, best
= 0, diff
= 0x7fffffff;
1081 int size
= palette_size
;
1084 for( i
= 0; i
< size
&& diff
; i
++ )
1086 if( i
== NB_RESERVED_COLORS
/2 )
1088 int newi
= size
- NB_RESERVED_COLORS
/2;
1092 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
1093 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
1094 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
1096 r
= r
*r
+ g
*g
+ b
*b
;
1098 if( r
< diff
) { best
= i
; diff
= r
; }
1101 return (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[best
] : best
;
1104 /***********************************************************************
1105 * X11DRV_PALETTE_FormatSystemPalette
1107 static void X11DRV_PALETTE_FormatSystemPalette(void)
1109 /* Build free list so we'd have an easy way to find
1110 * out if there are any available colorcells.
1113 int i
, j
= X11DRV_PALETTE_firstFree
= NB_RESERVED_COLORS
/2;
1115 COLOR_sysPal
[j
].peFlags
= 0;
1116 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
1117 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
1119 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
1120 X11DRV_PALETTE_freeList
[j
] = i
; /* next */
1123 X11DRV_PALETTE_freeList
[j
] = 0;
1126 /***********************************************************************
1127 * X11DRV_PALETTE_CheckSysColor
1129 static BOOL
X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY
*sys_pal_template
, COLORREF c
)
1132 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
1133 if( c
== (*(const COLORREF
*)(sys_pal_template
+ i
) & 0x00ffffff) )
1139 /***********************************************************************
1140 * X11DRV_LookupSysPaletteExact
1142 static int X11DRV_LookupSysPaletteExact( BYTE r
, BYTE g
, BYTE b
)
1145 for( i
= 0; i
< palette_size
; i
++ )
1147 if( COLOR_sysPal
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
1148 if( COLOR_sysPal
[i
].peRed
== r
&&
1149 COLOR_sysPal
[i
].peGreen
== g
&&
1150 COLOR_sysPal
[i
].peBlue
== b
)
1157 /***********************************************************************
1158 * RealizePalette (X11DRV.@)
1160 UINT
X11DRV_RealizePalette( X11DRV_PDEVICE
*physDev
, HPALETTE hpal
, BOOL primary
)
1164 UINT i
, iRemapped
= 0;
1165 int *prev_mapping
, *mapping
;
1166 PALETTEENTRY entries
[256];
1169 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
) return 0;
1171 if (!GetObjectW( hpal
, sizeof(num_entries
), &num_entries
)) return 0;
1173 /* initialize palette mapping table */
1174 prev_mapping
= palette_get_mapping( hpal
);
1176 mapping
= HeapReAlloc( GetProcessHeap(), 0, prev_mapping
, sizeof(int)*num_entries
);
1178 mapping
= HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries
);
1180 if(mapping
== NULL
) {
1181 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1184 palette_set_mapping( hpal
, mapping
);
1186 if (num_entries
> 256)
1188 FIXME( "more than 256 entries not supported\n" );
1191 if (!(num_entries
= GetPaletteEntries( hpal
, 0, num_entries
, entries
))) return 0;
1193 /* reset dynamic system palette entries */
1195 EnterCriticalSection( &palette_cs
);
1196 if( primary
&& X11DRV_PALETTE_firstFree
!= -1)
1197 X11DRV_PALETTE_FormatSystemPalette();
1199 for (i
= 0; i
< num_entries
; i
++)
1204 /* Even though the docs say that only one flag is to be set,
1205 * they are a bitmask. At least one app sets more than one at
1207 if ( entries
[i
].peFlags
& PC_EXPLICIT
) {
1208 /* palette entries are indices into system palette */
1209 index
= *(WORD
*)&entries
[i
];
1210 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
1212 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
1215 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1217 if ( entries
[i
].peFlags
& PC_RESERVED
) {
1218 /* forbid future mappings to this entry */
1219 flag
|= PC_SYS_RESERVED
;
1222 if (! (entries
[i
].peFlags
& PC_NOCOLLAPSE
) ) {
1223 /* try to collapse identical colors */
1224 index
= X11DRV_LookupSysPaletteExact( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
);
1229 if( X11DRV_PALETTE_firstFree
> 0 )
1232 index
= X11DRV_PALETTE_firstFree
; /* ought to be available */
1233 X11DRV_PALETTE_firstFree
= X11DRV_PALETTE_freeList
[index
];
1235 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
) ? X11DRV_PALETTE_PaletteToXPixel
[index
] : index
;
1236 color
.red
= entries
[i
].peRed
<< 8;
1237 color
.green
= entries
[i
].peGreen
<< 8;
1238 color
.blue
= entries
[i
].peBlue
<< 8;
1239 color
.flags
= DoRed
| DoGreen
| DoBlue
;
1241 XStoreColor(gdi_display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
1242 wine_tsx11_unlock();
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( 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
);
1285 XDeleteContext( gdi_display
, (XID
)hpal
, palette_context
);
1286 wine_tsx11_unlock();
1287 HeapFree( GetProcessHeap(), 0, mapping
);
1293 /***********************************************************************
1294 * GetSystemPaletteEntries (X11DRV.@)
1296 UINT
X11DRV_GetSystemPaletteEntries( X11DRV_PDEVICE
*physDev
, UINT start
, UINT count
,
1297 LPPALETTEENTRY entries
)
1301 if (!entries
) return palette_size
;
1302 if (start
>= palette_size
) return 0;
1303 if (start
+ count
>= palette_size
) count
= palette_size
- start
;
1305 EnterCriticalSection( &palette_cs
);
1306 for (i
= 0; i
< count
; i
++)
1308 entries
[i
].peRed
= COLOR_sysPal
[start
+ i
].peRed
;
1309 entries
[i
].peGreen
= COLOR_sysPal
[start
+ i
].peGreen
;
1310 entries
[i
].peBlue
= COLOR_sysPal
[start
+ i
].peBlue
;
1311 entries
[i
].peFlags
= 0;
1312 TRACE("\tidx(%02x) -> RGB(%08x)\n", start
+ i
, *(COLORREF
*)(entries
+ i
) );
1314 LeaveCriticalSection( &palette_cs
);
1319 /***********************************************************************
1320 * GetNearestColor (X11DRV.@)
1322 COLORREF
X11DRV_GetNearestColor( X11DRV_PDEVICE
*physDev
, COLORREF color
)
1324 unsigned char spec_type
= color
>> 24;
1327 if (!palette_size
) return color
;
1329 if (spec_type
== 1 || spec_type
== 2)
1331 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1335 HPALETTE hpal
= GetCurrentObject( physDev
->hdc
, OBJ_PAL
);
1337 if (!hpal
) hpal
= GetStockObject( DEFAULT_PALETTE
);
1339 if (spec_type
== 2) /* PALETTERGB */
1340 index
= GetNearestPaletteIndex( hpal
, color
);
1341 else /* PALETTEINDEX */
1342 index
= LOWORD(color
);
1344 if (!GetPaletteEntries( hpal
, index
, 1, &entry
))
1346 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color
, index
);
1347 if (!GetPaletteEntries( hpal
, 0, 1, &entry
)) return CLR_INVALID
;
1349 color
= RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
1351 color
&= 0x00ffffff;
1352 EnterCriticalSection( &palette_cs
);
1353 nearest
= (0x00ffffff & *(COLORREF
*)(COLOR_sysPal
+ X11DRV_SysPaletteLookupPixel(color
, FALSE
)));
1354 LeaveCriticalSection( &palette_cs
);
1356 TRACE("(%06x): returning %06x\n", color
, nearest
);
1361 /***********************************************************************
1362 * RealizeDefaultPalette (X11DRV.@)
1364 UINT
X11DRV_RealizeDefaultPalette( X11DRV_PDEVICE
*physDev
)
1368 if (palette_size
&& GetObjectType(physDev
->hdc
) != OBJ_MEMDC
)
1370 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1371 int i
, index
, *mapping
= palette_get_mapping( GetStockObject(DEFAULT_PALETTE
) );
1372 PALETTEENTRY entries
[NB_RESERVED_COLORS
];
1374 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE
), 0, NB_RESERVED_COLORS
, entries
);
1375 EnterCriticalSection( &palette_cs
);
1376 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
1378 index
= X11DRV_PALETTE_LookupSystemXPixel( RGB(entries
[i
].peRed
,
1380 entries
[i
].peBlue
) );
1381 /* mapping is allocated in COLOR_InitPalette() */
1382 if( index
!= mapping
[i
] )
1388 LeaveCriticalSection( &palette_cs
);