]> icculus.org git repositories - dana/openbox.git/blob - render/color.c
mem leak
[dana/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 RrColor *RrColorNew(const RrInstance *inst, gint r, gint g, gint b)
58 {
59     /* this should be replaced with something far cooler */
60     RrColor *out = NULL;
61     XColor xcol;
62     gint key;
63
64     key = (r << 24) + (g << 16) + (b << 8);
65     if ((out = g_hash_table_lookup(RrColorHash(inst), &key))) {
66         out->refcount++;
67     } else {
68         xcol.red = (r << 8) | r;
69         xcol.green = (g << 8) | g;
70         xcol.blue = (b << 8) | b;
71         if (XAllocColor(RrDisplay(inst), RrColormap(inst), &xcol)) {
72             out = g_new(RrColor, 1);
73             out->inst = inst;
74             out->r = xcol.red >> 8;
75             out->g = xcol.green >> 8;
76             out->b = xcol.blue >> 8;
77             out->gc = None;
78             out->pixel = xcol.pixel;
79             out->key = key;
80             out->refcount = 1;
81             g_hash_table_insert(RrColorHash(inst), &out->key, out);
82         }
83     }
84     return out;
85 }
86
87 void RrColorFree(RrColor *c)
88 {
89     if (c) {
90         if (--c->refcount < 1) {
91             g_assert(g_hash_table_lookup(RrColorHash(c->inst), &c->key));
92             g_hash_table_remove(RrColorHash(c->inst), &c->key);
93             if (c->pixel) XFreeColors(RrDisplay(c->inst), RrColormap(c->inst),
94                                       &c->pixel, 1, 0);
95             if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
96             g_free(c);
97         }
98     }
99 }
100
101 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
102 {
103     int r, g, b;
104     int x,y;
105     RrPixel32 *p32 = (RrPixel32 *) im->data;
106     RrPixel16 *p16 = (RrPixel16 *) im->data;
107     unsigned char *p8 = (unsigned char *)im->data;
108     switch (im->bits_per_pixel) {
109     case 32:
110         if ((RrRedOffset(inst) != RrDefaultRedOffset) ||
111             (RrBlueOffset(inst) != RrDefaultBlueOffset) ||
112             (RrGreenOffset(inst) != RrDefaultGreenOffset)) {
113             for (y = 0; y < im->height; y++) {
114                 for (x = 0; x < im->width; x++) {
115                     r = (data[x] >> RrDefaultRedOffset) & 0xFF;
116                     g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
117                     b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
118                     p32[x] = (r << RrRedOffset(inst))
119                            + (g << RrGreenOffset(inst))
120                            + (b << RrBlueOffset(inst));
121                 }
122                 data += im->width;
123                 p32 += im->width;
124             } 
125         } else im->data = (char*) data;
126         break;
127     case 16:
128         for (y = 0; y < im->height; y++) {
129             for (x = 0; x < im->width; x++) {
130                 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
131                 r = r >> RrRedShift(inst);
132                 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
133                 g = g >> RrGreenShift(inst);
134                 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
135                 b = b >> RrBlueShift(inst);
136                 p16[x] = (r << RrRedOffset(inst))
137                        + (g << RrGreenOffset(inst))
138                        + (b << RrBlueOffset(inst));
139             }
140             data += im->width;
141             p16 += im->bytes_per_line/2;
142         }
143     break;
144     case 8:
145         g_assert(RrVisual(inst)->class != TrueColor);
146         for (y = 0; y < im->height; y++) {
147             for (x = 0; x < im->width; x++) {
148                 p8[x] = RrPickColor(inst,
149                                     data[x] >> RrDefaultRedOffset,
150                                     data[x] >> RrDefaultGreenOffset,
151                                     data[x] >> RrDefaultBlueOffset)->pixel;
152         }
153         data += im->width;
154         p8 += im->bytes_per_line;
155   }
156
157     break;
158     default:
159         g_warning("your bit depth is currently unhandled\n");
160     }
161 }
162
163 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b) 
164 {
165   r = (r & 0xff) >> (8-RrPseudoBPC(inst));
166   g = (g & 0xff) >> (8-RrPseudoBPC(inst));
167   b = (b & 0xff) >> (8-RrPseudoBPC(inst));
168   return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
169                                (g << (1*RrPseudoBPC(inst))) +
170                                b];
171 }
172
173 static void swap_byte_order(XImage *im)
174 {
175     int x, y, di;
176
177     di = 0;
178     for (y = 0; y < im->height; ++y) {
179         for (x = 0; x < im->height; ++x) {
180             char *c = &im->data[di + x * im->bits_per_pixel / 8];
181             char t;
182
183             switch (im->bits_per_pixel) {
184             case 32:
185                 t = c[2];
186                 c[2] = c[3];
187                 c[3] = t;
188             case 16:
189                 t = c[0];
190                 c[0] = c[1];
191                 c[1] = t;
192             case 8:
193                 break;
194             default:
195                 g_warning("your bit depth is currently unhandled");
196             }
197         }
198         di += im->bytes_per_line;
199     }
200
201     if (im->byte_order == LSBFirst)
202         im->byte_order = MSBFirst;
203     else
204         im->byte_order = LSBFirst;
205 }
206
207 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
208 {
209     int r, g, b;
210     int x,y;
211     RrPixel32 *p32 = (RrPixel32 *) im->data;
212     RrPixel16 *p16 = (RrPixel16 *) im->data;
213     unsigned char *p8 = (unsigned char *)im->data;
214
215     if (im->byte_order != LSBFirst)
216         swap_byte_order(im);
217
218     switch (im->bits_per_pixel) {
219     case 32:
220         for (y = 0; y < im->height; y++) {
221             for (x = 0; x < im->width; x++) {
222                 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
223                 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
224                 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
225                 data[x] = (r << RrDefaultRedOffset)
226                     + (g << RrDefaultGreenOffset)
227                     + (b << RrDefaultBlueOffset)
228                     + (0xff << RrDefaultAlphaOffset);
229             }
230             data += im->width;
231             p32 += im->bytes_per_line/4;
232         }
233         break;
234     case 16:
235         for (y = 0; y < im->height; y++) {
236             for (x = 0; x < im->width; x++) {
237                 r = (p16[x] & RrRedMask(inst)) >>
238                     RrRedOffset(inst) <<
239                     RrRedShift(inst);
240                 g = (p16[x] & RrGreenMask(inst)) >>
241                     RrGreenOffset(inst) <<
242                     RrGreenShift(inst);
243                 b = (p16[x] & RrBlueMask(inst)) >>
244                     RrBlueOffset(inst) <<
245                     RrBlueShift(inst);
246                 data[x] = (r << RrDefaultRedOffset)
247                     + (g << RrDefaultGreenOffset)
248                     + (b << RrDefaultBlueOffset)
249                     + (0xff << RrDefaultAlphaOffset);
250             }
251             data += im->width;
252             p16 += im->bytes_per_line/2;
253         }
254         break;
255     case 8:
256         g_warning("this image bit depth is currently unhandled");
257         break;
258     case 1:
259         for (y = 0; y < im->height; y++) {
260             for (x = 0; x < im->width; x++) {
261                 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
262                     data[x] = 0xff << RrDefaultAlphaOffset; /* black */
263                 else
264                     data[x] = 0xffffffff; /* white */
265             }
266             data += im->width;
267             p8 += im->bytes_per_line;
268         }
269         break;
270     default:
271         g_warning("this image bit depth is currently unhandled");
272     }
273 }
274
275 int RrColorRed(const RrColor *c)
276 {
277     return c->r;
278 }
279
280 int RrColorGreen(const RrColor *c)
281 {
282     return c->g;
283 }
284
285 int RrColorBlue(const RrColor *c)
286 {
287     return c->b;
288 }
289
290 gulong RrColorPixel(const RrColor *c)
291 {
292     return c->pixel;
293 }
294
295 GC RrColorGC(RrColor *c)
296 {
297     if (!c->gc)
298         RrColorAllocateGC(c);
299     return c->gc;
300 }