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
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic ignored "-Wunsuffixed-float-constants"
#endif
void
libglitter_desaturate_double(double **rasters, size_t nrasters, size_t rowsize, size_t cellsize,
size_t width, size_t height, double saturation, const double *restrict primary_ys)
{
size_t y, x, ch, raster_y, raster_i;
double intensity;
rowsize *= cellsize;
if (nrasters == 3) {
double *raster1 = rasters[0];
double *raster2 = rasters[1];
double *raster3 = rasters[2];
double primary1_y = primary_ys ? primary_ys[0] : (double)0.212673370378408277403536885686;
double primary2_y = primary_ys ? primary_ys[1] : (double)0.715151730491031756287156895269;
double primary3_y = primary_ys ? primary_ys[2] : (double)0.072174899130559869164791564344;
for (y = 0, raster_y = 0; y < height; y++, raster_y += rowsize) {
for (x = 0, raster_i = raster_y; x < width; x++, raster_i += cellsize) {
intensity = raster1[raster_i] * primary1_y;
intensity += raster2[raster_i] * primary2_y;
intensity += raster3[raster_i] * primary3_y;
raster1[raster_i] = fma(raster1[raster_i] - intensity, saturation, intensity);
raster2[raster_i] = fma(raster2[raster_i] - intensity, saturation, intensity);
raster3[raster_i] = fma(raster3[raster_i] - intensity, saturation, intensity);
}
}
} else {
for (y = 0, raster_y = 0; y < height; y++, raster_y += rowsize) {
for (x = 0, raster_i = raster_y; x < width; x++, raster_i += cellsize) {
intensity = 0;
for (ch = 0; ch < nrasters; ch++)
intensity += rasters[ch][raster_i] * primary_ys[ch];
for (ch = 0; ch < nrasters; ch++)
rasters[ch][raster_i] = fma(rasters[ch][raster_i] - intensity, saturation, intensity);
}
}
}
}
#else
int
main(void)
{
double rasters1[4 * 10 * 3][4], *rasters1list[4], primary_ys[4], saturation;
double rasters2[4 * 10 * 3][4], *rasters2list[4], saturations[4];
size_t i, j;
void *randptr = malloc(1);
srand((unsigned int)(uintptr_t)randptr);
free(randptr);
#define RANDOMISE(SAT)\
do {\
saturation = (SAT);\
saturations[0] = saturation;\
saturations[1] = saturation;\
saturations[2] = saturation;\
saturations[3] = saturation;\
for (i = 0; i < 4; i++) {\
rasters1list[i] = rasters1[i];\
rasters2list[i] = rasters2[i];\
primary_ys[i] = rand() / (double)RAND_MAX;\
for (j = 0; j < 4 * 10 * 3; j++)\
rasters1[j][i] = rand() / (double)RAND_MAX;\
}\
memcpy(rasters2, rasters1, sizeof(rasters1));\
} while (0)
RANDOMISE(rand() / (double)RAND_MAX);
libglitter_desaturate_double(rasters1list, 4, 10, 3, 7, 4, saturation, primary_ys);
libglitter_per_channel_desaturate_double(rasters2list, 4, 10, 3, 7, 4, saturations, primary_ys);
ASSERT(!memcmp(rasters1, rasters2, sizeof(rasters1)));
RANDOMISE(rand() / (double)RAND_MAX);
libglitter_desaturate_double(rasters1list, 3, 10, 3, 7, 4, saturation, primary_ys);
libglitter_per_channel_desaturate_double(rasters2list, 3, 10, 3, 7, 4, saturations, primary_ys);
ASSERT(!memcmp(rasters1, rasters2, sizeof(rasters1)));
RANDOMISE(rand() / (double)RAND_MAX);
libglitter_desaturate_double(rasters1list, 3, 10, 3, 7, 4, saturation, NULL);
libglitter_per_channel_desaturate_double(rasters2list, 3, 10, 3, 7, 4, saturations, NULL);
ASSERT(!memcmp(rasters1, rasters2, sizeof(rasters1)));
RANDOMISE(0);
libglitter_desaturate_double(rasters1list, 4, 10, 3, 7, 4, saturation, primary_ys);
libglitter_per_channel_desaturate_double(rasters2list, 4, 10, 3, 7, 4, saturations, primary_ys);
ASSERT(!memcmp(rasters1, rasters2, sizeof(rasters1)));
RANDOMISE(0);
libglitter_desaturate_double(rasters1list, 3, 10, 3, 7, 4, saturation, primary_ys);
libglitter_per_channel_desaturate_double(rasters2list, 3, 10, 3, 7, 4, saturations, primary_ys);
ASSERT(!memcmp(rasters1, rasters2, sizeof(rasters1)));
RANDOMISE(0);
libglitter_desaturate_double(rasters1list, 3, 10, 3, 7, 4, saturation, NULL);
libglitter_per_channel_desaturate_double(rasters2list, 3, 10, 3, 7, 4, saturations, NULL);
ASSERT(!memcmp(rasters1, rasters2, sizeof(rasters1)));
RANDOMISE(1);
libglitter_desaturate_double(rasters1list, 4, 10, 3, 7, 4, saturation, primary_ys);
libglitter_per_channel_desaturate_double(rasters2list, 4, 10, 3, 7, 4, saturations, primary_ys);
ASSERT(!memcmp(rasters1, rasters2, sizeof(rasters1)));
RANDOMISE(1);
libglitter_desaturate_double(rasters1list, 3, 10, 3, 7, 4, saturation, primary_ys);
libglitter_per_channel_desaturate_double(rasters2list, 3, 10, 3, 7, 4, saturations, primary_ys);
ASSERT(!memcmp(rasters1, rasters2, sizeof(rasters1)));
RANDOMISE(1);
libglitter_desaturate_double(rasters1list, 3, 10, 3, 7, 4, saturation, NULL);
libglitter_per_channel_desaturate_double(rasters2list, 3, 10, 3, 7, 4, saturations, NULL);
ASSERT(!memcmp(rasters1, rasters2, sizeof(rasters1)));
return 0;
}
#endif
|