]> icculus.org git repositories - mikachu/openbox.git/blob - render/color.c
there's the rect grad. now stop bugging me :(
[mikachu/openbox.git] / render / color.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3 #include <string.h>
4 #include "render.h"
5 #include "color.h"
6 #include "../kernel/openbox.h"
7
8 XColor *pseudo_colors;
9 int pseudo_bpc;
10
11 void color_allocate_gc(color_rgb *in)
12 {
13     XGCValues gcv;
14
15     gcv.foreground = in->pixel;
16     gcv.cap_style = CapProjecting;
17     in->gc = XCreateGC(ob_display, ob_root, GCForeground | GCCapStyle, &gcv);
18 }
19
20 color_rgb *color_parse(char *colorname)
21 {
22     XColor xcol;
23
24     g_assert(colorname != NULL);
25     /* get rgb values from colorname */
26
27     xcol.red = 0;
28     xcol.green = 0;
29     xcol.blue = 0;
30     xcol.pixel = 0;
31     if (!XParseColor(ob_display, render_colormap, colorname, &xcol)) {
32         g_warning("unable to parse color '%s'", colorname);
33         return NULL;
34     }
35     return color_new(xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
36 }
37
38 color_rgb *color_new(int r, int g, int b)
39 {
40 /* this should be replaced with something far cooler */
41     color_rgb *out;
42     XColor xcol;
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;
51         out->gc = None;
52         out->pixel = xcol.pixel;
53         return out;
54     }
55     return NULL;
56 }
57
58 /*XXX same color could be pointed to twice, this might have to be a refcount*/
59
60 void color_free(color_rgb *c)
61 {
62     if (c != NULL) {
63         if (c->gc != None)
64             XFreeGC(ob_display, c->gc);
65         g_free(c);
66     }
67 }
68
69 void reduce_depth(pixel32 *data, XImage *im)
70 {
71     int r, g, b;
72     int x,y;
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) {
77     case 32:
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);
89                 }
90                 data += im->width;
91                 p32 += im->width;
92             } 
93         } else im->data = (char*) data;
94         break;
95     case 16:
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);
107             }
108             data += im->width;
109             p16 += im->bytes_per_line/2;
110         }
111     break;
112     case 8:
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;
119         }
120         data += im->width;
121         p8 += im->bytes_per_line;
122   }
123
124     break;
125     default:
126         g_message("your bit depth is currently unhandled\n");
127     }
128 }
129
130 XColor *pickColor(int r, int g, int b) 
131 {
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];
136 }
137
138 static void swap_byte_order(XImage *im)
139 {
140     int x, y, di;
141
142     g_message("SWAPPING BYTE ORDER");
143
144     di = 0;
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];
148             char t;
149
150             switch (im->bits_per_pixel) {
151             case 32:
152                 t = c[2];
153                 c[2] = c[3];
154                 c[3] = t;
155             case 16:
156                 t = c[0];
157                 c[0] = c[1];
158                 c[1] = t;
159             case 8:
160                 break;
161             default:
162                 g_message("your bit depth is currently unhandled\n");
163             }
164         }
165         di += im->bytes_per_line;
166     }
167
168     if (im->byte_order == LSBFirst)
169         im->byte_order = MSBFirst;
170     else
171         im->byte_order = LSBFirst;
172 }
173
174 void increase_depth(pixel32 *data, XImage *im)
175 {
176     int r, g, b;
177     int x,y;
178     pixel32 *p32 = (pixel32 *) im->data;
179     pixel16 *p16 = (pixel16 *) im->data;
180     unsigned char *p8 = (unsigned char *)im->data;
181
182     if (im->byte_order != render_endian)
183         swap_byte_order(im);
184
185     switch (im->bits_per_pixel) {
186     case 32:
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);
196             }
197             data += im->width;
198             p32 += im->bytes_per_line/4;
199         }
200         break;
201     case 16:
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 <<
205                     render_red_shift;
206                 g = (p16[x] & render_green_mask) >> render_green_offset <<
207                     render_green_shift;
208                 b = (p16[x] & render_blue_mask) >> render_blue_offset <<
209                     render_blue_shift;
210                 data[x] = (r << default_red_offset)
211                     + (g << default_green_offset)
212                     + (b << default_blue_offset)
213                     + (0xff << default_alpha_offset);
214             }
215             data += im->width;
216             p16 += im->bytes_per_line/2;
217         }
218         break;
219     case 8:
220         g_assert(render_visual->class != TrueColor);
221         for (y = 0; y < im->height; y++) {
222             for (x = 0; x < im->width; x++) {
223                 XColor icolor;
224                 int ii, r, g, b;
225                 gulong dev, closest = 0xffffffff, close = 0;
226
227                 icolor.pixel = p8[x];
228                 XQueryColor(ob_display, render_colormap, &icolor);
229
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));
240
241                     if (dev < closest) {
242                         closest = dev;
243                         close = ii;
244                     }
245                 }
246                 data[x] =
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);
254         }
255         data += im->width;
256         p8 += im->bytes_per_line;
257   }
258
259     break;
260     default:
261         g_message("your bit depth is currently unhandled\n");
262     }
263 }