Owncolmap removed in favour of more intelligent color allocation.

git-svn-id: svn://svn.code.sf.net/p/rdesktop/code/trunk/rdesktop@184 423420c4-83ab-492f-b58f-81f9feb106b5
This commit is contained in:
Peter Kallden 2002-09-18 12:13:08 +00:00
parent 5ccf491924
commit ce93ec91a4

93
xwin.c
View File

@ -59,7 +59,6 @@ static Pixmap backstore;
}
/* colour maps */
static BOOL owncolmap;
static Colormap xcolmap;
static uint32 *colmap;
@ -71,9 +70,8 @@ static XIC IC = NULL;
/* toggle fullscreen globals */
static unsigned long input_mask;
#define TRANSLATE(col) ( owncolmap ? col : translate_colour(colmap[col]) )
#define SET_FOREGROUND(col) XSetForeground(display, gc, TRANSLATE(col));
#define SET_BACKGROUND(col) XSetBackground(display, gc, TRANSLATE(col));
#define SET_FOREGROUND(col) XSetForeground(display, gc, translate_colour(colmap[col]));
#define SET_BACKGROUND(col) XSetBackground(display, gc, translate_colour(colmap[col]));
static int rop2_map[] = {
GXclear, /* 0 */
@ -333,9 +331,6 @@ ui_init()
return False;
}
if (depth <= 8)
owncolmap = True;
else
xcolmap = DefaultColormapOfScreen(screen);
if (DoesBackingStore(screen) == NotUseful)
@ -637,7 +632,7 @@ ui_create_bitmap(int width, int height, uint8 * data)
Pixmap bitmap;
uint8 *tdata;
tdata = (owncolmap ? data : translate_image(width, height, data));
tdata = translate_image(width, height, data);
bitmap = XCreatePixmap(display, wnd, width, height, depth);
image = XCreateImage(display, visual, depth, ZPixmap, 0,
(char *) tdata, width, height, 8, 0);
@ -645,7 +640,6 @@ ui_create_bitmap(int width, int height, uint8 * data)
XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
XFree(image);
if (!owncolmap)
xfree(tdata);
return (HBITMAP) bitmap;
}
@ -656,7 +650,7 @@ ui_paint_bitmap(int x, int y, int cx, int cy, int width, int height, uint8 * dat
XImage *image;
uint8 *tdata;
tdata = (owncolmap ? data : translate_image(width, height, data));
tdata = translate_image(width, height, data);
image = XCreateImage(display, visual, depth, ZPixmap, 0,
(char *) tdata, width, height, 8, 0);
@ -671,7 +665,6 @@ ui_paint_bitmap(int x, int y, int cx, int cy, int width, int height, uint8 * dat
}
XFree(image);
if (!owncolmap)
xfree(tdata);
}
@ -807,42 +800,59 @@ ui_create_colourmap(COLOURMAP * colours)
{
COLOURENTRY *entry;
int i, ncolours = colours->ncolours;
if (owncolmap)
{
XColor *xcolours, *xentry;
Colormap map;
xcolours = xmalloc(sizeof(XColor) * ncolours);
for (i = 0; i < ncolours; i++)
{
entry = &colours->colours[i];
xentry = &xcolours[i];
xentry->pixel = i;
MAKE_XCOLOR(xentry, entry);
}
map = XCreateColormap(display, wnd, visual, AllocAll);
XStoreColors(display, map, xcolours, ncolours);
xfree(xcolours);
return (HCOLOURMAP) map;
}
else
{
uint32 *map = xmalloc(sizeof(*colmap) * ncolours);
XColor xentry;
XColor xc_cache[256];
uint32 colour;
int colLookup = 256;
for (i = 0; i < ncolours; i++)
{
entry = &colours->colours[i];
MAKE_XCOLOR(&xentry, entry);
if (XAllocColor(display, xcolmap, &xentry) != 0)
if (XAllocColor(display, xcolmap, &xentry) == 0)
{
/* Allocation failed, find closest match. */
int j = 256;
int nMinDist = 3 * 256 * 256;
long nDist = nMinDist;
/* only get the colors once */
while( colLookup-- ){
xc_cache[colLookup].pixel = colLookup;
xc_cache[colLookup].red = xc_cache[colLookup].green = xc_cache[colLookup].blue = 0;
xc_cache[colLookup].flags = 0;
XQueryColor(display, DefaultColormap(display, DefaultScreen(display)), &xc_cache[colLookup]);
}
colLookup = 0;
/* approximate the pixel */
while( j-- ){
if( xc_cache[j].flags ){
nDist =
((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) *
((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) +
((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) *
((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) +
((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8)) *
((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8));
}
if( nDist < nMinDist ){
nMinDist = nDist;
xentry.pixel = j;
}
}
}
colour = xentry.pixel;
else
colour = WhitePixelOfScreen(screen);
/* update our cache */
if( xentry.pixel < 256 ){
xc_cache[xentry.pixel].red = xentry.red;
xc_cache[xentry.pixel].green = xentry.green;
xc_cache[xentry.pixel].blue = xentry.blue;
}
/* byte swap here to make translate_image faster */
map[i] = translate_colour(colour);
@ -850,23 +860,16 @@ ui_create_colourmap(COLOURMAP * colours)
return map;
}
}
void
ui_destroy_colourmap(HCOLOURMAP map)
{
if (owncolmap)
XFreeColormap(display, (Colormap) map);
else
xfree(map);
}
void
ui_set_colourmap(HCOLOURMAP map)
{
if (owncolmap)
XSetWindowColormap(display, wnd, (Colormap) map);
else
colmap = map;
}