shlwapi: Use proper helpers for iface calls.
[wine/multimedia.git] / dlls / winex11.drv / palette.c
bloba2eb5e9adc21aafdcd9d8b94274924b28ce58e65
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;
57 static int COLOR_gapFilled = 0;
59 Colormap X11DRV_PALETTE_PaletteXColormap = 0;
60 UINT16 X11DRV_PALETTE_PaletteFlags = 0;
62 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
63 ColorShifts X11DRV_PALETTE_default_shifts = { {0,0,0,}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
64 static int X11DRV_PALETTE_Graymax = 0;
66 static int palette_size;
68 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
69 static int X11DRV_PALETTE_firstFree = 0;
70 static unsigned char X11DRV_PALETTE_freeList[256];
72 static XContext palette_context; /* X context to associate a color mapping to a palette */
74 static CRITICAL_SECTION palette_cs;
75 static CRITICAL_SECTION_DEBUG critsect_debug =
77 0, 0, &palette_cs,
78 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
79 0, 0, { (DWORD_PTR)(__FILE__ ": palette_cs") }
81 static CRITICAL_SECTION palette_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
83 /**********************************************************************/
85 /* Map an EGA index (0..15) to a pixel value in the system color space. */
87 int X11DRV_PALETTE_mapEGAPixel[16];
89 /**********************************************************************/
91 #define NB_COLORCUBE_START_INDEX 63
92 #define NB_PALETTE_EMPTY_VALUE -1
94 /* Maps entry in the system palette to X pixel value */
95 int *X11DRV_PALETTE_PaletteToXPixel = NULL;
97 /* Maps pixel to the entry in the system palette */
98 int *X11DRV_PALETTE_XPixelToPalette = NULL;
100 /**********************************************************************/
102 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template );
103 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template );
104 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template );
105 static void X11DRV_PALETTE_FormatSystemPalette(void);
106 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c);
107 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col);
110 /***********************************************************************
111 * palette_get_mapping
113 static int *palette_get_mapping( HPALETTE hpal )
115 int *mapping;
117 if (XFindContext( gdi_display, (XID)hpal, palette_context, (char **)&mapping )) mapping = NULL;
118 return mapping;
122 /***********************************************************************
123 * palette_set_mapping
125 static void palette_set_mapping( HPALETTE hpal, int *mapping )
127 XSaveContext( gdi_display, (XID)hpal, palette_context, (char *)mapping );
131 /***********************************************************************
132 * COLOR_Init
134 * Initialize color management.
136 int X11DRV_PALETTE_Init(void)
138 int mask, white, black;
139 int monoPlane;
140 int *mapping;
141 PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS];
143 TRACE("initializing palette manager...\n");
145 palette_context = XUniqueContext();
146 white = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
147 black = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
148 monoPlane = 1;
149 for( mask = 1; !((white & mask)^(black & mask)); mask <<= 1 )
150 monoPlane++;
151 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
152 palette_size = visual->map_entries;
154 switch(visual->class)
156 case DirectColor:
157 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
158 case GrayScale:
159 case PseudoColor:
160 if (private_color_map)
162 XSetWindowAttributes win_attr;
164 X11DRV_PALETTE_PaletteXColormap = XCreateColormap( gdi_display, root_window,
165 visual, AllocAll );
166 if (X11DRV_PALETTE_PaletteXColormap)
168 X11DRV_PALETTE_PaletteFlags |= (X11DRV_PALETTE_PRIVATE | X11DRV_PALETTE_WHITESET);
170 monoPlane = 1;
171 for( white = palette_size - 1; !(white & 1); white >>= 1 )
172 monoPlane++;
174 if( root_window != DefaultRootWindow(gdi_display) )
176 win_attr.colormap = X11DRV_PALETTE_PaletteXColormap;
177 XChangeWindowAttributes( gdi_display, root_window, CWColormap, &win_attr );
180 } else {
181 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
182 visual, AllocNone);
184 break;
186 case StaticGray:
187 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
188 visual, AllocNone);
189 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
190 X11DRV_PALETTE_Graymax = (1 << screen_depth)-1;
191 break;
193 case TrueColor:
194 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
195 case StaticColor: {
196 int *depths,nrofdepths;
197 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
198 * depths 1 and 4
200 depths = XListDepths(gdi_display,DefaultScreen(gdi_display),&nrofdepths);
201 if ((nrofdepths==2) && ((depths[0]==4) || depths[1]==4)) {
202 monoPlane = 1;
203 for( white = palette_size - 1; !(white & 1); white >>= 1 )
204 monoPlane++;
205 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
206 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
207 visual, AllocNone);
209 else
211 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
212 visual, AllocNone);
213 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
214 X11DRV_PALETTE_ComputeColorShifts(&X11DRV_PALETTE_default_shifts, visual->red_mask, visual->green_mask, visual->blue_mask);
216 XFree(depths);
217 break;
221 TRACE(" visual class %i (%i)\n", visual->class, monoPlane);
223 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, sys_pal_template );
225 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
227 palette_size = 0;
229 else
231 if ((mapping = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * NB_RESERVED_COLORS )))
232 palette_set_mapping( GetStockObject(DEFAULT_PALETTE), mapping );
234 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
235 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template );
236 else
237 X11DRV_PALETTE_BuildSharedMap( sys_pal_template );
239 /* Build free list */
241 if( X11DRV_PALETTE_firstFree != -1 )
242 X11DRV_PALETTE_FormatSystemPalette();
244 X11DRV_PALETTE_FillDefaultColors( sys_pal_template );
245 palette_size = visual->map_entries;
248 return palette_size;
251 /***********************************************************************
252 * X11DRV_PALETTE_Cleanup
254 * Free external colors we grabbed in the FillDefaultPalette()
256 void X11DRV_PALETTE_Cleanup(void)
258 if( COLOR_gapFilled )
260 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap,
261 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel + COLOR_gapStart),
262 COLOR_gapFilled, 0);
264 DeleteCriticalSection(&palette_cs);
267 /***********************************************************************
268 * X11DRV_PALETTE_ComputeChannelShift
270 * Calculate conversion parameters for a given color mask
272 static void X11DRV_PALETTE_ComputeChannelShift(unsigned long maskbits, ChannelShift *physical, ChannelShift *to_logical)
274 int i;
276 if (maskbits==0)
278 physical->shift=0;
279 physical->scale=0;
280 physical->max=0;
281 to_logical->shift=0;
282 to_logical->scale=0;
283 to_logical->max=0;
284 return;
287 for(i=0;!(maskbits&1);i++)
288 maskbits >>= 1;
290 physical->shift = i;
291 physical->max = maskbits;
293 for(i=0;maskbits!=0;i++)
294 maskbits >>= 1;
295 physical->scale = i;
297 if (physical->scale>8)
299 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
300 * So we adjust the shifts to also normalize the color fields to
301 * the Win32 standard of 8 bits per color.
303 to_logical->shift=physical->shift+(physical->scale-8);
304 to_logical->scale=8;
305 to_logical->max=0xff;
306 } else {
307 to_logical->shift=physical->shift;
308 to_logical->scale=physical->scale;
309 to_logical->max=physical->max;
313 /***********************************************************************
314 * X11DRV_PALETTE_ComputeColorShifts
316 * Calculate conversion parameters for a given color
318 void X11DRV_PALETTE_ComputeColorShifts(ColorShifts *shifts, unsigned long redMask, unsigned long greenMask, unsigned long blueMask)
320 X11DRV_PALETTE_ComputeChannelShift(redMask, &shifts->physicalRed, &shifts->logicalRed);
321 X11DRV_PALETTE_ComputeChannelShift(greenMask, &shifts->physicalGreen, &shifts->logicalGreen);
322 X11DRV_PALETTE_ComputeChannelShift(blueMask, &shifts->physicalBlue, &shifts->logicalBlue);
325 /***********************************************************************
326 * X11DRV_PALETTE_BuildPrivateMap
328 * Allocate colorcells and initialize mapping tables.
330 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template )
332 /* Private colormap - identity mapping */
334 XColor color;
335 int i;
337 if((COLOR_sysPal = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*palette_size)) == NULL) {
338 WARN("Unable to allocate the system palette\n");
339 return FALSE;
342 TRACE("Building private map - %i palette entries\n", palette_size);
344 /* Allocate system palette colors */
346 for( i=0; i < palette_size; i++ )
348 if( i < NB_RESERVED_COLORS/2 )
350 color.red = sys_pal_template[i].peRed * 65535 / 255;
351 color.green = sys_pal_template[i].peGreen * 65535 / 255;
352 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
353 COLOR_sysPal[i] = sys_pal_template[i];
354 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
356 else if( i >= palette_size - NB_RESERVED_COLORS/2 )
358 int j = NB_RESERVED_COLORS + i - palette_size;
359 color.red = sys_pal_template[j].peRed * 65535 / 255;
360 color.green = sys_pal_template[j].peGreen * 65535 / 255;
361 color.blue = sys_pal_template[j].peBlue * 65535 / 255;
362 COLOR_sysPal[i] = sys_pal_template[j];
363 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
366 color.flags = DoRed | DoGreen | DoBlue;
367 color.pixel = i;
368 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
370 /* Set EGA mapping if color is from the first or last eight */
372 if (i < 8)
373 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
374 else if (i >= palette_size - 8 )
375 X11DRV_PALETTE_mapEGAPixel[i - (palette_size - 16)] = color.pixel;
378 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
380 COLOR_gapStart = 256; COLOR_gapEnd = -1;
382 X11DRV_PALETTE_firstFree = (palette_size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
384 return FALSE;
387 /***********************************************************************
388 * X11DRV_PALETTE_BuildSharedMap
390 * Allocate colorcells and initialize mapping tables.
392 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template )
394 XColor color;
395 unsigned long sysPixel[NB_RESERVED_COLORS];
396 unsigned long* pixDynMapping = NULL;
397 unsigned long plane_masks[1];
398 int i, j, warn = 0;
399 int diff, r, g, b, bp = 0, wp = 1;
400 int step = 1;
401 unsigned int max = 256;
402 Colormap defaultCM;
403 XColor defaultColors[256];
405 /* Copy the first bunch of colors out of the default colormap to prevent
406 * colormap flashing as much as possible. We're likely to get the most
407 * important Window Manager colors, etc in the first 128 colors */
408 defaultCM = DefaultColormap( gdi_display, DefaultScreen(gdi_display) );
410 if (copy_default_colors > 256) copy_default_colors = 256;
411 for (i = 0; i < copy_default_colors; i++)
412 defaultColors[i].pixel = (long) i;
413 XQueryColors(gdi_display, defaultCM, &defaultColors[0], copy_default_colors);
414 for (i = 0; i < copy_default_colors; i++)
415 XAllocColor( gdi_display, X11DRV_PALETTE_PaletteXColormap, &defaultColors[i] );
417 if (alloc_system_colors > 256) alloc_system_colors = 256;
418 else if (alloc_system_colors < 20) alloc_system_colors = 20;
419 TRACE("%d colors configured.\n", alloc_system_colors);
421 TRACE("Building shared map - %i palette entries\n", palette_size);
423 /* Be nice and allocate system colors as read-only */
425 for( i = 0; i < NB_RESERVED_COLORS; i++ )
427 color.red = sys_pal_template[i].peRed * 65535 / 255;
428 color.green = sys_pal_template[i].peGreen * 65535 / 255;
429 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
430 color.flags = DoRed | DoGreen | DoBlue;
432 if (!XAllocColor( gdi_display, X11DRV_PALETTE_PaletteXColormap, &color ))
434 XColor best, c;
436 if( !warn++ )
438 WARN("Not enough colors for the full system palette.\n");
440 bp = BlackPixel(gdi_display, DefaultScreen(gdi_display));
441 wp = WhitePixel(gdi_display, DefaultScreen(gdi_display));
443 max = (0xffffffff)>>(32 - screen_depth);
444 if( max > 256 )
446 step = max/256;
447 max = 256;
451 /* reinit color (XAllocColor() may change it)
452 * and map to the best shared colorcell */
454 color.red = sys_pal_template[i].peRed * 65535 / 255;
455 color.green = sys_pal_template[i].peGreen * 65535 / 255;
456 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
458 best.pixel = best.red = best.green = best.blue = 0;
459 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
461 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &c);
462 r = (c.red - color.red)>>8;
463 g = (c.green - color.green)>>8;
464 b = (c.blue - color.blue)>>8;
465 r = r*r + g*g + b*b;
466 if( r < diff ) { best = c; diff = r; }
469 if( XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &best) )
470 color.pixel = best.pixel;
471 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
474 sysPixel[i] = color.pixel;
476 TRACE("syscolor(%x) -> pixel %i\n", *(const COLORREF*)(sys_pal_template+i),
477 (int)color.pixel);
479 /* Set EGA mapping if color in the first or last eight */
481 if (i < 8)
482 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
483 else if (i >= NB_RESERVED_COLORS - 8 )
484 X11DRV_PALETTE_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
487 /* now allocate changeable set */
489 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
491 int c_min = 0, c_max = palette_size, c_val;
493 TRACE("Dynamic colormap...\n");
495 /* let's become the first client that actually follows
496 * X guidelines and does binary search...
499 if((pixDynMapping = HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size)) == NULL) {
500 WARN("Out of memory while building system palette.\n");
501 return FALSE;
504 /* comment this out if you want to debug palette init */
505 XGrabServer(gdi_display);
507 while( c_max - c_min > 0 )
509 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
511 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
512 plane_masks, 0, pixDynMapping, c_val) )
513 c_max = c_val - 1;
514 else
516 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap, pixDynMapping, c_val, 0);
517 c_min = c_val;
521 if( c_min > alloc_system_colors - NB_RESERVED_COLORS)
522 c_min = alloc_system_colors - NB_RESERVED_COLORS;
524 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
526 if( c_min > 0 )
527 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
528 plane_masks, 0, pixDynMapping, c_min) )
530 WARN("Inexplicable failure during colorcell allocation.\n");
531 c_min = 0;
534 palette_size = c_min + NB_RESERVED_COLORS;
536 XUngrabServer(gdi_display);
538 TRACE("adjusted size %i colorcells\n", palette_size);
540 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
542 /* virtual colorspace - ToPhysical takes care of
543 * color translations but we have to allocate full palette
544 * to maintain compatibility
546 palette_size = 256;
547 TRACE("Virtual colorspace - screendepth %i\n", screen_depth);
549 else palette_size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
550 * of colors and map to them */
552 TRACE("Shared system palette uses %i colors.\n", palette_size);
554 /* set gap to account for pixel shortage. It has to be right in the center
555 * of the system palette because otherwise raster ops get screwed. */
557 if( palette_size >= 256 )
558 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
559 else
560 { COLOR_gapStart = palette_size/2; COLOR_gapEnd = 255 - palette_size/2; }
562 X11DRV_PALETTE_firstFree = ( palette_size > NB_RESERVED_COLORS &&
563 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
564 ? NB_RESERVED_COLORS/2 : -1;
566 COLOR_sysPal = HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
567 if(COLOR_sysPal == NULL) {
568 ERR("Unable to allocate the system palette!\n");
569 HeapFree(GetProcessHeap(), 0, pixDynMapping);
570 return FALSE;
573 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
575 if (screen_depth <= 8)
577 X11DRV_PALETTE_XPixelToPalette = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
578 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
579 ERR("Out of memory: XPixelToPalette!\n");
580 HeapFree(GetProcessHeap(), 0, pixDynMapping);
581 return FALSE;
583 for( i = 0; i < 256; i++ )
584 X11DRV_PALETTE_XPixelToPalette[i] = NB_PALETTE_EMPTY_VALUE;
587 /* for hicolor visuals PaletteToPixel mapping is used to skip
588 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
591 X11DRV_PALETTE_PaletteToXPixel = HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
592 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
593 ERR("Out of memory: PaletteToXPixel!\n");
594 HeapFree(GetProcessHeap(), 0, pixDynMapping);
595 return FALSE;
598 for( i = j = 0; i < 256; i++ )
600 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
602 X11DRV_PALETTE_PaletteToXPixel[i] = NB_PALETTE_EMPTY_VALUE;
603 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
604 continue;
607 if( i < NB_RESERVED_COLORS/2 )
609 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
610 COLOR_sysPal[i] = sys_pal_template[i];
611 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
613 else if( i >= 256 - NB_RESERVED_COLORS/2 )
615 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
616 COLOR_sysPal[i] = sys_pal_template[(i + NB_RESERVED_COLORS) - 256];
617 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
619 else if( pixDynMapping )
620 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
621 else
622 X11DRV_PALETTE_PaletteToXPixel[i] = i;
624 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
626 if( X11DRV_PALETTE_XPixelToPalette )
627 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
630 HeapFree(GetProcessHeap(), 0, pixDynMapping);
632 return TRUE;
635 /***********************************************************************
636 * Colormap Initialization
638 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template )
640 /* initialize unused entries to what Windows uses as a color
641 * cube - based on Greg Kreider's code.
644 int i = 0, idx = 0;
645 int red, no_r, inc_r;
646 int green, no_g, inc_g;
647 int blue, no_b, inc_b;
649 if (palette_size <= NB_RESERVED_COLORS)
650 return;
651 while (i*i*i < (palette_size - NB_RESERVED_COLORS)) i++;
652 no_r = no_g = no_b = --i;
653 if ((no_r * (no_g+1) * no_b) < (palette_size - NB_RESERVED_COLORS)) no_g++;
654 if ((no_r * no_g * (no_b+1)) < (palette_size - NB_RESERVED_COLORS)) no_b++;
655 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
656 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
657 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
659 idx = X11DRV_PALETTE_firstFree;
661 if( idx != -1 )
662 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
663 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
664 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
666 /* weird but true */
668 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
670 COLOR_sysPal[idx].peRed = red;
671 COLOR_sysPal[idx].peGreen = green;
672 COLOR_sysPal[idx].peBlue = blue;
674 /* set X color */
676 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
678 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
679 if (shifts->physicalRed.max != 255) no_r = (red * shifts->physicalRed.max) / 255;
680 if (shifts->physicalGreen.max != 255) no_g = (green * shifts->physicalGreen.max) / 255;
681 if (shifts->physicalBlue.max != 255) no_b = (blue * shifts->physicalBlue.max) / 255;
683 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << shifts->physicalRed.shift) | (no_g << shifts->physicalGreen.shift) | (no_b << shifts->physicalBlue.shift);
685 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
687 XColor color;
688 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
689 color.red = COLOR_sysPal[idx].peRed << 8;
690 color.green = COLOR_sysPal[idx].peGreen << 8;
691 color.blue = COLOR_sysPal[idx].peBlue << 8;
692 color.flags = DoRed | DoGreen | DoBlue;
693 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
695 idx = X11DRV_PALETTE_freeList[idx];
698 /* try to fill some entries in the "gap" with
699 * what's already in the colormap - they will be
700 * mappable to but not changeable. */
702 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
704 XColor xc;
705 int r, g, b, max;
707 max = alloc_system_colors - (256 - (COLOR_gapEnd - COLOR_gapStart));
708 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
709 if( X11DRV_PALETTE_XPixelToPalette[i] == NB_PALETTE_EMPTY_VALUE )
711 xc.pixel = i;
713 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc);
714 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
716 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template, RGB(r, g, b)) &&
717 XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc) )
719 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
720 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
721 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
722 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
723 if( --max <= 0 ) break;
726 COLOR_gapFilled = idx - COLOR_gapStart;
731 /***********************************************************************
732 * X11DRV_IsSolidColor
734 * Check whether 'color' can be represented with a solid color.
736 BOOL X11DRV_IsSolidColor( COLORREF color )
738 int i;
739 const PALETTEENTRY *pEntry = COLOR_sysPal;
741 if (color & 0xff000000) return TRUE; /* indexed color */
743 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
745 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return TRUE; /* no palette */
747 EnterCriticalSection( &palette_cs );
748 for (i = 0; i < palette_size ; i++, pEntry++)
750 if( i < COLOR_gapStart || i > COLOR_gapEnd )
751 if ((GetRValue(color) == pEntry->peRed) &&
752 (GetGValue(color) == pEntry->peGreen) &&
753 (GetBValue(color) == pEntry->peBlue))
755 LeaveCriticalSection( &palette_cs );
756 return TRUE;
759 LeaveCriticalSection( &palette_cs );
760 return FALSE;
764 /***********************************************************************
765 * X11DRV_PALETTE_ToLogical
767 * Return RGB color for given X pixel.
769 COLORREF X11DRV_PALETTE_ToLogical(X11DRV_PDEVICE *physDev, int pixel)
771 XColor color;
773 #if 0
774 /* truecolor visual */
776 if (screen_depth >= 24) return pixel;
777 #endif
779 /* check for hicolor visuals first */
781 if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
783 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
785 if(physDev->color_shifts)
786 shifts = physDev->color_shifts;
788 color.red = (pixel >> shifts->logicalRed.shift) & shifts->logicalRed.max;
789 if (shifts->logicalRed.scale<8)
790 color.red= color.red << (8-shifts->logicalRed.scale) |
791 color.red >> (2*shifts->logicalRed.scale-8);
792 color.green = (pixel >> shifts->logicalGreen.shift) & shifts->logicalGreen.max;
793 if (shifts->logicalGreen.scale<8)
794 color.green=color.green << (8-shifts->logicalGreen.scale) |
795 color.green >> (2*shifts->logicalGreen.scale-8);
796 color.blue = (pixel >> shifts->logicalBlue.shift) & shifts->logicalBlue.max;
797 if (shifts->logicalBlue.scale<8)
798 color.blue= color.blue << (8-shifts->logicalBlue.scale) |
799 color.blue >> (2*shifts->logicalBlue.scale-8);
800 return RGB(color.red,color.green,color.blue);
803 /* check if we can bypass X */
805 if ((screen_depth <= 8) && (pixel < 256) &&
806 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) ) {
807 COLORREF ret;
808 EnterCriticalSection( &palette_cs );
809 ret = *(COLORREF *)(COLOR_sysPal + (X11DRV_PALETTE_XPixelToPalette ? X11DRV_PALETTE_XPixelToPalette[pixel]: pixel)) & 0x00ffffff;
810 LeaveCriticalSection( &palette_cs );
811 return ret;
814 color.pixel = pixel;
815 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
816 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
820 /***********************************************************************
821 * X11DRV_SysPaletteLookupPixel
823 static int X11DRV_SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
825 int i, best = 0, diff = 0x7fffffff;
826 int r,g,b;
828 for( i = 0; i < palette_size && diff ; i++ )
830 if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) ||
831 (skipReserved && COLOR_sysPal[i].peFlags & PC_SYS_RESERVED) )
832 continue;
834 r = COLOR_sysPal[i].peRed - GetRValue(col);
835 g = COLOR_sysPal[i].peGreen - GetGValue(col);
836 b = COLOR_sysPal[i].peBlue - GetBValue(col);
838 r = r*r + g*g + b*b;
840 if( r < diff ) { best = i; diff = r; }
842 return best;
846 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
848 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
849 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
852 /***********************************************************************
853 * X11DRV_PALETTE_GetColor
855 * Resolve PALETTEINDEX/PALETTERGB/DIBINDEX COLORREFs to an RGB COLORREF.
857 COLORREF X11DRV_PALETTE_GetColor( X11DRV_PDEVICE *physDev, COLORREF color )
859 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
860 PALETTEENTRY entry;
862 if (color & (1 << 24)) /* PALETTEINDEX */
864 unsigned int idx = LOWORD(color);
865 if (!GetPaletteEntries( hPal, idx, 1, &entry )) return 0;
866 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
869 if (color >> 24 == 2) /* PALETTERGB */
871 unsigned int idx = GetNearestPaletteIndex( hPal, color & 0xffffff );
872 if (!GetPaletteEntries( hPal, idx, 1, &entry )) return 0;
873 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
876 if (color >> 16 == 0x10ff) /* DIBINDEX */
877 return 0;
879 return color & 0xffffff;
882 /***********************************************************************
883 * X11DRV_PALETTE_ToPhysical
885 * Return the physical color closest to 'color'.
887 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
889 WORD index = 0;
890 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
891 int *mapping = palette_get_mapping( hPal );
892 PALETTEENTRY entry;
893 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
895 if(physDev->color_shifts)
896 shifts = physDev->color_shifts;
898 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
900 /* there is no colormap limitation; we are going to have to compute
901 * the pixel value from the visual information stored earlier
903 unsigned long red, green, blue;
905 if (color & (1 << 24)) /* PALETTEINDEX */
907 unsigned int idx = LOWORD( color );
909 if (!GetPaletteEntries( hPal, idx, 1, &entry ))
911 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color, idx);
912 return 0;
914 if (mapping) return mapping[idx];
915 red = entry.peRed;
916 green = entry.peGreen;
917 blue = entry.peBlue;
919 else if (color >> 16 == 0x10ff) /* DIBINDEX */
921 return 0;
923 else /* RGB */
925 if (physDev->depth == 1)
926 return (((color >> 16) & 0xff) +
927 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
928 red = GetRValue( color );
929 green = GetGValue( color );
930 blue = GetBValue( color );
933 if (X11DRV_PALETTE_Graymax)
935 /* grayscale only; return scaled value */
936 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
938 else
940 /* scale each individually and construct the TrueColor pixel value */
941 if (shifts->physicalRed.scale < 8)
942 red = red >> (8-shifts->physicalRed.scale);
943 else if (shifts->physicalRed.scale > 8)
944 red = red << (shifts->physicalRed.scale-8) |
945 red >> (16-shifts->physicalRed.scale);
946 if (shifts->physicalGreen.scale < 8)
947 green = green >> (8-shifts->physicalGreen.scale);
948 else if (shifts->physicalGreen.scale > 8)
949 green = green << (shifts->physicalGreen.scale-8) |
950 green >> (16-shifts->physicalGreen.scale);
951 if (shifts->physicalBlue.scale < 8)
952 blue = blue >> (8-shifts->physicalBlue.scale);
953 else if (shifts->physicalBlue.scale > 8)
954 blue = blue << (shifts->physicalBlue.scale-8) |
955 blue >> (16-shifts->physicalBlue.scale);
957 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
960 else
962 if (!mapping)
963 WARN("Palette %p is not realized\n", hPal);
965 if (color & (1 << 24)) /* PALETTEINDEX */
967 index = LOWORD( color );
968 if (!GetPaletteEntries( hPal, index, 1, &entry ))
969 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color, index);
970 else if (mapping) index = mapping[index];
972 else if (color >> 24 == 2) /* PALETTERGB */
974 index = GetNearestPaletteIndex( hPal, color );
975 if (mapping) index = mapping[index];
977 else if (color >> 16 == 0x10ff) /* DIBINDEX */
979 return 0;
981 else /* RGB */
983 if (physDev->depth == 1)
984 return (((color >> 16) & 0xff) +
985 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
987 EnterCriticalSection( &palette_cs );
988 index = X11DRV_SysPaletteLookupPixel( color & 0xffffff, FALSE);
989 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
990 LeaveCriticalSection( &palette_cs );
993 return index;
996 /***********************************************************************
997 * X11DRV_PALETTE_LookupPixel
999 int X11DRV_PALETTE_LookupPixel(ColorShifts *shifts, COLORREF color )
1001 unsigned char spec_type = color >> 24;
1003 /* Only accept RGB which has spec_type = 0 */
1004 if(spec_type)
1005 return 0;
1007 color &= 0xffffff;
1009 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
1011 unsigned long red, green, blue;
1012 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
1014 if (X11DRV_PALETTE_Graymax)
1016 /* grayscale only; return scaled value */
1017 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
1019 else
1021 /* No shifts are set in case of 1-bit */
1022 if(!shifts) shifts = &X11DRV_PALETTE_default_shifts;
1024 /* scale each individually and construct the TrueColor pixel value */
1025 if (shifts->physicalRed.scale < 8)
1026 red = red >> (8-shifts->physicalRed.scale);
1027 else if (shifts->physicalRed.scale > 8)
1028 red = red << (shifts->physicalRed.scale-8) |
1029 red >> (16-shifts->physicalRed.scale);
1030 if (shifts->physicalGreen.scale < 8)
1031 green = green >> (8-shifts->physicalGreen.scale);
1032 else if (shifts->physicalGreen.scale > 8)
1033 green = green << (shifts->physicalGreen.scale-8) |
1034 green >> (16-shifts->physicalGreen.scale);
1035 if (shifts->physicalBlue.scale < 8)
1036 blue = blue >> (8-shifts->physicalBlue.scale);
1037 else if (shifts->physicalBlue.scale > 8)
1038 blue = blue << (shifts->physicalBlue.scale-8) |
1039 blue >> (16-shifts->physicalBlue.scale);
1041 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
1044 else
1046 WORD index;
1047 HPALETTE hPal = GetStockObject(DEFAULT_PALETTE);
1048 int *mapping = palette_get_mapping( hPal );
1050 if (!mapping)
1051 WARN("Palette %p is not realized\n", hPal);
1053 EnterCriticalSection( &palette_cs );
1054 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
1055 if (X11DRV_PALETTE_PaletteToXPixel)
1056 index = X11DRV_PALETTE_PaletteToXPixel[index];
1057 LeaveCriticalSection( &palette_cs );
1058 return index;
1063 /***********************************************************************
1064 * X11DRV_PALETTE_LookupSystemXPixel
1066 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
1068 int i, best = 0, diff = 0x7fffffff;
1069 int size = palette_size;
1070 int r,g,b;
1072 for( i = 0; i < size && diff ; i++ )
1074 if( i == NB_RESERVED_COLORS/2 )
1076 int newi = size - NB_RESERVED_COLORS/2;
1077 if (newi>i) i=newi;
1080 r = COLOR_sysPal[i].peRed - GetRValue(col);
1081 g = COLOR_sysPal[i].peGreen - GetGValue(col);
1082 b = COLOR_sysPal[i].peBlue - GetBValue(col);
1084 r = r*r + g*g + b*b;
1086 if( r < diff ) { best = i; diff = r; }
1089 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
1092 /***********************************************************************
1093 * X11DRV_PALETTE_FormatSystemPalette
1095 static void X11DRV_PALETTE_FormatSystemPalette(void)
1097 /* Build free list so we'd have an easy way to find
1098 * out if there are any available colorcells.
1101 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1103 COLOR_sysPal[j].peFlags = 0;
1104 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1105 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1107 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1108 X11DRV_PALETTE_freeList[j] = i; /* next */
1109 j = i;
1111 X11DRV_PALETTE_freeList[j] = 0;
1114 /***********************************************************************
1115 * X11DRV_PALETTE_CheckSysColor
1117 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1119 int i;
1120 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1121 if( c == (*(const COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1122 return 0;
1123 return 1;
1127 /***********************************************************************
1128 * X11DRV_LookupSysPaletteExact
1130 static int X11DRV_LookupSysPaletteExact( BYTE r, BYTE g, BYTE b )
1132 int i;
1133 for( i = 0; i < palette_size; i++ )
1135 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1136 if( COLOR_sysPal[i].peRed == r &&
1137 COLOR_sysPal[i].peGreen == g &&
1138 COLOR_sysPal[i].peBlue == b )
1139 return i;
1141 return -1;
1145 /***********************************************************************
1146 * RealizePalette (X11DRV.@)
1148 UINT X11DRV_RealizePalette( PHYSDEV dev, HPALETTE hpal, BOOL primary )
1150 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1151 char flag;
1152 int index;
1153 UINT i, iRemapped = 0;
1154 int *prev_mapping, *mapping;
1155 PALETTEENTRY entries[256];
1156 WORD num_entries;
1158 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1160 if (!GetObjectW( hpal, sizeof(num_entries), &num_entries )) return 0;
1162 /* initialize palette mapping table */
1163 prev_mapping = palette_get_mapping( hpal );
1164 if (prev_mapping)
1165 mapping = HeapReAlloc( GetProcessHeap(), 0, prev_mapping, sizeof(int)*num_entries);
1166 else
1167 mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries);
1169 if(mapping == NULL) {
1170 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1171 return 0;
1173 palette_set_mapping( hpal, mapping );
1175 if (num_entries > 256)
1177 FIXME( "more than 256 entries not supported\n" );
1178 num_entries = 256;
1180 if (!(num_entries = GetPaletteEntries( hpal, 0, num_entries, entries ))) return 0;
1182 /* reset dynamic system palette entries */
1184 EnterCriticalSection( &palette_cs );
1185 if( primary && X11DRV_PALETTE_firstFree != -1)
1186 X11DRV_PALETTE_FormatSystemPalette();
1188 for (i = 0; i < num_entries; i++)
1190 index = -1;
1191 flag = PC_SYS_USED;
1193 /* Even though the docs say that only one flag is to be set,
1194 * they are a bitmask. At least one app sets more than one at
1195 * the same time. */
1196 if ( entries[i].peFlags & PC_EXPLICIT ) {
1197 /* palette entries are indices into system palette */
1198 index = *(WORD*)&entries[i];
1199 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1201 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1202 index = 0;
1204 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1205 } else {
1206 if ( entries[i].peFlags & PC_RESERVED ) {
1207 /* forbid future mappings to this entry */
1208 flag |= PC_SYS_RESERVED;
1211 if (! (entries[i].peFlags & PC_NOCOLLAPSE) ) {
1212 /* try to collapse identical colors */
1213 index = X11DRV_LookupSysPaletteExact( entries[i].peRed, entries[i].peGreen, entries[i].peBlue );
1216 if( index < 0 )
1218 if( X11DRV_PALETTE_firstFree > 0 )
1220 XColor color;
1221 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1222 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1224 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1225 color.red = entries[i].peRed << 8;
1226 color.green = entries[i].peGreen << 8;
1227 color.blue = entries[i].peBlue << 8;
1228 color.flags = DoRed | DoGreen | DoBlue;
1229 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
1231 COLOR_sysPal[index] = entries[i];
1232 COLOR_sysPal[index].peFlags = flag;
1233 X11DRV_PALETTE_freeList[index] = 0;
1235 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1237 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1239 index = X11DRV_PALETTE_LookupPixel( physDev->color_shifts, RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ));
1242 /* we have to map to existing entry in the system palette */
1244 index = X11DRV_SysPaletteLookupPixel( RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ),
1245 TRUE );
1248 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1251 if( !prev_mapping || mapping[i] != index ) iRemapped++;
1252 mapping[i] = index;
1254 TRACE("entry %i (%x) -> pixel %i\n", i, *(COLORREF*)&entries[i], index);
1257 LeaveCriticalSection( &palette_cs );
1258 return iRemapped;
1262 /***********************************************************************
1263 * UnrealizePalette (X11DRV.@)
1265 BOOL X11DRV_UnrealizePalette( HPALETTE hpal )
1267 int *mapping = palette_get_mapping( hpal );
1269 if (mapping)
1271 XDeleteContext( gdi_display, (XID)hpal, palette_context );
1272 HeapFree( GetProcessHeap(), 0, mapping );
1274 return TRUE;
1278 /***********************************************************************
1279 * GetSystemPaletteEntries (X11DRV.@)
1281 UINT X11DRV_GetSystemPaletteEntries( PHYSDEV dev, UINT start, UINT count, LPPALETTEENTRY entries )
1283 UINT i;
1285 if (!entries) return palette_size;
1286 if (start >= palette_size) return 0;
1287 if (start + count >= palette_size) count = palette_size - start;
1289 EnterCriticalSection( &palette_cs );
1290 for (i = 0; i < count; i++)
1292 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1293 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1294 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1295 entries[i].peFlags = 0;
1296 TRACE("\tidx(%02x) -> RGB(%08x)\n", start + i, *(COLORREF*)(entries + i) );
1298 LeaveCriticalSection( &palette_cs );
1299 return count;
1303 /***********************************************************************
1304 * GetNearestColor (X11DRV.@)
1306 COLORREF X11DRV_GetNearestColor( PHYSDEV dev, COLORREF color )
1308 unsigned char spec_type = color >> 24;
1309 COLORREF nearest;
1311 if (!palette_size) return color;
1313 if (spec_type == 1 || spec_type == 2)
1315 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1317 UINT index;
1318 PALETTEENTRY entry;
1319 HPALETTE hpal = GetCurrentObject( dev->hdc, OBJ_PAL );
1321 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1323 if (spec_type == 2) /* PALETTERGB */
1324 index = GetNearestPaletteIndex( hpal, color );
1325 else /* PALETTEINDEX */
1326 index = LOWORD(color);
1328 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1330 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
1331 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1333 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1335 color &= 0x00ffffff;
1336 EnterCriticalSection( &palette_cs );
1337 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1338 LeaveCriticalSection( &palette_cs );
1340 TRACE("(%06x): returning %06x\n", color, nearest );
1341 return nearest;
1345 /***********************************************************************
1346 * RealizeDefaultPalette (X11DRV.@)
1348 UINT X11DRV_RealizeDefaultPalette( PHYSDEV dev )
1350 UINT ret = 0;
1352 if (palette_size && GetObjectType(dev->hdc) != OBJ_MEMDC)
1354 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1355 int i, index, *mapping = palette_get_mapping( GetStockObject(DEFAULT_PALETTE) );
1356 PALETTEENTRY entries[NB_RESERVED_COLORS];
1358 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, entries );
1359 EnterCriticalSection( &palette_cs );
1360 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1362 index = X11DRV_PALETTE_LookupSystemXPixel( RGB(entries[i].peRed,
1363 entries[i].peGreen,
1364 entries[i].peBlue) );
1365 /* mapping is allocated in COLOR_InitPalette() */
1366 if( index != mapping[i] )
1368 mapping[i]=index;
1369 ret++;
1372 LeaveCriticalSection( &palette_cs );
1374 return ret;