--- sourceforge.net/trunk/rdesktop/xwin.c 2002/11/25 18:23:44 278 +++ sourceforge.net/trunk/rdesktop/xwin.c 2002/11/26 10:09:14 279 @@ -79,11 +79,13 @@ } /* colour maps */ +BOOL owncolmap = False; static Colormap xcolmap; static uint32 *colmap; -#define SET_FOREGROUND(col) XSetForeground(display, gc, translate_colour(colmap[col])); -#define SET_BACKGROUND(col) XSetBackground(display, gc, translate_colour(colmap[col])); +#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)); static int rop2_map[] = { GXclear, /* 0 */ @@ -289,7 +291,16 @@ return False; } - xcolmap = DefaultColormapOfScreen(screen); + if (owncolmap != True) + { + xcolmap = DefaultColormapOfScreen(screen); + if (depth <= 8) + { + printf("You're using a screen depth of 8-bits or lower\n"); + printf("If you get scewed colours, try the -C switch\n"); + } + } + gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL); if (DoesBackingStore(screen) != Always) @@ -711,7 +722,7 @@ Pixmap bitmap; uint8 *tdata; - tdata = translate_image(width, height, data); + tdata = (owncolmap ? data : 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); @@ -719,7 +730,8 @@ XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height); XFree(image); - xfree(tdata); + if (!owncolmap) + xfree(tdata); return (HBITMAP) bitmap; } @@ -729,7 +741,7 @@ XImage *image; uint8 *tdata; - tdata = translate_image(width, height, data); + tdata = (owncolmap ? data : translate_image(width, height, data)); image = XCreateImage(display, visual, depth, ZPixmap, 0, (char *) tdata, width, height, 8, 0); @@ -744,7 +756,8 @@ } XFree(image); - xfree(tdata); + if (!owncolmap) + xfree(tdata); } void @@ -875,95 +888,125 @@ (xc)->blue = ((c)->blue << 8) | (c)->blue; \ (xc)->flags = DoRed | DoGreen | DoBlue; + HCOLOURMAP ui_create_colourmap(COLOURMAP * colours) { COLOURENTRY *entry; int i, ncolours = colours->ncolours; - uint32 *map = xmalloc(sizeof(*colmap) * ncolours); - XColor xentry; - XColor xc_cache[256]; - uint32 colour; - int colLookup = 256; - for (i = 0; i < ncolours; i++) + if (!owncolmap) { - entry = &colours->colours[i]; - MAKE_XCOLOR(&xentry, entry); - - if (XAllocColor(display, xcolmap, &xentry) == 0) + uint32 *map = xmalloc(sizeof(*colmap) * ncolours); + XColor xentry; + XColor xc_cache[256]; + uint32 colour; + int colLookup = 256; + for (i = 0; i < ncolours; i++) { - /* Allocation failed, find closest match. */ - int j = 256; - int nMinDist = 3 * 256 * 256; - long nDist = nMinDist; + entry = &colours->colours[i]; + MAKE_XCOLOR(&xentry, entry); - /* only get the colors once */ - while (colLookup--) + if (XAllocColor(display, xcolmap, &xentry) == 0) { - 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; + /* Allocation failed, find closest match. */ + int j = 256; + int nMinDist = 3 * 256 * 256; + long nDist = nMinDist; - /* approximate the pixel */ - while (j--) - { - if (xc_cache[j].flags) + /* only get the colors once */ + while (colLookup--) { - 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)); + 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]); } - if (nDist < nMinDist) + colLookup = 0; + + /* approximate the pixel */ + while (j--) { - nMinDist = nDist; - xentry.pixel = 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; + + /* 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); } - colour = xentry.pixel; + return map; + } + else + { + XColor *xcolours, *xentry; + Colormap map; - /* update our cache */ - if (xentry.pixel < 256) + xcolours = xmalloc(sizeof(XColor) * ncolours); + for (i = 0; i < ncolours; i++) { - xc_cache[xentry.pixel].red = xentry.red; - xc_cache[xentry.pixel].green = xentry.green; - xc_cache[xentry.pixel].blue = xentry.blue; - + 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); - /* byte swap here to make translate_image faster */ - map[i] = translate_colour(colour); + xfree(xcolours); + return (HCOLOURMAP) map; } - - return map; } void ui_destroy_colourmap(HCOLOURMAP map) { - xfree(map); + if (!owncolmap) + xfree(map); + else + XFreeColormap(display, (Colormap) map); } void ui_set_colourmap(HCOLOURMAP map) { - colmap = map; + if (!owncolmap) + colmap = map; + else + XSetWindowColormap(display, wnd, (Colormap) map); } void