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