This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libglitter_redistribute_energy_double.c
331 lines (298 loc) · 9.64 KB
/
libglitter_redistribute_energy_double.c
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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-equal"
#elif defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wfloat-equal"
#endif
static int
exactly(double a, double b)
{
return a == b;
}
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic pop
#elif defined(__clang__)
# pragma clang diagnostic pop
#endif
static void
vconvolute(double *restrict raster, size_t rowsize, size_t width, size_t height, size_t kernelsize, const double *kernel)
{
size_t y, x, i;
if (kernelsize == 3 && exactly(kernel[0], kernel[1]) && exactly(kernel[1], kernel[2])) {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++)
raster[x] += raster[x + 1 * rowsize];
for (x = 0; x < width; x++) {
raster[x] += raster[x + 2 * rowsize];
raster[x] *= kernel[0];
}
raster = &raster[rowsize];
}
for (x = 0; x < width; x++) {
raster[x] += raster[x + rowsize];
raster[x] *= kernel[0];
raster[x + rowsize] *= kernel[0];
}
} else {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++)
raster[x] *= kernel[0];
for (i = 1; i < kernelsize; i++)
for (x = 0; x < width; x++)
raster[x] = fma(raster[x + i * rowsize], kernel[i], raster[x]);
raster = &raster[rowsize];
}
for (y = 0; y < kernelsize - 1; y++) {
for (x = 0; x < width; x++)
raster[x] *= kernel[0];
for (i = 1; i < kernelsize - 1 - y; i++)
for (x = 0; x < width; x++)
raster[x] = fma(raster[x + i * rowsize], kernel[i], raster[x]);
raster = &raster[rowsize];
}
}
}
static void
hconvolute(double *restrict raster, size_t rowsize, size_t width, size_t height, size_t kernelsize, const double *kernel)
{
size_t y, x, i;
if (kernelsize == 3 && exactly(kernel[0], kernel[1]) && exactly(kernel[1], kernel[2])) {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
raster[x + 1] += raster[x + 2];
raster[x] += raster[x + 2];
raster[x] *= kernel[0];
}
raster[width] *= kernel[0];
raster[width + 1] *= kernel[0];
raster = &raster[rowsize];
}
} else {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
raster[x] *= kernel[0];
for (i = 1; i < kernelsize; i++)
raster[x] = fma(raster[x + i], kernel[i], raster[x]);
}
raster = &raster[width];
for (x = 0; x < kernelsize - 1; x++) {
raster[x] *= kernel[0];
for (i = 1; i < kernelsize - 1 - x; i++)
raster[x] = fma(raster[x + i], kernel[i], raster[x]);
}
raster = &raster[rowsize - width];
}
}
}
void
libglitter_redistribute_energy_double(double *restrict raster, size_t rowsize, size_t width, size_t height,
size_t hkernelsize, size_t vkernelsize, const double *hkernel, const double *vkernel)
{
if (vkernelsize > 1) {
raster -= (vkernelsize - 1) * rowsize;
vconvolute(raster, rowsize, width, height, vkernelsize, vkernel);
height += vkernelsize - 1;
}
if (hkernelsize > 1) {
raster -= hkernelsize - 1;
hconvolute(raster, rowsize, width, height, hkernelsize, hkernel);
width += hkernelsize - 1;
}
}
#else
#define TOLERANCE 0.0001
static int
eq(double a, double b)
{
double r = a - b;
return (r < 0 ? -r : r) < TOLERANCE;
}
int
main(void)
{
double raster[100], hkern[5], vkern[5];
size_t i, j;
for (i = 0; i < sizeof(raster) / sizeof(*raster); i++)
raster[i] = (double)i;
hkern[0] = vkern[0] = 0;
libglitter_redistribute_energy_double(&raster[11], 10, 7, 8, 1, 1, NULL, NULL);
for (i = 0; i < sizeof(raster) / sizeof(*raster); i++)
ASSERT(raster[i] == (double)i);
libglitter_redistribute_energy_double(&raster[11], 10, 7, 8, 1, 1, hkern, NULL);
for (i = 0; i < sizeof(raster) / sizeof(*raster); i++)
ASSERT(raster[i] == (double)i);
libglitter_redistribute_energy_double(&raster[11], 10, 7, 8, 1, 1, NULL, vkern);
for (i = 0; i < sizeof(raster) / sizeof(*raster); i++)
ASSERT(raster[i] == (double)i);
libglitter_redistribute_energy_double(&raster[11], 10, 7, 8, 1, 1, hkern, vkern);
for (i = 0; i < sizeof(raster) / sizeof(*raster); i++)
ASSERT(raster[i] == (double)i);
memset(raster, 0, sizeof(raster));
raster[53] = 1;
raster[57] = 2;
hkern[0] = 1 / 3.;
hkern[1] = 1 / 3.;
hkern[2] = 1 / 3.;
libglitter_redistribute_energy_double(&raster[12], 10, 7, 8, 3, 1, hkern, NULL);
ASSERT(eq(raster[52 - 1], 1 / 3.));
ASSERT(eq(raster[53 - 1], 1 / 3.));
ASSERT(eq(raster[54 - 1], 1 / 3.));
ASSERT(eq(raster[56 - 1], 2 / 3.));
ASSERT(eq(raster[57 - 1], 2 / 3.));
ASSERT(eq(raster[58 - 1], 2 / 3.));
for (i = 0; i < sizeof(raster) / sizeof(*raster); i++)
if (!(51 <= i && i <= 53) && !(55 <= i && i <= 57))
ASSERT(!raster[i]);
memset(raster, 0, sizeof(raster));
raster[53] = 1;
raster[57] = 2;
hkern[0] = 1 / 4.;
hkern[1] = 1 / 2.;
hkern[2] = 1 / 4.;
libglitter_redistribute_energy_double(&raster[12], 10, 7, 8, 3, 1, hkern, NULL);
ASSERT(eq(raster[52 - 1], 1 / 4.));
ASSERT(eq(raster[53 - 1], 1 / 2.));
ASSERT(eq(raster[54 - 1], 1 / 4.));
ASSERT(eq(raster[56 - 1], 2 / 4.));
ASSERT(eq(raster[57 - 1], 2 / 2.));
ASSERT(eq(raster[58 - 1], 2 / 4.));
for (i = 0; i < sizeof(raster) / sizeof(*raster); i++)
if (!(51 <= i && i <= 53) && !(55 <= i && i <= 57))
ASSERT(!raster[i]);
memset(raster, 0, sizeof(raster));
raster[35] = 1;
raster[75] = 2;
vkern[0] = 1 / 3.;
vkern[1] = 1 / 3.;
vkern[2] = 1 / 3.;
libglitter_redistribute_energy_double(&raster[21], 10, 8, 7, 1, 3, NULL, vkern);
ASSERT(eq(raster[25 - 10], 1 / 3.));
ASSERT(eq(raster[35 - 10], 1 / 3.));
ASSERT(eq(raster[45 - 10], 1 / 3.));
ASSERT(eq(raster[65 - 10], 2 / 3.));
ASSERT(eq(raster[75 - 10], 2 / 3.));
ASSERT(eq(raster[85 - 10], 2 / 3.));
for (i = 0; i < sizeof(raster) / sizeof(*raster); i++)
if (i != 15 && i != 25 && i != 35 && i != 55 && i != 65 && i != 75)
ASSERT(!raster[i]);
memset(raster, 0, sizeof(raster));
raster[35] = 1;
raster[75] = 2;
vkern[0] = 1 / 4.;
vkern[1] = 1 / 2.;
vkern[2] = 1 / 4.;
libglitter_redistribute_energy_double(&raster[21], 10, 8, 7, 1, 3, NULL, vkern);
ASSERT(eq(raster[25 - 10], 1 / 4.));
ASSERT(eq(raster[35 - 10], 1 / 2.));
ASSERT(eq(raster[45 - 10], 1 / 4.));
ASSERT(eq(raster[65 - 10], 2 / 4.));
ASSERT(eq(raster[75 - 10], 2 / 2.));
ASSERT(eq(raster[85 - 10], 2 / 4.));
for (i = 0; i < sizeof(raster) / sizeof(*raster); i++)
if (i != 15 && i != 25 && i != 35 && i != 55 && i != 65 && i != 75)
ASSERT(!raster[i]);
memset(raster, 0, sizeof(raster));
for (j = 20; j < 100; j += 10)
for (i = 2; i < 10; i += 1)
raster[j + i] = 1;
vkern[0] = 1 / 3.;
vkern[1] = 1 / 3.;
vkern[2] = 1 / 3.;
hkern[0] = 1 / 3.;
hkern[1] = 1 / 3.;
hkern[2] = 1 / 3.;
libglitter_redistribute_energy_double(&raster[22], 10, 8, 8, 3, 3, hkern, vkern);
for (j = 0; j < 100; j += 10)
for (i = 0; i < 10; i += 1)
ASSERT(eq(raster[j + i],
((i == 0 || i == 9) ? 1 / 3. :
(i == 1 || i == 8) ? 2 / 3. : 1.) *
((j == 0 || j == 90) ? 1 / 3. :
(j == 10 || j == 80) ? 2 / 3. : 1.)));
memset(raster, 0, sizeof(raster));
for (j = 20; j < 100; j += 10)
for (i = 2; i < 10; i += 1)
raster[j + i] = 1;
vkern[0] = 1 / 3.;
vkern[1] = 1 / 3.;
vkern[2] = 1 / 3.;
hkern[0] = 2 / 9.;
hkern[1] = 5 / 9.;
hkern[2] = 2 / 9.;
libglitter_redistribute_energy_double(&raster[22], 10, 8, 8, 3, 3, hkern, vkern);
for (j = 0; j < 100; j += 10)
for (i = 0; i < 10; i += 1)
ASSERT(eq(raster[j + i],
((i == 0 || i == 9) ? 2 / 9. :
(i == 1 || i == 8) ? 7 / 9. : 1.) *
((j == 0 || j == 90) ? 1 / 3. :
(j == 10 || j == 80) ? 2 / 3. : 1.)));
memset(raster, 0, sizeof(raster));
for (j = 20; j < 100; j += 10)
for (i = 2; i < 10; i += 1)
raster[j + i] = 1;
vkern[0] = 2 / 9.;
vkern[1] = 5 / 9.;
vkern[2] = 2 / 9.;
hkern[0] = 1 / 3.;
hkern[1] = 1 / 3.;
hkern[2] = 1 / 3.;
libglitter_redistribute_energy_double(&raster[22], 10, 8, 8, 3, 3, hkern, vkern);
for (j = 0; j < 100; j += 10)
for (i = 0; i < 10; i += 1)
ASSERT(eq(raster[j + i],
((i == 0 || i == 9) ? 1 / 3. :
(i == 1 || i == 8) ? 2 / 3. : 1.) *
((j == 0 || j == 90) ? 2 / 9. :
(j == 10 || j == 80) ? 7 / 9. : 1.)));
memset(raster, 0, sizeof(raster));
for (j = 20; j < 100; j += 10)
for (i = 2; i < 10; i += 1)
raster[j + i] = 1;
vkern[0] = 2 / 9.;
vkern[1] = 5 / 9.;
vkern[2] = 2 / 9.;
hkern[0] = 2 / 9.;
hkern[1] = 5 / 9.;
hkern[2] = 2 / 9.;
libglitter_redistribute_energy_double(&raster[22], 10, 8, 8, 3, 3, hkern, vkern);
for (j = 0; j < 100; j += 10)
for (i = 0; i < 10; i += 1)
ASSERT(eq(raster[j + i],
((i == 0 || i == 9) ? 2 / 9. :
(i == 1 || i == 8) ? 7 / 9. : 1.) *
((j == 0 || j == 90) ? 2 / 9. :
(j == 10 || j == 80) ? 7 / 9. : 1.)));
memset(raster, 0, sizeof(raster));
for (j = 40; j < 100; j += 10)
for (i = 4; i < 10; i += 1)
raster[j + i] = 1;
vkern[0] = 1 / 5.;
vkern[1] = 1 / 5.;
vkern[2] = 1 / 5.;
vkern[3] = 1 / 5.;
vkern[4] = 1 / 5.;
hkern[0] = 1 / 9.;
hkern[1] = 2 / 9.;
hkern[2] = 3 / 9.;
hkern[3] = 2 / 9.;
hkern[4] = 1 / 9.;
libglitter_redistribute_energy_double(&raster[44], 10, 6, 6, 5, 5, hkern, vkern);
for (j = 0; j < 100; j += 10)
for (i = 0; i < 10; i += 1)
ASSERT(eq(raster[j + i],
((i == 0 || i == 9) ? 1 / 9. :
(i == 1 || i == 8) ? 3 / 9. :
(i == 2 || i == 7) ? 6 / 9. :
(i == 3 || i == 6) ? 8 / 9. : 1.) *
((j == 0 || j == 90) ? 1 / 5. :
(j == 10 || j == 80) ? 2 / 5. :
(j == 20 || j == 70) ? 3 / 5. :
(j == 30 || j == 60) ? 4 / 5. : 1.)));
return 0;
}
#endif