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)
72 case XA_RGB_DEFAULT_MAP
:
73 status
= default_allocation(vinfo
, red_max
, green_max
, blue_max
);
76 best_allocation(vinfo
, red_max
, green_max
, blue_max
);
79 gray_allocation(vinfo
->colormap_size
, red_max
, green_max
, blue_max
);
82 *red_max
= vinfo
->colormap_size
- 1;
83 *green_max
= *blue_max
= 0;
85 case XA_RGB_GREEN_MAP
:
86 *green_max
= vinfo
->colormap_size
- 1;
87 *red_max
= *blue_max
= 0;
90 *blue_max
= vinfo
->colormap_size
- 1;
91 *red_max
= *green_max
= 0;
99 /****************************************************************************/
100 /* Determine the appropriate color allocations of a gray scale.
102 * Keith Packard, MIT X Consortium
105 static void gray_allocation(n
, red_max
, green_max
, blue_max
)
106 int n
; /* the number of cells of the gray scale */
107 unsigned long *red_max
, *green_max
, *blue_max
;
109 *red_max
= (n
* 30) / 100;
110 *green_max
= (n
* 59) / 100;
111 *blue_max
= (n
* 11) / 100;
112 *green_max
+= ((n
- 1) - (*red_max
+ *green_max
+ *blue_max
));
115 /****************************************************************************/
116 /* Determine an appropriate color allocation for the RGB_DEFAULT_MAP.
117 * If a map has less than a minimum number of definable entries, we do not
118 * produce an allocation for an RGB_DEFAULT_MAP.
120 * For 16 planes, the default colormap will have 27 each RGB; for 12 planes,
121 * 12 each. For 8 planes, let n = the number of colormap entries, which may
122 * be 256 or 254. Then, maximum red value = floor(cube_root(n - 125)) - 1.
123 * Maximum green and maximum blue values are identical to maximum red.
124 * This leaves at least 125 cells which clients can allocate.
126 * Return 0 if an allocation has been determined, non-zero otherwise.
129 static int default_allocation(vinfo
, red
, green
, blue
)
131 unsigned long *red
, *green
, *blue
;
133 int ngrays
; /* number of gray cells */
135 switch (vinfo
->class) {
138 if (vinfo
->colormap_size
> 65000)
139 /* intended for displays with 16 planes */
140 *red
= *green
= *blue
= (unsigned long) 27;
141 else if (vinfo
->colormap_size
> 4000)
142 /* intended for displays with 12 planes */
143 *red
= *green
= *blue
= (unsigned long) 12;
144 else if (vinfo
->colormap_size
< 250)
147 /* intended for displays with 8 planes */
148 *red
= *green
= *blue
= (unsigned long)
149 (icbrt(vinfo
->colormap_size
- 125) - 1);
154 if (vinfo
->colormap_size
< 10)
156 *red
= *green
= *blue
= vinfo
->colormap_size
/ 2 - 1;
161 *red
= vinfo
->red_mask
/ lowbit(vinfo
->red_mask
);
162 *green
= vinfo
->green_mask
/ lowbit(vinfo
->green_mask
);
163 *blue
= vinfo
->blue_mask
/ lowbit(vinfo
->blue_mask
);
168 if (vinfo
->colormap_size
> 65000)
170 else if (vinfo
->colormap_size
> 4000)
172 else if (vinfo
->colormap_size
< 250)
176 gray_allocation(ngrays
, red
, green
, blue
);
185 /****************************************************************************/
186 /* Determine an appropriate color allocation for the RGB_BEST_MAP.
188 * For a DirectColor or TrueColor visual, the allocation is determined
189 * by the red_mask, green_mask, and blue_mask members of the visual info.
191 * Otherwise, if the colormap size is an integral power of 2, determine
192 * the allocation according to the number of bits given to each color,
193 * with green getting more than red, and red more than blue, if there
194 * are to be inequities in the distribution. If the colormap size is
195 * not an integral power of 2, let n = the number of colormap entries.
196 * Then maximum red value = floor(cube_root(n)) - 1;
197 * maximum blue value = floor(cube_root(n)) - 1;
198 * maximum green value = n / ((# red values) * (# blue values)) - 1;
199 * Which, on a GPX, allows for 252 entries in the best map, out of 254
200 * defineable colormap entries.
203 static void best_allocation(vinfo
, red
, green
, blue
)
205 unsigned long *red
, *green
, *blue
;
208 if (vinfo
->class == DirectColor
|| vinfo
->class == TrueColor
)
210 *red
= vinfo
->red_mask
;
211 while ((*red
& 01) == 0)
213 *green
= vinfo
->green_mask
;
214 while ((*green
& 01) == 0)
216 *blue
= vinfo
->blue_mask
;
217 while ((*blue
& 01) == 0)
222 register int bits
, n
;
224 /* Determine n such that n is the least integral power of 2 which is
225 * greater than or equal to the number of entries in the colormap.
229 while (vinfo
->colormap_size
> n
)
235 /* If the number of entries in the colormap is a power of 2, determine
236 * the allocation by "dealing" the bits, first to green, then red, then
237 * blue. If not, find the maximum integral red, green, and blue values
238 * which, when multiplied together, do not exceed the number of
242 if (n
== vinfo
->colormap_size
)
244 register int r
, g
, b
;
246 g
= b
+ ((bits
% 3) ? 1 : 0);
247 r
= b
+ (((bits
% 3) == 2) ? 1 : 0);
254 *red
= icbrt_with_bits(vinfo
->colormap_size
, bits
);
256 *green
= (vinfo
->colormap_size
/ ((*red
) * (*blue
)));
266 * integer cube roots by Newton's method
268 * Stephen Gildea, MIT X Consortium, July 1991
271 static int icbrt(a
) /* integer cube root */
274 register int bits
= 0;
275 register unsigned n
= a
;
282 return icbrt_with_bits(a
, bits
);
286 static int icbrt_with_bits(a
, bits
)
288 int bits
; /* log 2 of a */
290 return icbrt_with_guess(a
, a
>>2*bits
/3);
297 /* Newton's Method: x_n+1 = x_n - ( f(x_n) / f'(x_n) ) */
299 /* for cube roots, x^3 - a = 0, x_new = x - 1/3 (x - a/x^2) */
302 * Quick and dirty cube roots. Nothing fancy here, just Newton's method.
303 * Only works for positive integers (since that's all we need).
304 * We actually return floor(cbrt(a)) because that's what we need here, too.
307 static int icbrt_with_guess(a
, guess
)
324 delta
= (guess
- a
/(guess
*guess
))/3;
326 printf("pass %d: guess=%d, delta=%d\n", icbrt_loopcount
, guess
, delta
);
329 } while (delta
!= 0);
331 if (guess
*guess
*guess
> a
)