Release 970914
[wine.git] / objects / color.c
blobb07b790e99fdd547f2a91c386e9f60f0c6def18b
1 /*
2 * Color functions
4 * Copyright 1993 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
6 */
8 #include <stdlib.h>
9 #include <X11/Xlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "windows.h"
13 #include "options.h"
14 #include "gdi.h"
15 #include "color.h"
16 #include "stddebug.h"
17 #include "debug.h"
18 #include "xmalloc.h"
20 /* Palette indexed mode:
22 * logical palette -> mapping -> pixel
25 * Windows needs contiguous color space ( from 0 to n ) but
26 * it is possible only with the private colormap. Otherwise we
27 * have to map DC palette indices to real pixel values. With
28 * private colormaps it boils down to the identity mapping. The
29 * other special case is when we have a fixed color visual with
30 * the screendepth > 8 - we abandon palette mappings altogether
31 * because pixel values can be calculated without X server
32 * assistance.
34 * Windows palette manager is described in the
35 * http://premium.microsoft.com/msdn/library/techart/f30/f34/f40/d4d/sa942.htm
38 typedef struct
40 Colormap colorMap;
41 UINT16 size;
42 UINT16 flags;
43 INT32 monoPlane; /* bit plane different for white and black pixels */
45 INT32 (*mapColor)( DC*, COLORREF );
46 } CSPACE;
48 static CSPACE cSpace = {0, 0, 0};
50 static int COLOR_Redshift = 0; /* to handle abortive COLOR_VIRTUAL visuals */
51 static int COLOR_Redmax = 0;
52 static int COLOR_Greenshift = 0;
53 static int COLOR_Greenmax = 0;
54 static int COLOR_Blueshift = 0;
55 static int COLOR_Bluemax = 0;
56 static int COLOR_Graymax = 0;
58 /* System color space.
60 * First 10 and last 10 colors in COLOR_sysPalette are
61 * "guarded". RealizePalette changes only the rest of colorcells. For
62 * currently inactive window it changes only DC palette mappings.
65 #define NB_COLORCUBE_START_INDEX 63
67 Visual* visual = NULL;
69 static PALETTEENTRY* COLOR_sysPal = NULL; /* current system palette */
70 static int COLOR_gapStart = 256;
71 static int COLOR_gapEnd = -1;
72 static int COLOR_gapFilled = 0;
73 static int COLOR_max = 256;
75 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
76 static int COLOR_firstFree = 0;
77 static unsigned char COLOR_freeList[256];
79 /* Maps entry in the system palette to X pixel value */
80 int* COLOR_PaletteToPixel = NULL;
82 /* Maps pixel to the entry in the system palette */
83 int* COLOR_PixelToPalette = NULL;
85 static const PALETTEENTRY __sysPalTemplate[NB_RESERVED_COLORS] =
87 /* first 10 entries in the system palette */
88 /* red green blue flags */
89 { 0x00, 0x00, 0x00, PC_SYS_USED },
90 { 0x80, 0x00, 0x00, PC_SYS_USED },
91 { 0x00, 0x80, 0x00, PC_SYS_USED },
92 { 0x80, 0x80, 0x00, PC_SYS_USED },
93 { 0x00, 0x00, 0x80, PC_SYS_USED },
94 { 0x80, 0x00, 0x80, PC_SYS_USED },
95 { 0x00, 0x80, 0x80, PC_SYS_USED },
96 { 0xc0, 0xc0, 0xc0, PC_SYS_USED },
97 { 0xc0, 0xdc, 0xc0, PC_SYS_USED },
98 { 0xa6, 0xca, 0xf0, PC_SYS_USED },
100 /* ... c_min/2 dynamic colorcells */
102 /* ... gap (for sparse palettes) */
104 /* ... c_min/2 dynamic colorcells */
106 { 0xff, 0xfb, 0xf0, PC_SYS_USED },
107 { 0xa0, 0xa0, 0xa4, PC_SYS_USED },
108 { 0x80, 0x80, 0x80, PC_SYS_USED },
109 { 0xff, 0x00, 0x00, PC_SYS_USED },
110 { 0x00, 0xff, 0x00, PC_SYS_USED },
111 { 0xff, 0xff, 0x00, PC_SYS_USED },
112 { 0x00, 0x00, 0xff, PC_SYS_USED },
113 { 0xff, 0x00, 0xff, PC_SYS_USED },
114 { 0x00, 0xff, 0xff, PC_SYS_USED },
115 { 0xff, 0xff, 0xff, PC_SYS_USED } /* last 10 */
118 /* Map an EGA index (0..15) to a pixel value in the system color space. */
120 int COLOR_mapEGAPixel[16];
122 /***********************************************************************
123 * Misc auxiliary functions
125 Colormap COLOR_GetColormap(void)
127 return cSpace.colorMap;
130 BOOL32 COLOR_GetMonoPlane(INT32* plane)
132 *plane = cSpace.monoPlane;
133 return (cSpace.flags & COLOR_WHITESET) ? TRUE : FALSE;
136 UINT16 COLOR_GetSystemPaletteSize(void)
138 return (cSpace.flags & COLOR_PRIVATE) ? cSpace.size : 256;
141 UINT16 COLOR_GetSystemPaletteFlags(void)
143 return cSpace.flags;
146 const PALETTEENTRY* COLOR_GetSystemPaletteTemplate(void)
148 return __sysPalTemplate;
151 COLORREF COLOR_GetSystemPaletteEntry(UINT32 i)
153 return *(COLORREF*)(COLOR_sysPal + i) & 0x00ffffff;
156 void COLOR_FormatSystemPalette(void)
158 /* Build free list so we'd have an easy way to find
159 * out if there are any available colorcells.
162 int i, j = COLOR_firstFree = NB_RESERVED_COLORS/2;
164 COLOR_sysPal[j].peFlags = 0;
165 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
166 if( i < COLOR_gapStart || i > COLOR_gapEnd )
168 COLOR_sysPal[i].peFlags = 0; /* unused tag */
169 COLOR_freeList[j] = i; /* next */
170 j = i;
172 COLOR_freeList[j] = 0;
175 BOOL32 COLOR_CheckSysColor(COLORREF c)
177 int i;
178 for( i = 0; i < NB_RESERVED_COLORS; i++ )
179 if( c == (*(COLORREF*)(__sysPalTemplate + i) & 0x00ffffff) )
180 return 0;
181 return 1;
184 /***********************************************************************
185 * Colormap Initialization
187 static void COLOR_FillDefaultColors(void)
189 /* initialize unused entries to what Windows uses as a color
190 * cube - based on Greg Kreider's code.
193 int i = 0, idx = 0;
194 int red, no_r, inc_r;
195 int green, no_g, inc_g;
196 int blue, no_b, inc_b;
198 if (cSpace.size <= NB_RESERVED_COLORS)
199 return;
200 while (i*i*i < (cSpace.size - NB_RESERVED_COLORS)) i++;
201 no_r = no_g = no_b = --i;
202 if ((no_r * (no_g+1) * no_b) < (cSpace.size - NB_RESERVED_COLORS)) no_g++;
203 if ((no_r * no_g * (no_b+1)) < (cSpace.size - NB_RESERVED_COLORS)) no_b++;
204 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
205 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
206 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
208 idx = COLOR_firstFree;
210 if( idx != -1 )
211 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
212 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
213 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
215 /* weird but true */
217 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
219 COLOR_sysPal[idx].peRed = red;
220 COLOR_sysPal[idx].peGreen = green;
221 COLOR_sysPal[idx].peBlue = blue;
223 /* set X color */
225 if( cSpace.flags & COLOR_VIRTUAL )
227 if (COLOR_Redmax != 255) no_r = (red * COLOR_Redmax) / 255;
228 if (COLOR_Greenmax != 255) no_g = (green * COLOR_Greenmax) / 255;
229 if (COLOR_Bluemax != 255) no_b = (blue * COLOR_Bluemax) / 255;
231 COLOR_PaletteToPixel[idx] = (no_r << COLOR_Redshift) | (no_g << COLOR_Greenshift) | (no_b << COLOR_Blueshift);
233 else if( !(cSpace.flags & COLOR_FIXED) )
235 XColor color = { color.pixel = (COLOR_PaletteToPixel)? COLOR_PaletteToPixel[idx] : idx ,
236 COLOR_sysPal[idx].peRed << 8,
237 COLOR_sysPal[idx].peGreen << 8,
238 COLOR_sysPal[idx].peGreen << 8,
239 (DoRed | DoGreen | DoBlue) };
240 XStoreColor(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 XQueryColor(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 XAllocColor(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 dprintf_palette(stddeb,"\tbuilding 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 XStoreColor(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;
331 return FALSE;
334 static BOOL32 COLOR_BuildSharedMap(CSPACE* cs)
336 XColor color;
337 unsigned long sysPixel[NB_RESERVED_COLORS];
338 unsigned long* pixDynMapping = NULL;
339 unsigned long plane_masks[1];
340 int i, j, warn = 0;
341 int diff, r, g, b, max = 256, bp = 0, wp = 1;
342 int step = 1;
344 /* read "AllocSystemColors" from wine.conf */
346 COLOR_max = PROFILE_GetWineIniInt( "options", "AllocSystemColors", 256);
347 if (COLOR_max > 256) COLOR_max = 256;
348 else if (COLOR_max < 20) COLOR_max = 20;
349 dprintf_palette(stddeb,"COLOR_Init: %d colors configured.\n", COLOR_max);
351 dprintf_palette(stddeb,"\tbuilding shared map - %i palette entries\n", cs->size);
353 /* Be nice and allocate system colors as read-only */
355 for( i = 0; i < NB_RESERVED_COLORS; i++ )
357 color.red = __sysPalTemplate[i].peRed * 65535 / 255;
358 color.green = __sysPalTemplate[i].peGreen * 65535 / 255;
359 color.blue = __sysPalTemplate[i].peBlue * 65535 / 255;
360 color.flags = DoRed | DoGreen | DoBlue;
362 if (!XAllocColor( display, cSpace.colorMap, &color ))
364 XColor best, c;
366 if( !warn++ )
368 dprintf_palette(stddeb, "Not enough colors for the full system palette.\n");
370 bp = BlackPixel(display, DefaultScreen(display));
371 wp = WhitePixel(display, DefaultScreen(display));
373 max = (0xffffffff)>>(32 - screenDepth);
374 if( max > 256 )
376 step = max/256;
377 max = 256;
381 /* reinit color (XAllocColor() may change it)
382 * and map to the best shared colorcell */
384 color.red = __sysPalTemplate[i].peRed * 65535 / 255;
385 color.green = __sysPalTemplate[i].peGreen * 65535 / 255;
386 color.blue = __sysPalTemplate[i].peBlue * 65535 / 255;
388 best.pixel = best.red = best.green = best.blue = 0;
389 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
391 XQueryColor(display, cSpace.colorMap, &c);
392 r = (c.red - color.red)>>8;
393 g = (c.green - color.green)>>8;
394 b = (c.blue - color.blue)>>8;
395 r = r*r + g*g + b*b;
396 if( r < diff ) { best = c; diff = r; }
399 if( XAllocColor(display, cSpace.colorMap, &best) )
400 color.pixel = best.pixel;
401 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
404 sysPixel[i] = color.pixel;
406 dprintf_palette(stddeb,"\tsyscolor(%lx) -> pixel %i\n",
407 *(COLORREF*)(__sysPalTemplate+i), (int)color.pixel);
409 /* Set EGA mapping if color in the first or last eight */
411 if (i < 8)
412 COLOR_mapEGAPixel[i] = color.pixel;
413 else if (i >= NB_RESERVED_COLORS - 8 )
414 COLOR_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
417 /* now allocate changeable set */
419 if( !(cSpace.flags & COLOR_FIXED) )
421 int c_min = 0, c_max = cs->size, c_val;
423 dprintf_palette(stddeb,"\tdynamic colormap... ");
425 /* comment this out if you want to debug palette init */
427 XGrabServer(display);
429 /* let's become the first client that actually follows
430 * X guidelines and does binary search...
433 pixDynMapping = (unsigned long*)xmalloc(sizeof(long)*cs->size);
434 while( c_max - c_min > 0 )
436 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
438 if( !XAllocColorCells(display, cs->colorMap, False,
439 plane_masks, 0, pixDynMapping, c_val) )
440 c_max = c_val - 1;
441 else
443 XFreeColors(display, cs->colorMap, pixDynMapping, c_val, 0);
444 c_min = c_val;
448 if( c_min > COLOR_max - NB_RESERVED_COLORS)
449 c_min = COLOR_max - NB_RESERVED_COLORS;
451 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
453 if( c_min > 0 )
454 if( !XAllocColorCells(display, cs->colorMap, False,
455 plane_masks, 0, pixDynMapping, c_min) )
457 fprintf(stderr,"Inexplicable failure during colorcell allocation.\n");
458 c_min = 0;
461 cs->size = c_min + NB_RESERVED_COLORS;
463 XUngrabServer(display);
465 dprintf_palette(stddeb,"adjusted size %i colorcells\n", cs->size);
467 else if( cSpace.flags & COLOR_VIRTUAL )
469 /* virtual colorspace - ToPhysical takes care of
470 * color translations but we have to allocate full palette
471 * to maintain compatibility
473 cs->size = 256;
474 dprintf_palette(stddeb,"\tvirtual colorspace - screendepth %i\n", screenDepth);
476 else cs->size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
477 * of colors and map to them */
479 dprintf_palette(stddeb,"Shared system palette uses %i colors.\n", cs->size);
481 /* set gap to account for pixel shortage. It has to be right in the center
482 * of the system palette because otherwise raster ops get screwed. */
484 if( cs->size >= 256 )
485 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
486 else
487 { COLOR_gapStart = cs->size/2; COLOR_gapEnd = 255 - cs->size/2; }
489 COLOR_firstFree = ( cs->size > NB_RESERVED_COLORS &&
490 (cs->flags & COLOR_VIRTUAL || !(cs->flags & COLOR_FIXED)) )
491 ? NB_RESERVED_COLORS/2 : -1;
493 COLOR_sysPal = (PALETTEENTRY*)xmalloc(sizeof(PALETTEENTRY)*256);
495 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
497 if( screenDepth <= 8 )
499 COLOR_PixelToPalette = (int*)xmalloc(sizeof(int)*256);
500 memset( COLOR_PixelToPalette, 0, 256*sizeof(int) );
503 /* for hicolor visuals PaletteToPixel mapping is used to skip
504 * RGB->pixel calculation in COLOR_ToPhysical().
507 COLOR_PaletteToPixel = (int*)xmalloc(sizeof(int)*256);
509 for( i = j = 0; i < 256; i++ )
511 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
513 COLOR_PaletteToPixel[i] = 0;
514 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
515 continue;
518 if( i < NB_RESERVED_COLORS/2 )
520 COLOR_PaletteToPixel[i] = sysPixel[i];
521 COLOR_sysPal[i] = __sysPalTemplate[i];
523 else if( i >= 256 - NB_RESERVED_COLORS/2 )
525 COLOR_PaletteToPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
526 COLOR_sysPal[i] = __sysPalTemplate[(i + NB_RESERVED_COLORS) - 256];
528 else if( pixDynMapping )
529 COLOR_PaletteToPixel[i] = pixDynMapping[j++];
530 else
531 COLOR_PaletteToPixel[i] = i;
533 dprintf_palette(stddeb,"\tindex %i -> pixel %i\n", i, COLOR_PaletteToPixel[i]);
535 if( COLOR_PixelToPalette )
536 COLOR_PixelToPalette[COLOR_PaletteToPixel[i]] = i;
539 if( pixDynMapping ) free(pixDynMapping);
540 return TRUE;
543 /***********************************************************************
544 * COLOR_Computeshifts
546 * Calculate conversion parameters for direct mapped visuals
548 static void COLOR_Computeshifts(unsigned long maskbits, int *shift, int *max)
550 int i;
552 if (maskbits==0)
554 *shift=0;
555 *max=0;
556 return;
559 for(i=0;!(maskbits&1);i++)
560 maskbits >>= 1;
562 *shift = i;
563 *max = maskbits;
567 /***********************************************************************
568 * COLOR_Init
570 * Initialize color management.
572 BOOL32 COLOR_Init(void)
574 int mask, white, black;
576 visual = DefaultVisual( display, DefaultScreen(display) );
578 dprintf_palette(stddeb,"COLOR_Init: initializing palette manager...");
580 white = WhitePixelOfScreen( screen );
581 black = BlackPixelOfScreen( screen );
582 cSpace.monoPlane = 1;
583 for( mask = 1; !((white & mask)^(black & mask)); mask <<= 1 )
584 cSpace.monoPlane++;
585 cSpace.flags = (white & mask) ? COLOR_WHITESET : 0;
586 cSpace.size = visual->map_entries;
588 switch(visual->class)
590 case DirectColor:
591 cSpace.flags |= COLOR_VIRTUAL;
592 case GrayScale:
593 case PseudoColor:
594 if (Options.usePrivateMap)
596 XSetWindowAttributes win_attr;
598 cSpace.colorMap = XCreateColormap( display, rootWindow,
599 visual, AllocAll );
600 if (cSpace.colorMap)
602 cSpace.flags |= (COLOR_PRIVATE | COLOR_WHITESET);
604 cSpace.monoPlane = 1;
605 for( white = cSpace.size - 1; !(white & 1); white >>= 1 )
606 cSpace.monoPlane++;
608 if( rootWindow != DefaultRootWindow(display) )
610 win_attr.colormap = cSpace.colorMap;
611 XChangeWindowAttributes( display, rootWindow,
612 CWColormap, &win_attr );
614 break;
617 cSpace.colorMap = DefaultColormapOfScreen( screen );
618 break;
620 case StaticGray:
621 cSpace.colorMap = DefaultColormapOfScreen( screen );
622 cSpace.flags |= COLOR_FIXED;
623 COLOR_Graymax = (1<<screenDepth)-1;
624 break;
626 case TrueColor:
627 cSpace.flags |= COLOR_VIRTUAL;
628 case StaticColor: {
629 int *depths,nrofdepths;
630 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
631 * depths 1 and 4
633 depths=XListDepths(display,DefaultScreen(display),&nrofdepths);
634 if ((nrofdepths==2) && ((depths[0]==4) || depths[1]==4)) {
635 cSpace.monoPlane = 1;
636 for( white = cSpace.size - 1; !(white & 1); white >>= 1 )
637 cSpace.monoPlane++;
638 cSpace.flags = (white & mask) ? COLOR_WHITESET : 0;
639 cSpace.colorMap = DefaultColormapOfScreen( screen );
640 XFree(depths);
641 break;
643 XFree(depths);
644 cSpace.colorMap = DefaultColormapOfScreen( screen );
645 cSpace.flags |= COLOR_FIXED;
646 COLOR_Computeshifts(visual->red_mask, &COLOR_Redshift, &COLOR_Redmax);
647 COLOR_Computeshifts(visual->green_mask, &COLOR_Greenshift, &COLOR_Greenmax);
648 COLOR_Computeshifts(visual->blue_mask, &COLOR_Blueshift, &COLOR_Bluemax);
649 break;
653 dprintf_palette(stddeb," visual class %i (%i)\n",
654 visual->class, cSpace.monoPlane);
656 memset(COLOR_freeList, 0, 256*sizeof(unsigned char));
658 if (cSpace.flags & COLOR_PRIVATE)
659 COLOR_BuildPrivateMap( &cSpace );
660 else
661 COLOR_BuildSharedMap( &cSpace );
663 /* Build free list */
665 if( COLOR_firstFree != -1 )
666 COLOR_FormatSystemPalette();
668 COLOR_FillDefaultColors();
670 return TRUE;
673 /***********************************************************************
674 * COLOR_Cleanup
676 * Free external colors we grabbed in the FillDefaultPalette()
678 void COLOR_Cleanup(void)
680 if( COLOR_gapFilled )
681 XFreeColors(display, cSpace.colorMap,
682 (unsigned long*)(COLOR_PaletteToPixel + COLOR_gapStart),
683 COLOR_gapFilled, 0);
686 /***********************************************************************
687 * COLOR_IsSolid
689 * Check whether 'color' can be represented with a solid color.
691 BOOL32 COLOR_IsSolid( COLORREF color )
693 int i;
694 const PALETTEENTRY *pEntry = COLOR_sysPal;
696 if (color & 0xff000000) return TRUE; /* indexed color */
698 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
700 for (i = 0; i < 256 ; i++, pEntry++)
702 if( i < COLOR_gapStart || i > COLOR_gapEnd )
703 if ((GetRValue(color) == pEntry->peRed) &&
704 (GetGValue(color) == pEntry->peGreen) &&
705 (GetBValue(color) == pEntry->peBlue)) return TRUE;
707 return FALSE;
711 /***********************************************************************
712 * COLOR_PaletteLookupPixel
714 int COLOR_PaletteLookupPixel( PALETTEENTRY* palPalEntry, int size,
715 int* mapping, COLORREF col, BOOL32 skipReserved )
717 int i, best = 0, diff = 0x7fffffff;
718 int r,g,b;
720 for( i = 0; i < size && diff ; i++ )
722 if( !(palPalEntry[i].peFlags & PC_SYS_USED) ||
723 (skipReserved && palPalEntry[i].peFlags & PC_SYS_RESERVED) )
724 continue;
726 r = palPalEntry[i].peRed - GetRValue(col);
727 g = palPalEntry[i].peGreen - GetGValue(col);
728 b = palPalEntry[i].peBlue - GetBValue(col);
730 r = r*r + g*g + b*b;
732 if( r < diff ) { best = i; diff = r; }
734 return (mapping) ? mapping[best] : best;
737 /***********************************************************************
738 * COLOR_LookupSystemPixel
740 int COLOR_LookupSystemPixel(COLORREF col)
742 int i, best = 0, diff = 0x7fffffff;
743 int size = COLOR_GetSystemPaletteSize();
744 int r,g,b;
746 for( i = 0; i < size && diff ; i++ )
748 if( i == NB_RESERVED_COLORS/2 )
750 int newi = size - NB_RESERVED_COLORS/2;
751 if (newi>i) i=newi;
754 r = COLOR_sysPal[i].peRed - GetRValue(col);
755 g = COLOR_sysPal[i].peGreen - GetGValue(col);
756 b = COLOR_sysPal[i].peBlue - GetBValue(col);
758 r = r*r + g*g + b*b;
760 if( r < diff ) { best = i; diff = r; }
763 return (COLOR_PaletteToPixel)? COLOR_PaletteToPixel[best] : best;
766 /***********************************************************************
767 * COLOR_PaletteLookupExactIndex
769 int COLOR_PaletteLookupExactIndex( PALETTEENTRY* palPalEntry, int size,
770 COLORREF col )
772 int i;
773 BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col);
774 for( i = 0; i < size; i++ )
776 if( palPalEntry[i].peFlags & PC_SYS_USED ) /* skips gap */
777 if( palPalEntry[i].peRed == r &&
778 palPalEntry[i].peGreen == g &&
779 palPalEntry[i].peBlue == b )
780 return i;
782 return -1;
785 /***********************************************************************
786 * COLOR_LookupNearestColor
788 COLORREF COLOR_LookupNearestColor( PALETTEENTRY* palPalEntry, int size, COLORREF color )
790 unsigned char spec_type = color >> 24;
791 int i;
793 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
795 if( spec_type == 2 ) /* PALETTERGB */
796 color = *(COLORREF*)
797 (palPalEntry + COLOR_PaletteLookupPixel(palPalEntry,size,NULL,color,FALSE));
799 else if( spec_type == 1 ) /* PALETTEINDEX */
800 if( (i = color & 0x0000ffff) >= size )
802 fprintf(stderr, "\tRGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, i);
803 color = *(COLORREF*)palPalEntry;
805 else color = *(COLORREF*)(palPalEntry + i);
807 color &= 0x00ffffff;
808 return (0x00ffffff & *(COLORREF*)
809 (COLOR_sysPal + COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL, color, FALSE)));
812 /***********************************************************************
813 * COLOR_ToLogical
815 * Return RGB color for given X pixel.
817 COLORREF COLOR_ToLogical(int pixel)
819 XColor color;
821 /* truecolor visual */
823 if (screenDepth >= 24) return pixel;
825 /* check for hicolor visuals first */
827 if ( cSpace.flags & COLOR_FIXED && !COLOR_Graymax )
829 color.red = (pixel >> COLOR_Redshift) & COLOR_Redmax;
830 color.green = (pixel >> COLOR_Greenshift) & COLOR_Greenmax;
831 color.blue = (pixel >> COLOR_Blueshift) & COLOR_Bluemax;
832 return RGB((color.red * 255)/COLOR_Redmax,
833 (color.green * 255)/COLOR_Greenmax,
834 (color.blue * 255)/COLOR_Bluemax);
837 /* check if we can bypass X */
839 if ((screenDepth <= 8) && (pixel < 256) &&
840 !(cSpace.flags & (COLOR_VIRTUAL | COLOR_FIXED)) )
841 return ( *(COLORREF*)(COLOR_sysPal +
842 ((COLOR_PixelToPalette)?COLOR_PixelToPalette[pixel]:pixel)) ) & 0x00ffffff;
844 color.pixel = pixel;
845 XQueryColor(display, cSpace.colorMap, &color);
846 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
850 /***********************************************************************
851 * COLOR_ToPhysical
853 * Return the physical color closest to 'color'.
855 int COLOR_ToPhysical( DC *dc, COLORREF color )
857 WORD index = 0;
858 HPALETTE16 hPal = (dc)? dc->w.hPalette: STOCK_DEFAULT_PALETTE;
859 unsigned char spec_type = color >> 24;
860 PALETTEOBJ* palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
862 if ( cSpace.flags & COLOR_FIXED )
864 /* there is no colormap limitation; we are going to have to compute
865 * the pixel value from the visual information stored earlier
868 unsigned long red, green, blue;
869 unsigned idx = 0;
871 switch(spec_type)
873 case 2: /* PALETTERGB - not sure if we really need to search palette */
875 idx = COLOR_PaletteLookupPixel( palPtr->logpalette.palPalEntry,
876 palPtr->logpalette.palNumEntries,
877 NULL, color, FALSE);
879 if( palPtr->mapping )
881 GDI_HEAP_UNLOCK( hPal );
882 return palPtr->mapping[idx];
885 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
886 break;
888 case 1: /* PALETTEINDEX */
890 if( (idx = color & 0xffff) >= palPtr->logpalette.palNumEntries)
892 fprintf(stderr, "\tRGB(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
893 GDI_HEAP_UNLOCK( hPal );
894 return 0;
897 if( palPtr->mapping )
899 GDI_HEAP_UNLOCK( hPal );
900 return palPtr->mapping[idx];
902 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
903 break;
905 default:
906 color &= 0xffffff;
907 /* fall through to RGB */
909 case 0: /* RGB */
910 if( dc && (dc->w.bitsPerPixel == 1) )
912 GDI_HEAP_UNLOCK( hPal );
913 return (((color >> 16) & 0xff) +
914 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
919 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
921 if (COLOR_Graymax)
923 /* grayscale only; return scaled value */
924 GDI_HEAP_UNLOCK( hPal );
925 return ( (red * 30 + green * 69 + blue * 11) * COLOR_Graymax) / 25500;
927 else
929 /* scale each individually and construct the TrueColor pixel value */
930 if (COLOR_Redmax != 255) red = (red * COLOR_Redmax) / 255;
931 if (COLOR_Greenmax != 255) green = (green * COLOR_Greenmax) / 255;
932 if (COLOR_Bluemax != 255) blue = (blue * COLOR_Bluemax) / 255;
934 GDI_HEAP_UNLOCK( hPal );
935 return (red << COLOR_Redshift) | (green << COLOR_Greenshift) | (blue << COLOR_Blueshift);
938 else
941 /* palPtr can be NULL when DC is being destroyed */
943 if( !palPtr ) return 0;
944 else if( !palPtr->mapping )
945 dprintf_palette(stddeb,"\tpalette %04x is not realized\n", dc->w.hPalette);
947 switch(spec_type) /* we have to peruse DC and system palette */
949 default:
950 color &= 0xffffff;
951 /* fall through to RGB */
953 case 0: /* RGB */
954 if( dc && (dc->w.bitsPerPixel == 1) )
956 GDI_HEAP_UNLOCK( hPal );
957 return (((color >> 16) & 0xff) +
958 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
961 index = COLOR_PaletteLookupPixel( COLOR_sysPal, 256,
962 COLOR_PaletteToPixel, color, FALSE);
964 /* dprintf_palette(stddeb,"\tRGB(%lx) -> pixel %i\n", color, index);
966 break;
967 case 1: /* PALETTEINDEX */
968 index = color & 0xffff;
970 if( index >= palPtr->logpalette.palNumEntries )
971 fprintf(stderr, "\tRGB(%lx) : index %i is out of bounds\n", color, index);
972 else if( palPtr->mapping ) index = palPtr->mapping[index];
974 /* dprintf_palette(stddeb,"\tPALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
976 break;
977 case 2: /* PALETTERGB */
978 index = COLOR_PaletteLookupPixel( palPtr->logpalette.palPalEntry,
979 palPtr->logpalette.palNumEntries,
980 palPtr->mapping, color, FALSE);
981 /* dprintf_palette(stddeb,"\tPALETTERGB(%lx) -> pixel %i\n", color, index);
983 break;
987 GDI_HEAP_UNLOCK( hPal );
988 return index;
991 /***********************************************************************
992 * COLOR_SetMapping
994 * Set the color-mapping table for selected palette.
995 * Return number of entries which mapping has changed.
997 int COLOR_SetMapping( PALETTEOBJ* palPtr, UINT32 uStart, UINT32 uNum, BOOL32 mapOnly )
999 char flag;
1000 int prevMapping = (palPtr->mapping) ? 1 : 0;
1001 int index, iRemapped = 0;
1003 /* reset dynamic system palette entries */
1005 if( !mapOnly && COLOR_firstFree != -1)
1006 COLOR_FormatSystemPalette();
1008 /* initialize palette mapping table */
1010 palPtr->mapping = (int*)xrealloc(palPtr->mapping, sizeof(int)*
1011 palPtr->logpalette.palNumEntries);
1013 for( uNum += uStart; uStart < uNum; uStart++ )
1015 index = -1;
1016 flag = PC_SYS_USED;
1018 switch( palPtr->logpalette.palPalEntry[uStart].peFlags & 0x07 )
1020 case PC_EXPLICIT: /* palette entries are indices into system palette */
1021 index = *(WORD*)(palPtr->logpalette.palPalEntry + uStart);
1022 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1024 fprintf(stderr,"PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1025 index = 0;
1027 break;
1029 case PC_RESERVED: /* forbid future mappings to this entry */
1030 flag |= PC_SYS_RESERVED;
1032 /* fall through */
1033 default: /* try to collapse identical colors */
1034 index = COLOR_PaletteLookupExactIndex(COLOR_sysPal, 256,
1035 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1036 /* fall through */
1037 case PC_NOCOLLAPSE:
1038 if( index < 0 )
1040 if( COLOR_firstFree > 0 && !(cSpace.flags & COLOR_FIXED) )
1042 XColor color;
1043 index = COLOR_firstFree; /* ought to be available */
1044 COLOR_firstFree = COLOR_freeList[index];
1046 color.pixel = (COLOR_PaletteToPixel) ? COLOR_PaletteToPixel[index] : index;
1047 color.red = palPtr->logpalette.palPalEntry[uStart].peRed << 8;
1048 color.green = palPtr->logpalette.palPalEntry[uStart].peGreen << 8;
1049 color.blue = palPtr->logpalette.palPalEntry[uStart].peBlue << 8;
1050 color.flags = DoRed | DoGreen | DoBlue;
1051 XStoreColor(display, cSpace.colorMap, &color);
1053 COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[uStart];
1054 COLOR_sysPal[index].peFlags = flag;
1055 COLOR_freeList[index] = 0;
1057 if( COLOR_PaletteToPixel ) index = COLOR_PaletteToPixel[index];
1058 break;
1060 else if ( cSpace.flags & COLOR_VIRTUAL )
1062 index = COLOR_ToPhysical( NULL, 0x00ffffff &
1063 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1064 break;
1067 /* we have to map to existing entry in the system palette */
1069 index = COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL,
1070 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), TRUE);
1072 palPtr->logpalette.palPalEntry[uStart].peFlags |= PC_SYS_USED;
1074 if( COLOR_PaletteToPixel ) index = COLOR_PaletteToPixel[index];
1075 break;
1078 if( !prevMapping || palPtr->mapping[uStart] != index ) iRemapped++;
1079 palPtr->mapping[uStart] = index;
1081 dprintf_palette(stddeb,"\tentry %i (%lx) -> pixel %i\n", uStart,
1082 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), index);
1085 return iRemapped;