4 static RrInstance *definst = NULL;
6 static void RrTrueColorSetup (RrInstance *inst);
7 static void RrPseudoColorSetup (RrInstance *inst);
9 RrInstance* RrInstanceNew (Display *display, gint screen)
11 definst = g_new (RrInstance, 1);
12 definst->display = display;
13 definst->screen = screen;
15 definst->depth = DefaultDepth(display, screen);
16 definst->visual = DefaultVisual(display, screen);
17 definst->colormap = DefaultColormap(display, screen);
19 definst->pseudo_colors = NULL;
21 switch (definst->visual->class) {
23 RrTrueColorSetup(definst);
29 RrPseudoColorSetup(definst);
32 g_critical("Unsupported visual class");
34 return definst = NULL;
39 void RrTrueColorSetup (RrInstance *inst)
41 unsigned long red_mask, green_mask, blue_mask;
42 XImage *timage = NULL;
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;
53 inst->green_offset = 0;
54 inst->blue_offset = 0;
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; }
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--; }
67 #define RrPseudoNcolors(isnt) (1 << (inst->pseudo_bpc * 3))
69 void RrPseudoColorSetup (RrInstance *inst)
72 int tr, tg, tb, n, r, g, b, i, incolors, ii;
76 /* determine the number of colors and the bits-per-color */
77 inst->pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
78 g_assert(inst->pseudo_bpc >= 1);
79 _ncolors = RrPseudoNcolors(inst);
81 if (_ncolors > 1 << inst->depth) {
82 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
83 inst->pseudo_bpc = 1 << (inst->depth/3) >> 3;
84 _ncolors = 1 << (inst->pseudo_bpc * 3);
87 /* build a color cube */
88 inst->pseudo_colors = g_new(XColor, _ncolors);
89 cpc = 1 << inst->pseudo_bpc; /* colors per channel */
91 for (n = 0, r = 0; r < cpc; r++)
92 for (g = 0; g < cpc; g++)
93 for (b = 0; b < cpc; b++, n++) {
94 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
95 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
96 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
97 inst->pseudo_colors[n].red = tr | tr << 8;
98 inst->pseudo_colors[n].green = tg | tg << 8;
99 inst->pseudo_colors[n].blue = tb | tb << 8;
100 /* used to track allocation */
101 inst->pseudo_colors[n].flags = DoRed|DoGreen|DoBlue;
104 /* allocate the colors */
105 for (i = 0; i < _ncolors; i++)
106 if (!XAllocColor(inst->display, inst->colormap,
107 &inst->pseudo_colors[i]))
108 inst->pseudo_colors[i].flags = 0; /* mark it as unallocated */
110 /* try allocate any colors that failed allocation above */
112 /* get the allocated values from the X server
113 (only the first 256 XXX why!?)
115 incolors = (((1 << inst->depth) > 256) ? 256 : (1 << inst->depth));
116 for (i = 0; i < incolors; i++)
117 icolors[i].pixel = i;
118 XQueryColors(inst->display, inst->colormap, icolors, incolors);
120 /* try match unallocated ones */
121 for (i = 0; i < _ncolors; i++) {
122 if (!inst->pseudo_colors[i].flags) { /* if it wasn't allocated... */
123 unsigned long closest = 0xffffffff, close = 0;
124 for (ii = 0; ii < incolors; ii++) {
125 /* find deviations */
126 r = (inst->pseudo_colors[i].red - icolors[ii].red) & 0xff;
127 g = (inst->pseudo_colors[i].green - icolors[ii].green) & 0xff;
128 b = (inst->pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
129 /* find a weighted absolute deviation */
130 dev = (r * r) + (g * g) + (b * b);
138 inst->pseudo_colors[i].red = icolors[close].red;
139 inst->pseudo_colors[i].green = icolors[close].green;
140 inst->pseudo_colors[i].blue = icolors[close].blue;
141 inst->pseudo_colors[i].pixel = icolors[close].pixel;
143 /* try alloc this closest color, it had better succeed! */
144 if (XAllocColor(inst->display, inst->colormap,
145 &inst->pseudo_colors[i]))
146 /* mark as alloced */
147 inst->pseudo_colors[i].flags = DoRed|DoGreen|DoBlue;
149 /* wtf has gone wrong, its already alloced for chissake! */
150 g_assert_not_reached();
155 void RrInstanceFree (RrInstance *inst)
158 if (inst == definst) definst = NULL;
159 g_free(inst->pseudo_colors);
163 Display* RrDisplay (const RrInstance *inst)
165 return (inst ? inst : definst)->display;
168 gint RrScreen (const RrInstance *inst)
170 return (inst ? inst : definst)->screen;
173 Window RrRootWindow (const RrInstance *inst)
175 return RootWindow (RrDisplay (inst), RrScreen (inst));
178 Visual *RrVisual (const RrInstance *inst)
180 return (inst ? inst : definst)->visual;
183 gint RrDepth (const RrInstance *inst)
185 return (inst ? inst : definst)->depth;
188 Colormap RrColormap (const RrInstance *inst)
190 return (inst ? inst : definst)->colormap;
193 gint RrRedOffset (const RrInstance *inst)
195 return (inst ? inst : definst)->red_offset;
198 gint RrGreenOffset (const RrInstance *inst)
200 return (inst ? inst : definst)->green_offset;
203 gint RrBlueOffset (const RrInstance *inst)
205 return (inst ? inst : definst)->blue_offset;
208 gint RrRedShift (const RrInstance *inst)
210 return (inst ? inst : definst)->red_shift;
213 gint RrGreenShift (const RrInstance *inst)
215 return (inst ? inst : definst)->green_shift;
218 gint RrBlueShift (const RrInstance *inst)
220 return (inst ? inst : definst)->blue_shift;
223 gint RrRedMask (const RrInstance *inst)
225 return (inst ? inst : definst)->red_mask;
228 gint RrGreenMask (const RrInstance *inst)
230 return (inst ? inst : definst)->green_mask;
233 gint RrBlueMask (const RrInstance *inst)
235 return (inst ? inst : definst)->blue_mask;
238 guint RrPseudoBPC (const RrInstance *inst)
240 return (inst ? inst : definst)->pseudo_bpc;
243 XColor *RrPseudoColors (const RrInstance *inst)
245 return (inst ? inst : definst)->pseudo_colors;