]> Dogcows Code - chaz/openbox/blob - render/color.c
Merge branch 'backport' into work
[chaz/openbox] / 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 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 RrPixel8 *p8 = (RrPixel8 *) 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 24:
147 {
148 /* reverse the ordering, shifting left 16bit should be the first byte
149 out of three, etc */
150 const guint roff = (16 - RrRedOffset(inst)) / 8;
151 const guint goff = (16 - RrGreenOffset(inst)) / 8;
152 const guint boff = (16 - RrBlueOffset(inst)) / 8;
153 gint outx;
154 for (y = 0; y < im->height; y++) {
155 for (x = 0, outx = 0; x < im->width; x++, outx += 3) {
156 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
157 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
158 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
159 p8[outx+roff] = r;
160 p8[outx+goff] = g;
161 p8[outx+boff] = b;
162 }
163 data += im->width;
164 p8 += im->bytes_per_line;
165 }
166 break;
167 }
168 case 16:
169 for (y = 0; y < im->height; y++) {
170 for (x = 0; x < im->width; x++) {
171 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
172 r = r >> RrRedShift(inst);
173 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
174 g = g >> RrGreenShift(inst);
175 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
176 b = b >> RrBlueShift(inst);
177 p16[x] = (r << RrRedOffset(inst))
178 + (g << RrGreenOffset(inst))
179 + (b << RrBlueOffset(inst));
180 }
181 data += im->width;
182 p16 += im->bytes_per_line/2;
183 }
184 break;
185 case 8:
186 if (RrVisual(inst)->class == TrueColor) {
187 for (y = 0; y < im->height; y++) {
188 for (x = 0; x < im->width; x++) {
189 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
190 r = r >> RrRedShift(inst);
191 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
192 g = g >> RrGreenShift(inst);
193 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
194 b = b >> RrBlueShift(inst);
195 p8[x] = (r << RrRedOffset(inst))
196 + (g << RrGreenOffset(inst))
197 + (b << RrBlueOffset(inst));
198 }
199 data += im->width;
200 p8 += im->bytes_per_line;
201 }
202 } else {
203 for (y = 0; y < im->height; y++) {
204 for (x = 0; x < im->width; x++) {
205 p8[x] = RrPickColor(inst,
206 data[x] >> RrDefaultRedOffset,
207 data[x] >> RrDefaultGreenOffset,
208 data[x] >> RrDefaultBlueOffset)->pixel;
209 }
210 data += im->width;
211 p8 += im->bytes_per_line;
212 }
213 }
214 break;
215 default:
216 g_error("This image bit depth (%i) is currently unhandled", im->bits_per_pixel);
217
218 }
219 }
220
221 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b)
222 {
223 r = (r & 0xff) >> (8-RrPseudoBPC(inst));
224 g = (g & 0xff) >> (8-RrPseudoBPC(inst));
225 b = (b & 0xff) >> (8-RrPseudoBPC(inst));
226 return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
227 (g << (1*RrPseudoBPC(inst))) +
228 b];
229 }
230
231 static void swap_byte_order(XImage *im)
232 {
233 gint x, y, di;
234
235 di = 0;
236 for (y = 0; y < im->height; ++y) {
237 for (x = 0; x < im->height; ++x) {
238 gchar *c = &im->data[di + x * im->bits_per_pixel / 8];
239 gchar t;
240
241 switch (im->bits_per_pixel) {
242 case 32:
243 t = c[2];
244 c[2] = c[3];
245 c[3] = t;
246 case 16:
247 t = c[0];
248 c[0] = c[1];
249 c[1] = t;
250 case 8:
251 case 1:
252 break;
253 default:
254 g_error("Your bit depth (%i) is currently unhandled",
255 im->bits_per_pixel);
256 }
257 }
258 di += im->bytes_per_line;
259 }
260
261 if (im->byte_order == LSBFirst)
262 im->byte_order = MSBFirst;
263 else
264 im->byte_order = LSBFirst;
265 }
266
267 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
268 {
269 gint r, g, b;
270 gint x,y;
271 RrPixel32 *p32 = (RrPixel32 *) im->data;
272 RrPixel16 *p16 = (RrPixel16 *) im->data;
273 guchar *p8 = (guchar *)im->data;
274
275 if (im->byte_order != LSBFirst)
276 swap_byte_order(im);
277
278 switch (im->bits_per_pixel) {
279 case 32:
280 for (y = 0; y < im->height; y++) {
281 for (x = 0; x < im->width; x++) {
282 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
283 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
284 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
285 data[x] = (r << RrDefaultRedOffset)
286 + (g << RrDefaultGreenOffset)
287 + (b << RrDefaultBlueOffset)
288 + (0xff << RrDefaultAlphaOffset);
289 }
290 data += im->width;
291 p32 += im->bytes_per_line/4;
292 }
293 break;
294 case 16:
295 for (y = 0; y < im->height; y++) {
296 for (x = 0; x < im->width; x++) {
297 r = (p16[x] & RrRedMask(inst)) >>
298 RrRedOffset(inst) <<
299 RrRedShift(inst);
300 g = (p16[x] & RrGreenMask(inst)) >>
301 RrGreenOffset(inst) <<
302 RrGreenShift(inst);
303 b = (p16[x] & RrBlueMask(inst)) >>
304 RrBlueOffset(inst) <<
305 RrBlueShift(inst);
306 data[x] = (r << RrDefaultRedOffset)
307 + (g << RrDefaultGreenOffset)
308 + (b << RrDefaultBlueOffset)
309 + (0xff << RrDefaultAlphaOffset);
310 }
311 data += im->width;
312 p16 += im->bytes_per_line/2;
313 }
314 break;
315 case 8:
316 g_error("This image bit depth (%i) is currently unhandled", 8);
317 break;
318 case 1:
319 for (y = 0; y < im->height; y++) {
320 for (x = 0; x < im->width; x++) {
321 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
322 data[x] = 0xff << RrDefaultAlphaOffset; /* black */
323 else
324 data[x] = 0xffffffff; /* white */
325 }
326 data += im->width;
327 p8 += im->bytes_per_line;
328 }
329 break;
330 default:
331 g_error("This image bit depth (%i) is currently unhandled",
332 im->bits_per_pixel);
333 }
334 }
335
336 gint RrColorRed(const RrColor *c)
337 {
338 return c->r;
339 }
340
341 gint RrColorGreen(const RrColor *c)
342 {
343 return c->g;
344 }
345
346 gint RrColorBlue(const RrColor *c)
347 {
348 return c->b;
349 }
350
351 gulong RrColorPixel(const RrColor *c)
352 {
353 return c->pixel;
354 }
355
356 GC RrColorGC(RrColor *c)
357 {
358 if (!c->gc)
359 RrColorAllocateGC(c);
360 return c->gc;
361 }
This page took 0.050259 seconds and 5 git commands to generate.