2 * X11DRV OEM bitmap objects
4 * Copyright 1994, 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(palette);
34 /* Palette indexed mode:
35 * logical palette -> mapping -> pixel
38 * Windows needs contiguous color space ( from 0 to n ) but
39 * it is possible only with the private colormap. Otherwise we
40 * have to map DC palette indices to real pixel values. With
41 * private colormaps it boils down to the identity mapping. The
42 * other special case is when we have a fixed color visual with
43 * the screendepth > 8 - we abandon palette mappings altogether
44 * because pixel values can be calculated without X server
48 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
50 #define PC_SYS_USED 0x80 /* palentry is used (both system and logical) */
51 #define PC_SYS_RESERVED 0x40 /* system palentry is not to be mapped to */
53 static PALETTEENTRY *COLOR_sysPal; /* current system palette */
55 static int COLOR_gapStart = 256;
56 static int COLOR_gapEnd = -1;
57 static int COLOR_gapFilled = 0;
59 UINT16 X11DRV_PALETTE_PaletteFlags = 0;
61 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
62 ColorShifts X11DRV_PALETTE_default_shifts = { {0,0,0,}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
63 static int X11DRV_PALETTE_Graymax = 0;
65 static int palette_size;
67 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
68 static int X11DRV_PALETTE_firstFree = 0;
69 static unsigned char X11DRV_PALETTE_freeList[256];
71 static XContext palette_context; /* X context to associate a color mapping to a palette */
73 static CRITICAL_SECTION palette_cs;
74 static CRITICAL_SECTION_DEBUG critsect_debug =
77 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
78 0, 0, { (DWORD_PTR)(__FILE__ ": palette_cs") }
80 static CRITICAL_SECTION palette_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
82 /**********************************************************************/
84 /* Map an EGA index (0..15) to a pixel value in the system color space. */
86 int X11DRV_PALETTE_mapEGAPixel[16];
88 /**********************************************************************/
90 #define NB_COLORCUBE_START_INDEX 63
91 #define NB_PALETTE_EMPTY_VALUE -1
93 /* Maps entry in the system palette to X pixel value */
94 int *X11DRV_PALETTE_PaletteToXPixel = NULL;
96 /* Maps pixel to the entry in the system palette */
97 int *X11DRV_PALETTE_XPixelToPalette = NULL;
99 /**********************************************************************/
101 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template );
102 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template );
103 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template );
104 static void X11DRV_PALETTE_FormatSystemPalette(void);
105 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c);
106 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col);
109 /***********************************************************************
110 * palette_get_mapping
112 static int *palette_get_mapping( HPALETTE hpal )
116 if (XFindContext( gdi_display, (XID)hpal, palette_context, (char **)&mapping )) mapping = NULL;
121 /***********************************************************************
122 * palette_set_mapping
124 static void palette_set_mapping( HPALETTE hpal, int *mapping )
126 XSaveContext( gdi_display, (XID)hpal, palette_context, (char *)mapping );
130 /***********************************************************************
133 * Initialize color management.
135 int X11DRV_PALETTE_Init(void)
138 PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS];
140 TRACE("initializing palette manager...\n");
142 palette_context = XUniqueContext();
143 palette_size = default_visual.colormap_size;
145 switch(default_visual.class)
148 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
152 if (private_color_map)
154 XSetWindowAttributes win_attr;
156 XFreeColormap( gdi_display, default_colormap );
157 default_colormap = XCreateColormap( gdi_display, root_window,
158 default_visual.visual, AllocAll );
159 if (default_colormap)
161 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_PRIVATE;
163 if( root_window != DefaultRootWindow(gdi_display) )
165 win_attr.colormap = default_colormap;
166 XChangeWindowAttributes( gdi_display, root_window, CWColormap, &win_attr );
173 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
174 X11DRV_PALETTE_Graymax = (1 << default_visual.depth)-1;
178 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
181 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
182 X11DRV_PALETTE_ComputeColorShifts(&X11DRV_PALETTE_default_shifts, default_visual.red_mask, default_visual.green_mask, default_visual.blue_mask);
186 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
192 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, sys_pal_template );
194 if ((mapping = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * NB_RESERVED_COLORS )))
195 palette_set_mapping( GetStockObject(DEFAULT_PALETTE), mapping );
197 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
198 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template );
200 X11DRV_PALETTE_BuildSharedMap( sys_pal_template );
202 /* Build free list */
204 if( X11DRV_PALETTE_firstFree != -1 )
205 X11DRV_PALETTE_FormatSystemPalette();
207 X11DRV_PALETTE_FillDefaultColors( sys_pal_template );
208 palette_size = default_visual.colormap_size;
214 /***********************************************************************
215 * X11DRV_PALETTE_ComputeChannelShift
217 * Calculate conversion parameters for a given color mask
219 static void X11DRV_PALETTE_ComputeChannelShift(unsigned long maskbits, ChannelShift *physical, ChannelShift *to_logical)
234 for(i=0;!(maskbits&1);i++)
238 physical->max = maskbits;
240 for(i=0;maskbits!=0;i++)
244 if (physical->scale>8)
246 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
247 * So we adjust the shifts to also normalize the color fields to
248 * the Win32 standard of 8 bits per color.
250 to_logical->shift=physical->shift+(physical->scale-8);
252 to_logical->max=0xff;
254 to_logical->shift=physical->shift;
255 to_logical->scale=physical->scale;
256 to_logical->max=physical->max;
260 /***********************************************************************
261 * X11DRV_PALETTE_ComputeColorShifts
263 * Calculate conversion parameters for a given color
265 void X11DRV_PALETTE_ComputeColorShifts(ColorShifts *shifts, unsigned long redMask, unsigned long greenMask, unsigned long blueMask)
267 X11DRV_PALETTE_ComputeChannelShift(redMask, &shifts->physicalRed, &shifts->logicalRed);
268 X11DRV_PALETTE_ComputeChannelShift(greenMask, &shifts->physicalGreen, &shifts->logicalGreen);
269 X11DRV_PALETTE_ComputeChannelShift(blueMask, &shifts->physicalBlue, &shifts->logicalBlue);
272 /***********************************************************************
273 * X11DRV_PALETTE_BuildPrivateMap
275 * Allocate colorcells and initialize mapping tables.
277 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template )
279 /* Private colormap - identity mapping */
284 if((COLOR_sysPal = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*palette_size)) == NULL) {
285 WARN("Unable to allocate the system palette\n");
289 TRACE("Building private map - %i palette entries\n", palette_size);
291 /* Allocate system palette colors */
293 for( i=0; i < palette_size; i++ )
295 if( i < NB_RESERVED_COLORS/2 )
297 color.red = sys_pal_template[i].peRed * 65535 / 255;
298 color.green = sys_pal_template[i].peGreen * 65535 / 255;
299 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
300 COLOR_sysPal[i] = sys_pal_template[i];
301 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
303 else if( i >= palette_size - NB_RESERVED_COLORS/2 )
305 int j = NB_RESERVED_COLORS + i - palette_size;
306 color.red = sys_pal_template[j].peRed * 65535 / 255;
307 color.green = sys_pal_template[j].peGreen * 65535 / 255;
308 color.blue = sys_pal_template[j].peBlue * 65535 / 255;
309 COLOR_sysPal[i] = sys_pal_template[j];
310 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
313 color.flags = DoRed | DoGreen | DoBlue;
315 XStoreColor(gdi_display, default_colormap, &color);
317 /* Set EGA mapping if color is from the first or last eight */
320 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
321 else if (i >= palette_size - 8 )
322 X11DRV_PALETTE_mapEGAPixel[i - (palette_size - 16)] = color.pixel;
325 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
327 COLOR_gapStart = 256; COLOR_gapEnd = -1;
329 X11DRV_PALETTE_firstFree = (palette_size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
334 /***********************************************************************
335 * X11DRV_PALETTE_BuildSharedMap
337 * Allocate colorcells and initialize mapping tables.
339 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template )
342 unsigned long sysPixel[NB_RESERVED_COLORS];
343 unsigned long* pixDynMapping = NULL;
344 unsigned long plane_masks[1];
346 int diff, r, g, b, bp = 0, wp = 1;
348 unsigned int max = 256;
350 XColor defaultColors[256];
352 /* Copy the first bunch of colors out of the default colormap to prevent
353 * colormap flashing as much as possible. We're likely to get the most
354 * important Window Manager colors, etc in the first 128 colors */
355 defaultCM = DefaultColormap( gdi_display, DefaultScreen(gdi_display) );
357 if (copy_default_colors > 256) copy_default_colors = 256;
358 for (i = 0; i < copy_default_colors; i++)
359 defaultColors[i].pixel = (long) i;
360 XQueryColors(gdi_display, defaultCM, &defaultColors[0], copy_default_colors);
361 for (i = 0; i < copy_default_colors; i++)
362 XAllocColor( gdi_display, default_colormap, &defaultColors[i] );
364 if (alloc_system_colors > 256) alloc_system_colors = 256;
365 else if (alloc_system_colors < 20) alloc_system_colors = 20;
366 TRACE("%d colors configured.\n", alloc_system_colors);
368 TRACE("Building shared map - %i palette entries\n", palette_size);
370 /* Be nice and allocate system colors as read-only */
372 for( i = 0; i < NB_RESERVED_COLORS; i++ )
374 color.red = sys_pal_template[i].peRed * 65535 / 255;
375 color.green = sys_pal_template[i].peGreen * 65535 / 255;
376 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
377 color.flags = DoRed | DoGreen | DoBlue;
379 if (!XAllocColor( gdi_display, default_colormap, &color ))
385 WARN("Not enough colors for the full system palette.\n");
387 bp = BlackPixel(gdi_display, DefaultScreen(gdi_display));
388 wp = WhitePixel(gdi_display, DefaultScreen(gdi_display));
390 max = 0xffffffff >> (32 - default_visual.depth);
398 /* reinit color (XAllocColor() may change it)
399 * and map to the best shared colorcell */
401 color.red = sys_pal_template[i].peRed * 65535 / 255;
402 color.green = sys_pal_template[i].peGreen * 65535 / 255;
403 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
405 best.pixel = best.red = best.green = best.blue = 0;
406 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
408 XQueryColor(gdi_display, default_colormap, &c);
409 r = (c.red - color.red)>>8;
410 g = (c.green - color.green)>>8;
411 b = (c.blue - color.blue)>>8;
413 if( r < diff ) { best = c; diff = r; }
416 if( XAllocColor(gdi_display, default_colormap, &best) )
417 color.pixel = best.pixel;
418 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
421 sysPixel[i] = color.pixel;
423 TRACE("syscolor(%x) -> pixel %i\n", *(const COLORREF*)(sys_pal_template+i),
426 /* Set EGA mapping if color in the first or last eight */
429 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
430 else if (i >= NB_RESERVED_COLORS - 8 )
431 X11DRV_PALETTE_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
434 /* now allocate changeable set */
436 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
438 int c_min = 0, c_max = palette_size, c_val;
440 TRACE("Dynamic colormap...\n");
442 /* let's become the first client that actually follows
443 * X guidelines and does binary search...
446 if((pixDynMapping = HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size)) == NULL) {
447 WARN("Out of memory while building system palette.\n");
451 /* comment this out if you want to debug palette init */
452 XGrabServer(gdi_display);
454 while( c_max - c_min > 0 )
456 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
458 if( !XAllocColorCells(gdi_display, default_colormap, False,
459 plane_masks, 0, pixDynMapping, c_val) )
463 XFreeColors(gdi_display, default_colormap, pixDynMapping, c_val, 0);
468 if( c_min > alloc_system_colors - NB_RESERVED_COLORS)
469 c_min = alloc_system_colors - NB_RESERVED_COLORS;
471 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
474 if( !XAllocColorCells(gdi_display, default_colormap, False,
475 plane_masks, 0, pixDynMapping, c_min) )
477 WARN("Inexplicable failure during colorcell allocation.\n");
481 palette_size = c_min + NB_RESERVED_COLORS;
483 XUngrabServer(gdi_display);
485 TRACE("adjusted size %i colorcells\n", palette_size);
487 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
489 /* virtual colorspace - ToPhysical takes care of
490 * color translations but we have to allocate full palette
491 * to maintain compatibility
494 TRACE("Virtual colorspace - screendepth %i\n", default_visual.depth);
496 else palette_size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
497 * of colors and map to them */
499 TRACE("Shared system palette uses %i colors.\n", palette_size);
501 /* set gap to account for pixel shortage. It has to be right in the center
502 * of the system palette because otherwise raster ops get screwed. */
504 if( palette_size >= 256 )
505 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
507 { COLOR_gapStart = palette_size/2; COLOR_gapEnd = 255 - palette_size/2; }
509 X11DRV_PALETTE_firstFree = ( palette_size > NB_RESERVED_COLORS &&
510 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
511 ? NB_RESERVED_COLORS/2 : -1;
513 COLOR_sysPal = HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
514 if(COLOR_sysPal == NULL) {
515 ERR("Unable to allocate the system palette!\n");
516 HeapFree(GetProcessHeap(), 0, pixDynMapping);
520 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
522 if (default_visual.depth <= 8)
524 X11DRV_PALETTE_XPixelToPalette = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
525 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
526 ERR("Out of memory: XPixelToPalette!\n");
527 HeapFree(GetProcessHeap(), 0, pixDynMapping);
530 for( i = 0; i < 256; i++ )
531 X11DRV_PALETTE_XPixelToPalette[i] = NB_PALETTE_EMPTY_VALUE;
534 /* for hicolor visuals PaletteToPixel mapping is used to skip
535 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
538 X11DRV_PALETTE_PaletteToXPixel = HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
539 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
540 ERR("Out of memory: PaletteToXPixel!\n");
541 HeapFree(GetProcessHeap(), 0, pixDynMapping);
545 for( i = j = 0; i < 256; i++ )
547 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
549 X11DRV_PALETTE_PaletteToXPixel[i] = NB_PALETTE_EMPTY_VALUE;
550 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
554 if( i < NB_RESERVED_COLORS/2 )
556 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
557 COLOR_sysPal[i] = sys_pal_template[i];
558 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
560 else if( i >= 256 - NB_RESERVED_COLORS/2 )
562 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
563 COLOR_sysPal[i] = sys_pal_template[(i + NB_RESERVED_COLORS) - 256];
564 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
566 else if( pixDynMapping )
567 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
569 X11DRV_PALETTE_PaletteToXPixel[i] = i;
571 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
573 if( X11DRV_PALETTE_XPixelToPalette )
574 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
577 HeapFree(GetProcessHeap(), 0, pixDynMapping);
582 /***********************************************************************
583 * Colormap Initialization
585 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template )
587 /* initialize unused entries to what Windows uses as a color
588 * cube - based on Greg Kreider's code.
592 int red, no_r, inc_r;
593 int green, no_g, inc_g;
594 int blue, no_b, inc_b;
596 if (palette_size <= NB_RESERVED_COLORS)
598 while (i*i*i < (palette_size - NB_RESERVED_COLORS)) i++;
599 no_r = no_g = no_b = --i;
600 if ((no_r * (no_g+1) * no_b) < (palette_size - NB_RESERVED_COLORS)) no_g++;
601 if ((no_r * no_g * (no_b+1)) < (palette_size - NB_RESERVED_COLORS)) no_b++;
602 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
603 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
604 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
606 idx = X11DRV_PALETTE_firstFree;
609 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
610 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
611 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
615 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
617 COLOR_sysPal[idx].peRed = red;
618 COLOR_sysPal[idx].peGreen = green;
619 COLOR_sysPal[idx].peBlue = blue;
623 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
625 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
626 if (shifts->physicalRed.max != 255) no_r = (red * shifts->physicalRed.max) / 255;
627 if (shifts->physicalGreen.max != 255) no_g = (green * shifts->physicalGreen.max) / 255;
628 if (shifts->physicalBlue.max != 255) no_b = (blue * shifts->physicalBlue.max) / 255;
630 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << shifts->physicalRed.shift) | (no_g << shifts->physicalGreen.shift) | (no_b << shifts->physicalBlue.shift);
632 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
635 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
636 color.red = COLOR_sysPal[idx].peRed << 8;
637 color.green = COLOR_sysPal[idx].peGreen << 8;
638 color.blue = COLOR_sysPal[idx].peBlue << 8;
639 color.flags = DoRed | DoGreen | DoBlue;
640 XStoreColor(gdi_display, default_colormap, &color);
642 idx = X11DRV_PALETTE_freeList[idx];
645 /* try to fill some entries in the "gap" with
646 * what's already in the colormap - they will be
647 * mappable to but not changeable. */
649 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
654 max = alloc_system_colors - (256 - (COLOR_gapEnd - COLOR_gapStart));
655 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
656 if( X11DRV_PALETTE_XPixelToPalette[i] == NB_PALETTE_EMPTY_VALUE )
660 XQueryColor(gdi_display, default_colormap, &xc);
661 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
663 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template, RGB(r, g, b)) &&
664 XAllocColor(gdi_display, default_colormap, &xc) )
666 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
667 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
668 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
669 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
670 if( --max <= 0 ) break;
673 COLOR_gapFilled = idx - COLOR_gapStart;
678 /***********************************************************************
679 * X11DRV_IsSolidColor
681 * Check whether 'color' can be represented with a solid color.
683 BOOL X11DRV_IsSolidColor( COLORREF color )
686 const PALETTEENTRY *pEntry = COLOR_sysPal;
688 if (color & 0xff000000) return TRUE; /* indexed color */
690 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
692 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return TRUE; /* no palette */
694 EnterCriticalSection( &palette_cs );
695 for (i = 0; i < palette_size ; i++, pEntry++)
697 if( i < COLOR_gapStart || i > COLOR_gapEnd )
698 if ((GetRValue(color) == pEntry->peRed) &&
699 (GetGValue(color) == pEntry->peGreen) &&
700 (GetBValue(color) == pEntry->peBlue))
702 LeaveCriticalSection( &palette_cs );
706 LeaveCriticalSection( &palette_cs );
711 /***********************************************************************
712 * X11DRV_PALETTE_ToLogical
714 * Return RGB color for given X pixel.
716 COLORREF X11DRV_PALETTE_ToLogical(X11DRV_PDEVICE *physDev, int pixel)
720 /* check for hicolor visuals first */
722 if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
724 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
726 if(physDev->color_shifts)
727 shifts = physDev->color_shifts;
729 color.red = (pixel >> shifts->logicalRed.shift) & shifts->logicalRed.max;
730 if (shifts->logicalRed.scale<8)
731 color.red= color.red << (8-shifts->logicalRed.scale) |
732 color.red >> (2*shifts->logicalRed.scale-8);
733 color.green = (pixel >> shifts->logicalGreen.shift) & shifts->logicalGreen.max;
734 if (shifts->logicalGreen.scale<8)
735 color.green=color.green << (8-shifts->logicalGreen.scale) |
736 color.green >> (2*shifts->logicalGreen.scale-8);
737 color.blue = (pixel >> shifts->logicalBlue.shift) & shifts->logicalBlue.max;
738 if (shifts->logicalBlue.scale<8)
739 color.blue= color.blue << (8-shifts->logicalBlue.scale) |
740 color.blue >> (2*shifts->logicalBlue.scale-8);
741 return RGB(color.red,color.green,color.blue);
744 /* check if we can bypass X */
746 if ((default_visual.depth <= 8) && (pixel < 256) &&
747 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) ) {
749 EnterCriticalSection( &palette_cs );
750 ret = *(COLORREF *)(COLOR_sysPal + (X11DRV_PALETTE_XPixelToPalette ? X11DRV_PALETTE_XPixelToPalette[pixel]: pixel)) & 0x00ffffff;
751 LeaveCriticalSection( &palette_cs );
756 XQueryColor(gdi_display, default_colormap, &color);
757 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
761 /***********************************************************************
762 * X11DRV_SysPaletteLookupPixel
764 static int X11DRV_SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
766 int i, best = 0, diff = 0x7fffffff;
769 for( i = 0; i < palette_size && diff ; i++ )
771 if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) ||
772 (skipReserved && COLOR_sysPal[i].peFlags & PC_SYS_RESERVED) )
775 r = COLOR_sysPal[i].peRed - GetRValue(col);
776 g = COLOR_sysPal[i].peGreen - GetGValue(col);
777 b = COLOR_sysPal[i].peBlue - GetBValue(col);
781 if( r < diff ) { best = i; diff = r; }
787 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
789 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
790 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
793 /***********************************************************************
794 * X11DRV_PALETTE_GetColor
796 * Resolve PALETTEINDEX/PALETTERGB/DIBINDEX COLORREFs to an RGB COLORREF.
798 COLORREF X11DRV_PALETTE_GetColor( X11DRV_PDEVICE *physDev, COLORREF color )
800 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
803 if (color & (1 << 24)) /* PALETTEINDEX */
805 unsigned int idx = LOWORD(color);
806 if (!GetPaletteEntries( hPal, idx, 1, &entry )) return 0;
807 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
810 if (color >> 24 == 2) /* PALETTERGB */
812 unsigned int idx = GetNearestPaletteIndex( hPal, color & 0xffffff );
813 if (!GetPaletteEntries( hPal, idx, 1, &entry )) return 0;
814 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
817 if (color >> 16 == 0x10ff) /* DIBINDEX */
820 return color & 0xffffff;
823 /***********************************************************************
824 * X11DRV_PALETTE_ToPhysical
826 * Return the physical color closest to 'color'.
828 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
831 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
832 int *mapping = palette_get_mapping( hPal );
834 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
836 if(physDev->color_shifts)
837 shifts = physDev->color_shifts;
839 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
841 /* there is no colormap limitation; we are going to have to compute
842 * the pixel value from the visual information stored earlier
844 unsigned long red, green, blue;
846 if (color & (1 << 24)) /* PALETTEINDEX */
848 unsigned int idx = LOWORD( color );
850 if (!GetPaletteEntries( hPal, idx, 1, &entry ))
852 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color, idx);
855 if (mapping) return mapping[idx];
857 green = entry.peGreen;
860 else if (color >> 16 == 0x10ff) /* DIBINDEX */
866 if (physDev->depth == 1)
867 return (((color >> 16) & 0xff) +
868 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
869 red = GetRValue( color );
870 green = GetGValue( color );
871 blue = GetBValue( color );
874 if (X11DRV_PALETTE_Graymax)
876 /* grayscale only; return scaled value */
877 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
881 /* scale each individually and construct the TrueColor pixel value */
882 if (shifts->physicalRed.scale < 8)
883 red = red >> (8-shifts->physicalRed.scale);
884 else if (shifts->physicalRed.scale > 8)
885 red = red << (shifts->physicalRed.scale-8) |
886 red >> (16-shifts->physicalRed.scale);
887 if (shifts->physicalGreen.scale < 8)
888 green = green >> (8-shifts->physicalGreen.scale);
889 else if (shifts->physicalGreen.scale > 8)
890 green = green << (shifts->physicalGreen.scale-8) |
891 green >> (16-shifts->physicalGreen.scale);
892 if (shifts->physicalBlue.scale < 8)
893 blue = blue >> (8-shifts->physicalBlue.scale);
894 else if (shifts->physicalBlue.scale > 8)
895 blue = blue << (shifts->physicalBlue.scale-8) |
896 blue >> (16-shifts->physicalBlue.scale);
898 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
904 WARN("Palette %p is not realized\n", hPal);
906 if (color & (1 << 24)) /* PALETTEINDEX */
908 index = LOWORD( color );
909 if (!GetPaletteEntries( hPal, index, 1, &entry ))
910 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color, index);
911 else if (mapping) index = mapping[index];
913 else if (color >> 24 == 2) /* PALETTERGB */
915 index = GetNearestPaletteIndex( hPal, color );
916 if (mapping) index = mapping[index];
918 else if (color >> 16 == 0x10ff) /* DIBINDEX */
924 if (physDev->depth == 1)
925 return (((color >> 16) & 0xff) +
926 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
928 EnterCriticalSection( &palette_cs );
929 index = X11DRV_SysPaletteLookupPixel( color & 0xffffff, FALSE);
930 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
931 LeaveCriticalSection( &palette_cs );
937 /***********************************************************************
938 * X11DRV_PALETTE_LookupPixel
940 static int X11DRV_PALETTE_LookupPixel(ColorShifts *shifts, COLORREF color )
942 unsigned char spec_type = color >> 24;
944 /* Only accept RGB which has spec_type = 0 */
950 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
952 unsigned long red, green, blue;
953 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
955 if (X11DRV_PALETTE_Graymax)
957 /* grayscale only; return scaled value */
958 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
962 /* No shifts are set in case of 1-bit */
963 if(!shifts) shifts = &X11DRV_PALETTE_default_shifts;
965 /* scale each individually and construct the TrueColor pixel value */
966 if (shifts->physicalRed.scale < 8)
967 red = red >> (8-shifts->physicalRed.scale);
968 else if (shifts->physicalRed.scale > 8)
969 red = red << (shifts->physicalRed.scale-8) |
970 red >> (16-shifts->physicalRed.scale);
971 if (shifts->physicalGreen.scale < 8)
972 green = green >> (8-shifts->physicalGreen.scale);
973 else if (shifts->physicalGreen.scale > 8)
974 green = green << (shifts->physicalGreen.scale-8) |
975 green >> (16-shifts->physicalGreen.scale);
976 if (shifts->physicalBlue.scale < 8)
977 blue = blue >> (8-shifts->physicalBlue.scale);
978 else if (shifts->physicalBlue.scale > 8)
979 blue = blue << (shifts->physicalBlue.scale-8) |
980 blue >> (16-shifts->physicalBlue.scale);
982 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
988 HPALETTE hPal = GetStockObject(DEFAULT_PALETTE);
989 int *mapping = palette_get_mapping( hPal );
992 WARN("Palette %p is not realized\n", hPal);
994 EnterCriticalSection( &palette_cs );
995 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
996 if (X11DRV_PALETTE_PaletteToXPixel)
997 index = X11DRV_PALETTE_PaletteToXPixel[index];
998 LeaveCriticalSection( &palette_cs );
1004 /***********************************************************************
1005 * X11DRV_PALETTE_LookupSystemXPixel
1007 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
1009 int i, best = 0, diff = 0x7fffffff;
1010 int size = palette_size;
1013 for( i = 0; i < size && diff ; i++ )
1015 if( i == NB_RESERVED_COLORS/2 )
1017 int newi = size - NB_RESERVED_COLORS/2;
1021 r = COLOR_sysPal[i].peRed - GetRValue(col);
1022 g = COLOR_sysPal[i].peGreen - GetGValue(col);
1023 b = COLOR_sysPal[i].peBlue - GetBValue(col);
1025 r = r*r + g*g + b*b;
1027 if( r < diff ) { best = i; diff = r; }
1030 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
1033 /***********************************************************************
1034 * X11DRV_PALETTE_FormatSystemPalette
1036 static void X11DRV_PALETTE_FormatSystemPalette(void)
1038 /* Build free list so we'd have an easy way to find
1039 * out if there are any available colorcells.
1042 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1044 COLOR_sysPal[j].peFlags = 0;
1045 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1046 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1048 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1049 X11DRV_PALETTE_freeList[j] = i; /* next */
1052 X11DRV_PALETTE_freeList[j] = 0;
1055 /***********************************************************************
1056 * X11DRV_PALETTE_CheckSysColor
1058 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1061 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1062 if( c == (*(const COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1068 /***********************************************************************
1069 * X11DRV_LookupSysPaletteExact
1071 static int X11DRV_LookupSysPaletteExact( BYTE r, BYTE g, BYTE b )
1074 for( i = 0; i < palette_size; i++ )
1076 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1077 if( COLOR_sysPal[i].peRed == r &&
1078 COLOR_sysPal[i].peGreen == g &&
1079 COLOR_sysPal[i].peBlue == b )
1086 /***********************************************************************
1087 * RealizePalette (X11DRV.@)
1089 UINT X11DRV_RealizePalette( PHYSDEV dev, HPALETTE hpal, BOOL primary )
1091 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1094 UINT i, iRemapped = 0;
1095 int *prev_mapping, *mapping;
1096 PALETTEENTRY entries[256];
1099 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1101 if (!GetObjectW( hpal, sizeof(num_entries), &num_entries )) return 0;
1103 /* initialize palette mapping table */
1104 prev_mapping = palette_get_mapping( hpal );
1106 mapping = HeapReAlloc( GetProcessHeap(), 0, prev_mapping, sizeof(int)*num_entries);
1108 mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries);
1110 if(mapping == NULL) {
1111 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1114 palette_set_mapping( hpal, mapping );
1116 if (num_entries > 256)
1118 FIXME( "more than 256 entries not supported\n" );
1121 if (!(num_entries = GetPaletteEntries( hpal, 0, num_entries, entries ))) return 0;
1123 /* reset dynamic system palette entries */
1125 EnterCriticalSection( &palette_cs );
1126 if( primary && X11DRV_PALETTE_firstFree != -1)
1127 X11DRV_PALETTE_FormatSystemPalette();
1129 for (i = 0; i < num_entries; i++)
1134 /* Even though the docs say that only one flag is to be set,
1135 * they are a bitmask. At least one app sets more than one at
1137 if ( entries[i].peFlags & PC_EXPLICIT ) {
1138 /* palette entries are indices into system palette */
1139 index = *(WORD*)&entries[i];
1140 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1142 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1145 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1147 if ( entries[i].peFlags & PC_RESERVED ) {
1148 /* forbid future mappings to this entry */
1149 flag |= PC_SYS_RESERVED;
1152 if (! (entries[i].peFlags & PC_NOCOLLAPSE) ) {
1153 /* try to collapse identical colors */
1154 index = X11DRV_LookupSysPaletteExact( entries[i].peRed, entries[i].peGreen, entries[i].peBlue );
1159 if( X11DRV_PALETTE_firstFree > 0 )
1162 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1163 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1165 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1166 color.red = entries[i].peRed << 8;
1167 color.green = entries[i].peGreen << 8;
1168 color.blue = entries[i].peBlue << 8;
1169 color.flags = DoRed | DoGreen | DoBlue;
1170 XStoreColor(gdi_display, default_colormap, &color);
1172 COLOR_sysPal[index] = entries[i];
1173 COLOR_sysPal[index].peFlags = flag;
1174 X11DRV_PALETTE_freeList[index] = 0;
1176 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1178 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1180 index = X11DRV_PALETTE_LookupPixel( physDev->color_shifts, RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ));
1183 /* we have to map to existing entry in the system palette */
1185 index = X11DRV_SysPaletteLookupPixel( RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ),
1189 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1192 if( !prev_mapping || mapping[i] != index ) iRemapped++;
1195 TRACE("entry %i (%x) -> pixel %i\n", i, *(COLORREF*)&entries[i], index);
1198 LeaveCriticalSection( &palette_cs );
1203 /***********************************************************************
1204 * UnrealizePalette (X11DRV.@)
1206 BOOL X11DRV_UnrealizePalette( HPALETTE hpal )
1208 int *mapping = palette_get_mapping( hpal );
1212 XDeleteContext( gdi_display, (XID)hpal, palette_context );
1213 HeapFree( GetProcessHeap(), 0, mapping );
1219 /***********************************************************************
1220 * GetSystemPaletteEntries (X11DRV.@)
1222 UINT X11DRV_GetSystemPaletteEntries( PHYSDEV dev, UINT start, UINT count, LPPALETTEENTRY entries )
1226 if (!entries) return palette_size;
1227 if (start >= palette_size) return 0;
1228 if (start + count >= palette_size) count = palette_size - start;
1230 EnterCriticalSection( &palette_cs );
1231 for (i = 0; i < count; i++)
1233 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1234 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1235 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1236 entries[i].peFlags = 0;
1237 TRACE("\tidx(%02x) -> RGB(%08x)\n", start + i, *(COLORREF*)(entries + i) );
1239 LeaveCriticalSection( &palette_cs );
1244 /***********************************************************************
1245 * GetNearestColor (X11DRV.@)
1247 COLORREF X11DRV_GetNearestColor( PHYSDEV dev, COLORREF color )
1249 unsigned char spec_type = color >> 24;
1252 if (!palette_size) return color;
1254 if (spec_type == 1 || spec_type == 2)
1256 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1260 HPALETTE hpal = GetCurrentObject( dev->hdc, OBJ_PAL );
1262 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1264 if (spec_type == 2) /* PALETTERGB */
1265 index = GetNearestPaletteIndex( hpal, color );
1266 else /* PALETTEINDEX */
1267 index = LOWORD(color);
1269 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1271 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
1272 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1274 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1276 color &= 0x00ffffff;
1277 EnterCriticalSection( &palette_cs );
1278 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1279 LeaveCriticalSection( &palette_cs );
1281 TRACE("(%06x): returning %06x\n", color, nearest );
1286 /***********************************************************************
1287 * RealizeDefaultPalette (X11DRV.@)
1289 UINT X11DRV_RealizeDefaultPalette( PHYSDEV dev )
1293 if (palette_size && GetObjectType(dev->hdc) != OBJ_MEMDC)
1295 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1296 int i, index, *mapping = palette_get_mapping( GetStockObject(DEFAULT_PALETTE) );
1297 PALETTEENTRY entries[NB_RESERVED_COLORS];
1299 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, entries );
1300 EnterCriticalSection( &palette_cs );
1301 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1303 index = X11DRV_PALETTE_LookupSystemXPixel( RGB(entries[i].peRed,
1305 entries[i].peBlue) );
1306 /* mapping is allocated in COLOR_InitPalette() */
1307 if( index != mapping[i] )
1313 LeaveCriticalSection( &palette_cs );