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
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;
57 Colormap X11DRV_PALETTE_PaletteXColormap = 0;
58 UINT16 X11DRV_PALETTE_PaletteFlags = 0;
66 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
67 static ColorShifts X11DRV_PALETTE_PRed = {0,0,0};
68 static ColorShifts X11DRV_PALETTE_LRed = {0,0,0};
69 static ColorShifts X11DRV_PALETTE_PGreen = {0,0,0};
70 static ColorShifts X11DRV_PALETTE_LGreen = {0,0,0};
71 static ColorShifts X11DRV_PALETTE_PBlue = {0,0,0};
72 static ColorShifts X11DRV_PALETTE_LBlue = {0,0,0};
73 static int X11DRV_PALETTE_Graymax = 0;
75 static int palette_size;
77 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
78 static int X11DRV_PALETTE_firstFree = 0;
79 static unsigned char X11DRV_PALETTE_freeList[256];
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_ComputeShifts(unsigned long maskbits, ColorShifts *physical, ColorShifts *to_logical);
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 /***********************************************************************
112 * Initialize color management.
114 int X11DRV_PALETTE_Init(void)
116 int mask, white, black;
118 PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS];
120 TRACE("initializing palette manager...\n");
122 white = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
123 black = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
125 for( mask = 1; !((white & mask)^(black & mask)); mask <<= 1 )
127 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
128 palette_size = visual->map_entries;
130 switch(visual->class)
133 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
137 if (private_color_map)
139 XSetWindowAttributes win_attr;
141 X11DRV_PALETTE_PaletteXColormap = XCreateColormap( gdi_display, root_window,
143 if (X11DRV_PALETTE_PaletteXColormap)
145 X11DRV_PALETTE_PaletteFlags |= (X11DRV_PALETTE_PRIVATE | X11DRV_PALETTE_WHITESET);
148 for( white = palette_size - 1; !(white & 1); white >>= 1 )
151 if( root_window != DefaultRootWindow(gdi_display) )
153 win_attr.colormap = X11DRV_PALETTE_PaletteXColormap;
154 XChangeWindowAttributes( gdi_display, root_window, CWColormap, &win_attr );
158 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
166 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
168 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
169 X11DRV_PALETTE_Graymax = (1 << screen_depth)-1;
174 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
176 int *depths,nrofdepths;
177 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
181 depths = XListDepths(gdi_display,DefaultScreen(gdi_display),&nrofdepths);
182 if ((nrofdepths==2) && ((depths[0]==4) || depths[1]==4)) {
184 for( white = palette_size - 1; !(white & 1); white >>= 1 )
186 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
187 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
192 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
194 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
195 X11DRV_PALETTE_ComputeShifts(visual->red_mask, &X11DRV_PALETTE_PRed, &X11DRV_PALETTE_LRed);
196 X11DRV_PALETTE_ComputeShifts(visual->green_mask, &X11DRV_PALETTE_PGreen, &X11DRV_PALETTE_LGreen);
197 X11DRV_PALETTE_ComputeShifts(visual->blue_mask, &X11DRV_PALETTE_PBlue, &X11DRV_PALETTE_LBlue);
205 TRACE(" visual class %i (%i)\n", visual->class, monoPlane);
207 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, sys_pal_template );
209 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
215 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
216 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template );
218 X11DRV_PALETTE_BuildSharedMap( sys_pal_template );
220 /* Build free list */
222 if( X11DRV_PALETTE_firstFree != -1 )
223 X11DRV_PALETTE_FormatSystemPalette();
225 X11DRV_PALETTE_FillDefaultColors( sys_pal_template );
226 palette_size = visual->map_entries;
232 /***********************************************************************
233 * X11DRV_PALETTE_Cleanup
235 * Free external colors we grabbed in the FillDefaultPalette()
237 void X11DRV_PALETTE_Cleanup(void)
239 if( COLOR_gapFilled )
242 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap,
243 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel + COLOR_gapStart),
249 /***********************************************************************
250 * X11DRV_PALETTE_ComputeShifts
252 * Calculate conversion parameters for direct mapped visuals
254 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits, ColorShifts *physical, ColorShifts *to_logical)
269 for(i=0;!(maskbits&1);i++)
273 physical->max = maskbits;
275 for(i=0;maskbits!=0;i++)
279 if (physical->scale>8)
281 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
282 * So we adjust the shifts to also normalize the color fields to
283 * the Win32 standard of 8 bits per color.
285 to_logical->shift=physical->shift+(physical->scale-8);
287 to_logical->max=0xff;
289 to_logical->shift=physical->shift;
290 to_logical->scale=physical->scale;
291 to_logical->max=physical->max;
295 /***********************************************************************
296 * X11DRV_PALETTE_BuildPrivateMap
298 * Allocate colorcells and initialize mapping tables.
300 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template )
302 /* Private colormap - identity mapping */
307 if((COLOR_sysPal = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*palette_size)) == NULL) {
308 WARN("Unable to allocate the system palette\n");
312 TRACE("Building private map - %i palette entries\n", palette_size);
314 /* Allocate system palette colors */
317 for( i=0; i < palette_size; i++ )
319 if( i < NB_RESERVED_COLORS/2 )
321 color.red = sys_pal_template[i].peRed * 65535 / 255;
322 color.green = sys_pal_template[i].peGreen * 65535 / 255;
323 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
324 COLOR_sysPal[i] = sys_pal_template[i];
325 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
327 else if( i >= palette_size - NB_RESERVED_COLORS/2 )
329 int j = NB_RESERVED_COLORS + i - palette_size;
330 color.red = sys_pal_template[j].peRed * 65535 / 255;
331 color.green = sys_pal_template[j].peGreen * 65535 / 255;
332 color.blue = sys_pal_template[j].peBlue * 65535 / 255;
333 COLOR_sysPal[i] = sys_pal_template[j];
334 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
337 color.flags = DoRed | DoGreen | DoBlue;
339 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
341 /* Set EGA mapping if color is from the first or last eight */
344 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
345 else if (i >= palette_size - 8 )
346 X11DRV_PALETTE_mapEGAPixel[i - (palette_size - 16)] = color.pixel;
350 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
352 COLOR_gapStart = 256; COLOR_gapEnd = -1;
354 X11DRV_PALETTE_firstFree = (palette_size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
359 /***********************************************************************
360 * X11DRV_PALETTE_BuildSharedMap
362 * Allocate colorcells and initialize mapping tables.
364 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template )
367 unsigned long sysPixel[NB_RESERVED_COLORS];
368 unsigned long* pixDynMapping = NULL;
369 unsigned long plane_masks[1];
371 int diff, r, g, b, bp = 0, wp = 1;
373 unsigned int max = 256;
375 XColor defaultColors[256];
377 /* Copy the first bunch of colors out of the default colormap to prevent
378 * colormap flashing as much as possible. We're likely to get the most
379 * important Window Manager colors, etc in the first 128 colors */
380 defaultCM = DefaultColormap( gdi_display, DefaultScreen(gdi_display) );
382 if (copy_default_colors > 256) copy_default_colors = 256;
383 for (i = 0; i < copy_default_colors; i++)
384 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, X11DRV_PALETTE_PaletteXColormap, &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, X11DRV_PALETTE_PaletteXColormap, &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 - screen_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, X11DRV_PALETTE_PaletteXColormap, &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, X11DRV_PALETTE_PaletteXColormap, &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(%lx) -> pixel %i\n",
450 *(const COLORREF*)(sys_pal_template+i), (int)color.pixel);
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;
461 /* now allocate changeable set */
463 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
465 int c_min = 0, c_max = palette_size, c_val;
467 TRACE("Dynamic colormap...\n");
469 /* let's become the first client that actually follows
470 * X guidelines and does binary search...
473 if((pixDynMapping = HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size)) == NULL) {
474 WARN("Out of memory while building system palette.\n");
479 /* comment this out if you want to debug palette init */
480 XGrabServer(gdi_display);
482 while( c_max - c_min > 0 )
484 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
486 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
487 plane_masks, 0, pixDynMapping, c_val) )
491 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap, pixDynMapping, c_val, 0);
496 if( c_min > alloc_system_colors - NB_RESERVED_COLORS)
497 c_min = alloc_system_colors - NB_RESERVED_COLORS;
499 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
502 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
503 plane_masks, 0, pixDynMapping, c_min) )
505 WARN("Inexplicable failure during colorcell allocation.\n");
509 palette_size = c_min + NB_RESERVED_COLORS;
511 XUngrabServer(gdi_display);
514 TRACE("adjusted size %i colorcells\n", palette_size);
516 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
518 /* virtual colorspace - ToPhysical takes care of
519 * color translations but we have to allocate full palette
520 * to maintain compatibility
523 TRACE("Virtual colorspace - screendepth %i\n", screen_depth);
525 else palette_size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
526 * of colors and map to them */
528 TRACE("Shared system palette uses %i colors.\n", palette_size);
530 /* set gap to account for pixel shortage. It has to be right in the center
531 * of the system palette because otherwise raster ops get screwed. */
533 if( palette_size >= 256 )
534 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
536 { COLOR_gapStart = palette_size/2; COLOR_gapEnd = 255 - palette_size/2; }
538 X11DRV_PALETTE_firstFree = ( palette_size > NB_RESERVED_COLORS &&
539 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
540 ? NB_RESERVED_COLORS/2 : -1;
542 COLOR_sysPal = HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
543 if(COLOR_sysPal == NULL) {
544 ERR("Unable to allocate the system palette!\n");
545 HeapFree(GetProcessHeap(), 0, pixDynMapping);
549 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
551 if (screen_depth <= 8)
553 X11DRV_PALETTE_XPixelToPalette = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
554 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
555 ERR("Out of memory: XPixelToPalette!\n");
556 HeapFree(GetProcessHeap(), 0, pixDynMapping);
559 for( i = 0; i < 256; i++ )
560 X11DRV_PALETTE_XPixelToPalette[i] = NB_PALETTE_EMPTY_VALUE;
563 /* for hicolor visuals PaletteToPixel mapping is used to skip
564 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
567 X11DRV_PALETTE_PaletteToXPixel = HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
568 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
569 ERR("Out of memory: PaletteToXPixel!\n");
570 HeapFree(GetProcessHeap(), 0, pixDynMapping);
574 for( i = j = 0; i < 256; i++ )
576 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
578 X11DRV_PALETTE_PaletteToXPixel[i] = NB_PALETTE_EMPTY_VALUE;
579 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
583 if( i < NB_RESERVED_COLORS/2 )
585 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
586 COLOR_sysPal[i] = sys_pal_template[i];
587 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
589 else if( i >= 256 - NB_RESERVED_COLORS/2 )
591 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
592 COLOR_sysPal[i] = sys_pal_template[(i + NB_RESERVED_COLORS) - 256];
593 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
595 else if( pixDynMapping )
596 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
598 X11DRV_PALETTE_PaletteToXPixel[i] = i;
600 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
602 if( X11DRV_PALETTE_XPixelToPalette )
603 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
606 HeapFree(GetProcessHeap(), 0, pixDynMapping);
611 /***********************************************************************
612 * Colormap Initialization
614 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template )
616 /* initialize unused entries to what Windows uses as a color
617 * cube - based on Greg Kreider's code.
621 int red, no_r, inc_r;
622 int green, no_g, inc_g;
623 int blue, no_b, inc_b;
625 if (palette_size <= NB_RESERVED_COLORS)
627 while (i*i*i < (palette_size - NB_RESERVED_COLORS)) i++;
628 no_r = no_g = no_b = --i;
629 if ((no_r * (no_g+1) * no_b) < (palette_size - NB_RESERVED_COLORS)) no_g++;
630 if ((no_r * no_g * (no_b+1)) < (palette_size - NB_RESERVED_COLORS)) no_b++;
631 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
632 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
633 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
637 idx = X11DRV_PALETTE_firstFree;
640 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
641 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
642 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
646 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
648 COLOR_sysPal[idx].peRed = red;
649 COLOR_sysPal[idx].peGreen = green;
650 COLOR_sysPal[idx].peBlue = blue;
654 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
656 if (X11DRV_PALETTE_PRed.max != 255) no_r = (red * X11DRV_PALETTE_PRed.max) / 255;
657 if (X11DRV_PALETTE_PGreen.max != 255) no_g = (green * X11DRV_PALETTE_PGreen.max) / 255;
658 if (X11DRV_PALETTE_PBlue.max != 255) no_b = (blue * X11DRV_PALETTE_PBlue.max) / 255;
660 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << X11DRV_PALETTE_PRed.shift) | (no_g << X11DRV_PALETTE_PGreen.shift) | (no_b << X11DRV_PALETTE_PBlue.shift);
662 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
665 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
666 color.red = COLOR_sysPal[idx].peRed << 8;
667 color.green = COLOR_sysPal[idx].peGreen << 8;
668 color.blue = COLOR_sysPal[idx].peBlue << 8;
669 color.flags = DoRed | DoGreen | DoBlue;
670 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
672 idx = X11DRV_PALETTE_freeList[idx];
675 /* try to fill some entries in the "gap" with
676 * what's already in the colormap - they will be
677 * mappable to but not changeable. */
679 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
684 max = alloc_system_colors - (256 - (COLOR_gapEnd - COLOR_gapStart));
685 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
686 if( X11DRV_PALETTE_XPixelToPalette[i] == NB_PALETTE_EMPTY_VALUE )
690 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc);
691 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
693 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template, RGB(r, g, b)) &&
694 XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc) )
696 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
697 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
698 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
699 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
700 if( --max <= 0 ) break;
703 COLOR_gapFilled = idx - COLOR_gapStart;
709 /***********************************************************************
710 * X11DRV_IsSolidColor
712 * Check whether 'color' can be represented with a solid color.
714 BOOL X11DRV_IsSolidColor( COLORREF color )
717 const PALETTEENTRY *pEntry = COLOR_sysPal;
719 if (color & 0xff000000) return TRUE; /* indexed color */
721 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
723 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return TRUE; /* no palette */
725 for (i = 0; i < palette_size ; i++, pEntry++)
727 if( i < COLOR_gapStart || i > COLOR_gapEnd )
728 if ((GetRValue(color) == pEntry->peRed) &&
729 (GetGValue(color) == pEntry->peGreen) &&
730 (GetBValue(color) == pEntry->peBlue)) return TRUE;
736 /***********************************************************************
737 * X11DRV_PALETTE_ToLogical
739 * Return RGB color for given X pixel.
741 COLORREF X11DRV_PALETTE_ToLogical(int pixel)
746 /* truecolor visual */
748 if (screen_depth >= 24) return pixel;
751 /* check for hicolor visuals first */
753 if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
755 color.red = (pixel >> X11DRV_PALETTE_LRed.shift) & X11DRV_PALETTE_LRed.max;
756 if (X11DRV_PALETTE_LRed.scale<8)
757 color.red= color.red << (8-X11DRV_PALETTE_LRed.scale) |
758 color.red >> (2*X11DRV_PALETTE_LRed.scale-8);
759 color.green = (pixel >> X11DRV_PALETTE_LGreen.shift) & X11DRV_PALETTE_LGreen.max;
760 if (X11DRV_PALETTE_LGreen.scale<8)
761 color.green=color.green << (8-X11DRV_PALETTE_LGreen.scale) |
762 color.green >> (2*X11DRV_PALETTE_LGreen.scale-8);
763 color.blue = (pixel >> X11DRV_PALETTE_LBlue.shift) & X11DRV_PALETTE_LBlue.max;
764 if (X11DRV_PALETTE_LBlue.scale<8)
765 color.blue= color.blue << (8-X11DRV_PALETTE_LBlue.scale) |
766 color.blue >> (2*X11DRV_PALETTE_LBlue.scale-8);
767 return RGB(color.red,color.green,color.blue);
770 /* check if we can bypass X */
772 if ((screen_depth <= 8) && (pixel < 256) &&
773 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) ) {
774 return ( *(COLORREF*)(COLOR_sysPal +
775 ((X11DRV_PALETTE_XPixelToPalette)?X11DRV_PALETTE_XPixelToPalette[pixel]:pixel)) ) & 0x00ffffff;
780 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
782 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
786 /***********************************************************************
787 * X11DRV_SysPaletteLookupPixel
789 static int X11DRV_SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
791 int i, best = 0, diff = 0x7fffffff;
794 for( i = 0; i < palette_size && diff ; i++ )
796 if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) ||
797 (skipReserved && COLOR_sysPal[i].peFlags & PC_SYS_RESERVED) )
800 r = COLOR_sysPal[i].peRed - GetRValue(col);
801 g = COLOR_sysPal[i].peGreen - GetGValue(col);
802 b = COLOR_sysPal[i].peBlue - GetBValue(col);
806 if( r < diff ) { best = i; diff = r; }
812 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
814 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
815 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
818 /***********************************************************************
819 * X11DRV_PALETTE_ToPhysical
821 * Return the physical color closest to 'color'.
823 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
826 HPALETTE hPal = physDev ? GetCurrentObject(physDev->hdc, OBJ_PAL ) : GetStockObject(DEFAULT_PALETTE);
827 unsigned char spec_type = color >> 24;
828 PALETTEOBJ* palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
830 /* palPtr can be NULL when DC is being destroyed */
831 if( !palPtr ) return 0;
833 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
835 /* there is no colormap limitation; we are going to have to compute
836 * the pixel value from the visual information stored earlier
839 unsigned long red, green, blue;
840 unsigned idx = color & 0xffff;
845 case 0x10: /* DIBINDEX */
846 if( X11DRV_GetDIBColorTable( physDev, idx, 1, &quad ) != 1 ) {
847 WARN("DIBINDEX(%lx) : idx %d is out of bounds, assuming black\n", color , idx);
848 GDI_ReleaseObj( hPal );
851 color = RGB( quad.rgbRed, quad.rgbGreen, quad.rgbBlue );
854 case 1: /* PALETTEINDEX */
856 if( idx >= palPtr->logpalette.palNumEntries)
858 WARN("PALETTEINDEX(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
859 GDI_ReleaseObj( hPal );
863 if( palPtr->mapping )
865 int ret = palPtr->mapping[idx];
866 GDI_ReleaseObj( hPal );
869 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
874 /* fall through to RGB */
877 if (physDev && (physDev->depth == 1) )
881 GDI_ReleaseObj( hPal );
882 if (physDev->bitmap && physDev->bitmap->colorTable)
884 if(!colour_is_brighter(physDev->bitmap->colorTable[1], physDev->bitmap->colorTable[0]))
887 return (((color >> 16) & 0xff) +
888 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? white : 1 - white;
893 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
895 if (X11DRV_PALETTE_Graymax)
897 /* grayscale only; return scaled value */
898 GDI_ReleaseObj( hPal );
899 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
903 /* scale each individually and construct the TrueColor pixel value */
904 if (X11DRV_PALETTE_PRed.scale < 8)
905 red = red >> (8-X11DRV_PALETTE_PRed.scale);
906 else if (X11DRV_PALETTE_PRed.scale > 8)
907 red = red << (X11DRV_PALETTE_PRed.scale-8) |
908 red >> (16-X11DRV_PALETTE_PRed.scale);
909 if (X11DRV_PALETTE_PGreen.scale < 8)
910 green = green >> (8-X11DRV_PALETTE_PGreen.scale);
911 else if (X11DRV_PALETTE_PGreen.scale > 8)
912 green = green << (X11DRV_PALETTE_PGreen.scale-8) |
913 green >> (16-X11DRV_PALETTE_PGreen.scale);
914 if (X11DRV_PALETTE_PBlue.scale < 8)
915 blue = blue >> (8-X11DRV_PALETTE_PBlue.scale);
916 else if (X11DRV_PALETTE_PBlue.scale > 8)
917 blue = blue << (X11DRV_PALETTE_PBlue.scale-8) |
918 blue >> (16-X11DRV_PALETTE_PBlue.scale);
920 GDI_ReleaseObj( hPal );
921 return (red << X11DRV_PALETTE_PRed.shift) | (green << X11DRV_PALETTE_PGreen.shift) | (blue << X11DRV_PALETTE_PBlue.shift);
927 if( !palPtr->mapping )
928 WARN("Palette %p is not realized\n", hPal);
930 switch(spec_type) /* we have to peruse DC and system palette */
934 /* fall through to RGB */
937 if (physDev && (physDev->depth == 1) )
941 GDI_ReleaseObj( hPal );
942 if (physDev->bitmap && physDev->bitmap->colorTable)
944 if(!colour_is_brighter(physDev->bitmap->colorTable[1], physDev->bitmap->colorTable[0]))
947 return (((color >> 16) & 0xff) +
948 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? white : 1 - white;
951 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
952 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
954 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
957 case 1: /* PALETTEINDEX */
958 index = color & 0xffff;
960 if( index >= palPtr->logpalette.palNumEntries )
961 WARN("PALETTEINDEX(%lx) : index %i is out of bounds\n", color, index);
962 else if( palPtr->mapping ) index = palPtr->mapping[index];
964 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
967 case 2: /* PALETTERGB */
968 index = GetNearestPaletteIndex( hPal, color );
969 if (palPtr->mapping) index = palPtr->mapping[index];
970 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
976 GDI_ReleaseObj( hPal );
980 /***********************************************************************
981 * X11DRV_PALETTE_LookupSystemXPixel
983 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
985 int i, best = 0, diff = 0x7fffffff;
986 int size = palette_size;
989 for( i = 0; i < size && diff ; i++ )
991 if( i == NB_RESERVED_COLORS/2 )
993 int newi = size - NB_RESERVED_COLORS/2;
997 r = COLOR_sysPal[i].peRed - GetRValue(col);
998 g = COLOR_sysPal[i].peGreen - GetGValue(col);
999 b = COLOR_sysPal[i].peBlue - GetBValue(col);
1001 r = r*r + g*g + b*b;
1003 if( r < diff ) { best = i; diff = r; }
1006 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
1009 /***********************************************************************
1010 * X11DRV_PALETTE_FormatSystemPalette
1012 static void X11DRV_PALETTE_FormatSystemPalette(void)
1014 /* Build free list so we'd have an easy way to find
1015 * out if there are any available colorcells.
1018 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1020 COLOR_sysPal[j].peFlags = 0;
1021 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1022 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1024 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1025 X11DRV_PALETTE_freeList[j] = i; /* next */
1028 X11DRV_PALETTE_freeList[j] = 0;
1031 /***********************************************************************
1032 * X11DRV_PALETTE_CheckSysColor
1034 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1037 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1038 if( c == (*(const COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1044 /***********************************************************************
1045 * X11DRV_LookupSysPaletteExact
1047 static int X11DRV_LookupSysPaletteExact( COLORREF col )
1050 BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col);
1051 for( i = 0; i < palette_size; i++ )
1053 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1054 if( COLOR_sysPal[i].peRed == r &&
1055 COLOR_sysPal[i].peGreen == g &&
1056 COLOR_sysPal[i].peBlue == b )
1063 /***********************************************************************
1064 * X11DRV_PALETTE_SetMapping
1066 * Set the color-mapping table for selected palette.
1067 * Return number of entries which mapping has changed.
1069 static UINT X11DRV_PALETTE_SetMapping( PALETTEOBJ* palPtr, UINT uStart, UINT uNum, BOOL mapOnly )
1072 int prevMapping = (palPtr->mapping) ? 1 : 0;
1077 /* reset dynamic system palette entries */
1079 if( !mapOnly && X11DRV_PALETTE_firstFree != -1)
1080 X11DRV_PALETTE_FormatSystemPalette();
1082 /* initialize palette mapping table */
1083 if (palPtr->mapping)
1084 mapping = HeapReAlloc( GetProcessHeap(), 0, palPtr->mapping,
1085 sizeof(int)*palPtr->logpalette.palNumEntries);
1087 mapping = HeapAlloc( GetProcessHeap(), 0,
1088 sizeof(int)*palPtr->logpalette.palNumEntries);
1090 if(mapping == NULL) {
1091 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1094 palPtr->mapping = mapping;
1096 if (uStart >= palPtr->logpalette.palNumEntries) return 0;
1098 if (uStart + uNum > palPtr->logpalette.palNumEntries)
1099 uNum = palPtr->logpalette.palNumEntries - uStart;
1101 for( uNum += uStart; uStart < uNum; uStart++ )
1106 /* Even though the docs say that only one flag is to be set,
1107 * they are a bitmask. At least one app sets more than one at
1109 if ( palPtr->logpalette.palPalEntry[uStart].peFlags & PC_EXPLICIT ) {
1110 /* palette entries are indices into system palette */
1111 index = *(WORD*)(palPtr->logpalette.palPalEntry + uStart);
1112 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1114 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1117 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1119 if ( palPtr->logpalette.palPalEntry[uStart].peFlags & PC_RESERVED ) {
1120 /* forbid future mappings to this entry */
1121 flag |= PC_SYS_RESERVED;
1124 if (! (palPtr->logpalette.palPalEntry[uStart].peFlags & PC_NOCOLLAPSE) ) {
1125 /* try to collapse identical colors */
1126 index = X11DRV_LookupSysPaletteExact(*(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1131 if( X11DRV_PALETTE_firstFree > 0 )
1134 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1135 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1137 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1138 color.red = palPtr->logpalette.palPalEntry[uStart].peRed << 8;
1139 color.green = palPtr->logpalette.palPalEntry[uStart].peGreen << 8;
1140 color.blue = palPtr->logpalette.palPalEntry[uStart].peBlue << 8;
1141 color.flags = DoRed | DoGreen | DoBlue;
1143 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
1144 wine_tsx11_unlock();
1146 COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[uStart];
1147 COLOR_sysPal[index].peFlags = flag;
1148 X11DRV_PALETTE_freeList[index] = 0;
1150 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1152 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1154 index = X11DRV_PALETTE_ToPhysical( NULL, 0x00ffffff &
1155 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1158 /* we have to map to existing entry in the system palette */
1160 index = X11DRV_SysPaletteLookupPixel( *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), TRUE);
1162 palPtr->logpalette.palPalEntry[uStart].peFlags |= PC_SYS_USED;
1164 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1167 if( !prevMapping || palPtr->mapping[uStart] != index ) iRemapped++;
1168 palPtr->mapping[uStart] = index;
1170 TRACE("entry %i (%lx) -> pixel %i\n", uStart,
1171 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), index);
1177 /***********************************************************************
1178 * GetSystemPaletteEntries (X11DRV.@)
1180 UINT X11DRV_GetSystemPaletteEntries( X11DRV_PDEVICE *physDev, UINT start, UINT count,
1181 LPPALETTEENTRY entries )
1185 if (!entries) return palette_size;
1186 if (start >= palette_size) return 0;
1187 if (start + count >= palette_size) count = palette_size - start;
1189 for (i = 0; i < count; i++)
1191 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1192 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1193 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1194 entries[i].peFlags = 0;
1195 TRACE("\tidx(%02x) -> RGB(%08lx)\n", start + i, *(COLORREF*)(entries + i) );
1201 /***********************************************************************
1202 * GetNearestColor (X11DRV.@)
1204 COLORREF X11DRV_GetNearestColor( X11DRV_PDEVICE *physDev, COLORREF color )
1206 unsigned char spec_type = color >> 24;
1209 if (!palette_size) return color;
1211 if (spec_type == 1 || spec_type == 2)
1213 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1217 HPALETTE hpal = GetCurrentObject( physDev->hdc, OBJ_PAL );
1219 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1221 if (spec_type == 2) /* PALETTERGB */
1222 index = GetNearestPaletteIndex( hpal, color );
1223 else /* PALETTEINDEX */
1224 index = LOWORD(color);
1226 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1228 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, index );
1229 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1231 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1233 color &= 0x00ffffff;
1234 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1236 TRACE("(%06lx): returning %06lx\n", color, nearest );
1241 /***********************************************************************
1242 * RealizePalette (X11DRV.@)
1244 UINT X11DRV_RealizePalette( X11DRV_PDEVICE *physDev, HPALETTE hpal, BOOL primary )
1249 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1251 if (!(palPtr = GDI_GetObjPtr( hpal, PALETTE_MAGIC ))) return 0;
1252 ret = X11DRV_PALETTE_SetMapping( palPtr, 0, palPtr->logpalette.palNumEntries, !primary );
1253 GDI_ReleaseObj( hpal );
1258 /***********************************************************************
1259 * RealizeDefaultPalette (X11DRV.@)
1261 UINT X11DRV_RealizeDefaultPalette( X11DRV_PDEVICE *physDev )
1265 if (palette_size && GetObjectType(physDev->hdc) != OBJ_MEMDC)
1267 PALETTEOBJ* palPtr = GDI_GetObjPtr( GetStockObject(DEFAULT_PALETTE), PALETTE_MAGIC );
1270 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1273 for( i = 0; i < 20; i++ )
1275 index = X11DRV_PALETTE_LookupSystemXPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
1276 /* mapping is allocated in COLOR_InitPalette() */
1277 if( index != palPtr->mapping[i] )
1279 palPtr->mapping[i]=index;
1283 GDI_ReleaseObj( GetStockObject(DEFAULT_PALETTE) );