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