changed indentation to use spaces only
[wmaker-crm.git] / wrlib / CmapAlloc.c
blob20d0305c05c777481ade66b240aa8987fc448530
1 /* $XConsortium: CmapAlloc.c,v 1.9 94/04/17 20:15:52 rws Exp $ */
3 /*
5 Copyright (c) 1989, 1994 X Consortium
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 Except as contained in this notice, the name of the X Consortium shall not be
25 used in advertising or otherwise to promote the sale, use or other dealings
26 in this Software without prior written authorization from the X Consortium.
31 * Author: Donna Converse, MIT X Consortium
34 #include <X11/Xlib.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xutil.h>
37 #include <stdio.h>
39 #define lowbit(x) ((x) & (~(x) + 1))
41 static int default_allocation();
42 static void best_allocation();
43 static void gray_allocation();
44 static int icbrt();
45 static int icbrt_with_bits();
46 static int icbrt_with_guess();
48 /* To determine the best allocation of reds, greens, and blues in a
49 * standard colormap, use XmuGetColormapAllocation.
50 * vinfo specifies visual information for a chosen visual
51 * property specifies one of the standard colormap property names
52 * red_max returns maximum red value
53 * green_max returns maximum green value
54 * blue_max returns maximum blue value
56 * XmuGetColormapAllocation returns 0 on failure, non-zero on success.
57 * It is assumed that the visual is appropriate for the colormap property.
60 Status XmuGetColormapAllocation(vinfo, property, red_max, green_max, blue_max)
61 XVisualInfo *vinfo;
62 Atom property;
63 unsigned long *red_max, *green_max, *blue_max;
65 Status status = 1;
67 if (vinfo->colormap_size <= 2)
68 return 0;
70 switch (property) {
71 case XA_RGB_DEFAULT_MAP:
72 status = default_allocation(vinfo, red_max, green_max, blue_max);
73 break;
74 case XA_RGB_BEST_MAP:
75 best_allocation(vinfo, red_max, green_max, blue_max);
76 break;
77 case XA_RGB_GRAY_MAP:
78 gray_allocation(vinfo->colormap_size, red_max, green_max, blue_max);
79 break;
80 case XA_RGB_RED_MAP:
81 *red_max = vinfo->colormap_size - 1;
82 *green_max = *blue_max = 0;
83 break;
84 case XA_RGB_GREEN_MAP:
85 *green_max = vinfo->colormap_size - 1;
86 *red_max = *blue_max = 0;
87 break;
88 case XA_RGB_BLUE_MAP:
89 *blue_max = vinfo->colormap_size - 1;
90 *red_max = *green_max = 0;
91 break;
92 default:
93 status = 0;
95 return status;
98 /****************************************************************************/
99 /* Determine the appropriate color allocations of a gray scale.
101 * Keith Packard, MIT X Consortium
104 static void gray_allocation(n, red_max, green_max, blue_max)
105 int n; /* the number of cells of the gray scale */
106 unsigned long *red_max, *green_max, *blue_max;
108 *red_max = (n * 30) / 100;
109 *green_max = (n * 59) / 100;
110 *blue_max = (n * 11) / 100;
111 *green_max += ((n - 1) - (*red_max + *green_max + *blue_max));
114 /****************************************************************************/
115 /* Determine an appropriate color allocation for the RGB_DEFAULT_MAP.
116 * If a map has less than a minimum number of definable entries, we do not
117 * produce an allocation for an RGB_DEFAULT_MAP.
119 * For 16 planes, the default colormap will have 27 each RGB; for 12 planes,
120 * 12 each. For 8 planes, let n = the number of colormap entries, which may
121 * be 256 or 254. Then, maximum red value = floor(cube_root(n - 125)) - 1.
122 * Maximum green and maximum blue values are identical to maximum red.
123 * This leaves at least 125 cells which clients can allocate.
125 * Return 0 if an allocation has been determined, non-zero otherwise.
128 static int default_allocation(vinfo, red, green, blue)
129 XVisualInfo *vinfo;
130 unsigned long *red, *green, *blue;
132 int ngrays; /* number of gray cells */
134 switch (vinfo->class) {
135 case PseudoColor:
137 if (vinfo->colormap_size > 65000)
138 /* intended for displays with 16 planes */
139 *red = *green = *blue = (unsigned long) 27;
140 else if (vinfo->colormap_size > 4000)
141 /* intended for displays with 12 planes */
142 *red = *green = *blue = (unsigned long) 12;
143 else if (vinfo->colormap_size < 250)
144 return 0;
145 else
146 /* intended for displays with 8 planes */
147 *red = *green = *blue = (unsigned long)
148 (icbrt(vinfo->colormap_size - 125) - 1);
149 break;
151 case DirectColor:
153 if (vinfo->colormap_size < 10)
154 return 0;
155 *red = *green = *blue = vinfo->colormap_size / 2 - 1;
156 break;
158 case TrueColor:
160 *red = vinfo->red_mask / lowbit(vinfo->red_mask);
161 *green = vinfo->green_mask / lowbit(vinfo->green_mask);
162 *blue = vinfo->blue_mask / lowbit(vinfo->blue_mask);
163 break;
165 case GrayScale:
167 if (vinfo->colormap_size > 65000)
168 ngrays = 4096;
169 else if (vinfo->colormap_size > 4000)
170 ngrays = 512;
171 else if (vinfo->colormap_size < 250)
172 return 0;
173 else
174 ngrays = 12;
175 gray_allocation(ngrays, red, green, blue);
176 break;
178 default:
179 return 0;
181 return 1;
184 /****************************************************************************/
185 /* Determine an appropriate color allocation for the RGB_BEST_MAP.
187 * For a DirectColor or TrueColor visual, the allocation is determined
188 * by the red_mask, green_mask, and blue_mask members of the visual info.
190 * Otherwise, if the colormap size is an integral power of 2, determine
191 * the allocation according to the number of bits given to each color,
192 * with green getting more than red, and red more than blue, if there
193 * are to be inequities in the distribution. If the colormap size is
194 * not an integral power of 2, let n = the number of colormap entries.
195 * Then maximum red value = floor(cube_root(n)) - 1;
196 * maximum blue value = floor(cube_root(n)) - 1;
197 * maximum green value = n / ((# red values) * (# blue values)) - 1;
198 * Which, on a GPX, allows for 252 entries in the best map, out of 254
199 * defineable colormap entries.
202 static void best_allocation(vinfo, red, green, blue)
203 XVisualInfo *vinfo;
204 unsigned long *red, *green, *blue;
207 if (vinfo->class == DirectColor || vinfo->class == TrueColor) {
208 *red = vinfo->red_mask;
209 while ((*red & 01) == 0)
210 *red >>= 1;
211 *green = vinfo->green_mask;
212 while ((*green & 01) == 0)
213 *green >>=1;
214 *blue = vinfo->blue_mask;
215 while ((*blue & 01) == 0)
216 *blue >>= 1;
217 } else {
218 register int bits, n;
220 /* Determine n such that n is the least integral power of 2 which is
221 * greater than or equal to the number of entries in the colormap.
223 n = 1;
224 bits = 0;
225 while (vinfo->colormap_size > n) {
226 n = n << 1;
227 bits++;
230 /* If the number of entries in the colormap is a power of 2, determine
231 * the allocation by "dealing" the bits, first to green, then red, then
232 * blue. If not, find the maximum integral red, green, and blue values
233 * which, when multiplied together, do not exceed the number of
235 * colormap entries.
237 if (n == vinfo->colormap_size) {
238 register int r, g, b;
239 b = bits / 3;
240 g = b + ((bits % 3) ? 1 : 0);
241 r = b + (((bits % 3) == 2) ? 1 : 0);
242 *red = 1 << r;
243 *green = 1 << g;
244 *blue = 1 << b;
245 } else {
246 *red = icbrt_with_bits(vinfo->colormap_size, bits);
247 *blue = *red;
248 *green = (vinfo->colormap_size / ((*red) * (*blue)));
250 (*red)--;
251 (*green)--;
252 (*blue)--;
254 return;
258 * integer cube roots by Newton's method
260 * Stephen Gildea, MIT X Consortium, July 1991
263 static int icbrt(a) /* integer cube root */
264 int a;
266 register int bits = 0;
267 register unsigned n = a;
269 while (n) {
270 bits++;
271 n >>= 1;
273 return icbrt_with_bits(a, bits);
277 static int icbrt_with_bits(a, bits)
278 int a;
279 int bits; /* log 2 of a */
281 return icbrt_with_guess(a, a>>2*bits/3);
284 #ifdef DEBUG
285 int icbrt_loopcount;
286 #endif
288 /* Newton's Method: x_n+1 = x_n - ( f(x_n) / f'(x_n) ) */
290 /* for cube roots, x^3 - a = 0, x_new = x - 1/3 (x - a/x^2) */
293 * Quick and dirty cube roots. Nothing fancy here, just Newton's method.
294 * Only works for positive integers (since that's all we need).
295 * We actually return floor(cbrt(a)) because that's what we need here, too.
298 static int icbrt_with_guess(a, guess)
299 int a, guess;
301 register int delta;
303 #ifdef DEBUG
304 icbrt_loopcount = 0;
305 #endif
306 if (a <= 0)
307 return 0;
308 if (guess < 1)
309 guess = 1;
311 do {
312 #ifdef DEBUG
313 icbrt_loopcount++;
314 #endif
315 delta = (guess - a/(guess*guess))/3;
316 #ifdef DEBUG
317 printf("pass %d: guess=%d, delta=%d\n", icbrt_loopcount, guess, delta);
318 #endif
319 guess -= delta;
320 } while (delta != 0);
322 if (guess*guess*guess > a)
323 guess--;
325 return guess;