aboutsummaryrefslogtreecommitdiffstats
path: root/libfonts_unget_subpixel_order_class.c
blob: 47d8366f40f59c2cdb4ed5ec6dc338dd502f880a (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


int
libfonts_unget_subpixel_order_class(enum libfonts_subpixel_order *orderp,
                                    enum libfonts_subpixel_order_class layout,
                                    enum libfonts_subpixel_colour cell1,
                                    enum libfonts_subpixel_colour cell2,
                                    enum libfonts_subpixel_colour cell3)
{
	int i;

	if (cell1 < 0 || cell1 > 2 ||
	    cell2 < 0 || cell2 > 2 ||
	    cell3 < 0 || cell3 > 2 ||
	    cell1 == cell2 ||
	    cell2 == cell3 ||
	    cell3 == cell1) {
		errno = EINVAL;
		return -1;
	}

	if (layout == LIBFONTS_SUBPIXEL_ORDER_CLASS_OTHER) {
		if (orderp)
			*orderp = LIBFONTS_SUBPIXEL_ORDER_UNKNOWN;
		return 0;

	} else if (layout == LIBFONTS_SUBPIXEL_ORDER_CLASS_123 || layout == LIBFONTS_SUBPIXEL_ORDER_CLASS_1_2_3) {
		if (orderp) {
			/* RGB, BGR, GBR, RBG, BRG, GRB */
			i = ((cell2 + 2) % 3) * 2;
			i += (cell1 < cell3) ^ (cell2 == LIBFONTS_SUBPIXEL_COLOUR_GREEN);
			*orderp = i * 2 + (layout == LIBFONTS_SUBPIXEL_ORDER_CLASS_1_2_3) + LIBFONTS_SUBPIXEL_ORDER_RGB;
		}
		return 1;

	} else if (layout >= LIBFONTS_SUBPIXEL_ORDER_CLASS_11_23 && layout <= LIBFONTS_SUBPIXEL_ORDER_CLASS_BALANCED_13_12) {
		if (orderp) {
			/* RGB, RBG, GRB, GBR, BRG, BGR */
			i = (cell1 * 2) + (cell2 > cell3);
			*orderp = i * 4 + (layout - LIBFONTS_SUBPIXEL_ORDER_CLASS_11_23) % 4 + LIBFONTS_SUBPIXEL_ORDER_RR_GB;
			*orderp += (layout >= LIBFONTS_SUBPIXEL_ORDER_CLASS_BALANCED_11_23) * 24;
		}
		return 1;

	} else {
		errno = EINVAL;
		return -1;
	}
}


#else


int
main(void)
{
#define R LIBFONTS_SUBPIXEL_COLOUR_RED
#define G LIBFONTS_SUBPIXEL_COLOUR_GREEN
#define B LIBFONTS_SUBPIXEL_COLOUR_BLUE

#define T(ORDER, LAYOUT, C1, C2, C3)\
	T_(LIBFONTS_SUBPIXEL_ORDER_##ORDER,\
	   LIBFONTS_SUBPIXEL_ORDER_CLASS_##LAYOUT,\
	   C1, C2, C3)
#define T_(ORDER, LAYOUT, C1, C2, C3)\
	do {\
		errno = 0;\
		order = -1;\
		ASSERT(libfonts_unget_subpixel_order_class(&order, LAYOUT, C1, C2, C3) == 1);\
		ASSERT(order == ORDER);\
		ASSERT(!errno);\
		ASSERT(libfonts_unget_subpixel_order_class(NULL, LAYOUT, C1, C2, C3) == 1);\
		ASSERT(!errno);\
	} while (0)

#define TU(LAYOUT, C1, C2, C3)\
	TU_(LIBFONTS_SUBPIXEL_ORDER_CLASS_##LAYOUT, C1, C2, C3)
#define TU_(LAYOUT, C1, C2, C3)\
	do {\
		errno = 0;\
		order = -1;\
		ASSERT(libfonts_unget_subpixel_order_class(&order, LAYOUT, C1, C2, C3) == 0);\
		ASSERT(order == LIBFONTS_SUBPIXEL_ORDER_UNKNOWN);\
		ASSERT(!errno);\
		ASSERT(libfonts_unget_subpixel_order_class(NULL, LAYOUT, C1, C2, C3) == 0);\
		ASSERT(!errno);\
	} while (0)

#define TE(LAYOUT, C1, C2, C3)\
	TE_(LIBFONTS_SUBPIXEL_ORDER_CLASS_##LAYOUT, C1, C2, C3)
#define TE_(LAYOUT, C1, C2, C3)\
	do {\
		errno = 0;\
		ASSERT(libfonts_unget_subpixel_order_class(&order, LAYOUT, C1, C2, C3) == -1);\
		ASSERT(errno == EINVAL);\
		errno = 0;\
		ASSERT(libfonts_unget_subpixel_order_class(NULL, LAYOUT, C1, C2, C3) == -1);\
		ASSERT(errno == EINVAL);\
	} while (0)

	enum libfonts_subpixel_order order;

	TU(OTHER, R, G, B);
	TU(OTHER, G, B, R);
	TU(OTHER, B, R, G);
	TU(OTHER, R, B, G);
	TU(OTHER, G, R, B);
	TU(OTHER, B, G, R);

	TE(OTHER, -1, G, B);
	TE(OTHER, R, -1, B);
	TE(OTHER, R, G, -1);
	TE(OTHER, -1, -2, B);
	TE(OTHER, 3, G, B);
	TE(OTHER, R, 3, B);
	TE(OTHER, R, G, 3);
	TE(OTHER, R, 3, 4);
	TE(123, -1, G, B);
	TE(123, R, -1, B);
	TE(123, R, G, -1);
	TE(123, -1, -2, B);
	TE(123, 3, G, B);
	TE(123, R, 3, B);
	TE(123, R, G, 3);
	TE(123, R, 3, 4);
	TE(123, R, R, G);
	TE(123, R, G, R);
	TE(123, R, G, G);
	TE_(-1, R, G, B);
	TE_(9999, R, G, B);

	T(RGB, 123, R, G, B);
	T(R_G_B, 1_2_3, R, G, B);
	T(BGR, 123, B, G, R);
	T(B_G_R, 1_2_3, B, G, R);

	T(GBR, 123, G, B, R);
	T(G_B_R, 1_2_3, G, B, R);
	T(RBG, 123, R, B, G);
	T(R_B_G, 1_2_3, R, B, G);

	T(BRG, 123, B, R, G);
	T(B_R_G, 1_2_3, B, R, G);
	T(GRB, 123, G, R, B);
	T(G_R_B, 1_2_3, G, R, B);

	T(RR_GB, 11_23, R, G, B);
	T(GR_BR, 21_31, R, G, B);
	T(BG_RR, 32_11, R, G, B);
	T(RB_RG, 13_12, R, G, B);

	T(RR_BG, 11_23, R, B, G);
	T(BR_GR, 21_31, R, B, G);
	T(GB_RR, 32_11, R, B, G);
	T(RG_RB, 13_12, R, B, G);

	T(GG_RB, 11_23, G, R, B);
	T(RG_BG, 21_31, G, R, B);
	T(BR_GG, 32_11, G, R, B);
	T(GB_GR, 13_12, G, R, B);

	T(GG_BR, 11_23, G, B, R);
	T(BG_RG, 21_31, G, B, R);
	T(RB_GG, 32_11, G, B, R);
	T(GR_GB, 13_12, G, B, R);

	T(BB_RG, 11_23, B, R, G);
	T(RB_GB, 21_31, B, R, G);
	T(GR_BB, 32_11, B, R, G);
	T(BG_BR, 13_12, B, R, G);

	T(BB_GR, 11_23, B, G, R);
	T(GB_RB, 21_31, B, G, R);
	T(RG_BB, 32_11, B, G, R);
	T(BR_BG, 13_12, B, G, R);

	T(BALANCED_RR_GB, BALANCED_11_23, R, G, B);
	T(BALANCED_GR_BR, BALANCED_21_31, R, G, B);
	T(BALANCED_BG_RR, BALANCED_32_11, R, G, B);
	T(BALANCED_RB_RG, BALANCED_13_12, R, G, B);

	T(BALANCED_RR_BG, BALANCED_11_23, R, B, G);
	T(BALANCED_BR_GR, BALANCED_21_31, R, B, G);
	T(BALANCED_GB_RR, BALANCED_32_11, R, B, G);
	T(BALANCED_RG_RB, BALANCED_13_12, R, B, G);

	T(BALANCED_GG_RB, BALANCED_11_23, G, R, B);
	T(BALANCED_RG_BG, BALANCED_21_31, G, R, B);
	T(BALANCED_BR_GG, BALANCED_32_11, G, R, B);
	T(BALANCED_GB_GR, BALANCED_13_12, G, R, B);

	T(BALANCED_GG_BR, BALANCED_11_23, G, B, R);
	T(BALANCED_BG_RG, BALANCED_21_31, G, B, R);
	T(BALANCED_RB_GG, BALANCED_32_11, G, B, R);
	T(BALANCED_GR_GB, BALANCED_13_12, G, B, R);

	T(BALANCED_BB_RG, BALANCED_11_23, B, R, G);
	T(BALANCED_RB_GB, BALANCED_21_31, B, R, G);
	T(BALANCED_GR_BB, BALANCED_32_11, B, R, G);
	T(BALANCED_BG_BR, BALANCED_13_12, B, R, G);

	T(BALANCED_BB_GR, BALANCED_11_23, B, G, R);
	T(BALANCED_GB_RB, BALANCED_21_31, B, G, R);
	T(BALANCED_RG_BB, BALANCED_32_11, B, G, R);
	T(BALANCED_BR_BG, BALANCED_13_12, B, G, R);

	return 0;
}


#endif