2 * X11DRV OEM bitmap objects
4 * Copyright 1994, 1995 Alexandre Julliard
10 #ifndef X_DISPLAY_MISSING
18 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(palette
);
28 /* Palette indexed mode:
29 * logical palette -> mapping -> pixel
32 * Windows needs contiguous color space ( from 0 to n ) but
33 * it is possible only with the private colormap. Otherwise we
34 * have to map DC palette indices to real pixel values. With
35 * private colormaps it boils down to the identity mapping. The
36 * other special case is when we have a fixed color visual with
37 * the screendepth > 8 - we abandon palette mappings altogether
38 * because pixel values can be calculated without X server
41 * Windows palette manager is described in the
42 * http://premium.microsoft.com/msdn/library/techart/f30/f34/f40/d4d/sa942.htm
45 extern PALETTEENTRY
*COLOR_sysPal
;
46 extern int COLOR_gapStart
;
47 extern int COLOR_gapEnd
;
48 extern int COLOR_gapFilled
;
51 extern const PALETTEENTRY COLOR_sysPalTemplate
[NB_RESERVED_COLORS
];
53 Colormap X11DRV_PALETTE_PaletteXColormap
= 0;
54 UINT16 X11DRV_PALETTE_PaletteFlags
= 0;
56 static int X11DRV_PALETTE_Redshift
= 0; /* to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
57 static int X11DRV_PALETTE_Redmax
= 0;
58 static int X11DRV_PALETTE_Greenshift
= 0;
59 static int X11DRV_PALETTE_Greenmax
= 0;
60 static int X11DRV_PALETTE_Blueshift
= 0;
61 static int X11DRV_PALETTE_Bluemax
= 0;
62 static int X11DRV_PALETTE_Graymax
= 0;
64 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
65 static int X11DRV_PALETTE_firstFree
= 0;
66 static unsigned char X11DRV_PALETTE_freeList
[256];
68 /**********************************************************************/
70 /* Map an EGA index (0..15) to a pixel value in the system color space. */
72 int X11DRV_PALETTE_mapEGAPixel
[16];
74 /**********************************************************************/
76 #define NB_COLORCUBE_START_INDEX 63
78 /* Maps entry in the system palette to X pixel value */
79 int *X11DRV_PALETTE_PaletteToXPixel
= NULL
;
81 /* Maps pixel to the entry in the system palette */
82 int *X11DRV_PALETTE_XPixelToPalette
= NULL
;
84 /**********************************************************************/
86 static BOOL
X11DRV_PALETTE_BuildPrivateMap(void);
87 static BOOL
X11DRV_PALETTE_BuildSharedMap(void);
88 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits
, int *shift
, int *max
);
89 static void X11DRV_PALETTE_FillDefaultColors(void);
90 static void X11DRV_PALETTE_FormatSystemPalette(void);
91 static BOOL
X11DRV_PALETTE_CheckSysColor(COLORREF c
);
92 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
);
94 /***********************************************************************
97 * Initialize color management.
99 BOOL
X11DRV_PALETTE_Init(void)
101 int mask
, white
, black
;
104 Visual
*visual
= X11DRV_GetVisual();
106 TRACE("initializing palette manager...\n");
108 white
= WhitePixelOfScreen( X11DRV_GetXScreen() );
109 black
= BlackPixelOfScreen( X11DRV_GetXScreen() );
111 for( mask
= 1; !((white
& mask
)^(black
& mask
)); mask
<<= 1 )
113 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
114 X11DRV_DevCaps
.sizePalette
= visual
->map_entries
;
116 switch(visual
->class)
119 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
122 if (PROFILE_GetWineIniBool( "x11drv", "PrivateColorMap", 0 ))
124 XSetWindowAttributes win_attr
;
126 X11DRV_PALETTE_PaletteXColormap
= TSXCreateColormap( display
, X11DRV_GetXRootWindow(),
128 if (X11DRV_PALETTE_PaletteXColormap
)
130 X11DRV_PALETTE_PaletteFlags
|= (X11DRV_PALETTE_PRIVATE
| X11DRV_PALETTE_WHITESET
);
133 for( white
= X11DRV_DevCaps
.sizePalette
- 1; !(white
& 1); white
>>= 1 )
136 if( X11DRV_GetXRootWindow() != DefaultRootWindow(display
) )
138 win_attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
;
139 TSXChangeWindowAttributes( display
, X11DRV_GetXRootWindow(),
140 CWColormap
, &win_attr
);
145 X11DRV_PALETTE_PaletteXColormap
= DefaultColormapOfScreen( X11DRV_GetXScreen() );
149 X11DRV_PALETTE_PaletteXColormap
= DefaultColormapOfScreen( X11DRV_GetXScreen() );
150 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
151 X11DRV_PALETTE_Graymax
= (1 << X11DRV_GetDepth())-1;
155 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
157 int *depths
,nrofdepths
;
158 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
161 depths
=TSXListDepths(display
,DefaultScreen(display
),&nrofdepths
);
162 if ((nrofdepths
==2) && ((depths
[0]==4) || depths
[1]==4)) {
164 for( white
= X11DRV_DevCaps
.sizePalette
- 1; !(white
& 1); white
>>= 1 )
166 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
167 X11DRV_PALETTE_PaletteXColormap
= DefaultColormapOfScreen( X11DRV_GetXScreen() );
172 X11DRV_PALETTE_PaletteXColormap
= DefaultColormapOfScreen( X11DRV_GetXScreen() );
173 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
174 X11DRV_PALETTE_ComputeShifts(visual
->red_mask
, &X11DRV_PALETTE_Redshift
, &X11DRV_PALETTE_Redmax
);
175 X11DRV_PALETTE_ComputeShifts(visual
->green_mask
, &X11DRV_PALETTE_Greenshift
, &X11DRV_PALETTE_Greenmax
);
176 X11DRV_PALETTE_ComputeShifts(visual
->blue_mask
, &X11DRV_PALETTE_Blueshift
, &X11DRV_PALETTE_Bluemax
);
181 TRACE(" visual class %i (%i)\n", visual
->class, monoPlane
);
183 memset(X11DRV_PALETTE_freeList
, 0, 256*sizeof(unsigned char));
185 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
186 X11DRV_PALETTE_BuildPrivateMap();
188 X11DRV_PALETTE_BuildSharedMap();
190 /* Build free list */
192 if( X11DRV_PALETTE_firstFree
!= -1 )
193 X11DRV_PALETTE_FormatSystemPalette();
195 X11DRV_PALETTE_FillDefaultColors();
197 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
198 X11DRV_DevCaps
.sizePalette
= 0;
201 X11DRV_DevCaps
.rasterCaps
|= RC_PALETTE
;
202 X11DRV_DevCaps
.sizePalette
= visual
->map_entries
;
208 /***********************************************************************
209 * X11DRV_PALETTE_Cleanup
211 * Free external colors we grabbed in the FillDefaultPalette()
213 void X11DRV_PALETTE_Cleanup(void)
215 if( COLOR_gapFilled
)
216 TSXFreeColors(display
, X11DRV_PALETTE_PaletteXColormap
,
217 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel
+ COLOR_gapStart
),
221 /***********************************************************************
222 * X11DRV_PALETTE_ComputeShifts
224 * Calculate conversion parameters for direct mapped visuals
226 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits
, int *shift
, int *max
)
237 for(i
=0;!(maskbits
&1);i
++)
244 /***********************************************************************
245 * X11DRV_PALETTE_BuildPrivateMap
247 * Allocate colorcells and initialize mapping tables.
249 static BOOL
X11DRV_PALETTE_BuildPrivateMap(void)
251 /* Private colormap - identity mapping */
256 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*X11DRV_DevCaps
.sizePalette
);
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;
316 /* read "AllocSystemColors" from wine.conf */
318 COLOR_max
= PROFILE_GetWineIniInt( "x11drv", "AllocSystemColors", 256);
319 if (COLOR_max
> 256) COLOR_max
= 256;
320 else if (COLOR_max
< 20) COLOR_max
= 20;
321 TRACE("%d colors configured.\n", COLOR_max
);
323 TRACE("Building shared map - %i palette entries\n", X11DRV_DevCaps
.sizePalette
);
325 /* Be nice and allocate system colors as read-only */
327 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
329 color
.red
= COLOR_sysPalTemplate
[i
].peRed
* 65535 / 255;
330 color
.green
= COLOR_sysPalTemplate
[i
].peGreen
* 65535 / 255;
331 color
.blue
= COLOR_sysPalTemplate
[i
].peBlue
* 65535 / 255;
332 color
.flags
= DoRed
| DoGreen
| DoBlue
;
334 if (!TSXAllocColor( display
, X11DRV_PALETTE_PaletteXColormap
, &color
))
340 WARN("Not enough colors for the full system palette.\n");
342 bp
= BlackPixel(display
, DefaultScreen(display
));
343 wp
= WhitePixel(display
, DefaultScreen(display
));
345 max
= (0xffffffff)>>(32 - X11DRV_GetDepth());
353 /* reinit color (XAllocColor() may change it)
354 * and map to the best shared colorcell */
356 color
.red
= COLOR_sysPalTemplate
[i
].peRed
* 65535 / 255;
357 color
.green
= COLOR_sysPalTemplate
[i
].peGreen
* 65535 / 255;
358 color
.blue
= COLOR_sysPalTemplate
[i
].peBlue
* 65535 / 255;
360 best
.pixel
= best
.red
= best
.green
= best
.blue
= 0;
361 for( c
.pixel
= 0, diff
= 0x7fffffff; c
.pixel
< max
; c
.pixel
+= step
)
363 TSXQueryColor(display
, X11DRV_PALETTE_PaletteXColormap
, &c
);
364 r
= (c
.red
- color
.red
)>>8;
365 g
= (c
.green
- color
.green
)>>8;
366 b
= (c
.blue
- color
.blue
)>>8;
368 if( r
< diff
) { best
= c
; diff
= r
; }
371 if( TSXAllocColor(display
, X11DRV_PALETTE_PaletteXColormap
, &best
) )
372 color
.pixel
= best
.pixel
;
373 else color
.pixel
= (i
< NB_RESERVED_COLORS
/2)? bp
: wp
;
376 sysPixel
[i
] = color
.pixel
;
378 TRACE("syscolor(%lx) -> pixel %i\n",
379 *(COLORREF
*)(COLOR_sysPalTemplate
+i
), (int)color
.pixel
);
381 /* Set EGA mapping if color in the first or last eight */
384 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
385 else if (i
>= NB_RESERVED_COLORS
- 8 )
386 X11DRV_PALETTE_mapEGAPixel
[i
- (NB_RESERVED_COLORS
-16)] = color
.pixel
;
389 /* now allocate changeable set */
391 if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
393 int c_min
= 0, c_max
= X11DRV_DevCaps
.sizePalette
, c_val
;
395 TRACE("Dynamic colormap... \n");
397 /* comment this out if you want to debug palette init */
399 TSXGrabServer(display
);
401 /* let's become the first client that actually follows
402 * X guidelines and does binary search...
405 pixDynMapping
= (unsigned long*)xmalloc(sizeof(long)*X11DRV_DevCaps
.sizePalette
);
406 while( c_max
- c_min
> 0 )
408 c_val
= (c_max
+ c_min
)/2 + (c_max
+ c_min
)%2;
410 if( !TSXAllocColorCells(display
, X11DRV_PALETTE_PaletteXColormap
, False
,
411 plane_masks
, 0, pixDynMapping
, c_val
) )
415 TSXFreeColors(display
, X11DRV_PALETTE_PaletteXColormap
, pixDynMapping
, c_val
, 0);
420 if( c_min
> COLOR_max
- NB_RESERVED_COLORS
)
421 c_min
= COLOR_max
- NB_RESERVED_COLORS
;
423 c_min
= (c_min
/2) + (c_min
/2); /* need even set for split palette */
426 if( !TSXAllocColorCells(display
, X11DRV_PALETTE_PaletteXColormap
, False
,
427 plane_masks
, 0, pixDynMapping
, c_min
) )
429 WARN("Inexplicable failure during colorcell allocation.\n");
433 X11DRV_DevCaps
.sizePalette
= c_min
+ NB_RESERVED_COLORS
;
435 TSXUngrabServer(display
);
437 TRACE("adjusted size %i colorcells\n", X11DRV_DevCaps
.sizePalette
);
439 else if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
441 /* virtual colorspace - ToPhysical takes care of
442 * color translations but we have to allocate full palette
443 * to maintain compatibility
445 X11DRV_DevCaps
.sizePalette
= 256;
446 TRACE("Virtual colorspace - screendepth %i\n", X11DRV_GetDepth());
448 else X11DRV_DevCaps
.sizePalette
= NB_RESERVED_COLORS
; /* system palette only - however we can alloc a bunch
449 * of colors and map to them */
451 TRACE("Shared system palette uses %i colors.\n", X11DRV_DevCaps
.sizePalette
);
453 /* set gap to account for pixel shortage. It has to be right in the center
454 * of the system palette because otherwise raster ops get screwed. */
456 if( X11DRV_DevCaps
.sizePalette
>= 256 )
457 { COLOR_gapStart
= 256; COLOR_gapEnd
= -1; }
459 { COLOR_gapStart
= X11DRV_DevCaps
.sizePalette
/2; COLOR_gapEnd
= 255 - X11DRV_DevCaps
.sizePalette
/2; }
461 X11DRV_PALETTE_firstFree
= ( X11DRV_DevCaps
.sizePalette
> NB_RESERVED_COLORS
&&
462 (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
|| !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)) )
463 ? NB_RESERVED_COLORS
/2 : -1;
465 COLOR_sysPal
= (PALETTEENTRY
*)malloc(sizeof(PALETTEENTRY
)*256);
466 if(COLOR_sysPal
== NULL
) {
467 ERR("Can not allocate system palette!\n");
471 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
473 if (X11DRV_GetDepth() <= 8)
475 X11DRV_PALETTE_XPixelToPalette
= (int*)calloc(256, sizeof(int));
476 if(X11DRV_PALETTE_XPixelToPalette
== NULL
) {
477 ERR("Out of memory: XPixelToPalette!\n");
482 /* for hicolor visuals PaletteToPixel mapping is used to skip
483 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
486 X11DRV_PALETTE_PaletteToXPixel
= (int*)malloc(sizeof(int)*256);
487 if(X11DRV_PALETTE_PaletteToXPixel
== NULL
) {
488 ERR("Out of memory: PaletteToXPixel!\n");
492 for( i
= j
= 0; i
< 256; i
++ )
494 if( i
>= COLOR_gapStart
&& i
<= COLOR_gapEnd
)
496 X11DRV_PALETTE_PaletteToXPixel
[i
] = 0;
497 COLOR_sysPal
[i
].peFlags
= 0; /* mark as unused */
501 if( i
< NB_RESERVED_COLORS
/2 )
503 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[i
];
504 COLOR_sysPal
[i
] = COLOR_sysPalTemplate
[i
];
506 else if( i
>= 256 - NB_RESERVED_COLORS
/2 )
508 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[(i
+ NB_RESERVED_COLORS
) - 256];
509 COLOR_sysPal
[i
] = COLOR_sysPalTemplate
[(i
+ NB_RESERVED_COLORS
) - 256];
511 else if( pixDynMapping
)
512 X11DRV_PALETTE_PaletteToXPixel
[i
] = pixDynMapping
[j
++];
514 X11DRV_PALETTE_PaletteToXPixel
[i
] = i
;
516 TRACE("index %i -> pixel %i\n", i
, X11DRV_PALETTE_PaletteToXPixel
[i
]);
518 if( X11DRV_PALETTE_XPixelToPalette
)
519 X11DRV_PALETTE_XPixelToPalette
[X11DRV_PALETTE_PaletteToXPixel
[i
]] = i
;
522 if( pixDynMapping
) free(pixDynMapping
);
527 /***********************************************************************
528 * Colormap Initialization
530 static void X11DRV_PALETTE_FillDefaultColors(void)
532 /* initialize unused entries to what Windows uses as a color
533 * cube - based on Greg Kreider's code.
537 int red
, no_r
, inc_r
;
538 int green
, no_g
, inc_g
;
539 int blue
, no_b
, inc_b
;
541 if (X11DRV_DevCaps
.sizePalette
<= NB_RESERVED_COLORS
)
543 while (i
*i
*i
< (X11DRV_DevCaps
.sizePalette
- NB_RESERVED_COLORS
)) i
++;
544 no_r
= no_g
= no_b
= --i
;
545 if ((no_r
* (no_g
+1) * no_b
) < (X11DRV_DevCaps
.sizePalette
- NB_RESERVED_COLORS
)) no_g
++;
546 if ((no_r
* no_g
* (no_b
+1)) < (X11DRV_DevCaps
.sizePalette
- NB_RESERVED_COLORS
)) no_b
++;
547 inc_r
= (255 - NB_COLORCUBE_START_INDEX
)/no_r
;
548 inc_g
= (255 - NB_COLORCUBE_START_INDEX
)/no_g
;
549 inc_b
= (255 - NB_COLORCUBE_START_INDEX
)/no_b
;
551 idx
= X11DRV_PALETTE_firstFree
;
554 for (blue
= NB_COLORCUBE_START_INDEX
; blue
< 256 && idx
; blue
+= inc_b
)
555 for (green
= NB_COLORCUBE_START_INDEX
; green
< 256 && idx
; green
+= inc_g
)
556 for (red
= NB_COLORCUBE_START_INDEX
; red
< 256 && idx
; red
+= inc_r
)
560 if( red
== NB_COLORCUBE_START_INDEX
&& green
== red
&& blue
== green
) continue;
562 COLOR_sysPal
[idx
].peRed
= red
;
563 COLOR_sysPal
[idx
].peGreen
= green
;
564 COLOR_sysPal
[idx
].peBlue
= blue
;
568 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
570 if (X11DRV_PALETTE_Redmax
!= 255) no_r
= (red
* X11DRV_PALETTE_Redmax
) / 255;
571 if (X11DRV_PALETTE_Greenmax
!= 255) no_g
= (green
* X11DRV_PALETTE_Greenmax
) / 255;
572 if (X11DRV_PALETTE_Bluemax
!= 255) no_b
= (blue
* X11DRV_PALETTE_Bluemax
) / 255;
574 X11DRV_PALETTE_PaletteToXPixel
[idx
] = (no_r
<< X11DRV_PALETTE_Redshift
) | (no_g
<< X11DRV_PALETTE_Greenshift
) | (no_b
<< X11DRV_PALETTE_Blueshift
);
576 else if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
579 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[idx
] : idx
;
580 color
.red
= COLOR_sysPal
[idx
].peRed
<< 8;
581 color
.green
= COLOR_sysPal
[idx
].peGreen
<< 8;
582 color
.blue
= COLOR_sysPal
[idx
].peBlue
<< 8;
583 color
.flags
= DoRed
| DoGreen
| DoBlue
;
584 TSXStoreColor(display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
586 idx
= X11DRV_PALETTE_freeList
[idx
];
589 /* try to fill some entries in the "gap" with
590 * what's already in the colormap - they will be
591 * mappable to but not changeable. */
593 if( COLOR_gapStart
< COLOR_gapEnd
&& X11DRV_PALETTE_XPixelToPalette
)
598 max
= COLOR_max
- (256 - (COLOR_gapEnd
- COLOR_gapStart
));
599 for ( i
= 0, idx
= COLOR_gapStart
; i
< 256 && idx
<= COLOR_gapEnd
; i
++ )
600 if( X11DRV_PALETTE_XPixelToPalette
[i
] == 0 )
604 TSXQueryColor(display
, X11DRV_PALETTE_PaletteXColormap
, &xc
);
605 r
= xc
.red
>>8; g
= xc
.green
>>8; b
= xc
.blue
>>8;
607 if( xc
.pixel
< 256 && X11DRV_PALETTE_CheckSysColor(RGB(r
, g
, b
)) &&
608 TSXAllocColor(display
, X11DRV_PALETTE_PaletteXColormap
, &xc
) )
610 X11DRV_PALETTE_XPixelToPalette
[xc
.pixel
] = idx
;
611 X11DRV_PALETTE_PaletteToXPixel
[idx
] = xc
.pixel
;
612 *(COLORREF
*)(COLOR_sysPal
+ idx
) = RGB(r
, g
, b
);
613 COLOR_sysPal
[idx
++].peFlags
|= PC_SYS_USED
;
614 if( --max
<= 0 ) break;
617 COLOR_gapFilled
= idx
- COLOR_gapStart
;
622 /***********************************************************************
623 * X11DRV_PALETTE_ToLogical
625 * Return RGB color for given X pixel.
627 COLORREF
X11DRV_PALETTE_ToLogical(int pixel
)
632 /* truecolor visual */
634 if (X11DRV_GetDepth() >= 24) return pixel
;
637 /* check for hicolor visuals first */
639 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
&& !X11DRV_PALETTE_Graymax
)
641 color
.red
= (pixel
>> X11DRV_PALETTE_Redshift
) & X11DRV_PALETTE_Redmax
;
642 color
.green
= (pixel
>> X11DRV_PALETTE_Greenshift
) & X11DRV_PALETTE_Greenmax
;
643 color
.blue
= (pixel
>> X11DRV_PALETTE_Blueshift
) & X11DRV_PALETTE_Bluemax
;
644 return RGB(MulDiv(color
.red
, 255, X11DRV_PALETTE_Redmax
),
645 MulDiv(color
.green
, 255, X11DRV_PALETTE_Greenmax
),
646 MulDiv(color
.blue
, 255, X11DRV_PALETTE_Bluemax
));
649 /* check if we can bypass X */
651 if ((X11DRV_GetDepth() <= 8) && (pixel
< 256) &&
652 !(X11DRV_PALETTE_PaletteFlags
& (X11DRV_PALETTE_VIRTUAL
| X11DRV_PALETTE_FIXED
)) )
653 return ( *(COLORREF
*)(COLOR_sysPal
+
654 ((X11DRV_PALETTE_XPixelToPalette
)?X11DRV_PALETTE_XPixelToPalette
[pixel
]:pixel
)) ) & 0x00ffffff;
657 TSXQueryColor(display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
658 return RGB(color
.red
>> 8, color
.green
>> 8, color
.blue
>> 8);
661 /***********************************************************************
662 * X11DRV_PALETTE_ToPhysical
664 * Return the physical color closest to 'color'.
666 int X11DRV_PALETTE_ToPhysical( DC
*dc
, COLORREF color
)
669 HPALETTE16 hPal
= (dc
)? dc
->w
.hPalette
: STOCK_DEFAULT_PALETTE
;
670 unsigned char spec_type
= color
>> 24;
671 PALETTEOBJ
* palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hPal
, PALETTE_MAGIC
);
673 /* palPtr can be NULL when DC is being destroyed */
674 if( !palPtr
) return 0;
676 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
678 /* there is no colormap limitation; we are going to have to compute
679 * the pixel value from the visual information stored earlier
682 unsigned long red
, green
, blue
;
687 case 1: /* PALETTEINDEX */
689 if( (idx
= color
& 0xffff) >= palPtr
->logpalette
.palNumEntries
)
691 WARN("RGB(%lx) : idx %d is out of bounds, assuming black\n", color
, idx
);
692 GDI_HEAP_UNLOCK( hPal
);
696 if( palPtr
->mapping
)
698 GDI_HEAP_UNLOCK( hPal
);
699 return palPtr
->mapping
[idx
];
701 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
706 /* fall through to RGB */
709 if( dc
&& (dc
->w
.bitsPerPixel
== 1) )
711 GDI_HEAP_UNLOCK( hPal
);
712 return (((color
>> 16) & 0xff) +
713 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
718 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
720 if (X11DRV_PALETTE_Graymax
)
722 /* grayscale only; return scaled value */
723 GDI_HEAP_UNLOCK( hPal
);
724 return ( (red
* 30 + green
* 69 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
728 /* scale each individually and construct the TrueColor pixel value */
729 if (X11DRV_PALETTE_Redmax
!= 255)
730 red
= MulDiv(red
, X11DRV_PALETTE_Redmax
, 255);
731 if (X11DRV_PALETTE_Greenmax
!= 255)
732 green
= MulDiv(green
, X11DRV_PALETTE_Greenmax
, 255);
733 if (X11DRV_PALETTE_Bluemax
!= 255)
734 blue
= MulDiv(blue
, X11DRV_PALETTE_Bluemax
, 255);
736 GDI_HEAP_UNLOCK( hPal
);
737 return (red
<< X11DRV_PALETTE_Redshift
) | (green
<< X11DRV_PALETTE_Greenshift
) | (blue
<< X11DRV_PALETTE_Blueshift
);
743 if( !palPtr
->mapping
)
744 WARN("Palette %04x is not realized\n", dc
->w
.hPalette
);
746 switch(spec_type
) /* we have to peruse DC and system palette */
750 /* fall through to RGB */
753 if( dc
&& (dc
->w
.bitsPerPixel
== 1) )
755 GDI_HEAP_UNLOCK( hPal
);
756 return (((color
>> 16) & 0xff) +
757 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
760 index
= COLOR_PaletteLookupPixel( COLOR_sysPal
, 256,
761 X11DRV_PALETTE_PaletteToXPixel
, color
, FALSE
);
763 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
766 case 1: /* PALETTEINDEX */
767 index
= color
& 0xffff;
769 if( index
>= palPtr
->logpalette
.palNumEntries
)
770 WARN("RGB(%lx) : index %i is out of bounds\n", color
, index
);
771 else if( palPtr
->mapping
) index
= palPtr
->mapping
[index
];
773 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
776 case 2: /* PALETTERGB */
777 index
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
778 palPtr
->logpalette
.palNumEntries
,
779 palPtr
->mapping
, color
, FALSE
);
780 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
786 GDI_HEAP_UNLOCK( hPal
);
790 /***********************************************************************
791 * X11DRV_PALETTE_LookupSystemXPixel
793 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
)
795 int i
, best
= 0, diff
= 0x7fffffff;
796 int size
= X11DRV_DevCaps
.sizePalette
;
799 for( i
= 0; i
< size
&& diff
; i
++ )
801 if( i
== NB_RESERVED_COLORS
/2 )
803 int newi
= size
- NB_RESERVED_COLORS
/2;
807 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
808 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
809 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
813 if( r
< diff
) { best
= i
; diff
= r
; }
816 return (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[best
] : best
;
819 /***********************************************************************
820 * X11DRV_PALETTE_FormatSystemPalette
822 static void X11DRV_PALETTE_FormatSystemPalette(void)
824 /* Build free list so we'd have an easy way to find
825 * out if there are any available colorcells.
828 int i
, j
= X11DRV_PALETTE_firstFree
= NB_RESERVED_COLORS
/2;
830 COLOR_sysPal
[j
].peFlags
= 0;
831 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
832 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
834 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
835 X11DRV_PALETTE_freeList
[j
] = i
; /* next */
838 X11DRV_PALETTE_freeList
[j
] = 0;
841 /***********************************************************************
842 * X11DRV_PALETTE_CheckSysColor
844 static BOOL
X11DRV_PALETTE_CheckSysColor(COLORREF c
)
847 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
848 if( c
== (*(COLORREF
*)(COLOR_sysPalTemplate
+ i
) & 0x00ffffff) )
853 /***********************************************************************
854 * X11DRV_PALETTE_SetMapping
856 * Set the color-mapping table for selected palette.
857 * Return number of entries which mapping has changed.
859 int X11DRV_PALETTE_SetMapping( PALETTEOBJ
* palPtr
, UINT uStart
, UINT uNum
, BOOL mapOnly
)
862 int prevMapping
= (palPtr
->mapping
) ? 1 : 0;
863 int index
, iRemapped
= 0;
866 /* reset dynamic system palette entries */
868 if( !mapOnly
&& X11DRV_PALETTE_firstFree
!= -1)
869 X11DRV_PALETTE_FormatSystemPalette();
871 /* initialize palette mapping table */
873 mapping
= (int*)realloc(palPtr
->mapping
, sizeof(int)*
874 palPtr
->logpalette
.palNumEntries
);
875 if(mapping
== NULL
) {
876 ERR("Can not allocate new mapping -- memory exausted!");
879 palPtr
->mapping
= mapping
;
881 for( uNum
+= uStart
; uStart
< uNum
; uStart
++ )
886 switch( palPtr
->logpalette
.palPalEntry
[uStart
].peFlags
& 0x07 )
888 case PC_EXPLICIT
: /* palette entries are indices into system palette */
889 index
= *(WORD
*)(palPtr
->logpalette
.palPalEntry
+ uStart
);
890 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
892 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
897 case PC_RESERVED
: /* forbid future mappings to this entry */
898 flag
|= PC_SYS_RESERVED
;
901 default: /* try to collapse identical colors */
902 index
= COLOR_PaletteLookupExactIndex(COLOR_sysPal
, 256,
903 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
));
908 if( X11DRV_PALETTE_firstFree
> 0 && !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
911 index
= X11DRV_PALETTE_firstFree
; /* ought to be available */
912 X11DRV_PALETTE_firstFree
= X11DRV_PALETTE_freeList
[index
];
914 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
) ? X11DRV_PALETTE_PaletteToXPixel
[index
] : index
;
915 color
.red
= palPtr
->logpalette
.palPalEntry
[uStart
].peRed
<< 8;
916 color
.green
= palPtr
->logpalette
.palPalEntry
[uStart
].peGreen
<< 8;
917 color
.blue
= palPtr
->logpalette
.palPalEntry
[uStart
].peBlue
<< 8;
918 color
.flags
= DoRed
| DoGreen
| DoBlue
;
919 TSXStoreColor(display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
921 COLOR_sysPal
[index
] = palPtr
->logpalette
.palPalEntry
[uStart
];
922 COLOR_sysPal
[index
].peFlags
= flag
;
923 X11DRV_PALETTE_freeList
[index
] = 0;
925 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
928 else if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
930 index
= X11DRV_PALETTE_ToPhysical( NULL
, 0x00ffffff &
931 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
));
935 /* we have to map to existing entry in the system palette */
937 index
= COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
,
938 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
), TRUE
);
940 palPtr
->logpalette
.palPalEntry
[uStart
].peFlags
|= PC_SYS_USED
;
942 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
946 if( !prevMapping
|| palPtr
->mapping
[uStart
] != index
) iRemapped
++;
947 palPtr
->mapping
[uStart
] = index
;
949 TRACE("entry %i (%lx) -> pixel %i\n", uStart
,
950 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
), index
);
956 /***********************************************************************
957 * X11DRV_PALETTE_UpdateMapping
959 * Update the color-mapping table for selected palette.
960 * Return number of entries which mapping has changed.
962 int X11DRV_PALETTE_UpdateMapping(PALETTEOBJ
*palPtr
)
964 int i
, index
, realized
= 0;
966 if (!X11DRV_DevCaps
.sizePalette
)
969 for( i
= 0; i
< 20; i
++ )
971 index
= X11DRV_PALETTE_LookupSystemXPixel(*(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
));
973 /* mapping is allocated in COLOR_InitPalette() */
975 if( index
!= palPtr
->mapping
[i
] ) { palPtr
->mapping
[i
]=index
; realized
++; }
981 /**************************************************************************
982 * X11DRV_PALETTE_IsDark
984 BOOL
X11DRV_PALETTE_IsDark(int pixel
)
986 COLORREF col
= X11DRV_PALETTE_ToLogical(pixel
);
987 return (GetRValue(col
) + GetGValue(col
) + GetBValue(col
)) <= 0x180;
990 #endif /* !defined(X_DISPLAY_MISSING) */