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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
47 * Windows palette manager is described in the
48 * http://premium.microsoft.com/msdn/library/techart/f30/f34/f40/d4d/sa942.htm
51 static PALETTEENTRY *COLOR_sysPal; /* current system palette */
53 static int COLOR_gapStart = 256;
54 static int COLOR_gapEnd = -1;
55 static int COLOR_gapFilled = 0;
56 static int COLOR_max = 256;
58 Colormap X11DRV_PALETTE_PaletteXColormap = 0;
59 UINT16 X11DRV_PALETTE_PaletteFlags = 0;
67 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
68 static ColorShifts X11DRV_PALETTE_PRed = {0,0,0};
69 static ColorShifts X11DRV_PALETTE_LRed = {0,0,0};
70 static ColorShifts X11DRV_PALETTE_PGreen = {0,0,0};
71 static ColorShifts X11DRV_PALETTE_LGreen = {0,0,0};
72 static ColorShifts X11DRV_PALETTE_PBlue = {0,0,0};
73 static ColorShifts X11DRV_PALETTE_LBlue = {0,0,0};
74 static int X11DRV_PALETTE_Graymax = 0;
76 static int palette_size;
78 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
79 static int X11DRV_PALETTE_firstFree = 0;
80 static unsigned char X11DRV_PALETTE_freeList[256];
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_ComputeShifts(unsigned long maskbits, ColorShifts *physical, ColorShifts *to_logical);
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 /***********************************************************************
113 * Initialize color management.
115 int X11DRV_PALETTE_Init(void)
117 int mask, white, black;
119 PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS];
121 TRACE("initializing palette manager...\n");
123 white = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
124 black = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
126 for( mask = 1; !((white & mask)^(black & mask)); mask <<= 1 )
128 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
129 palette_size = visual->map_entries;
131 switch(visual->class)
134 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
139 BOOL private_color_map = FALSE;
140 /* @@ Wine registry key: HKLM\Software\Wine\Wine\Config\x11drv */
141 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
144 DWORD type, count = sizeof(buffer);
145 if(!RegQueryValueExA(hkey, "PrivateColorMap", 0, &type, buffer, &count))
148 private_color_map = (ch == 'y' || ch == 'Y' || ch == 't' || ch == 'T' || ch == '1');
154 if (private_color_map)
156 XSetWindowAttributes win_attr;
158 X11DRV_PALETTE_PaletteXColormap = XCreateColormap( gdi_display, root_window,
160 if (X11DRV_PALETTE_PaletteXColormap)
162 X11DRV_PALETTE_PaletteFlags |= (X11DRV_PALETTE_PRIVATE | X11DRV_PALETTE_WHITESET);
165 for( white = palette_size - 1; !(white & 1); white >>= 1 )
168 if( root_window != DefaultRootWindow(gdi_display) )
170 win_attr.colormap = X11DRV_PALETTE_PaletteXColormap;
171 XChangeWindowAttributes( gdi_display, root_window, CWColormap, &win_attr );
175 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
184 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
186 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
187 X11DRV_PALETTE_Graymax = (1 << screen_depth)-1;
192 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
194 int *depths,nrofdepths;
195 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
199 depths = XListDepths(gdi_display,DefaultScreen(gdi_display),&nrofdepths);
200 if ((nrofdepths==2) && ((depths[0]==4) || depths[1]==4)) {
202 for( white = palette_size - 1; !(white & 1); white >>= 1 )
204 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
205 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
210 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
212 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
213 X11DRV_PALETTE_ComputeShifts(visual->red_mask, &X11DRV_PALETTE_PRed, &X11DRV_PALETTE_LRed);
214 X11DRV_PALETTE_ComputeShifts(visual->green_mask, &X11DRV_PALETTE_PGreen, &X11DRV_PALETTE_LGreen);
215 X11DRV_PALETTE_ComputeShifts(visual->blue_mask, &X11DRV_PALETTE_PBlue, &X11DRV_PALETTE_LBlue);
223 TRACE(" visual class %i (%i)\n", visual->class, monoPlane);
225 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, sys_pal_template );
227 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
233 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
234 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template );
236 X11DRV_PALETTE_BuildSharedMap( sys_pal_template );
238 /* Build free list */
240 if( X11DRV_PALETTE_firstFree != -1 )
241 X11DRV_PALETTE_FormatSystemPalette();
243 X11DRV_PALETTE_FillDefaultColors( sys_pal_template );
244 palette_size = visual->map_entries;
250 /***********************************************************************
251 * X11DRV_PALETTE_Cleanup
253 * Free external colors we grabbed in the FillDefaultPalette()
255 void X11DRV_PALETTE_Cleanup(void)
257 if( COLOR_gapFilled )
260 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap,
261 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel + COLOR_gapStart),
267 /***********************************************************************
268 * X11DRV_PALETTE_ComputeShifts
270 * Calculate conversion parameters for direct mapped visuals
272 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits, ColorShifts *physical, ColorShifts *to_logical)
287 for(i=0;!(maskbits&1);i++)
291 physical->max = maskbits;
293 for(i=0;maskbits!=0;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);
305 to_logical->max=0xff;
307 to_logical->shift=physical->shift;
308 to_logical->scale=physical->scale;
309 to_logical->max=physical->max;
313 /***********************************************************************
314 * X11DRV_PALETTE_BuildPrivateMap
316 * Allocate colorcells and initialize mapping tables.
318 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template )
320 /* Private colormap - identity mapping */
325 if((COLOR_sysPal = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*palette_size)) == NULL) {
326 WARN("Unable to allocate the system palette\n");
330 TRACE("Building private map - %i palette entries\n", palette_size);
332 /* Allocate system palette colors */
335 for( i=0; i < palette_size; i++ )
337 if( i < NB_RESERVED_COLORS/2 )
339 color.red = sys_pal_template[i].peRed * 65535 / 255;
340 color.green = sys_pal_template[i].peGreen * 65535 / 255;
341 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
342 COLOR_sysPal[i] = sys_pal_template[i];
343 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
345 else if( i >= palette_size - NB_RESERVED_COLORS/2 )
347 int j = NB_RESERVED_COLORS + i - palette_size;
348 color.red = sys_pal_template[j].peRed * 65535 / 255;
349 color.green = sys_pal_template[j].peGreen * 65535 / 255;
350 color.blue = sys_pal_template[j].peBlue * 65535 / 255;
351 COLOR_sysPal[i] = sys_pal_template[j];
352 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
355 color.flags = DoRed | DoGreen | DoBlue;
357 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
359 /* Set EGA mapping if color is from the first or last eight */
362 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
363 else if (i >= palette_size - 8 )
364 X11DRV_PALETTE_mapEGAPixel[i - (palette_size - 16)] = color.pixel;
368 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
370 COLOR_gapStart = 256; COLOR_gapEnd = -1;
372 X11DRV_PALETTE_firstFree = (palette_size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
377 /***********************************************************************
378 * X11DRV_PALETTE_BuildSharedMap
380 * Allocate colorcells and initialize mapping tables.
382 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template )
385 unsigned long sysPixel[NB_RESERVED_COLORS];
386 unsigned long* pixDynMapping = NULL;
387 unsigned long plane_masks[1];
389 int diff, r, g, b, bp = 0, wp = 1;
391 int defaultCM_max_copy;
392 unsigned int max = 256;
394 XColor defaultColors[256];
397 defaultCM_max_copy = 128;
400 /* @@ Wine registry key: HKLM\Software\Wine\Wine\Config\x11drv */
401 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
406 count = sizeof(buffer);
407 if(!RegQueryValueExA(hkey, "CopyDefaultColors", 0, &type, buffer, &count))
408 defaultCM_max_copy = atoi(buffer);
410 count = sizeof(buffer);
411 if(!RegQueryValueExA(hkey, "AllocSystemColors", 0, &type, buffer, &count))
412 COLOR_max = atoi(buffer);
417 /* Copy the first bunch of colors out of the default colormap to prevent
418 * colormap flashing as much as possible. We're likely to get the most
419 * important Window Manager colors, etc in the first 128 colors */
420 defaultCM = DefaultColormap( gdi_display, DefaultScreen(gdi_display) );
422 for (i = 0; i < defaultCM_max_copy; i++)
423 defaultColors[i].pixel = (long) i;
425 XQueryColors(gdi_display, defaultCM, &defaultColors[0], defaultCM_max_copy);
426 for (i = 0; i < defaultCM_max_copy; i++)
427 XAllocColor( gdi_display, X11DRV_PALETTE_PaletteXColormap, &defaultColors[i] );
429 if (COLOR_max > 256) COLOR_max = 256;
430 else if (COLOR_max < 20) COLOR_max = 20;
431 TRACE("%d colors configured.\n", COLOR_max);
433 TRACE("Building shared map - %i palette entries\n", palette_size);
435 /* Be nice and allocate system colors as read-only */
437 for( i = 0; i < NB_RESERVED_COLORS; i++ )
439 color.red = sys_pal_template[i].peRed * 65535 / 255;
440 color.green = sys_pal_template[i].peGreen * 65535 / 255;
441 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
442 color.flags = DoRed | DoGreen | DoBlue;
444 if (!XAllocColor( gdi_display, X11DRV_PALETTE_PaletteXColormap, &color ))
450 WARN("Not enough colors for the full system palette.\n");
452 bp = BlackPixel(gdi_display, DefaultScreen(gdi_display));
453 wp = WhitePixel(gdi_display, DefaultScreen(gdi_display));
455 max = (0xffffffff)>>(32 - screen_depth);
463 /* reinit color (XAllocColor() may change it)
464 * and map to the best shared colorcell */
466 color.red = sys_pal_template[i].peRed * 65535 / 255;
467 color.green = sys_pal_template[i].peGreen * 65535 / 255;
468 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
470 best.pixel = best.red = best.green = best.blue = 0;
471 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
473 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &c);
474 r = (c.red - color.red)>>8;
475 g = (c.green - color.green)>>8;
476 b = (c.blue - color.blue)>>8;
478 if( r < diff ) { best = c; diff = r; }
481 if( XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &best) )
482 color.pixel = best.pixel;
483 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
486 sysPixel[i] = color.pixel;
488 TRACE("syscolor(%lx) -> pixel %i\n",
489 *(const COLORREF*)(sys_pal_template+i), (int)color.pixel);
491 /* Set EGA mapping if color in the first or last eight */
494 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
495 else if (i >= NB_RESERVED_COLORS - 8 )
496 X11DRV_PALETTE_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
500 /* now allocate changeable set */
502 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
504 int c_min = 0, c_max = palette_size, c_val;
506 TRACE("Dynamic colormap... \n");
508 /* let's become the first client that actually follows
509 * X guidelines and does binary search...
512 if((pixDynMapping = HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size)) == NULL) {
513 WARN("Out of memory while building system palette.\n");
518 /* comment this out if you want to debug palette init */
519 XGrabServer(gdi_display);
521 while( c_max - c_min > 0 )
523 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
525 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
526 plane_masks, 0, pixDynMapping, c_val) )
530 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap, pixDynMapping, c_val, 0);
535 if( c_min > COLOR_max - NB_RESERVED_COLORS)
536 c_min = COLOR_max - NB_RESERVED_COLORS;
538 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
541 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
542 plane_masks, 0, pixDynMapping, c_min) )
544 WARN("Inexplicable failure during colorcell allocation.\n");
548 palette_size = c_min + NB_RESERVED_COLORS;
550 XUngrabServer(gdi_display);
553 TRACE("adjusted size %i colorcells\n", palette_size);
555 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
557 /* virtual colorspace - ToPhysical takes care of
558 * color translations but we have to allocate full palette
559 * to maintain compatibility
562 TRACE("Virtual colorspace - screendepth %i\n", screen_depth);
564 else palette_size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
565 * of colors and map to them */
567 TRACE("Shared system palette uses %i colors.\n", palette_size);
569 /* set gap to account for pixel shortage. It has to be right in the center
570 * of the system palette because otherwise raster ops get screwed. */
572 if( palette_size >= 256 )
573 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
575 { COLOR_gapStart = palette_size/2; COLOR_gapEnd = 255 - palette_size/2; }
577 X11DRV_PALETTE_firstFree = ( palette_size > NB_RESERVED_COLORS &&
578 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
579 ? NB_RESERVED_COLORS/2 : -1;
581 COLOR_sysPal = HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
582 if(COLOR_sysPal == NULL) {
583 ERR("Unable to allocate the system palette!\n");
584 HeapFree(GetProcessHeap(), 0, pixDynMapping);
588 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
590 if (screen_depth <= 8)
592 X11DRV_PALETTE_XPixelToPalette = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
593 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
594 ERR("Out of memory: XPixelToPalette!\n");
595 HeapFree(GetProcessHeap(), 0, pixDynMapping);
598 for( i = 0; i < 256; i++ )
599 X11DRV_PALETTE_XPixelToPalette[i] = NB_PALETTE_EMPTY_VALUE;
602 /* for hicolor visuals PaletteToPixel mapping is used to skip
603 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
606 X11DRV_PALETTE_PaletteToXPixel = HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
607 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
608 ERR("Out of memory: PaletteToXPixel!\n");
609 HeapFree(GetProcessHeap(), 0, pixDynMapping);
613 for( i = j = 0; i < 256; i++ )
615 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
617 X11DRV_PALETTE_PaletteToXPixel[i] = NB_PALETTE_EMPTY_VALUE;
618 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
622 if( i < NB_RESERVED_COLORS/2 )
624 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
625 COLOR_sysPal[i] = sys_pal_template[i];
626 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
628 else if( i >= 256 - NB_RESERVED_COLORS/2 )
630 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
631 COLOR_sysPal[i] = sys_pal_template[(i + NB_RESERVED_COLORS) - 256];
632 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
634 else if( pixDynMapping )
635 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
637 X11DRV_PALETTE_PaletteToXPixel[i] = i;
639 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
641 if( X11DRV_PALETTE_XPixelToPalette )
642 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
645 HeapFree(GetProcessHeap(), 0, pixDynMapping);
650 /***********************************************************************
651 * Colormap Initialization
653 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template )
655 /* initialize unused entries to what Windows uses as a color
656 * cube - based on Greg Kreider's code.
660 int red, no_r, inc_r;
661 int green, no_g, inc_g;
662 int blue, no_b, inc_b;
664 if (palette_size <= NB_RESERVED_COLORS)
666 while (i*i*i < (palette_size - NB_RESERVED_COLORS)) i++;
667 no_r = no_g = no_b = --i;
668 if ((no_r * (no_g+1) * no_b) < (palette_size - NB_RESERVED_COLORS)) no_g++;
669 if ((no_r * no_g * (no_b+1)) < (palette_size - NB_RESERVED_COLORS)) no_b++;
670 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
671 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
672 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
676 idx = X11DRV_PALETTE_firstFree;
679 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
680 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
681 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
685 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
687 COLOR_sysPal[idx].peRed = red;
688 COLOR_sysPal[idx].peGreen = green;
689 COLOR_sysPal[idx].peBlue = blue;
693 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
695 if (X11DRV_PALETTE_PRed.max != 255) no_r = (red * X11DRV_PALETTE_PRed.max) / 255;
696 if (X11DRV_PALETTE_PGreen.max != 255) no_g = (green * X11DRV_PALETTE_PGreen.max) / 255;
697 if (X11DRV_PALETTE_PBlue.max != 255) no_b = (blue * X11DRV_PALETTE_PBlue.max) / 255;
699 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << X11DRV_PALETTE_PRed.shift) | (no_g << X11DRV_PALETTE_PGreen.shift) | (no_b << X11DRV_PALETTE_PBlue.shift);
701 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
704 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
705 color.red = COLOR_sysPal[idx].peRed << 8;
706 color.green = COLOR_sysPal[idx].peGreen << 8;
707 color.blue = COLOR_sysPal[idx].peBlue << 8;
708 color.flags = DoRed | DoGreen | DoBlue;
709 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
711 idx = X11DRV_PALETTE_freeList[idx];
714 /* try to fill some entries in the "gap" with
715 * what's already in the colormap - they will be
716 * mappable to but not changeable. */
718 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
723 max = COLOR_max - (256 - (COLOR_gapEnd - COLOR_gapStart));
724 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
725 if( X11DRV_PALETTE_XPixelToPalette[i] == NB_PALETTE_EMPTY_VALUE )
729 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc);
730 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
732 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template, RGB(r, g, b)) &&
733 XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc) )
735 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
736 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
737 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
738 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
739 if( --max <= 0 ) break;
742 COLOR_gapFilled = idx - COLOR_gapStart;
748 /***********************************************************************
749 * X11DRV_IsSolidColor
751 * Check whether 'color' can be represented with a solid color.
753 BOOL X11DRV_IsSolidColor( COLORREF color )
756 const PALETTEENTRY *pEntry = COLOR_sysPal;
758 if (color & 0xff000000) return TRUE; /* indexed color */
760 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
762 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return TRUE; /* no palette */
764 for (i = 0; i < palette_size ; i++, pEntry++)
766 if( i < COLOR_gapStart || i > COLOR_gapEnd )
767 if ((GetRValue(color) == pEntry->peRed) &&
768 (GetGValue(color) == pEntry->peGreen) &&
769 (GetBValue(color) == pEntry->peBlue)) return TRUE;
775 /***********************************************************************
776 * X11DRV_PALETTE_ToLogical
778 * Return RGB color for given X pixel.
780 COLORREF X11DRV_PALETTE_ToLogical(int pixel)
785 /* truecolor visual */
787 if (screen_depth >= 24) return pixel;
790 /* check for hicolor visuals first */
792 if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
794 color.red = (pixel >> X11DRV_PALETTE_LRed.shift) & X11DRV_PALETTE_LRed.max;
795 if (X11DRV_PALETTE_LRed.scale<8)
796 color.red= color.red << (8-X11DRV_PALETTE_LRed.scale) |
797 color.red >> (2*X11DRV_PALETTE_LRed.scale-8);
798 color.green = (pixel >> X11DRV_PALETTE_LGreen.shift) & X11DRV_PALETTE_LGreen.max;
799 if (X11DRV_PALETTE_LGreen.scale<8)
800 color.green=color.green << (8-X11DRV_PALETTE_LGreen.scale) |
801 color.green >> (2*X11DRV_PALETTE_LGreen.scale-8);
802 color.blue = (pixel >> X11DRV_PALETTE_LBlue.shift) & X11DRV_PALETTE_LBlue.max;
803 if (X11DRV_PALETTE_LBlue.scale<8)
804 color.blue= color.blue << (8-X11DRV_PALETTE_LBlue.scale) |
805 color.blue >> (2*X11DRV_PALETTE_LBlue.scale-8);
806 return RGB(color.red,color.green,color.blue);
809 /* check if we can bypass X */
811 if ((screen_depth <= 8) && (pixel < 256) &&
812 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) ) {
813 return ( *(COLORREF*)(COLOR_sysPal +
814 ((X11DRV_PALETTE_XPixelToPalette)?X11DRV_PALETTE_XPixelToPalette[pixel]:pixel)) ) & 0x00ffffff;
819 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
821 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
825 /***********************************************************************
826 * X11DRV_SysPaletteLookupPixel
828 static int X11DRV_SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
830 int i, best = 0, diff = 0x7fffffff;
833 for( i = 0; i < palette_size && diff ; i++ )
835 if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) ||
836 (skipReserved && COLOR_sysPal[i].peFlags & PC_SYS_RESERVED) )
839 r = COLOR_sysPal[i].peRed - GetRValue(col);
840 g = COLOR_sysPal[i].peGreen - GetGValue(col);
841 b = COLOR_sysPal[i].peBlue - GetBValue(col);
845 if( r < diff ) { best = i; diff = r; }
851 /***********************************************************************
852 * X11DRV_PALETTE_ToPhysical
854 * Return the physical color closest to 'color'.
856 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
859 HPALETTE hPal = physDev ? GetCurrentObject(physDev->hdc, OBJ_PAL ) : GetStockObject(DEFAULT_PALETTE);
860 unsigned char spec_type = color >> 24;
861 PALETTEOBJ* palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
863 /* palPtr can be NULL when DC is being destroyed */
864 if( !palPtr ) return 0;
866 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
868 /* there is no colormap limitation; we are going to have to compute
869 * the pixel value from the visual information stored earlier
872 unsigned long red, green, blue;
873 unsigned idx = color & 0xffff;
878 case 0x10: /* DIBINDEX */
879 if( X11DRV_GetDIBColorTable( physDev, idx, 1, &quad ) != 1 ) {
880 WARN("DIBINDEX(%lx) : idx %d is out of bounds, assuming black\n", color , idx);
881 GDI_ReleaseObj( hPal );
884 color = RGB( quad.rgbRed, quad.rgbGreen, quad.rgbBlue );
887 case 1: /* PALETTEINDEX */
889 if( idx >= palPtr->logpalette.palNumEntries)
891 WARN("PALETTEINDEX(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
892 GDI_ReleaseObj( hPal );
896 if( palPtr->mapping )
898 int ret = palPtr->mapping[idx];
899 GDI_ReleaseObj( hPal );
902 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
907 /* fall through to RGB */
910 if (physDev && (physDev->depth == 1) )
912 GDI_ReleaseObj( hPal );
913 return (((color >> 16) & 0xff) +
914 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
919 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
921 if (X11DRV_PALETTE_Graymax)
923 /* grayscale only; return scaled value */
924 GDI_ReleaseObj( hPal );
925 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
929 /* scale each individually and construct the TrueColor pixel value */
930 if (X11DRV_PALETTE_PRed.scale < 8)
931 red = red >> (8-X11DRV_PALETTE_PRed.scale);
932 else if (X11DRV_PALETTE_PRed.scale > 8)
933 red = red << (X11DRV_PALETTE_PRed.scale-8) |
934 red >> (16-X11DRV_PALETTE_PRed.scale);
935 if (X11DRV_PALETTE_PGreen.scale < 8)
936 green = green >> (8-X11DRV_PALETTE_PGreen.scale);
937 else if (X11DRV_PALETTE_PGreen.scale > 8)
938 green = green << (X11DRV_PALETTE_PGreen.scale-8) |
939 green >> (16-X11DRV_PALETTE_PGreen.scale);
940 if (X11DRV_PALETTE_PBlue.scale < 8)
941 blue = blue >> (8-X11DRV_PALETTE_PBlue.scale);
942 else if (X11DRV_PALETTE_PBlue.scale > 8)
943 blue = blue << (X11DRV_PALETTE_PBlue.scale-8) |
944 blue >> (16-X11DRV_PALETTE_PBlue.scale);
946 GDI_ReleaseObj( hPal );
947 return (red << X11DRV_PALETTE_PRed.shift) | (green << X11DRV_PALETTE_PGreen.shift) | (blue << X11DRV_PALETTE_PBlue.shift);
953 if( !palPtr->mapping )
954 WARN("Palette %p is not realized\n", hPal);
956 switch(spec_type) /* we have to peruse DC and system palette */
960 /* fall through to RGB */
963 if (physDev && (physDev->depth == 1) )
965 GDI_ReleaseObj( hPal );
966 return (((color >> 16) & 0xff) +
967 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
970 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
971 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
973 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
976 case 1: /* PALETTEINDEX */
977 index = color & 0xffff;
979 if( index >= palPtr->logpalette.palNumEntries )
980 WARN("PALETTEINDEX(%lx) : index %i is out of bounds\n", color, index);
981 else if( palPtr->mapping ) index = palPtr->mapping[index];
983 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
986 case 2: /* PALETTERGB */
987 index = GetNearestPaletteIndex( hPal, color );
988 if (palPtr->mapping) index = palPtr->mapping[index];
989 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
995 GDI_ReleaseObj( hPal );
999 /***********************************************************************
1000 * X11DRV_PALETTE_LookupSystemXPixel
1002 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
1004 int i, best = 0, diff = 0x7fffffff;
1005 int size = palette_size;
1008 for( i = 0; i < size && diff ; i++ )
1010 if( i == NB_RESERVED_COLORS/2 )
1012 int newi = size - NB_RESERVED_COLORS/2;
1016 r = COLOR_sysPal[i].peRed - GetRValue(col);
1017 g = COLOR_sysPal[i].peGreen - GetGValue(col);
1018 b = COLOR_sysPal[i].peBlue - GetBValue(col);
1020 r = r*r + g*g + b*b;
1022 if( r < diff ) { best = i; diff = r; }
1025 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
1028 /***********************************************************************
1029 * X11DRV_PALETTE_FormatSystemPalette
1031 static void X11DRV_PALETTE_FormatSystemPalette(void)
1033 /* Build free list so we'd have an easy way to find
1034 * out if there are any available colorcells.
1037 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1039 COLOR_sysPal[j].peFlags = 0;
1040 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1041 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1043 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1044 X11DRV_PALETTE_freeList[j] = i; /* next */
1047 X11DRV_PALETTE_freeList[j] = 0;
1050 /***********************************************************************
1051 * X11DRV_PALETTE_CheckSysColor
1053 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1056 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1057 if( c == (*(const COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1063 /***********************************************************************
1064 * X11DRV_LookupSysPaletteExact
1066 static int X11DRV_LookupSysPaletteExact( COLORREF col )
1069 BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col);
1070 for( i = 0; i < palette_size; i++ )
1072 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1073 if( COLOR_sysPal[i].peRed == r &&
1074 COLOR_sysPal[i].peGreen == g &&
1075 COLOR_sysPal[i].peBlue == b )
1082 /***********************************************************************
1083 * X11DRV_PALETTE_SetMapping
1085 * Set the color-mapping table for selected palette.
1086 * Return number of entries which mapping has changed.
1088 static UINT X11DRV_PALETTE_SetMapping( PALETTEOBJ* palPtr, UINT uStart, UINT uNum, BOOL mapOnly )
1091 int prevMapping = (palPtr->mapping) ? 1 : 0;
1096 /* reset dynamic system palette entries */
1098 if( !mapOnly && X11DRV_PALETTE_firstFree != -1)
1099 X11DRV_PALETTE_FormatSystemPalette();
1101 /* initialize palette mapping table */
1102 if (palPtr->mapping)
1103 mapping = HeapReAlloc( GetProcessHeap(), 0, palPtr->mapping,
1104 sizeof(int)*palPtr->logpalette.palNumEntries);
1106 mapping = HeapAlloc( GetProcessHeap(), 0,
1107 sizeof(int)*palPtr->logpalette.palNumEntries);
1109 if(mapping == NULL) {
1110 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1113 palPtr->mapping = mapping;
1115 if (uStart >= palPtr->logpalette.palNumEntries) return 0;
1117 if (uStart + uNum > palPtr->logpalette.palNumEntries)
1118 uNum = palPtr->logpalette.palNumEntries - uStart;
1120 for( uNum += uStart; uStart < uNum; uStart++ )
1125 /* Even though the docs say that only one flag is to be set,
1126 * they are a bitmask. At least one app sets more than one at
1128 if ( palPtr->logpalette.palPalEntry[uStart].peFlags & PC_EXPLICIT ) {
1129 /* palette entries are indices into system palette */
1130 index = *(WORD*)(palPtr->logpalette.palPalEntry + uStart);
1131 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1133 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1136 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1138 if ( palPtr->logpalette.palPalEntry[uStart].peFlags & PC_RESERVED ) {
1139 /* forbid future mappings to this entry */
1140 flag |= PC_SYS_RESERVED;
1143 if (! (palPtr->logpalette.palPalEntry[uStart].peFlags & PC_NOCOLLAPSE) ) {
1144 /* try to collapse identical colors */
1145 index = X11DRV_LookupSysPaletteExact(*(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1150 if( X11DRV_PALETTE_firstFree > 0 )
1153 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1154 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1156 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1157 color.red = palPtr->logpalette.palPalEntry[uStart].peRed << 8;
1158 color.green = palPtr->logpalette.palPalEntry[uStart].peGreen << 8;
1159 color.blue = palPtr->logpalette.palPalEntry[uStart].peBlue << 8;
1160 color.flags = DoRed | DoGreen | DoBlue;
1162 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
1163 wine_tsx11_unlock();
1165 COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[uStart];
1166 COLOR_sysPal[index].peFlags = flag;
1167 X11DRV_PALETTE_freeList[index] = 0;
1169 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1171 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1173 index = X11DRV_PALETTE_ToPhysical( NULL, 0x00ffffff &
1174 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1177 /* we have to map to existing entry in the system palette */
1179 index = X11DRV_SysPaletteLookupPixel( *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), TRUE);
1181 palPtr->logpalette.palPalEntry[uStart].peFlags |= PC_SYS_USED;
1183 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1186 if( !prevMapping || palPtr->mapping[uStart] != index ) iRemapped++;
1187 palPtr->mapping[uStart] = index;
1189 TRACE("entry %i (%lx) -> pixel %i\n", uStart,
1190 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), index);
1196 /***********************************************************************
1197 * GetSystemPaletteEntries (X11DRV.@)
1199 UINT X11DRV_GetSystemPaletteEntries( X11DRV_PDEVICE *physDev, UINT start, UINT count,
1200 LPPALETTEENTRY entries )
1204 if (!entries) return palette_size;
1205 if (start >= palette_size) return 0;
1206 if (start + count >= palette_size) count = palette_size - start;
1208 for (i = 0; i < count; i++)
1210 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1211 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1212 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1213 entries[i].peFlags = 0;
1214 TRACE("\tidx(%02x) -> RGB(%08lx)\n", start + i, *(COLORREF*)(entries + i) );
1220 /***********************************************************************
1221 * GetNearestColor (X11DRV.@)
1223 COLORREF X11DRV_GetNearestColor( X11DRV_PDEVICE *physDev, COLORREF color )
1225 unsigned char spec_type = color >> 24;
1228 if (!palette_size) return color;
1230 if (spec_type == 1 || spec_type == 2)
1232 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1236 HPALETTE hpal = GetCurrentObject( physDev->hdc, OBJ_PAL );
1238 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1240 if (spec_type == 2) /* PALETTERGB */
1241 index = GetNearestPaletteIndex( hpal, color );
1242 else /* PALETTEINDEX */
1243 index = LOWORD(color);
1245 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1247 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, index );
1248 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1250 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1252 color &= 0x00ffffff;
1253 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1255 TRACE("(%06lx): returning %06lx\n", color, nearest );
1260 /***********************************************************************
1261 * RealizePalette (X11DRV.@)
1263 UINT X11DRV_RealizePalette( X11DRV_PDEVICE *physDev, HPALETTE hpal, BOOL primary )
1268 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1270 if (!(palPtr = GDI_GetObjPtr( hpal, PALETTE_MAGIC ))) return 0;
1271 ret = X11DRV_PALETTE_SetMapping( palPtr, 0, palPtr->logpalette.palNumEntries, !primary );
1272 GDI_ReleaseObj( hpal );
1277 /***********************************************************************
1278 * RealizeDefaultPalette (X11DRV.@)
1280 UINT X11DRV_RealizeDefaultPalette( X11DRV_PDEVICE *physDev )
1284 if (palette_size && GetObjectType(physDev->hdc) != OBJ_MEMDC)
1286 PALETTEOBJ* palPtr = GDI_GetObjPtr( GetStockObject(DEFAULT_PALETTE), PALETTE_MAGIC );
1289 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1292 for( i = 0; i < 20; i++ )
1294 index = X11DRV_PALETTE_LookupSystemXPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
1295 /* mapping is allocated in COLOR_InitPalette() */
1296 if( index != palPtr->mapping[i] )
1298 palPtr->mapping[i]=index;
1302 GDI_ReleaseObj( GetStockObject(DEFAULT_PALETTE) );