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