4
* Copyright (C) 1997,98 Rasca, Berlin
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
#include <X11/Intrinsic.h>
24
#include <X11/StringDefs.h>
27
#define max(x,y) (x > y ? x : y)
33
GetColors (Display *dpy, XWindowAttributes *winfo, XColor **colors)
35
# define lowbit(x) ((x) & (~(x) + 1))
39
visual = winfo->visual;
40
ncolors = visual->map_entries;
43
*colors = (XColor *) malloc (sizeof(XColor) * ncolors);
45
perror ("GetColors()");
48
if ((visual->class == DirectColor) ||
49
(visual->class == TrueColor)) {
50
Pixel red, green, blue, red0, green0, blue0;
52
printf ("GetColors() Direct- or TrueColor (%d)\n", ncolors);
54
red = green = blue = 0;
55
red0 = lowbit(visual->red_mask);
56
green0 = lowbit(visual->green_mask);
57
blue0 = lowbit(visual->blue_mask);
58
for (i = 0; i < ncolors; i++) {
59
(*colors)[i].pixel = red | green | blue;
62
if (red > visual->red_mask)
65
if (green > visual->green_mask)
68
if (blue > visual->blue_mask)
72
for (i = 0; i < ncolors; i++) {
73
(*colors)[i].pixel = i;
77
XQueryColors (dpy, winfo->colormap, *colors, ncolors);
79
for (i = 0; i < ncolors; i++) {
80
printf ("color[%d] pixel:%d red:%d green:%d blue:%d\n", i,
91
* fill the 'ci' structure with some usefull information
92
* we need to process the color values in the ximage->data
96
GetColorInfo (XImage *image, ColorInfo *ci /* return struct */)
98
unsigned long red_mask, green_mask, blue_mask, alpha_mask;
99
// the shifts are unsigned longs as well
104
// setting shifts and bit_depths to zero
105
ci->red_shift = ci->green_shift = ci->blue_shift = ci->alpha_shift = 0;
106
ci->red_bit_depth = ci->green_bit_depth = ci->blue_bit_depth =
107
ci->alpha_bit_depth = 0;
109
red_mask = image->red_mask;
111
// shift red_mask to the right till all empty bits have been
112
// shifted out and count how many they were
113
while ((red_mask & 0x01) == 0) {
117
// count how many bits are set in the mask = depth
118
while ((red_mask & 0x01) == 1) {
123
// why don't I just set this above when 0 bits are shifted out of the
125
ci->red_max_val = (1 << ci->red_bit_depth) - 1;
127
green_mask = image->green_mask;
128
if (green_mask > 0) {
129
while ((green_mask & 0x01) == 0) {
133
while ((green_mask & 0x01) == 1) {
135
ci->green_bit_depth++;
138
ci->green_max_val = (1 << ci->green_bit_depth) - 1;
140
blue_mask = image->blue_mask;
142
while ((blue_mask & 0x01) == 0) {
146
while ((blue_mask & 0x01) == 1) {
148
ci->blue_bit_depth++;
151
ci->blue_max_val = (1 << ci->blue_bit_depth) - 1;
153
/* over all max values */
154
// whatever they are good for
155
ci->max_val = max (ci->red_max_val, ci->green_max_val);
156
ci->max_val = max (ci->blue_max_val, ci->max_val);
157
ci->bit_depth = max (ci->red_bit_depth, ci->green_bit_depth);
158
ci->bit_depth = max (ci->blue_bit_depth, ci->bit_depth);
159
if (image->bits_per_pixel > image->depth) {
161
// this seems to not reflect X's ignorance of alpha in its
164
(image->red_mask | image->blue_mask | image->green_mask);
165
alpha_mask = ci->alpha_mask;
166
if (alpha_mask > 0) {
167
while ((alpha_mask & 0x01) == 0) {
171
while ((alpha_mask & 0x01) == 1) {
173
ci->alpha_bit_depth++;
176
ci->alpha_max_val = (1 << ci->alpha_bit_depth) - 1;
179
printf ("rgb mask %08x bit_depth %lu max_val %lu\n",
180
image->red_mask|image->green_mask|image->blue_mask,
181
ci->bit_depth, ci->max_val);
182
printf ("red mask %08x bit_depth %lu shift %lu max_val %lu\n",
183
image->red_mask, ci->red_bit_depth,ci->red_shift,ci->red_max_val);
184
printf ("green mask %08x bit_depth %lu shift %lu max_val %lu\n",
185
image->green_mask,ci->green_bit_depth,ci->green_shift,ci->green_max_val);
186
printf ("blue mask %08x bit_depth %lu shift %lu max_val %lu\n",
187
image->blue_mask, ci->blue_bit_depth,ci->blue_shift,ci->blue_max_val);
188
printf ("alpha mask %08x bit_depth %lu shift %lu max_val %lu\n",
189
ci->alpha_mask, ci->alpha_bit_depth,ci->alpha_shift, ci->alpha_max_val);