aboutsummaryrefslogtreecommitdiffstats
path: root/libglitter_split_uint64_raster.c
blob: 75ec90b9a37622a0760f5ffdeaba150479558016 (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


void
libglitter_split_uint64_raster(uint16_t *rasters[3], uint16_t **alphap, uint64_t *raster,
                               uint64_t red, uint64_t green, uint64_t blue)
{
	uint64_t alpha, map;
	size_t position;
	unsigned char channel;

	alpha = ~(red | green | blue);
	red   /= 0xFF;
	green /= 0xFF;
	blue  /= 0xFF;
	alpha /= 0xFF;
	red   *= 0;
	green *= 1;
	blue  *= 2;
	alpha *= 3;
	map = red | green | blue | alpha;

	for (position = 0; position < 4; position++) {
		channel = ((const unsigned char *)&map)[position * sizeof(uint16_t)];
		if (channel < 3)
			rasters[channel] = &((uint16_t *)raster)[position];
		else if (alphap)
			*alphap = &((uint16_t *)raster)[position];
	}
}


#else


static void
check(int with_alpha, int r, int g, int b, int a)
{
	int red_shift    = r * sizeof(uint16_t) * 8;
	int green_shift  = g * sizeof(uint16_t) * 8;
	int blue_shift   = b * sizeof(uint16_t) * 8;
	int alpha_shift  = a * sizeof(uint16_t) * 8;
	uint16_t full  = ~(uint16_t)0;
	uint16_t red   = (uint16_t)~0 / (uint16_t)0xF * (uint16_t)0x5;
	uint16_t green = (uint16_t)~0 / (uint16_t)0xF * (uint16_t)0x8;
	uint16_t blue  = (uint16_t)~0 / (uint16_t)0xF * (uint16_t)0xA;
	uint16_t alpha = (uint16_t)~0 / (uint16_t)0xF * (uint16_t)0x9;
	uint64_t red_mask   = (uint64_t)red   << red_shift;
	uint64_t green_mask = (uint64_t)green << green_shift;
	uint64_t blue_mask  = (uint64_t)blue  << blue_shift;
	uint64_t alpha_mask = (uint64_t)alpha << alpha_shift;
	uint64_t raster = red_mask | green_mask | blue_mask | alpha_mask;
	uint16_t *rasters[4] = {NULL, NULL, NULL, NULL};

	ASSERT(r != g && r != b && r != a);
	ASSERT(g != b && g != a);
	ASSERT(b != a);

	libglitter_split_uint64_raster(rasters,
	                               with_alpha ? &rasters[3] : NULL,
	                               &raster,
	                               (uint64_t)full << red_shift,
	                               (uint64_t)full << green_shift,
	                               (uint64_t)full << blue_shift);
	ASSERT(rasters[0]);
	ASSERT(rasters[1]);
	ASSERT(rasters[2]);
	ASSERT(*rasters[0] == red);
	ASSERT(*rasters[1] == green);
	ASSERT(*rasters[2] == blue);
	if (with_alpha) {
		ASSERT(rasters[3]);
		ASSERT(*rasters[3] == alpha);
	} else {
		ASSERT(!rasters[3]);
	}
}

int
main(void)
{
	check(0, 0, 1, 2, 3);
	check(0, 0, 1, 3, 2);
	check(0, 0, 2, 1, 3);
	check(0, 0, 2, 3, 1);
	check(0, 0, 3, 1, 2);
	check(0, 0, 3, 2, 1);
	check(0, 1, 0, 2, 3);
	check(0, 1, 0, 3, 2);
	check(0, 1, 2, 0, 3);
	check(0, 1, 2, 3, 0);
	check(0, 1, 3, 0, 2);
	check(0, 1, 3, 2, 0);
	check(0, 2, 0, 1, 3);
	check(0, 2, 0, 3, 1);
	check(0, 2, 1, 0, 3);
	check(0, 2, 1, 3, 0);
	check(0, 2, 3, 0, 1);
	check(0, 2, 3, 1, 0);
	check(0, 3, 0, 1, 2);
	check(0, 3, 0, 2, 1);
	check(0, 3, 1, 0, 2);
	check(0, 3, 1, 2, 0);
	check(0, 3, 2, 0, 1);
	check(0, 3, 2, 1, 0);
	check(1, 0, 1, 2, 3);
	check(1, 0, 1, 3, 2);
	check(1, 0, 2, 1, 3);
	check(1, 0, 2, 3, 1);
	check(1, 0, 3, 1, 2);
	check(1, 0, 3, 2, 1);
	check(1, 1, 0, 2, 3);
	check(1, 1, 0, 3, 2);
	check(1, 1, 2, 0, 3);
	check(1, 1, 2, 3, 0);
	check(1, 1, 3, 0, 2);
	check(1, 1, 3, 2, 0);
	check(1, 2, 0, 1, 3);
	check(1, 2, 0, 3, 1);
	check(1, 2, 1, 0, 3);
	check(1, 2, 1, 3, 0);
	check(1, 2, 3, 0, 1);
	check(1, 2, 3, 1, 0);
	check(1, 3, 0, 1, 2);
	check(1, 3, 0, 2, 1);
	check(1, 3, 1, 0, 2);
	check(1, 3, 1, 2, 0);
	check(1, 3, 2, 0, 1);
	check(1, 3, 2, 1, 0);
	return 0;
}


#endif