Blender V5.3
IMB_transform_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
7#include "BLI_color_types.hh"
8#include "BLI_math_matrix.hh"
10
11#include "IMB_imbuf.hh"
12
13#include "BKE_gtest_base.hh"
14
15namespace blender::imbuf::tests {
16
18
20{
22 ColorTheme4b *col = reinterpret_cast<ColorTheme4b *>(img->byte_data_for_write());
23
24 /* Source pixels are spelled out in 2x2 blocks below:
25 * nearest filter results in corner pixel from each block, bilinear
26 * is average of each block. */
27 col[0] = ColorTheme4b(0, 0, 0, 255);
28 col[1] = ColorTheme4b(255, 0, 0, 255);
29 col[6] = ColorTheme4b(255, 255, 0, 255);
30 col[7] = ColorTheme4b(255, 255, 255, 255);
31
32 col[2] = ColorTheme4b(133, 55, 31, 13);
33 col[3] = ColorTheme4b(133, 55, 31, 15);
34 col[8] = ColorTheme4b(133, 55, 31, 17);
35 col[9] = ColorTheme4b(133, 55, 31, 19);
36
37 col[4] = ColorTheme4b(50, 200, 0, 255);
38 col[5] = ColorTheme4b(55, 0, 32, 254);
39 col[10] = ColorTheme4b(56, 0, 64, 253);
40 col[11] = ColorTheme4b(57, 0, 96, 252);
41
42 return img;
43}
44
54
56{
59 float3x3 matrix = math::from_scale<float3x3>(float3(6.0f / 9.0f, 2.0f / 7.0f, 1.0f));
60 IMB_transform(src, dst, IMB_TRANSFORM_MODE_REGULAR, filter, matrix, nullptr);
61 IMB_freeImBuf(src);
62 return dst;
63}
64
65TEST_F(ImBufTransformTest, nearest_2x_smaller)
66{
68 const ColorTheme4b *got = reinterpret_cast<ColorTheme4b *>(res->byte_data_for_write());
69 EXPECT_EQ(got[0], ColorTheme4b(255, 255, 255, 255));
70 EXPECT_EQ(got[1], ColorTheme4b(133, 55, 31, 19));
71 EXPECT_EQ(got[2], ColorTheme4b(57, 0, 96, 252));
72 IMB_freeImBuf(res);
73}
74
75TEST_F(ImBufTransformTest, box_2x_smaller)
76{
78 const ColorTheme4b *got = reinterpret_cast<const ColorTheme4b *>(res->byte_data());
79 /* At 2x reduction should be same as bilinear, save for some rounding errors. */
80 EXPECT_EQ(got[0], ColorTheme4b(191, 128, 64, 255));
81 EXPECT_EQ(got[1], ColorTheme4b(133, 55, 31, 16));
82 EXPECT_EQ(got[2], ColorTheme4b(54, 50, 48, 254));
83 IMB_freeImBuf(res);
84}
85
86TEST_F(ImBufTransformTest, bilinear_2x_smaller)
87{
89 const ColorTheme4b *got = reinterpret_cast<const ColorTheme4b *>(res->byte_data());
90 EXPECT_EQ(got[0], ColorTheme4b(191, 128, 64, 255));
91 EXPECT_EQ(got[1], ColorTheme4b(133, 55, 31, 16));
92 EXPECT_EQ(got[2], ColorTheme4b(55, 50, 48, 254));
93 IMB_freeImBuf(res);
94}
95
96TEST_F(ImBufTransformTest, cubic_bspline_2x_smaller)
97{
99 const ColorTheme4b *got = reinterpret_cast<const ColorTheme4b *>(res->byte_data());
100 EXPECT_EQ(got[0], ColorTheme4b(189, 126, 62, 250));
101 EXPECT_EQ(got[1], ColorTheme4b(134, 57, 33, 26));
102 EXPECT_EQ(got[2], ColorTheme4b(56, 49, 48, 249));
103 IMB_freeImBuf(res);
104}
105
106TEST_F(ImBufTransformTest, cubic_mitchell_2x_smaller)
107{
109 const ColorTheme4b *got = reinterpret_cast<const ColorTheme4b *>(res->byte_data());
110 EXPECT_EQ(got[0], ColorTheme4b(195, 130, 67, 255));
111 EXPECT_EQ(got[1], ColorTheme4b(132, 51, 28, 0));
112 EXPECT_EQ(got[2], ColorTheme4b(52, 52, 48, 255));
113 IMB_freeImBuf(res);
114}
115
116TEST_F(ImBufTransformTest, cubic_mitchell_fractional_larger)
117{
119 const ColorTheme4b *got = reinterpret_cast<const ColorTheme4b *>(res->byte_data());
120 EXPECT_EQ(got[0 + 0 * res->x], ColorTheme4b(0, 0, 0, 255));
121 EXPECT_EQ(got[1 + 0 * res->x], ColorTheme4b(127, 0, 0, 255));
122 EXPECT_EQ(got[7 + 0 * res->x], ColorTheme4b(49, 109, 13, 255));
123 EXPECT_EQ(got[2 + 2 * res->x], ColorTheme4b(236, 53, 50, 215));
124 EXPECT_EQ(got[3 + 2 * res->x], ColorTheme4b(155, 55, 35, 54));
125 EXPECT_EQ(got[8 + 6 * res->x], ColorTheme4b(57, 0, 98, 252));
126 IMB_freeImBuf(res);
127}
128
129TEST_F(ImBufTransformTest, nearest_very_large_scale)
130{
131 /* Create 511x1 black image, with three middle pixels being red/green/blue. */
133 ColorTheme4b col_r = ColorTheme4b(255, 0, 0, 255);
134 ColorTheme4b col_g = ColorTheme4b(0, 255, 0, 255);
135 ColorTheme4b col_b = ColorTheme4b(0, 0, 255, 255);
136 ColorTheme4b col_0 = ColorTheme4b(0, 0, 0, 0);
137 ColorTheme4b *src_col = reinterpret_cast<ColorTheme4b *>(src->byte_data_for_write());
138 src_col[254] = col_r;
139 src_col[255] = col_g;
140 src_col[256] = col_b;
141
142 /* Create 3841x1 image, and scale the input image so that the three middle
143 * pixels cover almost all of it, except the rightmost pixel. */
146 float2(254, 0), 0.0f, float2(3.0f / 3840.0f, 1));
148
149 /* Check result: leftmost red, middle green, two rightmost pixels blue and black.
150 * If the transform code internally does not have enough precision while stepping
151 * through the scan-line, the rightmost side will not come out correctly. */
152 const ColorTheme4b *got = reinterpret_cast<const ColorTheme4b *>(res->byte_data());
153 EXPECT_EQ(got[0], col_r);
154 EXPECT_EQ(got[res->x / 2], col_g);
155 EXPECT_EQ(got[res->x - 2], col_b);
156 EXPECT_EQ(got[res->x - 1], col_0);
157 IMB_freeImBuf(src);
158 IMB_freeImBuf(res);
159}
160
161} // namespace blender::imbuf::tests
TEST_F(ColorManagementConfigSwitchTest, colorspace_pointers_survive_switch)
static ImBuf * create_6x2_test_image()
static ImBuf * transform_2x_smaller(eIMBInterpolationFilterMode filter)
static ImBuf * transform_fractional_larger(eIMBInterpolationFilterMode filter)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
MatT from_loc_rot_scale(const typename MatT::loc_type &location, const RotationT &rotation, const VecBase< typename MatT::base_type, ScaleDim > &scale)
ColorTheme4< uint8_t > ColorTheme4b
void IMB_transform(const ImBuf *src, ImBuf *dst, eIMBTransformMode mode, eIMBInterpolationFilterMode filter, const float3x3 &transform_matrix, const rctf *src_crop)
Transform source image buffer onto destination image buffer using a transform matrix.
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< float, 2 > float2
eIMBInterpolationFilterMode
Definition IMB_imbuf.hh:337
@ IMB_FILTER_NEAREST
Definition IMB_imbuf.hh:338
@ IMB_FILTER_BOX
Definition IMB_imbuf.hh:342
@ IMB_FILTER_CUBIC_BSPLINE
Definition IMB_imbuf.hh:340
@ IMB_FILTER_CUBIC_MITCHELL
Definition IMB_imbuf.hh:341
@ IMB_FILTER_BILINEAR
Definition IMB_imbuf.hh:339
MatBase< float, 3, 3 > float3x3
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
@ IMB_TRANSFORM_MODE_REGULAR
Do not crop or repeat.
Definition IMB_imbuf.hh:578
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60