From a18c1697b1176c26e74267f2ac7a153a2cf2c442 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Sun, 18 May 2003 23:06:11 +0000 Subject: [PATCH] make icons use pixel32 data, and image_draw takes pixel32 data. client.c gets pixmap icons as a backup to netwm ones, and they are converted into pixel32 data. --- openbox/client.c | 52 +++++++++++++++--- openbox/client.h | 4 +- render/color.c | 135 +++++++++++++++++++++++++++++++++++++++++++++-- render/color.h | 12 ++++- render/image.c | 10 ++-- render/render.c | 70 +++++++++++++++++++----- render/render.h | 8 +-- 7 files changed, 255 insertions(+), 36 deletions(-) diff --git a/openbox/client.c b/openbox/client.c index 0e5a59f0..8efed96a 100644 --- a/openbox/client.c +++ b/openbox/client.c @@ -1142,6 +1142,9 @@ void client_update_wmhints(Client *self) it->data); } + /* the WM_HINTS can contain an icon */ + client_update_icons(self); + /* because the self->transient flag wont change from this call, we don't need to update the window's type and such, only its transient_for, and the transients lists of other windows in @@ -1317,11 +1320,23 @@ void client_update_icons(Client *self) /* store the icons */ i = 0; for (j = 0; j < self->nicons; ++j) { + guint x, y, t; + w = self->icons[j].width = data[i++]; h = self->icons[j].height = data[i++]; - self->icons[j].data = - g_memdup(&data[i], w * h * sizeof(gulong)); - i += w * h; + + self->icons[j].data = g_new(pixel32, w * h); + for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) { + if (x >= w) { + x = 0; + ++y; + } + self->icons[j].data[t] = + (((data[i] >> 24) & 0xff) << default_alpha_offset) + + (((data[i] >> 16) & 0xff) << default_red_offset) + + (((data[i] >> 8) & 0xff) << default_green_offset) + + (((data[i] >> 0) & 0xff) << default_blue_offset); + } g_assert(i <= num); } @@ -1331,13 +1346,34 @@ void client_update_icons(Client *self) if (num == 2) { self->nicons++; self->icons = g_new(Icon, self->nicons); - /* XXX WHAT IF THIS FAILS YOU TWIT!@!*()@ */ - render_pixmap_to_rgba(data[0], data[1], - &self->icons[self->nicons-1].width, - &self->icons[self->nicons-1].height, - &self->icons[self->nicons-1].data); + if (!render_pixmap_to_rgba(data[0], data[1], + &self->icons[self->nicons-1].width, + &self->icons[self->nicons-1].height, + &self->icons[self->nicons-1].data)) { + g_free(&self->icons[self->nicons-1]); + self->nicons--; + } } g_free(data); + } else { + XWMHints *hints; + + if ((hints = XGetWMHints(ob_display, self->window))) { + if (hints->flags & IconPixmapHint) { + self->nicons++; + self->icons = g_new(Icon, self->nicons); + if (!render_pixmap_to_rgba(hints->icon_pixmap, + (hints->flags & IconMaskHint ? + hints->icon_mask : None), + &self->icons[self->nicons-1].width, + &self->icons[self->nicons-1].height, + &self->icons[self->nicons-1].data)){ + g_free(&self->icons[self->nicons-1]); + self->nicons--; + } + } + XFree(hints); + } } if (self->frame) diff --git a/openbox/client.h b/openbox/client.h index 7545b860..b4e0494d 100644 --- a/openbox/client.h +++ b/openbox/client.h @@ -3,6 +3,8 @@ #include "geom.h" #include "stacking.h" +#include "render/color.h" + #include #include @@ -16,7 +18,7 @@ struct Group; /*! Holds an icon in ARGB format */ typedef struct Icon { int width, height; - gulong *data; + pixel32 *data; } Icon; /*! The MWM Hints as retrieved from the window property diff --git a/render/color.c b/render/color.c index 3a5b6cdb..b9bafe32 100644 --- a/render/color.c +++ b/render/color.c @@ -1,5 +1,6 @@ #include #include +#include #include "render.h" #include "color.h" #include "../kernel/openbox.h" @@ -82,9 +83,9 @@ void reduce_depth(pixel32 *data, XImage *im) r = (data[x] >> default_red_offset) & 0xFF; g = (data[x] >> default_green_offset) & 0xFF; b = (data[x] >> default_blue_offset) & 0xFF; - p32[x] = (r << render_red_offset) - + (g << render_green_offset) - + (b << render_blue_offset); + p32[x] = (r << render_red_shift) + + (g << render_green_shift) + + (b << render_blue_shift); } data += im->width; p32 += im->width; @@ -125,6 +126,7 @@ void reduce_depth(pixel32 *data, XImage *im) g_message("your bit depth is currently unhandled\n"); } } + XColor *pickColor(int r, int g, int b) { r = (r & 0xff) >> (8-pseudo_bpc); @@ -132,3 +134,130 @@ XColor *pickColor(int r, int g, int b) b = (b & 0xff) >> (8-pseudo_bpc); return &pseudo_colors[(r << (2*pseudo_bpc)) + (g << (1*pseudo_bpc)) + b]; } + +static void swap_byte_order(XImage *im) +{ + int x, y, di; + + g_message("SWAPPING BYTE ORDER"); + + di = 0; + for (y = 0; y < im->height; ++y) { + for (x = 0; x < im->height; ++x) { + char *c = &im->data[di + x * im->bits_per_pixel / 8]; + char t; + + switch (im->bits_per_pixel) { + case 32: + t = c[2]; + c[2] = c[3]; + c[3] = t; + case 16: + t = c[0]; + c[0] = c[1]; + c[1] = t; + case 8: + break; + default: + g_message("your bit depth is currently unhandled\n"); + } + } + di += im->bytes_per_line; + } + + if (im->byte_order == LSBFirst) + im->byte_order = MSBFirst; + else + im->byte_order = LSBFirst; +} + +void increase_depth(pixel32 *data, XImage *im) +{ + int r, g, b; + int x,y; + pixel32 *p32 = (pixel32 *) im->data; + pixel16 *p16 = (pixel16 *) im->data; + unsigned char *p8 = (unsigned char *)im->data; + + if (im->byte_order != render_endian) + swap_byte_order(im); + + switch (im->bits_per_pixel) { + case 32: + for (y = 0; y < im->height; y++) { + for (x = 0; x < im->width; x++) { + r = (p32[x] >> render_red_offset) & 0xff; + g = (p32[x] >> render_green_offset) & 0xff; + b = (p32[x] >> render_blue_offset) & 0xff; + data[x] = (r << default_red_offset) + + (g << default_green_offset) + + (b << default_blue_offset) + + (0xff << default_alpha_offset); + } + data += im->width; + p32 += im->bytes_per_line/4; + } + break; + case 16: + for (y = 0; y < im->height; y++) { + for (x = 0; x < im->width; x++) { + r = (p16[x] & render_red_mask) >> render_red_offset << + render_red_shift; + g = (p16[x] & render_green_mask) >> render_green_offset << + render_green_shift; + b = (p16[x] & render_blue_mask) >> render_blue_offset << + render_blue_shift; + data[x] = (r << default_red_offset) + + (g << default_green_offset) + + (b << default_blue_offset) + + (0xff << default_alpha_offset); + } + data += im->width; + p16 += im->bytes_per_line/2; + } + break; + case 8: + g_assert(render_visual->class != TrueColor); + for (y = 0; y < im->height; y++) { + for (x = 0; x < im->width; x++) { + XColor icolor; + int ii, r, g, b; + gulong dev, closest = 0xffffffff, close = 0; + + icolor.pixel = p8[x]; + XQueryColor(ob_display, render_colormap, &icolor); + + /* find the nearest color match */ + for (ii = 0; ii < pseudo_ncolors(); ii++) { + /* find deviations */ + r = (pseudo_colors[ii].red - icolor.red) & 0xff; + g = (pseudo_colors[ii].green - icolor.green) & 0xff; + b = (pseudo_colors[ii].blue - icolor.blue) & 0xff; + /* find a weighted absolute deviation */ + dev = (r * r) * (0xff - (icolor.red & 0xff)) + + (g * g) * (0xff - (icolor.green & 0xff)) + + (b * b) * (0xff - (icolor.blue & 0xff)); + + if (dev < closest) { + closest = dev; + close = ii; + } + } + data[x] = + (pseudo_colors[close].red & 0xff << + default_red_offset) + + (pseudo_colors[close].green & 0xff << + default_green_offset) + + (pseudo_colors[close].blue & 0xff << + default_blue_offset) + + (0xff << default_alpha_offset); + } + data += im->width; + p8 += im->bytes_per_line; + } + + break; + default: + g_message("your bit depth is currently unhandled\n"); + } +} diff --git a/render/color.h b/render/color.h index a2970e4b..fb669fb5 100644 --- a/render/color.h +++ b/render/color.h @@ -25,12 +25,14 @@ typedef u_int16_t pixel16; #define default_red_offset 0 #define default_green_offset 8 #define default_blue_offset 16 -#define endian MSBFirst +#define default_alpha_offset 24 +#define render_endian MSBFirst #else +#define default_alpha_offset 24 #define default_red_offset 16 #define default_green_offset 8 #define default_blue_offset 0 -#define endian LSBFirst +#define render_endian LSBFirst #endif /* G_BYTE_ORDER == G_BIG_ENDIAN */ @@ -48,6 +50,7 @@ color_rgb *color_parse(char *colorname); color_rgb *color_new(int r, int g, int b); void color_free(color_rgb *in); void reduce_depth(pixel32 *data, XImage *im); +void increase_depth(pixel32 *data, XImage *im); extern int render_red_offset; extern int render_green_offset; @@ -57,6 +60,11 @@ extern int render_red_shift; extern int render_green_shift; extern int render_blue_shift; +extern int render_red_mask; +extern int render_green_mask; +extern int render_blue_mask; + extern int pseudo_bpc; +#define pseudo_ncolors() (1 << (pseudo_bpc * 3)) extern XColor *pseudo_colors; #endif /* __color_h */ diff --git a/render/image.c b/render/image.c index 7b58712b..feb25970 100644 --- a/render/image.c +++ b/render/image.c @@ -5,7 +5,7 @@ void image_draw(pixel32 *target, TextureRGBA *rgba, Rect *position, Rect *surarea) { - gulong *draw = rgba->data; + pixel32 *draw = rgba->data; guint c, i, e, t, sfw, sfh; sfw = position->width; sfh = position->height; @@ -50,10 +50,10 @@ void image_draw(pixel32 *target, TextureRGBA *rgba, Rect *position, for (i = 0, c = 0, t = position->x, e = sfw*sfh; i < e; ++i, ++t) { guchar alpha, r, g, b, bgr, bgg, bgb; - alpha = draw[i] >> 24; - r = draw[i] >> 16; - g = draw[i] >> 8; - b = draw[i]; + alpha = draw[i] >> default_alpha_offset; + r = draw[i] >> default_red_offset; + g = draw[i] >> default_green_offset; + b = draw[i] >> default_blue_offset; if (c >= sfw) { c = 0; diff --git a/render/render.c b/render/render.c index a1022bd0..cfad7143 100644 --- a/render/render.c +++ b/render/render.c @@ -19,6 +19,7 @@ Visual *render_visual; Colormap render_colormap; int render_red_offset = 0, render_green_offset = 0, render_blue_offset = 0; int render_red_shift, render_green_shift, render_blue_shift; +int render_red_mask, render_green_mask, render_blue_mask; void render_startup(void) { @@ -87,9 +88,9 @@ void truecolor_startup(void) ZPixmap, 0, NULL, 1, 1, 32, 0); g_assert(timage != NULL); /* find the offsets for each color in the visual's masks */ - red_mask = timage->red_mask; - green_mask = timage->green_mask; - blue_mask = timage->blue_mask; + render_red_mask = red_mask = timage->red_mask; + render_green_mask = green_mask = timage->green_mask; + render_blue_mask = blue_mask = timage->blue_mask; render_red_offset = 0; render_green_offset = 0; @@ -117,7 +118,7 @@ void pseudocolor_startup(void) /* determine the number of colors and the bits-per-color */ pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */ g_assert(pseudo_bpc >= 1); - _ncolors = 1 << (pseudo_bpc * 3); + _ncolors = pseudo_ncolors(); if (_ncolors > 1 << render_depth) { g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n"); @@ -166,7 +167,9 @@ void pseudocolor_startup(void) g = (pseudo_colors[i].green - icolors[ii].green) & 0xff; b = (pseudo_colors[i].blue - icolors[ii].blue) & 0xff; /* find a weighted absolute deviation */ - dev = (r * r) + (g * g) + (b * b); + dev = (r * r) * (0xff - (icolors[ii].red & 0xff)) + + (g * g) * (0xff - (icolors[ii].green & 0xff)) + + (b * b) * (0xff - (icolors[ii].blue & 0xff)); if (dev < closest) { closest = dev; @@ -400,23 +403,23 @@ void appearance_free(Appearance *a) void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h) { - unsigned char *scratch; + pixel32 *scratch; XImage *im = NULL; im = XCreateImage(ob_display, render_visual, render_depth, ZPixmap, 0, NULL, w, h, 32, 0); g_assert(im != NULL); - im->byte_order = endian; + im->byte_order = render_endian; /* this malloc is a complete waste of time on normal 32bpp as reduce_depth just sets im->data = data and returns */ - scratch = malloc(im->width * im->height * sizeof(pixel32)); + scratch = g_new(pixel32, im->width * im->height); im->data = (char*) scratch; reduce_depth(in, im); XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen), im, 0, 0, x, y, w, h); im->data = NULL; XDestroyImage(im); - free(scratch); + g_free(scratch); } void appearance_minsize(Appearance *l, int *w, int *h) @@ -468,9 +471,50 @@ void appearance_minsize(Appearance *l, int *w, int *h) } } -void render_pixmap_to_rgba(Pixmap pmap, Pixmap mask, - int *w, int *h, gulong **data) +gboolean render_pixmap_to_rgba(Pixmap pmap, Pixmap mask, + int *w, int *h, pixel32 **data) { - *w = *h = 0; - *data = NULL; + Window xr; + int xx, xy; + guint pw, ph, mw, mh, xb, xd, i, x, y, di; + XImage *xi, *xm = NULL; + + if (!XGetGeometry(ob_display, pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd)) + return FALSE; + if (mask) { + if (!XGetGeometry(ob_display, mask, &xr, &xx, &xy, &mw, &mh, &xb, &xd)) + return FALSE; + if (pw != mw || ph != mh || xd != 1) + return FALSE; + } + + xi = XGetImage(ob_display, pmap, 0, 0, pw, ph, 0xffffffff, ZPixmap); + if (!xi) + return FALSE; + + if (mask) { + xm = XGetImage(ob_display, mask, 0, 0, mw, mh, 0xffffffff, ZPixmap); + if (!xm) + return FALSE; + } + + *data = g_new(pixel32, pw * ph); + increase_depth(*data, xi); + + if (mask) { + /* apply transparency from the mask */ + di = 0; + for (i = 0, y = 0; y < ph; ++y) { + for (x = 0; x < pw; ++x, ++i) { + if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1)) + (*data)[i] &= ~(0xff << default_alpha_offset); + } + di += xm->bytes_per_line; + } + } + + *w = pw; + *h = ph; + + return TRUE; } diff --git a/render/render.h b/render/render.h index 3fbf3249..68fec468 100644 --- a/render/render.h +++ b/render/render.h @@ -112,11 +112,11 @@ typedef struct TextureMask { typedef struct TextureRGBA { guint width; guint height; - unsigned long *data; + pixel32 *data; /* cached scaled so we don't have to scale often */ guint cwidth; guint cheight; - unsigned long *cache; + pixel32 *cache; } TextureRGBA; typedef union { @@ -159,7 +159,7 @@ void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h); void appearance_minsize(Appearance *l, int *w, int *h); -void render_pixmap_to_rgba(Pixmap pmap, Pixmap mask, - int *w, int *h, gulong **data); +gboolean render_pixmap_to_rgba(Pixmap pmap, Pixmap mask, + int *w, int *h, pixel32 **data); #endif /*__render_h*/ -- 2.45.2