9 void RrColorAllocateGC(RrColor *in)
13 gcv.foreground = in->pixel;
14 gcv.cap_style = CapProjecting;
15 in->gc = XCreateGC(RrDisplay(in->inst),
16 RrRootWindow(in->inst),
17 GCForeground | GCCapStyle, &gcv);
20 RrColor *RrColorParse(const RrInstance *inst, gchar *colorname)
24 g_assert(colorname != NULL);
25 /* get rgb values from colorname */
31 if (!XParseColor(RrDisplay(inst), RrColormap(inst), colorname, &xcol)) {
32 g_warning("unable to parse color '%s'", colorname);
35 return RrColorNew(inst, xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
38 RrColor *RrColorNew(const RrInstance *inst, gint r, gint g, gint b)
40 /* this should be replaced with something far cooler */
45 key = (r << 24) + (g << 16) + (b << 8);
46 if ((out = g_hash_table_lookup(RrColorHash(inst), &key))) {
49 xcol.red = (r << 8) | r;
50 xcol.green = (g << 8) | g;
51 xcol.blue = (b << 8) | b;
52 if (XAllocColor(RrDisplay(inst), RrColormap(inst), &xcol)) {
53 out = g_new(RrColor, 1);
55 out->r = xcol.red >> 8;
56 out->g = xcol.green >> 8;
57 out->b = xcol.blue >> 8;
59 out->pixel = xcol.pixel;
62 g_hash_table_replace(RrColorHash(inst), &out->key, out);
68 /*XXX same color could be pointed to twice, this might have to be a refcount*/
70 void RrColorFree(RrColor *c)
73 if (--c->refcount < 1) {
74 g_hash_table_remove(RrColorHash(c->inst), &c->key);
75 if (c->pixel) XFreeColors(RrDisplay(c->inst), RrColormap(c->inst),
77 if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
83 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
87 RrPixel32 *p32 = (RrPixel32 *) im->data;
88 RrPixel16 *p16 = (RrPixel16 *) im->data;
89 unsigned char *p8 = (unsigned char *)im->data;
90 switch (im->bits_per_pixel) {
92 if ((RrRedOffset(inst) != RrDefaultRedOffset) ||
93 (RrBlueOffset(inst) != RrDefaultBlueOffset) ||
94 (RrGreenOffset(inst) != RrDefaultGreenOffset)) {
95 for (y = 0; y < im->height; y++) {
96 for (x = 0; x < im->width; x++) {
97 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
98 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
99 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
100 p32[x] = (r << RrRedOffset(inst))
101 + (g << RrGreenOffset(inst))
102 + (b << RrBlueOffset(inst));
107 } else im->data = (char*) data;
110 for (y = 0; y < im->height; y++) {
111 for (x = 0; x < im->width; x++) {
112 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
113 r = r >> RrRedShift(inst);
114 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
115 g = g >> RrGreenShift(inst);
116 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
117 b = b >> RrBlueShift(inst);
118 p16[x] = (r << RrRedOffset(inst))
119 + (g << RrGreenOffset(inst))
120 + (b << RrBlueOffset(inst));
123 p16 += im->bytes_per_line/2;
127 g_assert(RrVisual(inst)->class != TrueColor);
128 for (y = 0; y < im->height; y++) {
129 for (x = 0; x < im->width; x++) {
130 p8[x] = RrPickColor(inst,
131 data[x] >> RrDefaultRedOffset,
132 data[x] >> RrDefaultGreenOffset,
133 data[x] >> RrDefaultBlueOffset)->pixel;
136 p8 += im->bytes_per_line;
141 g_warning("your bit depth is currently unhandled\n");
145 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b)
147 r = (r & 0xff) >> (8-RrPseudoBPC(inst));
148 g = (g & 0xff) >> (8-RrPseudoBPC(inst));
149 b = (b & 0xff) >> (8-RrPseudoBPC(inst));
150 return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
151 (g << (1*RrPseudoBPC(inst))) +
155 static void swap_byte_order(XImage *im)
160 for (y = 0; y < im->height; ++y) {
161 for (x = 0; x < im->height; ++x) {
162 char *c = &im->data[di + x * im->bits_per_pixel / 8];
165 switch (im->bits_per_pixel) {
177 g_warning("your bit depth is currently unhandled");
180 di += im->bytes_per_line;
183 if (im->byte_order == LSBFirst)
184 im->byte_order = MSBFirst;
186 im->byte_order = LSBFirst;
189 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
193 RrPixel32 *p32 = (RrPixel32 *) im->data;
194 RrPixel16 *p16 = (RrPixel16 *) im->data;
195 unsigned char *p8 = (unsigned char *)im->data;
197 if (im->byte_order != LSBFirst)
200 switch (im->bits_per_pixel) {
202 for (y = 0; y < im->height; y++) {
203 for (x = 0; x < im->width; x++) {
204 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
205 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
206 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
207 data[x] = (r << RrDefaultRedOffset)
208 + (g << RrDefaultGreenOffset)
209 + (b << RrDefaultBlueOffset)
210 + (0xff << RrDefaultAlphaOffset);
213 p32 += im->bytes_per_line/4;
217 for (y = 0; y < im->height; y++) {
218 for (x = 0; x < im->width; x++) {
219 r = (p16[x] & RrRedMask(inst)) >>
222 g = (p16[x] & RrGreenMask(inst)) >>
223 RrGreenOffset(inst) <<
225 b = (p16[x] & RrBlueMask(inst)) >>
226 RrBlueOffset(inst) <<
228 data[x] = (r << RrDefaultRedOffset)
229 + (g << RrDefaultGreenOffset)
230 + (b << RrDefaultBlueOffset)
231 + (0xff << RrDefaultAlphaOffset);
234 p16 += im->bytes_per_line/2;
238 g_warning("this image bit depth is currently unhandled");
241 for (y = 0; y < im->height; y++) {
242 for (x = 0; x < im->width; x++) {
243 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
244 data[x] = 0xff << RrDefaultAlphaOffset; /* black */
246 data[x] = 0xffffffff; /* white */
249 p8 += im->bytes_per_line;
253 g_warning("this image bit depth is currently unhandled");
257 int RrColorRed(const RrColor *c)
262 int RrColorGreen(const RrColor *c)
267 int RrColorBlue(const RrColor *c)
272 gulong RrColorPixel(const RrColor *c)
277 GC RrColorGC(RrColor *c) /* XXX make this const RrColor* when the GCs are in
278 a cache.. if possible? */
281 RrColorAllocateGC(c);