4 * Copyright 1993 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
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
34 * Windows palette manager is described in the
35 * http://premium.microsoft.com/msdn/library/techart/f30/f34/f40/d4d/sa942.htm
43 INT32 monoPlane; /* bit plane different for white and black pixels */
45 INT32 (*mapColor)( DC*, COLORREF );
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)
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 */
172 COLOR_freeList[j] = 0;
175 BOOL32 COLOR_CheckSysColor(COLORREF c)
178 for( i = 0; i < NB_RESERVED_COLORS; i++ )
179 if( c == (*(COLORREF*)(__sysPalTemplate + i) & 0x00ffffff) )
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.
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)
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;
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 )
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;
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 )
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 )
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 */
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;
316 XStoreColor(display, cs->colorMap, &color);
318 /* Set EGA mapping if color is from the first or last eight */
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;
334 static BOOL32 COLOR_BuildSharedMap(CSPACE* cs)
337 unsigned long sysPixel[NB_RESERVED_COLORS];
338 unsigned long* pixDynMapping = NULL;
339 unsigned long plane_masks[1];
341 int diff, r, g, b, max = 256, bp = 0, wp = 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 ))
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);
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;
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 */
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) )
443 XFreeColors(display, cs->colorMap, pixDynMapping, c_val, 0);
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 */
454 if( !XAllocColorCells(display, cs->colorMap, False,
455 plane_masks, 0, pixDynMapping, c_min) )
457 fprintf(stderr,"Inexplicable failure during colorcell allocation.\n");
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
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; }
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 */
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++];
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);
543 /***********************************************************************
544 * COLOR_Computeshifts
546 * Calculate conversion parameters for direct mapped visuals
548 static void COLOR_Computeshifts(unsigned long maskbits, int *shift, int *max)
559 for(i=0;!(maskbits&1);i++)
567 /***********************************************************************
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 )
585 cSpace.flags = (white & mask) ? COLOR_WHITESET : 0;
586 cSpace.size = visual->map_entries;
588 switch(visual->class)
591 cSpace.flags |= COLOR_VIRTUAL;
594 if (Options.usePrivateMap)
596 XSetWindowAttributes win_attr;
598 cSpace.colorMap = XCreateColormap( display, rootWindow,
602 cSpace.flags |= (COLOR_PRIVATE | COLOR_WHITESET);
604 cSpace.monoPlane = 1;
605 for( white = cSpace.size - 1; !(white & 1); white >>= 1 )
608 if( rootWindow != DefaultRootWindow(display) )
610 win_attr.colormap = cSpace.colorMap;
611 XChangeWindowAttributes( display, rootWindow,
612 CWColormap, &win_attr );
617 cSpace.colorMap = DefaultColormapOfScreen( screen );
621 cSpace.colorMap = DefaultColormapOfScreen( screen );
622 cSpace.flags |= COLOR_FIXED;
623 COLOR_Graymax = (1<<screenDepth)-1;
627 cSpace.flags |= COLOR_VIRTUAL;
629 int *depths,nrofdepths;
630 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
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 )
638 cSpace.flags = (white & mask) ? COLOR_WHITESET : 0;
639 cSpace.colorMap = DefaultColormapOfScreen( screen );
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);
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 );
661 COLOR_BuildSharedMap( &cSpace );
663 /* Build free list */
665 if( COLOR_firstFree != -1 )
666 COLOR_FormatSystemPalette();
668 COLOR_FillDefaultColors();
673 /***********************************************************************
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),
686 /***********************************************************************
689 * Check whether 'color' can be represented with a solid color.
691 BOOL32 COLOR_IsSolid( COLORREF color )
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;
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;
720 for( i = 0; i < size && diff ; i++ )
722 if( !(palPalEntry[i].peFlags & PC_SYS_USED) ||
723 (skipReserved && palPalEntry[i].peFlags & PC_SYS_RESERVED) )
726 r = palPalEntry[i].peRed - GetRValue(col);
727 g = palPalEntry[i].peGreen - GetGValue(col);
728 b = palPalEntry[i].peBlue - GetBValue(col);
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();
746 for( i = 0; i < size && diff ; i++ )
748 if( i == NB_RESERVED_COLORS/2 )
750 int newi = size - NB_RESERVED_COLORS/2;
754 r = COLOR_sysPal[i].peRed - GetRValue(col);
755 g = COLOR_sysPal[i].peGreen - GetGValue(col);
756 b = COLOR_sysPal[i].peBlue - GetBValue(col);
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,
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 )
785 /***********************************************************************
786 * COLOR_LookupNearestColor
788 COLORREF COLOR_LookupNearestColor( PALETTEENTRY* palPalEntry, int size, COLORREF color )
790 unsigned char spec_type = color >> 24;
793 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
795 if( spec_type == 2 ) /* PALETTERGB */
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);
808 return (0x00ffffff & *(COLORREF*)
809 (COLOR_sysPal + COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL, color, FALSE)));
812 /***********************************************************************
815 * Return RGB color for given X pixel.
817 COLORREF COLOR_ToLogical(int pixel)
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;
845 XQueryColor(display, cSpace.colorMap, &color);
846 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
850 /***********************************************************************
853 * Return the physical color closest to 'color'.
855 int COLOR_ToPhysical( DC *dc, COLORREF color )
858 HPALETTE16 hPal = (dc)? dc->w.hPalette: STOCK_DEFAULT_PALETTE;
859 unsigned char spec_type = color >> 24;
861 if ( cSpace.flags & COLOR_FIXED )
863 /* there is no colormap limitation; we are going to have to compute
864 * the pixel value from the visual information stored earlier
867 unsigned long red, green, blue;
869 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
873 case 2: /* PALETTERGB - not sure if we really need to search palette */
875 idx = COLOR_PaletteLookupPixel( palPtr->logpalette.palPalEntry,
876 palPtr->logpalette.palNumEntries,
879 if( palPtr->mapping ) return palPtr->mapping[idx];
881 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
884 case 1: /* PALETTEINDEX */
886 if( (idx = color & 0xffff) >= palPtr->logpalette.palNumEntries)
888 fprintf(stderr, "\tRGB(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
892 if( palPtr->mapping ) return palPtr->mapping[idx];
894 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
899 /* fall through to RGB */
902 if( dc && (dc->w.bitsPerPixel == 1) )
903 return (((color >> 16) & 0xff) +
904 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
907 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
911 /* grayscale only; return scaled value */
912 return ( (red * 30 + green * 69 + blue * 11) * COLOR_Graymax) / 25500;
916 /* scale each individually and construct the TrueColor pixel value */
917 if (COLOR_Redmax != 255) red = (red * COLOR_Redmax) / 255;
918 if (COLOR_Greenmax != 255) green = (green * COLOR_Greenmax) / 255;
919 if (COLOR_Bluemax != 255) blue = (blue * COLOR_Bluemax) / 255;
921 return (red << COLOR_Redshift) | (green << COLOR_Greenshift) | (blue << COLOR_Blueshift);
926 PALETTEOBJ* palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC);
928 /* palPtr can be NULL when DC is being destroyed */
930 if( !palPtr ) return 0;
931 else if( !palPtr->mapping )
932 dprintf_palette(stddeb,"\tpalette %04x is not realized\n", dc->w.hPalette);
934 switch(spec_type) /* we have to peruse DC and system palette */
938 /* fall through to RGB */
941 if( dc && (dc->w.bitsPerPixel == 1) )
942 return (((color >> 16) & 0xff) +
943 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
945 index = COLOR_PaletteLookupPixel( COLOR_sysPal, 256,
946 COLOR_PaletteToPixel, color, FALSE);
948 /* dprintf_palette(stddeb,"\tRGB(%lx) -> pixel %i\n", color, index);
951 case 1: /* PALETTEINDEX */
952 index = color & 0xffff;
954 if( index >= palPtr->logpalette.palNumEntries )
955 fprintf(stderr, "\tRGB(%lx) : index %i is out of bounds\n", color, index);
956 else if( palPtr->mapping ) index = palPtr->mapping[index];
958 /* dprintf_palette(stddeb,"\tPALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
961 case 2: /* PALETTERGB */
962 index = COLOR_PaletteLookupPixel( palPtr->logpalette.palPalEntry,
963 palPtr->logpalette.palNumEntries,
964 palPtr->mapping, color, FALSE);
965 /* dprintf_palette(stddeb,"\tPALETTERGB(%lx) -> pixel %i\n", color, index);
974 /***********************************************************************
977 * Set the color-mapping table for selected palette.
978 * Return number of entries which mapping has changed.
980 int COLOR_SetMapping( PALETTEOBJ* palPtr, BOOL32 mapOnly )
984 int prevMapping = (palPtr->mapping) ? 1 : 0;
987 /* reset dynamic system palette entries */
989 if( !mapOnly && COLOR_firstFree != -1)
990 COLOR_FormatSystemPalette();
992 /* initialize palette mapping table */
994 palPtr->mapping = (int*)xrealloc(palPtr->mapping, sizeof(int)*
995 palPtr->logpalette.palNumEntries);
997 for( i = 0; i < palPtr->logpalette.palNumEntries; i++ )
1002 switch( palPtr->logpalette.palPalEntry[i].peFlags & 0x07 )
1004 case PC_EXPLICIT: /* palette entries are indices into system palette */
1005 index = *(WORD*)(palPtr->logpalette.palPalEntry + i);
1006 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1008 fprintf(stderr,"PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1013 case PC_RESERVED: /* forbid future mappings to this entry */
1014 flag |= PC_SYS_RESERVED;
1017 default: /* try to collapse identical colors */
1018 index = COLOR_PaletteLookupExactIndex(COLOR_sysPal, 256,
1019 *(COLORREF*)(palPtr->logpalette.palPalEntry + i));
1024 if( COLOR_firstFree > 0 && !(cSpace.flags & COLOR_FIXED) )
1027 index = COLOR_firstFree; /* ought to be available */
1028 COLOR_firstFree = COLOR_freeList[index];
1030 color.pixel = (COLOR_PaletteToPixel) ? COLOR_PaletteToPixel[index] : index;
1031 color.red = palPtr->logpalette.palPalEntry[i].peRed << 8;
1032 color.green = palPtr->logpalette.palPalEntry[i].peGreen << 8;
1033 color.blue = palPtr->logpalette.palPalEntry[i].peBlue << 8;
1034 color.flags = DoRed | DoGreen | DoBlue;
1035 XStoreColor(display, cSpace.colorMap, &color);
1037 COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[i];
1038 COLOR_sysPal[index].peFlags = flag;
1039 COLOR_freeList[index] = 0;
1041 if( COLOR_PaletteToPixel ) index = COLOR_PaletteToPixel[index];
1044 else if ( cSpace.flags & COLOR_VIRTUAL )
1046 index = COLOR_ToPhysical( NULL, 0x00ffffff &
1047 *(COLORREF*)(palPtr->logpalette.palPalEntry + i));
1051 /* we have to map to existing entry in the system palette */
1053 index = COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL,
1054 *(COLORREF*)(palPtr->logpalette.palPalEntry + i), TRUE);
1056 palPtr->logpalette.palPalEntry[i].peFlags |= PC_SYS_USED;
1058 if( COLOR_PaletteToPixel ) index = COLOR_PaletteToPixel[index];
1062 if( !prevMapping || palPtr->mapping[i] != index ) iRemapped++;
1063 palPtr->mapping[i] = index;
1065 dprintf_palette(stddeb,"\tentry %i (%lx) -> pixel %i\n", i,
1066 *(COLORREF*)(palPtr->logpalette.palPalEntry + i), index);