]> icculus.org git repositories - mikachu/openbox.git/blob - render/color.c
initial commit of split gradient, looks like some RrColor leaking perhaps
[mikachu/openbox.git] / render / color.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    color.c for the Openbox window manager
4    Copyright (c) 2003        Ben Jansens
5    Copyright (c) 2003        Derek Foreman
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "render.h"
21 #include "color.h"
22 #include "instance.h"
23
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <string.h>
27
28 void RrColorAllocateGC(RrColor *in)
29 {
30     XGCValues gcv;
31
32     gcv.foreground = in->pixel;
33     gcv.cap_style = CapProjecting;
34     in->gc = XCreateGC(RrDisplay(in->inst),
35                        RrRootWindow(in->inst),
36                        GCForeground | GCCapStyle, &gcv);
37 }
38
39 RrColor *RrColorParse(const RrInstance *inst, gchar *colorname)
40 {
41     XColor xcol;
42
43     g_assert(colorname != NULL);
44     /* get rgb values from colorname */
45
46     xcol.red = 0;
47     xcol.green = 0;
48     xcol.blue = 0;
49     xcol.pixel = 0;
50     if (!XParseColor(RrDisplay(inst), RrColormap(inst), colorname, &xcol)) {
51         g_warning("unable to parse color '%s'", colorname);
52         return NULL;
53     }
54     return RrColorNew(inst, xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
55 }
56
57 /*#define NO_COLOR_CACHE*/
58 #ifdef DEBUG
59 gint id;
60 #endif
61
62 RrColor *RrColorNew(const RrInstance *inst, gint r, gint g, gint b)
63 {
64     /* this should be replaced with something far cooler */
65     RrColor *out = NULL;
66     XColor xcol;
67     gint key;
68
69     key = (r << 24) + (g << 16) + (b << 8);
70 #ifndef NO_COLOR_CACHE
71     if ((out = g_hash_table_lookup(RrColorHash(inst), &key))) {
72         out->refcount++;
73     } else {
74 #endif
75         xcol.red = (r << 8) | r;
76         xcol.green = (g << 8) | g;
77         xcol.blue = (b << 8) | b;
78         if (XAllocColor(RrDisplay(inst), RrColormap(inst), &xcol)) {
79             out = g_new(RrColor, 1);
80             out->inst = inst;
81             out->r = xcol.red >> 8;
82             out->g = xcol.green >> 8;
83             out->b = xcol.blue >> 8;
84             out->gc = None;
85             out->pixel = xcol.pixel;
86             out->key = key;
87             out->refcount = 1;
88 #ifdef DEBUG
89             out->id = id++;
90 #endif
91 #ifndef NO_COLOR_CACHE
92             g_hash_table_insert(RrColorHash(inst), &out->key, out);
93         }
94 #endif
95     }
96     return out;
97 }
98
99 void RrColorFree(RrColor *c)
100 {
101     if (c) {
102         if (--c->refcount < 1) {
103 #ifndef NO_COLOR_CACHE
104             g_assert(g_hash_table_lookup(RrColorHash(c->inst), &c->key));
105             g_hash_table_remove(RrColorHash(c->inst), &c->key);
106 #endif
107             if (c->pixel) XFreeColors(RrDisplay(c->inst), RrColormap(c->inst),
108                                       &c->pixel, 1, 0);
109             if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
110             g_free(c);
111         }
112     }
113 }
114
115 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
116 {
117     gint r, g, b;
118     gint x,y;
119     RrPixel32 *p32 = (RrPixel32 *) im->data;
120     RrPixel16 *p16 = (RrPixel16 *) im->data;
121     guchar *p8 = (guchar *)im->data;
122     switch (im->bits_per_pixel) {
123     case 32:
124         if ((RrRedOffset(inst) != RrDefaultRedOffset) ||
125             (RrBlueOffset(inst) != RrDefaultBlueOffset) ||
126             (RrGreenOffset(inst) != RrDefaultGreenOffset)) {
127             for (y = 0; y < im->height; y++) {
128                 for (x = 0; x < im->width; x++) {
129                     r = (data[x] >> RrDefaultRedOffset) & 0xFF;
130                     g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
131                     b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
132                     p32[x] = (r << RrRedOffset(inst))
133                            + (g << RrGreenOffset(inst))
134                            + (b << RrBlueOffset(inst));
135                 }
136                 data += im->width;
137                 p32 += im->width;
138             } 
139         } else im->data = (gchar*) data;
140         break;
141     case 16:
142         for (y = 0; y < im->height; y++) {
143             for (x = 0; x < im->width; x++) {
144                 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
145                 r = r >> RrRedShift(inst);
146                 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
147                 g = g >> RrGreenShift(inst);
148                 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
149                 b = b >> RrBlueShift(inst);
150                 p16[x] = (r << RrRedOffset(inst))
151                        + (g << RrGreenOffset(inst))
152                        + (b << RrBlueOffset(inst));
153             }
154             data += im->width;
155             p16 += im->bytes_per_line/2;
156         }
157         break;
158     case 8:
159         g_assert(RrVisual(inst)->class != TrueColor);
160         for (y = 0; y < im->height; y++) {
161             for (x = 0; x < im->width; x++) {
162                 p8[x] = RrPickColor(inst,
163                                     data[x] >> RrDefaultRedOffset,
164                                     data[x] >> RrDefaultGreenOffset,
165                                     data[x] >> RrDefaultBlueOffset)->pixel;
166             }
167             data += im->width;
168             p8 += im->bytes_per_line;
169         }
170         break;
171     default:
172         g_warning("your bit depth is currently unhandled\n");
173     }
174 }
175
176 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b) 
177 {
178   r = (r & 0xff) >> (8-RrPseudoBPC(inst));
179   g = (g & 0xff) >> (8-RrPseudoBPC(inst));
180   b = (b & 0xff) >> (8-RrPseudoBPC(inst));
181   return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
182                                (g << (1*RrPseudoBPC(inst))) +
183                                b];
184 }
185
186 static void swap_byte_order(XImage *im)
187 {
188     gint x, y, di;
189
190     di = 0;
191     for (y = 0; y < im->height; ++y) {
192         for (x = 0; x < im->height; ++x) {
193             gchar *c = &im->data[di + x * im->bits_per_pixel / 8];
194             gchar t;
195
196             switch (im->bits_per_pixel) {
197             case 32:
198                 t = c[2];
199                 c[2] = c[3];
200                 c[3] = t;
201             case 16:
202                 t = c[0];
203                 c[0] = c[1];
204                 c[1] = t;
205             case 8:
206             case 1:
207                 break;
208             default:
209                 g_warning("Your bit depth is currently unhandled");
210             }
211         }
212         di += im->bytes_per_line;
213     }
214
215     if (im->byte_order == LSBFirst)
216         im->byte_order = MSBFirst;
217     else
218         im->byte_order = LSBFirst;
219 }
220
221 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
222 {
223     gint r, g, b;
224     gint x,y;
225     RrPixel32 *p32 = (RrPixel32 *) im->data;
226     RrPixel16 *p16 = (RrPixel16 *) im->data;
227     guchar *p8 = (guchar *)im->data;
228
229     if (im->byte_order != LSBFirst)
230         swap_byte_order(im);
231
232     switch (im->bits_per_pixel) {
233     case 32:
234         for (y = 0; y < im->height; y++) {
235             for (x = 0; x < im->width; x++) {
236                 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
237                 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
238                 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
239                 data[x] = (r << RrDefaultRedOffset)
240                     + (g << RrDefaultGreenOffset)
241                     + (b << RrDefaultBlueOffset)
242                     + (0xff << RrDefaultAlphaOffset);
243             }
244             data += im->width;
245             p32 += im->bytes_per_line/4;
246         }
247         break;
248     case 16:
249         for (y = 0; y < im->height; y++) {
250             for (x = 0; x < im->width; x++) {
251                 r = (p16[x] & RrRedMask(inst)) >>
252                     RrRedOffset(inst) <<
253                     RrRedShift(inst);
254                 g = (p16[x] & RrGreenMask(inst)) >>
255                     RrGreenOffset(inst) <<
256                     RrGreenShift(inst);
257                 b = (p16[x] & RrBlueMask(inst)) >>
258                     RrBlueOffset(inst) <<
259                     RrBlueShift(inst);
260                 data[x] = (r << RrDefaultRedOffset)
261                     + (g << RrDefaultGreenOffset)
262                     + (b << RrDefaultBlueOffset)
263                     + (0xff << RrDefaultAlphaOffset);
264             }
265             data += im->width;
266             p16 += im->bytes_per_line/2;
267         }
268         break;
269     case 8:
270         g_warning("this image bit depth is currently unhandled");
271         break;
272     case 1:
273         for (y = 0; y < im->height; y++) {
274             for (x = 0; x < im->width; x++) {
275                 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
276                     data[x] = 0xff << RrDefaultAlphaOffset; /* black */
277                 else
278                     data[x] = 0xffffffff; /* white */
279             }
280             data += im->width;
281             p8 += im->bytes_per_line;
282         }
283         break;
284     default:
285         g_warning("this image bit depth is currently unhandled");
286     }
287 }
288
289 gint RrColorRed(const RrColor *c)
290 {
291     return c->r;
292 }
293
294 gint RrColorGreen(const RrColor *c)
295 {
296     return c->g;
297 }
298
299 gint RrColorBlue(const RrColor *c)
300 {
301     return c->b;
302 }
303
304 gulong RrColorPixel(const RrColor *c)
305 {
306     return c->pixel;
307 }
308
309 GC RrColorGC(RrColor *c)
310 {
311     if (!c->gc)
312         RrColorAllocateGC(c);
313     return c->gc;
314 }