4 static RrInstance *definst = NULL;
6 void RrTrueColorSetup (RrInstance *inst);
7 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;
75 g_message("Initializing PseudoColor RenderControl\n");
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);
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);
88 /* build a color cube */
89 inst->pseudo_colors = g_new(XColor, _ncolors);
90 cpc = 1 << inst->pseudo_bpc; /* colors per channel */
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;
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 */
111 /* try allocate any colors that failed allocation above */
113 /* get the allocated values from the X server
114 (only the first 256 XXX why!?)
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);
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);
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;
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;
150 /* wtf has gone wrong, its already alloced for chissake! */
151 g_assert_not_reached();
156 void RrInstanceFree (RrInstance *inst)
159 if (inst == definst) definst = NULL;
160 g_free(inst->pseudo_colors);
164 Display* RrDisplay (const RrInstance *inst)
166 return (inst ? inst : definst)->display;
169 gint RrScreen (const RrInstance *inst)
171 return (inst ? inst : definst)->screen;
174 Window RrRootWindow (const RrInstance *inst)
176 return RootWindow (RrDisplay (inst), RrScreen (inst));
179 Visual *RrVisual (const RrInstance *inst)
181 return (inst ? inst : definst)->visual;
184 gint RrDepth (const RrInstance *inst)
186 return (inst ? inst : definst)->depth;
189 Colormap RrColormap (const RrInstance *inst)
191 return (inst ? inst : definst)->colormap;
194 gint RrRedOffset (const RrInstance *inst)
196 return (inst ? inst : definst)->red_offset;
199 gint RrGreenOffset (const RrInstance *inst)
201 return (inst ? inst : definst)->green_offset;
204 gint RrBlueOffset (const RrInstance *inst)
206 return (inst ? inst : definst)->blue_offset;
209 gint RrRedShift (const RrInstance *inst)
211 return (inst ? inst : definst)->red_shift;
214 gint RrGreenShift (const RrInstance *inst)
216 return (inst ? inst : definst)->green_shift;
219 gint RrBlueShift (const RrInstance *inst)
221 return (inst ? inst : definst)->blue_shift;
224 gint RrRedMask (const RrInstance *inst)
226 return (inst ? inst : definst)->red_mask;
229 gint RrGreenMask (const RrInstance *inst)
231 return (inst ? inst : definst)->green_mask;
234 gint RrBlueMask (const RrInstance *inst)
236 return (inst ? inst : definst)->blue_mask;
239 guint RrPseudoBPC (const RrInstance *inst)
241 return (inst ? inst : definst)->pseudo_bpc;
244 XColor *RrPseudoColors (const RrInstance *inst)
246 return (inst ? inst : definst)->pseudo_colors;