]> Dogcows Code - chaz/openbox/blob - render/instance.c
rename color_rgb to RrColor
[chaz/openbox] / render / instance.c
1 #include "render.h"
2 #include "instance.h"
3
4 static RrInstance *definst = NULL;
5
6 void RrTrueColorSetup (RrInstance *inst);
7 void RrPseudoColorSetup (RrInstance *inst);
8
9 RrInstance* RrInstanceNew (Display *display, gint screen)
10 {
11 definst = g_new (RrInstance, 1);
12 definst->display = display;
13 definst->screen = screen;
14
15 definst->depth = DefaultDepth(display, screen);
16 definst->visual = DefaultVisual(display, screen);
17 definst->colormap = DefaultColormap(display, screen);
18
19 definst->pseudo_colors = NULL;
20
21 switch (definst->visual->class) {
22 case TrueColor:
23 RrTrueColorSetup(definst);
24 break;
25 case PseudoColor:
26 case StaticColor:
27 case GrayScale:
28 case StaticGray:
29 RrPseudoColorSetup(definst);
30 break;
31 default:
32 g_critical("Unsupported visual class");
33 g_free (definst);
34 return definst = NULL;
35 }
36 return definst;
37 }
38
39 void RrTrueColorSetup (RrInstance *inst)
40 {
41 unsigned long red_mask, green_mask, blue_mask;
42 XImage *timage = NULL;
43
44 timage = XCreateImage(inst->display, inst->visual, inst->depth,
45 ZPixmap, 0, NULL, 1, 1, 32, 0);
46 g_assert(timage != NULL);
47 /* find the offsets for each color in the visual's masks */
48 inst->red_mask = red_mask = timage->red_mask;
49 inst->green_mask = green_mask = timage->green_mask;
50 inst->blue_mask = blue_mask = timage->blue_mask;
51
52 inst->red_offset = 0;
53 inst->green_offset = 0;
54 inst->blue_offset = 0;
55
56 while (! (red_mask & 1)) { inst->red_offset++; red_mask >>= 1; }
57 while (! (green_mask & 1)) { inst->green_offset++; green_mask >>= 1; }
58 while (! (blue_mask & 1)) { inst->blue_offset++; blue_mask >>= 1; }
59
60 inst->red_shift = inst->green_shift = inst->blue_shift = 8;
61 while (red_mask) { red_mask >>= 1; inst->red_shift--; }
62 while (green_mask) { green_mask >>= 1; inst->green_shift--; }
63 while (blue_mask) { blue_mask >>= 1; inst->blue_shift--; }
64 XFree(timage);
65 }
66
67 #define RrPseudoNcolors(isnt) (1 << (inst->pseudo_bpc * 3))
68
69 void RrPseudoColorSetup (RrInstance *inst)
70 {
71 XColor icolors[256];
72 int tr, tg, tb, n, r, g, b, i, incolors, ii;
73 unsigned long dev;
74 int cpc, _ncolors;
75 g_message("Initializing PseudoColor RenderControl\n");
76
77 /* determine the number of colors and the bits-per-color */
78 inst->pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
79 g_assert(inst->pseudo_bpc >= 1);
80 _ncolors = RrPseudoNcolors(inst);
81
82 if (_ncolors > 1 << inst->depth) {
83 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
84 inst->pseudo_bpc = 1 << (inst->depth/3) >> 3;
85 _ncolors = 1 << (inst->pseudo_bpc * 3);
86 }
87
88 /* build a color cube */
89 inst->pseudo_colors = g_new(XColor, _ncolors);
90 cpc = 1 << inst->pseudo_bpc; /* colors per channel */
91
92 for (n = 0, r = 0; r < cpc; r++)
93 for (g = 0; g < cpc; g++)
94 for (b = 0; b < cpc; b++, n++) {
95 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
96 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
97 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
98 inst->pseudo_colors[n].red = tr | tr << 8;
99 inst->pseudo_colors[n].green = tg | tg << 8;
100 inst->pseudo_colors[n].blue = tb | tb << 8;
101 /* used to track allocation */
102 inst->pseudo_colors[n].flags = DoRed|DoGreen|DoBlue;
103 }
104
105 /* allocate the colors */
106 for (i = 0; i < _ncolors; i++)
107 if (!XAllocColor(inst->display, inst->colormap,
108 &inst->pseudo_colors[i]))
109 inst->pseudo_colors[i].flags = 0; /* mark it as unallocated */
110
111 /* try allocate any colors that failed allocation above */
112
113 /* get the allocated values from the X server
114 (only the first 256 XXX why!?)
115 */
116 incolors = (((1 << inst->depth) > 256) ? 256 : (1 << inst->depth));
117 for (i = 0; i < incolors; i++)
118 icolors[i].pixel = i;
119 XQueryColors(inst->display, inst->colormap, icolors, incolors);
120
121 /* try match unallocated ones */
122 for (i = 0; i < _ncolors; i++) {
123 if (!inst->pseudo_colors[i].flags) { /* if it wasn't allocated... */
124 unsigned long closest = 0xffffffff, close = 0;
125 for (ii = 0; ii < incolors; ii++) {
126 /* find deviations */
127 r = (inst->pseudo_colors[i].red - icolors[ii].red) & 0xff;
128 g = (inst->pseudo_colors[i].green - icolors[ii].green) & 0xff;
129 b = (inst->pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
130 /* find a weighted absolute deviation */
131 dev = (r * r) + (g * g) + (b * b);
132
133 if (dev < closest) {
134 closest = dev;
135 close = ii;
136 }
137 }
138
139 inst->pseudo_colors[i].red = icolors[close].red;
140 inst->pseudo_colors[i].green = icolors[close].green;
141 inst->pseudo_colors[i].blue = icolors[close].blue;
142 inst->pseudo_colors[i].pixel = icolors[close].pixel;
143
144 /* try alloc this closest color, it had better succeed! */
145 if (XAllocColor(inst->display, inst->colormap,
146 &inst->pseudo_colors[i]))
147 /* mark as alloced */
148 inst->pseudo_colors[i].flags = DoRed|DoGreen|DoBlue;
149 else
150 /* wtf has gone wrong, its already alloced for chissake! */
151 g_assert_not_reached();
152 }
153 }
154 }
155
156 void RrInstanceFree (RrInstance *inst)
157 {
158 if (inst) {
159 if (inst == definst) definst = NULL;
160 g_free(inst->pseudo_colors);
161 }
162 }
163
164 Display* RrDisplay (const RrInstance *inst)
165 {
166 return (inst ? inst : definst)->display;
167 }
168
169 gint RrScreen (const RrInstance *inst)
170 {
171 return (inst ? inst : definst)->screen;
172 }
173
174 Window RrRootWindow (const RrInstance *inst)
175 {
176 return RootWindow (RrDisplay (inst), RrScreen (inst));
177 }
178
179 Visual *RrVisual (const RrInstance *inst)
180 {
181 return (inst ? inst : definst)->visual;
182 }
183
184 gint RrDepth (const RrInstance *inst)
185 {
186 return (inst ? inst : definst)->depth;
187 }
188
189 Colormap RrColormap (const RrInstance *inst)
190 {
191 return (inst ? inst : definst)->colormap;
192 }
193
194 gint RrRedOffset (const RrInstance *inst)
195 {
196 return (inst ? inst : definst)->red_offset;
197 }
198
199 gint RrGreenOffset (const RrInstance *inst)
200 {
201 return (inst ? inst : definst)->green_offset;
202 }
203
204 gint RrBlueOffset (const RrInstance *inst)
205 {
206 return (inst ? inst : definst)->blue_offset;
207 }
208
209 gint RrRedShift (const RrInstance *inst)
210 {
211 return (inst ? inst : definst)->red_shift;
212 }
213
214 gint RrGreenShift (const RrInstance *inst)
215 {
216 return (inst ? inst : definst)->green_shift;
217 }
218
219 gint RrBlueShift (const RrInstance *inst)
220 {
221 return (inst ? inst : definst)->blue_shift;
222 }
223
224 gint RrRedMask (const RrInstance *inst)
225 {
226 return (inst ? inst : definst)->red_mask;
227 }
228
229 gint RrGreenMask (const RrInstance *inst)
230 {
231 return (inst ? inst : definst)->green_mask;
232 }
233
234 gint RrBlueMask (const RrInstance *inst)
235 {
236 return (inst ? inst : definst)->blue_mask;
237 }
238
239 guint RrPseudoBPC (const RrInstance *inst)
240 {
241 return (inst ? inst : definst)->pseudo_bpc;
242 }
243
244 XColor *RrPseudoColors (const RrInstance *inst)
245 {
246 return (inst ? inst : definst)->pseudo_colors;
247 }
This page took 0.044779 seconds and 4 git commands to generate.