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
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(palette
);
38 /* Palette indexed mode:
39 * logical palette -> mapping -> pixel
42 * Windows needs contiguous color space ( from 0 to n ) but
43 * it is possible only with the private colormap. Otherwise we
44 * have to map DC palette indices to real pixel values. With
45 * private colormaps it boils down to the identity mapping. The
46 * other special case is when we have a fixed color visual with
47 * the screendepth > 8 - we abandon palette mappings altogether
48 * because pixel values can be calculated without X server
52 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
54 #define PC_SYS_USED 0x80 /* palentry is used (both system and logical) */
55 #define PC_SYS_RESERVED 0x40 /* system palentry is not to be mapped to */
57 static PALETTEENTRY
*COLOR_sysPal
; /* current system palette */
59 static int COLOR_gapStart
= 256;
60 static int COLOR_gapEnd
= -1;
62 UINT16 X11DRV_PALETTE_PaletteFlags
= 0;
64 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
65 ColorShifts X11DRV_PALETTE_default_shifts
= { {0,0,0,}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
66 static int X11DRV_PALETTE_Graymax
= 0;
68 static int palette_size
;
70 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
71 static int X11DRV_PALETTE_firstFree
= 0;
72 static unsigned char X11DRV_PALETTE_freeList
[256];
74 static XContext palette_context
; /* X context to associate a color mapping to a palette */
76 static pthread_mutex_t palette_mutex
= PTHREAD_MUTEX_INITIALIZER
;
78 /**********************************************************************/
80 /* Map an EGA index (0..15) to a pixel value in the system color space. */
82 int X11DRV_PALETTE_mapEGAPixel
[16];
84 /**********************************************************************/
86 #define NB_COLORCUBE_START_INDEX 63
87 #define NB_PALETTE_EMPTY_VALUE -1
89 /* Maps entry in the system palette to X pixel value */
90 int *X11DRV_PALETTE_PaletteToXPixel
= NULL
;
92 /* Maps pixel to the entry in the system palette */
93 int *X11DRV_PALETTE_XPixelToPalette
= NULL
;
95 /**********************************************************************/
97 static BOOL
X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY
*sys_pal_template
);
98 static BOOL
X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY
*sys_pal_template
);
99 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY
*sys_pal_template
);
100 static void X11DRV_PALETTE_FormatSystemPalette(void);
101 static BOOL
X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY
*sys_pal_template
, COLORREF c
);
102 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
);
105 /***********************************************************************
106 * palette_get_mapping
108 static int *palette_get_mapping( HPALETTE hpal
)
112 if (XFindContext( gdi_display
, (XID
)hpal
, palette_context
, (char **)&mapping
)) mapping
= NULL
;
116 /* copied from gdi32 */
117 static const RGBQUAD
*get_default_color_table( int bpp
)
119 static const RGBQUAD table_1
[2] =
121 { 0x00, 0x00, 0x00 }, { 0xff, 0xff, 0xff }
123 static const RGBQUAD table_4
[16] =
125 { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x80 }, { 0x00, 0x80, 0x00 }, { 0x00, 0x80, 0x80 },
126 { 0x80, 0x00, 0x00 }, { 0x80, 0x00, 0x80 }, { 0x80, 0x80, 0x00 }, { 0x80, 0x80, 0x80 },
127 { 0xc0, 0xc0, 0xc0 }, { 0x00, 0x00, 0xff }, { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0xff },
128 { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0xff }, { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0xff },
130 static const RGBQUAD table_8
[256] =
132 /* first and last 10 entries are the default system palette entries */
133 { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x80 }, { 0x00, 0x80, 0x00 }, { 0x00, 0x80, 0x80 },
134 { 0x80, 0x00, 0x00 }, { 0x80, 0x00, 0x80 }, { 0x80, 0x80, 0x00 }, { 0xc0, 0xc0, 0xc0 },
135 { 0xc0, 0xdc, 0xc0 }, { 0xf0, 0xca, 0xa6 }, { 0x00, 0x20, 0x40 }, { 0x00, 0x20, 0x60 },
136 { 0x00, 0x20, 0x80 }, { 0x00, 0x20, 0xa0 }, { 0x00, 0x20, 0xc0 }, { 0x00, 0x20, 0xe0 },
137 { 0x00, 0x40, 0x00 }, { 0x00, 0x40, 0x20 }, { 0x00, 0x40, 0x40 }, { 0x00, 0x40, 0x60 },
138 { 0x00, 0x40, 0x80 }, { 0x00, 0x40, 0xa0 }, { 0x00, 0x40, 0xc0 }, { 0x00, 0x40, 0xe0 },
139 { 0x00, 0x60, 0x00 }, { 0x00, 0x60, 0x20 }, { 0x00, 0x60, 0x40 }, { 0x00, 0x60, 0x60 },
140 { 0x00, 0x60, 0x80 }, { 0x00, 0x60, 0xa0 }, { 0x00, 0x60, 0xc0 }, { 0x00, 0x60, 0xe0 },
141 { 0x00, 0x80, 0x00 }, { 0x00, 0x80, 0x20 }, { 0x00, 0x80, 0x40 }, { 0x00, 0x80, 0x60 },
142 { 0x00, 0x80, 0x80 }, { 0x00, 0x80, 0xa0 }, { 0x00, 0x80, 0xc0 }, { 0x00, 0x80, 0xe0 },
143 { 0x00, 0xa0, 0x00 }, { 0x00, 0xa0, 0x20 }, { 0x00, 0xa0, 0x40 }, { 0x00, 0xa0, 0x60 },
144 { 0x00, 0xa0, 0x80 }, { 0x00, 0xa0, 0xa0 }, { 0x00, 0xa0, 0xc0 }, { 0x00, 0xa0, 0xe0 },
145 { 0x00, 0xc0, 0x00 }, { 0x00, 0xc0, 0x20 }, { 0x00, 0xc0, 0x40 }, { 0x00, 0xc0, 0x60 },
146 { 0x00, 0xc0, 0x80 }, { 0x00, 0xc0, 0xa0 }, { 0x00, 0xc0, 0xc0 }, { 0x00, 0xc0, 0xe0 },
147 { 0x00, 0xe0, 0x00 }, { 0x00, 0xe0, 0x20 }, { 0x00, 0xe0, 0x40 }, { 0x00, 0xe0, 0x60 },
148 { 0x00, 0xe0, 0x80 }, { 0x00, 0xe0, 0xa0 }, { 0x00, 0xe0, 0xc0 }, { 0x00, 0xe0, 0xe0 },
149 { 0x40, 0x00, 0x00 }, { 0x40, 0x00, 0x20 }, { 0x40, 0x00, 0x40 }, { 0x40, 0x00, 0x60 },
150 { 0x40, 0x00, 0x80 }, { 0x40, 0x00, 0xa0 }, { 0x40, 0x00, 0xc0 }, { 0x40, 0x00, 0xe0 },
151 { 0x40, 0x20, 0x00 }, { 0x40, 0x20, 0x20 }, { 0x40, 0x20, 0x40 }, { 0x40, 0x20, 0x60 },
152 { 0x40, 0x20, 0x80 }, { 0x40, 0x20, 0xa0 }, { 0x40, 0x20, 0xc0 }, { 0x40, 0x20, 0xe0 },
153 { 0x40, 0x40, 0x00 }, { 0x40, 0x40, 0x20 }, { 0x40, 0x40, 0x40 }, { 0x40, 0x40, 0x60 },
154 { 0x40, 0x40, 0x80 }, { 0x40, 0x40, 0xa0 }, { 0x40, 0x40, 0xc0 }, { 0x40, 0x40, 0xe0 },
155 { 0x40, 0x60, 0x00 }, { 0x40, 0x60, 0x20 }, { 0x40, 0x60, 0x40 }, { 0x40, 0x60, 0x60 },
156 { 0x40, 0x60, 0x80 }, { 0x40, 0x60, 0xa0 }, { 0x40, 0x60, 0xc0 }, { 0x40, 0x60, 0xe0 },
157 { 0x40, 0x80, 0x00 }, { 0x40, 0x80, 0x20 }, { 0x40, 0x80, 0x40 }, { 0x40, 0x80, 0x60 },
158 { 0x40, 0x80, 0x80 }, { 0x40, 0x80, 0xa0 }, { 0x40, 0x80, 0xc0 }, { 0x40, 0x80, 0xe0 },
159 { 0x40, 0xa0, 0x00 }, { 0x40, 0xa0, 0x20 }, { 0x40, 0xa0, 0x40 }, { 0x40, 0xa0, 0x60 },
160 { 0x40, 0xa0, 0x80 }, { 0x40, 0xa0, 0xa0 }, { 0x40, 0xa0, 0xc0 }, { 0x40, 0xa0, 0xe0 },
161 { 0x40, 0xc0, 0x00 }, { 0x40, 0xc0, 0x20 }, { 0x40, 0xc0, 0x40 }, { 0x40, 0xc0, 0x60 },
162 { 0x40, 0xc0, 0x80 }, { 0x40, 0xc0, 0xa0 }, { 0x40, 0xc0, 0xc0 }, { 0x40, 0xc0, 0xe0 },
163 { 0x40, 0xe0, 0x00 }, { 0x40, 0xe0, 0x20 }, { 0x40, 0xe0, 0x40 }, { 0x40, 0xe0, 0x60 },
164 { 0x40, 0xe0, 0x80 }, { 0x40, 0xe0, 0xa0 }, { 0x40, 0xe0, 0xc0 }, { 0x40, 0xe0, 0xe0 },
165 { 0x80, 0x00, 0x00 }, { 0x80, 0x00, 0x20 }, { 0x80, 0x00, 0x40 }, { 0x80, 0x00, 0x60 },
166 { 0x80, 0x00, 0x80 }, { 0x80, 0x00, 0xa0 }, { 0x80, 0x00, 0xc0 }, { 0x80, 0x00, 0xe0 },
167 { 0x80, 0x20, 0x00 }, { 0x80, 0x20, 0x20 }, { 0x80, 0x20, 0x40 }, { 0x80, 0x20, 0x60 },
168 { 0x80, 0x20, 0x80 }, { 0x80, 0x20, 0xa0 }, { 0x80, 0x20, 0xc0 }, { 0x80, 0x20, 0xe0 },
169 { 0x80, 0x40, 0x00 }, { 0x80, 0x40, 0x20 }, { 0x80, 0x40, 0x40 }, { 0x80, 0x40, 0x60 },
170 { 0x80, 0x40, 0x80 }, { 0x80, 0x40, 0xa0 }, { 0x80, 0x40, 0xc0 }, { 0x80, 0x40, 0xe0 },
171 { 0x80, 0x60, 0x00 }, { 0x80, 0x60, 0x20 }, { 0x80, 0x60, 0x40 }, { 0x80, 0x60, 0x60 },
172 { 0x80, 0x60, 0x80 }, { 0x80, 0x60, 0xa0 }, { 0x80, 0x60, 0xc0 }, { 0x80, 0x60, 0xe0 },
173 { 0x80, 0x80, 0x00 }, { 0x80, 0x80, 0x20 }, { 0x80, 0x80, 0x40 }, { 0x80, 0x80, 0x60 },
174 { 0x80, 0x80, 0x80 }, { 0x80, 0x80, 0xa0 }, { 0x80, 0x80, 0xc0 }, { 0x80, 0x80, 0xe0 },
175 { 0x80, 0xa0, 0x00 }, { 0x80, 0xa0, 0x20 }, { 0x80, 0xa0, 0x40 }, { 0x80, 0xa0, 0x60 },
176 { 0x80, 0xa0, 0x80 }, { 0x80, 0xa0, 0xa0 }, { 0x80, 0xa0, 0xc0 }, { 0x80, 0xa0, 0xe0 },
177 { 0x80, 0xc0, 0x00 }, { 0x80, 0xc0, 0x20 }, { 0x80, 0xc0, 0x40 }, { 0x80, 0xc0, 0x60 },
178 { 0x80, 0xc0, 0x80 }, { 0x80, 0xc0, 0xa0 }, { 0x80, 0xc0, 0xc0 }, { 0x80, 0xc0, 0xe0 },
179 { 0x80, 0xe0, 0x00 }, { 0x80, 0xe0, 0x20 }, { 0x80, 0xe0, 0x40 }, { 0x80, 0xe0, 0x60 },
180 { 0x80, 0xe0, 0x80 }, { 0x80, 0xe0, 0xa0 }, { 0x80, 0xe0, 0xc0 }, { 0x80, 0xe0, 0xe0 },
181 { 0xc0, 0x00, 0x00 }, { 0xc0, 0x00, 0x20 }, { 0xc0, 0x00, 0x40 }, { 0xc0, 0x00, 0x60 },
182 { 0xc0, 0x00, 0x80 }, { 0xc0, 0x00, 0xa0 }, { 0xc0, 0x00, 0xc0 }, { 0xc0, 0x00, 0xe0 },
183 { 0xc0, 0x20, 0x00 }, { 0xc0, 0x20, 0x20 }, { 0xc0, 0x20, 0x40 }, { 0xc0, 0x20, 0x60 },
184 { 0xc0, 0x20, 0x80 }, { 0xc0, 0x20, 0xa0 }, { 0xc0, 0x20, 0xc0 }, { 0xc0, 0x20, 0xe0 },
185 { 0xc0, 0x40, 0x00 }, { 0xc0, 0x40, 0x20 }, { 0xc0, 0x40, 0x40 }, { 0xc0, 0x40, 0x60 },
186 { 0xc0, 0x40, 0x80 }, { 0xc0, 0x40, 0xa0 }, { 0xc0, 0x40, 0xc0 }, { 0xc0, 0x40, 0xe0 },
187 { 0xc0, 0x60, 0x00 }, { 0xc0, 0x60, 0x20 }, { 0xc0, 0x60, 0x40 }, { 0xc0, 0x60, 0x60 },
188 { 0xc0, 0x60, 0x80 }, { 0xc0, 0x60, 0xa0 }, { 0xc0, 0x60, 0xc0 }, { 0xc0, 0x60, 0xe0 },
189 { 0xc0, 0x80, 0x00 }, { 0xc0, 0x80, 0x20 }, { 0xc0, 0x80, 0x40 }, { 0xc0, 0x80, 0x60 },
190 { 0xc0, 0x80, 0x80 }, { 0xc0, 0x80, 0xa0 }, { 0xc0, 0x80, 0xc0 }, { 0xc0, 0x80, 0xe0 },
191 { 0xc0, 0xa0, 0x00 }, { 0xc0, 0xa0, 0x20 }, { 0xc0, 0xa0, 0x40 }, { 0xc0, 0xa0, 0x60 },
192 { 0xc0, 0xa0, 0x80 }, { 0xc0, 0xa0, 0xa0 }, { 0xc0, 0xa0, 0xc0 }, { 0xc0, 0xa0, 0xe0 },
193 { 0xc0, 0xc0, 0x00 }, { 0xc0, 0xc0, 0x20 }, { 0xc0, 0xc0, 0x40 }, { 0xc0, 0xc0, 0x60 },
194 { 0xc0, 0xc0, 0x80 }, { 0xc0, 0xc0, 0xa0 }, { 0xf0, 0xfb, 0xff }, { 0xa4, 0xa0, 0xa0 },
195 { 0x80, 0x80, 0x80 }, { 0x00, 0x00, 0xff }, { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0xff },
196 { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0xff }, { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0xff },
201 case 1: return table_1
;
202 case 4: return table_4
;
203 case 8: return table_8
;
204 default: return NULL
;
208 /***********************************************************************
209 * palette_set_mapping
211 static void palette_set_mapping( HPALETTE hpal
, int *mapping
)
213 XSaveContext( gdi_display
, (XID
)hpal
, palette_context
, (char *)mapping
);
217 /***********************************************************************
218 * X11DRV_PALETTE_ComputeChannelShift
220 * Calculate conversion parameters for a given color mask
222 static void X11DRV_PALETTE_ComputeChannelShift(unsigned long maskbits
, ChannelShift
*physical
, ChannelShift
*to_logical
)
237 for(i
=0;!(maskbits
&1);i
++)
241 physical
->max
= maskbits
;
243 for(i
=0;maskbits
!=0;i
++)
247 if (physical
->scale
>8)
249 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
250 * So we adjust the shifts to also normalize the color fields to
251 * the Win32 standard of 8 bits per color.
253 to_logical
->shift
=physical
->shift
+(physical
->scale
-8);
255 to_logical
->max
=0xff;
257 to_logical
->shift
=physical
->shift
;
258 to_logical
->scale
=physical
->scale
;
259 to_logical
->max
=physical
->max
;
263 /***********************************************************************
264 * X11DRV_PALETTE_ComputeColorShifts
266 * Calculate conversion parameters for a given color
268 static void X11DRV_PALETTE_ComputeColorShifts(ColorShifts
*shifts
, unsigned long redMask
, unsigned long greenMask
, unsigned long blueMask
)
270 X11DRV_PALETTE_ComputeChannelShift(redMask
, &shifts
->physicalRed
, &shifts
->logicalRed
);
271 X11DRV_PALETTE_ComputeChannelShift(greenMask
, &shifts
->physicalGreen
, &shifts
->logicalGreen
);
272 X11DRV_PALETTE_ComputeChannelShift(blueMask
, &shifts
->physicalBlue
, &shifts
->logicalBlue
);
275 /***********************************************************************
278 * Initialize color management.
280 int X11DRV_PALETTE_Init(void)
283 PALETTEENTRY sys_pal_template
[NB_RESERVED_COLORS
];
285 TRACE("initializing palette manager...\n");
287 palette_context
= XUniqueContext();
288 palette_size
= default_visual
.colormap_size
;
290 switch(default_visual
.class)
293 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
297 if (private_color_map
)
299 XSetWindowAttributes win_attr
;
301 XFreeColormap( gdi_display
, default_colormap
);
302 default_colormap
= XCreateColormap( gdi_display
, root_window
,
303 default_visual
.visual
, AllocAll
);
304 if (default_colormap
)
306 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_PRIVATE
;
308 if (is_virtual_desktop())
310 win_attr
.colormap
= default_colormap
;
311 XChangeWindowAttributes( gdi_display
, root_window
, CWColormap
, &win_attr
);
318 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
319 X11DRV_PALETTE_Graymax
= (1 << default_visual
.depth
)-1;
323 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
326 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
327 X11DRV_PALETTE_ComputeColorShifts(&X11DRV_PALETTE_default_shifts
, default_visual
.red_mask
, default_visual
.green_mask
, default_visual
.blue_mask
);
331 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
337 get_palette_entries( GetStockObject(DEFAULT_PALETTE
), 0, NB_RESERVED_COLORS
, sys_pal_template
);
339 if ((mapping
= calloc( 1, sizeof(int) * NB_RESERVED_COLORS
)))
340 palette_set_mapping( GetStockObject(DEFAULT_PALETTE
), mapping
);
342 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
343 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template
);
345 X11DRV_PALETTE_BuildSharedMap( sys_pal_template
);
347 /* Build free list */
349 if( X11DRV_PALETTE_firstFree
!= -1 )
350 X11DRV_PALETTE_FormatSystemPalette();
352 X11DRV_PALETTE_FillDefaultColors( sys_pal_template
);
353 palette_size
= default_visual
.colormap_size
;
359 /***********************************************************************
360 * X11DRV_PALETTE_BuildPrivateMap
362 * Allocate colorcells and initialize mapping tables.
364 static BOOL
X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY
*sys_pal_template
)
366 /* Private colormap - identity mapping */
371 if((COLOR_sysPal
= malloc( sizeof(PALETTEENTRY
) * palette_size
)) == NULL
)
373 WARN("Unable to allocate the system palette\n");
377 TRACE("Building private map - %i palette entries\n", palette_size
);
379 /* Allocate system palette colors */
381 for( i
=0; i
< palette_size
; i
++ )
383 if( i
< NB_RESERVED_COLORS
/2 )
385 color
.red
= sys_pal_template
[i
].peRed
* (65535 / 255);
386 color
.green
= sys_pal_template
[i
].peGreen
* (65535 / 255);
387 color
.blue
= sys_pal_template
[i
].peBlue
* (65535 / 255);
388 COLOR_sysPal
[i
] = sys_pal_template
[i
];
389 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
391 else if( i
>= palette_size
- NB_RESERVED_COLORS
/2 )
393 int j
= NB_RESERVED_COLORS
+ i
- palette_size
;
394 color
.red
= sys_pal_template
[j
].peRed
* (65535 / 255);
395 color
.green
= sys_pal_template
[j
].peGreen
* (65535 / 255);
396 color
.blue
= sys_pal_template
[j
].peBlue
* (65535 / 255);
397 COLOR_sysPal
[i
] = sys_pal_template
[j
];
398 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
401 color
.flags
= DoRed
| DoGreen
| DoBlue
;
403 XStoreColor(gdi_display
, default_colormap
, &color
);
405 /* Set EGA mapping if color is from the first or last eight */
408 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
409 else if (i
>= palette_size
- 8 )
410 X11DRV_PALETTE_mapEGAPixel
[i
- (palette_size
- 16)] = color
.pixel
;
413 X11DRV_PALETTE_XPixelToPalette
= X11DRV_PALETTE_PaletteToXPixel
= NULL
;
415 COLOR_gapStart
= 256; COLOR_gapEnd
= -1;
417 X11DRV_PALETTE_firstFree
= (palette_size
> NB_RESERVED_COLORS
)?NB_RESERVED_COLORS
/2 : -1;
422 /***********************************************************************
423 * X11DRV_PALETTE_BuildSharedMap
425 * Allocate colorcells and initialize mapping tables.
427 static BOOL
X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY
*sys_pal_template
)
430 unsigned long sysPixel
[NB_RESERVED_COLORS
];
431 unsigned long* pixDynMapping
= NULL
;
432 unsigned long plane_masks
[1];
434 int diff
, r
, g
, b
, bp
= 0, wp
= 1;
436 unsigned int max
= 256;
438 XColor defaultColors
[256];
440 /* Copy the first bunch of colors out of the default colormap to prevent
441 * colormap flashing as much as possible. We're likely to get the most
442 * important Window Manager colors, etc in the first 128 colors */
443 defaultCM
= DefaultColormap( gdi_display
, DefaultScreen(gdi_display
) );
445 if (copy_default_colors
> 256) copy_default_colors
= 256;
446 for (i
= 0; i
< copy_default_colors
; i
++)
447 defaultColors
[i
].pixel
= (long) i
;
448 XQueryColors(gdi_display
, defaultCM
, &defaultColors
[0], copy_default_colors
);
449 for (i
= 0; i
< copy_default_colors
; i
++)
450 XAllocColor( gdi_display
, default_colormap
, &defaultColors
[i
] );
452 if (alloc_system_colors
> 256) alloc_system_colors
= 256;
453 else if (alloc_system_colors
< 20) alloc_system_colors
= 20;
454 TRACE("%d colors configured.\n", alloc_system_colors
);
456 TRACE("Building shared map - %i palette entries\n", palette_size
);
458 /* Be nice and allocate system colors as read-only */
460 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
462 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
463 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
464 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
465 color
.flags
= DoRed
| DoGreen
| DoBlue
;
467 if (!XAllocColor( gdi_display
, default_colormap
, &color
))
473 WARN("Not enough colors for the full system palette.\n");
475 bp
= BlackPixel(gdi_display
, DefaultScreen(gdi_display
));
476 wp
= WhitePixel(gdi_display
, DefaultScreen(gdi_display
));
478 max
= 0xffffffff >> (32 - default_visual
.depth
);
486 /* reinit color (XAllocColor() may change it)
487 * and map to the best shared colorcell */
489 color
.red
= sys_pal_template
[i
].peRed
* 65535 / 255;
490 color
.green
= sys_pal_template
[i
].peGreen
* 65535 / 255;
491 color
.blue
= sys_pal_template
[i
].peBlue
* 65535 / 255;
493 best
.pixel
= best
.red
= best
.green
= best
.blue
= 0;
494 for( c
.pixel
= 0, diff
= 0x7fffffff; c
.pixel
< max
; c
.pixel
+= step
)
496 XQueryColor(gdi_display
, default_colormap
, &c
);
497 r
= (c
.red
- color
.red
)>>8;
498 g
= (c
.green
- color
.green
)>>8;
499 b
= (c
.blue
- color
.blue
)>>8;
501 if( r
< diff
) { best
= c
; diff
= r
; }
504 if( XAllocColor(gdi_display
, default_colormap
, &best
) )
505 color
.pixel
= best
.pixel
;
506 else color
.pixel
= (i
< NB_RESERVED_COLORS
/2)? bp
: wp
;
509 sysPixel
[i
] = color
.pixel
;
511 TRACE("syscolor %s -> pixel %i\n", debugstr_color(*(const COLORREF
*)(sys_pal_template
+i
)),
514 /* Set EGA mapping if color in the first or last eight */
517 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
518 else if (i
>= NB_RESERVED_COLORS
- 8 )
519 X11DRV_PALETTE_mapEGAPixel
[i
- (NB_RESERVED_COLORS
-16)] = color
.pixel
;
522 /* now allocate changeable set */
524 if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
526 int c_min
= 0, c_max
= palette_size
, c_val
;
528 TRACE("Dynamic colormap...\n");
530 /* let's become the first client that actually follows
531 * X guidelines and does binary search...
534 if (!(pixDynMapping
= malloc( sizeof(*pixDynMapping
) * palette_size
)))
536 WARN("Out of memory while building system palette.\n");
540 /* comment this out if you want to debug palette init */
541 XGrabServer(gdi_display
);
543 while( c_max
- c_min
> 0 )
545 c_val
= (c_max
+ c_min
)/2 + (c_max
+ c_min
)%2;
547 if( !XAllocColorCells(gdi_display
, default_colormap
, False
,
548 plane_masks
, 0, pixDynMapping
, c_val
) )
552 XFreeColors(gdi_display
, default_colormap
, pixDynMapping
, c_val
, 0);
557 if( c_min
> alloc_system_colors
- NB_RESERVED_COLORS
)
558 c_min
= alloc_system_colors
- NB_RESERVED_COLORS
;
560 c_min
= (c_min
/2) + (c_min
/2); /* need even set for split palette */
563 if( !XAllocColorCells(gdi_display
, default_colormap
, False
,
564 plane_masks
, 0, pixDynMapping
, c_min
) )
566 WARN("Inexplicable failure during colorcell allocation.\n");
570 palette_size
= c_min
+ NB_RESERVED_COLORS
;
572 XUngrabServer(gdi_display
);
575 TRACE("adjusted size %i colorcells\n", palette_size
);
577 else if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
579 /* virtual colorspace - ToPhysical takes care of
580 * color translations but we have to allocate full palette
581 * to maintain compatibility
584 TRACE("Virtual colorspace - screendepth %i\n", default_visual
.depth
);
586 else palette_size
= NB_RESERVED_COLORS
; /* system palette only - however we can alloc a bunch
587 * of colors and map to them */
589 TRACE("Shared system palette uses %i colors.\n", palette_size
);
591 /* set gap to account for pixel shortage. It has to be right in the center
592 * of the system palette because otherwise raster ops get screwed. */
594 if( palette_size
>= 256 )
595 { COLOR_gapStart
= 256; COLOR_gapEnd
= -1; }
597 { COLOR_gapStart
= palette_size
/2; COLOR_gapEnd
= 255 - palette_size
/2; }
599 X11DRV_PALETTE_firstFree
= ( palette_size
> NB_RESERVED_COLORS
&&
600 (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
|| !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)) )
601 ? NB_RESERVED_COLORS
/2 : -1;
603 COLOR_sysPal
= malloc( sizeof(PALETTEENTRY
) * 256 );
604 if(COLOR_sysPal
== NULL
) {
605 ERR("Unable to allocate the system palette!\n");
606 free( pixDynMapping
);
610 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
612 if (default_visual
.depth
<= 8)
614 X11DRV_PALETTE_XPixelToPalette
= malloc( 256 * sizeof(int) );
615 if(X11DRV_PALETTE_XPixelToPalette
== NULL
) {
616 ERR("Out of memory: XPixelToPalette!\n");
617 free( pixDynMapping
);
620 for( i
= 0; i
< 256; i
++ )
621 X11DRV_PALETTE_XPixelToPalette
[i
] = NB_PALETTE_EMPTY_VALUE
;
624 /* for hicolor visuals PaletteToPixel mapping is used to skip
625 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
628 X11DRV_PALETTE_PaletteToXPixel
= malloc( sizeof(int) * 256 );
629 if(X11DRV_PALETTE_PaletteToXPixel
== NULL
) {
630 ERR("Out of memory: PaletteToXPixel!\n");
631 free( pixDynMapping
);
635 for( i
= j
= 0; i
< 256; i
++ )
637 if( i
>= COLOR_gapStart
&& i
<= COLOR_gapEnd
)
639 X11DRV_PALETTE_PaletteToXPixel
[i
] = NB_PALETTE_EMPTY_VALUE
;
640 COLOR_sysPal
[i
].peFlags
= 0; /* mark as unused */
644 if( i
< NB_RESERVED_COLORS
/2 )
646 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[i
];
647 COLOR_sysPal
[i
] = sys_pal_template
[i
];
648 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
650 else if( i
>= 256 - NB_RESERVED_COLORS
/2 )
652 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[(i
+ NB_RESERVED_COLORS
) - 256];
653 COLOR_sysPal
[i
] = sys_pal_template
[(i
+ NB_RESERVED_COLORS
) - 256];
654 COLOR_sysPal
[i
].peFlags
|= PC_SYS_USED
;
656 else if( pixDynMapping
)
657 X11DRV_PALETTE_PaletteToXPixel
[i
] = pixDynMapping
[j
++];
659 X11DRV_PALETTE_PaletteToXPixel
[i
] = i
;
661 TRACE("index %i -> pixel %i\n", i
, X11DRV_PALETTE_PaletteToXPixel
[i
]);
663 if( X11DRV_PALETTE_XPixelToPalette
)
664 X11DRV_PALETTE_XPixelToPalette
[X11DRV_PALETTE_PaletteToXPixel
[i
]] = i
;
667 free( pixDynMapping
);
672 /***********************************************************************
673 * Colormap Initialization
675 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY
*sys_pal_template
)
677 /* initialize unused entries to what Windows uses as a color
678 * cube - based on Greg Kreider's code.
682 int red
, no_r
, inc_r
;
683 int green
, no_g
, inc_g
;
684 int blue
, no_b
, inc_b
;
686 if (palette_size
<= NB_RESERVED_COLORS
)
688 while (i
*i
*i
<= (palette_size
- NB_RESERVED_COLORS
)) i
++;
689 no_r
= no_g
= no_b
= --i
;
690 if ((no_r
* (no_g
+1) * no_b
) <= (palette_size
- NB_RESERVED_COLORS
)) no_g
++;
691 if ((no_r
* no_g
* (no_b
+1)) <= (palette_size
- NB_RESERVED_COLORS
)) no_b
++;
692 inc_r
= (255 - NB_COLORCUBE_START_INDEX
)/no_r
;
693 inc_g
= (255 - NB_COLORCUBE_START_INDEX
)/no_g
;
694 inc_b
= (255 - NB_COLORCUBE_START_INDEX
)/no_b
;
696 idx
= X11DRV_PALETTE_firstFree
;
699 for (blue
= NB_COLORCUBE_START_INDEX
; blue
< 256 && idx
; blue
+= inc_b
)
700 for (green
= NB_COLORCUBE_START_INDEX
; green
< 256 && idx
; green
+= inc_g
)
701 for (red
= NB_COLORCUBE_START_INDEX
; red
< 256 && idx
; red
+= inc_r
)
705 if( red
== NB_COLORCUBE_START_INDEX
&& green
== red
&& blue
== green
) continue;
707 COLOR_sysPal
[idx
].peRed
= red
;
708 COLOR_sysPal
[idx
].peGreen
= green
;
709 COLOR_sysPal
[idx
].peBlue
= blue
;
713 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
715 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
716 if (shifts
->physicalRed
.max
!= 255) no_r
= (red
* shifts
->physicalRed
.max
) / 255;
717 if (shifts
->physicalGreen
.max
!= 255) no_g
= (green
* shifts
->physicalGreen
.max
) / 255;
718 if (shifts
->physicalBlue
.max
!= 255) no_b
= (blue
* shifts
->physicalBlue
.max
) / 255;
720 X11DRV_PALETTE_PaletteToXPixel
[idx
] = (no_r
<< shifts
->physicalRed
.shift
) | (no_g
<< shifts
->physicalGreen
.shift
) | (no_b
<< shifts
->physicalBlue
.shift
);
722 else if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
725 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[idx
] : idx
;
726 color
.red
= COLOR_sysPal
[idx
].peRed
* (65535 / 255);
727 color
.green
= COLOR_sysPal
[idx
].peGreen
* (65535 / 255);
728 color
.blue
= COLOR_sysPal
[idx
].peBlue
* (65535 / 255);
729 color
.flags
= DoRed
| DoGreen
| DoBlue
;
730 XStoreColor(gdi_display
, default_colormap
, &color
);
732 idx
= X11DRV_PALETTE_freeList
[idx
];
735 /* try to fill some entries in the "gap" with
736 * what's already in the colormap - they will be
737 * mappable to but not changeable. */
739 if( COLOR_gapStart
< COLOR_gapEnd
&& X11DRV_PALETTE_XPixelToPalette
)
744 max
= alloc_system_colors
- (256 - (COLOR_gapEnd
- COLOR_gapStart
));
745 for ( i
= 0, idx
= COLOR_gapStart
; i
< 256 && idx
<= COLOR_gapEnd
; i
++ )
746 if( X11DRV_PALETTE_XPixelToPalette
[i
] == NB_PALETTE_EMPTY_VALUE
)
750 XQueryColor(gdi_display
, default_colormap
, &xc
);
751 r
= xc
.red
>>8; g
= xc
.green
>>8; b
= xc
.blue
>>8;
753 if( xc
.pixel
< 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template
, RGB(r
, g
, b
)) &&
754 XAllocColor(gdi_display
, default_colormap
, &xc
) )
756 X11DRV_PALETTE_XPixelToPalette
[xc
.pixel
] = idx
;
757 X11DRV_PALETTE_PaletteToXPixel
[idx
] = xc
.pixel
;
758 *(COLORREF
*)(COLOR_sysPal
+ idx
) = RGB(r
, g
, b
);
759 COLOR_sysPal
[idx
++].peFlags
|= PC_SYS_USED
;
760 if( --max
<= 0 ) break;
767 /***********************************************************************
768 * X11DRV_IsSolidColor
770 * Check whether 'color' can be represented with a solid color.
772 BOOL
X11DRV_IsSolidColor( COLORREF color
)
775 const PALETTEENTRY
*pEntry
= COLOR_sysPal
;
777 if (color
& 0xff000000) return TRUE
; /* indexed color */
779 if (!color
|| (color
== 0xffffff)) return TRUE
; /* black or white */
781 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
) return TRUE
; /* no palette */
783 pthread_mutex_lock( &palette_mutex
);
784 for (i
= 0; i
< palette_size
; i
++, pEntry
++)
786 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
787 if ((GetRValue(color
) == pEntry
->peRed
) &&
788 (GetGValue(color
) == pEntry
->peGreen
) &&
789 (GetBValue(color
) == pEntry
->peBlue
))
791 pthread_mutex_unlock( &palette_mutex
);
795 pthread_mutex_unlock( &palette_mutex
);
800 /***********************************************************************
801 * X11DRV_PALETTE_ToLogical
803 * Return RGB color for given X pixel.
805 COLORREF
X11DRV_PALETTE_ToLogical(X11DRV_PDEVICE
*physDev
, int pixel
)
809 /* check for hicolor visuals first */
811 if ( (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) && !X11DRV_PALETTE_Graymax
)
813 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
815 if(physDev
->color_shifts
)
816 shifts
= physDev
->color_shifts
;
818 color
.red
= (pixel
>> shifts
->logicalRed
.shift
) & shifts
->logicalRed
.max
;
819 if (shifts
->logicalRed
.scale
<8)
820 color
.red
= color
.red
<< (8-shifts
->logicalRed
.scale
) |
821 color
.red
>> (2*shifts
->logicalRed
.scale
-8);
822 color
.green
= (pixel
>> shifts
->logicalGreen
.shift
) & shifts
->logicalGreen
.max
;
823 if (shifts
->logicalGreen
.scale
<8)
824 color
.green
=color
.green
<< (8-shifts
->logicalGreen
.scale
) |
825 color
.green
>> (2*shifts
->logicalGreen
.scale
-8);
826 color
.blue
= (pixel
>> shifts
->logicalBlue
.shift
) & shifts
->logicalBlue
.max
;
827 if (shifts
->logicalBlue
.scale
<8)
828 color
.blue
= color
.blue
<< (8-shifts
->logicalBlue
.scale
) |
829 color
.blue
>> (2*shifts
->logicalBlue
.scale
-8);
830 return RGB(color
.red
,color
.green
,color
.blue
);
833 /* check if we can bypass X */
835 if ((default_visual
.depth
<= 8) && (pixel
< 256) &&
836 !(X11DRV_PALETTE_PaletteFlags
& (X11DRV_PALETTE_VIRTUAL
| X11DRV_PALETTE_FIXED
)) ) {
838 pthread_mutex_lock( &palette_mutex
);
839 ret
= *(COLORREF
*)(COLOR_sysPal
+ (X11DRV_PALETTE_XPixelToPalette
? X11DRV_PALETTE_XPixelToPalette
[pixel
]: pixel
)) & 0x00ffffff;
840 pthread_mutex_unlock( &palette_mutex
);
845 XQueryColor(gdi_display
, default_colormap
, &color
);
846 return RGB(color
.red
>> 8, color
.green
>> 8, color
.blue
>> 8);
850 /***********************************************************************
851 * X11DRV_SysPaletteLookupPixel
853 static int X11DRV_SysPaletteLookupPixel( COLORREF col
, BOOL skipReserved
)
855 int i
, best
= 0, diff
= 0x7fffffff;
858 for( i
= 0; i
< palette_size
&& diff
; i
++ )
860 if( !(COLOR_sysPal
[i
].peFlags
& PC_SYS_USED
) ||
861 (skipReserved
&& COLOR_sysPal
[i
].peFlags
& PC_SYS_RESERVED
) )
864 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
865 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
866 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
870 if( r
< diff
) { best
= i
; diff
= r
; }
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
= NtGdiGetDCObject( physDev
->dev
.hdc
, NTGDI_OBJ_PAL
);
886 if (color
& (1 << 24)) /* PALETTEINDEX */
888 unsigned int idx
= LOWORD(color
);
889 if (!get_palette_entries( hPal
, idx
, 1, &entry
)) return 0;
890 return RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
893 if (color
>> 24 == 2) /* PALETTERGB */
895 unsigned int idx
= NtGdiGetNearestPaletteIndex( hPal
, color
& 0xffffff );
896 if (!get_palette_entries( hPal
, idx
, 1, &entry
)) return 0;
897 return RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
900 if (color
>> 16 == 0x10ff) /* DIBINDEX */
903 return color
& 0xffffff;
906 /***********************************************************************
907 * X11DRV_PALETTE_ToPhysical
909 * Return the physical color closest to 'color'.
911 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE
*physDev
, COLORREF color
)
914 HPALETTE hPal
= NtGdiGetDCObject( physDev
->dev
.hdc
, NTGDI_OBJ_PAL
);
915 int *mapping
= palette_get_mapping( hPal
);
917 ColorShifts
*shifts
= &X11DRV_PALETTE_default_shifts
;
919 if(physDev
->color_shifts
)
920 shifts
= physDev
->color_shifts
;
922 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
924 /* there is no colormap limitation; we are going to have to compute
925 * the pixel value from the visual information stored earlier
927 unsigned long red
, green
, blue
;
929 if (color
& (1 << 24)) /* PALETTEINDEX */
931 unsigned int idx
= LOWORD( color
);
933 if (!get_palette_entries( hPal
, idx
, 1, &entry
))
935 WARN("%s: out of bounds, assuming black\n", debugstr_color(color
));
938 if (mapping
) return mapping
[idx
];
940 green
= entry
.peGreen
;
943 else if (color
>> 16 == 0x10ff) /* DIBINDEX */
949 if (physDev
->depth
== 1)
950 return (((color
>> 16) & 0xff) +
951 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
952 red
= GetRValue( color
);
953 green
= GetGValue( color
);
954 blue
= GetBValue( color
);
957 if (X11DRV_PALETTE_Graymax
)
959 /* grayscale only; return scaled value */
960 return ( (red
* 30 + green
* 59 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
964 /* scale each individually and construct the TrueColor pixel value */
965 if (shifts
->physicalRed
.scale
< 8)
966 red
= red
>> (8-shifts
->physicalRed
.scale
);
967 else if (shifts
->physicalRed
.scale
> 8)
968 red
= red
<< (shifts
->physicalRed
.scale
-8) |
969 red
>> (16-shifts
->physicalRed
.scale
);
970 if (shifts
->physicalGreen
.scale
< 8)
971 green
= green
>> (8-shifts
->physicalGreen
.scale
);
972 else if (shifts
->physicalGreen
.scale
> 8)
973 green
= green
<< (shifts
->physicalGreen
.scale
-8) |
974 green
>> (16-shifts
->physicalGreen
.scale
);
975 if (shifts
->physicalBlue
.scale
< 8)
976 blue
= blue
>> (8-shifts
->physicalBlue
.scale
);
977 else if (shifts
->physicalBlue
.scale
> 8)
978 blue
= blue
<< (shifts
->physicalBlue
.scale
-8) |
979 blue
>> (16-shifts
->physicalBlue
.scale
);
981 return (red
<< shifts
->physicalRed
.shift
) | (green
<< shifts
->physicalGreen
.shift
) | (blue
<< shifts
->physicalBlue
.shift
);
987 WARN("Palette %p is not realized\n", hPal
);
989 if (color
& (1 << 24)) /* PALETTEINDEX */
991 index
= LOWORD( color
);
992 if (!get_palette_entries( hPal
, index
, 1, &entry
))
993 WARN("%s: out of bounds\n", debugstr_color(color
));
994 else if (mapping
) index
= mapping
[index
];
996 else if (color
>> 24 == 2) /* PALETTERGB */
998 index
= NtGdiGetNearestPaletteIndex( hPal
, color
);
999 if (mapping
) index
= mapping
[index
];
1001 else if (color
>> 16 == 0x10ff) /* DIBINDEX */
1007 if (physDev
->depth
== 1)
1008 return (((color
>> 16) & 0xff) +
1009 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
1011 pthread_mutex_lock( &palette_mutex
);
1012 index
= X11DRV_SysPaletteLookupPixel( color
& 0xffffff, FALSE
);
1013 if (X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1014 pthread_mutex_unlock( &palette_mutex
);
1020 /***********************************************************************
1021 * X11DRV_PALETTE_LookupPixel
1023 static int X11DRV_PALETTE_LookupPixel(ColorShifts
*shifts
, COLORREF color
)
1025 unsigned char spec_type
= color
>> 24;
1027 /* Only accept RGB which has spec_type = 0 */
1033 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
1035 unsigned long red
, green
, blue
;
1036 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
1038 if (X11DRV_PALETTE_Graymax
)
1040 /* grayscale only; return scaled value */
1041 return ( (red
* 30 + green
* 59 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
1045 /* No shifts are set in case of 1-bit */
1046 if(!shifts
) shifts
= &X11DRV_PALETTE_default_shifts
;
1048 /* scale each individually and construct the TrueColor pixel value */
1049 if (shifts
->physicalRed
.scale
< 8)
1050 red
= red
>> (8-shifts
->physicalRed
.scale
);
1051 else if (shifts
->physicalRed
.scale
> 8)
1052 red
= red
<< (shifts
->physicalRed
.scale
-8) |
1053 red
>> (16-shifts
->physicalRed
.scale
);
1054 if (shifts
->physicalGreen
.scale
< 8)
1055 green
= green
>> (8-shifts
->physicalGreen
.scale
);
1056 else if (shifts
->physicalGreen
.scale
> 8)
1057 green
= green
<< (shifts
->physicalGreen
.scale
-8) |
1058 green
>> (16-shifts
->physicalGreen
.scale
);
1059 if (shifts
->physicalBlue
.scale
< 8)
1060 blue
= blue
>> (8-shifts
->physicalBlue
.scale
);
1061 else if (shifts
->physicalBlue
.scale
> 8)
1062 blue
= blue
<< (shifts
->physicalBlue
.scale
-8) |
1063 blue
>> (16-shifts
->physicalBlue
.scale
);
1065 return (red
<< shifts
->physicalRed
.shift
) | (green
<< shifts
->physicalGreen
.shift
) | (blue
<< shifts
->physicalBlue
.shift
);
1071 HPALETTE hPal
= GetStockObject(DEFAULT_PALETTE
);
1072 int *mapping
= palette_get_mapping( hPal
);
1075 WARN("Palette %p is not realized\n", hPal
);
1077 pthread_mutex_lock( &palette_mutex
);
1078 index
= X11DRV_SysPaletteLookupPixel( color
, FALSE
);
1079 if (X11DRV_PALETTE_PaletteToXPixel
)
1080 index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1081 pthread_mutex_unlock( &palette_mutex
);
1087 /***********************************************************************
1088 * X11DRV_PALETTE_LookupSystemXPixel
1090 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
)
1092 int i
, best
= 0, diff
= 0x7fffffff;
1093 int size
= palette_size
;
1096 for( i
= 0; i
< size
&& diff
; i
++ )
1098 if( i
== NB_RESERVED_COLORS
/2 )
1100 int newi
= size
- NB_RESERVED_COLORS
/2;
1104 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
1105 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
1106 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
1108 r
= r
*r
+ g
*g
+ b
*b
;
1110 if( r
< diff
) { best
= i
; diff
= r
; }
1113 return (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[best
] : best
;
1116 /* window surfaces use the default color table */
1117 int *get_window_surface_mapping( int bpp
, int *mapping
)
1119 const RGBQUAD
*table
= get_default_color_table( bpp
);
1122 if (!table
) return NULL
;
1123 for (i
= 0; i
< 1 << bpp
; i
++)
1124 mapping
[i
] = X11DRV_PALETTE_LookupSystemXPixel( RGB( table
[i
].rgbRed
,
1126 table
[i
].rgbBlue
) );
1131 /***********************************************************************
1132 * X11DRV_PALETTE_FormatSystemPalette
1134 static void X11DRV_PALETTE_FormatSystemPalette(void)
1136 /* Build free list so we'd have an easy way to find
1137 * out if there are any available colorcells.
1140 int i
, j
= X11DRV_PALETTE_firstFree
= NB_RESERVED_COLORS
/2;
1142 COLOR_sysPal
[j
].peFlags
= 0;
1143 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
1144 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
1146 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
1147 X11DRV_PALETTE_freeList
[j
] = i
; /* next */
1150 X11DRV_PALETTE_freeList
[j
] = 0;
1153 /***********************************************************************
1154 * X11DRV_PALETTE_CheckSysColor
1156 static BOOL
X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY
*sys_pal_template
, COLORREF c
)
1159 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
1160 if( c
== (*(const COLORREF
*)(sys_pal_template
+ i
) & 0x00ffffff) )
1166 /***********************************************************************
1167 * X11DRV_LookupSysPaletteExact
1169 static int X11DRV_LookupSysPaletteExact( BYTE r
, BYTE g
, BYTE b
)
1172 for( i
= 0; i
< palette_size
; i
++ )
1174 if( COLOR_sysPal
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
1175 if( COLOR_sysPal
[i
].peRed
== r
&&
1176 COLOR_sysPal
[i
].peGreen
== g
&&
1177 COLOR_sysPal
[i
].peBlue
== b
)
1184 /***********************************************************************
1185 * RealizePalette (X11DRV.@)
1187 UINT CDECL
X11DRV_RealizePalette( PHYSDEV dev
, HPALETTE hpal
, BOOL primary
)
1189 X11DRV_PDEVICE
*physDev
= get_x11drv_dev( dev
);
1192 UINT i
, iRemapped
= 0;
1193 int *prev_mapping
, *mapping
;
1194 PALETTEENTRY entries
[256];
1197 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
) return 0;
1199 if (!NtGdiExtGetObjectW( hpal
, sizeof(num_entries
), &num_entries
)) return 0;
1201 /* initialize palette mapping table */
1202 prev_mapping
= palette_get_mapping( hpal
);
1203 mapping
= realloc( prev_mapping
, sizeof(int) * num_entries
);
1205 if(mapping
== NULL
) {
1206 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1209 palette_set_mapping( hpal
, mapping
);
1211 if (num_entries
> 256)
1213 FIXME( "more than 256 entries not supported\n" );
1216 if (!(num_entries
= get_palette_entries( hpal
, 0, num_entries
, entries
))) return 0;
1218 /* reset dynamic system palette entries */
1220 pthread_mutex_lock( &palette_mutex
);
1221 if( primary
&& X11DRV_PALETTE_firstFree
!= -1)
1222 X11DRV_PALETTE_FormatSystemPalette();
1224 for (i
= 0; i
< num_entries
; i
++)
1229 /* Even though the docs say that only one flag is to be set,
1230 * they are a bitmask. At least one app sets more than one at
1232 if ( entries
[i
].peFlags
& PC_EXPLICIT
) {
1233 /* palette entries are indices into system palette */
1234 index
= *(WORD
*)&entries
[i
];
1235 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
1237 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
1240 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1242 if ( entries
[i
].peFlags
& PC_RESERVED
) {
1243 /* forbid future mappings to this entry */
1244 flag
|= PC_SYS_RESERVED
;
1247 if (! (entries
[i
].peFlags
& PC_NOCOLLAPSE
) ) {
1248 /* try to collapse identical colors */
1249 index
= X11DRV_LookupSysPaletteExact( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
);
1254 if( X11DRV_PALETTE_firstFree
> 0 )
1257 index
= X11DRV_PALETTE_firstFree
; /* ought to be available */
1258 X11DRV_PALETTE_firstFree
= X11DRV_PALETTE_freeList
[index
];
1260 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
) ? X11DRV_PALETTE_PaletteToXPixel
[index
] : index
;
1261 color
.red
= entries
[i
].peRed
* (65535 / 255);
1262 color
.green
= entries
[i
].peGreen
* (65535 / 255);
1263 color
.blue
= entries
[i
].peBlue
* (65535 / 255);
1264 color
.flags
= DoRed
| DoGreen
| DoBlue
;
1265 XStoreColor(gdi_display
, default_colormap
, &color
);
1267 COLOR_sysPal
[index
] = entries
[i
];
1268 COLOR_sysPal
[index
].peFlags
= flag
;
1269 X11DRV_PALETTE_freeList
[index
] = 0;
1271 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1273 else if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
1275 index
= X11DRV_PALETTE_LookupPixel( physDev
->color_shifts
, RGB( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
));
1278 /* we have to map to existing entry in the system palette */
1280 index
= X11DRV_SysPaletteLookupPixel( RGB( entries
[i
].peRed
, entries
[i
].peGreen
, entries
[i
].peBlue
),
1284 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
1287 if( !prev_mapping
|| mapping
[i
] != index
) iRemapped
++;
1290 TRACE("entry %i %s -> pixel %i\n", i
, debugstr_color(*(COLORREF
*)&entries
[i
]), index
);
1293 pthread_mutex_unlock( &palette_mutex
);
1298 /***********************************************************************
1299 * UnrealizePalette (X11DRV.@)
1301 BOOL CDECL
X11DRV_UnrealizePalette( HPALETTE hpal
)
1303 int *mapping
= palette_get_mapping( hpal
);
1307 XDeleteContext( gdi_display
, (XID
)hpal
, palette_context
);
1314 /***********************************************************************
1315 * GetSystemPaletteEntries (X11DRV.@)
1317 UINT CDECL
X11DRV_GetSystemPaletteEntries( PHYSDEV dev
, UINT start
, UINT count
, LPPALETTEENTRY entries
)
1323 dev
= GET_NEXT_PHYSDEV(dev
, pGetSystemPaletteEntries
);
1324 return dev
->funcs
->pGetSystemPaletteEntries(dev
, start
, count
, entries
);
1326 if (!entries
) return palette_size
;
1327 if (start
>= palette_size
) return 0;
1328 if (start
+ count
>= palette_size
) count
= palette_size
- start
;
1330 pthread_mutex_lock( &palette_mutex
);
1331 for (i
= 0; i
< count
; i
++)
1333 entries
[i
].peRed
= COLOR_sysPal
[start
+ i
].peRed
;
1334 entries
[i
].peGreen
= COLOR_sysPal
[start
+ i
].peGreen
;
1335 entries
[i
].peBlue
= COLOR_sysPal
[start
+ i
].peBlue
;
1336 entries
[i
].peFlags
= 0;
1337 TRACE("\tidx(%02x) -> %s\n", start
+ i
, debugstr_color(*(COLORREF
*)(entries
+ i
)) );
1339 pthread_mutex_unlock( &palette_mutex
);
1344 /***********************************************************************
1345 * GetNearestColor (X11DRV.@)
1347 COLORREF CDECL
X11DRV_GetNearestColor( PHYSDEV dev
, COLORREF color
)
1349 unsigned char spec_type
= color
>> 24;
1352 if (!palette_size
) return color
;
1354 if (spec_type
== 1 || spec_type
== 2)
1356 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1360 HPALETTE hpal
= NtGdiGetDCObject( dev
->hdc
, NTGDI_OBJ_PAL
);
1362 if (!hpal
) hpal
= GetStockObject( DEFAULT_PALETTE
);
1364 if (spec_type
== 2) /* PALETTERGB */
1365 index
= NtGdiGetNearestPaletteIndex( hpal
, color
);
1366 else /* PALETTEINDEX */
1367 index
= LOWORD(color
);
1369 if (!get_palette_entries( hpal
, index
, 1, &entry
))
1371 WARN("%s: idx %d is out of bounds, assuming NULL\n", debugstr_color(color
), index
);
1372 if (!get_palette_entries( hpal
, 0, 1, &entry
)) return CLR_INVALID
;
1374 color
= RGB( entry
.peRed
, entry
.peGreen
, entry
.peBlue
);
1376 color
&= 0x00ffffff;
1377 pthread_mutex_lock( &palette_mutex
);
1378 nearest
= (0x00ffffff & *(COLORREF
*)(COLOR_sysPal
+ X11DRV_SysPaletteLookupPixel(color
, FALSE
)));
1379 pthread_mutex_unlock( &palette_mutex
);
1381 TRACE("(%s): returning %s\n", debugstr_color(color
), debugstr_color(nearest
) );
1386 /***********************************************************************
1387 * RealizeDefaultPalette (X11DRV.@)
1389 UINT CDECL
X11DRV_RealizeDefaultPalette( PHYSDEV dev
)
1394 if (palette_size
&& NtGdiGetDCDword( dev
->hdc
, NtGdiIsMemDC
, &is_memdc
) && is_memdc
)
1396 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1397 int i
, index
, *mapping
= palette_get_mapping( GetStockObject(DEFAULT_PALETTE
) );
1398 PALETTEENTRY entries
[NB_RESERVED_COLORS
];
1400 get_palette_entries( GetStockObject(DEFAULT_PALETTE
), 0, NB_RESERVED_COLORS
, entries
);
1401 pthread_mutex_lock( &palette_mutex
);
1402 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
1404 index
= X11DRV_PALETTE_LookupSystemXPixel( RGB(entries
[i
].peRed
,
1406 entries
[i
].peBlue
) );
1407 /* mapping is allocated in COLOR_InitPalette() */
1408 if( index
!= mapping
[i
] )
1414 pthread_mutex_unlock( &palette_mutex
);