2 * X11DRV OEM bitmap objects
4 * Copyright 1994, 1995 Alexandre Julliard
16 #include "debugtools.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
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
;
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 /***********************************************************************
94 * Initialize color management.
96 BOOL
X11DRV_PALETTE_Init(void)
98 int mask
, white
, black
;
101 Visual
*visual
= X11DRV_GetVisual();
103 TRACE("initializing palette manager...\n");
105 white
= WhitePixelOfScreen( X11DRV_GetXScreen() );
106 black
= BlackPixelOfScreen( X11DRV_GetXScreen() );
108 for( mask
= 1; !((white
& mask
)^(black
& mask
)); mask
<<= 1 )
110 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
111 X11DRV_DevCaps
.sizePalette
= visual
->map_entries
;
113 switch(visual
->class)
116 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
119 if (PROFILE_GetWineIniBool( "x11drv", "PrivateColorMap", 0 ))
121 XSetWindowAttributes win_attr
;
123 X11DRV_PALETTE_PaletteXColormap
= TSXCreateColormap( display
, X11DRV_GetXRootWindow(),
125 if (X11DRV_PALETTE_PaletteXColormap
)
127 X11DRV_PALETTE_PaletteFlags
|= (X11DRV_PALETTE_PRIVATE
| X11DRV_PALETTE_WHITESET
);
130 for( white
= X11DRV_DevCaps
.sizePalette
- 1; !(white
& 1); white
>>= 1 )
133 if( X11DRV_GetXRootWindow() != DefaultRootWindow(display
) )
135 win_attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
;
136 TSXChangeWindowAttributes( display
, X11DRV_GetXRootWindow(),
137 CWColormap
, &win_attr
);
142 X11DRV_PALETTE_PaletteXColormap
= TSXCreateColormap(display
, X11DRV_GetXRootWindow(), visual
, AllocNone
);
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;
152 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
154 int *depths
,nrofdepths
;
155 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
158 depths
=TSXListDepths(display
,DefaultScreen(display
),&nrofdepths
);
159 if ((nrofdepths
==2) && ((depths
[0]==4) || depths
[1]==4)) {
161 for( white
= X11DRV_DevCaps
.sizePalette
- 1; !(white
& 1); white
>>= 1 )
163 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
164 X11DRV_PALETTE_PaletteXColormap
= TSXCreateColormap(display
, X11DRV_GetXRootWindow(), visual
, AllocNone
);
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
);
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();
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;
198 X11DRV_DevCaps
.rasterCaps
|= RC_PALETTE
;
199 X11DRV_DevCaps
.sizePalette
= visual
->map_entries
;
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
),
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
)
234 for(i
=0;!(maskbits
&1);i
++)
241 /***********************************************************************
242 * X11DRV_PALETTE_BuildPrivateMap
244 * Allocate colorcells and initialize mapping tables.
246 static BOOL
X11DRV_PALETTE_BuildPrivateMap(void)
248 /* Private colormap - identity mapping */
253 if((COLOR_sysPal
= (PALETTEENTRY
*)HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY
)*X11DRV_DevCaps
.sizePalette
)) == NULL
) {
254 WARN("Can not allocate system palette\n");
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
;
282 TSXStoreColor(display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
284 /* Set EGA mapping if color is from the first or last eight */
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;
301 /***********************************************************************
302 * X11DRV_PALETTE_BuildSharedMap
304 * Allocate colorcells and initialize mapping tables.
306 static BOOL
X11DRV_PALETTE_BuildSharedMap(void)
309 unsigned long sysPixel
[NB_RESERVED_COLORS
];
310 unsigned long* pixDynMapping
= NULL
;
311 unsigned long plane_masks
[1];
313 int diff
, r
, g
, b
, max
= 256, bp
= 0, wp
= 1;
315 int defaultCM_max_copy
;
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
))
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());
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;
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 */
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");
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
) )
432 TSXFreeColors(display
, X11DRV_PALETTE_PaletteXColormap
, pixDynMapping
, c_val
, 0);
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 */
443 if( !TSXAllocColorCells(display
, X11DRV_PALETTE_PaletteXColormap
, False
,
444 plane_masks
, 0, pixDynMapping
, c_min
) )
446 WARN("Inexplicable failure during colorcell allocation.\n");
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; }
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");
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");
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");
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 */
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
++];
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
);
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.
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
)
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
;
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
)
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
;
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
) )
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
)
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 )
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
)
649 /* truecolor visual */
651 if (X11DRV_GetDepth() >= 24) return pixel
;
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;
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
)
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
;
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
);
713 if( palPtr
->mapping
)
715 int ret
= palPtr
->mapping
[idx
];
716 GDI_ReleaseObj( hPal
);
719 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
724 /* fall through to 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;
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
);
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 */
768 /* fall through to 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);
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);
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);
804 GDI_ReleaseObj( hPal
);
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
;
817 for( i
= 0; i
< size
&& diff
; i
++ )
819 if( i
== NB_RESERVED_COLORS
/2 )
821 int newi
= size
- NB_RESERVED_COLORS
/2;
825 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
826 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
827 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
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 */
856 X11DRV_PALETTE_freeList
[j
] = 0;
859 /***********************************************************************
860 * X11DRV_PALETTE_CheckSysColor
862 static BOOL
X11DRV_PALETTE_CheckSysColor(COLORREF c
)
865 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
866 if( c
== (*(COLORREF
*)(COLOR_sysPalTemplate
+ i
) & 0x00ffffff) )
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
)
880 int prevMapping
= (palPtr
->mapping
) ? 1 : 0;
881 int index
, iRemapped
= 0;
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!");
897 palPtr
->mapping
= mapping
;
899 for( uNum
+= uStart
; uStart
< uNum
; uStart
++ )
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
);
915 case PC_RESERVED
: /* forbid future mappings to this entry */
916 flag
|= PC_SYS_RESERVED
;
919 default: /* try to collapse identical colors */
920 index
= COLOR_PaletteLookupExactIndex(COLOR_sysPal
, 256,
921 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
));
926 if( X11DRV_PALETTE_firstFree
> 0 && !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
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
];
946 else if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
948 index
= X11DRV_PALETTE_ToPhysical( NULL
, 0x00ffffff &
949 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
));
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
];
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
);
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
)
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
++; }
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;