2 * X11DRV OEM bitmap objects
4 * Copyright 1994, 1995 Alexandre Julliard
10 #ifndef X_DISPLAY_MISSING
18 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(palette
)
29 /* Palette indexed mode:
30 * logical palette -> mapping -> pixel
33 * Windows needs contiguous color space ( from 0 to n ) but
34 * it is possible only with the private colormap. Otherwise we
35 * have to map DC palette indices to real pixel values. With
36 * private colormaps it boils down to the identity mapping. The
37 * other special case is when we have a fixed color visual with
38 * the screendepth > 8 - we abandon palette mappings altogether
39 * because pixel values can be calculated without X server
42 * Windows palette manager is described in the
43 * http://premium.microsoft.com/msdn/library/techart/f30/f34/f40/d4d/sa942.htm
46 extern PALETTEENTRY
*COLOR_sysPal
;
47 extern int COLOR_gapStart
;
48 extern int COLOR_gapEnd
;
49 extern int COLOR_gapFilled
;
52 extern const PALETTEENTRY COLOR_sysPalTemplate
[NB_RESERVED_COLORS
];
54 Colormap X11DRV_PALETTE_PaletteXColormap
= 0;
55 UINT16 X11DRV_PALETTE_PaletteFlags
= 0;
57 static int X11DRV_PALETTE_Redshift
= 0; /* to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
58 static int X11DRV_PALETTE_Redmax
= 0;
59 static int X11DRV_PALETTE_Greenshift
= 0;
60 static int X11DRV_PALETTE_Greenmax
= 0;
61 static int X11DRV_PALETTE_Blueshift
= 0;
62 static int X11DRV_PALETTE_Bluemax
= 0;
63 static int X11DRV_PALETTE_Graymax
= 0;
65 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
66 static int X11DRV_PALETTE_firstFree
= 0;
67 static unsigned char X11DRV_PALETTE_freeList
[256];
69 /**********************************************************************/
71 /* Map an EGA index (0..15) to a pixel value in the system color space. */
73 int X11DRV_PALETTE_mapEGAPixel
[16];
75 /**********************************************************************/
77 #define NB_COLORCUBE_START_INDEX 63
79 /* Maps entry in the system palette to X pixel value */
80 int *X11DRV_PALETTE_PaletteToXPixel
= NULL
;
82 /* Maps pixel to the entry in the system palette */
83 int *X11DRV_PALETTE_XPixelToPalette
= NULL
;
85 /**********************************************************************/
87 static BOOL
X11DRV_PALETTE_BuildPrivateMap(void);
88 static BOOL
X11DRV_PALETTE_BuildSharedMap(void);
89 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits
, int *shift
, int *max
);
90 static void X11DRV_PALETTE_FillDefaultColors(void);
91 static void X11DRV_PALETTE_FormatSystemPalette(void);
92 static BOOL
X11DRV_PALETTE_CheckSysColor(COLORREF c
);
93 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
);
95 /***********************************************************************
98 * Initialize color management.
100 BOOL
X11DRV_PALETTE_Init(void)
102 int mask
, white
, black
;
105 Visual
*visual
= DefaultVisual( display
, DefaultScreen(display
) );
107 TRACE("initializing palette manager...\n");
109 white
= WhitePixelOfScreen( X11DRV_GetXScreen() );
110 black
= BlackPixelOfScreen( X11DRV_GetXScreen() );
112 for( mask
= 1; !((white
& mask
)^(black
& mask
)); mask
<<= 1 )
114 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
115 X11DRV_DevCaps
.sizePalette
= visual
->map_entries
;
117 switch(visual
->class)
120 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
123 if (Options
.usePrivateMap
)
125 XSetWindowAttributes win_attr
;
127 X11DRV_PALETTE_PaletteXColormap
= TSXCreateColormap( display
, X11DRV_GetXRootWindow(),
129 if (X11DRV_PALETTE_PaletteXColormap
)
131 X11DRV_PALETTE_PaletteFlags
|= (X11DRV_PALETTE_PRIVATE
| X11DRV_PALETTE_WHITESET
);
134 for( white
= X11DRV_DevCaps
.sizePalette
- 1; !(white
& 1); white
>>= 1 )
137 if( X11DRV_GetXRootWindow() != DefaultRootWindow(display
) )
139 win_attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
;
140 TSXChangeWindowAttributes( display
, X11DRV_GetXRootWindow(),
141 CWColormap
, &win_attr
);
146 X11DRV_PALETTE_PaletteXColormap
= DefaultColormapOfScreen( X11DRV_GetXScreen() );
150 X11DRV_PALETTE_PaletteXColormap
= DefaultColormapOfScreen( X11DRV_GetXScreen() );
151 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
152 X11DRV_PALETTE_Graymax
= (1<<MONITOR_GetDepth(&MONITOR_PrimaryMonitor
))-1;
156 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_VIRTUAL
;
158 int *depths
,nrofdepths
;
159 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
162 depths
=TSXListDepths(display
,DefaultScreen(display
),&nrofdepths
);
163 if ((nrofdepths
==2) && ((depths
[0]==4) || depths
[1]==4)) {
165 for( white
= X11DRV_DevCaps
.sizePalette
- 1; !(white
& 1); white
>>= 1 )
167 X11DRV_PALETTE_PaletteFlags
= (white
& mask
) ? X11DRV_PALETTE_WHITESET
: 0;
168 X11DRV_PALETTE_PaletteXColormap
= DefaultColormapOfScreen( X11DRV_GetXScreen() );
173 X11DRV_PALETTE_PaletteXColormap
= DefaultColormapOfScreen( X11DRV_GetXScreen() );
174 X11DRV_PALETTE_PaletteFlags
|= X11DRV_PALETTE_FIXED
;
175 X11DRV_PALETTE_ComputeShifts(visual
->red_mask
, &X11DRV_PALETTE_Redshift
, &X11DRV_PALETTE_Redmax
);
176 X11DRV_PALETTE_ComputeShifts(visual
->green_mask
, &X11DRV_PALETTE_Greenshift
, &X11DRV_PALETTE_Greenmax
);
177 X11DRV_PALETTE_ComputeShifts(visual
->blue_mask
, &X11DRV_PALETTE_Blueshift
, &X11DRV_PALETTE_Bluemax
);
182 TRACE(" visual class %i (%i)\n", visual
->class, monoPlane
);
184 memset(X11DRV_PALETTE_freeList
, 0, 256*sizeof(unsigned char));
186 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
187 X11DRV_PALETTE_BuildPrivateMap();
189 X11DRV_PALETTE_BuildSharedMap();
191 /* Build free list */
193 if( X11DRV_PALETTE_firstFree
!= -1 )
194 X11DRV_PALETTE_FormatSystemPalette();
196 X11DRV_PALETTE_FillDefaultColors();
198 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
199 X11DRV_DevCaps
.sizePalette
= 0;
202 X11DRV_DevCaps
.rasterCaps
|= RC_PALETTE
;
203 X11DRV_DevCaps
.sizePalette
= visual
->map_entries
;
209 /***********************************************************************
210 * X11DRV_PALETTE_Cleanup
212 * Free external colors we grabbed in the FillDefaultPalette()
214 void X11DRV_PALETTE_Cleanup(void)
216 if( COLOR_gapFilled
)
217 TSXFreeColors(display
, X11DRV_PALETTE_PaletteXColormap
,
218 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel
+ COLOR_gapStart
),
222 /***********************************************************************
223 * X11DRV_PALETTE_ComputeShifts
225 * Calculate conversion parameters for direct mapped visuals
227 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits
, int *shift
, int *max
)
238 for(i
=0;!(maskbits
&1);i
++)
245 /***********************************************************************
246 * X11DRV_PALETTE_BuildPrivateMap
248 * Allocate colorcells and initialize mapping tables.
250 static BOOL
X11DRV_PALETTE_BuildPrivateMap(void)
252 /* Private colormap - identity mapping */
257 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*X11DRV_DevCaps
.sizePalette
);
259 TRACE("Building private map - %i palette entries\n", X11DRV_DevCaps
.sizePalette
);
261 /* Allocate system palette colors */
263 for( i
=0; i
< X11DRV_DevCaps
.sizePalette
; i
++ )
265 if( i
< NB_RESERVED_COLORS
/2 )
267 color
.red
= COLOR_sysPalTemplate
[i
].peRed
* 65535 / 255;
268 color
.green
= COLOR_sysPalTemplate
[i
].peGreen
* 65535 / 255;
269 color
.blue
= COLOR_sysPalTemplate
[i
].peBlue
* 65535 / 255;
270 COLOR_sysPal
[i
] = COLOR_sysPalTemplate
[i
];
272 else if( i
>= X11DRV_DevCaps
.sizePalette
- NB_RESERVED_COLORS
/2 )
274 int j
= NB_RESERVED_COLORS
+ i
- X11DRV_DevCaps
.sizePalette
;
275 color
.red
= COLOR_sysPalTemplate
[j
].peRed
* 65535 / 255;
276 color
.green
= COLOR_sysPalTemplate
[j
].peGreen
* 65535 / 255;
277 color
.blue
= COLOR_sysPalTemplate
[j
].peBlue
* 65535 / 255;
278 COLOR_sysPal
[i
] = COLOR_sysPalTemplate
[j
];
281 color
.flags
= DoRed
| DoGreen
| DoBlue
;
283 TSXStoreColor(display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
285 /* Set EGA mapping if color is from the first or last eight */
288 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
289 else if (i
>= X11DRV_DevCaps
.sizePalette
- 8 )
290 X11DRV_PALETTE_mapEGAPixel
[i
- (X11DRV_DevCaps
.sizePalette
- 16)] = color
.pixel
;
293 X11DRV_PALETTE_XPixelToPalette
= X11DRV_PALETTE_PaletteToXPixel
= NULL
;
295 COLOR_gapStart
= 256; COLOR_gapEnd
= -1;
297 X11DRV_PALETTE_firstFree
= (X11DRV_DevCaps
.sizePalette
> NB_RESERVED_COLORS
)?NB_RESERVED_COLORS
/2 : -1;
302 /***********************************************************************
303 * X11DRV_PALETTE_BuildSharedMap
305 * Allocate colorcells and initialize mapping tables.
307 static BOOL
X11DRV_PALETTE_BuildSharedMap(void)
310 unsigned long sysPixel
[NB_RESERVED_COLORS
];
311 unsigned long* pixDynMapping
= NULL
;
312 unsigned long plane_masks
[1];
314 int diff
, r
, g
, b
, max
= 256, bp
= 0, wp
= 1;
317 /* read "AllocSystemColors" from wine.conf */
319 COLOR_max
= PROFILE_GetWineIniInt( "options", "AllocSystemColors", 256);
320 if (COLOR_max
> 256) COLOR_max
= 256;
321 else if (COLOR_max
< 20) COLOR_max
= 20;
322 TRACE("%d colors configured.\n", COLOR_max
);
324 TRACE("Building shared map - %i palette entries\n", X11DRV_DevCaps
.sizePalette
);
326 /* Be nice and allocate system colors as read-only */
328 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
330 color
.red
= COLOR_sysPalTemplate
[i
].peRed
* 65535 / 255;
331 color
.green
= COLOR_sysPalTemplate
[i
].peGreen
* 65535 / 255;
332 color
.blue
= COLOR_sysPalTemplate
[i
].peBlue
* 65535 / 255;
333 color
.flags
= DoRed
| DoGreen
| DoBlue
;
335 if (!TSXAllocColor( display
, X11DRV_PALETTE_PaletteXColormap
, &color
))
341 WARN("Not enough colors for the full system palette.\n");
343 bp
= BlackPixel(display
, DefaultScreen(display
));
344 wp
= WhitePixel(display
, DefaultScreen(display
));
346 max
= (0xffffffff)>>(32 - MONITOR_GetDepth(&MONITOR_PrimaryMonitor
));
354 /* reinit color (XAllocColor() may change it)
355 * and map to the best shared colorcell */
357 color
.red
= COLOR_sysPalTemplate
[i
].peRed
* 65535 / 255;
358 color
.green
= COLOR_sysPalTemplate
[i
].peGreen
* 65535 / 255;
359 color
.blue
= COLOR_sysPalTemplate
[i
].peBlue
* 65535 / 255;
361 best
.pixel
= best
.red
= best
.green
= best
.blue
= 0;
362 for( c
.pixel
= 0, diff
= 0x7fffffff; c
.pixel
< max
; c
.pixel
+= step
)
364 TSXQueryColor(display
, X11DRV_PALETTE_PaletteXColormap
, &c
);
365 r
= (c
.red
- color
.red
)>>8;
366 g
= (c
.green
- color
.green
)>>8;
367 b
= (c
.blue
- color
.blue
)>>8;
369 if( r
< diff
) { best
= c
; diff
= r
; }
372 if( TSXAllocColor(display
, X11DRV_PALETTE_PaletteXColormap
, &best
) )
373 color
.pixel
= best
.pixel
;
374 else color
.pixel
= (i
< NB_RESERVED_COLORS
/2)? bp
: wp
;
377 sysPixel
[i
] = color
.pixel
;
379 TRACE("syscolor(%lx) -> pixel %i\n",
380 *(COLORREF
*)(COLOR_sysPalTemplate
+i
), (int)color
.pixel
);
382 /* Set EGA mapping if color in the first or last eight */
385 X11DRV_PALETTE_mapEGAPixel
[i
] = color
.pixel
;
386 else if (i
>= NB_RESERVED_COLORS
- 8 )
387 X11DRV_PALETTE_mapEGAPixel
[i
- (NB_RESERVED_COLORS
-16)] = color
.pixel
;
390 /* now allocate changeable set */
392 if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
394 int c_min
= 0, c_max
= X11DRV_DevCaps
.sizePalette
, c_val
;
396 TRACE("Dynamic colormap... \n");
398 /* comment this out if you want to debug palette init */
400 TSXGrabServer(display
);
402 /* let's become the first client that actually follows
403 * X guidelines and does binary search...
406 pixDynMapping
= (unsigned long*)xmalloc(sizeof(long)*X11DRV_DevCaps
.sizePalette
);
407 while( c_max
- c_min
> 0 )
409 c_val
= (c_max
+ c_min
)/2 + (c_max
+ c_min
)%2;
411 if( !TSXAllocColorCells(display
, X11DRV_PALETTE_PaletteXColormap
, False
,
412 plane_masks
, 0, pixDynMapping
, c_val
) )
416 TSXFreeColors(display
, X11DRV_PALETTE_PaletteXColormap
, pixDynMapping
, c_val
, 0);
421 if( c_min
> COLOR_max
- NB_RESERVED_COLORS
)
422 c_min
= COLOR_max
- NB_RESERVED_COLORS
;
424 c_min
= (c_min
/2) + (c_min
/2); /* need even set for split palette */
427 if( !TSXAllocColorCells(display
, X11DRV_PALETTE_PaletteXColormap
, False
,
428 plane_masks
, 0, pixDynMapping
, c_min
) )
430 WARN("Inexplicable failure during colorcell allocation.\n");
434 X11DRV_DevCaps
.sizePalette
= c_min
+ NB_RESERVED_COLORS
;
436 TSXUngrabServer(display
);
438 TRACE("adjusted size %i colorcells\n", X11DRV_DevCaps
.sizePalette
);
440 else if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
442 /* virtual colorspace - ToPhysical takes care of
443 * color translations but we have to allocate full palette
444 * to maintain compatibility
446 X11DRV_DevCaps
.sizePalette
= 256;
447 TRACE("Virtual colorspace - screendepth %i\n", MONITOR_GetDepth(&MONITOR_PrimaryMonitor
));
449 else X11DRV_DevCaps
.sizePalette
= NB_RESERVED_COLORS
; /* system palette only - however we can alloc a bunch
450 * of colors and map to them */
452 TRACE("Shared system palette uses %i colors.\n", X11DRV_DevCaps
.sizePalette
);
454 /* set gap to account for pixel shortage. It has to be right in the center
455 * of the system palette because otherwise raster ops get screwed. */
457 if( X11DRV_DevCaps
.sizePalette
>= 256 )
458 { COLOR_gapStart
= 256; COLOR_gapEnd
= -1; }
460 { COLOR_gapStart
= X11DRV_DevCaps
.sizePalette
/2; COLOR_gapEnd
= 255 - X11DRV_DevCaps
.sizePalette
/2; }
462 X11DRV_PALETTE_firstFree
= ( X11DRV_DevCaps
.sizePalette
> NB_RESERVED_COLORS
&&
463 (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
|| !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)) )
464 ? NB_RESERVED_COLORS
/2 : -1;
466 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*256);
468 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
470 if( MONITOR_GetDepth(&MONITOR_PrimaryMonitor
) <= 8 )
472 X11DRV_PALETTE_XPixelToPalette
= (int*)xmalloc(sizeof(int)*256);
473 memset( X11DRV_PALETTE_XPixelToPalette
, 0, 256*sizeof(int) );
476 /* for hicolor visuals PaletteToPixel mapping is used to skip
477 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
480 X11DRV_PALETTE_PaletteToXPixel
= (int*)xmalloc(sizeof(int)*256);
482 for( i
= j
= 0; i
< 256; i
++ )
484 if( i
>= COLOR_gapStart
&& i
<= COLOR_gapEnd
)
486 X11DRV_PALETTE_PaletteToXPixel
[i
] = 0;
487 COLOR_sysPal
[i
].peFlags
= 0; /* mark as unused */
491 if( i
< NB_RESERVED_COLORS
/2 )
493 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[i
];
494 COLOR_sysPal
[i
] = COLOR_sysPalTemplate
[i
];
496 else if( i
>= 256 - NB_RESERVED_COLORS
/2 )
498 X11DRV_PALETTE_PaletteToXPixel
[i
] = sysPixel
[(i
+ NB_RESERVED_COLORS
) - 256];
499 COLOR_sysPal
[i
] = COLOR_sysPalTemplate
[(i
+ NB_RESERVED_COLORS
) - 256];
501 else if( pixDynMapping
)
502 X11DRV_PALETTE_PaletteToXPixel
[i
] = pixDynMapping
[j
++];
504 X11DRV_PALETTE_PaletteToXPixel
[i
] = i
;
506 TRACE("index %i -> pixel %i\n", i
, X11DRV_PALETTE_PaletteToXPixel
[i
]);
508 if( X11DRV_PALETTE_XPixelToPalette
)
509 X11DRV_PALETTE_XPixelToPalette
[X11DRV_PALETTE_PaletteToXPixel
[i
]] = i
;
512 if( pixDynMapping
) free(pixDynMapping
);
517 /***********************************************************************
518 * Colormap Initialization
520 static void X11DRV_PALETTE_FillDefaultColors(void)
522 /* initialize unused entries to what Windows uses as a color
523 * cube - based on Greg Kreider's code.
527 int red
, no_r
, inc_r
;
528 int green
, no_g
, inc_g
;
529 int blue
, no_b
, inc_b
;
531 if (X11DRV_DevCaps
.sizePalette
<= NB_RESERVED_COLORS
)
533 while (i
*i
*i
< (X11DRV_DevCaps
.sizePalette
- NB_RESERVED_COLORS
)) i
++;
534 no_r
= no_g
= no_b
= --i
;
535 if ((no_r
* (no_g
+1) * no_b
) < (X11DRV_DevCaps
.sizePalette
- NB_RESERVED_COLORS
)) no_g
++;
536 if ((no_r
* no_g
* (no_b
+1)) < (X11DRV_DevCaps
.sizePalette
- NB_RESERVED_COLORS
)) no_b
++;
537 inc_r
= (255 - NB_COLORCUBE_START_INDEX
)/no_r
;
538 inc_g
= (255 - NB_COLORCUBE_START_INDEX
)/no_g
;
539 inc_b
= (255 - NB_COLORCUBE_START_INDEX
)/no_b
;
541 idx
= X11DRV_PALETTE_firstFree
;
544 for (blue
= NB_COLORCUBE_START_INDEX
; blue
< 256 && idx
; blue
+= inc_b
)
545 for (green
= NB_COLORCUBE_START_INDEX
; green
< 256 && idx
; green
+= inc_g
)
546 for (red
= NB_COLORCUBE_START_INDEX
; red
< 256 && idx
; red
+= inc_r
)
550 if( red
== NB_COLORCUBE_START_INDEX
&& green
== red
&& blue
== green
) continue;
552 COLOR_sysPal
[idx
].peRed
= red
;
553 COLOR_sysPal
[idx
].peGreen
= green
;
554 COLOR_sysPal
[idx
].peBlue
= blue
;
558 if( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
560 if (X11DRV_PALETTE_Redmax
!= 255) no_r
= (red
* X11DRV_PALETTE_Redmax
) / 255;
561 if (X11DRV_PALETTE_Greenmax
!= 255) no_g
= (green
* X11DRV_PALETTE_Greenmax
) / 255;
562 if (X11DRV_PALETTE_Bluemax
!= 255) no_b
= (blue
* X11DRV_PALETTE_Bluemax
) / 255;
564 X11DRV_PALETTE_PaletteToXPixel
[idx
] = (no_r
<< X11DRV_PALETTE_Redshift
) | (no_g
<< X11DRV_PALETTE_Greenshift
) | (no_b
<< X11DRV_PALETTE_Blueshift
);
566 else if( !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
569 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[idx
] : idx
;
570 color
.red
= COLOR_sysPal
[idx
].peRed
<< 8;
571 color
.green
= COLOR_sysPal
[idx
].peGreen
<< 8;
572 color
.blue
= COLOR_sysPal
[idx
].peBlue
<< 8;
573 color
.flags
= DoRed
| DoGreen
| DoBlue
;
574 TSXStoreColor(display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
576 idx
= X11DRV_PALETTE_freeList
[idx
];
579 /* try to fill some entries in the "gap" with
580 * what's already in the colormap - they will be
581 * mappable to but not changeable. */
583 if( COLOR_gapStart
< COLOR_gapEnd
&& X11DRV_PALETTE_XPixelToPalette
)
588 max
= COLOR_max
- (256 - (COLOR_gapEnd
- COLOR_gapStart
));
589 for ( i
= 0, idx
= COLOR_gapStart
; i
< 256 && idx
<= COLOR_gapEnd
; i
++ )
590 if( X11DRV_PALETTE_XPixelToPalette
[i
] == 0 )
594 TSXQueryColor(display
, X11DRV_PALETTE_PaletteXColormap
, &xc
);
595 r
= xc
.red
>>8; g
= xc
.green
>>8; b
= xc
.blue
>>8;
597 if( xc
.pixel
< 256 && X11DRV_PALETTE_CheckSysColor(RGB(r
, g
, b
)) &&
598 TSXAllocColor(display
, X11DRV_PALETTE_PaletteXColormap
, &xc
) )
600 X11DRV_PALETTE_XPixelToPalette
[xc
.pixel
] = idx
;
601 X11DRV_PALETTE_PaletteToXPixel
[idx
] = xc
.pixel
;
602 *(COLORREF
*)(COLOR_sysPal
+ idx
) = RGB(r
, g
, b
);
603 COLOR_sysPal
[idx
++].peFlags
|= PC_SYS_USED
;
604 if( --max
<= 0 ) break;
607 COLOR_gapFilled
= idx
- COLOR_gapStart
;
612 /***********************************************************************
613 * X11DRV_PALETTE_ToLogical
615 * Return RGB color for given X pixel.
617 COLORREF
X11DRV_PALETTE_ToLogical(int pixel
)
622 /* truecolor visual */
624 if (MONITOR_GetDepth(&MONITOR_PrimaryMonitor
) >= 24) return pixel
;
627 /* check for hicolor visuals first */
629 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
&& !X11DRV_PALETTE_Graymax
)
631 color
.red
= (pixel
>> X11DRV_PALETTE_Redshift
) & X11DRV_PALETTE_Redmax
;
632 color
.green
= (pixel
>> X11DRV_PALETTE_Greenshift
) & X11DRV_PALETTE_Greenmax
;
633 color
.blue
= (pixel
>> X11DRV_PALETTE_Blueshift
) & X11DRV_PALETTE_Bluemax
;
634 return RGB(MulDiv(color
.red
, 255, X11DRV_PALETTE_Redmax
),
635 MulDiv(color
.green
, 255, X11DRV_PALETTE_Greenmax
),
636 MulDiv(color
.blue
, 255, X11DRV_PALETTE_Bluemax
));
639 /* check if we can bypass X */
641 if ((MONITOR_GetDepth(&MONITOR_PrimaryMonitor
) <= 8) && (pixel
< 256) &&
642 !(X11DRV_PALETTE_PaletteFlags
& (X11DRV_PALETTE_VIRTUAL
| X11DRV_PALETTE_FIXED
)) )
643 return ( *(COLORREF
*)(COLOR_sysPal
+
644 ((X11DRV_PALETTE_XPixelToPalette
)?X11DRV_PALETTE_XPixelToPalette
[pixel
]:pixel
)) ) & 0x00ffffff;
647 TSXQueryColor(display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
648 return RGB(color
.red
>> 8, color
.green
>> 8, color
.blue
>> 8);
651 /***********************************************************************
652 * X11DRV_PALETTE_ToPhysical
654 * Return the physical color closest to 'color'.
656 int X11DRV_PALETTE_ToPhysical( DC
*dc
, COLORREF color
)
659 HPALETTE16 hPal
= (dc
)? dc
->w
.hPalette
: STOCK_DEFAULT_PALETTE
;
660 unsigned char spec_type
= color
>> 24;
661 PALETTEOBJ
* palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hPal
, PALETTE_MAGIC
);
663 /* palPtr can be NULL when DC is being destroyed */
664 if( !palPtr
) return 0;
666 if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
)
668 /* there is no colormap limitation; we are going to have to compute
669 * the pixel value from the visual information stored earlier
672 unsigned long red
, green
, blue
;
677 case 1: /* PALETTEINDEX */
679 if( (idx
= color
& 0xffff) >= palPtr
->logpalette
.palNumEntries
)
681 WARN("RGB(%lx) : idx %d is out of bounds, assuming black\n", color
, idx
);
682 GDI_HEAP_UNLOCK( hPal
);
686 if( palPtr
->mapping
)
688 GDI_HEAP_UNLOCK( hPal
);
689 return palPtr
->mapping
[idx
];
691 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
696 /* fall through to RGB */
699 if( dc
&& (dc
->w
.bitsPerPixel
== 1) )
701 GDI_HEAP_UNLOCK( hPal
);
702 return (((color
>> 16) & 0xff) +
703 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
708 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
710 if (X11DRV_PALETTE_Graymax
)
712 /* grayscale only; return scaled value */
713 GDI_HEAP_UNLOCK( hPal
);
714 return ( (red
* 30 + green
* 69 + blue
* 11) * X11DRV_PALETTE_Graymax
) / 25500;
718 /* scale each individually and construct the TrueColor pixel value */
719 if (X11DRV_PALETTE_Redmax
!= 255)
720 red
= MulDiv(red
, X11DRV_PALETTE_Redmax
, 255);
721 if (X11DRV_PALETTE_Greenmax
!= 255)
722 green
= MulDiv(green
, X11DRV_PALETTE_Greenmax
, 255);
723 if (X11DRV_PALETTE_Bluemax
!= 255)
724 blue
= MulDiv(blue
, X11DRV_PALETTE_Bluemax
, 255);
726 GDI_HEAP_UNLOCK( hPal
);
727 return (red
<< X11DRV_PALETTE_Redshift
) | (green
<< X11DRV_PALETTE_Greenshift
) | (blue
<< X11DRV_PALETTE_Blueshift
);
733 if( !palPtr
->mapping
)
734 WARN("Palette %04x is not realized\n", dc
->w
.hPalette
);
736 switch(spec_type
) /* we have to peruse DC and system palette */
740 /* fall through to RGB */
743 if( dc
&& (dc
->w
.bitsPerPixel
== 1) )
745 GDI_HEAP_UNLOCK( hPal
);
746 return (((color
>> 16) & 0xff) +
747 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
750 index
= COLOR_PaletteLookupPixel( COLOR_sysPal
, 256,
751 X11DRV_PALETTE_PaletteToXPixel
, color
, FALSE
);
753 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
756 case 1: /* PALETTEINDEX */
757 index
= color
& 0xffff;
759 if( index
>= palPtr
->logpalette
.palNumEntries
)
760 WARN("RGB(%lx) : index %i is out of bounds\n", color
, index
);
761 else if( palPtr
->mapping
) index
= palPtr
->mapping
[index
];
763 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
766 case 2: /* PALETTERGB */
767 index
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
768 palPtr
->logpalette
.palNumEntries
,
769 palPtr
->mapping
, color
, FALSE
);
770 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
776 GDI_HEAP_UNLOCK( hPal
);
780 /***********************************************************************
781 * X11DRV_PALETTE_LookupSystemXPixel
783 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col
)
785 int i
, best
= 0, diff
= 0x7fffffff;
786 int size
= X11DRV_DevCaps
.sizePalette
;
789 for( i
= 0; i
< size
&& diff
; i
++ )
791 if( i
== NB_RESERVED_COLORS
/2 )
793 int newi
= size
- NB_RESERVED_COLORS
/2;
797 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
798 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
799 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
803 if( r
< diff
) { best
= i
; diff
= r
; }
806 return (X11DRV_PALETTE_PaletteToXPixel
)? X11DRV_PALETTE_PaletteToXPixel
[best
] : best
;
809 /***********************************************************************
810 * X11DRV_PALETTE_FormatSystemPalette
812 static void X11DRV_PALETTE_FormatSystemPalette(void)
814 /* Build free list so we'd have an easy way to find
815 * out if there are any available colorcells.
818 int i
, j
= X11DRV_PALETTE_firstFree
= NB_RESERVED_COLORS
/2;
820 COLOR_sysPal
[j
].peFlags
= 0;
821 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
822 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
824 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
825 X11DRV_PALETTE_freeList
[j
] = i
; /* next */
828 X11DRV_PALETTE_freeList
[j
] = 0;
831 /***********************************************************************
832 * X11DRV_PALETTE_CheckSysColor
834 static BOOL
X11DRV_PALETTE_CheckSysColor(COLORREF c
)
837 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
838 if( c
== (*(COLORREF
*)(COLOR_sysPalTemplate
+ i
) & 0x00ffffff) )
843 /***********************************************************************
844 * X11DRV_PALETTE_SetMapping
846 * Set the color-mapping table for selected palette.
847 * Return number of entries which mapping has changed.
849 int X11DRV_PALETTE_SetMapping( PALETTEOBJ
* palPtr
, UINT uStart
, UINT uNum
, BOOL mapOnly
)
852 int prevMapping
= (palPtr
->mapping
) ? 1 : 0;
853 int index
, iRemapped
= 0;
855 /* reset dynamic system palette entries */
857 if( !mapOnly
&& X11DRV_PALETTE_firstFree
!= -1)
858 X11DRV_PALETTE_FormatSystemPalette();
860 /* initialize palette mapping table */
862 palPtr
->mapping
= (int*)xrealloc(palPtr
->mapping
, sizeof(int)*
863 palPtr
->logpalette
.palNumEntries
);
865 for( uNum
+= uStart
; uStart
< uNum
; uStart
++ )
870 switch( palPtr
->logpalette
.palPalEntry
[uStart
].peFlags
& 0x07 )
872 case PC_EXPLICIT
: /* palette entries are indices into system palette */
873 index
= *(WORD
*)(palPtr
->logpalette
.palPalEntry
+ uStart
);
874 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
876 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
881 case PC_RESERVED
: /* forbid future mappings to this entry */
882 flag
|= PC_SYS_RESERVED
;
885 default: /* try to collapse identical colors */
886 index
= COLOR_PaletteLookupExactIndex(COLOR_sysPal
, 256,
887 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
));
892 if( X11DRV_PALETTE_firstFree
> 0 && !(X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_FIXED
) )
895 index
= X11DRV_PALETTE_firstFree
; /* ought to be available */
896 X11DRV_PALETTE_firstFree
= X11DRV_PALETTE_freeList
[index
];
898 color
.pixel
= (X11DRV_PALETTE_PaletteToXPixel
) ? X11DRV_PALETTE_PaletteToXPixel
[index
] : index
;
899 color
.red
= palPtr
->logpalette
.palPalEntry
[uStart
].peRed
<< 8;
900 color
.green
= palPtr
->logpalette
.palPalEntry
[uStart
].peGreen
<< 8;
901 color
.blue
= palPtr
->logpalette
.palPalEntry
[uStart
].peBlue
<< 8;
902 color
.flags
= DoRed
| DoGreen
| DoBlue
;
903 TSXStoreColor(display
, X11DRV_PALETTE_PaletteXColormap
, &color
);
905 COLOR_sysPal
[index
] = palPtr
->logpalette
.palPalEntry
[uStart
];
906 COLOR_sysPal
[index
].peFlags
= flag
;
907 X11DRV_PALETTE_freeList
[index
] = 0;
909 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
912 else if ( X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_VIRTUAL
)
914 index
= X11DRV_PALETTE_ToPhysical( NULL
, 0x00ffffff &
915 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
));
919 /* we have to map to existing entry in the system palette */
921 index
= COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
,
922 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
), TRUE
);
924 palPtr
->logpalette
.palPalEntry
[uStart
].peFlags
|= PC_SYS_USED
;
926 if( X11DRV_PALETTE_PaletteToXPixel
) index
= X11DRV_PALETTE_PaletteToXPixel
[index
];
930 if( !prevMapping
|| palPtr
->mapping
[uStart
] != index
) iRemapped
++;
931 palPtr
->mapping
[uStart
] = index
;
933 TRACE("entry %i (%lx) -> pixel %i\n", uStart
,
934 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
), index
);
940 /***********************************************************************
941 * X11DRV_PALETTE_UpdateMapping
943 * Update the color-mapping table for selected palette.
944 * Return number of entries which mapping has changed.
946 int X11DRV_PALETTE_UpdateMapping(PALETTEOBJ
*palPtr
)
948 int i
, index
, realized
= 0;
950 if (!X11DRV_DevCaps
.sizePalette
)
953 for( i
= 0; i
< 20; i
++ )
955 index
= X11DRV_PALETTE_LookupSystemXPixel(*(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
));
957 /* mapping is allocated in COLOR_InitPalette() */
959 if( index
!= palPtr
->mapping
[i
] ) { palPtr
->mapping
[i
]=index
; realized
++; }
965 /**************************************************************************
966 * X11DRV_PALETTE_IsDark
968 BOOL
X11DRV_PALETTE_IsDark(int pixel
)
970 COLORREF col
= X11DRV_PALETTE_ToLogical(pixel
);
971 return (GetRValue(col
) + GetGValue(col
) + GetBValue(col
)) <= 0x180;
974 #endif /* !defined(X_DISPLAY_MISSING) */