comparison src/video/Xext/XmuStdCmap/CmapAlloc.c @ 2185:2032348afed1

This code adds support for DirectColor visuals to SDL 1.3. The support uses part of the Xmu library. To ensure that the library is available and to keep people form having to install yet another library I have added the essential parts of Xmu in src/video/extensions/XmuStdCmap and an include file in src/video/extensions. The support makes use of standard X11 mechanisms to create color maps and make sure that an application uses the same color map for each window/visual combination. This should make it possible for gamma support to be implemented based on a single color map per application. Hurm... it looks like "make indent" modified a few extra files. Those are getting committed too.
author Bob Pendleton <bob@pendleton.com>
date Thu, 12 Jul 2007 20:00:50 +0000
parents
children
comparison
equal deleted inserted replaced
2184:8f8516e79a13 2185:2032348afed1
1 /* $Xorg: CmapAlloc.c,v 1.4 2001/02/09 02:03:51 xorgcvs Exp $ */
2
3 /*
4
5 Copyright 1989, 1994, 1998 The Open Group
6
7 Permission to use, copy, modify, distribute, and sell this software and its
8 documentation for any purpose is hereby granted without fee, provided that
9 the above copyright notice appear in all copies and that both that
10 copyright notice and this permission notice appear in supporting
11 documentation.
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 Except as contained in this notice, the name of The Open Group shall not be
24 used in advertising or otherwise to promote the sale, use or other dealings
25 in this Software without prior written authorization from The Open Group.
26
27 */
28 /* $XFree86: xc/lib/Xmu/CmapAlloc.c,v 1.6 2001/01/17 19:42:53 dawes Exp $ */
29
30 /*
31 * Author: Donna Converse, MIT X Consortium
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #include <X11/Xlib.h>
38 #include <X11/Xatom.h>
39 #include <X11/Xutil.h>
40 #include "../extensions/StdCmap.h"
41 #include <stdio.h>
42
43 #define lowbit(x) ((x) & (~(x) + 1))
44
45 /*
46 * Prototypes
47 */
48 static void best_allocation(XVisualInfo *, unsigned long *, unsigned long *,
49 unsigned long *);
50 static int default_allocation(XVisualInfo *, unsigned long *,
51 unsigned long *, unsigned long *);
52 static void gray_allocation(int, unsigned long *, unsigned long *,
53 unsigned long *);
54 static int icbrt(int);
55 static int icbrt_with_bits(int, int);
56 static int icbrt_with_guess(int, int);
57
58 /* To determine the best allocation of reds, greens, and blues in a
59 * standard colormap, use XmuGetColormapAllocation.
60 * vinfo specifies visual information for a chosen visual
61 * property specifies one of the standard colormap property names
62 * red_max returns maximum red value
63 * green_max returns maximum green value
64 * blue_max returns maximum blue value
65 *
66 * XmuGetColormapAllocation returns 0 on failure, non-zero on success.
67 * It is assumed that the visual is appropriate for the colormap property.
68 */
69
70 Status
71 XmuGetColormapAllocation(XVisualInfo * vinfo, Atom property,
72 unsigned long *red_max,
73 unsigned long *green_max, unsigned long *blue_max)
74 {
75 Status status = 1;
76
77 if (vinfo->colormap_size <= 2)
78 return 0;
79
80 switch (property) {
81 case XA_RGB_DEFAULT_MAP:
82 status = default_allocation(vinfo, red_max, green_max, blue_max);
83 break;
84 case XA_RGB_BEST_MAP:
85 best_allocation(vinfo, red_max, green_max, blue_max);
86 break;
87 case XA_RGB_GRAY_MAP:
88 gray_allocation(vinfo->colormap_size, red_max, green_max, blue_max);
89 break;
90 case XA_RGB_RED_MAP:
91 *red_max = vinfo->colormap_size - 1;
92 *green_max = *blue_max = 0;
93 break;
94 case XA_RGB_GREEN_MAP:
95 *green_max = vinfo->colormap_size - 1;
96 *red_max = *blue_max = 0;
97 break;
98 case XA_RGB_BLUE_MAP:
99 *blue_max = vinfo->colormap_size - 1;
100 *red_max = *green_max = 0;
101 break;
102 default:
103 status = 0;
104 }
105 return status;
106 }
107
108 /****************************************************************************/
109 /* Determine the appropriate color allocations of a gray scale.
110 *
111 * Keith Packard, MIT X Consortium
112 */
113
114 static void
115 gray_allocation(int n, unsigned long *red_max, unsigned long *green_max,
116 unsigned long *blue_max)
117 {
118 *red_max = (n * 30) / 100;
119 *green_max = (n * 59) / 100;
120 *blue_max = (n * 11) / 100;
121 *green_max += ((n - 1) - (*red_max + *green_max + *blue_max));
122 }
123
124 /****************************************************************************/
125 /* Determine an appropriate color allocation for the RGB_DEFAULT_MAP.
126 * If a map has less than a minimum number of definable entries, we do not
127 * produce an allocation for an RGB_DEFAULT_MAP.
128 *
129 * For 16 planes, the default colormap will have 27 each RGB; for 12 planes,
130 * 12 each. For 8 planes, let n = the number of colormap entries, which may
131 * be 256 or 254. Then, maximum red value = floor(cube_root(n - 125)) - 1.
132 * Maximum green and maximum blue values are identical to maximum red.
133 * This leaves at least 125 cells which clients can allocate.
134 *
135 * Return 0 if an allocation has been determined, non-zero otherwise.
136 */
137
138 static int
139 default_allocation(XVisualInfo * vinfo, unsigned long *red,
140 unsigned long *green, unsigned long *blue)
141 {
142 int ngrays; /* number of gray cells */
143
144 switch (vinfo->class) {
145 case PseudoColor:
146
147 if (vinfo->colormap_size > 65000)
148 /* intended for displays with 16 planes */
149 *red = *green = *blue = (unsigned long) 27;
150 else if (vinfo->colormap_size > 4000)
151 /* intended for displays with 12 planes */
152 *red = *green = *blue = (unsigned long) 12;
153 else if (vinfo->colormap_size < 250)
154 return 0;
155 else
156 /* intended for displays with 8 planes */
157 *red = *green = *blue = (unsigned long)
158 (icbrt(vinfo->colormap_size - 125) - 1);
159 break;
160
161 case DirectColor:
162
163 if (vinfo->colormap_size < 10)
164 return 0;
165 *red = *green = *blue = vinfo->colormap_size / 2 - 1;
166 break;
167
168 case TrueColor:
169
170 *red = vinfo->red_mask / lowbit(vinfo->red_mask);
171 *green = vinfo->green_mask / lowbit(vinfo->green_mask);
172 *blue = vinfo->blue_mask / lowbit(vinfo->blue_mask);
173 break;
174
175 case GrayScale:
176
177 if (vinfo->colormap_size > 65000)
178 ngrays = 4096;
179 else if (vinfo->colormap_size > 4000)
180 ngrays = 512;
181 else if (vinfo->colormap_size < 250)
182 return 0;
183 else
184 ngrays = 12;
185 gray_allocation(ngrays, red, green, blue);
186 break;
187
188 default:
189 return 0;
190 }
191 return 1;
192 }
193
194 /****************************************************************************/
195 /* Determine an appropriate color allocation for the RGB_BEST_MAP.
196 *
197 * For a DirectColor or TrueColor visual, the allocation is determined
198 * by the red_mask, green_mask, and blue_mask members of the visual info.
199 *
200 * Otherwise, if the colormap size is an integral power of 2, determine
201 * the allocation according to the number of bits given to each color,
202 * with green getting more than red, and red more than blue, if there
203 * are to be inequities in the distribution. If the colormap size is
204 * not an integral power of 2, let n = the number of colormap entries.
205 * Then maximum red value = floor(cube_root(n)) - 1;
206 * maximum blue value = floor(cube_root(n)) - 1;
207 * maximum green value = n / ((# red values) * (# blue values)) - 1;
208 * Which, on a GPX, allows for 252 entries in the best map, out of 254
209 * defineable colormap entries.
210 */
211
212 static void
213 best_allocation(XVisualInfo * vinfo, unsigned long *red, unsigned long *green,
214 unsigned long *blue)
215 {
216
217 if (vinfo->class == DirectColor || vinfo->class == TrueColor) {
218 *red = vinfo->red_mask;
219 while ((*red & 01) == 0)
220 *red >>= 1;
221 *green = vinfo->green_mask;
222 while ((*green & 01) == 0)
223 *green >>= 1;
224 *blue = vinfo->blue_mask;
225 while ((*blue & 01) == 0)
226 *blue >>= 1;
227 } else {
228 register int bits, n;
229
230 /* Determine n such that n is the least integral power of 2 which is
231 * greater than or equal to the number of entries in the colormap.
232 */
233 n = 1;
234 bits = 0;
235 while (vinfo->colormap_size > n) {
236 n = n << 1;
237 bits++;
238 }
239
240 /* If the number of entries in the colormap is a power of 2, determine
241 * the allocation by "dealing" the bits, first to green, then red, then
242 * blue. If not, find the maximum integral red, green, and blue values
243 * which, when multiplied together, do not exceed the number of
244
245 * colormap entries.
246 */
247 if (n == vinfo->colormap_size) {
248 register int r, g, b;
249 b = bits / 3;
250 g = b + ((bits % 3) ? 1 : 0);
251 r = b + (((bits % 3) == 2) ? 1 : 0);
252 *red = 1 << r;
253 *green = 1 << g;
254 *blue = 1 << b;
255 } else {
256 *red = icbrt_with_bits(vinfo->colormap_size, bits);
257 *blue = *red;
258 *green = (vinfo->colormap_size / ((*red) * (*blue)));
259 }
260 (*red)--;
261 (*green)--;
262 (*blue)--;
263 }
264 return;
265 }
266
267 /*
268 * integer cube roots by Newton's method
269 *
270 * Stephen Gildea, MIT X Consortium, July 1991
271 */
272
273 static int
274 icbrt(int a)
275 {
276 register int bits = 0;
277 register unsigned n = a;
278
279 while (n) {
280 bits++;
281 n >>= 1;
282 }
283 return icbrt_with_bits(a, bits);
284 }
285
286
287 static int
288 icbrt_with_bits(int a, int bits)
289 /* bits - log 2 of a */
290 {
291 return icbrt_with_guess(a, a >> 2 * bits / 3);
292 }
293
294 #ifdef _X_ROOT_STATS
295 int icbrt_loopcount;
296 #endif
297
298 /* Newton's Method: x_n+1 = x_n - ( f(x_n) / f'(x_n) ) */
299
300 /* for cube roots, x^3 - a = 0, x_new = x - 1/3 (x - a/x^2) */
301
302 /*
303 * Quick and dirty cube roots. Nothing fancy here, just Newton's method.
304 * Only works for positive integers (since that's all we need).
305 * We actually return floor(cbrt(a)) because that's what we need here, too.
306 */
307
308 static int
309 icbrt_with_guess(int a, int guess)
310 {
311 register int delta;
312
313 #ifdef _X_ROOT_STATS
314 icbrt_loopcount = 0;
315 #endif
316 if (a <= 0)
317 return 0;
318 if (guess < 1)
319 guess = 1;
320
321 do {
322 #ifdef _X_ROOT_STATS
323 icbrt_loopcount++;
324 #endif
325 delta = (guess - a / (guess * guess)) / 3;
326 #ifdef DEBUG
327 printf("pass %d: guess=%d, delta=%d\n", icbrt_loopcount, guess,
328 delta);
329 #endif
330 guess -= delta;
331 } while (delta != 0);
332
333 if (guess * guess * guess > a)
334 guess--;
335
336 return guess;
337 }