Blender V5.3
BLI_math_interp.hh
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#pragma once
6
24
25#include "BLI_math_base.hh"
26#include "BLI_math_base_c.hh"
28#include "BLI_simd.hh"
29
30namespace blender {
31
32namespace math {
33
37enum class InterpWrapMode {
45 Border
46};
47
49{
50 if (u >= 0) {
51 if (u < float(size)) {
52 return int32_t(u);
53 }
54 switch (wrap) {
55 default: /* case InterpWrapMode::Extend: */
56 return size - 1;
58 return int32_t(uint32_t(u) % uint32_t(size));
60 return -1;
61 }
62 }
63 switch (wrap) {
64 default: /* case InterpWrapMode::Extend: */
65 return 0;
67 int32_t x = int32_t(uint32_t(-floorf(u)) % uint32_t(size));
68 return x ? size - x : 0;
69 }
71 return -1;
72 }
73}
74
75/* -------------------------------------------------------------------- */
76/* Nearest (point) sampling. */
77
79 float *output,
80 int width,
81 int height,
82 int components,
83 float u,
84 float v,
85 InterpWrapMode wrap_u,
86 InterpWrapMode wrap_v)
87{
88 BLI_assert(buffer);
89 int x = wrap_coord(u, width, wrap_u);
90 int y = wrap_coord(v, height, wrap_v);
91 if (x < 0 || y < 0) {
92 for (int i = 0; i < components; i++) {
93 output[i] = 0.0f;
94 }
95 return;
96 }
97
98 const float *data = buffer + (int64_t(width) * y + x) * components;
99 for (int i = 0; i < components; i++) {
100 output[i] = data[i];
101 }
102}
103
113
115 const uchar *buffer, uchar *output, int width, int height, float u, float v)
116{
117 BLI_assert(buffer);
118 int x = int(u);
119 int y = int(v);
120
121 /* Outside image? */
122 if (x < 0 || x >= width || y < 0 || y >= height) {
123 output[0] = output[1] = output[2] = output[3] = 0;
124 return;
125 }
126
127 const uchar *data = buffer + (int64_t(width) * y + x) * 4;
128 output[0] = data[0];
129 output[1] = data[1];
130 output[2] = data[2];
131 output[3] = data[3];
132}
133
135 const uchar *buffer, int width, int height, float u, float v)
136{
137 uchar4 res;
138 interpolate_nearest_border_byte(buffer, res, width, height, u, v);
139 return res;
140}
141
143 const float *buffer, float *output, int width, int height, int components, float u, float v)
144{
145 BLI_assert(buffer);
146 int x = int(u);
147 int y = int(v);
148
149 /* Outside image? */
150 if (x < 0 || x >= width || y < 0 || y >= height) {
151 for (int i = 0; i < components; i++) {
152 output[i] = 0.0f;
153 }
154 return;
155 }
156
157 const float *data = buffer + (int64_t(width) * y + x) * components;
158 for (int i = 0; i < components; i++) {
159 output[i] = data[i];
160 }
161}
162
164 const float *buffer, int width, int height, float u, float v)
165{
166 float4 res;
167 interpolate_nearest_border_fl(buffer, res, width, height, 4, u, v);
168 return res;
169}
170
180
182 const uchar *buffer, uchar *output, int width, int height, float u, float v)
183{
184 BLI_assert(buffer);
185 const int x = u > 0 ? (u < width ? int(u) : width - 1) : 0;
186 const int y = v > 0 ? (v < height ? int(v) : height - 1) : 0;
187
188 const uchar *data = buffer + (int64_t(width) * y + x) * 4;
189 output[0] = data[0];
190 output[1] = data[1];
191 output[2] = data[2];
192 output[3] = data[3];
193}
194
195[[nodiscard]] inline uchar4 interpolate_nearest_byte(
196 const uchar *buffer, int width, int height, float u, float v)
197{
198 uchar4 res;
199 interpolate_nearest_byte(buffer, res, width, height, u, v);
200 return res;
201}
202
204 const float *buffer, float *output, int width, int height, int components, float u, float v)
205{
206 BLI_assert(buffer);
207 const int x = u > 0 ? (u < width ? int(u) : width - 1) : 0;
208 const int y = v > 0 ? (v < height ? int(v) : height - 1) : 0;
209
210 const float *data = buffer + (int64_t(width) * y + x) * components;
211 for (int i = 0; i < components; i++) {
212 output[i] = data[i];
213 }
214}
215
216[[nodiscard]] inline float4 interpolate_nearest_fl(
217 const float *buffer, int width, int height, float u, float v)
218{
219 float4 res;
220 interpolate_nearest_fl(buffer, res, width, height, 4, u, v);
221 return res;
222}
223
229[[nodiscard]] inline int32_t wrap_coord(float u, int32_t size)
230{
231 if (u < 0) {
232 int32_t x = int(uint32_t(-floor(u)) % uint32_t(size));
233 return x ? size - x : 0;
234 }
235 return int(uint32_t(u) % uint32_t(size));
236}
237
241
243 const uchar *buffer, uchar *output, int width, int height, float u, float v)
244{
245 BLI_assert(buffer);
246 int x = wrap_coord(u, width);
247 int y = wrap_coord(v, height);
248 BLI_assert(x >= 0 && y >= 0 && x < width && y < height);
249
250 const uchar *data = buffer + (int64_t(width) * y + x) * 4;
251 output[0] = data[0];
252 output[1] = data[1];
253 output[2] = data[2];
254 output[3] = data[3];
255}
256
258 const uchar *buffer, int width, int height, float u, float v)
259{
260 uchar4 res;
261 interpolate_nearest_wrap_byte(buffer, res, width, height, u, v);
262 return res;
263}
264
266 const float *buffer, float *output, int width, int height, int components, float u, float v)
267{
268 BLI_assert(buffer);
269 int x = wrap_coord(u, width);
270 int y = wrap_coord(v, height);
271 BLI_assert(x >= 0 && y >= 0 && x < width && y < height);
272
273 const float *data = buffer + (int64_t(width) * y + x) * components;
274 for (int i = 0; i < components; i++) {
275 output[i] = data[i];
276 }
277}
278
280 const float *buffer, int width, int height, float u, float v)
281{
282 float4 res;
283 interpolate_nearest_wrap_fl(buffer, res, width, height, 4, u, v);
284 return res;
285}
286
287/* -------------------------------------------------------------------- */
288/* Bilinear sampling. */
289
291 float *output,
292 int width,
293 int height,
294 int components,
295 float u,
296 float v,
297 InterpWrapMode wrap_x,
298 InterpWrapMode wrap_y)
299{
300 BLI_assert(buffer && output);
301 BLI_assert(components > 0 && components <= 4);
302
303 int x1 = wrap_coord(u, width, wrap_x);
304 int x2 = wrap_coord(u + 1, width, wrap_x);
305 int y1 = wrap_coord(v, height, wrap_y);
306 int y2 = wrap_coord(v + 1, height, wrap_y);
307
308 const float *row1, *row2, *row3, *row4;
309 const float empty[4] = {0.0f, 0.0f, 0.0f, 0.0f};
310
311 row1 = buffer + (int64_t(width) * y1 + x1) * components;
312 row2 = buffer + (int64_t(width) * y2 + x1) * components;
313 row3 = buffer + (int64_t(width) * y1 + x2) * components;
314 row4 = buffer + (int64_t(width) * y2 + x2) * components;
315
316 if (wrap_x == InterpWrapMode::Border) {
317 if (x1 < 0) {
318 row1 = empty;
319 row2 = empty;
320 }
321 if (x2 < 0) {
322 row3 = empty;
323 row4 = empty;
324 }
325 }
326 if (wrap_y == InterpWrapMode::Border) {
327 if (y1 < 0) {
328 row1 = empty;
329 row3 = empty;
330 }
331 if (y2 < 0) {
332 row2 = empty;
333 row4 = empty;
334 }
335 }
336
337 /* Finally, do interpolation. */
338 float a = u - floorf(u);
339 float b = v - floorf(v);
340 float a_b = a * b;
341 float ma_b = (1.0f - a) * b;
342 float a_mb = a * (1.0f - b);
343 float ma_mb = (1.0f - a) * (1.0f - b);
344
345 if (components == 1) {
346 output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
347 }
348 else if (components == 2) {
349 output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
350 output[1] = ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1];
351 }
352 else if (components == 3) {
353 output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
354 output[1] = ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1];
355 output[2] = ma_mb * row1[2] + a_mb * row3[2] + ma_b * row2[2] + a_b * row4[2];
356 }
357 else {
358#if BLI_HAVE_SSE2
359 __m128 rgba1 = _mm_loadu_ps(row1);
360 __m128 rgba2 = _mm_loadu_ps(row2);
361 __m128 rgba3 = _mm_loadu_ps(row3);
362 __m128 rgba4 = _mm_loadu_ps(row4);
363 rgba1 = _mm_mul_ps(_mm_set1_ps(ma_mb), rgba1);
364 rgba2 = _mm_mul_ps(_mm_set1_ps(ma_b), rgba2);
365 rgba3 = _mm_mul_ps(_mm_set1_ps(a_mb), rgba3);
366 rgba4 = _mm_mul_ps(_mm_set1_ps(a_b), rgba4);
367 __m128 rgba13 = _mm_add_ps(rgba1, rgba3);
368 __m128 rgba24 = _mm_add_ps(rgba2, rgba4);
369 __m128 rgba = _mm_add_ps(rgba13, rgba24);
370 _mm_storeu_ps(output, rgba);
371#else
372 output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
373 output[1] = ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1];
374 output[2] = ma_mb * row1[2] + a_mb * row3[2] + ma_b * row2[2] + a_b * row4[2];
375 output[3] = ma_mb * row1[3] + a_mb * row3[3] + ma_b * row2[3] + a_b * row4[3];
376#endif
377 }
378}
379
390
392 const uchar *buffer, int width, int height, float u, float v);
393
395 const float *buffer, int width, int height, float u, float v);
396
398 const float *buffer, float *output, int width, int height, int components, float u, float v);
399
410
412 const uchar *buffer, int width, int height, float u, float v);
413
414[[nodiscard]] float4 interpolate_bilinear_fl(
415 const float *buffer, int width, int height, float u, float v);
416
418 const float *buffer, float *output, int width, int height, int components, float u, float v);
419
424
426 const uchar *buffer, int width, int height, float u, float v);
427
429 const float *buffer, int width, int height, float u, float v);
430
431/* -------------------------------------------------------------------- */
432/* Cubic sampling. */
433
445
447 const uchar *buffer, int width, int height, float u, float v);
448
450 const float *buffer, int width, int height, float u, float v);
451
453 const float *buffer, float *output, int width, int height, int components, float u, float v);
454
455void interpolate_cubic_bspline_wrapmode_fl(const float *buffer,
456 float *output,
457 int width,
458 int height,
459 int components,
460 float u,
461 float v,
462 InterpWrapMode wrap_u,
463 InterpWrapMode wrap_v);
464
476
478 const uchar *buffer, int width, int height, float u, float v);
479
481 const float *buffer, int width, int height, float u, float v);
482
484 const float *buffer, float *output, int width, int height, int components, float u, float v);
485
486} // namespace math
487
488/* -------------------------------------------------------------------- */
489/* EWA sampling. */
490
491#define EWA_MAXIDX 255
492extern const float EWA_WTS[EWA_MAXIDX + 1];
493
494using ewa_filter_read_pixel_cb = void (*)(void *userdata, int x, int y, float result[4]);
495
497 float A, float B, float C, float F, float *a, float *b, float *th, float *ecc);
498
503void BLI_ewa_filter(int width,
504 int height,
505 bool intpol,
506 bool use_alpha,
507 const float uv[2],
508 const float du[2],
509 const float dv[2],
510 ewa_filter_read_pixel_cb read_pixel_cb,
511 void *userdata,
512 float result[4],
513 bool clip = true);
514
515} // namespace blender
#define EWA_MAXIDX
#define C
Definition RandGen.cpp:29
#define BLI_INLINE
#define A
#define output
#define B
#define F
BLI_INLINE Texture * wrap(MTLTexture *texture)
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_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
void interpolate_nearest_wrap_fl(const float *buffer, float *output, int width, int height, int components, float u, float v)
void interpolate_nearest_fl(const float *buffer, float *output, int width, int height, int components, float u, float v)
T floor(const T &a)
void interpolate_nearest_border_fl(const float *buffer, float *output, int width, int height, int components, float u, float v)
BLI_INLINE int32_t wrap_coord(float u, int32_t size, InterpWrapMode wrap)
void interpolate_nearest_byte(const uchar *buffer, uchar *output, int width, int height, float u, float v)
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)
BLI_INLINE void interpolate_nearest_wrapmode_fl(const float *buffer, float *output, int width, int height, int components, float u, float v, InterpWrapMode wrap_u, InterpWrapMode wrap_v)
float4 interpolate_cubic_mitchell_fl(const float *buffer, int width, int height, float u, float v)
void interpolate_nearest_wrap_byte(const uchar *buffer, uchar *output, int width, int height, float u, float v)
void interpolate_nearest_border_byte(const uchar *buffer, uchar *output, 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.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BLI_ewa_filter(int width, int height, bool intpol, bool use_alpha, const float uv[2], const float du[2], const float dv[2], ewa_filter_read_pixel_cb read_pixel_cb, void *userdata, float result[4], bool clip=true)
VecBase< uint8_t, 4 > uchar4
__int64 int64_t
Definition BKE_text.h:90
void(*)(void *userdata, int x, int y, float result[4]) ewa_filter_read_pixel_cb
ATTR_WARN_UNUSED_RESULT const BMVert * v
VecBase< float, 4 > float4
unsigned int uint32_t
Definition BKE_text.h:81
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
const float EWA_WTS[EWA_MAXIDX+1]
signed int int32_t
Definition BKE_text.h:78
void BLI_ewa_imp2radangle(float A, float B, float C, float F, float *a, float *b, float *th, float *ecc)
#define floorf