summaryrefslogtreecommitdiff
path: root/pixman/src/iccolor.c
blob: dbb1207fbe5f46d48e8fa07e27664b0e3f9eeb1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright © 2000 SuSE, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of SuSE not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  SuSE makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author:  Keith Packard, SuSE, Inc.
 */

#include "icint.h"

#ifdef ICINT_NEED_IC_ONES
/* Fall back on HACKMEM 169.  */
int
_IcOnes (unsigned long mask)
{
    register unsigned long y;

    y = (mask >> 1) &033333333333;
    y = mask - y - ((y >>1) & 033333333333);
    return (((y + (y >> 3)) & 030707070707) % 077);
}
#endif

void
IcColorToPixel (const IcFormat	*format,
		const IcColor	*color,
		IcBits		*pixel)
{
    uint32_t	    r, g, b, a;

    r = color->red >> (16 - _IcOnes (format->redMask));
    g = color->green >> (16 - _IcOnes (format->greenMask));
    b = color->blue >> (16 - _IcOnes (format->blueMask));
    a = color->alpha >> (16 - _IcOnes (format->alphaMask));
    r = r << format->red;
    g = g << format->green;
    b = b << format->blue;
    a = a << format->alpha;
    *pixel = r|g|b|a;
}
slim_hidden_def(IcColorToPixel);

static uint16_t
IcFillColor (uint32_t pixel, int bits)
{
    while (bits < 16)
    {
	pixel |= pixel << bits;
	bits <<= 1;
    }
    return (uint16_t) pixel;
}

void
IcPixelToColor (const IcFormat	*format,
		const IcBits	pixel,
		IcColor		*color)
{
    uint32_t	    r, g, b, a;

    r = (pixel >> format->red) & format->redMask;
    g = (pixel >> format->green) & format->greenMask;
    b = (pixel >> format->blue) & format->blueMask;
    a = (pixel >> format->alpha) & format->alphaMask;
    color->red = IcFillColor (r, _IcOnes (format->redMask));
    color->green = IcFillColor (r, _IcOnes (format->greenMask));
    color->blue = IcFillColor (r, _IcOnes (format->blueMask));
    color->alpha = IcFillColor (r, _IcOnes (format->alphaMask));
}