winedump: Add more image debug types.
[wine.git] / dlls / winex11.drv / palette.c
bloba399ec9552cde9bbcd082d00f89148ea6aff54bb
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;
120 /***********************************************************************
121 * palette_set_mapping
123 static void palette_set_mapping( HPALETTE hpal, int *mapping )
125 XSaveContext( gdi_display, (XID)hpal, palette_context, (char *)mapping );
129 /***********************************************************************
130 * X11DRV_PALETTE_ComputeChannelShift
132 * Calculate conversion parameters for a given color mask
134 static void X11DRV_PALETTE_ComputeChannelShift(unsigned long maskbits, ChannelShift *physical, ChannelShift *to_logical)
136 int i;
138 if (maskbits==0)
140 physical->shift=0;
141 physical->scale=0;
142 physical->max=0;
143 to_logical->shift=0;
144 to_logical->scale=0;
145 to_logical->max=0;
146 return;
149 for(i=0;!(maskbits&1);i++)
150 maskbits >>= 1;
152 physical->shift = i;
153 physical->max = maskbits;
155 for(i=0;maskbits!=0;i++)
156 maskbits >>= 1;
157 physical->scale = i;
159 if (physical->scale>8)
161 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
162 * So we adjust the shifts to also normalize the color fields to
163 * the Win32 standard of 8 bits per color.
165 to_logical->shift=physical->shift+(physical->scale-8);
166 to_logical->scale=8;
167 to_logical->max=0xff;
168 } else {
169 to_logical->shift=physical->shift;
170 to_logical->scale=physical->scale;
171 to_logical->max=physical->max;
175 /***********************************************************************
176 * X11DRV_PALETTE_ComputeColorShifts
178 * Calculate conversion parameters for a given color
180 static void X11DRV_PALETTE_ComputeColorShifts(ColorShifts *shifts, unsigned long redMask, unsigned long greenMask, unsigned long blueMask)
182 X11DRV_PALETTE_ComputeChannelShift(redMask, &shifts->physicalRed, &shifts->logicalRed);
183 X11DRV_PALETTE_ComputeChannelShift(greenMask, &shifts->physicalGreen, &shifts->logicalGreen);
184 X11DRV_PALETTE_ComputeChannelShift(blueMask, &shifts->physicalBlue, &shifts->logicalBlue);
187 /***********************************************************************
188 * COLOR_Init
190 * Initialize color management.
192 int X11DRV_PALETTE_Init(void)
194 int *mapping;
195 PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS];
197 TRACE("initializing palette manager...\n");
199 palette_context = XUniqueContext();
200 palette_size = default_visual.colormap_size;
202 switch(default_visual.class)
204 case DirectColor:
205 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
206 /* fall through */
207 case GrayScale:
208 case PseudoColor:
209 if (private_color_map)
211 XSetWindowAttributes win_attr;
213 XFreeColormap( gdi_display, default_colormap );
214 default_colormap = XCreateColormap( gdi_display, root_window,
215 default_visual.visual, AllocAll );
216 if (default_colormap)
218 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_PRIVATE;
220 if( root_window != DefaultRootWindow(gdi_display) )
222 win_attr.colormap = default_colormap;
223 XChangeWindowAttributes( gdi_display, root_window, CWColormap, &win_attr );
227 break;
229 case StaticGray:
230 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
231 X11DRV_PALETTE_Graymax = (1 << default_visual.depth)-1;
232 break;
234 case TrueColor:
235 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
236 /* fall through */
237 case StaticColor:
238 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
239 X11DRV_PALETTE_ComputeColorShifts(&X11DRV_PALETTE_default_shifts, default_visual.red_mask, default_visual.green_mask, default_visual.blue_mask);
240 break;
243 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
245 palette_size = 0;
247 else
249 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, sys_pal_template );
251 if ((mapping = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * NB_RESERVED_COLORS )))
252 palette_set_mapping( GetStockObject(DEFAULT_PALETTE), mapping );
254 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
255 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template );
256 else
257 X11DRV_PALETTE_BuildSharedMap( sys_pal_template );
259 /* Build free list */
261 if( X11DRV_PALETTE_firstFree != -1 )
262 X11DRV_PALETTE_FormatSystemPalette();
264 X11DRV_PALETTE_FillDefaultColors( sys_pal_template );
265 palette_size = default_visual.colormap_size;
268 return palette_size;
271 /***********************************************************************
272 * X11DRV_PALETTE_BuildPrivateMap
274 * Allocate colorcells and initialize mapping tables.
276 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template )
278 /* Private colormap - identity mapping */
280 XColor color;
281 int i;
283 if((COLOR_sysPal = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*palette_size)) == NULL) {
284 WARN("Unable to allocate the system palette\n");
285 return FALSE;
288 TRACE("Building private map - %i palette entries\n", palette_size);
290 /* Allocate system palette colors */
292 for( i=0; i < palette_size; i++ )
294 if( i < NB_RESERVED_COLORS/2 )
296 color.red = sys_pal_template[i].peRed * 65535 / 255;
297 color.green = sys_pal_template[i].peGreen * 65535 / 255;
298 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
299 COLOR_sysPal[i] = sys_pal_template[i];
300 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
302 else if( i >= palette_size - NB_RESERVED_COLORS/2 )
304 int j = NB_RESERVED_COLORS + i - palette_size;
305 color.red = sys_pal_template[j].peRed * 65535 / 255;
306 color.green = sys_pal_template[j].peGreen * 65535 / 255;
307 color.blue = sys_pal_template[j].peBlue * 65535 / 255;
308 COLOR_sysPal[i] = sys_pal_template[j];
309 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
312 color.flags = DoRed | DoGreen | DoBlue;
313 color.pixel = i;
314 XStoreColor(gdi_display, default_colormap, &color);
316 /* Set EGA mapping if color is from the first or last eight */
318 if (i < 8)
319 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
320 else if (i >= palette_size - 8 )
321 X11DRV_PALETTE_mapEGAPixel[i - (palette_size - 16)] = color.pixel;
324 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
326 COLOR_gapStart = 256; COLOR_gapEnd = -1;
328 X11DRV_PALETTE_firstFree = (palette_size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
330 return FALSE;
333 /***********************************************************************
334 * X11DRV_PALETTE_BuildSharedMap
336 * Allocate colorcells and initialize mapping tables.
338 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template )
340 XColor color;
341 unsigned long sysPixel[NB_RESERVED_COLORS];
342 unsigned long* pixDynMapping = NULL;
343 unsigned long plane_masks[1];
344 int i, j, warn = 0;
345 int diff, r, g, b, bp = 0, wp = 1;
346 int step = 1;
347 unsigned int max = 256;
348 Colormap defaultCM;
349 XColor defaultColors[256];
351 /* Copy the first bunch of colors out of the default colormap to prevent
352 * colormap flashing as much as possible. We're likely to get the most
353 * important Window Manager colors, etc in the first 128 colors */
354 defaultCM = DefaultColormap( gdi_display, DefaultScreen(gdi_display) );
356 if (copy_default_colors > 256) copy_default_colors = 256;
357 for (i = 0; i < copy_default_colors; i++)
358 defaultColors[i].pixel = (long) i;
359 XQueryColors(gdi_display, defaultCM, &defaultColors[0], copy_default_colors);
360 for (i = 0; i < copy_default_colors; i++)
361 XAllocColor( gdi_display, default_colormap, &defaultColors[i] );
363 if (alloc_system_colors > 256) alloc_system_colors = 256;
364 else if (alloc_system_colors < 20) alloc_system_colors = 20;
365 TRACE("%d colors configured.\n", alloc_system_colors);
367 TRACE("Building shared map - %i palette entries\n", palette_size);
369 /* Be nice and allocate system colors as read-only */
371 for( i = 0; i < NB_RESERVED_COLORS; i++ )
373 color.red = sys_pal_template[i].peRed * 65535 / 255;
374 color.green = sys_pal_template[i].peGreen * 65535 / 255;
375 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
376 color.flags = DoRed | DoGreen | DoBlue;
378 if (!XAllocColor( gdi_display, default_colormap, &color ))
380 XColor best, c;
382 if( !warn++ )
384 WARN("Not enough colors for the full system palette.\n");
386 bp = BlackPixel(gdi_display, DefaultScreen(gdi_display));
387 wp = WhitePixel(gdi_display, DefaultScreen(gdi_display));
389 max = 0xffffffff >> (32 - default_visual.depth);
390 if( max > 256 )
392 step = max/256;
393 max = 256;
397 /* reinit color (XAllocColor() may change it)
398 * and map to the best shared colorcell */
400 color.red = sys_pal_template[i].peRed * 65535 / 255;
401 color.green = sys_pal_template[i].peGreen * 65535 / 255;
402 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
404 best.pixel = best.red = best.green = best.blue = 0;
405 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
407 XQueryColor(gdi_display, default_colormap, &c);
408 r = (c.red - color.red)>>8;
409 g = (c.green - color.green)>>8;
410 b = (c.blue - color.blue)>>8;
411 r = r*r + g*g + b*b;
412 if( r < diff ) { best = c; diff = r; }
415 if( XAllocColor(gdi_display, default_colormap, &best) )
416 color.pixel = best.pixel;
417 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
420 sysPixel[i] = color.pixel;
422 TRACE("syscolor(%x) -> pixel %i\n", *(const COLORREF*)(sys_pal_template+i),
423 (int)color.pixel);
425 /* Set EGA mapping if color in the first or last eight */
427 if (i < 8)
428 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
429 else if (i >= NB_RESERVED_COLORS - 8 )
430 X11DRV_PALETTE_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
433 /* now allocate changeable set */
435 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
437 int c_min = 0, c_max = palette_size, c_val;
439 TRACE("Dynamic colormap...\n");
441 /* let's become the first client that actually follows
442 * X guidelines and does binary search...
445 if((pixDynMapping = HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size)) == NULL) {
446 WARN("Out of memory while building system palette.\n");
447 return FALSE;
450 /* comment this out if you want to debug palette init */
451 XGrabServer(gdi_display);
453 while( c_max - c_min > 0 )
455 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
457 if( !XAllocColorCells(gdi_display, default_colormap, False,
458 plane_masks, 0, pixDynMapping, c_val) )
459 c_max = c_val - 1;
460 else
462 XFreeColors(gdi_display, default_colormap, pixDynMapping, c_val, 0);
463 c_min = c_val;
467 if( c_min > alloc_system_colors - NB_RESERVED_COLORS)
468 c_min = alloc_system_colors - NB_RESERVED_COLORS;
470 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
472 if( c_min > 0 )
473 if( !XAllocColorCells(gdi_display, default_colormap, False,
474 plane_masks, 0, pixDynMapping, c_min) )
476 WARN("Inexplicable failure during colorcell allocation.\n");
477 c_min = 0;
480 palette_size = c_min + NB_RESERVED_COLORS;
482 XUngrabServer(gdi_display);
484 TRACE("adjusted size %i colorcells\n", palette_size);
486 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
488 /* virtual colorspace - ToPhysical takes care of
489 * color translations but we have to allocate full palette
490 * to maintain compatibility
492 palette_size = 256;
493 TRACE("Virtual colorspace - screendepth %i\n", default_visual.depth);
495 else palette_size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
496 * of colors and map to them */
498 TRACE("Shared system palette uses %i colors.\n", palette_size);
500 /* set gap to account for pixel shortage. It has to be right in the center
501 * of the system palette because otherwise raster ops get screwed. */
503 if( palette_size >= 256 )
504 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
505 else
506 { COLOR_gapStart = palette_size/2; COLOR_gapEnd = 255 - palette_size/2; }
508 X11DRV_PALETTE_firstFree = ( palette_size > NB_RESERVED_COLORS &&
509 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
510 ? NB_RESERVED_COLORS/2 : -1;
512 COLOR_sysPal = HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
513 if(COLOR_sysPal == NULL) {
514 ERR("Unable to allocate the system palette!\n");
515 HeapFree(GetProcessHeap(), 0, pixDynMapping);
516 return FALSE;
519 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
521 if (default_visual.depth <= 8)
523 X11DRV_PALETTE_XPixelToPalette = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
524 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
525 ERR("Out of memory: XPixelToPalette!\n");
526 HeapFree(GetProcessHeap(), 0, pixDynMapping);
527 return FALSE;
529 for( i = 0; i < 256; i++ )
530 X11DRV_PALETTE_XPixelToPalette[i] = NB_PALETTE_EMPTY_VALUE;
533 /* for hicolor visuals PaletteToPixel mapping is used to skip
534 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
537 X11DRV_PALETTE_PaletteToXPixel = HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
538 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
539 ERR("Out of memory: PaletteToXPixel!\n");
540 HeapFree(GetProcessHeap(), 0, pixDynMapping);
541 return FALSE;
544 for( i = j = 0; i < 256; i++ )
546 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
548 X11DRV_PALETTE_PaletteToXPixel[i] = NB_PALETTE_EMPTY_VALUE;
549 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
550 continue;
553 if( i < NB_RESERVED_COLORS/2 )
555 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
556 COLOR_sysPal[i] = sys_pal_template[i];
557 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
559 else if( i >= 256 - NB_RESERVED_COLORS/2 )
561 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
562 COLOR_sysPal[i] = sys_pal_template[(i + NB_RESERVED_COLORS) - 256];
563 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
565 else if( pixDynMapping )
566 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
567 else
568 X11DRV_PALETTE_PaletteToXPixel[i] = i;
570 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
572 if( X11DRV_PALETTE_XPixelToPalette )
573 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
576 HeapFree(GetProcessHeap(), 0, pixDynMapping);
578 return TRUE;
581 /***********************************************************************
582 * Colormap Initialization
584 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template )
586 /* initialize unused entries to what Windows uses as a color
587 * cube - based on Greg Kreider's code.
590 int i = 0, idx = 0;
591 int red, no_r, inc_r;
592 int green, no_g, inc_g;
593 int blue, no_b, inc_b;
595 if (palette_size <= NB_RESERVED_COLORS)
596 return;
597 while (i*i*i < (palette_size - NB_RESERVED_COLORS)) i++;
598 no_r = no_g = no_b = --i;
599 if ((no_r * (no_g+1) * no_b) < (palette_size - NB_RESERVED_COLORS)) no_g++;
600 if ((no_r * no_g * (no_b+1)) < (palette_size - NB_RESERVED_COLORS)) no_b++;
601 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
602 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
603 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
605 idx = X11DRV_PALETTE_firstFree;
607 if( idx != -1 )
608 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
609 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
610 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
612 /* weird but true */
614 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
616 COLOR_sysPal[idx].peRed = red;
617 COLOR_sysPal[idx].peGreen = green;
618 COLOR_sysPal[idx].peBlue = blue;
620 /* set X color */
622 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
624 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
625 if (shifts->physicalRed.max != 255) no_r = (red * shifts->physicalRed.max) / 255;
626 if (shifts->physicalGreen.max != 255) no_g = (green * shifts->physicalGreen.max) / 255;
627 if (shifts->physicalBlue.max != 255) no_b = (blue * shifts->physicalBlue.max) / 255;
629 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << shifts->physicalRed.shift) | (no_g << shifts->physicalGreen.shift) | (no_b << shifts->physicalBlue.shift);
631 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
633 XColor color;
634 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
635 color.red = COLOR_sysPal[idx].peRed << 8;
636 color.green = COLOR_sysPal[idx].peGreen << 8;
637 color.blue = COLOR_sysPal[idx].peBlue << 8;
638 color.flags = DoRed | DoGreen | DoBlue;
639 XStoreColor(gdi_display, default_colormap, &color);
641 idx = X11DRV_PALETTE_freeList[idx];
644 /* try to fill some entries in the "gap" with
645 * what's already in the colormap - they will be
646 * mappable to but not changeable. */
648 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
650 XColor xc;
651 int r, g, b, max;
653 max = alloc_system_colors - (256 - (COLOR_gapEnd - COLOR_gapStart));
654 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
655 if( X11DRV_PALETTE_XPixelToPalette[i] == NB_PALETTE_EMPTY_VALUE )
657 xc.pixel = i;
659 XQueryColor(gdi_display, default_colormap, &xc);
660 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
662 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template, RGB(r, g, b)) &&
663 XAllocColor(gdi_display, default_colormap, &xc) )
665 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
666 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
667 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
668 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
669 if( --max <= 0 ) break;
676 /***********************************************************************
677 * X11DRV_IsSolidColor
679 * Check whether 'color' can be represented with a solid color.
681 BOOL X11DRV_IsSolidColor( COLORREF color )
683 int i;
684 const PALETTEENTRY *pEntry = COLOR_sysPal;
686 if (color & 0xff000000) return TRUE; /* indexed color */
688 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
690 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return TRUE; /* no palette */
692 EnterCriticalSection( &palette_cs );
693 for (i = 0; i < palette_size ; i++, pEntry++)
695 if( i < COLOR_gapStart || i > COLOR_gapEnd )
696 if ((GetRValue(color) == pEntry->peRed) &&
697 (GetGValue(color) == pEntry->peGreen) &&
698 (GetBValue(color) == pEntry->peBlue))
700 LeaveCriticalSection( &palette_cs );
701 return TRUE;
704 LeaveCriticalSection( &palette_cs );
705 return FALSE;
709 /***********************************************************************
710 * X11DRV_PALETTE_ToLogical
712 * Return RGB color for given X pixel.
714 COLORREF X11DRV_PALETTE_ToLogical(X11DRV_PDEVICE *physDev, int pixel)
716 XColor color;
718 /* check for hicolor visuals first */
720 if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
722 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
724 if(physDev->color_shifts)
725 shifts = physDev->color_shifts;
727 color.red = (pixel >> shifts->logicalRed.shift) & shifts->logicalRed.max;
728 if (shifts->logicalRed.scale<8)
729 color.red= color.red << (8-shifts->logicalRed.scale) |
730 color.red >> (2*shifts->logicalRed.scale-8);
731 color.green = (pixel >> shifts->logicalGreen.shift) & shifts->logicalGreen.max;
732 if (shifts->logicalGreen.scale<8)
733 color.green=color.green << (8-shifts->logicalGreen.scale) |
734 color.green >> (2*shifts->logicalGreen.scale-8);
735 color.blue = (pixel >> shifts->logicalBlue.shift) & shifts->logicalBlue.max;
736 if (shifts->logicalBlue.scale<8)
737 color.blue= color.blue << (8-shifts->logicalBlue.scale) |
738 color.blue >> (2*shifts->logicalBlue.scale-8);
739 return RGB(color.red,color.green,color.blue);
742 /* check if we can bypass X */
744 if ((default_visual.depth <= 8) && (pixel < 256) &&
745 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) ) {
746 COLORREF ret;
747 EnterCriticalSection( &palette_cs );
748 ret = *(COLORREF *)(COLOR_sysPal + (X11DRV_PALETTE_XPixelToPalette ? X11DRV_PALETTE_XPixelToPalette[pixel]: pixel)) & 0x00ffffff;
749 LeaveCriticalSection( &palette_cs );
750 return ret;
753 color.pixel = pixel;
754 XQueryColor(gdi_display, default_colormap, &color);
755 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
759 /***********************************************************************
760 * X11DRV_SysPaletteLookupPixel
762 static int X11DRV_SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
764 int i, best = 0, diff = 0x7fffffff;
765 int r,g,b;
767 for( i = 0; i < palette_size && diff ; i++ )
769 if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) ||
770 (skipReserved && COLOR_sysPal[i].peFlags & PC_SYS_RESERVED) )
771 continue;
773 r = COLOR_sysPal[i].peRed - GetRValue(col);
774 g = COLOR_sysPal[i].peGreen - GetGValue(col);
775 b = COLOR_sysPal[i].peBlue - GetBValue(col);
777 r = r*r + g*g + b*b;
779 if( r < diff ) { best = i; diff = r; }
781 return best;
785 /***********************************************************************
786 * X11DRV_PALETTE_GetColor
788 * Resolve PALETTEINDEX/PALETTERGB/DIBINDEX COLORREFs to an RGB COLORREF.
790 COLORREF X11DRV_PALETTE_GetColor( X11DRV_PDEVICE *physDev, COLORREF color )
792 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
793 PALETTEENTRY entry;
795 if (color & (1 << 24)) /* PALETTEINDEX */
797 unsigned int idx = LOWORD(color);
798 if (!GetPaletteEntries( hPal, idx, 1, &entry )) return 0;
799 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
802 if (color >> 24 == 2) /* PALETTERGB */
804 unsigned int idx = GetNearestPaletteIndex( hPal, color & 0xffffff );
805 if (!GetPaletteEntries( hPal, idx, 1, &entry )) return 0;
806 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
809 if (color >> 16 == 0x10ff) /* DIBINDEX */
810 return 0;
812 return color & 0xffffff;
815 /***********************************************************************
816 * X11DRV_PALETTE_ToPhysical
818 * Return the physical color closest to 'color'.
820 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
822 WORD index = 0;
823 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
824 int *mapping = palette_get_mapping( hPal );
825 PALETTEENTRY entry;
826 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
828 if(physDev->color_shifts)
829 shifts = physDev->color_shifts;
831 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
833 /* there is no colormap limitation; we are going to have to compute
834 * the pixel value from the visual information stored earlier
836 unsigned long red, green, blue;
838 if (color & (1 << 24)) /* PALETTEINDEX */
840 unsigned int idx = LOWORD( color );
842 if (!GetPaletteEntries( hPal, idx, 1, &entry ))
844 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color, idx);
845 return 0;
847 if (mapping) return mapping[idx];
848 red = entry.peRed;
849 green = entry.peGreen;
850 blue = entry.peBlue;
852 else if (color >> 16 == 0x10ff) /* DIBINDEX */
854 return 0;
856 else /* RGB */
858 if (physDev->depth == 1)
859 return (((color >> 16) & 0xff) +
860 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
861 red = GetRValue( color );
862 green = GetGValue( color );
863 blue = GetBValue( color );
866 if (X11DRV_PALETTE_Graymax)
868 /* grayscale only; return scaled value */
869 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
871 else
873 /* scale each individually and construct the TrueColor pixel value */
874 if (shifts->physicalRed.scale < 8)
875 red = red >> (8-shifts->physicalRed.scale);
876 else if (shifts->physicalRed.scale > 8)
877 red = red << (shifts->physicalRed.scale-8) |
878 red >> (16-shifts->physicalRed.scale);
879 if (shifts->physicalGreen.scale < 8)
880 green = green >> (8-shifts->physicalGreen.scale);
881 else if (shifts->physicalGreen.scale > 8)
882 green = green << (shifts->physicalGreen.scale-8) |
883 green >> (16-shifts->physicalGreen.scale);
884 if (shifts->physicalBlue.scale < 8)
885 blue = blue >> (8-shifts->physicalBlue.scale);
886 else if (shifts->physicalBlue.scale > 8)
887 blue = blue << (shifts->physicalBlue.scale-8) |
888 blue >> (16-shifts->physicalBlue.scale);
890 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
893 else
895 if (!mapping)
896 WARN("Palette %p is not realized\n", hPal);
898 if (color & (1 << 24)) /* PALETTEINDEX */
900 index = LOWORD( color );
901 if (!GetPaletteEntries( hPal, index, 1, &entry ))
902 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color, index);
903 else if (mapping) index = mapping[index];
905 else if (color >> 24 == 2) /* PALETTERGB */
907 index = GetNearestPaletteIndex( hPal, color );
908 if (mapping) index = mapping[index];
910 else if (color >> 16 == 0x10ff) /* DIBINDEX */
912 return 0;
914 else /* RGB */
916 if (physDev->depth == 1)
917 return (((color >> 16) & 0xff) +
918 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
920 EnterCriticalSection( &palette_cs );
921 index = X11DRV_SysPaletteLookupPixel( color & 0xffffff, FALSE);
922 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
923 LeaveCriticalSection( &palette_cs );
926 return index;
929 /***********************************************************************
930 * X11DRV_PALETTE_LookupPixel
932 static int X11DRV_PALETTE_LookupPixel(ColorShifts *shifts, COLORREF color )
934 unsigned char spec_type = color >> 24;
936 /* Only accept RGB which has spec_type = 0 */
937 if(spec_type)
938 return 0;
940 color &= 0xffffff;
942 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
944 unsigned long red, green, blue;
945 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
947 if (X11DRV_PALETTE_Graymax)
949 /* grayscale only; return scaled value */
950 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
952 else
954 /* No shifts are set in case of 1-bit */
955 if(!shifts) shifts = &X11DRV_PALETTE_default_shifts;
957 /* scale each individually and construct the TrueColor pixel value */
958 if (shifts->physicalRed.scale < 8)
959 red = red >> (8-shifts->physicalRed.scale);
960 else if (shifts->physicalRed.scale > 8)
961 red = red << (shifts->physicalRed.scale-8) |
962 red >> (16-shifts->physicalRed.scale);
963 if (shifts->physicalGreen.scale < 8)
964 green = green >> (8-shifts->physicalGreen.scale);
965 else if (shifts->physicalGreen.scale > 8)
966 green = green << (shifts->physicalGreen.scale-8) |
967 green >> (16-shifts->physicalGreen.scale);
968 if (shifts->physicalBlue.scale < 8)
969 blue = blue >> (8-shifts->physicalBlue.scale);
970 else if (shifts->physicalBlue.scale > 8)
971 blue = blue << (shifts->physicalBlue.scale-8) |
972 blue >> (16-shifts->physicalBlue.scale);
974 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
977 else
979 WORD index;
980 HPALETTE hPal = GetStockObject(DEFAULT_PALETTE);
981 int *mapping = palette_get_mapping( hPal );
983 if (!mapping)
984 WARN("Palette %p is not realized\n", hPal);
986 EnterCriticalSection( &palette_cs );
987 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
988 if (X11DRV_PALETTE_PaletteToXPixel)
989 index = X11DRV_PALETTE_PaletteToXPixel[index];
990 LeaveCriticalSection( &palette_cs );
991 return index;
996 /***********************************************************************
997 * X11DRV_PALETTE_LookupSystemXPixel
999 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
1001 int i, best = 0, diff = 0x7fffffff;
1002 int size = palette_size;
1003 int r,g,b;
1005 for( i = 0; i < size && diff ; i++ )
1007 if( i == NB_RESERVED_COLORS/2 )
1009 int newi = size - NB_RESERVED_COLORS/2;
1010 if (newi>i) i=newi;
1013 r = COLOR_sysPal[i].peRed - GetRValue(col);
1014 g = COLOR_sysPal[i].peGreen - GetGValue(col);
1015 b = COLOR_sysPal[i].peBlue - GetBValue(col);
1017 r = r*r + g*g + b*b;
1019 if( r < diff ) { best = i; diff = r; }
1022 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
1025 /***********************************************************************
1026 * X11DRV_PALETTE_FormatSystemPalette
1028 static void X11DRV_PALETTE_FormatSystemPalette(void)
1030 /* Build free list so we'd have an easy way to find
1031 * out if there are any available colorcells.
1034 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1036 COLOR_sysPal[j].peFlags = 0;
1037 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1038 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1040 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1041 X11DRV_PALETTE_freeList[j] = i; /* next */
1042 j = i;
1044 X11DRV_PALETTE_freeList[j] = 0;
1047 /***********************************************************************
1048 * X11DRV_PALETTE_CheckSysColor
1050 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1052 int i;
1053 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1054 if( c == (*(const COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1055 return FALSE;
1056 return TRUE;
1060 /***********************************************************************
1061 * X11DRV_LookupSysPaletteExact
1063 static int X11DRV_LookupSysPaletteExact( BYTE r, BYTE g, BYTE b )
1065 int i;
1066 for( i = 0; i < palette_size; i++ )
1068 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1069 if( COLOR_sysPal[i].peRed == r &&
1070 COLOR_sysPal[i].peGreen == g &&
1071 COLOR_sysPal[i].peBlue == b )
1072 return i;
1074 return -1;
1078 /***********************************************************************
1079 * RealizePalette (X11DRV.@)
1081 UINT X11DRV_RealizePalette( PHYSDEV dev, HPALETTE hpal, BOOL primary )
1083 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1084 char flag;
1085 int index;
1086 UINT i, iRemapped = 0;
1087 int *prev_mapping, *mapping;
1088 PALETTEENTRY entries[256];
1089 WORD num_entries;
1091 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1093 if (!GetObjectW( hpal, sizeof(num_entries), &num_entries )) return 0;
1095 /* initialize palette mapping table */
1096 prev_mapping = palette_get_mapping( hpal );
1097 if (prev_mapping)
1098 mapping = HeapReAlloc( GetProcessHeap(), 0, prev_mapping, sizeof(int)*num_entries);
1099 else
1100 mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries);
1102 if(mapping == NULL) {
1103 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1104 return 0;
1106 palette_set_mapping( hpal, mapping );
1108 if (num_entries > 256)
1110 FIXME( "more than 256 entries not supported\n" );
1111 num_entries = 256;
1113 if (!(num_entries = GetPaletteEntries( hpal, 0, num_entries, entries ))) return 0;
1115 /* reset dynamic system palette entries */
1117 EnterCriticalSection( &palette_cs );
1118 if( primary && X11DRV_PALETTE_firstFree != -1)
1119 X11DRV_PALETTE_FormatSystemPalette();
1121 for (i = 0; i < num_entries; i++)
1123 index = -1;
1124 flag = PC_SYS_USED;
1126 /* Even though the docs say that only one flag is to be set,
1127 * they are a bitmask. At least one app sets more than one at
1128 * the same time. */
1129 if ( entries[i].peFlags & PC_EXPLICIT ) {
1130 /* palette entries are indices into system palette */
1131 index = *(WORD*)&entries[i];
1132 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1134 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1135 index = 0;
1137 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1138 } else {
1139 if ( entries[i].peFlags & PC_RESERVED ) {
1140 /* forbid future mappings to this entry */
1141 flag |= PC_SYS_RESERVED;
1144 if (! (entries[i].peFlags & PC_NOCOLLAPSE) ) {
1145 /* try to collapse identical colors */
1146 index = X11DRV_LookupSysPaletteExact( entries[i].peRed, entries[i].peGreen, entries[i].peBlue );
1149 if( index < 0 )
1151 if( X11DRV_PALETTE_firstFree > 0 )
1153 XColor color;
1154 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1155 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1157 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1158 color.red = entries[i].peRed << 8;
1159 color.green = entries[i].peGreen << 8;
1160 color.blue = entries[i].peBlue << 8;
1161 color.flags = DoRed | DoGreen | DoBlue;
1162 XStoreColor(gdi_display, default_colormap, &color);
1164 COLOR_sysPal[index] = entries[i];
1165 COLOR_sysPal[index].peFlags = flag;
1166 X11DRV_PALETTE_freeList[index] = 0;
1168 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1170 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1172 index = X11DRV_PALETTE_LookupPixel( physDev->color_shifts, RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ));
1175 /* we have to map to existing entry in the system palette */
1177 index = X11DRV_SysPaletteLookupPixel( RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ),
1178 TRUE );
1181 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1184 if( !prev_mapping || mapping[i] != index ) iRemapped++;
1185 mapping[i] = index;
1187 TRACE("entry %i (%x) -> pixel %i\n", i, *(COLORREF*)&entries[i], index);
1190 LeaveCriticalSection( &palette_cs );
1191 return iRemapped;
1195 /***********************************************************************
1196 * UnrealizePalette (X11DRV.@)
1198 BOOL X11DRV_UnrealizePalette( HPALETTE hpal )
1200 int *mapping = palette_get_mapping( hpal );
1202 if (mapping)
1204 XDeleteContext( gdi_display, (XID)hpal, palette_context );
1205 HeapFree( GetProcessHeap(), 0, mapping );
1207 return TRUE;
1211 /***********************************************************************
1212 * GetSystemPaletteEntries (X11DRV.@)
1214 UINT X11DRV_GetSystemPaletteEntries( PHYSDEV dev, UINT start, UINT count, LPPALETTEENTRY entries )
1216 UINT i;
1218 if (!entries) return palette_size;
1219 if (start >= palette_size) return 0;
1220 if (start + count >= palette_size) count = palette_size - start;
1222 EnterCriticalSection( &palette_cs );
1223 for (i = 0; i < count; i++)
1225 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1226 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1227 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1228 entries[i].peFlags = 0;
1229 TRACE("\tidx(%02x) -> RGB(%08x)\n", start + i, *(COLORREF*)(entries + i) );
1231 LeaveCriticalSection( &palette_cs );
1232 return count;
1236 /***********************************************************************
1237 * GetNearestColor (X11DRV.@)
1239 COLORREF X11DRV_GetNearestColor( PHYSDEV dev, COLORREF color )
1241 unsigned char spec_type = color >> 24;
1242 COLORREF nearest;
1244 if (!palette_size) return color;
1246 if (spec_type == 1 || spec_type == 2)
1248 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1250 UINT index;
1251 PALETTEENTRY entry;
1252 HPALETTE hpal = GetCurrentObject( dev->hdc, OBJ_PAL );
1254 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1256 if (spec_type == 2) /* PALETTERGB */
1257 index = GetNearestPaletteIndex( hpal, color );
1258 else /* PALETTEINDEX */
1259 index = LOWORD(color);
1261 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1263 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
1264 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1266 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1268 color &= 0x00ffffff;
1269 EnterCriticalSection( &palette_cs );
1270 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1271 LeaveCriticalSection( &palette_cs );
1273 TRACE("(%06x): returning %06x\n", color, nearest );
1274 return nearest;
1278 /***********************************************************************
1279 * RealizeDefaultPalette (X11DRV.@)
1281 UINT X11DRV_RealizeDefaultPalette( PHYSDEV dev )
1283 UINT ret = 0;
1285 if (palette_size && GetObjectType(dev->hdc) != OBJ_MEMDC)
1287 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1288 int i, index, *mapping = palette_get_mapping( GetStockObject(DEFAULT_PALETTE) );
1289 PALETTEENTRY entries[NB_RESERVED_COLORS];
1291 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, entries );
1292 EnterCriticalSection( &palette_cs );
1293 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1295 index = X11DRV_PALETTE_LookupSystemXPixel( RGB(entries[i].peRed,
1296 entries[i].peGreen,
1297 entries[i].peBlue) );
1298 /* mapping is allocated in COLOR_InitPalette() */
1299 if( index != mapping[i] )
1301 mapping[i]=index;
1302 ret++;
1305 LeaveCriticalSection( &palette_cs );
1307 return ret;