Copy the first 128 colors from the default colormap to Wine's private
[wine.git] / graphics / x11drv / palette.c
blob76f23cf4a4cb01ca25662570ccc7e9f4eb4cf2ff
1 /*
2 * X11DRV OEM bitmap objects
4 * Copyright 1994, 1995 Alexandre Julliard
6 */
8 #include "config.h"
10 #include "ts_xlib.h"
12 #include <stdlib.h>
13 #include <string.h>
15 #include "color.h"
16 #include "debugtools.h"
17 #include "gdi.h"
18 #include "options.h"
19 #include "palette.h"
20 #include "windef.h"
21 #include "x11drv.h"
23 DEFAULT_DEBUG_CHANNEL(palette);
25 /* Palette indexed mode:
26 * logical palette -> mapping -> pixel
29 * Windows needs contiguous color space ( from 0 to n ) but
30 * it is possible only with the private colormap. Otherwise we
31 * have to map DC palette indices to real pixel values. With
32 * private colormaps it boils down to the identity mapping. The
33 * other special case is when we have a fixed color visual with
34 * the screendepth > 8 - we abandon palette mappings altogether
35 * because pixel values can be calculated without X server
36 * assistance.
38 * Windows palette manager is described in the
39 * http://premium.microsoft.com/msdn/library/techart/f30/f34/f40/d4d/sa942.htm
42 extern PALETTEENTRY *COLOR_sysPal;
43 extern int COLOR_gapStart;
44 extern int COLOR_gapEnd;
45 extern int COLOR_gapFilled;
46 extern int COLOR_max;
48 extern const PALETTEENTRY COLOR_sysPalTemplate[NB_RESERVED_COLORS];
50 Colormap X11DRV_PALETTE_PaletteXColormap = 0;
51 UINT16 X11DRV_PALETTE_PaletteFlags = 0;
53 static int X11DRV_PALETTE_Redshift = 0; /* to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
54 static int X11DRV_PALETTE_Redmax = 0;
55 static int X11DRV_PALETTE_Greenshift = 0;
56 static int X11DRV_PALETTE_Greenmax = 0;
57 static int X11DRV_PALETTE_Blueshift = 0;
58 static int X11DRV_PALETTE_Bluemax = 0;
59 static int X11DRV_PALETTE_Graymax = 0;
61 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
62 static int X11DRV_PALETTE_firstFree = 0;
63 static unsigned char X11DRV_PALETTE_freeList[256];
65 /**********************************************************************/
67 /* Map an EGA index (0..15) to a pixel value in the system color space. */
69 int X11DRV_PALETTE_mapEGAPixel[16];
71 /**********************************************************************/
73 #define NB_COLORCUBE_START_INDEX 63
75 /* Maps entry in the system palette to X pixel value */
76 int *X11DRV_PALETTE_PaletteToXPixel = NULL;
78 /* Maps pixel to the entry in the system palette */
79 int *X11DRV_PALETTE_XPixelToPalette = NULL;
81 /**********************************************************************/
83 static BOOL X11DRV_PALETTE_BuildPrivateMap(void);
84 static BOOL X11DRV_PALETTE_BuildSharedMap(void);
85 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits, int *shift, int *max);
86 static void X11DRV_PALETTE_FillDefaultColors(void);
87 static void X11DRV_PALETTE_FormatSystemPalette(void);
88 static BOOL X11DRV_PALETTE_CheckSysColor(COLORREF c);
89 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col);
91 /***********************************************************************
92 * COLOR_Init
94 * Initialize color management.
96 BOOL X11DRV_PALETTE_Init(void)
98 int mask, white, black;
99 int monoPlane;
101 Visual *visual = X11DRV_GetVisual();
103 TRACE("initializing palette manager...\n");
105 white = WhitePixelOfScreen( X11DRV_GetXScreen() );
106 black = BlackPixelOfScreen( X11DRV_GetXScreen() );
107 monoPlane = 1;
108 for( mask = 1; !((white & mask)^(black & mask)); mask <<= 1 )
109 monoPlane++;
110 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
111 X11DRV_DevCaps.sizePalette = visual->map_entries;
113 switch(visual->class)
115 case DirectColor:
116 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
117 case GrayScale:
118 case PseudoColor:
119 if (PROFILE_GetWineIniBool( "x11drv", "PrivateColorMap", 0 ))
121 XSetWindowAttributes win_attr;
123 X11DRV_PALETTE_PaletteXColormap = TSXCreateColormap( display, X11DRV_GetXRootWindow(),
124 visual, AllocAll );
125 if (X11DRV_PALETTE_PaletteXColormap)
127 X11DRV_PALETTE_PaletteFlags |= (X11DRV_PALETTE_PRIVATE | X11DRV_PALETTE_WHITESET);
129 monoPlane = 1;
130 for( white = X11DRV_DevCaps.sizePalette - 1; !(white & 1); white >>= 1 )
131 monoPlane++;
133 if( X11DRV_GetXRootWindow() != DefaultRootWindow(display) )
135 win_attr.colormap = X11DRV_PALETTE_PaletteXColormap;
136 TSXChangeWindowAttributes( display, X11DRV_GetXRootWindow(),
137 CWColormap, &win_attr );
139 break;
142 X11DRV_PALETTE_PaletteXColormap = TSXCreateColormap(display, X11DRV_GetXRootWindow(), visual, AllocNone);
143 break;
145 case StaticGray:
146 X11DRV_PALETTE_PaletteXColormap = TSXCreateColormap(display, X11DRV_GetXRootWindow(), visual, AllocNone);
147 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
148 X11DRV_PALETTE_Graymax = (1 << X11DRV_GetDepth())-1;
149 break;
151 case TrueColor:
152 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
153 case StaticColor: {
154 int *depths,nrofdepths;
155 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
156 * depths 1 and 4
158 depths=TSXListDepths(display,DefaultScreen(display),&nrofdepths);
159 if ((nrofdepths==2) && ((depths[0]==4) || depths[1]==4)) {
160 monoPlane = 1;
161 for( white = X11DRV_DevCaps.sizePalette - 1; !(white & 1); white >>= 1 )
162 monoPlane++;
163 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
164 X11DRV_PALETTE_PaletteXColormap = TSXCreateColormap(display, X11DRV_GetXRootWindow(), visual, AllocNone);
165 TSXFree(depths);
166 break;
168 TSXFree(depths);
169 X11DRV_PALETTE_PaletteXColormap = TSXCreateColormap(display, X11DRV_GetXRootWindow(), visual, AllocNone);
170 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
171 X11DRV_PALETTE_ComputeShifts(visual->red_mask, &X11DRV_PALETTE_Redshift, &X11DRV_PALETTE_Redmax);
172 X11DRV_PALETTE_ComputeShifts(visual->green_mask, &X11DRV_PALETTE_Greenshift, &X11DRV_PALETTE_Greenmax);
173 X11DRV_PALETTE_ComputeShifts(visual->blue_mask, &X11DRV_PALETTE_Blueshift, &X11DRV_PALETTE_Bluemax);
174 break;
178 TRACE(" visual class %i (%i)\n", visual->class, monoPlane);
180 memset(X11DRV_PALETTE_freeList, 0, 256*sizeof(unsigned char));
182 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
183 X11DRV_PALETTE_BuildPrivateMap();
184 else
185 X11DRV_PALETTE_BuildSharedMap();
187 /* Build free list */
189 if( X11DRV_PALETTE_firstFree != -1 )
190 X11DRV_PALETTE_FormatSystemPalette();
192 X11DRV_PALETTE_FillDefaultColors();
194 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
195 X11DRV_DevCaps.sizePalette = 0;
196 else
198 X11DRV_DevCaps.rasterCaps |= RC_PALETTE;
199 X11DRV_DevCaps.sizePalette = visual->map_entries;
202 return TRUE;
205 /***********************************************************************
206 * X11DRV_PALETTE_Cleanup
208 * Free external colors we grabbed in the FillDefaultPalette()
210 void X11DRV_PALETTE_Cleanup(void)
212 if( COLOR_gapFilled )
213 TSXFreeColors(display, X11DRV_PALETTE_PaletteXColormap,
214 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel + COLOR_gapStart),
215 COLOR_gapFilled, 0);
218 /***********************************************************************
219 * X11DRV_PALETTE_ComputeShifts
221 * Calculate conversion parameters for direct mapped visuals
223 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits, int *shift, int *max)
225 int i;
227 if (maskbits==0)
229 *shift=0;
230 *max=0;
231 return;
234 for(i=0;!(maskbits&1);i++)
235 maskbits >>= 1;
237 *shift = i;
238 *max = maskbits;
241 /***********************************************************************
242 * X11DRV_PALETTE_BuildPrivateMap
244 * Allocate colorcells and initialize mapping tables.
246 static BOOL X11DRV_PALETTE_BuildPrivateMap(void)
248 /* Private colormap - identity mapping */
250 XColor color;
251 int i;
253 if((COLOR_sysPal = (PALETTEENTRY*)HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*X11DRV_DevCaps.sizePalette)) == NULL) {
254 WARN("Can not allocate system palette\n");
255 return FALSE;
258 TRACE("Building private map - %i palette entries\n", X11DRV_DevCaps.sizePalette);
260 /* Allocate system palette colors */
262 for( i=0; i < X11DRV_DevCaps.sizePalette; i++ )
264 if( i < NB_RESERVED_COLORS/2 )
266 color.red = COLOR_sysPalTemplate[i].peRed * 65535 / 255;
267 color.green = COLOR_sysPalTemplate[i].peGreen * 65535 / 255;
268 color.blue = COLOR_sysPalTemplate[i].peBlue * 65535 / 255;
269 COLOR_sysPal[i] = COLOR_sysPalTemplate[i];
271 else if( i >= X11DRV_DevCaps.sizePalette - NB_RESERVED_COLORS/2 )
273 int j = NB_RESERVED_COLORS + i - X11DRV_DevCaps.sizePalette;
274 color.red = COLOR_sysPalTemplate[j].peRed * 65535 / 255;
275 color.green = COLOR_sysPalTemplate[j].peGreen * 65535 / 255;
276 color.blue = COLOR_sysPalTemplate[j].peBlue * 65535 / 255;
277 COLOR_sysPal[i] = COLOR_sysPalTemplate[j];
280 color.flags = DoRed | DoGreen | DoBlue;
281 color.pixel = i;
282 TSXStoreColor(display, X11DRV_PALETTE_PaletteXColormap, &color);
284 /* Set EGA mapping if color is from the first or last eight */
286 if (i < 8)
287 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
288 else if (i >= X11DRV_DevCaps.sizePalette - 8 )
289 X11DRV_PALETTE_mapEGAPixel[i - (X11DRV_DevCaps.sizePalette - 16)] = color.pixel;
292 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
294 COLOR_gapStart = 256; COLOR_gapEnd = -1;
296 X11DRV_PALETTE_firstFree = (X11DRV_DevCaps.sizePalette > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
298 return FALSE;
301 /***********************************************************************
302 * X11DRV_PALETTE_BuildSharedMap
304 * Allocate colorcells and initialize mapping tables.
306 static BOOL X11DRV_PALETTE_BuildSharedMap(void)
308 XColor color;
309 unsigned long sysPixel[NB_RESERVED_COLORS];
310 unsigned long* pixDynMapping = NULL;
311 unsigned long plane_masks[1];
312 int i, j, warn = 0;
313 int diff, r, g, b, max = 256, bp = 0, wp = 1;
314 int step = 1;
315 int defaultCM_max_copy;
316 Colormap defaultCM;
317 XColor defaultColors[256];
319 /* Copy the first bunch of colors out of the default colormap to prevent
320 * colormap flashing as much as possible. We're likely to get the most
321 * important Window Manager colors, etc in the first 128 colors */
322 defaultCM = DefaultColormapOfScreen( X11DRV_GetXScreen() );
323 defaultCM_max_copy = PROFILE_GetWineIniInt( "x11drv", "CopyDefaultColors", 128);
324 for (i = 0; i < defaultCM_max_copy; i++)
325 defaultColors[i].pixel = (long) i;
326 TSXQueryColors(display, defaultCM, &defaultColors[0], defaultCM_max_copy);
327 for (i = 0; i < defaultCM_max_copy; i++)
328 TSXAllocColor( display, X11DRV_PALETTE_PaletteXColormap, &defaultColors[i] );
330 /* read "AllocSystemColors" from wine.conf */
332 COLOR_max = PROFILE_GetWineIniInt( "x11drv", "AllocSystemColors", 256);
333 if (COLOR_max > 256) COLOR_max = 256;
334 else if (COLOR_max < 20) COLOR_max = 20;
335 TRACE("%d colors configured.\n", COLOR_max);
337 TRACE("Building shared map - %i palette entries\n", X11DRV_DevCaps.sizePalette);
339 /* Be nice and allocate system colors as read-only */
341 for( i = 0; i < NB_RESERVED_COLORS; i++ )
343 color.red = COLOR_sysPalTemplate[i].peRed * 65535 / 255;
344 color.green = COLOR_sysPalTemplate[i].peGreen * 65535 / 255;
345 color.blue = COLOR_sysPalTemplate[i].peBlue * 65535 / 255;
346 color.flags = DoRed | DoGreen | DoBlue;
348 if (!TSXAllocColor( display, X11DRV_PALETTE_PaletteXColormap, &color ))
350 XColor best, c;
352 if( !warn++ )
354 WARN("Not enough colors for the full system palette.\n");
356 bp = BlackPixel(display, DefaultScreen(display));
357 wp = WhitePixel(display, DefaultScreen(display));
359 max = (0xffffffff)>>(32 - X11DRV_GetDepth());
360 if( max > 256 )
362 step = max/256;
363 max = 256;
367 /* reinit color (XAllocColor() may change it)
368 * and map to the best shared colorcell */
370 color.red = COLOR_sysPalTemplate[i].peRed * 65535 / 255;
371 color.green = COLOR_sysPalTemplate[i].peGreen * 65535 / 255;
372 color.blue = COLOR_sysPalTemplate[i].peBlue * 65535 / 255;
374 best.pixel = best.red = best.green = best.blue = 0;
375 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
377 TSXQueryColor(display, X11DRV_PALETTE_PaletteXColormap, &c);
378 r = (c.red - color.red)>>8;
379 g = (c.green - color.green)>>8;
380 b = (c.blue - color.blue)>>8;
381 r = r*r + g*g + b*b;
382 if( r < diff ) { best = c; diff = r; }
385 if( TSXAllocColor(display, X11DRV_PALETTE_PaletteXColormap, &best) )
386 color.pixel = best.pixel;
387 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
390 sysPixel[i] = color.pixel;
392 TRACE("syscolor(%lx) -> pixel %i\n",
393 *(COLORREF*)(COLOR_sysPalTemplate+i), (int)color.pixel);
395 /* Set EGA mapping if color in the first or last eight */
397 if (i < 8)
398 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
399 else if (i >= NB_RESERVED_COLORS - 8 )
400 X11DRV_PALETTE_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
403 /* now allocate changeable set */
405 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
407 int c_min = 0, c_max = X11DRV_DevCaps.sizePalette, c_val;
409 TRACE("Dynamic colormap... \n");
411 /* let's become the first client that actually follows
412 * X guidelines and does binary search...
415 if((pixDynMapping = (unsigned long*)HeapAlloc(GetProcessHeap(), 0, sizeof(long)*X11DRV_DevCaps.sizePalette)) == NULL) {
416 WARN("Out of memory while building system palette.\n");
417 return FALSE;
420 /* comment this out if you want to debug palette init */
421 TSXGrabServer(display);
423 while( c_max - c_min > 0 )
425 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
427 if( !TSXAllocColorCells(display, X11DRV_PALETTE_PaletteXColormap, False,
428 plane_masks, 0, pixDynMapping, c_val) )
429 c_max = c_val - 1;
430 else
432 TSXFreeColors(display, X11DRV_PALETTE_PaletteXColormap, pixDynMapping, c_val, 0);
433 c_min = c_val;
437 if( c_min > COLOR_max - NB_RESERVED_COLORS)
438 c_min = COLOR_max - NB_RESERVED_COLORS;
440 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
442 if( c_min > 0 )
443 if( !TSXAllocColorCells(display, X11DRV_PALETTE_PaletteXColormap, False,
444 plane_masks, 0, pixDynMapping, c_min) )
446 WARN("Inexplicable failure during colorcell allocation.\n");
447 c_min = 0;
450 X11DRV_DevCaps.sizePalette = c_min + NB_RESERVED_COLORS;
452 TSXUngrabServer(display);
454 TRACE("adjusted size %i colorcells\n", X11DRV_DevCaps.sizePalette);
456 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
458 /* virtual colorspace - ToPhysical takes care of
459 * color translations but we have to allocate full palette
460 * to maintain compatibility
462 X11DRV_DevCaps.sizePalette = 256;
463 TRACE("Virtual colorspace - screendepth %i\n", X11DRV_GetDepth());
465 else X11DRV_DevCaps.sizePalette = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
466 * of colors and map to them */
468 TRACE("Shared system palette uses %i colors.\n", X11DRV_DevCaps.sizePalette);
470 /* set gap to account for pixel shortage. It has to be right in the center
471 * of the system palette because otherwise raster ops get screwed. */
473 if( X11DRV_DevCaps.sizePalette >= 256 )
474 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
475 else
476 { COLOR_gapStart = X11DRV_DevCaps.sizePalette/2; COLOR_gapEnd = 255 - X11DRV_DevCaps.sizePalette/2; }
478 X11DRV_PALETTE_firstFree = ( X11DRV_DevCaps.sizePalette > NB_RESERVED_COLORS &&
479 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
480 ? NB_RESERVED_COLORS/2 : -1;
482 COLOR_sysPal = (PALETTEENTRY*)HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
483 if(COLOR_sysPal == NULL) {
484 ERR("Can not allocate system palette!\n");
485 return FALSE;
488 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
490 if (X11DRV_GetDepth() <= 8)
492 X11DRV_PALETTE_XPixelToPalette = (int*)calloc(256, sizeof(int));
493 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
494 ERR("Out of memory: XPixelToPalette!\n");
495 return FALSE;
499 /* for hicolor visuals PaletteToPixel mapping is used to skip
500 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
503 X11DRV_PALETTE_PaletteToXPixel = (int*)HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
504 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
505 ERR("Out of memory: PaletteToXPixel!\n");
506 return FALSE;
509 for( i = j = 0; i < 256; i++ )
511 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
513 X11DRV_PALETTE_PaletteToXPixel[i] = 0;
514 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
515 continue;
518 if( i < NB_RESERVED_COLORS/2 )
520 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
521 COLOR_sysPal[i] = COLOR_sysPalTemplate[i];
523 else if( i >= 256 - NB_RESERVED_COLORS/2 )
525 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
526 COLOR_sysPal[i] = COLOR_sysPalTemplate[(i + NB_RESERVED_COLORS) - 256];
528 else if( pixDynMapping )
529 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
530 else
531 X11DRV_PALETTE_PaletteToXPixel[i] = i;
533 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
535 if( X11DRV_PALETTE_XPixelToPalette )
536 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
539 if( pixDynMapping ) HeapFree(GetProcessHeap(), 0, pixDynMapping);
541 return TRUE;
544 /***********************************************************************
545 * Colormap Initialization
547 static void X11DRV_PALETTE_FillDefaultColors(void)
549 /* initialize unused entries to what Windows uses as a color
550 * cube - based on Greg Kreider's code.
553 int i = 0, idx = 0;
554 int red, no_r, inc_r;
555 int green, no_g, inc_g;
556 int blue, no_b, inc_b;
558 if (X11DRV_DevCaps.sizePalette <= NB_RESERVED_COLORS)
559 return;
560 while (i*i*i < (X11DRV_DevCaps.sizePalette - NB_RESERVED_COLORS)) i++;
561 no_r = no_g = no_b = --i;
562 if ((no_r * (no_g+1) * no_b) < (X11DRV_DevCaps.sizePalette - NB_RESERVED_COLORS)) no_g++;
563 if ((no_r * no_g * (no_b+1)) < (X11DRV_DevCaps.sizePalette - NB_RESERVED_COLORS)) no_b++;
564 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
565 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
566 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
568 idx = X11DRV_PALETTE_firstFree;
570 if( idx != -1 )
571 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
572 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
573 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
575 /* weird but true */
577 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
579 COLOR_sysPal[idx].peRed = red;
580 COLOR_sysPal[idx].peGreen = green;
581 COLOR_sysPal[idx].peBlue = blue;
583 /* set X color */
585 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
587 if (X11DRV_PALETTE_Redmax != 255) no_r = (red * X11DRV_PALETTE_Redmax) / 255;
588 if (X11DRV_PALETTE_Greenmax != 255) no_g = (green * X11DRV_PALETTE_Greenmax) / 255;
589 if (X11DRV_PALETTE_Bluemax != 255) no_b = (blue * X11DRV_PALETTE_Bluemax) / 255;
591 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << X11DRV_PALETTE_Redshift) | (no_g << X11DRV_PALETTE_Greenshift) | (no_b << X11DRV_PALETTE_Blueshift);
593 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
595 XColor color;
596 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
597 color.red = COLOR_sysPal[idx].peRed << 8;
598 color.green = COLOR_sysPal[idx].peGreen << 8;
599 color.blue = COLOR_sysPal[idx].peBlue << 8;
600 color.flags = DoRed | DoGreen | DoBlue;
601 TSXStoreColor(display, X11DRV_PALETTE_PaletteXColormap, &color);
603 idx = X11DRV_PALETTE_freeList[idx];
606 /* try to fill some entries in the "gap" with
607 * what's already in the colormap - they will be
608 * mappable to but not changeable. */
610 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
612 XColor xc;
613 int r, g, b, max;
615 max = COLOR_max - (256 - (COLOR_gapEnd - COLOR_gapStart));
616 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
617 if( X11DRV_PALETTE_XPixelToPalette[i] == 0 )
619 xc.pixel = i;
621 TSXQueryColor(display, X11DRV_PALETTE_PaletteXColormap, &xc);
622 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
624 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor(RGB(r, g, b)) &&
625 TSXAllocColor(display, X11DRV_PALETTE_PaletteXColormap, &xc) )
627 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
628 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
629 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
630 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
631 if( --max <= 0 ) break;
634 COLOR_gapFilled = idx - COLOR_gapStart;
639 /***********************************************************************
640 * X11DRV_PALETTE_ToLogical
642 * Return RGB color for given X pixel.
644 COLORREF X11DRV_PALETTE_ToLogical(int pixel)
646 XColor color;
648 #if 0
649 /* truecolor visual */
651 if (X11DRV_GetDepth() >= 24) return pixel;
652 #endif
654 /* check for hicolor visuals first */
656 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED && !X11DRV_PALETTE_Graymax )
658 color.red = (pixel >> X11DRV_PALETTE_Redshift) & X11DRV_PALETTE_Redmax;
659 color.green = (pixel >> X11DRV_PALETTE_Greenshift) & X11DRV_PALETTE_Greenmax;
660 color.blue = (pixel >> X11DRV_PALETTE_Blueshift) & X11DRV_PALETTE_Bluemax;
661 return RGB(MulDiv(color.red, 255, X11DRV_PALETTE_Redmax),
662 MulDiv(color.green, 255, X11DRV_PALETTE_Greenmax),
663 MulDiv(color.blue, 255, X11DRV_PALETTE_Bluemax));
666 /* check if we can bypass X */
668 if ((X11DRV_GetDepth() <= 8) && (pixel < 256) &&
669 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) )
670 return ( *(COLORREF*)(COLOR_sysPal +
671 ((X11DRV_PALETTE_XPixelToPalette)?X11DRV_PALETTE_XPixelToPalette[pixel]:pixel)) ) & 0x00ffffff;
673 color.pixel = pixel;
674 TSXQueryColor(display, X11DRV_PALETTE_PaletteXColormap, &color);
675 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
678 /***********************************************************************
679 * X11DRV_PALETTE_ToPhysical
681 * Return the physical color closest to 'color'.
683 int X11DRV_PALETTE_ToPhysical( DC *dc, COLORREF color )
685 WORD index = 0;
686 HPALETTE16 hPal = (dc)? dc->hPalette: GetStockObject(DEFAULT_PALETTE);
687 unsigned char spec_type = color >> 24;
688 PALETTEOBJ* palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
690 /* palPtr can be NULL when DC is being destroyed */
691 if( !palPtr ) return 0;
693 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
695 /* there is no colormap limitation; we are going to have to compute
696 * the pixel value from the visual information stored earlier
699 unsigned long red, green, blue;
700 unsigned idx = 0;
702 switch(spec_type)
704 case 1: /* PALETTEINDEX */
706 if( (idx = color & 0xffff) >= palPtr->logpalette.palNumEntries)
708 WARN("RGB(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
709 GDI_ReleaseObj( hPal );
710 return 0;
713 if( palPtr->mapping )
715 int ret = palPtr->mapping[idx];
716 GDI_ReleaseObj( hPal );
717 return ret;
719 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
720 break;
722 default:
723 color &= 0xffffff;
724 /* fall through to RGB */
726 case 0: /* RGB */
727 if( dc && (dc->bitsPerPixel == 1) )
729 GDI_ReleaseObj( hPal );
730 return (((color >> 16) & 0xff) +
731 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
736 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
738 if (X11DRV_PALETTE_Graymax)
740 /* grayscale only; return scaled value */
741 GDI_ReleaseObj( hPal );
742 return ( (red * 30 + green * 69 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
744 else
746 /* scale each individually and construct the TrueColor pixel value */
747 if (X11DRV_PALETTE_Redmax != 255)
748 red = MulDiv(red, X11DRV_PALETTE_Redmax, 255);
749 if (X11DRV_PALETTE_Greenmax != 255)
750 green = MulDiv(green, X11DRV_PALETTE_Greenmax, 255);
751 if (X11DRV_PALETTE_Bluemax != 255)
752 blue = MulDiv(blue, X11DRV_PALETTE_Bluemax, 255);
754 GDI_ReleaseObj( hPal );
755 return (red << X11DRV_PALETTE_Redshift) | (green << X11DRV_PALETTE_Greenshift) | (blue << X11DRV_PALETTE_Blueshift);
758 else
761 if( !palPtr->mapping )
762 WARN("Palette %04x is not realized\n", dc->hPalette);
764 switch(spec_type) /* we have to peruse DC and system palette */
766 default:
767 color &= 0xffffff;
768 /* fall through to RGB */
770 case 0: /* RGB */
771 if( dc && (dc->bitsPerPixel == 1) )
773 GDI_ReleaseObj( hPal );
774 return (((color >> 16) & 0xff) +
775 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
778 index = COLOR_PaletteLookupPixel( COLOR_sysPal, 256,
779 X11DRV_PALETTE_PaletteToXPixel, color, FALSE);
781 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
783 break;
784 case 1: /* PALETTEINDEX */
785 index = color & 0xffff;
787 if( index >= palPtr->logpalette.palNumEntries )
788 WARN("RGB(%lx) : index %i is out of bounds\n", color, index);
789 else if( palPtr->mapping ) index = palPtr->mapping[index];
791 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
793 break;
794 case 2: /* PALETTERGB */
795 index = COLOR_PaletteLookupPixel( palPtr->logpalette.palPalEntry,
796 palPtr->logpalette.palNumEntries,
797 palPtr->mapping, color, FALSE);
798 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
800 break;
804 GDI_ReleaseObj( hPal );
805 return index;
808 /***********************************************************************
809 * X11DRV_PALETTE_LookupSystemXPixel
811 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
813 int i, best = 0, diff = 0x7fffffff;
814 int size = X11DRV_DevCaps.sizePalette;
815 int r,g,b;
817 for( i = 0; i < size && diff ; i++ )
819 if( i == NB_RESERVED_COLORS/2 )
821 int newi = size - NB_RESERVED_COLORS/2;
822 if (newi>i) i=newi;
825 r = COLOR_sysPal[i].peRed - GetRValue(col);
826 g = COLOR_sysPal[i].peGreen - GetGValue(col);
827 b = COLOR_sysPal[i].peBlue - GetBValue(col);
829 r = r*r + g*g + b*b;
831 if( r < diff ) { best = i; diff = r; }
834 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
837 /***********************************************************************
838 * X11DRV_PALETTE_FormatSystemPalette
840 static void X11DRV_PALETTE_FormatSystemPalette(void)
842 /* Build free list so we'd have an easy way to find
843 * out if there are any available colorcells.
846 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
848 COLOR_sysPal[j].peFlags = 0;
849 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
850 if( i < COLOR_gapStart || i > COLOR_gapEnd )
852 COLOR_sysPal[i].peFlags = 0; /* unused tag */
853 X11DRV_PALETTE_freeList[j] = i; /* next */
854 j = i;
856 X11DRV_PALETTE_freeList[j] = 0;
859 /***********************************************************************
860 * X11DRV_PALETTE_CheckSysColor
862 static BOOL X11DRV_PALETTE_CheckSysColor(COLORREF c)
864 int i;
865 for( i = 0; i < NB_RESERVED_COLORS; i++ )
866 if( c == (*(COLORREF*)(COLOR_sysPalTemplate + i) & 0x00ffffff) )
867 return 0;
868 return 1;
871 /***********************************************************************
872 * X11DRV_PALETTE_SetMapping
874 * Set the color-mapping table for selected palette.
875 * Return number of entries which mapping has changed.
877 int X11DRV_PALETTE_SetMapping( PALETTEOBJ* palPtr, UINT uStart, UINT uNum, BOOL mapOnly )
879 char flag;
880 int prevMapping = (palPtr->mapping) ? 1 : 0;
881 int index, iRemapped = 0;
882 int* mapping;
884 /* reset dynamic system palette entries */
886 if( !mapOnly && X11DRV_PALETTE_firstFree != -1)
887 X11DRV_PALETTE_FormatSystemPalette();
889 /* initialize palette mapping table */
891 mapping = HeapReAlloc( GetProcessHeap(), 0, palPtr->mapping,
892 sizeof(int)*palPtr->logpalette.palNumEntries);
893 if(mapping == NULL) {
894 ERR("Can not allocate new mapping -- memory exausted!");
895 return 0;
897 palPtr->mapping = mapping;
899 for( uNum += uStart; uStart < uNum; uStart++ )
901 index = -1;
902 flag = PC_SYS_USED;
904 switch( palPtr->logpalette.palPalEntry[uStart].peFlags & 0x07 )
906 case PC_EXPLICIT: /* palette entries are indices into system palette */
907 index = *(WORD*)(palPtr->logpalette.palPalEntry + uStart);
908 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
910 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
911 index = 0;
913 break;
915 case PC_RESERVED: /* forbid future mappings to this entry */
916 flag |= PC_SYS_RESERVED;
918 /* fall through */
919 default: /* try to collapse identical colors */
920 index = COLOR_PaletteLookupExactIndex(COLOR_sysPal, 256,
921 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
922 /* fall through */
923 case PC_NOCOLLAPSE:
924 if( index < 0 )
926 if( X11DRV_PALETTE_firstFree > 0 && !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
928 XColor color;
929 index = X11DRV_PALETTE_firstFree; /* ought to be available */
930 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
932 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
933 color.red = palPtr->logpalette.palPalEntry[uStart].peRed << 8;
934 color.green = palPtr->logpalette.palPalEntry[uStart].peGreen << 8;
935 color.blue = palPtr->logpalette.palPalEntry[uStart].peBlue << 8;
936 color.flags = DoRed | DoGreen | DoBlue;
937 TSXStoreColor(display, X11DRV_PALETTE_PaletteXColormap, &color);
939 COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[uStart];
940 COLOR_sysPal[index].peFlags = flag;
941 X11DRV_PALETTE_freeList[index] = 0;
943 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
944 break;
946 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
948 index = X11DRV_PALETTE_ToPhysical( NULL, 0x00ffffff &
949 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
950 break;
953 /* we have to map to existing entry in the system palette */
955 index = COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL,
956 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), TRUE);
958 palPtr->logpalette.palPalEntry[uStart].peFlags |= PC_SYS_USED;
960 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
961 break;
964 if( !prevMapping || palPtr->mapping[uStart] != index ) iRemapped++;
965 palPtr->mapping[uStart] = index;
967 TRACE("entry %i (%lx) -> pixel %i\n", uStart,
968 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), index);
971 return iRemapped;
974 /***********************************************************************
975 * X11DRV_PALETTE_UpdateMapping
977 * Update the color-mapping table for selected palette.
978 * Return number of entries which mapping has changed.
980 int X11DRV_PALETTE_UpdateMapping(PALETTEOBJ *palPtr)
982 int i, index, realized = 0;
984 if (!X11DRV_DevCaps.sizePalette)
985 return 0;
987 for( i = 0; i < 20; i++ )
989 index = X11DRV_PALETTE_LookupSystemXPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
991 /* mapping is allocated in COLOR_InitPalette() */
993 if( index != palPtr->mapping[i] ) { palPtr->mapping[i]=index; realized++; }
996 return realized;
999 /**************************************************************************
1000 * X11DRV_PALETTE_IsDark
1002 BOOL X11DRV_PALETTE_IsDark(int pixel)
1004 COLORREF col = X11DRV_PALETTE_ToLogical(pixel);
1005 return (GetRValue(col) + GetGValue(col) + GetBValue(col)) <= 0x180;