XRGB.c revision 61b2299d
1/* $Xorg: XRGB.c,v 1.3 2000/08/17 19:45:04 cpqbld Exp $ */
2
3/*
4 * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
5 * 	All Rights Reserved
6 *
7 * This file is a component of an X Window System-specific implementation
8 * of Xcms based on the TekColor Color Management System.  Permission is
9 * hereby granted to use, copy, modify, sell, and otherwise distribute this
10 * software and its documentation for any purpose and without fee, provided
11 * that this copyright, permission, and disclaimer notice is reproduced in
12 * all copies of this software and in supporting documentation.  TekColor
13 * is a trademark of Tektronix, Inc.
14 *
15 * Tektronix makes no representation about the suitability of this software
16 * for any purpose.  It is provided "as is" and with all faults.
17 *
18 * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
19 * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
21 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
22 * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
23 * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
24 * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
25 *
26 *
27 *	NAME
28 *		XcmsRtoX.c
29 *
30 *	DESCRIPTION
31 *		Convert color specifications in XcmsRGB format in one array of
32 *		XcmsColor structures to RGB in an array of XColor structures.
33 *
34 *
35 */
36/* $XFree86: xc/lib/X11/XRGB.c,v 3.3 2001/07/29 05:01:11 tsi Exp $ */
37
38#ifdef HAVE_CONFIG_H
39#include <config.h>
40#endif
41#include "Xlibint.h"
42#include "Xcmsint.h"
43#include "Cv.h"
44
45/*
46 *      LOCAL VARIABLES
47 */
48
49static unsigned short const MASK[17] = {
50    0x0000,	/*  0 bitsPerRGB */
51    0x8000,	/*  1 bitsPerRGB */
52    0xc000,	/*  2 bitsPerRGB */
53    0xe000,	/*  3 bitsPerRGB */
54    0xf000,	/*  4 bitsPerRGB */
55    0xf800,	/*  5 bitsPerRGB */
56    0xfc00,	/*  6 bitsPerRGB */
57    0xfe00,	/*  7 bitsPerRGB */
58    0xff00,	/*  8 bitsPerRGB */
59    0xff80,	/*  9 bitsPerRGB */
60    0xffc0,	/* 10 bitsPerRGB */
61    0xffe0,	/* 11 bitsPerRGB */
62    0xfff0,	/* 12 bitsPerRGB */
63    0xfff8,	/* 13 bitsPerRGB */
64    0xfffc,	/* 14 bitsPerRGB */
65    0xfffe,	/* 15 bitsPerRGB */
66    0xffff	/* 16 bitsPerRGB */
67};
68
69
70
71/************************************************************************
72 *									*
73 *			API PRIVATE ROUTINES				*
74 *									*
75 ************************************************************************/
76
77/*
78 *	NAME
79 *		_XcmsRGB_to_XColor -
80 *
81 *	SYNOPSIS
82 */
83void
84_XcmsRGB_to_XColor(
85    XcmsColor *pColors,
86    XColor *pXColors,
87    unsigned int nColors)
88/*
89 *	DESCRIPTION
90 *	    Translates a color specification in XcmsRGBFormat in a XcmsColor
91 * 	    structure to an XColor structure.
92 *
93 *	RETURNS
94 *		void.
95 */
96{
97    for (; nColors--; pXColors++, pColors++) {
98	pXColors->pixel = pColors->pixel;
99	pXColors->red = pColors->spec.RGB.red;
100	pXColors->green = pColors->spec.RGB.green;
101	pXColors->blue  = pColors->spec.RGB.blue;
102	pXColors->flags = (DoRed | DoGreen | DoBlue);
103    }
104}
105
106
107/*
108 *	NAME
109 *		_XColor_to_XcmsRGB
110 *
111 *	SYNOPSIS
112 */
113void
114_XColor_to_XcmsRGB(
115    XcmsCCC ccc,
116    XColor *pXColors,
117    XcmsColor *pColors,
118    unsigned int nColors)
119/*
120 *	DESCRIPTION
121 *		Translates an RGB color specification in an XColor
122 *		structure to an XcmsRGB structure.
123 *
124 *		IMPORTANT NOTE:  Bit replication that may have been caused
125 *		with ResolveColor() routine in the X Server is undone
126 *		here if requested!  For example, if red = 0xcaca and the
127 *		bits_per_rgb is 8, then spec.RGB.red will be 0xca00.
128 *
129 *	RETURNS
130 *		void
131 */
132{
133    int bits_per_rgb = ccc->visual->bits_per_rgb;
134
135    for (; nColors--; pXColors++, pColors++) {
136	pColors->spec.RGB.red = (pXColors->red & MASK[bits_per_rgb]);
137	pColors->spec.RGB.green = (pXColors->green & MASK[bits_per_rgb]);
138	pColors->spec.RGB.blue = (pXColors->blue & MASK[bits_per_rgb]);
139	pColors->format = XcmsRGBFormat;
140	pColors->pixel = pXColors->pixel;
141    }
142}
143
144
145/*
146 *	NAME
147 *		_XcmsResolveColor
148 *
149 *	SYNOPSIS
150 */
151void
152_XcmsResolveColor(
153    XcmsCCC ccc,
154    XcmsColor *pXcmsColor)
155/*
156 *	DESCRIPTION
157 *	    Uses the X Server ResolveColor() algorithm to
158 *	    modify values to closest values supported by hardware.
159 *	    Old algorithm simply masked low-order bits.  The new algorithm
160 *	    has the effect of replicating significant bits into lower order
161 *	    bits in order to stretch the hardware value into all 16 bits.
162 *
163 *	    On a display with N-bit DACs, the "hardware" color is computed as:
164 *
165 *	    ((unsignedlong)(ClientValue >> (16-N)) * 0xFFFF) / ((1 << N) - 1)
166 *
167 *
168 *	RETURNS
169 *		void.
170 */
171{
172    int shift;
173    int max_color;
174
175    shift = 16 - ccc->visual->bits_per_rgb;
176    max_color = (1 << ccc->visual->bits_per_rgb) - 1;
177
178
179    pXcmsColor->spec.RGB.red =
180	    ((unsigned long)(pXcmsColor->spec.RGB.red >> shift) * 0xFFFF)
181	    / max_color;
182    pXcmsColor->spec.RGB.green =
183	    ((unsigned long)(pXcmsColor->spec.RGB.green >> shift) * 0xFFFF)
184	    / max_color;
185    pXcmsColor->spec.RGB.blue =
186	    ((unsigned long)(pXcmsColor->spec.RGB.blue  >> shift) * 0xFFFF)
187	    / max_color;
188}
189
190
191/*
192 *	NAME
193 *		_XcmsUnresolveColor
194 *
195 *	SYNOPSIS
196 */
197void
198_XcmsUnresolveColor(
199    XcmsCCC ccc,
200    XcmsColor *pColor)
201/*
202 *	DESCRIPTION
203 *		Masks out insignificant bits.
204 *
205 *	RETURNS
206 *		void.
207 *
208 *	ASSUMPTIONS
209 *		format == XcmsRGBFormat
210 */
211{
212    int bits_per_rgb = ccc->visual->bits_per_rgb;
213
214    pColor->spec.RGB.red &= MASK[bits_per_rgb];
215    pColor->spec.RGB.green &= MASK[bits_per_rgb];
216    pColor->spec.RGB.blue &= MASK[bits_per_rgb];
217}
218
219
220/*
221 *	NAME
222 *		_XUnresolveColor
223 *
224 *	SYNOPSIS
225 */
226void
227_XUnresolveColor(
228    XcmsCCC ccc,
229    XColor *pXColor)
230/*
231 *	DESCRIPTION
232 *		Masks out insignificant bits.
233 *
234 *	RETURNS
235 *		void.
236 */
237{
238    int bits_per_rgb = ccc->visual->bits_per_rgb;
239
240    pXColor->red &= MASK[bits_per_rgb];
241    pXColor->green &= MASK[bits_per_rgb];
242    pXColor->blue &= MASK[bits_per_rgb];
243}
244
245