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;
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 /***********************************************************************
813 * X11DRV_PALETTE_ToPhysical
815 * Return the physical color closest to 'color'.
817 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
820 HPALETTE hPal = physDev ? GetCurrentObject(physDev->hdc, OBJ_PAL ) : GetStockObject(DEFAULT_PALETTE);
821 unsigned char spec_type = color >> 24;
822 PALETTEOBJ* palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
824 /* palPtr can be NULL when DC is being destroyed */
825 if( !palPtr ) return 0;
827 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
829 /* there is no colormap limitation; we are going to have to compute
830 * the pixel value from the visual information stored earlier
833 unsigned long red, green, blue;
834 unsigned idx = color & 0xffff;
839 case 0x10: /* DIBINDEX */
840 if( X11DRV_GetDIBColorTable( physDev, idx, 1, &quad ) != 1 ) {
841 WARN("DIBINDEX(%lx) : idx %d is out of bounds, assuming black\n", color , idx);
842 GDI_ReleaseObj( hPal );
845 color = RGB( quad.rgbRed, quad.rgbGreen, quad.rgbBlue );
848 case 1: /* PALETTEINDEX */
850 if( idx >= palPtr->logpalette.palNumEntries)
852 WARN("PALETTEINDEX(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
853 GDI_ReleaseObj( hPal );
857 if( palPtr->mapping )
859 int ret = palPtr->mapping[idx];
860 GDI_ReleaseObj( hPal );
863 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
868 /* fall through to RGB */
871 if (physDev && (physDev->depth == 1) )
873 GDI_ReleaseObj( hPal );
874 return (((color >> 16) & 0xff) +
875 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
880 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
882 if (X11DRV_PALETTE_Graymax)
884 /* grayscale only; return scaled value */
885 GDI_ReleaseObj( hPal );
886 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
890 /* scale each individually and construct the TrueColor pixel value */
891 if (X11DRV_PALETTE_PRed.scale < 8)
892 red = red >> (8-X11DRV_PALETTE_PRed.scale);
893 else if (X11DRV_PALETTE_PRed.scale > 8)
894 red = red << (X11DRV_PALETTE_PRed.scale-8) |
895 red >> (16-X11DRV_PALETTE_PRed.scale);
896 if (X11DRV_PALETTE_PGreen.scale < 8)
897 green = green >> (8-X11DRV_PALETTE_PGreen.scale);
898 else if (X11DRV_PALETTE_PGreen.scale > 8)
899 green = green << (X11DRV_PALETTE_PGreen.scale-8) |
900 green >> (16-X11DRV_PALETTE_PGreen.scale);
901 if (X11DRV_PALETTE_PBlue.scale < 8)
902 blue = blue >> (8-X11DRV_PALETTE_PBlue.scale);
903 else if (X11DRV_PALETTE_PBlue.scale > 8)
904 blue = blue << (X11DRV_PALETTE_PBlue.scale-8) |
905 blue >> (16-X11DRV_PALETTE_PBlue.scale);
907 GDI_ReleaseObj( hPal );
908 return (red << X11DRV_PALETTE_PRed.shift) | (green << X11DRV_PALETTE_PGreen.shift) | (blue << X11DRV_PALETTE_PBlue.shift);
914 if( !palPtr->mapping )
915 WARN("Palette %p is not realized\n", hPal);
917 switch(spec_type) /* we have to peruse DC and system palette */
921 /* fall through to RGB */
924 if (physDev && (physDev->depth == 1) )
926 GDI_ReleaseObj( hPal );
927 return (((color >> 16) & 0xff) +
928 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
931 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
932 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
934 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
937 case 1: /* PALETTEINDEX */
938 index = color & 0xffff;
940 if( index >= palPtr->logpalette.palNumEntries )
941 WARN("PALETTEINDEX(%lx) : index %i is out of bounds\n", color, index);
942 else if( palPtr->mapping ) index = palPtr->mapping[index];
944 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
947 case 2: /* PALETTERGB */
948 index = GetNearestPaletteIndex( hPal, color );
949 if (palPtr->mapping) index = palPtr->mapping[index];
950 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
956 GDI_ReleaseObj( hPal );
960 /***********************************************************************
961 * X11DRV_PALETTE_LookupSystemXPixel
963 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
965 int i, best = 0, diff = 0x7fffffff;
966 int size = palette_size;
969 for( i = 0; i < size && diff ; i++ )
971 if( i == NB_RESERVED_COLORS/2 )
973 int newi = size - NB_RESERVED_COLORS/2;
977 r = COLOR_sysPal[i].peRed - GetRValue(col);
978 g = COLOR_sysPal[i].peGreen - GetGValue(col);
979 b = COLOR_sysPal[i].peBlue - GetBValue(col);
983 if( r < diff ) { best = i; diff = r; }
986 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
989 /***********************************************************************
990 * X11DRV_PALETTE_FormatSystemPalette
992 static void X11DRV_PALETTE_FormatSystemPalette(void)
994 /* Build free list so we'd have an easy way to find
995 * out if there are any available colorcells.
998 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1000 COLOR_sysPal[j].peFlags = 0;
1001 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1002 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1004 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1005 X11DRV_PALETTE_freeList[j] = i; /* next */
1008 X11DRV_PALETTE_freeList[j] = 0;
1011 /***********************************************************************
1012 * X11DRV_PALETTE_CheckSysColor
1014 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1017 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1018 if( c == (*(const COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1024 /***********************************************************************
1025 * X11DRV_LookupSysPaletteExact
1027 static int X11DRV_LookupSysPaletteExact( COLORREF col )
1030 BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col);
1031 for( i = 0; i < palette_size; i++ )
1033 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1034 if( COLOR_sysPal[i].peRed == r &&
1035 COLOR_sysPal[i].peGreen == g &&
1036 COLOR_sysPal[i].peBlue == b )
1043 /***********************************************************************
1044 * X11DRV_PALETTE_SetMapping
1046 * Set the color-mapping table for selected palette.
1047 * Return number of entries which mapping has changed.
1049 static UINT X11DRV_PALETTE_SetMapping( PALETTEOBJ* palPtr, UINT uStart, UINT uNum, BOOL mapOnly )
1052 int prevMapping = (palPtr->mapping) ? 1 : 0;
1057 /* reset dynamic system palette entries */
1059 if( !mapOnly && X11DRV_PALETTE_firstFree != -1)
1060 X11DRV_PALETTE_FormatSystemPalette();
1062 /* initialize palette mapping table */
1063 if (palPtr->mapping)
1064 mapping = HeapReAlloc( GetProcessHeap(), 0, palPtr->mapping,
1065 sizeof(int)*palPtr->logpalette.palNumEntries);
1067 mapping = HeapAlloc( GetProcessHeap(), 0,
1068 sizeof(int)*palPtr->logpalette.palNumEntries);
1070 if(mapping == NULL) {
1071 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1074 palPtr->mapping = mapping;
1076 if (uStart >= palPtr->logpalette.palNumEntries) return 0;
1078 if (uStart + uNum > palPtr->logpalette.palNumEntries)
1079 uNum = palPtr->logpalette.palNumEntries - uStart;
1081 for( uNum += uStart; uStart < uNum; uStart++ )
1086 /* Even though the docs say that only one flag is to be set,
1087 * they are a bitmask. At least one app sets more than one at
1089 if ( palPtr->logpalette.palPalEntry[uStart].peFlags & PC_EXPLICIT ) {
1090 /* palette entries are indices into system palette */
1091 index = *(WORD*)(palPtr->logpalette.palPalEntry + uStart);
1092 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1094 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1097 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1099 if ( palPtr->logpalette.palPalEntry[uStart].peFlags & PC_RESERVED ) {
1100 /* forbid future mappings to this entry */
1101 flag |= PC_SYS_RESERVED;
1104 if (! (palPtr->logpalette.palPalEntry[uStart].peFlags & PC_NOCOLLAPSE) ) {
1105 /* try to collapse identical colors */
1106 index = X11DRV_LookupSysPaletteExact(*(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1111 if( X11DRV_PALETTE_firstFree > 0 )
1114 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1115 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1117 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1118 color.red = palPtr->logpalette.palPalEntry[uStart].peRed << 8;
1119 color.green = palPtr->logpalette.palPalEntry[uStart].peGreen << 8;
1120 color.blue = palPtr->logpalette.palPalEntry[uStart].peBlue << 8;
1121 color.flags = DoRed | DoGreen | DoBlue;
1123 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
1124 wine_tsx11_unlock();
1126 COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[uStart];
1127 COLOR_sysPal[index].peFlags = flag;
1128 X11DRV_PALETTE_freeList[index] = 0;
1130 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1132 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1134 index = X11DRV_PALETTE_ToPhysical( NULL, 0x00ffffff &
1135 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1138 /* we have to map to existing entry in the system palette */
1140 index = X11DRV_SysPaletteLookupPixel( *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), TRUE);
1142 palPtr->logpalette.palPalEntry[uStart].peFlags |= PC_SYS_USED;
1144 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1147 if( !prevMapping || palPtr->mapping[uStart] != index ) iRemapped++;
1148 palPtr->mapping[uStart] = index;
1150 TRACE("entry %i (%lx) -> pixel %i\n", uStart,
1151 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), index);
1157 /***********************************************************************
1158 * GetSystemPaletteEntries (X11DRV.@)
1160 UINT X11DRV_GetSystemPaletteEntries( X11DRV_PDEVICE *physDev, UINT start, UINT count,
1161 LPPALETTEENTRY entries )
1165 if (!entries) return palette_size;
1166 if (start >= palette_size) return 0;
1167 if (start + count >= palette_size) count = palette_size - start;
1169 for (i = 0; i < count; i++)
1171 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1172 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1173 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1174 entries[i].peFlags = 0;
1175 TRACE("\tidx(%02x) -> RGB(%08lx)\n", start + i, *(COLORREF*)(entries + i) );
1181 /***********************************************************************
1182 * GetNearestColor (X11DRV.@)
1184 COLORREF X11DRV_GetNearestColor( X11DRV_PDEVICE *physDev, COLORREF color )
1186 unsigned char spec_type = color >> 24;
1189 if (!palette_size) return color;
1191 if (spec_type == 1 || spec_type == 2)
1193 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1197 HPALETTE hpal = GetCurrentObject( physDev->hdc, OBJ_PAL );
1199 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1201 if (spec_type == 2) /* PALETTERGB */
1202 index = GetNearestPaletteIndex( hpal, color );
1203 else /* PALETTEINDEX */
1204 index = LOWORD(color);
1206 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1208 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, index );
1209 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1211 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1213 color &= 0x00ffffff;
1214 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1216 TRACE("(%06lx): returning %06lx\n", color, nearest );
1221 /***********************************************************************
1222 * RealizePalette (X11DRV.@)
1224 UINT X11DRV_RealizePalette( X11DRV_PDEVICE *physDev, HPALETTE hpal, BOOL primary )
1229 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1231 if (!(palPtr = GDI_GetObjPtr( hpal, PALETTE_MAGIC ))) return 0;
1232 ret = X11DRV_PALETTE_SetMapping( palPtr, 0, palPtr->logpalette.palNumEntries, !primary );
1233 GDI_ReleaseObj( hpal );
1238 /***********************************************************************
1239 * RealizeDefaultPalette (X11DRV.@)
1241 UINT X11DRV_RealizeDefaultPalette( X11DRV_PDEVICE *physDev )
1245 if (palette_size && GetObjectType(physDev->hdc) != OBJ_MEMDC)
1247 PALETTEOBJ* palPtr = GDI_GetObjPtr( GetStockObject(DEFAULT_PALETTE), PALETTE_MAGIC );
1250 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1253 for( i = 0; i < 20; i++ )
1255 index = X11DRV_PALETTE_LookupSystemXPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
1256 /* mapping is allocated in COLOR_InitPalette() */
1257 if( index != palPtr->mapping[i] )
1259 palPtr->mapping[i]=index;
1263 GDI_ReleaseObj( GetStockObject(DEFAULT_PALETTE) );