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 private colormap. Otherwise we
27 * have to map DC palette indices to real pixel values.
28 * With private colormap it boils down to identity mapping. The
29 * other special case is when we have fixed color visual with
30 * screendepth > 8 - we abandon mapping altogether (pixel
31 * values can be calculated without X server assistance).
33 * For info about general Windows palette management read
34 * http://198.105.232.5/MSDN/LIBRARY/TECHNOTE/CH3.HTM
37 extern void BITBLT_SetAccelMode( int ); /* speed up INVERT raster ops whenever possible
38 * parameter is a speedup level (see bitblt.c)
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_RESERVED_COLORS 20 /* number of fixed colors in system palette */
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;
73 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
74 static int COLOR_firstFree
= 0;
76 /* Maps entry in the system palette to X pixel value */
77 int* COLOR_PaletteToPixel
= NULL
;
79 /* Maps pixel to the entry in the system palette */
80 int* COLOR_PixelToPalette
= NULL
;
82 static const PALETTEENTRY __sysPalTemplate
[NB_RESERVED_COLORS
] =
84 /* first 10 entries in the system palette */
85 /* red green blue flags */
86 { 0x00, 0x00, 0x00, PC_SYS_USED
},
87 { 0x80, 0x00, 0x00, PC_SYS_USED
},
88 { 0x00, 0x80, 0x00, PC_SYS_USED
},
89 { 0x80, 0x80, 0x00, PC_SYS_USED
},
90 { 0x00, 0x00, 0x80, PC_SYS_USED
},
91 { 0x80, 0x00, 0x80, PC_SYS_USED
},
92 { 0x00, 0x80, 0x80, PC_SYS_USED
},
93 { 0xc0, 0xc0, 0xc0, PC_SYS_USED
},
94 { 0xc0, 0xdc, 0xc0, PC_SYS_USED
},
95 { 0xa6, 0xca, 0xf0, PC_SYS_USED
},
97 /* ... c_min/2 dynamic colorcells */
99 /* ... gap (for sparse palettes) */
101 /* ... c_min/2 dynamic colorcells */
103 { 0xff, 0xfb, 0xf0, PC_SYS_USED
},
104 { 0xa0, 0xa0, 0xa4, PC_SYS_USED
},
105 { 0x80, 0x80, 0x80, PC_SYS_USED
},
106 { 0xff, 0x00, 0x00, PC_SYS_USED
},
107 { 0x00, 0xff, 0x00, PC_SYS_USED
},
108 { 0xff, 0xff, 0x00, PC_SYS_USED
},
109 { 0x00, 0x00, 0xff, PC_SYS_USED
},
110 { 0xff, 0x00, 0xff, PC_SYS_USED
},
111 { 0x00, 0xff, 0xff, PC_SYS_USED
},
112 { 0xff, 0xff, 0xff, PC_SYS_USED
} /* last 10 */
115 /* Map an EGA index (0..15) to a pixel value in the system color space. */
117 int COLOR_mapEGAPixel
[16];
119 /***********************************************************************
120 * Misc auxiliary functions
122 Colormap
COLOR_GetColormap(void)
124 return cSpace
.colorMap
;
127 UINT16
COLOR_GetSystemPaletteSize(void)
129 return (cSpace
.flags
& COLOR_PRIVATE
) ? cSpace
.size
: 256;
132 UINT16
COLOR_GetSystemPaletteFlags(void)
137 void COLOR_FormatSystemPalette(void)
139 int i
, j
= COLOR_firstFree
= NB_RESERVED_COLORS
/2;
141 COLOR_sysPal
[j
].peFlags
= 0;
142 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
143 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
145 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
146 COLOR_sysPal
[j
].peRed
= i
; /* next */
149 COLOR_sysPal
[j
].peRed
= 0; /* terminal */
153 /***********************************************************************
154 * COLOR_BuildPrivateMap/COLOR_BuildSharedMap
156 * Executed only one time so we don't care about sloppiness,
157 * the rest have to be very tight though...
159 static BOOL
COLOR_BuildPrivateMap(CSPACE
* cs
)
161 /* Private colormap - identity mapping */
166 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*cs
->size
);
168 dprintf_palette(stddeb
,"\tbuilding private map - %i palette entries\n", cs
->size
);
170 /* Allocate system palette colors */
172 for( i
=0; i
< cs
->size
; i
++ )
174 if( i
< NB_RESERVED_COLORS
/2 )
176 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
177 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
178 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
179 COLOR_sysPal
[i
] = __sysPalTemplate
[i
];
181 else if( i
>= cs
->size
- NB_RESERVED_COLORS
/2 )
183 int j
= NB_RESERVED_COLORS
+ i
- cs
->size
;
184 color
.red
= __sysPalTemplate
[j
].peRed
* 65535 / 255;
185 color
.green
= __sysPalTemplate
[j
].peGreen
* 65535 / 255;
186 color
.blue
= __sysPalTemplate
[j
].peBlue
* 65535 / 255;
187 COLOR_sysPal
[i
] = __sysPalTemplate
[j
];
190 color
.flags
= DoRed
| DoGreen
| DoBlue
;
192 XStoreColor(display
, cs
->colorMap
, &color
);
194 /* Set EGA mapping if color in the first or last eight */
197 COLOR_mapEGAPixel
[i
] = color
.pixel
;
198 else if (i
>= cs
->size
- 8 )
199 COLOR_mapEGAPixel
[i
- (cs
->size
- 16)] = color
.pixel
;
202 COLOR_PixelToPalette
= COLOR_PaletteToPixel
= NULL
;
204 COLOR_gapStart
= 256; COLOR_gapEnd
= -1;
206 COLOR_firstFree
= (cs
->size
> NB_RESERVED_COLORS
)?NB_RESERVED_COLORS
/2 : -1;
210 static BOOL
COLOR_BuildSharedMap(CSPACE
* cs
)
213 unsigned long sysPixel
[NB_RESERVED_COLORS
];
214 unsigned long* pixDynMapping
= NULL
;
215 unsigned long plane_masks
[1];
217 int color_ini_max
= 256;
219 /* read "AllocSystemColors" from wine.conf */
221 color_ini_max
= PROFILE_GetWineIniInt( "options", "AllocSystemColors", 256);
222 if (color_ini_max
> 256) color_ini_max
= 256;
223 if (color_ini_max
< 20) color_ini_max
= 20;
224 dprintf_palette(stddeb
,"COLOR_Init: %d colors configured.\n",color_ini_max
);
226 dprintf_palette(stddeb
,"\tbuilding shared map - %i palette entries\n", cs
->size
);
228 /* Be nice and allocate system colors as read-only */
230 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
232 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
233 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
234 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
235 color
.flags
= DoRed
| DoGreen
| DoBlue
;
237 if (!XAllocColor( display
, cSpace
.colorMap
, &color
))
239 fprintf(stderr
, "Warning: Not enough free colors for system palette.\n" );
240 color
.pixel
= color
.red
= color
.green
= color
.blue
= 0;
243 sysPixel
[i
] = color
.pixel
;
245 dprintf_palette(stddeb
,"\tsyscolor(%lx) -> pixel %i\n",
246 *(COLORREF
*)(__sysPalTemplate
+i
), (int)color
.pixel
);
248 /* Set EGA mapping if color in the first or last eight */
251 COLOR_mapEGAPixel
[i
] = color
.pixel
;
252 else if (i
>= NB_RESERVED_COLORS
- 8 )
253 COLOR_mapEGAPixel
[i
- (NB_RESERVED_COLORS
-16)] = color
.pixel
;
256 if( !(cSpace
.flags
& COLOR_FIXED
) )
258 int c_min
= 0, c_max
= cs
->size
, c_val
;
260 dprintf_palette(stddeb
,"\tdynamic colormap... ");
262 /* comment this out if you want to debug palette init */
264 XGrabServer(display
);
266 /* let's become the first client that actually follows
267 * X guidelines and does binary search...
270 pixDynMapping
= (unsigned long*)xmalloc(sizeof(long)*cs
->size
);
271 while( c_max
- c_min
> 0 )
273 c_val
= (c_max
+ c_min
)/2 + (c_max
+ c_min
)%2;
275 if( !XAllocColorCells(display
, cs
->colorMap
, False
,
276 plane_masks
, 0, pixDynMapping
, c_val
) )
280 XFreeColors(display
, cs
->colorMap
, pixDynMapping
, c_val
, 0);
285 if( c_min
> color_ini_max
- NB_RESERVED_COLORS
)
286 c_min
= color_ini_max
- NB_RESERVED_COLORS
;
288 c_min
= (c_min
/2) + (c_min
/2); /* need even set for split palette */
291 if( !XAllocColorCells(display
, cs
->colorMap
, False
,
292 plane_masks
, 0, pixDynMapping
, c_min
) )
294 fprintf(stderr
,"Inexplicable failure during colorcell allocation.\n");
298 cs
->size
= c_min
+ NB_RESERVED_COLORS
;
300 COLOR_gapStart
= cs
->size
/2;
301 COLOR_gapEnd
= 256 - cs
->size
/2;
303 XUngrabServer(display
);
305 dprintf_palette(stddeb
,"adjusted size %i colorcells\n", cs
->size
);
307 else if( cSpace
.flags
& COLOR_VIRTUAL
)
309 /* virtual colorspace - ToPhysical takes care of
310 * color translations but we have to allocate full palette
311 * to maintain compatibility
313 dprintf_palette(stddeb
,"\tvirtual colorspace - screendepth %i\n", screenDepth
);
315 else cs
->size
= NB_RESERVED_COLORS
; /* system palette only - however we can alloc a bunch
316 * of colors and map to them */
318 dprintf_palette(stddeb
,"Shared system palette uses %i colors.\n", cs
->size
);
320 /* Set gap to account for pixel shortage. It has to be right in the center
321 * of the system palette because otherwise raster ops get screwed.
322 * ( if we have 100 color palette and application does invert for pixel 1
323 * it gets pixel 254 - far beyond our pathetic palette unless these 100
324 * colors are mapped with the gap in the middle )
327 if( cs
->size
>= 256 )
328 { COLOR_gapStart
= 256; COLOR_gapEnd
= -1; }
330 { COLOR_gapStart
= cs
->size
/2; COLOR_gapEnd
= 255 - cs
->size
/2; }
332 COLOR_firstFree
= ( cs
->size
> NB_RESERVED_COLORS
&&
333 (cs
->flags
& COLOR_VIRTUAL
|| !(cs
->flags
& COLOR_FIXED
)) )
334 ? NB_RESERVED_COLORS
/2 : -1;
336 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*256);
338 /* Setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
340 if( screenDepth
<= 8 )
342 COLOR_PixelToPalette
= (int*)xmalloc(sizeof(int)*256);
343 COLOR_PaletteToPixel
= (int*)xmalloc(sizeof(int)*256);
345 for( i
= j
= 0; i
< 256; i
++ )
347 if( i
>= COLOR_gapStart
&& i
<= COLOR_gapEnd
)
349 COLOR_PaletteToPixel
[i
] = 0;
350 COLOR_sysPal
[i
].peFlags
= 0; /* mark as unused */
354 if( i
< NB_RESERVED_COLORS
/2 )
356 COLOR_PaletteToPixel
[i
] = sysPixel
[i
];
357 COLOR_sysPal
[i
] = __sysPalTemplate
[i
];
359 else if( i
>= 256 - NB_RESERVED_COLORS
/2 )
361 COLOR_PaletteToPixel
[i
] = sysPixel
[(i
+ NB_RESERVED_COLORS
) - 256];
362 COLOR_sysPal
[i
] = __sysPalTemplate
[(i
+ NB_RESERVED_COLORS
) - 256];
364 else if( pixDynMapping
)
365 COLOR_PaletteToPixel
[i
] = pixDynMapping
[j
++];
367 COLOR_PaletteToPixel
[i
] = i
;
369 dprintf_palette(stddeb
,"\tindex %i -> pixel %i\n", i
, COLOR_PaletteToPixel
[i
]);
371 memset( COLOR_PixelToPalette
, 0, 256*sizeof(int) );
372 COLOR_PixelToPalette
[COLOR_PaletteToPixel
[i
]] = i
;
375 if( pixDynMapping
) free(pixDynMapping
);
380 /***********************************************************************
383 * Create the system palette and initialize mapping tables.
385 static HPALETTE16
COLOR_InitPalette(void)
391 /* calculate max palette size */
393 cSpace
.size
= visual
->map_entries
;
395 if (cSpace
.flags
& COLOR_PRIVATE
)
396 COLOR_BuildPrivateMap( &cSpace
);
398 COLOR_BuildSharedMap( &cSpace
);
400 /* Build free list ( use peRed as "next" ) */
402 if( COLOR_firstFree
!= -1 )
403 COLOR_FormatSystemPalette();
405 /* create default palette (20 system colors) */
407 palPtr
= xmalloc( sizeof(LOGPALETTE
) + (NB_RESERVED_COLORS
-1)*sizeof(PALETTEENTRY
) );
408 if (!palPtr
) return FALSE
;
410 palPtr
->palVersion
= 0x300;
411 palPtr
->palNumEntries
= NB_RESERVED_COLORS
;
412 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
414 palPtr
->palPalEntry
[i
].peRed
= __sysPalTemplate
[i
].peRed
;
415 palPtr
->palPalEntry
[i
].peGreen
= __sysPalTemplate
[i
].peGreen
;
416 palPtr
->palPalEntry
[i
].peBlue
= __sysPalTemplate
[i
].peBlue
;
417 palPtr
->palPalEntry
[i
].peFlags
= 0;
419 hpalette
= CreatePalette( palPtr
);
425 /***********************************************************************
426 * COLOR_Computeshifts
428 * Calculate conversion parameters for direct mapped visuals
430 void COLOR_Computeshifts(unsigned long maskbits
, int *shift
, int *max
)
441 for(i
=0;!(maskbits
&1);i
++)
449 /***********************************************************************
452 * Initialize color map and system palette.
455 HPALETTE16
COLOR_Init(void)
457 visual
= DefaultVisual( display
, DefaultScreen(display
) );
459 dprintf_palette(stddeb
,"COLOR_Init: initializing palette manager...");
461 switch(visual
->class)
464 cSpace
.flags
|= COLOR_VIRTUAL
;
467 if (Options
.usePrivateMap
)
469 XSetWindowAttributes win_attr
;
471 cSpace
.flags
|= COLOR_PRIVATE
;
472 cSpace
.colorMap
= XCreateColormap( display
, rootWindow
,
476 if( rootWindow
!= DefaultRootWindow(display
) )
478 win_attr
.colormap
= cSpace
.colorMap
;
479 XChangeWindowAttributes( display
, rootWindow
,
480 CWColormap
, &win_attr
);
485 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
489 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
490 cSpace
.flags
|= COLOR_FIXED
;
491 COLOR_Graymax
= (1<<screenDepth
)-1;
495 cSpace
.flags
|= COLOR_VIRTUAL
;
497 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
498 cSpace
.flags
|= COLOR_FIXED
;
499 COLOR_Computeshifts(visual
->red_mask
, &COLOR_Redshift
, &COLOR_Redmax
);
500 COLOR_Computeshifts(visual
->green_mask
, &COLOR_Greenshift
, &COLOR_Greenmax
);
501 COLOR_Computeshifts(visual
->blue_mask
, &COLOR_Blueshift
, &COLOR_Bluemax
);
505 dprintf_palette(stddeb
," visual class %i\n", visual
->class);
507 return COLOR_InitPalette();
511 /***********************************************************************
514 * Check whether 'color' can be represented with a solid color.
516 BOOL
COLOR_IsSolid( COLORREF color
)
519 const PALETTEENTRY
*pEntry
= COLOR_sysPal
;
521 if (color
& 0xff000000) return TRUE
; /* indexed color */
523 if (!color
|| (color
== 0xffffff)) return TRUE
; /* black or white */
525 for (i
= 0; i
< 256 ; i
++, pEntry
++)
527 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
528 if ((GetRValue(color
) == pEntry
->peRed
) &&
529 (GetGValue(color
) == pEntry
->peGreen
) &&
530 (GetBValue(color
) == pEntry
->peBlue
)) return TRUE
;
536 /***********************************************************************
537 * COLOR_PaletteLookup
540 int COLOR_PaletteLookupPixel( PALETTEENTRY
* palPalEntry
, int size
,
541 int* mapping
, COLORREF col
, BOOL skipReserved
)
543 int i
, best
= 0, diff
= 0x7fffffff;
546 for( i
= 0; i
< size
; i
++ )
548 if( !(palPalEntry
[i
].peFlags
& PC_SYS_USED
) ||
549 (skipReserved
&& palPalEntry
[i
].peFlags
& PC_SYS_RESERVED
) )
552 r
= palPalEntry
[i
].peRed
- GetRValue(col
);
553 g
= palPalEntry
[i
].peGreen
- GetGValue(col
);
554 b
= palPalEntry
[i
].peBlue
- GetBValue(col
);
558 if( r
< diff
) { best
= i
; diff
= r
; }
560 return (mapping
) ? mapping
[best
] : best
;
564 /***********************************************************************
565 * COLOR_PaletteLookupExactIndex
568 int COLOR_PaletteLookupExactIndex( PALETTEENTRY
* palPalEntry
, int size
,
572 BYTE r
= GetRValue(col
), g
= GetGValue(col
), b
= GetBValue(col
);
573 for( i
= 0; i
< size
; i
++ )
575 if( palPalEntry
[i
].peFlags
& PC_SYS_USED
) /* skip gap */
576 if( palPalEntry
[i
].peRed
== r
&&
577 palPalEntry
[i
].peGreen
== g
&&
578 palPalEntry
[i
].peBlue
== b
)
585 /***********************************************************************
588 * Return RGB color for given X pixel.
590 COLORREF
COLOR_ToLogical(int pixel
)
594 if (screenDepth
> 8) return pixel
;
595 if ((screenDepth
<= 8) && (pixel
< 256) && !(cSpace
.flags
& COLOR_VIRTUAL
))
596 return ( *(COLORREF
*)(COLOR_sysPal
+ ((COLOR_PixelToPalette
)?COLOR_PixelToPalette
[pixel
]:pixel
)) ) & 0x00ffffff;
599 XQueryColor(display
, cSpace
.colorMap
, &color
);
601 return RGB((color
.red
* 255)/COLOR_Redmax
,
602 (color
.green
* 255)/COLOR_Greenmax
,
603 (color
.blue
* 255)/COLOR_Bluemax
);
607 /***********************************************************************
610 * Return the physical color closest to 'color'.
612 int COLOR_ToPhysical( DC
*dc
, COLORREF color
)
615 unsigned char spec_type
;
616 HPALETTE16 hPal
= (dc
)? dc
->w
.hPalette
: STOCK_DEFAULT_PALETTE
;
618 spec_type
= color
>> 24;
620 if( spec_type
== 0xff )
622 spec_type
= 0; /* 'write' seems to need that for 'Page 1' text */
628 fprintf(stderr
, "COLOR_ToPhysical : invalid RGB specifier for: %08lx\n", color
);
632 if (dc
&& (dc
->w
.bitsPerPixel
== 1) && (spec_type
== 0))
635 if (((color
>> 16) & 0xff) +
636 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2)
637 return 1; /* white */
638 else return 0; /* black */
641 if ( cSpace
.flags
& COLOR_FIXED
)
643 /* there is no colormap limitation; we are going to have to compute
644 * the pixel value from the visual information stored earlier
647 unsigned long red
, green
, blue
;
649 PALETTEOBJ
* palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hPal
, PALETTE_MAGIC
);
656 red
= GetRValue(color
);
657 green
= GetGValue(color
);
658 blue
= GetBValue(color
);
661 case 2: /* PALETTERGB - not sure if we really need to search palette */
663 idx
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
664 palPtr
->logpalette
.palNumEntries
,
666 case 1: /* PALETTEINDEX */
668 idx
= ((spec_type
== 1)?color
:idx
) & 0xffff;
670 if (idx
>= palPtr
->logpalette
.palNumEntries
)
672 fprintf(stderr
, "\tRGB(%lx) : idx %d is out of bounds, assuming black\n", color
, idx
);
674 red
= green
= blue
= 0;
676 else if( palPtr
->mapping
) return palPtr
->mapping
[idx
];
679 red
= palPtr
->logpalette
.palPalEntry
[idx
].peRed
;
680 green
= palPtr
->logpalette
.palPalEntry
[idx
].peGreen
;
681 blue
= palPtr
->logpalette
.palPalEntry
[idx
].peBlue
;
687 /* grayscale only; return scaled value */
688 return ( (red
* 30 + green
* 69 + blue
* 11) * COLOR_Graymax
) / 25500;
692 /* scale each individually and construct the TrueColor pixel value */
693 if (COLOR_Redmax
!= 255) red
= (red
* COLOR_Redmax
) / 255;
694 if (COLOR_Greenmax
!= 255) green
= (green
* COLOR_Greenmax
) / 255;
695 if (COLOR_Bluemax
!= 255) blue
= (blue
* COLOR_Bluemax
) / 255;
697 return (red
<< COLOR_Redshift
) | (green
<< COLOR_Greenshift
) | (blue
<< COLOR_Blueshift
);
702 PALETTEOBJ
* palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hPal
, PALETTE_MAGIC
);
704 /* palPtr can be NULL when DC is being destroyed */
706 if( !palPtr
) return 0;
707 else if( !palPtr
->mapping
)
708 dprintf_palette(stddeb
,"\tpalette %04x is not realized\n", dc
->w
.hPalette
);
710 switch(spec_type
) /* we have to peruse DC and system palette */
714 index
= COLOR_PaletteLookupPixel( COLOR_sysPal
, 256,
715 COLOR_PaletteToPixel
, color
, FALSE
);
717 /* dprintf_palette(stddeb,"\tRGB(%lx) -> pixel %i\n", color, index);
720 case 1: /* PALETTEINDEX */
721 index
= color
& 0xffff;
723 if( index
>= palPtr
->logpalette
.palNumEntries
)
724 fprintf(stderr
, "\tRGB(%lx) : index %i is out of bounds\n", color
, index
);
725 else if( palPtr
->mapping
) index
= palPtr
->mapping
[index
];
727 /* dprintf_palette(stddeb,"\tPALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
730 case 2: /* PALETTERGB */
731 index
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
732 palPtr
->logpalette
.palNumEntries
,
733 palPtr
->mapping
, color
, FALSE
);
734 /* dprintf_palette(stddeb,"\tPALETTERGB(%lx) -> pixel %i\n", color, index);
744 /***********************************************************************
747 * Set the color-mapping table for selected palette.
748 * Return number of entries which mapping has changed.
750 int COLOR_SetMapping( PALETTEOBJ
* palPtr
, BOOL mapOnly
)
754 int prevMapping
= (palPtr
->mapping
) ? 1 : 0;
757 /* free dynamic colors in system palette -
758 * certain optimization is to free them only when they are needed */
760 if( !mapOnly
&& COLOR_firstFree
!= -1)
761 COLOR_FormatSystemPalette();
763 /* initialize palette mapping table */
765 palPtr
->mapping
= (int*)xrealloc(palPtr
->mapping
, sizeof(int)*
766 palPtr
->logpalette
.palNumEntries
);
768 for( i
= 0; i
< palPtr
->logpalette
.palNumEntries
; i
++ )
773 switch( palPtr
->logpalette
.palPalEntry
[i
].peFlags
& 0x07 )
775 case PC_EXPLICIT
: /* palette entries are indices into system palette */
776 index
= *(WORD
*)(palPtr
->logpalette
.palPalEntry
+ i
);
777 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
779 fprintf(stderr
,"PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
784 case PC_RESERVED
: /* forbid future mappings to this entry */
785 flag
|= PC_SYS_RESERVED
;
787 default: /* try to collapse identical colors */
788 index
= COLOR_PaletteLookupExactIndex(COLOR_sysPal
, 256,
789 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
));
794 if( COLOR_firstFree
> 0 && !(cSpace
.flags
& COLOR_FIXED
) && !mapOnly
)
797 index
= COLOR_firstFree
; /* ought to be available */
798 COLOR_firstFree
= COLOR_sysPal
[index
].peRed
;
799 color
.pixel
= (COLOR_PaletteToPixel
) ? COLOR_PaletteToPixel
[index
] : index
;
800 color
.red
= palPtr
->logpalette
.palPalEntry
[i
].peRed
* 65535 / 255;
801 color
.green
= palPtr
->logpalette
.palPalEntry
[i
].peGreen
* 65535 / 255;
802 color
.blue
= palPtr
->logpalette
.palPalEntry
[i
].peBlue
* 65535 / 255;
803 color
.flags
= DoRed
| DoGreen
| DoBlue
;
804 XStoreColor(display
, cSpace
.colorMap
, &color
);
805 COLOR_sysPal
[index
] = palPtr
->logpalette
.palPalEntry
[i
];
806 COLOR_sysPal
[index
].peFlags
= flag
;
807 if( COLOR_PaletteToPixel
) index
= COLOR_PaletteToPixel
[index
];
810 else if ( cSpace
.flags
& COLOR_VIRTUAL
)
812 index
= COLOR_ToPhysical( NULL
, 0x00ffffff &
813 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
));
817 /* we have to map to existing entry in the system palette */
819 index
= COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
,
820 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
), TRUE
);
823 if( COLOR_PaletteToPixel
) index
= COLOR_PaletteToPixel
[index
];
827 if( !prevMapping
|| palPtr
->mapping
[i
] != index
) iRemapped
++;
828 palPtr
->mapping
[i
] = index
;
830 /* dprintf_palette(stddeb,"\tentry %i (%lx) -> pixel %i\n", i,
831 *(COLORREF*)(palPtr->logpalette.palPalEntry + i), index);