6 #include "../kernel/openbox.h"
11 void color_allocate_gc(color_rgb *in)
15 gcv.foreground = in->pixel;
16 gcv.cap_style = CapProjecting;
17 in->gc = XCreateGC(ob_display, ob_root, GCForeground | GCCapStyle, &gcv);
20 color_rgb *color_parse(char *colorname)
24 g_assert(colorname != NULL);
25 /* get rgb values from colorname */
31 if (!XParseColor(ob_display, render_colormap, colorname, &xcol)) {
32 g_warning("unable to parse color '%s'", colorname);
35 return color_new(xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
38 color_rgb *color_new(int r, int g, int b)
40 /* this should be replaced with something far cooler */
43 xcol.red = (r << 8) | r;
44 xcol.green = (g << 8) | g;
45 xcol.blue = (b << 8) | b;
46 if (XAllocColor(ob_display, render_colormap, &xcol)) {
47 out = g_new(color_rgb, 1);
48 out->r = xcol.red >> 8;
49 out->g = xcol.green >> 8;
50 out->b = xcol.blue >> 8;
52 out->pixel = xcol.pixel;
58 /*XXX same color could be pointed to twice, this might have to be a refcount*/
60 void color_free(color_rgb *c)
64 XFreeGC(ob_display, c->gc);
69 void reduce_depth(pixel32 *data, XImage *im)
73 pixel32 *p32 = (pixel32 *) im->data;
74 pixel16 *p16 = (pixel16 *) im->data;
75 unsigned char *p8 = (unsigned char *)im->data;
76 switch (im->bits_per_pixel) {
78 if ((render_red_offset != default_red_offset) ||
79 (render_blue_offset != default_blue_offset) ||
80 (render_green_offset != default_green_offset)) {
81 for (y = 0; y < im->height; y++) {
82 for (x = 0; x < im->width; x++) {
83 r = (data[x] >> default_red_offset) & 0xFF;
84 g = (data[x] >> default_green_offset) & 0xFF;
85 b = (data[x] >> default_blue_offset) & 0xFF;
86 p32[x] = (r << render_red_shift)
87 + (g << render_green_shift)
88 + (b << render_blue_shift);
93 } else im->data = (char*) data;
96 for (y = 0; y < im->height; y++) {
97 for (x = 0; x < im->width; x++) {
98 r = (data[x] >> default_red_offset) & 0xFF;
99 r = r >> render_red_shift;
100 g = (data[x] >> default_green_offset) & 0xFF;
101 g = g >> render_green_shift;
102 b = (data[x] >> default_blue_offset) & 0xFF;
103 b = b >> render_blue_shift;
104 p16[x] = (r << render_red_offset)
105 + (g << render_green_offset)
106 + (b << render_blue_offset);
109 p16 += im->bytes_per_line/2;
113 g_assert(render_visual->class != TrueColor);
114 for (y = 0; y < im->height; y++) {
115 for (x = 0; x < im->width; x++) {
116 p8[x] = pickColor(data[x] >> default_red_offset,
117 data[x] >> default_green_offset,
118 data[x] >> default_blue_offset)->pixel;
121 p8 += im->bytes_per_line;
126 g_message("your bit depth is currently unhandled\n");
130 XColor *pickColor(int r, int g, int b)
132 r = (r & 0xff) >> (8-pseudo_bpc);
133 g = (g & 0xff) >> (8-pseudo_bpc);
134 b = (b & 0xff) >> (8-pseudo_bpc);
135 return &pseudo_colors[(r << (2*pseudo_bpc)) + (g << (1*pseudo_bpc)) + b];
138 static void swap_byte_order(XImage *im)
142 g_message("SWAPPING BYTE ORDER");
145 for (y = 0; y < im->height; ++y) {
146 for (x = 0; x < im->height; ++x) {
147 char *c = &im->data[di + x * im->bits_per_pixel / 8];
150 switch (im->bits_per_pixel) {
162 g_message("your bit depth is currently unhandled\n");
165 di += im->bytes_per_line;
168 if (im->byte_order == LSBFirst)
169 im->byte_order = MSBFirst;
171 im->byte_order = LSBFirst;
174 void increase_depth(pixel32 *data, XImage *im)
178 pixel32 *p32 = (pixel32 *) im->data;
179 pixel16 *p16 = (pixel16 *) im->data;
180 unsigned char *p8 = (unsigned char *)im->data;
182 if (im->byte_order != render_endian)
185 switch (im->bits_per_pixel) {
187 for (y = 0; y < im->height; y++) {
188 for (x = 0; x < im->width; x++) {
189 r = (p32[x] >> render_red_offset) & 0xff;
190 g = (p32[x] >> render_green_offset) & 0xff;
191 b = (p32[x] >> render_blue_offset) & 0xff;
192 data[x] = (r << default_red_offset)
193 + (g << default_green_offset)
194 + (b << default_blue_offset)
195 + (0xff << default_alpha_offset);
198 p32 += im->bytes_per_line/4;
202 for (y = 0; y < im->height; y++) {
203 for (x = 0; x < im->width; x++) {
204 r = (p16[x] & render_red_mask) >> render_red_offset <<
206 g = (p16[x] & render_green_mask) >> render_green_offset <<
208 b = (p16[x] & render_blue_mask) >> render_blue_offset <<
210 data[x] = (r << default_red_offset)
211 + (g << default_green_offset)
212 + (b << default_blue_offset)
213 + (0xff << default_alpha_offset);
216 p16 += im->bytes_per_line/2;
220 g_assert(render_visual->class != TrueColor);
221 for (y = 0; y < im->height; y++) {
222 for (x = 0; x < im->width; x++) {
225 gulong dev, closest = 0xffffffff, close = 0;
227 icolor.pixel = p8[x];
228 XQueryColor(ob_display, render_colormap, &icolor);
230 /* find the nearest color match */
231 for (ii = 0; ii < pseudo_ncolors(); ii++) {
232 /* find deviations */
233 r = (pseudo_colors[ii].red - icolor.red) & 0xff;
234 g = (pseudo_colors[ii].green - icolor.green) & 0xff;
235 b = (pseudo_colors[ii].blue - icolor.blue) & 0xff;
236 /* find a weighted absolute deviation */
237 dev = (r * r) * (0xff - (icolor.red & 0xff)) +
238 (g * g) * (0xff - (icolor.green & 0xff)) +
239 (b * b) * (0xff - (icolor.blue & 0xff));
247 (pseudo_colors[close].red & 0xff <<
248 default_red_offset) +
249 (pseudo_colors[close].green & 0xff <<
250 default_green_offset) +
251 (pseudo_colors[close].blue & 0xff <<
252 default_blue_offset) +
253 (0xff << default_alpha_offset);
256 p8 += im->bytes_per_line;
261 g_message("your bit depth is currently unhandled\n");