2 * X11DRV OEM bitmap objects
4 * Copyright 1994, 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(palette);
34 /* Palette indexed mode:
35 * logical palette -> mapping -> pixel
38 * Windows needs contiguous color space ( from 0 to n ) but
39 * it is possible only with the private colormap. Otherwise we
40 * have to map DC palette indices to real pixel values. With
41 * private colormaps it boils down to the identity mapping. The
42 * other special case is when we have a fixed color visual with
43 * the screendepth > 8 - we abandon palette mappings altogether
44 * because pixel values can be calculated without X server
48 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
50 #define PC_SYS_USED 0x80 /* palentry is used (both system and logical) */
51 #define PC_SYS_RESERVED 0x40 /* system palentry is not to be mapped to */
53 static PALETTEENTRY *COLOR_sysPal; /* current system palette */
55 static int COLOR_gapStart = 256;
56 static int COLOR_gapEnd = -1;
57 static int COLOR_gapFilled = 0;
59 UINT16 X11DRV_PALETTE_PaletteFlags = 0;
61 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
62 ColorShifts X11DRV_PALETTE_default_shifts = { {0,0,0,}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
63 static int X11DRV_PALETTE_Graymax = 0;
65 static int palette_size;
67 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
68 static int X11DRV_PALETTE_firstFree = 0;
69 static unsigned char X11DRV_PALETTE_freeList[256];
71 static XContext palette_context; /* X context to associate a color mapping to a palette */
73 static CRITICAL_SECTION palette_cs;
74 static CRITICAL_SECTION_DEBUG critsect_debug =
77 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
78 0, 0, { (DWORD_PTR)(__FILE__ ": palette_cs") }
80 static CRITICAL_SECTION palette_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
82 /**********************************************************************/
84 /* Map an EGA index (0..15) to a pixel value in the system color space. */
86 int X11DRV_PALETTE_mapEGAPixel[16];
88 /**********************************************************************/
90 #define NB_COLORCUBE_START_INDEX 63
91 #define NB_PALETTE_EMPTY_VALUE -1
93 /* Maps entry in the system palette to X pixel value */
94 int *X11DRV_PALETTE_PaletteToXPixel = NULL;
96 /* Maps pixel to the entry in the system palette */
97 int *X11DRV_PALETTE_XPixelToPalette = NULL;
99 /**********************************************************************/
101 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template );
102 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template );
103 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template );
104 static void X11DRV_PALETTE_FormatSystemPalette(void);
105 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c);
106 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col);
109 /***********************************************************************
110 * palette_get_mapping
112 static int *palette_get_mapping( HPALETTE hpal )
116 if (XFindContext( gdi_display, (XID)hpal, palette_context, (char **)&mapping )) mapping = NULL;
121 /***********************************************************************
122 * palette_set_mapping
124 static void palette_set_mapping( HPALETTE hpal, int *mapping )
126 XSaveContext( gdi_display, (XID)hpal, palette_context, (char *)mapping );
130 /***********************************************************************
133 * Initialize color management.
135 int X11DRV_PALETTE_Init(void)
137 int mask, white, black;
140 PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS];
142 TRACE("initializing palette manager...\n");
144 palette_context = XUniqueContext();
145 white = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
146 black = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
148 for( mask = 1; !((white & mask)^(black & mask)); mask <<= 1 )
150 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
151 palette_size = default_visual.colormap_size;
153 switch(default_visual.class)
156 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
159 if (private_color_map)
161 XSetWindowAttributes win_attr;
163 XFreeColormap( gdi_display, default_colormap );
164 default_colormap = XCreateColormap( gdi_display, root_window,
165 default_visual.visual, AllocAll );
166 if (default_colormap)
168 X11DRV_PALETTE_PaletteFlags |= (X11DRV_PALETTE_PRIVATE | X11DRV_PALETTE_WHITESET);
171 for( white = palette_size - 1; !(white & 1); white >>= 1 )
174 if( root_window != DefaultRootWindow(gdi_display) )
176 win_attr.colormap = default_colormap;
177 XChangeWindowAttributes( gdi_display, root_window, CWColormap, &win_attr );
184 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
185 X11DRV_PALETTE_Graymax = (1 << default_visual.depth)-1;
189 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
191 int *depths,nrofdepths;
192 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
195 depths = XListDepths(gdi_display,DefaultScreen(gdi_display),&nrofdepths);
196 if ((nrofdepths==2) && ((depths[0]==4) || depths[1]==4)) {
198 for( white = palette_size - 1; !(white & 1); white >>= 1 )
200 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
204 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
205 X11DRV_PALETTE_ComputeColorShifts(&X11DRV_PALETTE_default_shifts, default_visual.red_mask, default_visual.green_mask, default_visual.blue_mask);
212 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, sys_pal_template );
214 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
220 if ((mapping = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * NB_RESERVED_COLORS )))
221 palette_set_mapping( GetStockObject(DEFAULT_PALETTE), mapping );
223 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
224 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template );
226 X11DRV_PALETTE_BuildSharedMap( sys_pal_template );
228 /* Build free list */
230 if( X11DRV_PALETTE_firstFree != -1 )
231 X11DRV_PALETTE_FormatSystemPalette();
233 X11DRV_PALETTE_FillDefaultColors( sys_pal_template );
234 palette_size = default_visual.colormap_size;
240 /***********************************************************************
241 * X11DRV_PALETTE_ComputeChannelShift
243 * Calculate conversion parameters for a given color mask
245 static void X11DRV_PALETTE_ComputeChannelShift(unsigned long maskbits, ChannelShift *physical, ChannelShift *to_logical)
260 for(i=0;!(maskbits&1);i++)
264 physical->max = maskbits;
266 for(i=0;maskbits!=0;i++)
270 if (physical->scale>8)
272 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
273 * So we adjust the shifts to also normalize the color fields to
274 * the Win32 standard of 8 bits per color.
276 to_logical->shift=physical->shift+(physical->scale-8);
278 to_logical->max=0xff;
280 to_logical->shift=physical->shift;
281 to_logical->scale=physical->scale;
282 to_logical->max=physical->max;
286 /***********************************************************************
287 * X11DRV_PALETTE_ComputeColorShifts
289 * Calculate conversion parameters for a given color
291 void X11DRV_PALETTE_ComputeColorShifts(ColorShifts *shifts, unsigned long redMask, unsigned long greenMask, unsigned long blueMask)
293 X11DRV_PALETTE_ComputeChannelShift(redMask, &shifts->physicalRed, &shifts->logicalRed);
294 X11DRV_PALETTE_ComputeChannelShift(greenMask, &shifts->physicalGreen, &shifts->logicalGreen);
295 X11DRV_PALETTE_ComputeChannelShift(blueMask, &shifts->physicalBlue, &shifts->logicalBlue);
298 /***********************************************************************
299 * X11DRV_PALETTE_BuildPrivateMap
301 * Allocate colorcells and initialize mapping tables.
303 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template )
305 /* Private colormap - identity mapping */
310 if((COLOR_sysPal = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*palette_size)) == NULL) {
311 WARN("Unable to allocate the system palette\n");
315 TRACE("Building private map - %i palette entries\n", palette_size);
317 /* Allocate system palette colors */
319 for( i=0; i < palette_size; i++ )
321 if( i < NB_RESERVED_COLORS/2 )
323 color.red = sys_pal_template[i].peRed * 65535 / 255;
324 color.green = sys_pal_template[i].peGreen * 65535 / 255;
325 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
326 COLOR_sysPal[i] = sys_pal_template[i];
327 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
329 else if( i >= palette_size - NB_RESERVED_COLORS/2 )
331 int j = NB_RESERVED_COLORS + i - palette_size;
332 color.red = sys_pal_template[j].peRed * 65535 / 255;
333 color.green = sys_pal_template[j].peGreen * 65535 / 255;
334 color.blue = sys_pal_template[j].peBlue * 65535 / 255;
335 COLOR_sysPal[i] = sys_pal_template[j];
336 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
339 color.flags = DoRed | DoGreen | DoBlue;
341 XStoreColor(gdi_display, default_colormap, &color);
343 /* Set EGA mapping if color is from the first or last eight */
346 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
347 else if (i >= palette_size - 8 )
348 X11DRV_PALETTE_mapEGAPixel[i - (palette_size - 16)] = color.pixel;
351 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
353 COLOR_gapStart = 256; COLOR_gapEnd = -1;
355 X11DRV_PALETTE_firstFree = (palette_size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
360 /***********************************************************************
361 * X11DRV_PALETTE_BuildSharedMap
363 * Allocate colorcells and initialize mapping tables.
365 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template )
368 unsigned long sysPixel[NB_RESERVED_COLORS];
369 unsigned long* pixDynMapping = NULL;
370 unsigned long plane_masks[1];
372 int diff, r, g, b, bp = 0, wp = 1;
374 unsigned int max = 256;
376 XColor defaultColors[256];
378 /* Copy the first bunch of colors out of the default colormap to prevent
379 * colormap flashing as much as possible. We're likely to get the most
380 * important Window Manager colors, etc in the first 128 colors */
381 defaultCM = DefaultColormap( gdi_display, DefaultScreen(gdi_display) );
383 if (copy_default_colors > 256) copy_default_colors = 256;
384 for (i = 0; i < copy_default_colors; i++)
385 defaultColors[i].pixel = (long) i;
386 XQueryColors(gdi_display, defaultCM, &defaultColors[0], copy_default_colors);
387 for (i = 0; i < copy_default_colors; i++)
388 XAllocColor( gdi_display, default_colormap, &defaultColors[i] );
390 if (alloc_system_colors > 256) alloc_system_colors = 256;
391 else if (alloc_system_colors < 20) alloc_system_colors = 20;
392 TRACE("%d colors configured.\n", alloc_system_colors);
394 TRACE("Building shared map - %i palette entries\n", palette_size);
396 /* Be nice and allocate system colors as read-only */
398 for( i = 0; i < NB_RESERVED_COLORS; i++ )
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;
403 color.flags = DoRed | DoGreen | DoBlue;
405 if (!XAllocColor( gdi_display, default_colormap, &color ))
411 WARN("Not enough colors for the full system palette.\n");
413 bp = BlackPixel(gdi_display, DefaultScreen(gdi_display));
414 wp = WhitePixel(gdi_display, DefaultScreen(gdi_display));
416 max = 0xffffffff >> (32 - default_visual.depth);
424 /* reinit color (XAllocColor() may change it)
425 * and map to the best shared colorcell */
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;
431 best.pixel = best.red = best.green = best.blue = 0;
432 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
434 XQueryColor(gdi_display, default_colormap, &c);
435 r = (c.red - color.red)>>8;
436 g = (c.green - color.green)>>8;
437 b = (c.blue - color.blue)>>8;
439 if( r < diff ) { best = c; diff = r; }
442 if( XAllocColor(gdi_display, default_colormap, &best) )
443 color.pixel = best.pixel;
444 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
447 sysPixel[i] = color.pixel;
449 TRACE("syscolor(%x) -> pixel %i\n", *(const COLORREF*)(sys_pal_template+i),
452 /* Set EGA mapping if color in the first or last eight */
455 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
456 else if (i >= NB_RESERVED_COLORS - 8 )
457 X11DRV_PALETTE_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
460 /* now allocate changeable set */
462 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
464 int c_min = 0, c_max = palette_size, c_val;
466 TRACE("Dynamic colormap...\n");
468 /* let's become the first client that actually follows
469 * X guidelines and does binary search...
472 if((pixDynMapping = HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size)) == NULL) {
473 WARN("Out of memory while building system palette.\n");
477 /* comment this out if you want to debug palette init */
478 XGrabServer(gdi_display);
480 while( c_max - c_min > 0 )
482 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
484 if( !XAllocColorCells(gdi_display, default_colormap, False,
485 plane_masks, 0, pixDynMapping, c_val) )
489 XFreeColors(gdi_display, default_colormap, pixDynMapping, c_val, 0);
494 if( c_min > alloc_system_colors - NB_RESERVED_COLORS)
495 c_min = alloc_system_colors - NB_RESERVED_COLORS;
497 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
500 if( !XAllocColorCells(gdi_display, default_colormap, False,
501 plane_masks, 0, pixDynMapping, c_min) )
503 WARN("Inexplicable failure during colorcell allocation.\n");
507 palette_size = c_min + NB_RESERVED_COLORS;
509 XUngrabServer(gdi_display);
511 TRACE("adjusted size %i colorcells\n", palette_size);
513 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
515 /* virtual colorspace - ToPhysical takes care of
516 * color translations but we have to allocate full palette
517 * to maintain compatibility
520 TRACE("Virtual colorspace - screendepth %i\n", default_visual.depth);
522 else palette_size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
523 * of colors and map to them */
525 TRACE("Shared system palette uses %i colors.\n", palette_size);
527 /* set gap to account for pixel shortage. It has to be right in the center
528 * of the system palette because otherwise raster ops get screwed. */
530 if( palette_size >= 256 )
531 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
533 { COLOR_gapStart = palette_size/2; COLOR_gapEnd = 255 - palette_size/2; }
535 X11DRV_PALETTE_firstFree = ( palette_size > NB_RESERVED_COLORS &&
536 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
537 ? NB_RESERVED_COLORS/2 : -1;
539 COLOR_sysPal = HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
540 if(COLOR_sysPal == NULL) {
541 ERR("Unable to allocate the system palette!\n");
542 HeapFree(GetProcessHeap(), 0, pixDynMapping);
546 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
548 if (default_visual.depth <= 8)
550 X11DRV_PALETTE_XPixelToPalette = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
551 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
552 ERR("Out of memory: XPixelToPalette!\n");
553 HeapFree(GetProcessHeap(), 0, pixDynMapping);
556 for( i = 0; i < 256; i++ )
557 X11DRV_PALETTE_XPixelToPalette[i] = NB_PALETTE_EMPTY_VALUE;
560 /* for hicolor visuals PaletteToPixel mapping is used to skip
561 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
564 X11DRV_PALETTE_PaletteToXPixel = HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
565 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
566 ERR("Out of memory: PaletteToXPixel!\n");
567 HeapFree(GetProcessHeap(), 0, pixDynMapping);
571 for( i = j = 0; i < 256; i++ )
573 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
575 X11DRV_PALETTE_PaletteToXPixel[i] = NB_PALETTE_EMPTY_VALUE;
576 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
580 if( i < NB_RESERVED_COLORS/2 )
582 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
583 COLOR_sysPal[i] = sys_pal_template[i];
584 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
586 else if( i >= 256 - NB_RESERVED_COLORS/2 )
588 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
589 COLOR_sysPal[i] = sys_pal_template[(i + NB_RESERVED_COLORS) - 256];
590 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
592 else if( pixDynMapping )
593 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
595 X11DRV_PALETTE_PaletteToXPixel[i] = i;
597 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
599 if( X11DRV_PALETTE_XPixelToPalette )
600 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
603 HeapFree(GetProcessHeap(), 0, pixDynMapping);
608 /***********************************************************************
609 * Colormap Initialization
611 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template )
613 /* initialize unused entries to what Windows uses as a color
614 * cube - based on Greg Kreider's code.
618 int red, no_r, inc_r;
619 int green, no_g, inc_g;
620 int blue, no_b, inc_b;
622 if (palette_size <= NB_RESERVED_COLORS)
624 while (i*i*i < (palette_size - NB_RESERVED_COLORS)) i++;
625 no_r = no_g = no_b = --i;
626 if ((no_r * (no_g+1) * no_b) < (palette_size - NB_RESERVED_COLORS)) no_g++;
627 if ((no_r * no_g * (no_b+1)) < (palette_size - NB_RESERVED_COLORS)) no_b++;
628 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
629 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
630 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
632 idx = X11DRV_PALETTE_firstFree;
635 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
636 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
637 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
641 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
643 COLOR_sysPal[idx].peRed = red;
644 COLOR_sysPal[idx].peGreen = green;
645 COLOR_sysPal[idx].peBlue = blue;
649 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
651 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
652 if (shifts->physicalRed.max != 255) no_r = (red * shifts->physicalRed.max) / 255;
653 if (shifts->physicalGreen.max != 255) no_g = (green * shifts->physicalGreen.max) / 255;
654 if (shifts->physicalBlue.max != 255) no_b = (blue * shifts->physicalBlue.max) / 255;
656 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << shifts->physicalRed.shift) | (no_g << shifts->physicalGreen.shift) | (no_b << shifts->physicalBlue.shift);
658 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
661 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
662 color.red = COLOR_sysPal[idx].peRed << 8;
663 color.green = COLOR_sysPal[idx].peGreen << 8;
664 color.blue = COLOR_sysPal[idx].peBlue << 8;
665 color.flags = DoRed | DoGreen | DoBlue;
666 XStoreColor(gdi_display, default_colormap, &color);
668 idx = X11DRV_PALETTE_freeList[idx];
671 /* try to fill some entries in the "gap" with
672 * what's already in the colormap - they will be
673 * mappable to but not changeable. */
675 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
680 max = alloc_system_colors - (256 - (COLOR_gapEnd - COLOR_gapStart));
681 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
682 if( X11DRV_PALETTE_XPixelToPalette[i] == NB_PALETTE_EMPTY_VALUE )
686 XQueryColor(gdi_display, default_colormap, &xc);
687 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
689 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template, RGB(r, g, b)) &&
690 XAllocColor(gdi_display, default_colormap, &xc) )
692 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
693 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
694 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
695 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
696 if( --max <= 0 ) break;
699 COLOR_gapFilled = idx - COLOR_gapStart;
704 /***********************************************************************
705 * X11DRV_IsSolidColor
707 * Check whether 'color' can be represented with a solid color.
709 BOOL X11DRV_IsSolidColor( COLORREF color )
712 const PALETTEENTRY *pEntry = COLOR_sysPal;
714 if (color & 0xff000000) return TRUE; /* indexed color */
716 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
718 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return TRUE; /* no palette */
720 EnterCriticalSection( &palette_cs );
721 for (i = 0; i < palette_size ; i++, pEntry++)
723 if( i < COLOR_gapStart || i > COLOR_gapEnd )
724 if ((GetRValue(color) == pEntry->peRed) &&
725 (GetGValue(color) == pEntry->peGreen) &&
726 (GetBValue(color) == pEntry->peBlue))
728 LeaveCriticalSection( &palette_cs );
732 LeaveCriticalSection( &palette_cs );
737 /***********************************************************************
738 * X11DRV_PALETTE_ToLogical
740 * Return RGB color for given X pixel.
742 COLORREF X11DRV_PALETTE_ToLogical(X11DRV_PDEVICE *physDev, int pixel)
747 /* truecolor visual */
749 if (screen_depth >= 24) return pixel;
752 /* check for hicolor visuals first */
754 if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
756 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
758 if(physDev->color_shifts)
759 shifts = physDev->color_shifts;
761 color.red = (pixel >> shifts->logicalRed.shift) & shifts->logicalRed.max;
762 if (shifts->logicalRed.scale<8)
763 color.red= color.red << (8-shifts->logicalRed.scale) |
764 color.red >> (2*shifts->logicalRed.scale-8);
765 color.green = (pixel >> shifts->logicalGreen.shift) & shifts->logicalGreen.max;
766 if (shifts->logicalGreen.scale<8)
767 color.green=color.green << (8-shifts->logicalGreen.scale) |
768 color.green >> (2*shifts->logicalGreen.scale-8);
769 color.blue = (pixel >> shifts->logicalBlue.shift) & shifts->logicalBlue.max;
770 if (shifts->logicalBlue.scale<8)
771 color.blue= color.blue << (8-shifts->logicalBlue.scale) |
772 color.blue >> (2*shifts->logicalBlue.scale-8);
773 return RGB(color.red,color.green,color.blue);
776 /* check if we can bypass X */
778 if ((default_visual.depth <= 8) && (pixel < 256) &&
779 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) ) {
781 EnterCriticalSection( &palette_cs );
782 ret = *(COLORREF *)(COLOR_sysPal + (X11DRV_PALETTE_XPixelToPalette ? X11DRV_PALETTE_XPixelToPalette[pixel]: pixel)) & 0x00ffffff;
783 LeaveCriticalSection( &palette_cs );
788 XQueryColor(gdi_display, default_colormap, &color);
789 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
793 /***********************************************************************
794 * X11DRV_SysPaletteLookupPixel
796 static int X11DRV_SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
798 int i, best = 0, diff = 0x7fffffff;
801 for( i = 0; i < palette_size && diff ; i++ )
803 if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) ||
804 (skipReserved && COLOR_sysPal[i].peFlags & PC_SYS_RESERVED) )
807 r = COLOR_sysPal[i].peRed - GetRValue(col);
808 g = COLOR_sysPal[i].peGreen - GetGValue(col);
809 b = COLOR_sysPal[i].peBlue - GetBValue(col);
813 if( r < diff ) { best = i; diff = r; }
819 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
821 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
822 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
825 /***********************************************************************
826 * X11DRV_PALETTE_GetColor
828 * Resolve PALETTEINDEX/PALETTERGB/DIBINDEX COLORREFs to an RGB COLORREF.
830 COLORREF X11DRV_PALETTE_GetColor( X11DRV_PDEVICE *physDev, COLORREF color )
832 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
835 if (color & (1 << 24)) /* PALETTEINDEX */
837 unsigned int idx = LOWORD(color);
838 if (!GetPaletteEntries( hPal, idx, 1, &entry )) return 0;
839 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
842 if (color >> 24 == 2) /* PALETTERGB */
844 unsigned int idx = GetNearestPaletteIndex( hPal, color & 0xffffff );
845 if (!GetPaletteEntries( hPal, idx, 1, &entry )) return 0;
846 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
849 if (color >> 16 == 0x10ff) /* DIBINDEX */
852 return color & 0xffffff;
855 /***********************************************************************
856 * X11DRV_PALETTE_ToPhysical
858 * Return the physical color closest to 'color'.
860 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
863 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
864 int *mapping = palette_get_mapping( hPal );
866 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
868 if(physDev->color_shifts)
869 shifts = physDev->color_shifts;
871 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
873 /* there is no colormap limitation; we are going to have to compute
874 * the pixel value from the visual information stored earlier
876 unsigned long red, green, blue;
878 if (color & (1 << 24)) /* PALETTEINDEX */
880 unsigned int idx = LOWORD( color );
882 if (!GetPaletteEntries( hPal, idx, 1, &entry ))
884 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color, idx);
887 if (mapping) return mapping[idx];
889 green = entry.peGreen;
892 else if (color >> 16 == 0x10ff) /* DIBINDEX */
898 if (physDev->depth == 1)
899 return (((color >> 16) & 0xff) +
900 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
901 red = GetRValue( color );
902 green = GetGValue( color );
903 blue = GetBValue( color );
906 if (X11DRV_PALETTE_Graymax)
908 /* grayscale only; return scaled value */
909 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
913 /* scale each individually and construct the TrueColor pixel value */
914 if (shifts->physicalRed.scale < 8)
915 red = red >> (8-shifts->physicalRed.scale);
916 else if (shifts->physicalRed.scale > 8)
917 red = red << (shifts->physicalRed.scale-8) |
918 red >> (16-shifts->physicalRed.scale);
919 if (shifts->physicalGreen.scale < 8)
920 green = green >> (8-shifts->physicalGreen.scale);
921 else if (shifts->physicalGreen.scale > 8)
922 green = green << (shifts->physicalGreen.scale-8) |
923 green >> (16-shifts->physicalGreen.scale);
924 if (shifts->physicalBlue.scale < 8)
925 blue = blue >> (8-shifts->physicalBlue.scale);
926 else if (shifts->physicalBlue.scale > 8)
927 blue = blue << (shifts->physicalBlue.scale-8) |
928 blue >> (16-shifts->physicalBlue.scale);
930 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
936 WARN("Palette %p is not realized\n", hPal);
938 if (color & (1 << 24)) /* PALETTEINDEX */
940 index = LOWORD( color );
941 if (!GetPaletteEntries( hPal, index, 1, &entry ))
942 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color, index);
943 else if (mapping) index = mapping[index];
945 else if (color >> 24 == 2) /* PALETTERGB */
947 index = GetNearestPaletteIndex( hPal, color );
948 if (mapping) index = mapping[index];
950 else if (color >> 16 == 0x10ff) /* DIBINDEX */
956 if (physDev->depth == 1)
957 return (((color >> 16) & 0xff) +
958 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
960 EnterCriticalSection( &palette_cs );
961 index = X11DRV_SysPaletteLookupPixel( color & 0xffffff, FALSE);
962 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
963 LeaveCriticalSection( &palette_cs );
969 /***********************************************************************
970 * X11DRV_PALETTE_LookupPixel
972 int X11DRV_PALETTE_LookupPixel(ColorShifts *shifts, COLORREF color )
974 unsigned char spec_type = color >> 24;
976 /* Only accept RGB which has spec_type = 0 */
982 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
984 unsigned long red, green, blue;
985 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
987 if (X11DRV_PALETTE_Graymax)
989 /* grayscale only; return scaled value */
990 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
994 /* No shifts are set in case of 1-bit */
995 if(!shifts) shifts = &X11DRV_PALETTE_default_shifts;
997 /* scale each individually and construct the TrueColor pixel value */
998 if (shifts->physicalRed.scale < 8)
999 red = red >> (8-shifts->physicalRed.scale);
1000 else if (shifts->physicalRed.scale > 8)
1001 red = red << (shifts->physicalRed.scale-8) |
1002 red >> (16-shifts->physicalRed.scale);
1003 if (shifts->physicalGreen.scale < 8)
1004 green = green >> (8-shifts->physicalGreen.scale);
1005 else if (shifts->physicalGreen.scale > 8)
1006 green = green << (shifts->physicalGreen.scale-8) |
1007 green >> (16-shifts->physicalGreen.scale);
1008 if (shifts->physicalBlue.scale < 8)
1009 blue = blue >> (8-shifts->physicalBlue.scale);
1010 else if (shifts->physicalBlue.scale > 8)
1011 blue = blue << (shifts->physicalBlue.scale-8) |
1012 blue >> (16-shifts->physicalBlue.scale);
1014 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
1020 HPALETTE hPal = GetStockObject(DEFAULT_PALETTE);
1021 int *mapping = palette_get_mapping( hPal );
1024 WARN("Palette %p is not realized\n", hPal);
1026 EnterCriticalSection( &palette_cs );
1027 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
1028 if (X11DRV_PALETTE_PaletteToXPixel)
1029 index = X11DRV_PALETTE_PaletteToXPixel[index];
1030 LeaveCriticalSection( &palette_cs );
1036 /***********************************************************************
1037 * X11DRV_PALETTE_LookupSystemXPixel
1039 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
1041 int i, best = 0, diff = 0x7fffffff;
1042 int size = palette_size;
1045 for( i = 0; i < size && diff ; i++ )
1047 if( i == NB_RESERVED_COLORS/2 )
1049 int newi = size - NB_RESERVED_COLORS/2;
1053 r = COLOR_sysPal[i].peRed - GetRValue(col);
1054 g = COLOR_sysPal[i].peGreen - GetGValue(col);
1055 b = COLOR_sysPal[i].peBlue - GetBValue(col);
1057 r = r*r + g*g + b*b;
1059 if( r < diff ) { best = i; diff = r; }
1062 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
1065 /***********************************************************************
1066 * X11DRV_PALETTE_FormatSystemPalette
1068 static void X11DRV_PALETTE_FormatSystemPalette(void)
1070 /* Build free list so we'd have an easy way to find
1071 * out if there are any available colorcells.
1074 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1076 COLOR_sysPal[j].peFlags = 0;
1077 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1078 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1080 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1081 X11DRV_PALETTE_freeList[j] = i; /* next */
1084 X11DRV_PALETTE_freeList[j] = 0;
1087 /***********************************************************************
1088 * X11DRV_PALETTE_CheckSysColor
1090 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1093 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1094 if( c == (*(const COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1100 /***********************************************************************
1101 * X11DRV_LookupSysPaletteExact
1103 static int X11DRV_LookupSysPaletteExact( BYTE r, BYTE g, BYTE b )
1106 for( i = 0; i < palette_size; i++ )
1108 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1109 if( COLOR_sysPal[i].peRed == r &&
1110 COLOR_sysPal[i].peGreen == g &&
1111 COLOR_sysPal[i].peBlue == b )
1118 /***********************************************************************
1119 * RealizePalette (X11DRV.@)
1121 UINT X11DRV_RealizePalette( PHYSDEV dev, HPALETTE hpal, BOOL primary )
1123 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1126 UINT i, iRemapped = 0;
1127 int *prev_mapping, *mapping;
1128 PALETTEENTRY entries[256];
1131 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1133 if (!GetObjectW( hpal, sizeof(num_entries), &num_entries )) return 0;
1135 /* initialize palette mapping table */
1136 prev_mapping = palette_get_mapping( hpal );
1138 mapping = HeapReAlloc( GetProcessHeap(), 0, prev_mapping, sizeof(int)*num_entries);
1140 mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries);
1142 if(mapping == NULL) {
1143 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1146 palette_set_mapping( hpal, mapping );
1148 if (num_entries > 256)
1150 FIXME( "more than 256 entries not supported\n" );
1153 if (!(num_entries = GetPaletteEntries( hpal, 0, num_entries, entries ))) return 0;
1155 /* reset dynamic system palette entries */
1157 EnterCriticalSection( &palette_cs );
1158 if( primary && X11DRV_PALETTE_firstFree != -1)
1159 X11DRV_PALETTE_FormatSystemPalette();
1161 for (i = 0; i < num_entries; i++)
1166 /* Even though the docs say that only one flag is to be set,
1167 * they are a bitmask. At least one app sets more than one at
1169 if ( entries[i].peFlags & PC_EXPLICIT ) {
1170 /* palette entries are indices into system palette */
1171 index = *(WORD*)&entries[i];
1172 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1174 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1177 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1179 if ( entries[i].peFlags & PC_RESERVED ) {
1180 /* forbid future mappings to this entry */
1181 flag |= PC_SYS_RESERVED;
1184 if (! (entries[i].peFlags & PC_NOCOLLAPSE) ) {
1185 /* try to collapse identical colors */
1186 index = X11DRV_LookupSysPaletteExact( entries[i].peRed, entries[i].peGreen, entries[i].peBlue );
1191 if( X11DRV_PALETTE_firstFree > 0 )
1194 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1195 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1197 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1198 color.red = entries[i].peRed << 8;
1199 color.green = entries[i].peGreen << 8;
1200 color.blue = entries[i].peBlue << 8;
1201 color.flags = DoRed | DoGreen | DoBlue;
1202 XStoreColor(gdi_display, default_colormap, &color);
1204 COLOR_sysPal[index] = entries[i];
1205 COLOR_sysPal[index].peFlags = flag;
1206 X11DRV_PALETTE_freeList[index] = 0;
1208 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1210 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1212 index = X11DRV_PALETTE_LookupPixel( physDev->color_shifts, RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ));
1215 /* we have to map to existing entry in the system palette */
1217 index = X11DRV_SysPaletteLookupPixel( RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ),
1221 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1224 if( !prev_mapping || mapping[i] != index ) iRemapped++;
1227 TRACE("entry %i (%x) -> pixel %i\n", i, *(COLORREF*)&entries[i], index);
1230 LeaveCriticalSection( &palette_cs );
1235 /***********************************************************************
1236 * UnrealizePalette (X11DRV.@)
1238 BOOL X11DRV_UnrealizePalette( HPALETTE hpal )
1240 int *mapping = palette_get_mapping( hpal );
1244 XDeleteContext( gdi_display, (XID)hpal, palette_context );
1245 HeapFree( GetProcessHeap(), 0, mapping );
1251 /***********************************************************************
1252 * GetSystemPaletteEntries (X11DRV.@)
1254 UINT X11DRV_GetSystemPaletteEntries( PHYSDEV dev, UINT start, UINT count, LPPALETTEENTRY entries )
1258 if (!entries) return palette_size;
1259 if (start >= palette_size) return 0;
1260 if (start + count >= palette_size) count = palette_size - start;
1262 EnterCriticalSection( &palette_cs );
1263 for (i = 0; i < count; i++)
1265 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1266 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1267 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1268 entries[i].peFlags = 0;
1269 TRACE("\tidx(%02x) -> RGB(%08x)\n", start + i, *(COLORREF*)(entries + i) );
1271 LeaveCriticalSection( &palette_cs );
1276 /***********************************************************************
1277 * GetNearestColor (X11DRV.@)
1279 COLORREF X11DRV_GetNearestColor( PHYSDEV dev, COLORREF color )
1281 unsigned char spec_type = color >> 24;
1284 if (!palette_size) return color;
1286 if (spec_type == 1 || spec_type == 2)
1288 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1292 HPALETTE hpal = GetCurrentObject( dev->hdc, OBJ_PAL );
1294 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1296 if (spec_type == 2) /* PALETTERGB */
1297 index = GetNearestPaletteIndex( hpal, color );
1298 else /* PALETTEINDEX */
1299 index = LOWORD(color);
1301 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1303 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
1304 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1306 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1308 color &= 0x00ffffff;
1309 EnterCriticalSection( &palette_cs );
1310 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1311 LeaveCriticalSection( &palette_cs );
1313 TRACE("(%06x): returning %06x\n", color, nearest );
1318 /***********************************************************************
1319 * RealizeDefaultPalette (X11DRV.@)
1321 UINT X11DRV_RealizeDefaultPalette( PHYSDEV dev )
1325 if (palette_size && GetObjectType(dev->hdc) != OBJ_MEMDC)
1327 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1328 int i, index, *mapping = palette_get_mapping( GetStockObject(DEFAULT_PALETTE) );
1329 PALETTEENTRY entries[NB_RESERVED_COLORS];
1331 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, entries );
1332 EnterCriticalSection( &palette_cs );
1333 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1335 index = X11DRV_PALETTE_LookupSystemXPixel( RGB(entries[i].peRed,
1337 entries[i].peBlue) );
1338 /* mapping is allocated in COLOR_InitPalette() */
1339 if( index != mapping[i] )
1345 LeaveCriticalSection( &palette_cs );