1 /* $XConsortium: CmapAlloc.c,v 1.9 94/04/17 20:15:52 rws Exp $ */
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
35 #include <X11/Xatom.h>
36 #include <X11/Xutil.h>
39 #define lowbit(x) ((x) & (~(x) + 1))
41 static int default_allocation();
42 static void best_allocation();
43 static void gray_allocation();
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
)
63 unsigned long *red_max
, *green_max
, *blue_max
;
67 if (vinfo
->colormap_size
<= 2)
71 case XA_RGB_DEFAULT_MAP
:
72 status
= default_allocation(vinfo
, red_max
, green_max
, blue_max
);
75 best_allocation(vinfo
, red_max
, green_max
, blue_max
);
78 gray_allocation(vinfo
->colormap_size
, red_max
, green_max
, blue_max
);
81 *red_max
= vinfo
->colormap_size
- 1;
82 *green_max
= *blue_max
= 0;
84 case XA_RGB_GREEN_MAP
:
85 *green_max
= vinfo
->colormap_size
- 1;
86 *red_max
= *blue_max
= 0;
89 *blue_max
= vinfo
->colormap_size
- 1;
90 *red_max
= *green_max
= 0;
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
)
130 unsigned long *red
, *green
, *blue
;
132 int ngrays
; /* number of gray cells */
134 switch (vinfo
->class) {
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)
146 /* intended for displays with 8 planes */
147 *red
= *green
= *blue
= (unsigned long)
148 (icbrt(vinfo
->colormap_size
- 125) - 1);
153 if (vinfo
->colormap_size
< 10)
155 *red
= *green
= *blue
= vinfo
->colormap_size
/ 2 - 1;
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
);
167 if (vinfo
->colormap_size
> 65000)
169 else if (vinfo
->colormap_size
> 4000)
171 else if (vinfo
->colormap_size
< 250)
175 gray_allocation(ngrays
, red
, green
, blue
);
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
)
204 unsigned long *red
, *green
, *blue
;
207 if (vinfo
->class == DirectColor
|| vinfo
->class == TrueColor
) {
208 *red
= vinfo
->red_mask
;
209 while ((*red
& 01) == 0)
211 *green
= vinfo
->green_mask
;
212 while ((*green
& 01) == 0)
214 *blue
= vinfo
->blue_mask
;
215 while ((*blue
& 01) == 0)
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.
225 while (vinfo
->colormap_size
> n
) {
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
237 if (n
== vinfo
->colormap_size
) {
238 register int r
, g
, b
;
240 g
= b
+ ((bits
% 3) ? 1 : 0);
241 r
= b
+ (((bits
% 3) == 2) ? 1 : 0);
246 *red
= icbrt_with_bits(vinfo
->colormap_size
, bits
);
248 *green
= (vinfo
->colormap_size
/ ((*red
) * (*blue
)));
258 * integer cube roots by Newton's method
260 * Stephen Gildea, MIT X Consortium, July 1991
263 static int icbrt(a
) /* integer cube root */
266 register int bits
= 0;
267 register unsigned n
= a
;
273 return icbrt_with_bits(a
, bits
);
277 static int icbrt_with_bits(a
, bits
)
279 int bits
; /* log 2 of a */
281 return icbrt_with_guess(a
, a
>>2*bits
/3);
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
)
315 delta
= (guess
- a
/(guess
*guess
))/3;
317 printf("pass %d: guess=%d, delta=%d\n", icbrt_loopcount
, guess
, delta
);
320 } while (delta
!= 0);
322 if (guess
*guess
*guess
> a
)