dbghelp: Remove superfluous casts to self.
[wine.git] / dlls / winex11.drv / palette.c
blob8fbd7820c955b764d3730bb1359ad4442186b6b7
1 /*
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "x11drv.h"
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
45 * assistance.
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;
58 UINT16 X11DRV_PALETTE_PaletteFlags = 0;
60 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
61 ColorShifts X11DRV_PALETTE_default_shifts = { {0,0,0,}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
62 static int X11DRV_PALETTE_Graymax = 0;
64 static int palette_size;
66 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
67 static int X11DRV_PALETTE_firstFree = 0;
68 static unsigned char X11DRV_PALETTE_freeList[256];
70 static XContext palette_context; /* X context to associate a color mapping to a palette */
72 static CRITICAL_SECTION palette_cs;
73 static CRITICAL_SECTION_DEBUG critsect_debug =
75 0, 0, &palette_cs,
76 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
77 0, 0, { (DWORD_PTR)(__FILE__ ": palette_cs") }
79 static CRITICAL_SECTION palette_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
81 /**********************************************************************/
83 /* Map an EGA index (0..15) to a pixel value in the system color space. */
85 int X11DRV_PALETTE_mapEGAPixel[16];
87 /**********************************************************************/
89 #define NB_COLORCUBE_START_INDEX 63
90 #define NB_PALETTE_EMPTY_VALUE -1
92 /* Maps entry in the system palette to X pixel value */
93 int *X11DRV_PALETTE_PaletteToXPixel = NULL;
95 /* Maps pixel to the entry in the system palette */
96 int *X11DRV_PALETTE_XPixelToPalette = NULL;
98 /**********************************************************************/
100 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template );
101 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template );
102 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template );
103 static void X11DRV_PALETTE_FormatSystemPalette(void);
104 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c);
105 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col);
108 /***********************************************************************
109 * palette_get_mapping
111 static int *palette_get_mapping( HPALETTE hpal )
113 int *mapping;
115 if (XFindContext( gdi_display, (XID)hpal, palette_context, (char **)&mapping )) mapping = NULL;
116 return mapping;
119 /* copied from gdi32 */
120 static const RGBQUAD *get_default_color_table( int bpp )
122 static const RGBQUAD table_1[2] =
124 { 0x00, 0x00, 0x00 }, { 0xff, 0xff, 0xff }
126 static const RGBQUAD table_4[16] =
128 { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x80 }, { 0x00, 0x80, 0x00 }, { 0x00, 0x80, 0x80 },
129 { 0x80, 0x00, 0x00 }, { 0x80, 0x00, 0x80 }, { 0x80, 0x80, 0x00 }, { 0x80, 0x80, 0x80 },
130 { 0xc0, 0xc0, 0xc0 }, { 0x00, 0x00, 0xff }, { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0xff },
131 { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0xff }, { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0xff },
133 static const RGBQUAD table_8[256] =
135 /* first and last 10 entries are the default system palette entries */
136 { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x80 }, { 0x00, 0x80, 0x00 }, { 0x00, 0x80, 0x80 },
137 { 0x80, 0x00, 0x00 }, { 0x80, 0x00, 0x80 }, { 0x80, 0x80, 0x00 }, { 0xc0, 0xc0, 0xc0 },
138 { 0xc0, 0xdc, 0xc0 }, { 0xf0, 0xca, 0xa6 }, { 0x00, 0x20, 0x40 }, { 0x00, 0x20, 0x60 },
139 { 0x00, 0x20, 0x80 }, { 0x00, 0x20, 0xa0 }, { 0x00, 0x20, 0xc0 }, { 0x00, 0x20, 0xe0 },
140 { 0x00, 0x40, 0x00 }, { 0x00, 0x40, 0x20 }, { 0x00, 0x40, 0x40 }, { 0x00, 0x40, 0x60 },
141 { 0x00, 0x40, 0x80 }, { 0x00, 0x40, 0xa0 }, { 0x00, 0x40, 0xc0 }, { 0x00, 0x40, 0xe0 },
142 { 0x00, 0x60, 0x00 }, { 0x00, 0x60, 0x20 }, { 0x00, 0x60, 0x40 }, { 0x00, 0x60, 0x60 },
143 { 0x00, 0x60, 0x80 }, { 0x00, 0x60, 0xa0 }, { 0x00, 0x60, 0xc0 }, { 0x00, 0x60, 0xe0 },
144 { 0x00, 0x80, 0x00 }, { 0x00, 0x80, 0x20 }, { 0x00, 0x80, 0x40 }, { 0x00, 0x80, 0x60 },
145 { 0x00, 0x80, 0x80 }, { 0x00, 0x80, 0xa0 }, { 0x00, 0x80, 0xc0 }, { 0x00, 0x80, 0xe0 },
146 { 0x00, 0xa0, 0x00 }, { 0x00, 0xa0, 0x20 }, { 0x00, 0xa0, 0x40 }, { 0x00, 0xa0, 0x60 },
147 { 0x00, 0xa0, 0x80 }, { 0x00, 0xa0, 0xa0 }, { 0x00, 0xa0, 0xc0 }, { 0x00, 0xa0, 0xe0 },
148 { 0x00, 0xc0, 0x00 }, { 0x00, 0xc0, 0x20 }, { 0x00, 0xc0, 0x40 }, { 0x00, 0xc0, 0x60 },
149 { 0x00, 0xc0, 0x80 }, { 0x00, 0xc0, 0xa0 }, { 0x00, 0xc0, 0xc0 }, { 0x00, 0xc0, 0xe0 },
150 { 0x00, 0xe0, 0x00 }, { 0x00, 0xe0, 0x20 }, { 0x00, 0xe0, 0x40 }, { 0x00, 0xe0, 0x60 },
151 { 0x00, 0xe0, 0x80 }, { 0x00, 0xe0, 0xa0 }, { 0x00, 0xe0, 0xc0 }, { 0x00, 0xe0, 0xe0 },
152 { 0x40, 0x00, 0x00 }, { 0x40, 0x00, 0x20 }, { 0x40, 0x00, 0x40 }, { 0x40, 0x00, 0x60 },
153 { 0x40, 0x00, 0x80 }, { 0x40, 0x00, 0xa0 }, { 0x40, 0x00, 0xc0 }, { 0x40, 0x00, 0xe0 },
154 { 0x40, 0x20, 0x00 }, { 0x40, 0x20, 0x20 }, { 0x40, 0x20, 0x40 }, { 0x40, 0x20, 0x60 },
155 { 0x40, 0x20, 0x80 }, { 0x40, 0x20, 0xa0 }, { 0x40, 0x20, 0xc0 }, { 0x40, 0x20, 0xe0 },
156 { 0x40, 0x40, 0x00 }, { 0x40, 0x40, 0x20 }, { 0x40, 0x40, 0x40 }, { 0x40, 0x40, 0x60 },
157 { 0x40, 0x40, 0x80 }, { 0x40, 0x40, 0xa0 }, { 0x40, 0x40, 0xc0 }, { 0x40, 0x40, 0xe0 },
158 { 0x40, 0x60, 0x00 }, { 0x40, 0x60, 0x20 }, { 0x40, 0x60, 0x40 }, { 0x40, 0x60, 0x60 },
159 { 0x40, 0x60, 0x80 }, { 0x40, 0x60, 0xa0 }, { 0x40, 0x60, 0xc0 }, { 0x40, 0x60, 0xe0 },
160 { 0x40, 0x80, 0x00 }, { 0x40, 0x80, 0x20 }, { 0x40, 0x80, 0x40 }, { 0x40, 0x80, 0x60 },
161 { 0x40, 0x80, 0x80 }, { 0x40, 0x80, 0xa0 }, { 0x40, 0x80, 0xc0 }, { 0x40, 0x80, 0xe0 },
162 { 0x40, 0xa0, 0x00 }, { 0x40, 0xa0, 0x20 }, { 0x40, 0xa0, 0x40 }, { 0x40, 0xa0, 0x60 },
163 { 0x40, 0xa0, 0x80 }, { 0x40, 0xa0, 0xa0 }, { 0x40, 0xa0, 0xc0 }, { 0x40, 0xa0, 0xe0 },
164 { 0x40, 0xc0, 0x00 }, { 0x40, 0xc0, 0x20 }, { 0x40, 0xc0, 0x40 }, { 0x40, 0xc0, 0x60 },
165 { 0x40, 0xc0, 0x80 }, { 0x40, 0xc0, 0xa0 }, { 0x40, 0xc0, 0xc0 }, { 0x40, 0xc0, 0xe0 },
166 { 0x40, 0xe0, 0x00 }, { 0x40, 0xe0, 0x20 }, { 0x40, 0xe0, 0x40 }, { 0x40, 0xe0, 0x60 },
167 { 0x40, 0xe0, 0x80 }, { 0x40, 0xe0, 0xa0 }, { 0x40, 0xe0, 0xc0 }, { 0x40, 0xe0, 0xe0 },
168 { 0x80, 0x00, 0x00 }, { 0x80, 0x00, 0x20 }, { 0x80, 0x00, 0x40 }, { 0x80, 0x00, 0x60 },
169 { 0x80, 0x00, 0x80 }, { 0x80, 0x00, 0xa0 }, { 0x80, 0x00, 0xc0 }, { 0x80, 0x00, 0xe0 },
170 { 0x80, 0x20, 0x00 }, { 0x80, 0x20, 0x20 }, { 0x80, 0x20, 0x40 }, { 0x80, 0x20, 0x60 },
171 { 0x80, 0x20, 0x80 }, { 0x80, 0x20, 0xa0 }, { 0x80, 0x20, 0xc0 }, { 0x80, 0x20, 0xe0 },
172 { 0x80, 0x40, 0x00 }, { 0x80, 0x40, 0x20 }, { 0x80, 0x40, 0x40 }, { 0x80, 0x40, 0x60 },
173 { 0x80, 0x40, 0x80 }, { 0x80, 0x40, 0xa0 }, { 0x80, 0x40, 0xc0 }, { 0x80, 0x40, 0xe0 },
174 { 0x80, 0x60, 0x00 }, { 0x80, 0x60, 0x20 }, { 0x80, 0x60, 0x40 }, { 0x80, 0x60, 0x60 },
175 { 0x80, 0x60, 0x80 }, { 0x80, 0x60, 0xa0 }, { 0x80, 0x60, 0xc0 }, { 0x80, 0x60, 0xe0 },
176 { 0x80, 0x80, 0x00 }, { 0x80, 0x80, 0x20 }, { 0x80, 0x80, 0x40 }, { 0x80, 0x80, 0x60 },
177 { 0x80, 0x80, 0x80 }, { 0x80, 0x80, 0xa0 }, { 0x80, 0x80, 0xc0 }, { 0x80, 0x80, 0xe0 },
178 { 0x80, 0xa0, 0x00 }, { 0x80, 0xa0, 0x20 }, { 0x80, 0xa0, 0x40 }, { 0x80, 0xa0, 0x60 },
179 { 0x80, 0xa0, 0x80 }, { 0x80, 0xa0, 0xa0 }, { 0x80, 0xa0, 0xc0 }, { 0x80, 0xa0, 0xe0 },
180 { 0x80, 0xc0, 0x00 }, { 0x80, 0xc0, 0x20 }, { 0x80, 0xc0, 0x40 }, { 0x80, 0xc0, 0x60 },
181 { 0x80, 0xc0, 0x80 }, { 0x80, 0xc0, 0xa0 }, { 0x80, 0xc0, 0xc0 }, { 0x80, 0xc0, 0xe0 },
182 { 0x80, 0xe0, 0x00 }, { 0x80, 0xe0, 0x20 }, { 0x80, 0xe0, 0x40 }, { 0x80, 0xe0, 0x60 },
183 { 0x80, 0xe0, 0x80 }, { 0x80, 0xe0, 0xa0 }, { 0x80, 0xe0, 0xc0 }, { 0x80, 0xe0, 0xe0 },
184 { 0xc0, 0x00, 0x00 }, { 0xc0, 0x00, 0x20 }, { 0xc0, 0x00, 0x40 }, { 0xc0, 0x00, 0x60 },
185 { 0xc0, 0x00, 0x80 }, { 0xc0, 0x00, 0xa0 }, { 0xc0, 0x00, 0xc0 }, { 0xc0, 0x00, 0xe0 },
186 { 0xc0, 0x20, 0x00 }, { 0xc0, 0x20, 0x20 }, { 0xc0, 0x20, 0x40 }, { 0xc0, 0x20, 0x60 },
187 { 0xc0, 0x20, 0x80 }, { 0xc0, 0x20, 0xa0 }, { 0xc0, 0x20, 0xc0 }, { 0xc0, 0x20, 0xe0 },
188 { 0xc0, 0x40, 0x00 }, { 0xc0, 0x40, 0x20 }, { 0xc0, 0x40, 0x40 }, { 0xc0, 0x40, 0x60 },
189 { 0xc0, 0x40, 0x80 }, { 0xc0, 0x40, 0xa0 }, { 0xc0, 0x40, 0xc0 }, { 0xc0, 0x40, 0xe0 },
190 { 0xc0, 0x60, 0x00 }, { 0xc0, 0x60, 0x20 }, { 0xc0, 0x60, 0x40 }, { 0xc0, 0x60, 0x60 },
191 { 0xc0, 0x60, 0x80 }, { 0xc0, 0x60, 0xa0 }, { 0xc0, 0x60, 0xc0 }, { 0xc0, 0x60, 0xe0 },
192 { 0xc0, 0x80, 0x00 }, { 0xc0, 0x80, 0x20 }, { 0xc0, 0x80, 0x40 }, { 0xc0, 0x80, 0x60 },
193 { 0xc0, 0x80, 0x80 }, { 0xc0, 0x80, 0xa0 }, { 0xc0, 0x80, 0xc0 }, { 0xc0, 0x80, 0xe0 },
194 { 0xc0, 0xa0, 0x00 }, { 0xc0, 0xa0, 0x20 }, { 0xc0, 0xa0, 0x40 }, { 0xc0, 0xa0, 0x60 },
195 { 0xc0, 0xa0, 0x80 }, { 0xc0, 0xa0, 0xa0 }, { 0xc0, 0xa0, 0xc0 }, { 0xc0, 0xa0, 0xe0 },
196 { 0xc0, 0xc0, 0x00 }, { 0xc0, 0xc0, 0x20 }, { 0xc0, 0xc0, 0x40 }, { 0xc0, 0xc0, 0x60 },
197 { 0xc0, 0xc0, 0x80 }, { 0xc0, 0xc0, 0xa0 }, { 0xf0, 0xfb, 0xff }, { 0xa4, 0xa0, 0xa0 },
198 { 0x80, 0x80, 0x80 }, { 0x00, 0x00, 0xff }, { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0xff },
199 { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0xff }, { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0xff },
202 switch (bpp)
204 case 1: return table_1;
205 case 4: return table_4;
206 case 8: return table_8;
207 default: return NULL;
211 /***********************************************************************
212 * palette_set_mapping
214 static void palette_set_mapping( HPALETTE hpal, int *mapping )
216 XSaveContext( gdi_display, (XID)hpal, palette_context, (char *)mapping );
220 /***********************************************************************
221 * X11DRV_PALETTE_ComputeChannelShift
223 * Calculate conversion parameters for a given color mask
225 static void X11DRV_PALETTE_ComputeChannelShift(unsigned long maskbits, ChannelShift *physical, ChannelShift *to_logical)
227 int i;
229 if (maskbits==0)
231 physical->shift=0;
232 physical->scale=0;
233 physical->max=0;
234 to_logical->shift=0;
235 to_logical->scale=0;
236 to_logical->max=0;
237 return;
240 for(i=0;!(maskbits&1);i++)
241 maskbits >>= 1;
243 physical->shift = i;
244 physical->max = maskbits;
246 for(i=0;maskbits!=0;i++)
247 maskbits >>= 1;
248 physical->scale = i;
250 if (physical->scale>8)
252 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
253 * So we adjust the shifts to also normalize the color fields to
254 * the Win32 standard of 8 bits per color.
256 to_logical->shift=physical->shift+(physical->scale-8);
257 to_logical->scale=8;
258 to_logical->max=0xff;
259 } else {
260 to_logical->shift=physical->shift;
261 to_logical->scale=physical->scale;
262 to_logical->max=physical->max;
266 /***********************************************************************
267 * X11DRV_PALETTE_ComputeColorShifts
269 * Calculate conversion parameters for a given color
271 static void X11DRV_PALETTE_ComputeColorShifts(ColorShifts *shifts, unsigned long redMask, unsigned long greenMask, unsigned long blueMask)
273 X11DRV_PALETTE_ComputeChannelShift(redMask, &shifts->physicalRed, &shifts->logicalRed);
274 X11DRV_PALETTE_ComputeChannelShift(greenMask, &shifts->physicalGreen, &shifts->logicalGreen);
275 X11DRV_PALETTE_ComputeChannelShift(blueMask, &shifts->physicalBlue, &shifts->logicalBlue);
278 /***********************************************************************
279 * COLOR_Init
281 * Initialize color management.
283 int X11DRV_PALETTE_Init(void)
285 int *mapping;
286 PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS];
288 TRACE("initializing palette manager...\n");
290 palette_context = XUniqueContext();
291 palette_size = default_visual.colormap_size;
293 switch(default_visual.class)
295 case DirectColor:
296 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
297 /* fall through */
298 case GrayScale:
299 case PseudoColor:
300 if (private_color_map)
302 XSetWindowAttributes win_attr;
304 XFreeColormap( gdi_display, default_colormap );
305 default_colormap = XCreateColormap( gdi_display, root_window,
306 default_visual.visual, AllocAll );
307 if (default_colormap)
309 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_PRIVATE;
311 if (is_virtual_desktop())
313 win_attr.colormap = default_colormap;
314 XChangeWindowAttributes( gdi_display, root_window, CWColormap, &win_attr );
318 break;
320 case StaticGray:
321 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
322 X11DRV_PALETTE_Graymax = (1 << default_visual.depth)-1;
323 break;
325 case TrueColor:
326 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
327 /* fall through */
328 case StaticColor:
329 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
330 X11DRV_PALETTE_ComputeColorShifts(&X11DRV_PALETTE_default_shifts, default_visual.red_mask, default_visual.green_mask, default_visual.blue_mask);
331 break;
334 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
336 palette_size = 0;
338 else
340 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, sys_pal_template );
342 if ((mapping = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * NB_RESERVED_COLORS )))
343 palette_set_mapping( GetStockObject(DEFAULT_PALETTE), mapping );
345 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
346 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template );
347 else
348 X11DRV_PALETTE_BuildSharedMap( sys_pal_template );
350 /* Build free list */
352 if( X11DRV_PALETTE_firstFree != -1 )
353 X11DRV_PALETTE_FormatSystemPalette();
355 X11DRV_PALETTE_FillDefaultColors( sys_pal_template );
356 palette_size = default_visual.colormap_size;
359 return palette_size;
362 /***********************************************************************
363 * X11DRV_PALETTE_BuildPrivateMap
365 * Allocate colorcells and initialize mapping tables.
367 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template )
369 /* Private colormap - identity mapping */
371 XColor color;
372 int i;
374 if((COLOR_sysPal = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*palette_size)) == NULL) {
375 WARN("Unable to allocate the system palette\n");
376 return FALSE;
379 TRACE("Building private map - %i palette entries\n", palette_size);
381 /* Allocate system palette colors */
383 for( i=0; i < palette_size; i++ )
385 if( i < NB_RESERVED_COLORS/2 )
387 color.red = sys_pal_template[i].peRed * (65535 / 255);
388 color.green = sys_pal_template[i].peGreen * (65535 / 255);
389 color.blue = sys_pal_template[i].peBlue * (65535 / 255);
390 COLOR_sysPal[i] = sys_pal_template[i];
391 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
393 else if( i >= palette_size - NB_RESERVED_COLORS/2 )
395 int j = NB_RESERVED_COLORS + i - palette_size;
396 color.red = sys_pal_template[j].peRed * (65535 / 255);
397 color.green = sys_pal_template[j].peGreen * (65535 / 255);
398 color.blue = sys_pal_template[j].peBlue * (65535 / 255);
399 COLOR_sysPal[i] = sys_pal_template[j];
400 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
403 color.flags = DoRed | DoGreen | DoBlue;
404 color.pixel = i;
405 XStoreColor(gdi_display, default_colormap, &color);
407 /* Set EGA mapping if color is from the first or last eight */
409 if (i < 8)
410 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
411 else if (i >= palette_size - 8 )
412 X11DRV_PALETTE_mapEGAPixel[i - (palette_size - 16)] = color.pixel;
415 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
417 COLOR_gapStart = 256; COLOR_gapEnd = -1;
419 X11DRV_PALETTE_firstFree = (palette_size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
421 return FALSE;
424 /***********************************************************************
425 * X11DRV_PALETTE_BuildSharedMap
427 * Allocate colorcells and initialize mapping tables.
429 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template )
431 XColor color;
432 unsigned long sysPixel[NB_RESERVED_COLORS];
433 unsigned long* pixDynMapping = NULL;
434 unsigned long plane_masks[1];
435 int i, j, warn = 0;
436 int diff, r, g, b, bp = 0, wp = 1;
437 int step = 1;
438 unsigned int max = 256;
439 Colormap defaultCM;
440 XColor defaultColors[256];
442 /* Copy the first bunch of colors out of the default colormap to prevent
443 * colormap flashing as much as possible. We're likely to get the most
444 * important Window Manager colors, etc in the first 128 colors */
445 defaultCM = DefaultColormap( gdi_display, DefaultScreen(gdi_display) );
447 if (copy_default_colors > 256) copy_default_colors = 256;
448 for (i = 0; i < copy_default_colors; i++)
449 defaultColors[i].pixel = (long) i;
450 XQueryColors(gdi_display, defaultCM, &defaultColors[0], copy_default_colors);
451 for (i = 0; i < copy_default_colors; i++)
452 XAllocColor( gdi_display, default_colormap, &defaultColors[i] );
454 if (alloc_system_colors > 256) alloc_system_colors = 256;
455 else if (alloc_system_colors < 20) alloc_system_colors = 20;
456 TRACE("%d colors configured.\n", alloc_system_colors);
458 TRACE("Building shared map - %i palette entries\n", palette_size);
460 /* Be nice and allocate system colors as read-only */
462 for( i = 0; i < NB_RESERVED_COLORS; i++ )
464 color.red = sys_pal_template[i].peRed * 65535 / 255;
465 color.green = sys_pal_template[i].peGreen * 65535 / 255;
466 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
467 color.flags = DoRed | DoGreen | DoBlue;
469 if (!XAllocColor( gdi_display, default_colormap, &color ))
471 XColor best, c;
473 if( !warn++ )
475 WARN("Not enough colors for the full system palette.\n");
477 bp = BlackPixel(gdi_display, DefaultScreen(gdi_display));
478 wp = WhitePixel(gdi_display, DefaultScreen(gdi_display));
480 max = 0xffffffff >> (32 - default_visual.depth);
481 if( max > 256 )
483 step = max/256;
484 max = 256;
488 /* reinit color (XAllocColor() may change it)
489 * and map to the best shared colorcell */
491 color.red = sys_pal_template[i].peRed * 65535 / 255;
492 color.green = sys_pal_template[i].peGreen * 65535 / 255;
493 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
495 best.pixel = best.red = best.green = best.blue = 0;
496 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
498 XQueryColor(gdi_display, default_colormap, &c);
499 r = (c.red - color.red)>>8;
500 g = (c.green - color.green)>>8;
501 b = (c.blue - color.blue)>>8;
502 r = r*r + g*g + b*b;
503 if( r < diff ) { best = c; diff = r; }
506 if( XAllocColor(gdi_display, default_colormap, &best) )
507 color.pixel = best.pixel;
508 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
511 sysPixel[i] = color.pixel;
513 TRACE("syscolor(%x) -> pixel %i\n", *(const COLORREF*)(sys_pal_template+i),
514 (int)color.pixel);
516 /* Set EGA mapping if color in the first or last eight */
518 if (i < 8)
519 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
520 else if (i >= NB_RESERVED_COLORS - 8 )
521 X11DRV_PALETTE_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
524 /* now allocate changeable set */
526 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
528 int c_min = 0, c_max = palette_size, c_val;
530 TRACE("Dynamic colormap...\n");
532 /* let's become the first client that actually follows
533 * X guidelines and does binary search...
536 if((pixDynMapping = HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size)) == NULL) {
537 WARN("Out of memory while building system palette.\n");
538 return FALSE;
541 /* comment this out if you want to debug palette init */
542 XGrabServer(gdi_display);
544 while( c_max - c_min > 0 )
546 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
548 if( !XAllocColorCells(gdi_display, default_colormap, False,
549 plane_masks, 0, pixDynMapping, c_val) )
550 c_max = c_val - 1;
551 else
553 XFreeColors(gdi_display, default_colormap, pixDynMapping, c_val, 0);
554 c_min = c_val;
558 if( c_min > alloc_system_colors - NB_RESERVED_COLORS)
559 c_min = alloc_system_colors - NB_RESERVED_COLORS;
561 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
563 if( c_min > 0 )
564 if( !XAllocColorCells(gdi_display, default_colormap, False,
565 plane_masks, 0, pixDynMapping, c_min) )
567 WARN("Inexplicable failure during colorcell allocation.\n");
568 c_min = 0;
571 palette_size = c_min + NB_RESERVED_COLORS;
573 XUngrabServer(gdi_display);
574 XFlush(gdi_display);
576 TRACE("adjusted size %i colorcells\n", palette_size);
578 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
580 /* virtual colorspace - ToPhysical takes care of
581 * color translations but we have to allocate full palette
582 * to maintain compatibility
584 palette_size = 256;
585 TRACE("Virtual colorspace - screendepth %i\n", default_visual.depth);
587 else palette_size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
588 * of colors and map to them */
590 TRACE("Shared system palette uses %i colors.\n", palette_size);
592 /* set gap to account for pixel shortage. It has to be right in the center
593 * of the system palette because otherwise raster ops get screwed. */
595 if( palette_size >= 256 )
596 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
597 else
598 { COLOR_gapStart = palette_size/2; COLOR_gapEnd = 255 - palette_size/2; }
600 X11DRV_PALETTE_firstFree = ( palette_size > NB_RESERVED_COLORS &&
601 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
602 ? NB_RESERVED_COLORS/2 : -1;
604 COLOR_sysPal = HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
605 if(COLOR_sysPal == NULL) {
606 ERR("Unable to allocate the system palette!\n");
607 HeapFree(GetProcessHeap(), 0, pixDynMapping);
608 return FALSE;
611 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
613 if (default_visual.depth <= 8)
615 X11DRV_PALETTE_XPixelToPalette = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
616 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
617 ERR("Out of memory: XPixelToPalette!\n");
618 HeapFree(GetProcessHeap(), 0, pixDynMapping);
619 return FALSE;
621 for( i = 0; i < 256; i++ )
622 X11DRV_PALETTE_XPixelToPalette[i] = NB_PALETTE_EMPTY_VALUE;
625 /* for hicolor visuals PaletteToPixel mapping is used to skip
626 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
629 X11DRV_PALETTE_PaletteToXPixel = HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
630 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
631 ERR("Out of memory: PaletteToXPixel!\n");
632 HeapFree(GetProcessHeap(), 0, pixDynMapping);
633 return FALSE;
636 for( i = j = 0; i < 256; i++ )
638 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
640 X11DRV_PALETTE_PaletteToXPixel[i] = NB_PALETTE_EMPTY_VALUE;
641 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
642 continue;
645 if( i < NB_RESERVED_COLORS/2 )
647 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
648 COLOR_sysPal[i] = sys_pal_template[i];
649 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
651 else if( i >= 256 - NB_RESERVED_COLORS/2 )
653 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
654 COLOR_sysPal[i] = sys_pal_template[(i + NB_RESERVED_COLORS) - 256];
655 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
657 else if( pixDynMapping )
658 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
659 else
660 X11DRV_PALETTE_PaletteToXPixel[i] = i;
662 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
664 if( X11DRV_PALETTE_XPixelToPalette )
665 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
668 HeapFree(GetProcessHeap(), 0, pixDynMapping);
670 return TRUE;
673 /***********************************************************************
674 * Colormap Initialization
676 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template )
678 /* initialize unused entries to what Windows uses as a color
679 * cube - based on Greg Kreider's code.
682 int i = 1, idx = 0;
683 int red, no_r, inc_r;
684 int green, no_g, inc_g;
685 int blue, no_b, inc_b;
687 if (palette_size <= NB_RESERVED_COLORS)
688 return;
689 while (i*i*i <= (palette_size - NB_RESERVED_COLORS)) i++;
690 no_r = no_g = no_b = --i;
691 if ((no_r * (no_g+1) * no_b) <= (palette_size - NB_RESERVED_COLORS)) no_g++;
692 if ((no_r * no_g * (no_b+1)) <= (palette_size - NB_RESERVED_COLORS)) no_b++;
693 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
694 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
695 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
697 idx = X11DRV_PALETTE_firstFree;
699 if( idx != -1 )
700 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
701 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
702 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
704 /* weird but true */
706 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
708 COLOR_sysPal[idx].peRed = red;
709 COLOR_sysPal[idx].peGreen = green;
710 COLOR_sysPal[idx].peBlue = blue;
712 /* set X color */
714 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
716 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
717 if (shifts->physicalRed.max != 255) no_r = (red * shifts->physicalRed.max) / 255;
718 if (shifts->physicalGreen.max != 255) no_g = (green * shifts->physicalGreen.max) / 255;
719 if (shifts->physicalBlue.max != 255) no_b = (blue * shifts->physicalBlue.max) / 255;
721 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << shifts->physicalRed.shift) | (no_g << shifts->physicalGreen.shift) | (no_b << shifts->physicalBlue.shift);
723 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
725 XColor color;
726 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
727 color.red = COLOR_sysPal[idx].peRed * (65535 / 255);
728 color.green = COLOR_sysPal[idx].peGreen * (65535 / 255);
729 color.blue = COLOR_sysPal[idx].peBlue * (65535 / 255);
730 color.flags = DoRed | DoGreen | DoBlue;
731 XStoreColor(gdi_display, default_colormap, &color);
733 idx = X11DRV_PALETTE_freeList[idx];
736 /* try to fill some entries in the "gap" with
737 * what's already in the colormap - they will be
738 * mappable to but not changeable. */
740 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
742 XColor xc;
743 int r, g, b, max;
745 max = alloc_system_colors - (256 - (COLOR_gapEnd - COLOR_gapStart));
746 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
747 if( X11DRV_PALETTE_XPixelToPalette[i] == NB_PALETTE_EMPTY_VALUE )
749 xc.pixel = i;
751 XQueryColor(gdi_display, default_colormap, &xc);
752 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
754 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template, RGB(r, g, b)) &&
755 XAllocColor(gdi_display, default_colormap, &xc) )
757 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
758 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
759 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
760 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
761 if( --max <= 0 ) break;
768 /***********************************************************************
769 * X11DRV_IsSolidColor
771 * Check whether 'color' can be represented with a solid color.
773 BOOL X11DRV_IsSolidColor( COLORREF color )
775 int i;
776 const PALETTEENTRY *pEntry = COLOR_sysPal;
778 if (color & 0xff000000) return TRUE; /* indexed color */
780 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
782 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return TRUE; /* no palette */
784 EnterCriticalSection( &palette_cs );
785 for (i = 0; i < palette_size ; i++, pEntry++)
787 if( i < COLOR_gapStart || i > COLOR_gapEnd )
788 if ((GetRValue(color) == pEntry->peRed) &&
789 (GetGValue(color) == pEntry->peGreen) &&
790 (GetBValue(color) == pEntry->peBlue))
792 LeaveCriticalSection( &palette_cs );
793 return TRUE;
796 LeaveCriticalSection( &palette_cs );
797 return FALSE;
801 /***********************************************************************
802 * X11DRV_PALETTE_ToLogical
804 * Return RGB color for given X pixel.
806 COLORREF X11DRV_PALETTE_ToLogical(X11DRV_PDEVICE *physDev, int pixel)
808 XColor color;
810 /* check for hicolor visuals first */
812 if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
814 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
816 if(physDev->color_shifts)
817 shifts = physDev->color_shifts;
819 color.red = (pixel >> shifts->logicalRed.shift) & shifts->logicalRed.max;
820 if (shifts->logicalRed.scale<8)
821 color.red= color.red << (8-shifts->logicalRed.scale) |
822 color.red >> (2*shifts->logicalRed.scale-8);
823 color.green = (pixel >> shifts->logicalGreen.shift) & shifts->logicalGreen.max;
824 if (shifts->logicalGreen.scale<8)
825 color.green=color.green << (8-shifts->logicalGreen.scale) |
826 color.green >> (2*shifts->logicalGreen.scale-8);
827 color.blue = (pixel >> shifts->logicalBlue.shift) & shifts->logicalBlue.max;
828 if (shifts->logicalBlue.scale<8)
829 color.blue= color.blue << (8-shifts->logicalBlue.scale) |
830 color.blue >> (2*shifts->logicalBlue.scale-8);
831 return RGB(color.red,color.green,color.blue);
834 /* check if we can bypass X */
836 if ((default_visual.depth <= 8) && (pixel < 256) &&
837 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) ) {
838 COLORREF ret;
839 EnterCriticalSection( &palette_cs );
840 ret = *(COLORREF *)(COLOR_sysPal + (X11DRV_PALETTE_XPixelToPalette ? X11DRV_PALETTE_XPixelToPalette[pixel]: pixel)) & 0x00ffffff;
841 LeaveCriticalSection( &palette_cs );
842 return ret;
845 color.pixel = pixel;
846 XQueryColor(gdi_display, default_colormap, &color);
847 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
851 /***********************************************************************
852 * X11DRV_SysPaletteLookupPixel
854 static int X11DRV_SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
856 int i, best = 0, diff = 0x7fffffff;
857 int r,g,b;
859 for( i = 0; i < palette_size && diff ; i++ )
861 if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) ||
862 (skipReserved && COLOR_sysPal[i].peFlags & PC_SYS_RESERVED) )
863 continue;
865 r = COLOR_sysPal[i].peRed - GetRValue(col);
866 g = COLOR_sysPal[i].peGreen - GetGValue(col);
867 b = COLOR_sysPal[i].peBlue - GetBValue(col);
869 r = r*r + g*g + b*b;
871 if( r < diff ) { best = i; diff = r; }
873 return best;
877 /***********************************************************************
878 * X11DRV_PALETTE_GetColor
880 * Resolve PALETTEINDEX/PALETTERGB/DIBINDEX COLORREFs to an RGB COLORREF.
882 COLORREF X11DRV_PALETTE_GetColor( X11DRV_PDEVICE *physDev, COLORREF color )
884 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
885 PALETTEENTRY entry;
887 if (color & (1 << 24)) /* PALETTEINDEX */
889 unsigned int idx = LOWORD(color);
890 if (!GetPaletteEntries( hPal, idx, 1, &entry )) return 0;
891 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
894 if (color >> 24 == 2) /* PALETTERGB */
896 unsigned int idx = GetNearestPaletteIndex( hPal, color & 0xffffff );
897 if (!GetPaletteEntries( hPal, idx, 1, &entry )) return 0;
898 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
901 if (color >> 16 == 0x10ff) /* DIBINDEX */
902 return 0;
904 return color & 0xffffff;
907 /***********************************************************************
908 * X11DRV_PALETTE_ToPhysical
910 * Return the physical color closest to 'color'.
912 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
914 WORD index = 0;
915 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
916 int *mapping = palette_get_mapping( hPal );
917 PALETTEENTRY entry;
918 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
920 if(physDev->color_shifts)
921 shifts = physDev->color_shifts;
923 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
925 /* there is no colormap limitation; we are going to have to compute
926 * the pixel value from the visual information stored earlier
928 unsigned long red, green, blue;
930 if (color & (1 << 24)) /* PALETTEINDEX */
932 unsigned int idx = LOWORD( color );
934 if (!GetPaletteEntries( hPal, idx, 1, &entry ))
936 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color, idx);
937 return 0;
939 if (mapping) return mapping[idx];
940 red = entry.peRed;
941 green = entry.peGreen;
942 blue = entry.peBlue;
944 else if (color >> 16 == 0x10ff) /* DIBINDEX */
946 return 0;
948 else /* RGB */
950 if (physDev->depth == 1)
951 return (((color >> 16) & 0xff) +
952 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
953 red = GetRValue( color );
954 green = GetGValue( color );
955 blue = GetBValue( color );
958 if (X11DRV_PALETTE_Graymax)
960 /* grayscale only; return scaled value */
961 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
963 else
965 /* scale each individually and construct the TrueColor pixel value */
966 if (shifts->physicalRed.scale < 8)
967 red = red >> (8-shifts->physicalRed.scale);
968 else if (shifts->physicalRed.scale > 8)
969 red = red << (shifts->physicalRed.scale-8) |
970 red >> (16-shifts->physicalRed.scale);
971 if (shifts->physicalGreen.scale < 8)
972 green = green >> (8-shifts->physicalGreen.scale);
973 else if (shifts->physicalGreen.scale > 8)
974 green = green << (shifts->physicalGreen.scale-8) |
975 green >> (16-shifts->physicalGreen.scale);
976 if (shifts->physicalBlue.scale < 8)
977 blue = blue >> (8-shifts->physicalBlue.scale);
978 else if (shifts->physicalBlue.scale > 8)
979 blue = blue << (shifts->physicalBlue.scale-8) |
980 blue >> (16-shifts->physicalBlue.scale);
982 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
985 else
987 if (!mapping)
988 WARN("Palette %p is not realized\n", hPal);
990 if (color & (1 << 24)) /* PALETTEINDEX */
992 index = LOWORD( color );
993 if (!GetPaletteEntries( hPal, index, 1, &entry ))
994 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color, index);
995 else if (mapping) index = mapping[index];
997 else if (color >> 24 == 2) /* PALETTERGB */
999 index = GetNearestPaletteIndex( hPal, color );
1000 if (mapping) index = mapping[index];
1002 else if (color >> 16 == 0x10ff) /* DIBINDEX */
1004 return 0;
1006 else /* RGB */
1008 if (physDev->depth == 1)
1009 return (((color >> 16) & 0xff) +
1010 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
1012 EnterCriticalSection( &palette_cs );
1013 index = X11DRV_SysPaletteLookupPixel( color & 0xffffff, FALSE);
1014 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
1015 LeaveCriticalSection( &palette_cs );
1018 return index;
1021 /***********************************************************************
1022 * X11DRV_PALETTE_LookupPixel
1024 static int X11DRV_PALETTE_LookupPixel(ColorShifts *shifts, COLORREF color )
1026 unsigned char spec_type = color >> 24;
1028 /* Only accept RGB which has spec_type = 0 */
1029 if(spec_type)
1030 return 0;
1032 color &= 0xffffff;
1034 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
1036 unsigned long red, green, blue;
1037 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
1039 if (X11DRV_PALETTE_Graymax)
1041 /* grayscale only; return scaled value */
1042 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
1044 else
1046 /* No shifts are set in case of 1-bit */
1047 if(!shifts) shifts = &X11DRV_PALETTE_default_shifts;
1049 /* scale each individually and construct the TrueColor pixel value */
1050 if (shifts->physicalRed.scale < 8)
1051 red = red >> (8-shifts->physicalRed.scale);
1052 else if (shifts->physicalRed.scale > 8)
1053 red = red << (shifts->physicalRed.scale-8) |
1054 red >> (16-shifts->physicalRed.scale);
1055 if (shifts->physicalGreen.scale < 8)
1056 green = green >> (8-shifts->physicalGreen.scale);
1057 else if (shifts->physicalGreen.scale > 8)
1058 green = green << (shifts->physicalGreen.scale-8) |
1059 green >> (16-shifts->physicalGreen.scale);
1060 if (shifts->physicalBlue.scale < 8)
1061 blue = blue >> (8-shifts->physicalBlue.scale);
1062 else if (shifts->physicalBlue.scale > 8)
1063 blue = blue << (shifts->physicalBlue.scale-8) |
1064 blue >> (16-shifts->physicalBlue.scale);
1066 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
1069 else
1071 WORD index;
1072 HPALETTE hPal = GetStockObject(DEFAULT_PALETTE);
1073 int *mapping = palette_get_mapping( hPal );
1075 if (!mapping)
1076 WARN("Palette %p is not realized\n", hPal);
1078 EnterCriticalSection( &palette_cs );
1079 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
1080 if (X11DRV_PALETTE_PaletteToXPixel)
1081 index = X11DRV_PALETTE_PaletteToXPixel[index];
1082 LeaveCriticalSection( &palette_cs );
1083 return index;
1088 /***********************************************************************
1089 * X11DRV_PALETTE_LookupSystemXPixel
1091 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
1093 int i, best = 0, diff = 0x7fffffff;
1094 int size = palette_size;
1095 int r,g,b;
1097 for( i = 0; i < size && diff ; i++ )
1099 if( i == NB_RESERVED_COLORS/2 )
1101 int newi = size - NB_RESERVED_COLORS/2;
1102 if (newi>i) i=newi;
1105 r = COLOR_sysPal[i].peRed - GetRValue(col);
1106 g = COLOR_sysPal[i].peGreen - GetGValue(col);
1107 b = COLOR_sysPal[i].peBlue - GetBValue(col);
1109 r = r*r + g*g + b*b;
1111 if( r < diff ) { best = i; diff = r; }
1114 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
1117 /* window surfaces use the default color table */
1118 int *get_window_surface_mapping( int bpp, int *mapping )
1120 const RGBQUAD *table = get_default_color_table( bpp );
1121 int i;
1123 if (!table) return NULL;
1124 for (i = 0; i < 1 << bpp; i++)
1125 mapping[i] = X11DRV_PALETTE_LookupSystemXPixel( RGB( table[i].rgbRed,
1126 table[i].rgbGreen,
1127 table[i].rgbBlue) );
1128 return mapping;
1132 /***********************************************************************
1133 * X11DRV_PALETTE_FormatSystemPalette
1135 static void X11DRV_PALETTE_FormatSystemPalette(void)
1137 /* Build free list so we'd have an easy way to find
1138 * out if there are any available colorcells.
1141 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1143 COLOR_sysPal[j].peFlags = 0;
1144 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1145 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1147 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1148 X11DRV_PALETTE_freeList[j] = i; /* next */
1149 j = i;
1151 X11DRV_PALETTE_freeList[j] = 0;
1154 /***********************************************************************
1155 * X11DRV_PALETTE_CheckSysColor
1157 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1159 int i;
1160 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1161 if( c == (*(const COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1162 return FALSE;
1163 return TRUE;
1167 /***********************************************************************
1168 * X11DRV_LookupSysPaletteExact
1170 static int X11DRV_LookupSysPaletteExact( BYTE r, BYTE g, BYTE b )
1172 int i;
1173 for( i = 0; i < palette_size; i++ )
1175 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1176 if( COLOR_sysPal[i].peRed == r &&
1177 COLOR_sysPal[i].peGreen == g &&
1178 COLOR_sysPal[i].peBlue == b )
1179 return i;
1181 return -1;
1185 /***********************************************************************
1186 * RealizePalette (X11DRV.@)
1188 UINT CDECL X11DRV_RealizePalette( PHYSDEV dev, HPALETTE hpal, BOOL primary )
1190 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1191 char flag;
1192 int index;
1193 UINT i, iRemapped = 0;
1194 int *prev_mapping, *mapping;
1195 PALETTEENTRY entries[256];
1196 WORD num_entries;
1198 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1200 if (!GetObjectW( hpal, sizeof(num_entries), &num_entries )) return 0;
1202 /* initialize palette mapping table */
1203 prev_mapping = palette_get_mapping( hpal );
1204 if (prev_mapping)
1205 mapping = HeapReAlloc( GetProcessHeap(), 0, prev_mapping, sizeof(int)*num_entries);
1206 else
1207 mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries);
1209 if(mapping == NULL) {
1210 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1211 return 0;
1213 palette_set_mapping( hpal, mapping );
1215 if (num_entries > 256)
1217 FIXME( "more than 256 entries not supported\n" );
1218 num_entries = 256;
1220 if (!(num_entries = GetPaletteEntries( hpal, 0, num_entries, entries ))) return 0;
1222 /* reset dynamic system palette entries */
1224 EnterCriticalSection( &palette_cs );
1225 if( primary && X11DRV_PALETTE_firstFree != -1)
1226 X11DRV_PALETTE_FormatSystemPalette();
1228 for (i = 0; i < num_entries; i++)
1230 index = -1;
1231 flag = PC_SYS_USED;
1233 /* Even though the docs say that only one flag is to be set,
1234 * they are a bitmask. At least one app sets more than one at
1235 * the same time. */
1236 if ( entries[i].peFlags & PC_EXPLICIT ) {
1237 /* palette entries are indices into system palette */
1238 index = *(WORD*)&entries[i];
1239 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1241 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1242 index = 0;
1244 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1245 } else {
1246 if ( entries[i].peFlags & PC_RESERVED ) {
1247 /* forbid future mappings to this entry */
1248 flag |= PC_SYS_RESERVED;
1251 if (! (entries[i].peFlags & PC_NOCOLLAPSE) ) {
1252 /* try to collapse identical colors */
1253 index = X11DRV_LookupSysPaletteExact( entries[i].peRed, entries[i].peGreen, entries[i].peBlue );
1256 if( index < 0 )
1258 if( X11DRV_PALETTE_firstFree > 0 )
1260 XColor color;
1261 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1262 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1264 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1265 color.red = entries[i].peRed * (65535 / 255);
1266 color.green = entries[i].peGreen * (65535 / 255);
1267 color.blue = entries[i].peBlue * (65535 / 255);
1268 color.flags = DoRed | DoGreen | DoBlue;
1269 XStoreColor(gdi_display, default_colormap, &color);
1271 COLOR_sysPal[index] = entries[i];
1272 COLOR_sysPal[index].peFlags = flag;
1273 X11DRV_PALETTE_freeList[index] = 0;
1275 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1277 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1279 index = X11DRV_PALETTE_LookupPixel( physDev->color_shifts, RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ));
1282 /* we have to map to existing entry in the system palette */
1284 index = X11DRV_SysPaletteLookupPixel( RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ),
1285 TRUE );
1288 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1291 if( !prev_mapping || mapping[i] != index ) iRemapped++;
1292 mapping[i] = index;
1294 TRACE("entry %i (%x) -> pixel %i\n", i, *(COLORREF*)&entries[i], index);
1297 LeaveCriticalSection( &palette_cs );
1298 return iRemapped;
1302 /***********************************************************************
1303 * UnrealizePalette (X11DRV.@)
1305 BOOL CDECL X11DRV_UnrealizePalette( HPALETTE hpal )
1307 int *mapping = palette_get_mapping( hpal );
1309 if (mapping)
1311 XDeleteContext( gdi_display, (XID)hpal, palette_context );
1312 HeapFree( GetProcessHeap(), 0, mapping );
1314 return TRUE;
1318 /***********************************************************************
1319 * GetSystemPaletteEntries (X11DRV.@)
1321 UINT CDECL X11DRV_GetSystemPaletteEntries( PHYSDEV dev, UINT start, UINT count, LPPALETTEENTRY entries )
1323 UINT i;
1325 if (!palette_size)
1327 dev = GET_NEXT_PHYSDEV(dev, pGetSystemPaletteEntries);
1328 return dev->funcs->pGetSystemPaletteEntries(dev, start, count, entries);
1330 if (!entries) return palette_size;
1331 if (start >= palette_size) return 0;
1332 if (start + count >= palette_size) count = palette_size - start;
1334 EnterCriticalSection( &palette_cs );
1335 for (i = 0; i < count; i++)
1337 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1338 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1339 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1340 entries[i].peFlags = 0;
1341 TRACE("\tidx(%02x) -> RGB(%08x)\n", start + i, *(COLORREF*)(entries + i) );
1343 LeaveCriticalSection( &palette_cs );
1344 return count;
1348 /***********************************************************************
1349 * GetNearestColor (X11DRV.@)
1351 COLORREF CDECL X11DRV_GetNearestColor( PHYSDEV dev, COLORREF color )
1353 unsigned char spec_type = color >> 24;
1354 COLORREF nearest;
1356 if (!palette_size) return color;
1358 if (spec_type == 1 || spec_type == 2)
1360 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1362 UINT index;
1363 PALETTEENTRY entry;
1364 HPALETTE hpal = GetCurrentObject( dev->hdc, OBJ_PAL );
1366 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1368 if (spec_type == 2) /* PALETTERGB */
1369 index = GetNearestPaletteIndex( hpal, color );
1370 else /* PALETTEINDEX */
1371 index = LOWORD(color);
1373 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1375 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
1376 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1378 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1380 color &= 0x00ffffff;
1381 EnterCriticalSection( &palette_cs );
1382 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1383 LeaveCriticalSection( &palette_cs );
1385 TRACE("(%06x): returning %06x\n", color, nearest );
1386 return nearest;
1390 /***********************************************************************
1391 * RealizeDefaultPalette (X11DRV.@)
1393 UINT CDECL X11DRV_RealizeDefaultPalette( PHYSDEV dev )
1395 UINT ret = 0;
1397 if (palette_size && GetObjectType(dev->hdc) != OBJ_MEMDC)
1399 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1400 int i, index, *mapping = palette_get_mapping( GetStockObject(DEFAULT_PALETTE) );
1401 PALETTEENTRY entries[NB_RESERVED_COLORS];
1403 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, entries );
1404 EnterCriticalSection( &palette_cs );
1405 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1407 index = X11DRV_PALETTE_LookupSystemXPixel( RGB(entries[i].peRed,
1408 entries[i].peGreen,
1409 entries[i].peBlue) );
1410 /* mapping is allocated in COLOR_InitPalette() */
1411 if( index != mapping[i] )
1413 mapping[i]=index;
1414 ret++;
1417 LeaveCriticalSection( &palette_cs );
1419 return ret;