Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / CmapAlloc.c
1 /* $XConsortium: CmapAlloc.c,v 1.9 94/04/17 20:15:52 rws Exp $ */
2
3 /*
4
5 Copyright (c) 1989, 1994 X Consortium
6
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:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
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.
23
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.
27
28 */
29
30 /*
31 * Author: Donna Converse, MIT X Consortium
32 */
33
34 #include <X11/Xlib.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xutil.h>
37 #include <stdio.h>
38
39 #define lowbit(x) ((x) & (~(x) + 1))
40
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();
47
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
55 *
56 * XmuGetColormapAllocation returns 0 on failure, non-zero on success.
57 * It is assumed that the visual is appropriate for the colormap property.
58 */
59
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;
64 {
65 Status status = 1;
66
67 if (vinfo->colormap_size <= 2)
68 return 0;
69
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;
94 }
95 return status;
96 }
97
98 /****************************************************************************/
99 /* Determine the appropriate color allocations of a gray scale.
100 *
101 * Keith Packard, MIT X Consortium
102 */
103
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;
107 {
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));
112 }
113
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.
118 *
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.
124 *
125 * Return 0 if an allocation has been determined, non-zero otherwise.
126 */
127
128 static int default_allocation(vinfo, red, green, blue)
129 XVisualInfo *vinfo;
130 unsigned long *red, *green, *blue;
131 {
132 int ngrays; /* number of gray cells */
133
134 switch (vinfo->class) {
135 case PseudoColor:
136
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;
150
151 case DirectColor:
152
153 if (vinfo->colormap_size < 10)
154 return 0;
155 *red = *green = *blue = vinfo->colormap_size / 2 - 1;
156 break;
157
158 case TrueColor:
159
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;
164
165 case GrayScale:
166
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;
177
178 default:
179 return 0;
180 }
181 return 1;
182 }
183
184 /****************************************************************************/
185 /* Determine an appropriate color allocation for the RGB_BEST_MAP.
186 *
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.
189 *
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.
200 */
201
202 static void best_allocation(vinfo, red, green, blue)
203 XVisualInfo *vinfo;
204 unsigned long *red, *green, *blue;
205 {
206
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;
219
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.
222 */
223 n = 1;
224 bits = 0;
225 while (vinfo->colormap_size > n) {
226 n = n << 1;
227 bits++;
228 }
229
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
234
235 * colormap entries.
236 */
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)));
249 }
250 (*red)--;
251 (*green)--;
252 (*blue)--;
253 }
254 return;
255 }
256
257 /*
258 * integer cube roots by Newton's method
259 *
260 * Stephen Gildea, MIT X Consortium, July 1991
261 */
262
263 static int icbrt(a) /* integer cube root */
264 int a;
265 {
266 register int bits = 0;
267 register unsigned n = a;
268
269 while (n) {
270 bits++;
271 n >>= 1;
272 }
273 return icbrt_with_bits(a, bits);
274 }
275
276 static int icbrt_with_bits(a, bits)
277 int a;
278 int bits; /* log 2 of a */
279 {
280 return icbrt_with_guess(a, a >> 2 * bits / 3);
281 }
282
283 #ifdef DEBUG
284 int icbrt_loopcount;
285 #endif
286
287 /* Newton's Method: x_n+1 = x_n - ( f(x_n) / f'(x_n) ) */
288
289 /* for cube roots, x^3 - a = 0, x_new = x - 1/3 (x - a/x^2) */
290
291 /*
292 * Quick and dirty cube roots. Nothing fancy here, just Newton's method.
293 * Only works for positive integers (since that's all we need).
294 * We actually return floor(cbrt(a)) because that's what we need here, too.
295 */
296
297 static int icbrt_with_guess(a, guess)
298 int a, guess;
299 {
300 register int delta;
301
302 #ifdef DEBUG
303 icbrt_loopcount = 0;
304 #endif
305 if (a <= 0)
306 return 0;
307 if (guess < 1)
308 guess = 1;
309
310 do {
311 #ifdef DEBUG
312 icbrt_loopcount++;
313 #endif
314 delta = (guess - a / (guess * guess)) / 3;
315 #ifdef DEBUG
316 printf("pass %d: guess=%d, delta=%d\n", icbrt_loopcount, guess, delta);
317 #endif
318 guess -= delta;
319 } while (delta != 0);
320
321 if (guess * guess * guess > a)
322 guess--;
323
324 return guess;
325 }