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