Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / CmapAlloc.c
Commit [+]AuthorDateLineData
33b8c225 kojima1999-11-16 00:13:21 +00001/* $XConsortium: CmapAlloc.c,v 1.9 94/04/17 20:15:52 rws Exp $ */
2
6830b057 dan2004-10-12 21:28:27 +00003/*
33b8c225 kojima1999-11-16 00:13:21 +00004
5Copyright (c) 1989, 1994 X Consortium
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24Except as contained in this notice, the name of the X Consortium shall not be
25used in advertising or otherwise to promote the sale, use or other dealings
26in 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
41static int default_allocation();
42static void best_allocation();
43static void gray_allocation();
44static int icbrt();
45static int icbrt_with_bits();
46static int icbrt_with_guess();
47
6830b057 dan2004-10-12 21:28:27 +000048/* To determine the best allocation of reds, greens, and blues in a
33b8c225 kojima1999-11-16 00:13:21 +000049 * standard colormap, use XmuGetColormapAllocation.
50 * vinfo specifies visual information for a chosen visual
51 * property specifies one of the standard colormap property names
6830b057 dan2004-10-12 21:28:27 +000052 * red_max returns maximum red value
33b8c225 kojima1999-11-16 00:13:21 +000053 * 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
60Status XmuGetColormapAllocation(vinfo, property, red_max, green_max, blue_max)
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020061XVisualInfo *vinfo;
62Atom property;
63unsigned long *red_max, *green_max, *blue_max;
33b8c225 kojima1999-11-16 00:13:21 +000064{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020065 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;
33b8c225 kojima1999-11-16 00:13:21 +000096}
97
98/****************************************************************************/
99/* Determine the appropriate color allocations of a gray scale.
100 *
101 * Keith Packard, MIT X Consortium
102 */
103
104static void gray_allocation(n, red_max, green_max, blue_max)
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200105int n; /* the number of cells of the gray scale */
106unsigned long *red_max, *green_max, *blue_max;
33b8c225 kojima1999-11-16 00:13:21 +0000107{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200108 *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));
33b8c225 kojima1999-11-16 00:13:21 +0000112}
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
6830b057 dan2004-10-12 21:28:27 +0000117 * produce an allocation for an RGB_DEFAULT_MAP.
33b8c225 kojima1999-11-16 00:13:21 +0000118 *
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
128static int default_allocation(vinfo, red, green, blue)
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200129XVisualInfo *vinfo;
130unsigned long *red, *green, *blue;
33b8c225 kojima1999-11-16 00:13:21 +0000131{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200132 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;
33b8c225 kojima1999-11-16 00:13:21 +0000182}
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 */
6830b057 dan2004-10-12 21:28:27 +0000201
33b8c225 kojima1999-11-16 00:13:21 +0000202static void best_allocation(vinfo, red, green, blue)
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200203XVisualInfo *vinfo;
204unsigned long *red, *green, *blue;
33b8c225 kojima1999-11-16 00:13:21 +0000205{
206
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200207 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;
33b8c225 kojima1999-11-16 00:13:21 +0000255}
256
257/*
258 * integer cube roots by Newton's method
259 *
260 * Stephen Gildea, MIT X Consortium, July 1991
261 */
262
263static int icbrt(a) /* integer cube root */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200264int a;
33b8c225 kojima1999-11-16 00:13:21 +0000265{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200266 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);
33b8c225 kojima1999-11-16 00:13:21 +0000274}
275
33b8c225 kojima1999-11-16 00:13:21 +0000276static int icbrt_with_bits(a, bits)
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200277int a;
278int bits; /* log 2 of a */
33b8c225 kojima1999-11-16 00:13:21 +0000279{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200280 return icbrt_with_guess(a, a >> 2 * bits / 3);
33b8c225 kojima1999-11-16 00:13:21 +0000281}
282
983e1541 dan2000-06-05 23:41:26 +0000283#ifdef DEBUG
33b8c225 kojima1999-11-16 00:13:21 +0000284int 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
297static int icbrt_with_guess(a, guess)
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200298int a, guess;
33b8c225 kojima1999-11-16 00:13:21 +0000299{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200300 register int delta;
33b8c225 kojima1999-11-16 00:13:21 +0000301
983e1541 dan2000-06-05 23:41:26 +0000302#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200303 icbrt_loopcount = 0;
33b8c225 kojima1999-11-16 00:13:21 +0000304#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200305 if (a <= 0)
306 return 0;
307 if (guess < 1)
308 guess = 1;
33b8c225 kojima1999-11-16 00:13:21 +0000309
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200310 do {
983e1541 dan2000-06-05 23:41:26 +0000311#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200312 icbrt_loopcount++;
33b8c225 kojima1999-11-16 00:13:21 +0000313#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200314 delta = (guess - a / (guess * guess)) / 3;
33b8c225 kojima1999-11-16 00:13:21 +0000315#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200316 printf("pass %d: guess=%d, delta=%d\n", icbrt_loopcount, guess, delta);
33b8c225 kojima1999-11-16 00:13:21 +0000317#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200318 guess -= delta;
319 } while (delta != 0);
33b8c225 kojima1999-11-16 00:13:21 +0000320
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200321 if (guess * guess * guess > a)
322 guess--;
33b8c225 kojima1999-11-16 00:13:21 +0000323
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200324 return guess;
33b8c225 kojima1999-11-16 00:13:21 +0000325}