handle increase_depth on 1bpp images
[mikachu/openbox.git] / 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 #include "../kernel/openbox.h"
7
8 XColor *pseudo_colors;
9 int pseudo_bpc;
10
11 void color_allocate_gc(color_rgb *in)
12 {
13     XGCValues gcv;
14
15     gcv.foreground = in->pixel;
16     gcv.cap_style = CapProjecting;
17     in->gc = XCreateGC(ob_display, ob_root, GCForeground | GCCapStyle, &gcv);
18 }
19
20 color_rgb *color_parse(char *colorname)
21 {
22     XColor xcol;
23
24     g_assert(colorname != NULL);
25     /* get rgb values from colorname */
26
27     xcol.red = 0;
28     xcol.green = 0;
29     xcol.blue = 0;
30     xcol.pixel = 0;
31     if (!XParseColor(ob_display, render_colormap, colorname, &xcol)) {
32         g_warning("unable to parse color '%s'", colorname);
33         return NULL;
34     }
35     return color_new(xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
36 }
37
38 color_rgb *color_new(int r, int g, int b)
39 {
40 /* this should be replaced with something far cooler */
41     color_rgb *out;
42     XColor xcol;
43     xcol.red = (r << 8) | r;
44     xcol.green = (g << 8) | g;
45     xcol.blue = (b << 8) | b;
46     if (XAllocColor(ob_display, render_colormap, &xcol)) {
47         out = g_new(color_rgb, 1);
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         return out;
54     }
55     return NULL;
56 }
57
58 /*XXX same color could be pointed to twice, this might have to be a refcount*/
59
60 void color_free(color_rgb *c)
61 {
62     if (c != NULL) {
63         if (c->gc != None)
64             XFreeGC(ob_display, c->gc);
65         g_free(c);
66     }
67 }
68
69 void reduce_depth(pixel32 *data, XImage *im)
70 {
71     int r, g, b;
72     int x,y;
73     pixel32 *p32 = (pixel32 *) im->data;
74     pixel16 *p16 = (pixel16 *) im->data;
75     unsigned char *p8 = (unsigned char *)im->data;
76     switch (im->bits_per_pixel) {
77     case 32:
78         if ((render_red_offset != default_red_offset) ||
79             (render_blue_offset != default_blue_offset) ||
80             (render_green_offset != default_green_offset)) {
81             for (y = 0; y < im->height; y++) {
82                 for (x = 0; x < im->width; x++) {
83                     r = (data[x] >> default_red_offset) & 0xFF;
84                     g = (data[x] >> default_green_offset) & 0xFF;
85                     b = (data[x] >> default_blue_offset) & 0xFF;
86                     p32[x] = (r << render_red_shift)
87                            + (g << render_green_shift)
88                            + (b << render_blue_shift);
89                 }
90                 data += im->width;
91                 p32 += im->width;
92             } 
93         } else im->data = (char*) data;
94         break;
95     case 16:
96         for (y = 0; y < im->height; y++) {
97             for (x = 0; x < im->width; x++) {
98                 r = (data[x] >> default_red_offset) & 0xFF;
99                 r = r >> render_red_shift;
100                 g = (data[x] >> default_green_offset) & 0xFF;
101                 g = g >> render_green_shift;
102                 b = (data[x] >> default_blue_offset) & 0xFF;
103                 b = b >> render_blue_shift;
104                 p16[x] = (r << render_red_offset)
105                     + (g << render_green_offset)
106                     + (b << render_blue_offset);
107             }
108             data += im->width;
109             p16 += im->bytes_per_line/2;
110         }
111     break;
112     case 8:
113         g_assert(render_visual->class != TrueColor);
114         for (y = 0; y < im->height; y++) {
115             for (x = 0; x < im->width; x++) {
116                 p8[x] = pickColor(data[x] >> default_red_offset,
117                        data[x] >> default_green_offset,
118                        data[x] >> default_blue_offset)->pixel;
119         }
120         data += im->width;
121         p8 += im->bytes_per_line;
122   }
123
124     break;
125     default:
126         g_message("your bit depth is currently unhandled\n");
127     }
128 }
129
130 XColor *pickColor(int r, int g, int b) 
131 {
132   r = (r & 0xff) >> (8-pseudo_bpc);
133   g = (g & 0xff) >> (8-pseudo_bpc);
134   b = (b & 0xff) >> (8-pseudo_bpc);
135   return &pseudo_colors[(r << (2*pseudo_bpc)) + (g << (1*pseudo_bpc)) + 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 increase_depth(pixel32 *data, XImage *im)
175 {
176     int r, g, b;
177     int x,y;
178     pixel32 *p32 = (pixel32 *) im->data;
179     pixel16 *p16 = (pixel16 *) im->data;
180     unsigned char *p8 = (unsigned char *)im->data;
181
182     if (im->byte_order != render_endian)
183         swap_byte_order(im);
184
185     switch (im->bits_per_pixel) {
186     case 32:
187         g_message("increase 32");
188         for (y = 0; y < im->height; y++) {
189             for (x = 0; x < im->width; x++) {
190                 r = (p32[x] >> render_red_offset) & 0xff;
191                 g = (p32[x] >> render_green_offset) & 0xff;
192                 b = (p32[x] >> render_blue_offset) & 0xff;
193                 data[x] = (r << default_red_offset)
194                     + (g << default_green_offset)
195                     + (b << default_blue_offset)
196                     + (0xff << default_alpha_offset);
197             }
198             data += im->width;
199             p32 += im->bytes_per_line/4;
200         }
201         break;
202     case 16:
203         g_message("increase 16");
204         for (y = 0; y < im->height; y++) {
205             for (x = 0; x < im->width; x++) {
206                 r = (p16[x] & render_red_mask) >> render_red_offset <<
207                     render_red_shift;
208                 g = (p16[x] & render_green_mask) >> render_green_offset <<
209                     render_green_shift;
210                 b = (p16[x] & render_blue_mask) >> render_blue_offset <<
211                     render_blue_shift;
212                 data[x] = (r << default_red_offset)
213                     + (g << default_green_offset)
214                     + (b << default_blue_offset)
215                     + (0xff << default_alpha_offset);
216             }
217             data += im->width;
218             p16 += im->bytes_per_line/2;
219         }
220         break;
221     case 8:
222         g_message("this bit depth is currently unhandled\n");
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 << default_alpha_offset; /* 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_message("your bit depth is currently unhandled\n");
238     }
239 }