Blender V5.3
IMB_scaling_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "testing/testing.h"
6
8
9#include "IMB_imbuf.hh"
10
11#include "BKE_gtest_base.hh"
12
13namespace blender::imbuf::tests {
14
16
18{
20 uchar4 *col = reinterpret_cast<uchar4 *>(img->byte_data_for_write());
21
22 /* Source pixels are spelled out in 2x2 blocks below:
23 * nearest filter results in corner pixel from each block, bilinear
24 * is average of each block. */
25 col[0] = uchar4(0, 0, 0, 255);
26 col[1] = uchar4(255, 0, 0, 255);
27 col[6] = uchar4(255, 255, 0, 255);
28 col[7] = uchar4(255, 255, 255, 255);
29
30 col[2] = uchar4(133, 55, 31, 13);
31 col[3] = uchar4(133, 55, 31, 15);
32 col[8] = uchar4(133, 55, 31, 17);
33 col[9] = uchar4(133, 55, 31, 19);
34
35 col[4] = uchar4(50, 200, 0, 255);
36 col[5] = uchar4(55, 0, 32, 254);
37 col[10] = uchar4(56, 0, 64, 253);
38 col[11] = uchar4(57, 0, 96, 252);
39
40 return img;
41}
42
43static ImBuf *create_6x2_test_image_fl(int channels)
44{
46 img->channels = channels;
47 float *col = img->float_data_for_write();
48
49 for (int y = 0; y < img->y; y++) {
50 for (int x = 0; x < img->x; x++) {
51 for (int ch = 0; ch < channels; ch++) {
52 *col = x * 1.25f + y * 0.5f + ch * 0.125f;
53 col++;
54 }
55 }
56 }
57 return img;
58}
59
60static ImBuf *scale_2x_smaller(bool nearest, bool threaded, int float_channels = 0)
61{
62 ImBuf *img = float_channels > 0 ? create_6x2_test_image_fl(float_channels) :
64 int ww = 3, hh = 1;
65 if (threaded) {
66 IMB_scale(img, ww, hh, IMBScaleFilter::Bilinear, true);
67 }
68 else if (nearest) {
69 IMB_scale(img, ww, hh, IMBScaleFilter::Nearest, false);
70 }
71 else {
72 IMB_scale(img, ww, hh, IMBScaleFilter::Box, false);
73 }
74 return img;
75}
76
77static ImBuf *scale_to_1x1(bool nearest, bool threaded, int float_channels = 0)
78{
79 ImBuf *img = float_channels > 0 ? create_6x2_test_image_fl(float_channels) :
81 int ww = 1, hh = 1;
82 if (threaded) {
83 IMB_scale(img, ww, hh, IMBScaleFilter::Bilinear, true);
84 }
85 else if (nearest) {
86 IMB_scale(img, ww, hh, IMBScaleFilter::Nearest, false);
87 }
88 else {
89 IMB_scale(img, ww, hh, IMBScaleFilter::Box, false);
90 }
91 return img;
92}
93
94static ImBuf *scale_fractional_larger(bool nearest, bool threaded, int float_channels = 0)
95{
96 ImBuf *img = float_channels > 0 ? create_6x2_test_image_fl(float_channels) :
98 int ww = 9, hh = 7;
99 if (threaded) {
100 IMB_scale(img, ww, hh, IMBScaleFilter::Bilinear, true);
101 }
102 else if (nearest) {
103 IMB_scale(img, ww, hh, IMBScaleFilter::Nearest, false);
104 }
105 else {
106 IMB_scale(img, ww, hh, IMBScaleFilter::Box, false);
107 }
108 return img;
109}
110
111TEST_F(ImBufScalingTest, nearest_2x_smaller)
112{
113 ImBuf *res = scale_2x_smaller(true, false);
114 const uchar4 *got = reinterpret_cast<const uchar4 *>(res->byte_data());
115 EXPECT_EQ(uint4(got[0]), uint4(0, 0, 0, 255));
116 EXPECT_EQ(uint4(got[1]), uint4(133, 55, 31, 13));
117 EXPECT_EQ(uint4(got[2]), uint4(50, 200, 0, 255));
118 IMB_freeImBuf(res);
119}
120
121TEST_F(ImBufScalingTest, threaded_2x_smaller)
122{
123 ImBuf *res = scale_2x_smaller(false, true);
124 const uchar4 *got = reinterpret_cast<const uchar4 *>(res->byte_data());
125 EXPECT_EQ(uint4(got[0]), uint4(191, 128, 64, 255));
126 EXPECT_EQ(uint4(got[1]), uint4(133, 55, 31, 16));
127 EXPECT_EQ(uint4(got[2]), uint4(55, 50, 48, 254));
128 IMB_freeImBuf(res);
129}
130
131TEST_F(ImBufScalingTest, bilinear_2x_smaller)
132{
133 ImBuf *res = scale_2x_smaller(false, false);
134 const uchar4 *got = reinterpret_cast<const uchar4 *>(res->byte_data());
135 /* NOTE: #IMB_transform results in (191, 128, 64, 255), <same>,
136 * (55, 50, 48, 254) i.e. different rounding. */
137 EXPECT_EQ(uint4(got[0]), uint4(191, 127, 63, 255));
138 EXPECT_EQ(uint4(got[1]), uint4(133, 55, 31, 16));
139 EXPECT_EQ(uint4(got[2]), uint4(55, 50, 48, 253));
140 IMB_freeImBuf(res);
141}
142
143TEST_F(ImBufScalingTest, nearest_to_1x1)
144{
145 ImBuf *res = scale_to_1x1(true, false);
146 const uchar4 *got = reinterpret_cast<const uchar4 *>(res->byte_data());
147 EXPECT_EQ(uint4(got[0]), uint4(0, 0, 0, 255));
148 IMB_freeImBuf(res);
149}
150
151TEST_F(ImBufScalingTest, threaded_to_1x1)
152{
153 ImBuf *res = scale_to_1x1(false, true);
154 const uchar4 *got = reinterpret_cast<const uchar4 *>(res->byte_data());
155 EXPECT_EQ(uint4(got[0]), uint4(133, 55, 31, 16));
156 IMB_freeImBuf(res);
157}
158
159TEST_F(ImBufScalingTest, bilinear_to_1x1)
160{
161 ImBuf *res = scale_to_1x1(false, false);
162 const uchar4 *got = reinterpret_cast<const uchar4 *>(res->byte_data());
163 EXPECT_EQ(uint4(got[0]), uint4(126, 78, 47, 174));
164 IMB_freeImBuf(res);
165}
166
167TEST_F(ImBufScalingTest, nearest_fractional_larger)
168{
169 ImBuf *res = scale_fractional_larger(true, false);
170 const uchar4 *got = reinterpret_cast<const uchar4 *>(res->byte_data());
171 EXPECT_EQ(uint4(got[0 + 0 * res->x]), uint4(0, 0, 0, 255));
172 EXPECT_EQ(uint4(got[1 + 0 * res->x]), uint4(0, 0, 0, 255));
173 EXPECT_EQ(uint4(got[7 + 0 * res->x]), uint4(50, 200, 0, 255));
174 EXPECT_EQ(uint4(got[2 + 2 * res->x]), uint4(255, 0, 0, 255));
175 EXPECT_EQ(uint4(got[3 + 2 * res->x]), uint4(133, 55, 31, 13));
176 EXPECT_EQ(uint4(got[8 + 6 * res->x]), uint4(57, 0, 96, 252));
177 IMB_freeImBuf(res);
178}
179
180TEST_F(ImBufScalingTest, bilinear_fractional_larger)
181{
182 ImBuf *res = scale_fractional_larger(false, false);
183 const uchar4 *got = reinterpret_cast<const uchar4 *>(res->byte_data());
184 EXPECT_EQ(uint4(got[0 + 0 * res->x]), uint4(0, 0, 0, 255));
185 EXPECT_EQ(uint4(got[1 + 0 * res->x]), uint4(127, 0, 0, 255));
186 EXPECT_EQ(uint4(got[7 + 0 * res->x]), uint4(52, 100, 16, 255));
187 EXPECT_EQ(uint4(got[2 + 2 * res->x]), uint4(235, 55, 51, 215));
188 EXPECT_EQ(uint4(got[3 + 2 * res->x]), uint4(153, 55, 35, 54));
189 EXPECT_EQ(uint4(got[8 + 5 * res->x]), uint4(57, 0, 91, 252));
190 EXPECT_EQ(uint4(got[0 + 6 * res->x]), uint4(164, 164, 0, 255));
191 EXPECT_EQ(uint4(got[7 + 6 * res->x]), uint4(55, 36, 57, 254));
192 EXPECT_EQ(uint4(got[8 + 6 * res->x]), uint4(56, 0, 73, 253));
193 IMB_freeImBuf(res);
194}
195
196static constexpr float EPS = 0.0001f;
197
198TEST_F(ImBufScalingTest, nearest_2x_smaller_fl1)
199{
200 ImBuf *res = scale_2x_smaller(true, false, 1);
201 const float *got = res->float_data();
202 EXPECT_NEAR(got[0], 0.0f, EPS);
203 EXPECT_NEAR(got[1], 2.5f, EPS);
204 EXPECT_NEAR(got[2], 5.0f, EPS);
205 IMB_freeImBuf(res);
206}
207
208TEST_F(ImBufScalingTest, nearest_2x_smaller_fl2)
209{
210 ImBuf *res = scale_2x_smaller(true, false, 2);
211 const float2 *got = reinterpret_cast<const float2 *>(res->float_data());
212 EXPECT_V2_NEAR(got[0], float2(0.0f, 0.125f), EPS);
213 EXPECT_V2_NEAR(got[1], float2(2.5f, 2.625f), EPS);
214 EXPECT_V2_NEAR(got[2], float2(5.0f, 5.125f), EPS);
215 IMB_freeImBuf(res);
216}
217
218TEST_F(ImBufScalingTest, nearest_2x_smaller_fl3)
219{
220 ImBuf *res = scale_2x_smaller(true, false, 3);
221 const float3 *got = reinterpret_cast<const float3 *>(res->float_data());
222 EXPECT_V3_NEAR(got[0], float3(0.0f, 0.125f, 0.25f), EPS);
223 EXPECT_V3_NEAR(got[1], float3(2.5f, 2.625f, 2.75f), EPS);
224 EXPECT_V3_NEAR(got[2], float3(5.0f, 5.125f, 5.25f), EPS);
225 IMB_freeImBuf(res);
226}
227
228TEST_F(ImBufScalingTest, nearest_2x_smaller_fl4)
229{
230 ImBuf *res = scale_2x_smaller(true, false, 4);
231 const float4 *got = reinterpret_cast<const float4 *>(res->float_data());
232 EXPECT_V4_NEAR(got[0], float4(0.0f, 0.125f, 0.25f, 0.375f), EPS);
233 EXPECT_V4_NEAR(got[1], float4(2.5f, 2.625f, 2.75f, 2.875f), EPS);
234 EXPECT_V4_NEAR(got[2], float4(5.0f, 5.125f, 5.25f, 5.375f), EPS);
235 IMB_freeImBuf(res);
236}
237
238TEST_F(ImBufScalingTest, nearest_to_1x1_fl3)
239{
240 ImBuf *res = scale_to_1x1(true, false, 3);
241 const float3 *got = reinterpret_cast<const float3 *>(res->float_data());
242 EXPECT_V3_NEAR(got[0], float3(0, 0.125f, 0.25f), EPS);
243 IMB_freeImBuf(res);
244}
245
246TEST_F(ImBufScalingTest, threaded_to_1x1_fl3)
247{
248 ImBuf *res = scale_to_1x1(false, true, 3);
249 const float3 *got = reinterpret_cast<const float3 *>(res->float_data());
250 EXPECT_V3_NEAR(got[0], float3(3.375f, 3.5f, 3.625f), EPS);
251 IMB_freeImBuf(res);
252}
253
254TEST_F(ImBufScalingTest, bilinear_to_1x1_fl3)
255{
256 ImBuf *res = scale_to_1x1(false, false, 3);
257 const float3 *got = reinterpret_cast<const float3 *>(res->float_data());
258 EXPECT_V3_NEAR(got[0], float3(3.36853f, 3.49353f, 3.61853f), EPS);
259 IMB_freeImBuf(res);
260}
261
262TEST_F(ImBufScalingTest, bilinear_2x_smaller_fl3)
263{
264 ImBuf *res = scale_2x_smaller(false, false, 3);
265 const float3 *got = reinterpret_cast<const float3 *>(res->float_data());
266 EXPECT_V3_NEAR(got[0], float3(0.87270f, 0.99770f, 1.12270f), EPS);
267 EXPECT_V3_NEAR(got[1], float3(3.36853f, 3.49353f, 3.61853f), EPS);
268 EXPECT_V3_NEAR(got[2], float3(5.86435f, 5.98935f, 6.11435f), EPS);
269 IMB_freeImBuf(res);
270}
271
272TEST_F(ImBufScalingTest, bilinear_2x_smaller_fl4)
273{
274 ImBuf *res = scale_2x_smaller(false, false, 4);
275 const float4 *got = reinterpret_cast<const float4 *>(res->float_data());
276 EXPECT_V4_NEAR(got[0], float4(0.87270f, 0.99770f, 1.12270f, 1.24770f), EPS);
277 EXPECT_V4_NEAR(got[1], float4(3.36853f, 3.49353f, 3.61853f, 3.74353f), EPS);
278 EXPECT_V4_NEAR(got[2], float4(5.86435f, 5.98935f, 6.11435f, 6.23935f), EPS);
279 IMB_freeImBuf(res);
280}
281
282TEST_F(ImBufScalingTest, threaded_2x_smaller_fl3)
283{
284 ImBuf *res = scale_2x_smaller(false, true, 3);
285 const float3 *got = reinterpret_cast<const float3 *>(res->float_data());
286 EXPECT_V3_NEAR(got[0], float3(0.875f, 1.0f, 1.125f), EPS);
287 EXPECT_V3_NEAR(got[1], float3(3.375f, 3.5f, 3.625f), EPS);
288 EXPECT_V3_NEAR(got[2], float3(5.875f, 6.0f, 6.125f), EPS);
289 IMB_freeImBuf(res);
290}
291
292TEST_F(ImBufScalingTest, threaded_2x_smaller_fl4)
293{
294 ImBuf *res = scale_2x_smaller(false, true, 4);
295 const float4 *got = reinterpret_cast<const float4 *>(res->float_data());
296 EXPECT_V4_NEAR(got[0], float4(0.875f, 1.0f, 1.125f, 1.25f), EPS);
297 EXPECT_V4_NEAR(got[1], float4(3.375f, 3.5f, 3.625f, 3.75f), EPS);
298 EXPECT_V4_NEAR(got[2], float4(5.875f, 6.0f, 6.125f, 6.25f), EPS);
299 IMB_freeImBuf(res);
300}
301
302} // namespace blender::imbuf::tests
#define EXPECT_NEAR(result, expect, threshold)
static constexpr float EPS
static ImBuf * create_6x2_test_image_fl(int channels)
TEST_F(ColorManagementConfigSwitchTest, colorspace_pointers_survive_switch)
static ImBuf * create_6x2_test_image()
static ImBuf * scale_fractional_larger(bool nearest, bool threaded, int float_channels=0)
static ImBuf * scale_2x_smaller(bool nearest, bool threaded, int float_channels=0)
static ImBuf * scale_to_1x1(bool nearest, bool threaded, int float_channels=0)
VecBase< uint8_t, 4 > uchar4
VecBase< uint32_t, 4 > uint4
VecBase< float, 4 > float4
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< float, 2 > float2
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
VecBase< float, 3 > float3
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:467
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
float * float_data_for_write()
Definition allocimbuf.cc:79