Release 990214.
[wine/multimedia.git] / objects / color.c
blob956c88d7a769ea55ecfcc1a8bd0be47ded6c3d14
1 /*
2 * Color functions
4 * Copyright 1993 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
6 */
8 #include "ts_xlib.h"
9 #include "x11drv.h"
11 #include <stdlib.h>
12 #include <string.h>
13 #include "wintypes.h"
14 #include "options.h"
15 #include "gdi.h"
16 #include "color.h"
17 #include "debug.h"
18 #include "xmalloc.h"
19 #include "monitor.h"
21 /* Palette indexed mode:
23 * logical palette -> mapping -> pixel
26 * Windows needs contiguous color space ( from 0 to n ) but
27 * it is possible only with the private colormap. Otherwise we
28 * have to map DC palette indices to real pixel values. With
29 * private colormaps it boils down to the identity mapping. The
30 * other special case is when we have a fixed color visual with
31 * the screendepth > 8 - we abandon palette mappings altogether
32 * because pixel values can be calculated without X server
33 * assistance.
35 * Windows palette manager is described in the
36 * http://premium.microsoft.com/msdn/library/techart/f30/f34/f40/d4d/sa942.htm
39 typedef struct
41 Colormap colorMap;
42 UINT16 size;
43 UINT16 flags;
44 INT32 monoPlane; /* bit plane different for white and black pixels */
46 INT32 (*mapColor)( DC*, COLORREF );
47 } CSPACE;
49 static CSPACE cSpace = {0, 0, 0};
51 static int COLOR_Redshift = 0; /* to handle abortive COLOR_VIRTUAL visuals */
52 static int COLOR_Redmax = 0;
53 static int COLOR_Greenshift = 0;
54 static int COLOR_Greenmax = 0;
55 static int COLOR_Blueshift = 0;
56 static int COLOR_Bluemax = 0;
57 static int COLOR_Graymax = 0;
59 /* System color space.
61 * First 10 and last 10 colors in COLOR_sysPalette are
62 * "guarded". RealizePalette changes only the rest of colorcells. For
63 * currently inactive window it changes only DC palette mappings.
66 #define NB_COLORCUBE_START_INDEX 63
68 Visual* visual = NULL;
70 static PALETTEENTRY* COLOR_sysPal = NULL; /* current system palette */
71 static int COLOR_gapStart = 256;
72 static int COLOR_gapEnd = -1;
73 static int COLOR_gapFilled = 0;
74 static int COLOR_max = 256;
76 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
77 static int COLOR_firstFree = 0;
78 static unsigned char COLOR_freeList[256];
80 /* Maps entry in the system palette to X pixel value */
81 int* COLOR_PaletteToPixel = NULL;
83 /* Maps pixel to the entry in the system palette */
84 int* COLOR_PixelToPalette = NULL;
86 static const PALETTEENTRY __sysPalTemplate[NB_RESERVED_COLORS] =
88 /* first 10 entries in the system palette */
89 /* red green blue flags */
90 { 0x00, 0x00, 0x00, PC_SYS_USED },
91 { 0x80, 0x00, 0x00, PC_SYS_USED },
92 { 0x00, 0x80, 0x00, PC_SYS_USED },
93 { 0x80, 0x80, 0x00, PC_SYS_USED },
94 { 0x00, 0x00, 0x80, PC_SYS_USED },
95 { 0x80, 0x00, 0x80, PC_SYS_USED },
96 { 0x00, 0x80, 0x80, PC_SYS_USED },
97 { 0xc0, 0xc0, 0xc0, PC_SYS_USED },
98 { 0xc0, 0xdc, 0xc0, PC_SYS_USED },
99 { 0xa6, 0xca, 0xf0, PC_SYS_USED },
101 /* ... c_min/2 dynamic colorcells */
103 /* ... gap (for sparse palettes) */
105 /* ... c_min/2 dynamic colorcells */
107 { 0xff, 0xfb, 0xf0, PC_SYS_USED },
108 { 0xa0, 0xa0, 0xa4, PC_SYS_USED },
109 { 0x80, 0x80, 0x80, PC_SYS_USED },
110 { 0xff, 0x00, 0x00, PC_SYS_USED },
111 { 0x00, 0xff, 0x00, PC_SYS_USED },
112 { 0xff, 0xff, 0x00, PC_SYS_USED },
113 { 0x00, 0x00, 0xff, PC_SYS_USED },
114 { 0xff, 0x00, 0xff, PC_SYS_USED },
115 { 0x00, 0xff, 0xff, PC_SYS_USED },
116 { 0xff, 0xff, 0xff, PC_SYS_USED } /* last 10 */
119 /* Map an EGA index (0..15) to a pixel value in the system color space. */
121 int COLOR_mapEGAPixel[16];
123 /***********************************************************************
124 * Misc auxiliary functions
126 Colormap X11DRV_COLOR_GetColormap(void)
128 return cSpace.colorMap;
131 BOOL32 COLOR_GetMonoPlane(INT32* plane)
133 *plane = cSpace.monoPlane;
134 return (cSpace.flags & COLOR_WHITESET) ? TRUE : FALSE;
137 UINT16 COLOR_GetSystemPaletteSize(void)
139 return (cSpace.flags & COLOR_PRIVATE) ? cSpace.size : 256;
142 UINT16 COLOR_GetSystemPaletteFlags(void)
144 return cSpace.flags;
147 const PALETTEENTRY* COLOR_GetSystemPaletteTemplate(void)
149 return __sysPalTemplate;
152 COLORREF COLOR_GetSystemPaletteEntry(UINT32 i)
154 return *(COLORREF*)(COLOR_sysPal + i) & 0x00ffffff;
157 void COLOR_FormatSystemPalette(void)
159 /* Build free list so we'd have an easy way to find
160 * out if there are any available colorcells.
163 int i, j = COLOR_firstFree = NB_RESERVED_COLORS/2;
165 COLOR_sysPal[j].peFlags = 0;
166 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
167 if( i < COLOR_gapStart || i > COLOR_gapEnd )
169 COLOR_sysPal[i].peFlags = 0; /* unused tag */
170 COLOR_freeList[j] = i; /* next */
171 j = i;
173 COLOR_freeList[j] = 0;
176 BOOL32 COLOR_CheckSysColor(COLORREF c)
178 int i;
179 for( i = 0; i < NB_RESERVED_COLORS; i++ )
180 if( c == (*(COLORREF*)(__sysPalTemplate + i) & 0x00ffffff) )
181 return 0;
182 return 1;
185 /***********************************************************************
186 * Colormap Initialization
188 static void COLOR_FillDefaultColors(void)
190 /* initialize unused entries to what Windows uses as a color
191 * cube - based on Greg Kreider's code.
194 int i = 0, idx = 0;
195 int red, no_r, inc_r;
196 int green, no_g, inc_g;
197 int blue, no_b, inc_b;
199 if (cSpace.size <= NB_RESERVED_COLORS)
200 return;
201 while (i*i*i < (cSpace.size - NB_RESERVED_COLORS)) i++;
202 no_r = no_g = no_b = --i;
203 if ((no_r * (no_g+1) * no_b) < (cSpace.size - NB_RESERVED_COLORS)) no_g++;
204 if ((no_r * no_g * (no_b+1)) < (cSpace.size - NB_RESERVED_COLORS)) no_b++;
205 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
206 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
207 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
209 idx = COLOR_firstFree;
211 if( idx != -1 )
212 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
213 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
214 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
216 /* weird but true */
218 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
220 COLOR_sysPal[idx].peRed = red;
221 COLOR_sysPal[idx].peGreen = green;
222 COLOR_sysPal[idx].peBlue = blue;
224 /* set X color */
226 if( cSpace.flags & COLOR_VIRTUAL )
228 if (COLOR_Redmax != 255) no_r = (red * COLOR_Redmax) / 255;
229 if (COLOR_Greenmax != 255) no_g = (green * COLOR_Greenmax) / 255;
230 if (COLOR_Bluemax != 255) no_b = (blue * COLOR_Bluemax) / 255;
232 COLOR_PaletteToPixel[idx] = (no_r << COLOR_Redshift) | (no_g << COLOR_Greenshift) | (no_b << COLOR_Blueshift);
234 else if( !(cSpace.flags & COLOR_FIXED) )
236 XColor color = { color.pixel = (COLOR_PaletteToPixel)? COLOR_PaletteToPixel[idx] : idx ,
237 COLOR_sysPal[idx].peRed << 8,
238 COLOR_sysPal[idx].peGreen << 8,
239 COLOR_sysPal[idx].peGreen << 8,
240 (DoRed | DoGreen | DoBlue) };
241 TSXStoreColor(display, cSpace.colorMap, &color);
243 idx = COLOR_freeList[idx];
246 /* try to fill some entries in the "gap" with
247 * what's already in the colormap - they will be
248 * mappable to but not changeable. */
250 if( COLOR_gapStart < COLOR_gapEnd && COLOR_PixelToPalette )
252 XColor xc;
253 int r, g, b, max;
255 max = COLOR_max - (256 - (COLOR_gapEnd - COLOR_gapStart));
256 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
257 if( COLOR_PixelToPalette[i] == 0 )
259 xc.pixel = i;
261 TSXQueryColor(display, cSpace.colorMap, &xc);
262 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
264 if( xc.pixel < 256 && COLOR_CheckSysColor(RGB(r, g, b)) &&
265 TSXAllocColor(display, cSpace.colorMap, &xc) )
267 COLOR_PixelToPalette[xc.pixel] = idx;
268 COLOR_PaletteToPixel[idx] = xc.pixel;
269 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
270 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
271 if( --max <= 0 ) break;
274 COLOR_gapFilled = idx - COLOR_gapStart;
278 /***********************************************************************
279 * COLOR_BuildPrivateMap/COLOR_BuildSharedMap
281 * Allocate colorcells and initialize mapping tables.
283 static BOOL32 COLOR_BuildPrivateMap(CSPACE* cs)
285 /* Private colormap - identity mapping */
287 XColor color;
288 int i;
290 COLOR_sysPal = (PALETTEENTRY*)xmalloc(sizeof(PALETTEENTRY)*cs->size);
292 TRACE(palette,"Building private map - %i palette entries\n", cs->size);
294 /* Allocate system palette colors */
296 for( i=0; i < cs->size; i++ )
298 if( i < NB_RESERVED_COLORS/2 )
300 color.red = __sysPalTemplate[i].peRed * 65535 / 255;
301 color.green = __sysPalTemplate[i].peGreen * 65535 / 255;
302 color.blue = __sysPalTemplate[i].peBlue * 65535 / 255;
303 COLOR_sysPal[i] = __sysPalTemplate[i];
305 else if( i >= cs->size - NB_RESERVED_COLORS/2 )
307 int j = NB_RESERVED_COLORS + i - cs->size;
308 color.red = __sysPalTemplate[j].peRed * 65535 / 255;
309 color.green = __sysPalTemplate[j].peGreen * 65535 / 255;
310 color.blue = __sysPalTemplate[j].peBlue * 65535 / 255;
311 COLOR_sysPal[i] = __sysPalTemplate[j];
314 color.flags = DoRed | DoGreen | DoBlue;
315 color.pixel = i;
316 TSXStoreColor(display, cs->colorMap, &color);
318 /* Set EGA mapping if color is from the first or last eight */
320 if (i < 8)
321 COLOR_mapEGAPixel[i] = color.pixel;
322 else if (i >= cs->size - 8 )
323 COLOR_mapEGAPixel[i - (cs->size - 16)] = color.pixel;
326 COLOR_PixelToPalette = COLOR_PaletteToPixel = NULL;
328 COLOR_gapStart = 256; COLOR_gapEnd = -1;
330 COLOR_firstFree = (cs->size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
332 return FALSE;
335 static BOOL32 COLOR_BuildSharedMap(CSPACE* cs)
337 XColor color;
338 unsigned long sysPixel[NB_RESERVED_COLORS];
339 unsigned long* pixDynMapping = NULL;
340 unsigned long plane_masks[1];
341 int i, j, warn = 0;
342 int diff, r, g, b, max = 256, bp = 0, wp = 1;
343 int step = 1;
345 /* read "AllocSystemColors" from wine.conf */
347 COLOR_max = PROFILE_GetWineIniInt( "options", "AllocSystemColors", 256);
348 if (COLOR_max > 256) COLOR_max = 256;
349 else if (COLOR_max < 20) COLOR_max = 20;
350 TRACE(palette,"%d colors configured.\n", COLOR_max);
352 TRACE(palette,"Building shared map - %i palette entries\n", cs->size);
354 /* Be nice and allocate system colors as read-only */
356 for( i = 0; i < NB_RESERVED_COLORS; i++ )
358 color.red = __sysPalTemplate[i].peRed * 65535 / 255;
359 color.green = __sysPalTemplate[i].peGreen * 65535 / 255;
360 color.blue = __sysPalTemplate[i].peBlue * 65535 / 255;
361 color.flags = DoRed | DoGreen | DoBlue;
363 if (!TSXAllocColor( display, cSpace.colorMap, &color ))
365 XColor best, c;
367 if( !warn++ )
369 WARN(palette, "Not enough colors for the full system palette.\n");
371 bp = BlackPixel(display, DefaultScreen(display));
372 wp = WhitePixel(display, DefaultScreen(display));
374 max = (0xffffffff)>>(32 - MONITOR_GetDepth(&MONITOR_PrimaryMonitor));
375 if( max > 256 )
377 step = max/256;
378 max = 256;
382 /* reinit color (XAllocColor() may change it)
383 * and map to the best shared colorcell */
385 color.red = __sysPalTemplate[i].peRed * 65535 / 255;
386 color.green = __sysPalTemplate[i].peGreen * 65535 / 255;
387 color.blue = __sysPalTemplate[i].peBlue * 65535 / 255;
389 best.pixel = best.red = best.green = best.blue = 0;
390 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
392 TSXQueryColor(display, cSpace.colorMap, &c);
393 r = (c.red - color.red)>>8;
394 g = (c.green - color.green)>>8;
395 b = (c.blue - color.blue)>>8;
396 r = r*r + g*g + b*b;
397 if( r < diff ) { best = c; diff = r; }
400 if( TSXAllocColor(display, cSpace.colorMap, &best) )
401 color.pixel = best.pixel;
402 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
405 sysPixel[i] = color.pixel;
407 TRACE(palette,"syscolor(%lx) -> pixel %i\n",
408 *(COLORREF*)(__sysPalTemplate+i), (int)color.pixel);
410 /* Set EGA mapping if color in the first or last eight */
412 if (i < 8)
413 COLOR_mapEGAPixel[i] = color.pixel;
414 else if (i >= NB_RESERVED_COLORS - 8 )
415 COLOR_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
418 /* now allocate changeable set */
420 if( !(cSpace.flags & COLOR_FIXED) )
422 int c_min = 0, c_max = cs->size, c_val;
424 TRACE(palette,"Dynamic colormap... \n");
426 /* comment this out if you want to debug palette init */
428 TSXGrabServer(display);
430 /* let's become the first client that actually follows
431 * X guidelines and does binary search...
434 pixDynMapping = (unsigned long*)xmalloc(sizeof(long)*cs->size);
435 while( c_max - c_min > 0 )
437 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
439 if( !TSXAllocColorCells(display, cs->colorMap, False,
440 plane_masks, 0, pixDynMapping, c_val) )
441 c_max = c_val - 1;
442 else
444 TSXFreeColors(display, cs->colorMap, pixDynMapping, c_val, 0);
445 c_min = c_val;
449 if( c_min > COLOR_max - NB_RESERVED_COLORS)
450 c_min = COLOR_max - NB_RESERVED_COLORS;
452 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
454 if( c_min > 0 )
455 if( !TSXAllocColorCells(display, cs->colorMap, False,
456 plane_masks, 0, pixDynMapping, c_min) )
458 WARN(palette,"Inexplicable failure during colorcell allocation.\n");
459 c_min = 0;
462 cs->size = c_min + NB_RESERVED_COLORS;
464 TSXUngrabServer(display);
466 TRACE(palette,"adjusted size %i colorcells\n", cs->size);
468 else if( cSpace.flags & COLOR_VIRTUAL )
470 /* virtual colorspace - ToPhysical takes care of
471 * color translations but we have to allocate full palette
472 * to maintain compatibility
474 cs->size = 256;
475 TRACE(palette,"Virtual colorspace - screendepth %i\n", MONITOR_GetDepth(&MONITOR_PrimaryMonitor));
477 else cs->size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
478 * of colors and map to them */
480 TRACE(palette,"Shared system palette uses %i colors.\n", cs->size);
482 /* set gap to account for pixel shortage. It has to be right in the center
483 * of the system palette because otherwise raster ops get screwed. */
485 if( cs->size >= 256 )
486 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
487 else
488 { COLOR_gapStart = cs->size/2; COLOR_gapEnd = 255 - cs->size/2; }
490 COLOR_firstFree = ( cs->size > NB_RESERVED_COLORS &&
491 (cs->flags & COLOR_VIRTUAL || !(cs->flags & COLOR_FIXED)) )
492 ? NB_RESERVED_COLORS/2 : -1;
494 COLOR_sysPal = (PALETTEENTRY*)xmalloc(sizeof(PALETTEENTRY)*256);
496 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
498 if( MONITOR_GetDepth(&MONITOR_PrimaryMonitor) <= 8 )
500 COLOR_PixelToPalette = (int*)xmalloc(sizeof(int)*256);
501 memset( COLOR_PixelToPalette, 0, 256*sizeof(int) );
504 /* for hicolor visuals PaletteToPixel mapping is used to skip
505 * RGB->pixel calculation in COLOR_ToPhysical().
508 COLOR_PaletteToPixel = (int*)xmalloc(sizeof(int)*256);
510 for( i = j = 0; i < 256; i++ )
512 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
514 COLOR_PaletteToPixel[i] = 0;
515 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
516 continue;
519 if( i < NB_RESERVED_COLORS/2 )
521 COLOR_PaletteToPixel[i] = sysPixel[i];
522 COLOR_sysPal[i] = __sysPalTemplate[i];
524 else if( i >= 256 - NB_RESERVED_COLORS/2 )
526 COLOR_PaletteToPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
527 COLOR_sysPal[i] = __sysPalTemplate[(i + NB_RESERVED_COLORS) - 256];
529 else if( pixDynMapping )
530 COLOR_PaletteToPixel[i] = pixDynMapping[j++];
531 else
532 COLOR_PaletteToPixel[i] = i;
534 TRACE(palette,"index %i -> pixel %i\n", i, COLOR_PaletteToPixel[i]);
536 if( COLOR_PixelToPalette )
537 COLOR_PixelToPalette[COLOR_PaletteToPixel[i]] = i;
540 if( pixDynMapping ) free(pixDynMapping);
542 return TRUE;
545 /***********************************************************************
546 * COLOR_Computeshifts
548 * Calculate conversion parameters for direct mapped visuals
550 static void COLOR_Computeshifts(unsigned long maskbits, int *shift, int *max)
552 int i;
554 if (maskbits==0)
556 *shift=0;
557 *max=0;
558 return;
561 for(i=0;!(maskbits&1);i++)
562 maskbits >>= 1;
564 *shift = i;
565 *max = maskbits;
569 /***********************************************************************
570 * COLOR_Init
572 * Initialize color management.
574 BOOL32 COLOR_Init(void)
576 int mask, white, black;
578 visual = DefaultVisual( display, DefaultScreen(display) );
580 TRACE(palette,"initializing palette manager...\n");
582 white = WhitePixelOfScreen( X11DRV_GetXScreen() );
583 black = BlackPixelOfScreen( X11DRV_GetXScreen() );
584 cSpace.monoPlane = 1;
585 for( mask = 1; !((white & mask)^(black & mask)); mask <<= 1 )
586 cSpace.monoPlane++;
587 cSpace.flags = (white & mask) ? COLOR_WHITESET : 0;
588 cSpace.size = visual->map_entries;
590 switch(visual->class)
592 case DirectColor:
593 cSpace.flags |= COLOR_VIRTUAL;
594 case GrayScale:
595 case PseudoColor:
596 if (Options.usePrivateMap)
598 XSetWindowAttributes win_attr;
600 cSpace.colorMap = TSXCreateColormap( display, X11DRV_GetXRootWindow(),
601 visual, AllocAll );
602 if (cSpace.colorMap)
604 cSpace.flags |= (COLOR_PRIVATE | COLOR_WHITESET);
606 cSpace.monoPlane = 1;
607 for( white = cSpace.size - 1; !(white & 1); white >>= 1 )
608 cSpace.monoPlane++;
610 if( X11DRV_GetXRootWindow() != DefaultRootWindow(display) )
612 win_attr.colormap = cSpace.colorMap;
613 TSXChangeWindowAttributes( display, X11DRV_GetXRootWindow(),
614 CWColormap, &win_attr );
616 break;
619 cSpace.colorMap = DefaultColormapOfScreen( X11DRV_GetXScreen() );
620 break;
622 case StaticGray:
623 cSpace.colorMap = DefaultColormapOfScreen( X11DRV_GetXScreen() );
624 cSpace.flags |= COLOR_FIXED;
625 COLOR_Graymax = (1<<MONITOR_GetDepth(&MONITOR_PrimaryMonitor))-1;
626 break;
628 case TrueColor:
629 cSpace.flags |= COLOR_VIRTUAL;
630 case StaticColor: {
631 int *depths,nrofdepths;
632 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
633 * depths 1 and 4
635 depths=TSXListDepths(display,DefaultScreen(display),&nrofdepths);
636 if ((nrofdepths==2) && ((depths[0]==4) || depths[1]==4)) {
637 cSpace.monoPlane = 1;
638 for( white = cSpace.size - 1; !(white & 1); white >>= 1 )
639 cSpace.monoPlane++;
640 cSpace.flags = (white & mask) ? COLOR_WHITESET : 0;
641 cSpace.colorMap = DefaultColormapOfScreen( X11DRV_GetXScreen() );
642 TSXFree(depths);
643 break;
645 TSXFree(depths);
646 cSpace.colorMap = DefaultColormapOfScreen( X11DRV_GetXScreen() );
647 cSpace.flags |= COLOR_FIXED;
648 COLOR_Computeshifts(visual->red_mask, &COLOR_Redshift, &COLOR_Redmax);
649 COLOR_Computeshifts(visual->green_mask, &COLOR_Greenshift, &COLOR_Greenmax);
650 COLOR_Computeshifts(visual->blue_mask, &COLOR_Blueshift, &COLOR_Bluemax);
651 break;
655 TRACE(palette," visual class %i (%i)\n",
656 visual->class, cSpace.monoPlane);
658 memset(COLOR_freeList, 0, 256*sizeof(unsigned char));
660 if (cSpace.flags & COLOR_PRIVATE)
661 COLOR_BuildPrivateMap( &cSpace );
662 else
663 COLOR_BuildSharedMap( &cSpace );
665 /* Build free list */
667 if( COLOR_firstFree != -1 )
668 COLOR_FormatSystemPalette();
670 COLOR_FillDefaultColors();
672 return TRUE;
675 /***********************************************************************
676 * COLOR_Cleanup
678 * Free external colors we grabbed in the FillDefaultPalette()
680 void COLOR_Cleanup(void)
682 if( COLOR_gapFilled )
683 TSXFreeColors(display, cSpace.colorMap,
684 (unsigned long*)(COLOR_PaletteToPixel + COLOR_gapStart),
685 COLOR_gapFilled, 0);
688 /***********************************************************************
689 * COLOR_IsSolid
691 * Check whether 'color' can be represented with a solid color.
693 BOOL32 COLOR_IsSolid( COLORREF color )
695 int i;
696 const PALETTEENTRY *pEntry = COLOR_sysPal;
698 if (color & 0xff000000) return TRUE; /* indexed color */
700 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
702 for (i = 0; i < 256 ; i++, pEntry++)
704 if( i < COLOR_gapStart || i > COLOR_gapEnd )
705 if ((GetRValue(color) == pEntry->peRed) &&
706 (GetGValue(color) == pEntry->peGreen) &&
707 (GetBValue(color) == pEntry->peBlue)) return TRUE;
709 return FALSE;
713 /***********************************************************************
714 * COLOR_PaletteLookupPixel
716 int COLOR_PaletteLookupPixel( PALETTEENTRY* palPalEntry, int size,
717 int* mapping, COLORREF col, BOOL32 skipReserved )
719 int i, best = 0, diff = 0x7fffffff;
720 int r,g,b;
722 for( i = 0; i < size && diff ; i++ )
724 if( !(palPalEntry[i].peFlags & PC_SYS_USED) ||
725 (skipReserved && palPalEntry[i].peFlags & PC_SYS_RESERVED) )
726 continue;
728 r = palPalEntry[i].peRed - GetRValue(col);
729 g = palPalEntry[i].peGreen - GetGValue(col);
730 b = palPalEntry[i].peBlue - GetBValue(col);
732 r = r*r + g*g + b*b;
734 if( r < diff ) { best = i; diff = r; }
736 return (mapping) ? mapping[best] : best;
739 /***********************************************************************
740 * COLOR_LookupSystemPixel
742 int COLOR_LookupSystemPixel(COLORREF col)
744 int i, best = 0, diff = 0x7fffffff;
745 int size = COLOR_GetSystemPaletteSize();
746 int r,g,b;
748 for( i = 0; i < size && diff ; i++ )
750 if( i == NB_RESERVED_COLORS/2 )
752 int newi = size - NB_RESERVED_COLORS/2;
753 if (newi>i) i=newi;
756 r = COLOR_sysPal[i].peRed - GetRValue(col);
757 g = COLOR_sysPal[i].peGreen - GetGValue(col);
758 b = COLOR_sysPal[i].peBlue - GetBValue(col);
760 r = r*r + g*g + b*b;
762 if( r < diff ) { best = i; diff = r; }
765 return (COLOR_PaletteToPixel)? COLOR_PaletteToPixel[best] : best;
768 /***********************************************************************
769 * COLOR_PaletteLookupExactIndex
771 int COLOR_PaletteLookupExactIndex( PALETTEENTRY* palPalEntry, int size,
772 COLORREF col )
774 int i;
775 BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col);
776 for( i = 0; i < size; i++ )
778 if( palPalEntry[i].peFlags & PC_SYS_USED ) /* skips gap */
779 if( palPalEntry[i].peRed == r &&
780 palPalEntry[i].peGreen == g &&
781 palPalEntry[i].peBlue == b )
782 return i;
784 return -1;
787 /***********************************************************************
788 * COLOR_LookupNearestColor
790 COLORREF COLOR_LookupNearestColor( PALETTEENTRY* palPalEntry, int size, COLORREF color )
792 unsigned char spec_type = color >> 24;
793 int i;
795 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
797 if( spec_type == 2 ) /* PALETTERGB */
798 color = *(COLORREF*)
799 (palPalEntry + COLOR_PaletteLookupPixel(palPalEntry,size,NULL,color,FALSE));
801 else if( spec_type == 1 ) /* PALETTEINDEX */
803 if( (i = color & 0x0000ffff) >= size )
805 WARN(palette, "RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, i);
806 color = *(COLORREF*)palPalEntry;
808 else color = *(COLORREF*)(palPalEntry + i);
811 color &= 0x00ffffff;
812 return (0x00ffffff & *(COLORREF*)
813 (COLOR_sysPal + COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL, color, FALSE)));
816 /***********************************************************************
817 * COLOR_ToLogical
819 * Return RGB color for given X pixel.
821 COLORREF COLOR_ToLogical(int pixel)
823 XColor color;
825 #if 0
826 /* truecolor visual */
828 if (MONITOR_GetDepth(&MONITOR_PrimaryMonitor) >= 24) return pixel;
829 #endif
831 /* check for hicolor visuals first */
833 if ( cSpace.flags & COLOR_FIXED && !COLOR_Graymax )
835 color.red = (pixel >> COLOR_Redshift) & COLOR_Redmax;
836 color.green = (pixel >> COLOR_Greenshift) & COLOR_Greenmax;
837 color.blue = (pixel >> COLOR_Blueshift) & COLOR_Bluemax;
838 return RGB((color.red * 255)/COLOR_Redmax,
839 (color.green * 255)/COLOR_Greenmax,
840 (color.blue * 255)/COLOR_Bluemax);
843 /* check if we can bypass X */
845 if ((MONITOR_GetDepth(&MONITOR_PrimaryMonitor) <= 8) && (pixel < 256) &&
846 !(cSpace.flags & (COLOR_VIRTUAL | COLOR_FIXED)) )
847 return ( *(COLORREF*)(COLOR_sysPal +
848 ((COLOR_PixelToPalette)?COLOR_PixelToPalette[pixel]:pixel)) ) & 0x00ffffff;
850 color.pixel = pixel;
851 TSXQueryColor(display, cSpace.colorMap, &color);
852 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
856 /***********************************************************************
857 * COLOR_ToPhysical
859 * Return the physical color closest to 'color'.
861 int COLOR_ToPhysical( DC *dc, COLORREF color )
863 WORD index = 0;
864 HPALETTE16 hPal = (dc)? dc->w.hPalette: STOCK_DEFAULT_PALETTE;
865 unsigned char spec_type = color >> 24;
866 PALETTEOBJ* palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
868 if ( cSpace.flags & COLOR_FIXED )
870 /* there is no colormap limitation; we are going to have to compute
871 * the pixel value from the visual information stored earlier
874 unsigned long red, green, blue;
875 unsigned idx = 0;
877 switch(spec_type)
879 case 2: /* PALETTERGB - not sure if we really need to search palette */
881 idx = COLOR_PaletteLookupPixel( palPtr->logpalette.palPalEntry,
882 palPtr->logpalette.palNumEntries,
883 NULL, color, FALSE);
885 if( palPtr->mapping )
887 GDI_HEAP_UNLOCK( hPal );
888 return palPtr->mapping[idx];
891 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
892 break;
894 case 1: /* PALETTEINDEX */
896 if( (idx = color & 0xffff) >= palPtr->logpalette.palNumEntries)
898 WARN(palette, "RGB(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
899 GDI_HEAP_UNLOCK( hPal );
900 return 0;
903 if( palPtr->mapping )
905 GDI_HEAP_UNLOCK( hPal );
906 return palPtr->mapping[idx];
908 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
909 break;
911 default:
912 color &= 0xffffff;
913 /* fall through to RGB */
915 case 0: /* RGB */
916 if( dc && (dc->w.bitsPerPixel == 1) )
918 GDI_HEAP_UNLOCK( hPal );
919 return (((color >> 16) & 0xff) +
920 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
925 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
927 if (COLOR_Graymax)
929 /* grayscale only; return scaled value */
930 GDI_HEAP_UNLOCK( hPal );
931 return ( (red * 30 + green * 69 + blue * 11) * COLOR_Graymax) / 25500;
933 else
935 /* scale each individually and construct the TrueColor pixel value */
936 if (COLOR_Redmax != 255) red = (red * COLOR_Redmax) / 255;
937 if (COLOR_Greenmax != 255) green = (green * COLOR_Greenmax) / 255;
938 if (COLOR_Bluemax != 255) blue = (blue * COLOR_Bluemax) / 255;
940 GDI_HEAP_UNLOCK( hPal );
941 return (red << COLOR_Redshift) | (green << COLOR_Greenshift) | (blue << COLOR_Blueshift);
944 else
947 /* palPtr can be NULL when DC is being destroyed */
949 if( !palPtr ) return 0;
950 else if( !palPtr->mapping )
951 WARN(palette, "Palette %04x is not realized\n", dc->w.hPalette);
953 switch(spec_type) /* we have to peruse DC and system palette */
955 default:
956 color &= 0xffffff;
957 /* fall through to RGB */
959 case 0: /* RGB */
960 if( dc && (dc->w.bitsPerPixel == 1) )
962 GDI_HEAP_UNLOCK( hPal );
963 return (((color >> 16) & 0xff) +
964 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
967 index = COLOR_PaletteLookupPixel( COLOR_sysPal, 256,
968 COLOR_PaletteToPixel, color, FALSE);
970 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
972 break;
973 case 1: /* PALETTEINDEX */
974 index = color & 0xffff;
976 if( index >= palPtr->logpalette.palNumEntries )
977 WARN(palette, "RGB(%lx) : index %i is out of bounds\n", color, index);
978 else if( palPtr->mapping ) index = palPtr->mapping[index];
980 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
982 break;
983 case 2: /* PALETTERGB */
984 index = COLOR_PaletteLookupPixel( palPtr->logpalette.palPalEntry,
985 palPtr->logpalette.palNumEntries,
986 palPtr->mapping, color, FALSE);
987 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
989 break;
993 GDI_HEAP_UNLOCK( hPal );
994 return index;
997 /***********************************************************************
998 * COLOR_SetMapping
1000 * Set the color-mapping table for selected palette.
1001 * Return number of entries which mapping has changed.
1003 int COLOR_SetMapping( PALETTEOBJ* palPtr, UINT32 uStart, UINT32 uNum, BOOL32 mapOnly )
1005 char flag;
1006 int prevMapping = (palPtr->mapping) ? 1 : 0;
1007 int index, iRemapped = 0;
1009 /* reset dynamic system palette entries */
1011 if( !mapOnly && COLOR_firstFree != -1)
1012 COLOR_FormatSystemPalette();
1014 /* initialize palette mapping table */
1016 palPtr->mapping = (int*)xrealloc(palPtr->mapping, sizeof(int)*
1017 palPtr->logpalette.palNumEntries);
1019 for( uNum += uStart; uStart < uNum; uStart++ )
1021 index = -1;
1022 flag = PC_SYS_USED;
1024 switch( palPtr->logpalette.palPalEntry[uStart].peFlags & 0x07 )
1026 case PC_EXPLICIT: /* palette entries are indices into system palette */
1027 index = *(WORD*)(palPtr->logpalette.palPalEntry + uStart);
1028 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1030 WARN(palette,"PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1031 index = 0;
1033 break;
1035 case PC_RESERVED: /* forbid future mappings to this entry */
1036 flag |= PC_SYS_RESERVED;
1038 /* fall through */
1039 default: /* try to collapse identical colors */
1040 index = COLOR_PaletteLookupExactIndex(COLOR_sysPal, 256,
1041 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1042 /* fall through */
1043 case PC_NOCOLLAPSE:
1044 if( index < 0 )
1046 if( COLOR_firstFree > 0 && !(cSpace.flags & COLOR_FIXED) )
1048 XColor color;
1049 index = COLOR_firstFree; /* ought to be available */
1050 COLOR_firstFree = COLOR_freeList[index];
1052 color.pixel = (COLOR_PaletteToPixel) ? COLOR_PaletteToPixel[index] : index;
1053 color.red = palPtr->logpalette.palPalEntry[uStart].peRed << 8;
1054 color.green = palPtr->logpalette.palPalEntry[uStart].peGreen << 8;
1055 color.blue = palPtr->logpalette.palPalEntry[uStart].peBlue << 8;
1056 color.flags = DoRed | DoGreen | DoBlue;
1057 TSXStoreColor(display, cSpace.colorMap, &color);
1059 COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[uStart];
1060 COLOR_sysPal[index].peFlags = flag;
1061 COLOR_freeList[index] = 0;
1063 if( COLOR_PaletteToPixel ) index = COLOR_PaletteToPixel[index];
1064 break;
1066 else if ( cSpace.flags & COLOR_VIRTUAL )
1068 index = COLOR_ToPhysical( NULL, 0x00ffffff &
1069 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1070 break;
1073 /* we have to map to existing entry in the system palette */
1075 index = COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL,
1076 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), TRUE);
1078 palPtr->logpalette.palPalEntry[uStart].peFlags |= PC_SYS_USED;
1080 if( COLOR_PaletteToPixel ) index = COLOR_PaletteToPixel[index];
1081 break;
1084 if( !prevMapping || palPtr->mapping[uStart] != index ) iRemapped++;
1085 palPtr->mapping[uStart] = index;
1087 TRACE(palette,"entry %i (%lx) -> pixel %i\n", uStart,
1088 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), index);
1091 return iRemapped;