We shouldn't enforce incr when the app resizes itself. But it's so confusing.
[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) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana 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_message("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 RrColor *RrColorCopy(RrColor* c)
105 {
106     return RrColorNew(c->inst, c->r, c->g, c->b);
107 }
108
109 void RrColorFree(RrColor *c)
110 {
111     if (c) {
112         if (--c->refcount < 1) {
113 #ifndef NO_COLOR_CACHE
114             g_assert(g_hash_table_lookup(RrColorHash(c->inst), &c->key));
115             g_hash_table_remove(RrColorHash(c->inst), &c->key);
116 #endif
117             if (c->pixel) XFreeColors(RrDisplay(c->inst), RrColormap(c->inst),
118                                       &c->pixel, 1, 0);
119             if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
120             g_free(c);
121         }
122     }
123 }
124
125 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
126 {
127     gint r, g, b;
128     gint x,y;
129     RrPixel32 *p32 = (RrPixel32 *) im->data;
130     RrPixel16 *p16 = (RrPixel16 *) im->data;
131     RrPixel8  *p8  = (RrPixel8 *)  im->data;
132     switch (im->bits_per_pixel) {
133     case 32:
134         if ((RrRedOffset(inst) != RrDefaultRedOffset) ||
135             (RrBlueOffset(inst) != RrDefaultBlueOffset) ||
136             (RrGreenOffset(inst) != RrDefaultGreenOffset)) {
137             for (y = 0; y < im->height; y++) {
138                 for (x = 0; x < im->width; x++) {
139                     r = (data[x] >> RrDefaultRedOffset) & 0xFF;
140                     g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
141                     b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
142                     p32[x] = (r << RrRedOffset(inst))
143                            + (g << RrGreenOffset(inst))
144                            + (b << RrBlueOffset(inst));
145                 }
146                 data += im->width;
147                 p32 += im->width;
148             }
149         } else im->data = (gchar*) data;
150         break;
151     case 16:
152         for (y = 0; y < im->height; y++) {
153             for (x = 0; x < im->width; x++) {
154                 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
155                 r = r >> RrRedShift(inst);
156                 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
157                 g = g >> RrGreenShift(inst);
158                 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
159                 b = b >> RrBlueShift(inst);
160                 p16[x] = (r << RrRedOffset(inst))
161                        + (g << RrGreenOffset(inst))
162                        + (b << RrBlueOffset(inst));
163             }
164             data += im->width;
165             p16 += im->bytes_per_line/2;
166         }
167         break;
168     case 8:
169         if (RrVisual(inst)->class == TrueColor) {
170             for (y = 0; y < im->height; y++) {
171                 for (x = 0; x < im->width; x++) {
172                     r = (data[x] >> RrDefaultRedOffset) & 0xFF;
173                     r = r >> RrRedShift(inst);
174                     g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
175                     g = g >> RrGreenShift(inst);
176                     b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
177                     b = b >> RrBlueShift(inst);
178                     p8[x] = (r << RrRedOffset(inst))
179                         + (g << RrGreenOffset(inst))
180                         + (b << RrBlueOffset(inst));
181                 }
182                 data += im->width;
183                 p8 += im->bytes_per_line;
184             }
185         } else {
186             for (y = 0; y < im->height; y++) {
187                 for (x = 0; x < im->width; x++) {
188                     p8[x] = RrPickColor(inst,
189                                         data[x] >> RrDefaultRedOffset,
190                                         data[x] >> RrDefaultGreenOffset,
191                                         data[x] >> RrDefaultBlueOffset)->pixel;
192                 }
193                 data += im->width;
194                 p8 += im->bytes_per_line;
195             }
196         }
197         break;
198     default:
199         g_error("Your bit depth is currently unhandled\n");
200
201     }
202 }
203
204 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b)
205 {
206   r = (r & 0xff) >> (8-RrPseudoBPC(inst));
207   g = (g & 0xff) >> (8-RrPseudoBPC(inst));
208   b = (b & 0xff) >> (8-RrPseudoBPC(inst));
209   return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
210                                (g << (1*RrPseudoBPC(inst))) +
211                                b];
212 }
213
214 static void swap_byte_order(XImage *im)
215 {
216     gint x, y, di;
217
218     di = 0;
219     for (y = 0; y < im->height; ++y) {
220         for (x = 0; x < im->height; ++x) {
221             gchar *c = &im->data[di + x * im->bits_per_pixel / 8];
222             gchar t;
223
224             switch (im->bits_per_pixel) {
225             case 32:
226                 t = c[2];
227                 c[2] = c[3];
228                 c[3] = t;
229             case 16:
230                 t = c[0];
231                 c[0] = c[1];
232                 c[1] = t;
233             case 8:
234             case 1:
235                 break;
236             default:
237                 g_error("Your bit depth (%i) is currently unhandled",
238                         im->bits_per_pixel);
239             }
240         }
241         di += im->bytes_per_line;
242     }
243
244     if (im->byte_order == LSBFirst)
245         im->byte_order = MSBFirst;
246     else
247         im->byte_order = LSBFirst;
248 }
249
250 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
251 {
252     gint r, g, b;
253     gint x,y;
254     RrPixel32 *p32 = (RrPixel32 *) im->data;
255     RrPixel16 *p16 = (RrPixel16 *) im->data;
256     guchar *p8 = (guchar *)im->data;
257
258     if (im->byte_order != LSBFirst)
259         swap_byte_order(im);
260
261     switch (im->bits_per_pixel) {
262     case 32:
263         for (y = 0; y < im->height; y++) {
264             for (x = 0; x < im->width; x++) {
265                 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
266                 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
267                 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
268                 data[x] = (r << RrDefaultRedOffset)
269                     + (g << RrDefaultGreenOffset)
270                     + (b << RrDefaultBlueOffset)
271                     + (0xff << RrDefaultAlphaOffset);
272             }
273             data += im->width;
274             p32 += im->bytes_per_line/4;
275         }
276         break;
277     case 16:
278         for (y = 0; y < im->height; y++) {
279             for (x = 0; x < im->width; x++) {
280                 r = (p16[x] & RrRedMask(inst)) >>
281                     RrRedOffset(inst) <<
282                     RrRedShift(inst);
283                 g = (p16[x] & RrGreenMask(inst)) >>
284                     RrGreenOffset(inst) <<
285                     RrGreenShift(inst);
286                 b = (p16[x] & RrBlueMask(inst)) >>
287                     RrBlueOffset(inst) <<
288                     RrBlueShift(inst);
289                 data[x] = (r << RrDefaultRedOffset)
290                     + (g << RrDefaultGreenOffset)
291                     + (b << RrDefaultBlueOffset)
292                     + (0xff << RrDefaultAlphaOffset);
293             }
294             data += im->width;
295             p16 += im->bytes_per_line/2;
296         }
297         break;
298     case 8:
299         g_error("This image bit depth (%i) is currently unhandled", 8);
300         break;
301     case 1:
302         for (y = 0; y < im->height; y++) {
303             for (x = 0; x < im->width; x++) {
304                 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
305                     data[x] = 0xff << RrDefaultAlphaOffset; /* black */
306                 else
307                     data[x] = 0xffffffff; /* white */
308             }
309             data += im->width;
310             p8 += im->bytes_per_line;
311         }
312         break;
313     default:
314         g_error("This image bit depth (%i) is currently unhandled",
315                 im->bits_per_pixel);
316     }
317 }
318
319 gint RrColorRed(const RrColor *c)
320 {
321     return c->r;
322 }
323
324 gint RrColorGreen(const RrColor *c)
325 {
326     return c->g;
327 }
328
329 gint RrColorBlue(const RrColor *c)
330 {
331     return c->b;
332 }
333
334 gulong RrColorPixel(const RrColor *c)
335 {
336     return c->pixel;
337 }
338
339 GC RrColorGC(RrColor *c)
340 {
341     if (!c->gc)
342         RrColorAllocateGC(c);
343     return c->gc;
344 }