aboutsummaryrefslogtreecommitdiffstats
path: root/libfonts_get_subpixel_map.c
blob: 9581a57c0225da64e5cef4e1f2baf874a08406df (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


int
libfonts_get_subpixel_map(enum libfonts_subpixel_order_class layout, size_t rowsize, uint8_t *restrict map,
                          uint8_t *restrict n0p, uint8_t *restrict n1p, uint8_t *restrict n2p)
{
	size_t n0, n1, n2;

#define SET_1_X_3(R1C1, R1C2, R1C3)\
	do {\
		if (map) {\
			map[0] = (R1C1);\
			map[1] = (R1C2);\
			map[2] = (R1C3);\
		}\
		n0 = 1;\
		n1 = 1;\
		n2 = 1;\
	} while (0)

#define SET_3_X_1(R1C1, R2C1, R3C1)\
	do {\
		if (map) {\
			if (rowsize < 1)\
				goto einval;\
			map[0 * rowsize] = (R1C1);\
			map[1 * rowsize] = (R2C1);\
			map[2 * rowsize] = (R3C1);\
		}\
		n0 = 1;\
		n1 = 1;\
		n2 = 1;\
	} while (0)

#define SET_2_X_2(R1C1, R1C2, R2C1, R2C2)\
	do {\
		if (map) {\
			if (rowsize < 2)\
				goto einval;\
			map[0 * rowsize + 0] = (R1C1);\
			map[0 * rowsize + 1] = (R1C2);\
			map[1 * rowsize + 0] = (R2C1);\
			map[1 * rowsize + 1] = (R2C2);\
		}\
		n0 = 2;\
		n1 = 1;\
		n2 = 1;\
	} while (0)

#define SET_3_X_2(R1C1, R1C2, R2C1, R2C2, R3C1, R3C2)\
	do {\
		if (map) {\
			if (rowsize < 2)\
				goto einval;\
			map[0 * rowsize + 0] = (R1C1);\
			map[0 * rowsize + 1] = (R1C2);\
			map[1 * rowsize + 0] = (R2C1);\
			map[1 * rowsize + 1] = (R2C2);\
			map[2 * rowsize + 0] = (R3C1);\
			map[2 * rowsize + 1] = (R3C2);\
		}\
		n0 = 2;\
		n1 = 2;\
		n2 = 2;\
	} while (0)

#define SET_2_X_3(R1C1, R1C2, R1C3, R2C1, R2C2, R2C3)\
	do {\
		if (map) {\
			if (rowsize < 3)\
				goto einval;\
			map[0 * rowsize + 0] = (R1C1);\
			map[0 * rowsize + 1] = (R1C2);\
			map[0 * rowsize + 2] = (R1C3);\
			map[1 * rowsize + 0] = (R2C1);\
			map[1 * rowsize + 1] = (R2C2);\
			map[1 * rowsize + 2] = (R2C3);\
		}\
		n0 = 2;\
		n1 = 2;\
		n2 = 2;\
	} while (0)

	switch (layout) {
	case LIBFONTS_SUBPIXEL_ORDER_CLASS_123:
		SET_1_X_3(0, 1, 2);
		break;

	case LIBFONTS_SUBPIXEL_ORDER_CLASS_1_2_3:
		SET_3_X_1(0,
		          1,
		          2);
		break;

	case LIBFONTS_SUBPIXEL_ORDER_CLASS_11_23:
		SET_2_X_2(0, 0,
		          1, 2);
		break;

	case LIBFONTS_SUBPIXEL_ORDER_CLASS_21_31:
		SET_2_X_2(1, 0,
		          2, 0);
		break;

	case LIBFONTS_SUBPIXEL_ORDER_CLASS_32_11:
		SET_2_X_2(2, 1,
		          0, 0);
		break;

	case LIBFONTS_SUBPIXEL_ORDER_CLASS_13_12:
		SET_2_X_2(0, 2,
		          0, 1);
		break;

	case LIBFONTS_SUBPIXEL_ORDER_CLASS_BALANCED_11_23:
		SET_3_X_2(0, 0,
		          1, 2,
		          1, 2);
		break;

	case LIBFONTS_SUBPIXEL_ORDER_CLASS_BALANCED_21_31:
		SET_2_X_3(1, 1, 0,
		          2, 2, 0);
		break;

	case LIBFONTS_SUBPIXEL_ORDER_CLASS_BALANCED_32_11:
		SET_3_X_2(2, 1,
		          2, 1,
		          0, 0);
		break;

	case LIBFONTS_SUBPIXEL_ORDER_CLASS_BALANCED_13_12:
		SET_2_X_3(0, 2, 2,
		          0, 1, 1);
		break;

	case LIBFONTS_SUBPIXEL_ORDER_CLASS_OTHER:
	default:
		goto einval;
	}

	if (n0p)
		*n0p = n0;
	if (n1p)
		*n1p = n1;
	if (n2p)
		*n2p = n2;

	return 0;

einval:
	errno = EINVAL;
	return -1;
}


#else


int
main(void)
{
	uint8_t map[128], ns[3];
	size_t i, j, rowsize;

#define T(L)\
	do {\
		errno = 0;\
		ASSERT(libfonts_get_subpixel_map(L, 16, NULL, NULL, NULL, NULL) == -1);\
		ASSERT(errno == EINVAL);\
		\
		errno = 0;\
		ASSERT(libfonts_get_subpixel_map(L, 16, map, NULL, NULL, NULL) == -1);\
		ASSERT(errno == EINVAL);\
		\
		errno = 0;\
		ASSERT(libfonts_get_subpixel_map(L, 16, map, &ns[0], &ns[1], &ns[2]) == -1);\
		ASSERT(errno == EINVAL);\
	} while (0)

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_OTHER);
	T((enum libfonts_subpixel_order_class)999);

#undef T

#define TEST_1_X_3(R1C1, R1C2, R1C3)\
	do {\
		ASSERT(map[0] == (R1C1) - 1);\
		ASSERT(map[1] == (R1C2) - 1);\
		ASSERT(map[2] == (R1C3) - 1);\
		ASSERT(ns[0] == 1);\
		ASSERT(ns[1] == 1);\
		ASSERT(ns[2] == 1);\
	} while (0)

#define TEST_3_X_1(R1C1, R2C1, R3C1)\
	do {\
		ASSERT(map[0 * rowsize] == (R1C1) - 1);\
		ASSERT(map[1 * rowsize] == (R2C1) - 1);\
		ASSERT(map[2 * rowsize] == (R3C1) - 1);\
		ASSERT(ns[0] == 1);\
		ASSERT(ns[1] == 1);\
		ASSERT(ns[2] == 1);\
	} while (0)

#define TEST_2_X_2(R1C1, R1C2, R2C1, R2C2)\
	do {\
		ASSERT(map[0 * rowsize + 0] == (R1C1) - 1);\
		ASSERT(map[0 * rowsize + 1] == (R1C2) - 1);\
		ASSERT(map[1 * rowsize + 0] == (R2C1) - 1);\
		ASSERT(map[1 * rowsize + 1] == (R2C2) - 1);\
		ASSERT(ns[0] == 2);\
		ASSERT(ns[1] == 1);\
		ASSERT(ns[2] == 1);\
	} while (0)

#define TEST_3_X_2(R1C1, R1C2, R2C1, R2C2, R3C1, R3C2)\
	do {\
		ASSERT(map[0 * rowsize + 0] == (R1C1) - 1);\
		ASSERT(map[0 * rowsize + 1] == (R1C2) - 1);\
		ASSERT(map[1 * rowsize + 0] == (R2C1) - 1);\
		ASSERT(map[1 * rowsize + 1] == (R2C2) - 1);\
		ASSERT(map[2 * rowsize + 0] == (R3C1) - 1);\
		ASSERT(map[2 * rowsize + 1] == (R3C2) - 1);\
		ASSERT(ns[0] == 2);\
		ASSERT(ns[1] == 2);\
		ASSERT(ns[2] == 2);\
	} while (0)

#define TEST_2_X_3(R1C1, R1C2, R1C3, R2C1, R2C2, R2C3)\
	do {\
		ASSERT(map[0 * rowsize + 0] == (R1C1) - 1);\
		ASSERT(map[0 * rowsize + 1] == (R1C2) - 1);\
		ASSERT(map[0 * rowsize + 2] == (R1C3) - 1);\
		ASSERT(map[1 * rowsize + 0] == (R2C1) - 1);\
		ASSERT(map[1 * rowsize + 1] == (R2C2) - 1);\
		ASSERT(map[1 * rowsize + 2] == (R2C3) - 1);\
		ASSERT(ns[0] == 2);\
		ASSERT(ns[1] == 2);\
		ASSERT(ns[2] == 2);\
	} while (0)

#define TEST_3_X_2_BALANCED_2_X_2(R1C1, R1C2, R2C1, R2C2)\
	TEST_3_X_2((R1C1), (R1C2),\
		   (R1C1) == (R1C2) ? (R2C1) : (R1C1),\
		   (R1C1) == (R1C2) ? (R2C2) : (R1C2),\
	           (R2C1), (R2C2))

#define TEST_2_X_3_BALANCED_2_X_2(R1C1, R1C2, R2C1, R2C2)\
	TEST_2_X_3((R1C1), (R1C1) == (R2C1) ? (R1C2) : (R1C1), (R1C2),\
	           (R2C1), (R1C1) == (R2C1) ? (R2C2) : (R2C1), (R2C2))

#define CHECK_NS(H, W, N0, N1, N2)\
	do {\
		ns[0] = 0, ns[1] = 0, ns[2] = 0;\
		for (i = 0; i < (H); i++) {\
			for (j = 0; j < (W); j++) {\
				ASSERT(map[i * rowsize + j] <= 2);\
				ns[map[i * rowsize + j]] += 1;\
			}\
		}\
		ASSERT(ns[0] == N0);\
		ASSERT(ns[1] == N1);\
		ASSERT(ns[2] == N2);\
	} while (0)

#define T_(L, ROWSIZE, H, W, MAP, N0, N1, N2, NSCHECK, MAPTEST)\
	do {\
		rowsize = (ROWSIZE);\
		\
		errno = EDOM;\
		ns[0] = 0, ns[1] = 0, ns[2] = 0;\
		ASSERT(!libfonts_get_subpixel_map(L, rowsize, MAP, NULL, NULL, NULL));\
		ASSERT(errno == EDOM);\
		NSCHECK;\
		MAPTEST;\
		\
		errno = EDOM;\
		ns[0] = 0, ns[1] = 0, ns[2] = 0;\
		ASSERT(!libfonts_get_subpixel_map(L, rowsize, MAP, &ns[0], NULL, NULL));\
		ASSERT(errno == EDOM);\
		ASSERT(ns[0] == (N0));\
		NSCHECK;\
		MAPTEST;\
		\
		errno = EDOM;\
		ns[0] = 0, ns[1] = 0, ns[2] = 0;\
		ASSERT(!libfonts_get_subpixel_map(L, rowsize, MAP, NULL, &ns[1], NULL));\
		ASSERT(errno == EDOM);\
		ASSERT(ns[1] == (N1));\
		NSCHECK;\
		MAPTEST;\
		\
		errno = EDOM;\
		ns[0] = 0, ns[1] = 0, ns[2] = 0;\
		ASSERT(!libfonts_get_subpixel_map(L, rowsize, MAP, &ns[0], &ns[1], NULL));\
		ASSERT(errno == EDOM);\
		ASSERT(ns[0] == (N0));\
		ASSERT(ns[1] == (N1));\
		NSCHECK;\
		MAPTEST;\
		\
		errno = EDOM;\
		ns[0] = 0, ns[1] = 0, ns[2] = 0;\
		ASSERT(!libfonts_get_subpixel_map(L, rowsize, MAP, NULL, NULL, &ns[2]));\
		ASSERT(errno == EDOM);\
		ASSERT(ns[2] == (N2));\
		NSCHECK;\
		MAPTEST;\
		\
		errno = EDOM;\
		ns[0] = 0, ns[1] = 0, ns[2] = 0;\
		ASSERT(!libfonts_get_subpixel_map(L, rowsize, MAP, &ns[0], NULL, &ns[2]));\
		ASSERT(errno == EDOM);\
		ASSERT(ns[0] == (N0));\
		ASSERT(ns[2] == (N2));\
		NSCHECK;\
		MAPTEST;\
		\
		errno = EDOM;\
		ns[0] = 0, ns[1] = 0, ns[2] = 0;\
		ASSERT(!libfonts_get_subpixel_map(L, rowsize, MAP, NULL, &ns[1], &ns[2]));\
		ASSERT(errno == EDOM);\
		ASSERT(ns[1] == (N1));\
		ASSERT(ns[2] == (N2));\
		NSCHECK;\
		MAPTEST;\
		\
		errno = EDOM;\
		ns[0] = 0, ns[1] = 0, ns[2] = 0;\
		ASSERT(!libfonts_get_subpixel_map(L, rowsize, MAP, &ns[0], &ns[1], &ns[2]));\
		ASSERT(errno == EDOM);\
		ASSERT(ns[0] == (N0));\
		ASSERT(ns[1] == (N1));\
		ASSERT(ns[2] == (N2));\
		NSCHECK;\
		MAPTEST;\
	} while (0)

#define T2_(L, ROWSIZE, H, N0, N1, N2, N0P, N1P, N2P)\
	do {\
		errno = EDOM;\
		ns[0] = 0, ns[1] = 0, ns[2] = 0;\
		ASSERT(!libfonts_get_subpixel_map(L, ROWSIZE, NULL, N0P, N1P, N2P));\
		ASSERT(errno == EDOM);\
		ASSERT(ns[0] == (N0));\
		ASSERT(ns[1] == (N1));\
		ASSERT(ns[2] == (N2));\
		\
		if (H > 1) {\
			errno = 0;\
			ASSERT(libfonts_get_subpixel_map(L, ROWSIZE, map, N0P, N1P, N2P) == -1);\
			ASSERT(errno == EINVAL);\
		} else {\
			errno = EDOM;\
			ns[0] = 0, ns[1] = 0, ns[2] = 0;\
			ASSERT(!libfonts_get_subpixel_map(L, ROWSIZE, map, N0P, N1P, N2P));\
			ASSERT(errno == EDOM);\
			ASSERT(ns[0] == (N0));\
			ASSERT(ns[1] == (N1));\
			ASSERT(ns[2] == (N2));\
		}\
	} while (0)

#define T(L, H, W, N0, N1, N2, MAPTEST)\
	do {\
		T_(L, W, H, W, map, N0, N1, N2, CHECK_NS(H, W, N0, N1, N2), MAPTEST);\
		T_(L, W, H, W, NULL, N0, N1, N2,,);\
		T_(L, W + 1, H, W, map, N0, N1, N2, CHECK_NS(H, W, N0, N1, N2), MAPTEST);\
		T_(L, W + 1, H, W, NULL, N0, N1, N2,,);\
		T2_(L, W - 1, H, 0, 0, 0, NULL, NULL, NULL);\
		T2_(L, W - 1, H, N0, 0, 0, &ns[0], NULL, NULL);\
		T2_(L, W - 1, H, 0, N1, 0, NULL, &ns[1], NULL);\
		T2_(L, W - 1, H, N0, N1, 0, &ns[0], &ns[1], NULL);\
		T2_(L, W - 1, H, 0, 0, N2, NULL, NULL, &ns[2]);\
		T2_(L, W - 1, H, N0, 0, N2, &ns[0], NULL, &ns[2]);\
		T2_(L, W - 1, H, 0, N1, N2, NULL, &ns[1], &ns[2]);\
		T2_(L, W - 1, H, N0, N1, N2, &ns[0], &ns[1], &ns[2]);\
	} while (0)

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_123,
	  1, 3, 1, 1, 1,
	  TEST_1_X_3(1, 2, 3));

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_1_2_3,
	  3, 1, 1, 1, 1,
	  TEST_3_X_1(1,
	             2,
	             3));

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_11_23,
	  2, 2, 2, 1, 1,
	  TEST_2_X_2(1, 1,
	             2, 3));

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_21_31,
	  2, 2, 2, 1, 1,
	  TEST_2_X_2(2, 1,
	             3, 1));

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_32_11,
	  2, 2, 2, 1, 1,
	  TEST_2_X_2(3, 2,
	             1, 1));

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_13_12,
	  2, 2, 2, 1, 1,
	  TEST_2_X_2(1, 3,
	             1, 2));

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_BALANCED_11_23,
	  3, 2, 2, 2, 2,
	  TEST_3_X_2(1, 1,
	             2, 3,
	             2, 3));

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_BALANCED_21_31,
	  2, 3, 2, 2, 2,
	  TEST_2_X_3(2, 2, 1,
	             3, 3, 1));

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_BALANCED_32_11,
	  3, 2, 2, 2, 2,
	  TEST_3_X_2_BALANCED_2_X_2(3, 2,
	                            1, 1));

	T(LIBFONTS_SUBPIXEL_ORDER_CLASS_BALANCED_13_12,
	  2, 3, 2, 2, 2,
	  TEST_2_X_3_BALANCED_2_X_2(1, 3,
	                            1, 2));

	return 0;
}


#endif