]> Dogcows Code - chaz/openbox/blob - render/color.c
used RrColorShift when RrColorOffset was what we wanted
[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 for (y = 0; y < im->height; y++) {
79 for (x = 0; x < im->width; x++) {
80 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
81 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
82 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
83 p32[x] = (r << RrRedOffset(inst))
84 + (g << RrGreenOffset(inst))
85 + (b << RrBlueOffset(inst));
86 }
87 data += im->width;
88 p32 += im->width;
89 }
90 } else im->data = (char*) data;
91 break;
92 case 16:
93 for (y = 0; y < im->height; y++) {
94 for (x = 0; x < im->width; x++) {
95 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
96 r = r >> RrRedShift(inst);
97 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
98 g = g >> RrGreenShift(inst);
99 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
100 b = b >> RrBlueShift(inst);
101 p16[x] = (r << RrRedOffset(inst))
102 + (g << RrGreenOffset(inst))
103 + (b << RrBlueOffset(inst));
104 }
105 data += im->width;
106 p16 += im->bytes_per_line/2;
107 }
108 break;
109 case 8:
110 g_assert(RrVisual(inst)->class != TrueColor);
111 for (y = 0; y < im->height; y++) {
112 for (x = 0; x < im->width; x++) {
113 p8[x] = RrPickColor(inst,
114 data[x] >> RrDefaultRedOffset,
115 data[x] >> RrDefaultGreenOffset,
116 data[x] >> RrDefaultBlueOffset)->pixel;
117 }
118 data += im->width;
119 p8 += im->bytes_per_line;
120 }
121
122 break;
123 default:
124 g_message("your bit depth is currently unhandled\n");
125 }
126 }
127
128 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b)
129 {
130 r = (r & 0xff) >> (8-RrPseudoBPC(inst));
131 g = (g & 0xff) >> (8-RrPseudoBPC(inst));
132 b = (b & 0xff) >> (8-RrPseudoBPC(inst));
133 return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
134 (g << (1*RrPseudoBPC(inst))) +
135 b];
136 }
137
138 static void swap_byte_order(XImage *im)
139 {
140 int x, y, di;
141
142 g_message("SWAPPING BYTE ORDER");
143
144 di = 0;
145 for (y = 0; y < im->height; ++y) {
146 for (x = 0; x < im->height; ++x) {
147 char *c = &im->data[di + x * im->bits_per_pixel / 8];
148 char t;
149
150 switch (im->bits_per_pixel) {
151 case 32:
152 t = c[2];
153 c[2] = c[3];
154 c[3] = t;
155 case 16:
156 t = c[0];
157 c[0] = c[1];
158 c[1] = t;
159 case 8:
160 break;
161 default:
162 g_message("your bit depth is currently unhandled\n");
163 }
164 }
165 di += im->bytes_per_line;
166 }
167
168 if (im->byte_order == LSBFirst)
169 im->byte_order = MSBFirst;
170 else
171 im->byte_order = LSBFirst;
172 }
173
174 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
175 {
176 int r, g, b;
177 int x,y;
178 RrPixel32 *p32 = (RrPixel32 *) im->data;
179 RrPixel16 *p16 = (RrPixel16 *) im->data;
180 unsigned char *p8 = (unsigned char *)im->data;
181
182 if (im->byte_order != RrEndian)
183 swap_byte_order(im);
184
185 switch (im->bits_per_pixel) {
186 case 32:
187 for (y = 0; y < im->height; y++) {
188 for (x = 0; x < im->width; x++) {
189 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
190 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
191 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
192 data[x] = (r << RrDefaultRedOffset)
193 + (g << RrDefaultGreenOffset)
194 + (b << RrDefaultBlueOffset)
195 + (0xff << RrDefaultAlphaOffset);
196 }
197 data += im->width;
198 p32 += im->bytes_per_line/4;
199 }
200 break;
201 case 16:
202 for (y = 0; y < im->height; y++) {
203 for (x = 0; x < im->width; x++) {
204 r = (p16[x] & RrRedMask(inst)) >>
205 RrRedOffset(inst) <<
206 RrRedShift(inst);
207 g = (p16[x] & RrGreenMask(inst)) >>
208 RrGreenOffset(inst) <<
209 RrGreenShift(inst);
210 b = (p16[x] & RrBlueMask(inst)) >>
211 RrBlueOffset(inst) <<
212 RrBlueShift(inst);
213 data[x] = (r << RrDefaultRedOffset)
214 + (g << RrDefaultGreenOffset)
215 + (b << RrDefaultBlueOffset)
216 + (0xff << RrDefaultAlphaOffset);
217 }
218 data += im->width;
219 p16 += im->bytes_per_line/2;
220 }
221 break;
222 case 8:
223 g_message("this image bit depth is currently unhandled\n");
224 break;
225 case 1:
226 for (y = 0; y < im->height; y++) {
227 for (x = 0; x < im->width; x++) {
228 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
229 data[x] = 0xff << RrDefaultAlphaOffset; /* black */
230 else
231 data[x] = 0xffffffff; /* white */
232 }
233 data += im->width;
234 p8 += im->bytes_per_line;
235 }
236 break;
237 default:
238 g_message("this image bit depth is currently unhandled\n");
239 }
240 }
This page took 0.04465 seconds and 5 git commands to generate.