Blender V5.3
BLI_math_interp_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "BLI_math_interp.hh"
8
9namespace blender {
10
11using namespace blender::math;
12
13static constexpr float float_tolerance = 0.00005f;
14static constexpr int image_width = 3;
15static constexpr int image_height = 3;
16static constexpr uchar image_char[image_height][image_width][4] = {
17 {{255, 254, 217, 216}, {230, 230, 230, 230}, {240, 160, 90, 20}},
18 {{0, 1, 2, 3}, {62, 72, 82, 92}, {126, 127, 128, 129}},
19 {{1, 2, 3, 4}, {73, 108, 153, 251}, {128, 129, 130, 131}},
20};
21static constexpr float image_fl[image_height][image_width][4] = {
22 {{255, 254, 217, 216}, {230, 230, 230, 230}, {240, 160, 90, 20}},
23 {{0, 1, 2, 3}, {62, 72, 82, 92}, {126, 127, 128, 129}},
24 {{1, 2, 3, 4}, {73, 108, 153, 251}, {128, 129, 130, 131}},
25};
26
27TEST(math_interp, NearestCharExactSamples)
28{
29 uchar4 res;
30 uchar4 exp1 = {73, 108, 153, 251};
32 EXPECT_EQ(exp1, res);
33 uchar4 exp2 = {240, 160, 90, 20};
35 EXPECT_EQ(exp2, res);
36}
37
38TEST(math_interp, NearestCharHalfwaySamples)
39{
40 uchar4 res;
41 uchar4 exp1 = {0, 1, 2, 3};
43 EXPECT_EQ(exp1, res);
44 uchar4 exp2 = {255, 254, 217, 216};
46 EXPECT_EQ(exp2, res);
47}
48
49TEST(math_interp, NearestFloatExactSamples)
50{
51 float4 res;
52 float4 exp1 = {73.0f, 108.0f, 153.0f, 251.0f};
54 EXPECT_EQ(exp1, res);
55 float4 exp2 = {240.0f, 160.0f, 90.0f, 20.0f};
57 EXPECT_EQ(exp2, res);
58}
59
60TEST(math_interp, NearestFloatHalfwaySamples)
61{
62 float4 res;
63 float4 exp1 = {0.0f, 1.0f, 2.0f, 3.0f};
65 EXPECT_EQ(exp1, res);
66 float4 exp2 = {255.0f, 254.0f, 217.0f, 216.0f};
68 EXPECT_EQ(exp2, res);
69}
70
71TEST(math_interp, BilinearCharExactSamples)
72{
73 uchar4 res;
74 uchar4 exp1 = {73, 108, 153, 251};
76 EXPECT_EQ(exp1, res);
77 uchar4 exp2 = {240, 160, 90, 20};
79 EXPECT_EQ(exp2, res);
80}
81
82TEST(math_interp, BilinearCharHalfwayUSamples)
83{
84 uchar4 res;
85 uchar4 exp1 = {31, 37, 42, 48};
87 EXPECT_EQ(exp1, res);
88 uchar4 exp2 = {243, 242, 224, 223};
90 EXPECT_EQ(exp2, res);
91}
92
93TEST(math_interp, BilinearCharHalfwayVSamples)
94{
95 uchar4 res;
96 uchar4 exp1 = {1, 2, 3, 4};
98 EXPECT_EQ(exp1, res);
99 uchar4 exp2 = {127, 128, 129, 130};
101 EXPECT_EQ(exp2, res);
102}
103
104TEST(math_interp, BilinearCharSamples)
105{
106 uchar4 res;
107 uchar4 exp1 = {136, 133, 132, 130};
109 image_char[0][0], image_width, image_height, 1.25f, 0.625f);
110 EXPECT_EQ(exp1, res);
111 uchar4 exp2 = {219, 191, 167, 142};
113 EXPECT_EQ(exp2, res);
114}
115
116TEST(math_interp, BilinearFloatSamples)
117{
118 float4 res;
119 float4 exp1 = {135.9375f, 133.28125f, 131.5625f, 129.84375f};
121 EXPECT_V4_NEAR(exp1, res, float_tolerance);
122 float4 exp2 = {219.36f, 191.2f, 166.64f, 142.08f};
124 EXPECT_V4_NEAR(exp2, res, float_tolerance);
125}
126
127TEST(math_interp, BilinearCharPartiallyOutsideImageBorder)
128{
129 uchar4 res;
130 uchar4 exp1 = {1, 1, 2, 2};
132 EXPECT_EQ(exp1, res);
133 uchar4 exp2 = {9, 11, 15, 22};
135 EXPECT_EQ(exp2, res);
136 uchar4 exp3 = {173, 115, 65, 14};
138 EXPECT_EQ(exp3, res);
139}
140
141TEST(math_interp, BilinearCharPartiallyOutsideImage)
142{
143 uchar4 res;
144 uint4 exp1 = {1, 2, 3, 4};
146 EXPECT_EQ(exp1, uint4(res));
147 uint4 exp2 = {87, 113, 147, 221};
149 EXPECT_EQ(exp2, uint4(res));
150 uint4 exp3 = {240, 160, 90, 20};
152 EXPECT_EQ(exp3, uint4(res));
153}
154
155TEST(math_interp, BilinearCharPartiallyOutsideImageWrap)
156{
157 uchar4 res;
158 uchar4 exp1 = {65, 66, 67, 68};
160 EXPECT_EQ(exp1, res);
161 uchar4 exp2 = {218, 203, 190, 182};
163 EXPECT_EQ(exp2, res);
164 uchar4 exp3 = {229, 171, 114, 64};
166 EXPECT_EQ(exp3, res);
167 /* test that Infinity does not wrap into an out-of-range number. If it does this throws an assert
168 * in math_interp.cc */
170 image_char[0][0], image_width, image_height, INFINITY, -INFINITY);
171}
172
173TEST(math_interp, BilinearFloatPartiallyOutsideImageBorder)
174{
175 float4 res;
176 float4 exp1 = {0.5f, 1, 1.5f, 2};
178 EXPECT_V4_NEAR(exp1, res, float_tolerance);
179 float4 exp2 = {8.675f, 11.325f, 14.725f, 22.1f};
181 EXPECT_V4_NEAR(exp2, res, float_tolerance);
182 float4 exp3 = {172.8f, 115.2f, 64.8f, 14.4f};
184 EXPECT_V4_NEAR(exp3, res, float_tolerance);
185}
186
187TEST(math_interp, BilinearFloatPartiallyOutsideImage)
188{
189 float4 res;
190 float4 exp1 = {1.0f, 2.0f, 3.0f, 4.0f};
191 res = interpolate_bilinear_fl(image_fl[0][0], image_width, image_height, -0.5f, 2.0f);
192 EXPECT_V4_NEAR(exp1, res, float_tolerance);
193 float4 exp2 = {86.75f, 113.25f, 147.25f, 221.0f};
194 res = interpolate_bilinear_fl(image_fl[0][0], image_width, image_height, 1.25f, 2.9f);
195 EXPECT_V4_NEAR(exp2, res, float_tolerance);
196 float4 exp3 = {240.0f, 160.0f, 90.0f, 20.0f};
197 res = interpolate_bilinear_fl(image_fl[0][0], image_width, image_height, 2.2f, -0.1f);
198 EXPECT_V4_NEAR(exp3, res, float_tolerance);
199 /* out-of-range numbers cast to 0x80000000 which is negative. Make sure it produces correct
200 * extend edge */
201 float4 exp4 = {240, 160, 90, 20};
202 res = interpolate_bilinear_fl(image_fl[0][0], image_width, image_height, 1e10f, -1e10f);
203 EXPECT_EQ(exp4, res);
204}
205
206TEST(math_interp, BilinearFloatPartiallyOutsideImageWrap)
207{
208 float4 res;
209 float4 exp1 = {64.5f, 65.5f, 66.5f, 67.5f};
211 res,
214 4,
215 -0.5f,
216 2.0f,
219 EXPECT_V4_NEAR(exp1, res, float_tolerance);
221 EXPECT_V4_NEAR(exp1, res, float_tolerance);
222
223 float4 exp2 = {217.92502f, 202.57501f, 190.22501f, 181.85f};
225 res,
228 4,
229 1.25f,
230 2.9f,
233 EXPECT_V4_NEAR(exp2, res, float_tolerance);
235 EXPECT_V4_NEAR(exp2, res, float_tolerance);
236
237 float4 exp3 = {228.96f, 171.27998f, 114.32f, 63.84f};
239 res,
242 4,
243 2.2f,
244 -0.1f,
247 EXPECT_V4_NEAR(exp3, res, float_tolerance);
249 EXPECT_V4_NEAR(exp3, res, float_tolerance);
250
251 /* Wrap only V axis. */
252 float4 exp4 = {191.5f, 191.0f, 163.5f, 163.0f};
254 res,
257 4,
258 -0.5f,
259 2.75f,
262 EXPECT_V4_NEAR(exp4, res, float_tolerance);
264 res,
267 4,
268 -0.5f,
269 5.75f,
272 EXPECT_V4_NEAR(exp4, res, float_tolerance);
273}
274
275TEST(math_interp, BilinearCharFullyOutsideImage)
276{
277 uchar4 res;
278 uchar4 exp = {0, 0, 0, 0};
279 /* Out of range on U */
281 EXPECT_EQ(exp, res);
283 EXPECT_EQ(exp, res);
285 EXPECT_EQ(exp, res);
287 EXPECT_EQ(exp, res);
288
289 /* Out of range on V */
291 EXPECT_EQ(exp, res);
293 EXPECT_EQ(exp, res);
295 EXPECT_EQ(exp, res);
297 EXPECT_EQ(exp, res);
298}
299
300TEST(math_interp, BilinearFloatFullyOutsideImage)
301{
302 float4 res;
303 float4 exp = {0, 0, 0, 0};
304 /* Out of range on U */
306 EXPECT_EQ(exp, res);
308 EXPECT_EQ(exp, res);
310 EXPECT_EQ(exp, res);
312 EXPECT_EQ(exp, res);
313
314 /* Out of range on V */
316 EXPECT_EQ(exp, res);
318 EXPECT_EQ(exp, res);
320 EXPECT_EQ(exp, res);
322 EXPECT_EQ(exp, res);
323}
324
325TEST(math_interp, CubicBSplineCharExactSamples)
326{
327 uchar4 res;
328 uchar4 exp1 = {69, 90, 116, 172};
330 EXPECT_EQ(exp1, res);
331 uchar4 exp2 = {218, 163, 115, 66};
333 EXPECT_EQ(exp2, res);
334}
335
336TEST(math_interp, CubicBSplineCharSamples)
337{
338 uchar4 res;
339 uchar4 exp1 = {142, 136, 131, 128};
341 EXPECT_EQ(exp1, res);
342 uchar4 exp2 = {202, 177, 154, 132};
344 EXPECT_EQ(exp2, res);
345}
346
347TEST(math_interp, CubicBSplineFloatSamples)
348{
349 float4 res;
350 float4 exp1 = {142.14418f, 136.255798f, 130.87924f, 127.85243f};
352 EXPECT_V4_NEAR(exp1, res, float_tolerance);
353 float4 exp2 = {202.36082f, 177.13397f, 154.21078f, 132.30153f};
355 EXPECT_V4_NEAR(exp2, res, float_tolerance);
356}
357
358TEST(math_interp, CubicBSplineCharPartiallyOutsideImage)
359{
360 uchar4 res;
361 uchar4 exp1 = {2, 4, 6, 8};
363 EXPECT_EQ(int4(exp1), int4(res));
364 uchar4 exp2 = {85, 107, 135, 195};
366 EXPECT_EQ(int4(exp2), int4(res));
367 uchar4 exp3 = {225, 161, 105, 49};
369 EXPECT_EQ(int4(exp3), int4(res));
370}
371
372TEST(math_interp, CubicBSplineFloatPartiallyOutsideImage)
373{
374 float4 res;
375 float4 exp1 = {2.29861f, 3.92014f, 5.71528f, 8.430554f};
377 EXPECT_V4_NEAR(exp1, res, float_tolerance);
379 res,
382 4,
383 -0.5f,
384 2.0f,
387 EXPECT_V4_NEAR(exp1, res, float_tolerance);
388
389 float4 exp2 = {85.41022f, 107.21497f, 135.13849f, 195.49146f};
391 EXPECT_V4_NEAR(exp2, res, float_tolerance);
393 res,
396 4,
397 1.25f,
398 2.9f,
401 EXPECT_V4_NEAR(exp2, res, float_tolerance);
402
403 float4 exp3 = {224.73579f, 160.66783f, 104.63521f, 48.60260f};
405 EXPECT_V4_NEAR(exp3, res, float_tolerance);
407 res,
410 4,
411 2.2f,
412 -0.1f,
415 EXPECT_V4_NEAR(exp3, res, float_tolerance);
416
417 /* Different wrap modes. */
418 float4 exp4 = {122.66441f, 89.68848f, 60.85706f, 32.02566f};
420 res,
423 4,
424 2.2f,
425 -0.1f,
428 EXPECT_V4_NEAR(exp4, res, float_tolerance);
429
430 float4 exp5 = {189.57422f, 157.32167f, 122.71796f, 95.81750f};
432 res,
435 4,
436 2.2f,
437 -0.1f,
440 EXPECT_V4_NEAR(exp5, res, float_tolerance);
441}
442
443TEST(math_interp, CubicMitchellCharExactSamples)
444{
445 uchar4 res;
446 uchar4 exp1 = {72, 101, 140, 223};
448 EXPECT_EQ(int4(exp1), int4(res));
449 uchar4 exp2 = {233, 162, 99, 37};
451 EXPECT_EQ(int4(exp2), int4(res));
452}
453
454TEST(math_interp, CubicMitchellCharSamples)
455{
456 uchar4 res;
457 uchar4 exp1 = {135, 132, 130, 127};
459 image_char[0][0], image_width, image_height, 1.25f, 0.625f);
460 EXPECT_EQ(int4(exp1), int4(res));
461 uchar4 exp2 = {216, 189, 167, 143};
463 EXPECT_EQ(int4(exp2), int4(res));
464}
465
466TEST(math_interp, CubicMitchellFloatSamples)
467{
468 float4 res;
469 float4 exp1 = {134.5659f, 131.91309f, 130.17685f, 126.66989f};
471 EXPECT_V4_NEAR(exp1, res, float_tolerance);
472 float4 exp2 = {216.27115f, 189.30673f, 166.93599f, 143.31964f};
474 EXPECT_V4_NEAR(exp2, res, float_tolerance);
475}
476
477TEST(math_interp, CubicMitchellCharPartiallyOutsideImage)
478{
479 uchar4 res;
480 uchar4 exp1 = {0, 0, 0, 0};
482 EXPECT_EQ(int4(exp1), int4(res));
483 uchar4 exp2 = {88, 116, 151, 228};
485 EXPECT_EQ(int4(exp2), int4(res));
486 uchar4 exp3 = {239, 159, 89, 19};
488 EXPECT_EQ(int4(exp3), int4(res));
489}
490
491TEST(math_interp, CubicMitchellFloatPartiallyOutsideImage)
492{
493 float4 res;
494 float4 exp1 = {0, 0, 0, 0};
496 EXPECT_V4_NEAR(exp1, res, float_tolerance);
497 float4 exp2 = {87.98676f, 115.63634f, 151.13014f, 228.19823f};
499 EXPECT_V4_NEAR(exp2, res, float_tolerance);
500 float4 exp3 = {238.6136f, 158.58293f, 88.55761f, 18.53225f};
502 EXPECT_V4_NEAR(exp3, res, float_tolerance);
503}
504
505} // namespace blender
#define exp2
#define exp
float4 interpolate_nearest_border_fl(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:29
float4 interpolate_bilinear_fl(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:59
uchar4 interpolate_bilinear_wrap_byte(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:97
uchar4 interpolate_nearest_border_byte(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:25
uchar4 interpolate_bilinear_byte(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:55
uchar4 interpolate_cubic_mitchell_byte(const ImBuf *in, float u, float v)
uchar4 interpolate_cubic_bspline_byte(const ImBuf *in, float u, float v)
float4 interpolate_cubic_bspline_fl(const ImBuf *in, float u, float v)
uchar4 interpolate_bilinear_border_byte(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:76
float4 interpolate_bilinear_wrap_fl(const ImBuf *in, float u, float v)
float4 interpolate_bilinear_border_fl(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:80
BLI_INLINE void interpolate_bilinear_wrapmode_fl(const float *buffer, float *output, int width, int height, int components, float u, float v, InterpWrapMode wrap_x, InterpWrapMode wrap_y)
float4 interpolate_cubic_mitchell_fl(const float *buffer, int width, int height, float u, float v)
void interpolate_cubic_bspline_wrapmode_fl(const float *buffer, float *output, int width, int height, int components, float u, float v, InterpWrapMode wrap_u, InterpWrapMode wrap_v)
BMesh Iterators.
VecBase< uint8_t, 4 > uchar4
VecBase< int32_t, 4 > int4
VecBase< uint32_t, 4 > uint4
static constexpr float image_fl[image_height][image_width][4]
TEST(array_store, Nop)
VecBase< float, 4 > float4
unsigned char uchar
Definition BKE_text.h:58
static constexpr uchar image_char[image_height][image_width][4]
static constexpr int image_height
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
static constexpr float float_tolerance
static constexpr int image_width