aboutsummaryrefslogtreecommitdiffstats
path: root/en_masse-template.c
blob: 7869d23ceee8582600ca6c9adc3b842471993907 (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
/* See LICENSE file for copyright and license details. */

#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic ignored "-Wfloat-equal"
#elif defined(__clang__)
# pragma clang diagnostic ignored "-Wcomma"
# pragma clang diagnostic ignored "-Wfloat-equal"
# pragma clang diagnostic ignored "-Wvla"
# pragma clang diagnostic ignored "-Wtautological-compare"
# pragma clang diagnostic ignored "-Wcovered-switch-default"
# pragma clang diagnostic ignored "-Wfloat-conversion"
# pragma clang diagnostic ignored "-Wconversion"
# pragma clang diagnostic ignored "-Wdouble-promotion"
# pragma clang diagnostic ignored "-Wswitch-enum"
# pragma clang diagnostic ignored "-Wcast-align"
#endif



int
libcolour_convert_en_masse(const libcolour_colour_t *from, const libcolour_colour_t *to,
			   libcolour_convert_en_masse_mode_t mode, size_t n, ...)
{
	libcolour_colour_t tfrom, tto;
	libcolour_convert_en_masse_mode_t alpha_mode = mode & 3;
	int on_cpu = mode & LIBCOLOUR_CONVERT_EN_MASSE_ON_CPU;
	int no_override = mode & LIBCOLOUR_CONVERT_EN_MASSE_NO_OVERRIDE;
	va_list args;
	TYPE *in1, *in2, *in3, *in_alpha = NULL;
	TYPE *out1, *out2, *out3, *out_alpha = NULL;
	size_t width, i;

	if ((unsigned int)mode > 15U) {
		errno = EINVAL;
		return -1;
	}
	if (n < 0) {
		errno = EINVAL;
		return -1;
	}

	va_start(args, n);

	if (alpha_mode == LIBCOLOUR_CONVERT_EN_MASSE_NO_ALPHA) {
		in1 = va_arg(args, TYPE *);
		in2 = in1 + 1;
		in3 = in1 + 2;
		width = 3;
	} else if (alpha_mode == LIBCOLOUR_CONVERT_EN_MASSE_ALPHA_FIRST) {
		in_alpha = va_arg(args, TYPE *);
		in1 = in_alpha + 1;
		in2 = in_alpha + 2;
		in3 = in_alpha + 3;
		width = 4;
	} else if (alpha_mode == LIBCOLOUR_CONVERT_EN_MASSE_ALPHA_LAST) {
		in1 = va_arg(args, TYPE *);
		in2 = in1 + 1;
		in3 = in1 + 2;
		in_alpha = in1 + 3;
		width = 4;
	} else {
		in1 = va_arg(args, TYPE *);
		in2 = va_arg(args, TYPE *);
		in3 = va_arg(args, TYPE *);
		width = 1;
	}

	if (!no_override) {
		out1 = in1;
		out2 = in2;
		out3 = in3;
	} else if (alpha_mode == LIBCOLOUR_CONVERT_EN_MASSE_NO_ALPHA) {
		out1 = va_arg(args, TYPE *);
		out2 = out1 + 1;
		out3 = out1 + 2;
	} else if (alpha_mode == LIBCOLOUR_CONVERT_EN_MASSE_ALPHA_FIRST) {
		out_alpha = va_arg(args, TYPE *);
		out1 = out_alpha + 1;
		out2 = out_alpha + 2;
		out3 = out_alpha + 3;
	} else if (alpha_mode == LIBCOLOUR_CONVERT_EN_MASSE_ALPHA_LAST) {
		out1 = va_arg(args, TYPE *);
		out2 = out1 + 1;
		out3 = out1 + 2;
		out_alpha = out1 + 3;
	} else {
		out1 = va_arg(args, TYPE *);
		out2 = va_arg(args, TYPE *);
		out3 = va_arg(args, TYPE *);
	}

	va_end(args);

	switch (from->model) {
#define X(C, T, N) case C: memcpy(&tfrom, from, sizeof(T));
	LIST_MODELS(X)
#undef X
	default:
		errno = EINVAL;
		return -1;
	}

	switch (to->model) {
#define X(C, T, N) case C: memcpy(&tto, to, sizeof(T));
	LIST_MODELS(X)
#undef X
	default:
		errno = EINVAL;
		return -1;
	}

	if (in_alpha != out_alpha) {
		for (i = 0; i < n; i++) {
			*out_alpha = *in_alpha;
			in_alpha += width;
			out_alpha += width;
		}
	}

	while (n--) {
		tfrom.rgb.R = *in1;
		tfrom.rgb.G = *in2;
		tfrom.rgb.B = *in3;
		libcolour_convert(&tfrom, &tto);
		*out1 = tto.rgb.R;
		*out2 = tto.rgb.G;
		*out3 = tto.rgb.B;
		in1 += width;
		in2 += width;
		in3 += width;
		out1 += width;
		out2 += width;
		out3 += width;
	}

	(void) on_cpu;
	return 0;
}