]> Dogcows Code - chaz/openbox/blob - render/color.c
remove rependancy on geom.h from the kernel, provide what is used in its own geom.h
[chaz/openbox] / render / color.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3 #include <string.h>
4 #include "render.h"
5 #include "color.h"
6
7 void RrColorAllocateGC(RrColor *in)
8 {
9 XGCValues gcv;
10
11 gcv.foreground = in->pixel;
12 gcv.cap_style = CapProjecting;
13 in->gc = XCreateGC(RrDisplay(in->inst),
14 RrRootWindow(in->inst),
15 GCForeground | GCCapStyle, &gcv);
16 }
17
18 RrColor *RrColorParse(const RrInstance *inst, gchar *colorname)
19 {
20 XColor xcol;
21
22 g_assert(colorname != NULL);
23 /* get rgb values from colorname */
24
25 xcol.red = 0;
26 xcol.green = 0;
27 xcol.blue = 0;
28 xcol.pixel = 0;
29 if (!XParseColor(RrDisplay(inst), RrColormap(inst), colorname, &xcol)) {
30 g_warning("unable to parse color '%s'", colorname);
31 return NULL;
32 }
33 return RrColorNew(inst, xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
34 }
35
36 RrColor *RrColorNew(const RrInstance *inst, gint r, gint g, gint b)
37 {
38 /* this should be replaced with something far cooler */
39 RrColor *out = NULL;
40 XColor xcol;
41 xcol.red = (r << 8) | r;
42 xcol.green = (g << 8) | g;
43 xcol.blue = (b << 8) | b;
44 if (XAllocColor(RrDisplay(inst), RrColormap(inst), &xcol)) {
45 out = g_new(RrColor, 1);
46 out->inst = inst;
47 out->r = xcol.red >> 8;
48 out->g = xcol.green >> 8;
49 out->b = xcol.blue >> 8;
50 out->gc = None;
51 out->pixel = xcol.pixel;
52 }
53 return out;
54 }
55
56 /*XXX same color could be pointed to twice, this might have to be a refcount*/
57
58 void RrColorFree(RrColor *c)
59 {
60 if (c) {
61 if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
62 g_free(c);
63 }
64 }
65
66 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
67 {
68 int r, g, b;
69 int x,y;
70 RrPixel32 *p32 = (RrPixel32 *) im->data;
71 RrPixel16 *p16 = (RrPixel16 *) im->data;
72 unsigned char *p8 = (unsigned char *)im->data;
73 switch (im->bits_per_pixel) {
74 case 32:
75 if ((RrRedOffset(inst) != RrDefaultRedOffset) ||
76 (RrBlueOffset(inst) != RrDefaultBlueOffset) ||
77 (RrGreenOffset(inst) != RrDefaultGreenOffset)) {
78 g_message("CONVERSION %d->%d %d->%d %d->%d",
79 RrDefaultRedOffset, RrRedOffset(inst),
80 RrDefaultBlueOffset, RrGreenOffset(inst),
81 RrDefaultGreenOffset, RrBlueOffset(inst));
82 for (y = 0; y < im->height; y++) {
83 for (x = 0; x < im->width; x++) {
84 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
85 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
86 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
87 p32[x] = (r << RrRedOffset(inst))
88 + (g << RrGreenOffset(inst))
89 + (b << RrBlueOffset(inst));
90 }
91 data += im->width;
92 p32 += im->width;
93 }
94 } else im->data = (char*) data;
95 break;
96 case 16:
97 for (y = 0; y < im->height; y++) {
98 for (x = 0; x < im->width; x++) {
99 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
100 r = r >> RrRedShift(inst);
101 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
102 g = g >> RrGreenShift(inst);
103 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
104 b = b >> RrBlueShift(inst);
105 p16[x] = (r << RrRedOffset(inst))
106 + (g << RrGreenOffset(inst))
107 + (b << RrBlueOffset(inst));
108 }
109 data += im->width;
110 p16 += im->bytes_per_line/2;
111 }
112 break;
113 case 8:
114 g_assert(RrVisual(inst)->class != TrueColor);
115 for (y = 0; y < im->height; y++) {
116 for (x = 0; x < im->width; x++) {
117 p8[x] = RrPickColor(inst,
118 data[x] >> RrDefaultRedOffset,
119 data[x] >> RrDefaultGreenOffset,
120 data[x] >> RrDefaultBlueOffset)->pixel;
121 }
122 data += im->width;
123 p8 += im->bytes_per_line;
124 }
125
126 break;
127 default:
128 g_message("your bit depth is currently unhandled\n");
129 }
130 }
131
132 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b)
133 {
134 r = (r & 0xff) >> (8-RrPseudoBPC(inst));
135 g = (g & 0xff) >> (8-RrPseudoBPC(inst));
136 b = (b & 0xff) >> (8-RrPseudoBPC(inst));
137 return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
138 (g << (1*RrPseudoBPC(inst))) +
139 b];
140 }
141
142 static void swap_byte_order(XImage *im)
143 {
144 int x, y, di;
145
146 g_message("SWAPPING BYTE ORDER");
147
148 di = 0;
149 for (y = 0; y < im->height; ++y) {
150 for (x = 0; x < im->height; ++x) {
151 char *c = &im->data[di + x * im->bits_per_pixel / 8];
152 char t;
153
154 switch (im->bits_per_pixel) {
155 case 32:
156 t = c[2];
157 c[2] = c[3];
158 c[3] = t;
159 case 16:
160 t = c[0];
161 c[0] = c[1];
162 c[1] = t;
163 case 8:
164 break;
165 default:
166 g_message("your bit depth is currently unhandled\n");
167 }
168 }
169 di += im->bytes_per_line;
170 }
171
172 if (im->byte_order == LSBFirst)
173 im->byte_order = MSBFirst;
174 else
175 im->byte_order = LSBFirst;
176 }
177
178 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
179 {
180 int r, g, b;
181 int x,y;
182 RrPixel32 *p32 = (RrPixel32 *) im->data;
183 RrPixel16 *p16 = (RrPixel16 *) im->data;
184 unsigned char *p8 = (unsigned char *)im->data;
185
186 if (im->byte_order != RrEndian)
187 swap_byte_order(im);
188
189 switch (im->bits_per_pixel) {
190 case 32:
191 for (y = 0; y < im->height; y++) {
192 for (x = 0; x < im->width; x++) {
193 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
194 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
195 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
196 data[x] = (r << RrDefaultRedOffset)
197 + (g << RrDefaultGreenOffset)
198 + (b << RrDefaultBlueOffset)
199 + (0xff << RrDefaultAlphaOffset);
200 }
201 data += im->width;
202 p32 += im->bytes_per_line/4;
203 }
204 break;
205 case 16:
206 for (y = 0; y < im->height; y++) {
207 for (x = 0; x < im->width; x++) {
208 r = (p16[x] & RrRedMask(inst)) >>
209 RrRedOffset(inst) <<
210 RrRedShift(inst);
211 g = (p16[x] & RrGreenMask(inst)) >>
212 RrGreenOffset(inst) <<
213 RrGreenShift(inst);
214 b = (p16[x] & RrBlueMask(inst)) >>
215 RrBlueOffset(inst) <<
216 RrBlueShift(inst);
217 data[x] = (r << RrDefaultRedOffset)
218 + (g << RrDefaultGreenOffset)
219 + (b << RrDefaultBlueOffset)
220 + (0xff << RrDefaultAlphaOffset);
221 }
222 data += im->width;
223 p16 += im->bytes_per_line/2;
224 }
225 break;
226 case 8:
227 g_message("this image bit depth is currently unhandled\n");
228 break;
229 case 1:
230 for (y = 0; y < im->height; y++) {
231 for (x = 0; x < im->width; x++) {
232 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
233 data[x] = 0xff << RrDefaultAlphaOffset; /* black */
234 else
235 data[x] = 0xffffffff; /* white */
236 }
237 data += im->width;
238 p8 += im->bytes_per_line;
239 }
240 break;
241 default:
242 g_message("this image bit depth is currently unhandled\n");
243 }
244 }
245
246 int RrColorRed(const RrColor *c)
247 {
248 return c->r;
249 }
250
251 int RrColorGreen(const RrColor *c)
252 {
253 return c->g;
254 }
255
256 int RrColorBlue(const RrColor *c)
257 {
258 return c->b;
259 }
260
261 gulong RrColorPixel(const RrColor *c)
262 {
263 return c->pixel;
264 }
This page took 0.048294 seconds and 5 git commands to generate.