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 void RrColorFree(RrColor *c)
71 if (--c->refcount < 1) {
72 g_hash_table_remove(RrColorHash(c->inst), &c->key);
73 if (c->pixel) XFreeColors(RrDisplay(c->inst), RrColormap(c->inst),
75 if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
81 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
85 RrPixel32 *p32 = (RrPixel32 *) im->data;
86 RrPixel16 *p16 = (RrPixel16 *) im->data;
87 unsigned char *p8 = (unsigned char *)im->data;
88 switch (im->bits_per_pixel) {
90 if ((RrRedOffset(inst) != RrDefaultRedOffset) ||
91 (RrBlueOffset(inst) != RrDefaultBlueOffset) ||
92 (RrGreenOffset(inst) != RrDefaultGreenOffset)) {
93 for (y = 0; y < im->height; y++) {
94 for (x = 0; x < im->width; x++) {
95 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
96 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
97 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
98 p32[x] = (r << RrRedOffset(inst))
99 + (g << RrGreenOffset(inst))
100 + (b << RrBlueOffset(inst));
105 } else im->data = (char*) data;
108 for (y = 0; y < im->height; y++) {
109 for (x = 0; x < im->width; x++) {
110 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
111 r = r >> RrRedShift(inst);
112 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
113 g = g >> RrGreenShift(inst);
114 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
115 b = b >> RrBlueShift(inst);
116 p16[x] = (r << RrRedOffset(inst))
117 + (g << RrGreenOffset(inst))
118 + (b << RrBlueOffset(inst));
121 p16 += im->bytes_per_line/2;
125 g_assert(RrVisual(inst)->class != TrueColor);
126 for (y = 0; y < im->height; y++) {
127 for (x = 0; x < im->width; x++) {
128 p8[x] = RrPickColor(inst,
129 data[x] >> RrDefaultRedOffset,
130 data[x] >> RrDefaultGreenOffset,
131 data[x] >> RrDefaultBlueOffset)->pixel;
134 p8 += im->bytes_per_line;
139 g_warning("your bit depth is currently unhandled\n");
143 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b)
145 r = (r & 0xff) >> (8-RrPseudoBPC(inst));
146 g = (g & 0xff) >> (8-RrPseudoBPC(inst));
147 b = (b & 0xff) >> (8-RrPseudoBPC(inst));
148 return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
149 (g << (1*RrPseudoBPC(inst))) +
153 static void swap_byte_order(XImage *im)
158 for (y = 0; y < im->height; ++y) {
159 for (x = 0; x < im->height; ++x) {
160 char *c = &im->data[di + x * im->bits_per_pixel / 8];
163 switch (im->bits_per_pixel) {
175 g_warning("your bit depth is currently unhandled");
178 di += im->bytes_per_line;
181 if (im->byte_order == LSBFirst)
182 im->byte_order = MSBFirst;
184 im->byte_order = LSBFirst;
187 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
191 RrPixel32 *p32 = (RrPixel32 *) im->data;
192 RrPixel16 *p16 = (RrPixel16 *) im->data;
193 unsigned char *p8 = (unsigned char *)im->data;
195 if (im->byte_order != LSBFirst)
198 switch (im->bits_per_pixel) {
200 for (y = 0; y < im->height; y++) {
201 for (x = 0; x < im->width; x++) {
202 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
203 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
204 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
205 data[x] = (r << RrDefaultRedOffset)
206 + (g << RrDefaultGreenOffset)
207 + (b << RrDefaultBlueOffset)
208 + (0xff << RrDefaultAlphaOffset);
211 p32 += im->bytes_per_line/4;
215 for (y = 0; y < im->height; y++) {
216 for (x = 0; x < im->width; x++) {
217 r = (p16[x] & RrRedMask(inst)) >>
220 g = (p16[x] & RrGreenMask(inst)) >>
221 RrGreenOffset(inst) <<
223 b = (p16[x] & RrBlueMask(inst)) >>
224 RrBlueOffset(inst) <<
226 data[x] = (r << RrDefaultRedOffset)
227 + (g << RrDefaultGreenOffset)
228 + (b << RrDefaultBlueOffset)
229 + (0xff << RrDefaultAlphaOffset);
232 p16 += im->bytes_per_line/2;
236 g_warning("this image bit depth is currently unhandled");
239 for (y = 0; y < im->height; y++) {
240 for (x = 0; x < im->width; x++) {
241 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
242 data[x] = 0xff << RrDefaultAlphaOffset; /* black */
244 data[x] = 0xffffffff; /* white */
247 p8 += im->bytes_per_line;
251 g_warning("this image bit depth is currently unhandled");
255 int RrColorRed(const RrColor *c)
260 int RrColorGreen(const RrColor *c)
265 int RrColorBlue(const RrColor *c)
270 gulong RrColorPixel(const RrColor *c)
275 GC RrColorGC(RrColor *c)
278 RrColorAllocateGC(c);