~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to src/colors.c

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2004-08-29 10:53:42 UTC
  • Revision ID: james.westby@ubuntu.com-20040829105342-qgmnry37eadfkoxx
Tags: upstream-1.1.3
ImportĀ upstreamĀ versionĀ 1.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * colors.c
 
3
 *
 
4
 * Copyright (C) 1997,98 Rasca, Berlin
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 */
 
20
 
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <X11/Intrinsic.h>
 
24
#include <X11/StringDefs.h>
 
25
#include "colors.h"
 
26
 
 
27
#define max(x,y) (x > y ? x : y)
 
28
 
 
29
/*
 
30
 * ..
 
31
 */
 
32
int
 
33
GetColors (Display *dpy, XWindowAttributes *winfo, XColor **colors)
 
34
{
 
35
#       define lowbit(x) ((x) & (~(x) + 1))
 
36
    int ncolors, i;
 
37
        Visual *visual;
 
38
 
 
39
        visual = winfo->visual;
 
40
    ncolors = visual->map_entries;
 
41
        if (*colors)
 
42
                free (*colors);
 
43
    *colors = (XColor *) malloc (sizeof(XColor) * ncolors);
 
44
    if (!*colors) {
 
45
        perror ("GetColors()");
 
46
        return(0);
 
47
    }
 
48
    if ((visual->class == DirectColor) ||
 
49
        (visual->class == TrueColor)) {
 
50
                Pixel red, green, blue, red0, green0, blue0;
 
51
#ifdef DEBUG
 
52
                printf ("GetColors() Direct- or TrueColor (%d)\n", ncolors);
 
53
#endif
 
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;
 
60
                        (*colors)[i].pad = 0;
 
61
                        red += red0;
 
62
                        if (red > visual->red_mask)
 
63
                                red = 0;
 
64
                        green += green0;
 
65
                        if (green > visual->green_mask)
 
66
                                green = 0;
 
67
                        blue += blue0;
 
68
                        if (blue > visual->blue_mask)
 
69
                                blue = 0;
 
70
                }
 
71
    } else {
 
72
                for (i = 0; i < ncolors; i++) {
 
73
                        (*colors)[i].pixel = i;
 
74
                        (*colors)[i].pad = 0;
 
75
                }
 
76
        }
 
77
        XQueryColors (dpy, winfo->colormap, *colors, ncolors);
 
78
#ifdef DEBUG2
 
79
        for (i = 0; i < ncolors; i++) {
 
80
                printf ("color[%d] pixel:%d red:%d green:%d blue:%d\n", i,
 
81
                        (*colors)[i].pixel,
 
82
                        (*colors)[i].red,
 
83
                        (*colors)[i].green,
 
84
                        (*colors)[i].blue);
 
85
        }
 
86
#endif
 
87
    return (ncolors);
 
88
}
 
89
 
 
90
/*
 
91
 * fill the 'ci' structure with some usefull information
 
92
 * we need to process the color values in the ximage->data
 
93
 * field
 
94
 */
 
95
void
 
96
GetColorInfo (XImage *image, ColorInfo *ci /* return struct */)
 
97
{
 
98
        unsigned long red_mask, green_mask, blue_mask, alpha_mask;
 
99
        // the shifts are unsigned longs as well
 
100
 
 
101
        if (!ci)
 
102
                return;
 
103
 
 
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;
 
108
 
 
109
        red_mask = image->red_mask;
 
110
        if (red_mask > 0) {
 
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) {
 
114
                        red_mask >>= 1;
 
115
                        ci->red_shift++;
 
116
                }
 
117
                // count how many bits are set in the mask = depth
 
118
                while ((red_mask & 0x01) == 1) {
 
119
                        red_mask >>= 1;
 
120
                        ci->red_bit_depth++;
 
121
                }
 
122
        }
 
123
        // why don't I just set this above when 0 bits are shifted out of the
 
124
        // mask??????
 
125
        ci->red_max_val = (1 << ci->red_bit_depth) - 1;
 
126
 
 
127
        green_mask = image->green_mask;
 
128
        if (green_mask > 0) {
 
129
                while ((green_mask & 0x01) == 0) {
 
130
                        green_mask >>= 1;
 
131
                        ci->green_shift++;
 
132
                }
 
133
                while ((green_mask & 0x01) == 1) {
 
134
                        green_mask >>= 1;
 
135
                        ci->green_bit_depth++;
 
136
                }
 
137
        }
 
138
        ci->green_max_val = (1 << ci->green_bit_depth) - 1;
 
139
 
 
140
        blue_mask = image->blue_mask;
 
141
        if (blue_mask > 0) {
 
142
                while ((blue_mask & 0x01) == 0) {
 
143
                        blue_mask >>= 1;
 
144
                        ci->blue_shift++;
 
145
                }
 
146
                while ((blue_mask & 0x01) == 1) {
 
147
                        blue_mask >>= 1;
 
148
                        ci->blue_bit_depth++;
 
149
                }
 
150
        }
 
151
        ci->blue_max_val = (1 << ci->blue_bit_depth) - 1;
 
152
 
 
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) {
 
160
                /* alpha? */
 
161
                // this seems to not reflect X's ignorance of alpha in its
 
162
                // masks
 
163
                ci->alpha_mask = ~
 
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) {
 
168
                                alpha_mask >>= 1;
 
169
                                ci->alpha_shift++;
 
170
                        }
 
171
                        while ((alpha_mask & 0x01) == 1) {
 
172
                                alpha_mask >>= 1;
 
173
                                ci->alpha_bit_depth++;
 
174
                        }
 
175
                }
 
176
                ci->alpha_max_val = (1 << ci->alpha_bit_depth) - 1;
 
177
        }
 
178
#ifdef DEBUG2
 
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);
 
190
#endif
 
191
}
 
192