Blender V5.3
math_interp.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
8
9#include <cmath>
10#include <cstring>
11
12#include "BLI_math_base.hh"
13#include "BLI_math_base_c.hh"
14#include "BLI_math_interp.hh"
15#include "BLI_math_vector_c.hh"
17#include "BLI_simd.hh"
18
19#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
20
21namespace blender {
22
23namespace math {
24
29
30/* Calculate cubic filter coefficients, for samples at -1,0,+1,+2.
31 * f is 0..1 offset from texel center in pixel space. */
32template<enum eCubicFilter filter> static float4 cubic_filter_coefficients(float f)
33{
34 float f2 = f * f;
35 float f3 = f2 * f;
36
37 if constexpr (filter == eCubicFilter::BSpline) {
38 /* Cubic B-Spline (Mitchell-Netravali filter with B=1, C=0 parameters). */
39 float w3 = f3 * (1.0f / 6.0f);
40 float w0 = -w3 + f2 * 0.5f - f * 0.5f + 1.0f / 6.0f;
41 float w1 = f3 * 0.5f - f2 * 1.0f + 2.0f / 3.0f;
42 float w2 = 1.0f - w0 - w1 - w3;
43 return float4(w0, w1, w2, w3);
44 }
45 else if constexpr (filter == eCubicFilter::Mitchell) {
46 /* Cubic Mitchell-Netravali filter with B=1/3, C=1/3 parameters. */
47 float w0 = -7.0f / 18.0f * f3 + 5.0f / 6.0f * f2 - 0.5f * f + 1.0f / 18.0f;
48 float w1 = 7.0f / 6.0f * f3 - 2.0f * f2 + 8.0f / 9.0f;
49 float w2 = -7.0f / 6.0f * f3 + 3.0f / 2.0f * f2 + 0.5f * f + 1.0f / 18.0f;
50 float w3 = 7.0f / 18.0f * f3 - 1.0f / 3.0f * f2;
51 return float4(w0, w1, w2, w3);
52 }
53}
54
55#if BLI_HAVE_SSE4
56template<eCubicFilter filter>
57BLI_INLINE void bicubic_interpolation_uchar_simd(
58 const uchar *src_buffer, uchar *output, int width, int height, float u, float v)
59{
60 __m128 uv = _mm_set_ps(0, 0, v, u);
61 __m128 uv_floor = _mm_floor_ps(uv);
62 __m128i i_uv = _mm_cvttps_epi32(uv_floor);
63
64 __m128 frac_uv = _mm_sub_ps(uv, uv_floor);
65
66 /* Calculate pixel weights. */
67 float4 wx = cubic_filter_coefficients<filter>(_mm_cvtss_f32(frac_uv));
69 _mm_cvtss_f32(_mm_shuffle_ps(frac_uv, frac_uv, 1)));
70
71 /* Read 4x4 source pixels and blend them. */
72 __m128 out = _mm_setzero_ps();
73 int iu = _mm_cvtsi128_si32(i_uv);
74 int iv = _mm_cvtsi128_si32(_mm_shuffle_epi32(i_uv, 1));
75
76 for (int n = 0; n < 4; n++) {
77 int y1 = iv + n - 1;
78 CLAMP(y1, 0, height - 1);
79 for (int m = 0; m < 4; m++) {
80
81 int x1 = iu + m - 1;
82 CLAMP(x1, 0, width - 1);
83 float w = wx[m] * wy[n];
84
85 const uchar *data = src_buffer + (width * y1 + x1) * 4;
86 /* Load 4 bytes and expand into 4-lane SIMD. */
87 __m128i sample_i = _mm_castps_si128(_mm_load_ss(reinterpret_cast<const float *>(data)));
88 sample_i = _mm_unpacklo_epi8(sample_i, _mm_setzero_si128());
89 sample_i = _mm_unpacklo_epi16(sample_i, _mm_setzero_si128());
90
91 /* Accumulate into out with weight. */
92 out = _mm_add_ps(out, _mm_mul_ps(_mm_cvtepi32_ps(sample_i), _mm_set1_ps(w)));
93 }
94 }
95
96 /* Pack and write to destination: pack to 16 bit signed, then to 8 bit
97 * unsigned, then write resulting 32-bit value. This will clamp
98 * out of range values too. */
99 out = _mm_add_ps(out, _mm_set1_ps(0.5f));
100 __m128i rgba32 = _mm_cvttps_epi32(out);
101 __m128i rgba16 = _mm_packs_epi32(rgba32, _mm_setzero_si128());
102 __m128i rgba8 = _mm_packus_epi16(rgba16, _mm_setzero_si128());
103 _mm_store_ss(reinterpret_cast<float *>(output), _mm_castsi128_ps(rgba8));
104}
105#endif /* BLI_HAVE_SSE4 */
106
107template<typename T, eCubicFilter filter>
108BLI_INLINE void bicubic_interpolation(const T *src_buffer,
109 T *output,
110 int width,
111 int height,
112 int components,
113 float u,
114 float v,
115 InterpWrapMode wrap_u,
116 InterpWrapMode wrap_v)
117{
118 BLI_assert(src_buffer && output);
119 BLI_assert(components > 0 && components <= 4);
120
121 /* GCC 14.x and newer can't reliably detect that `components` is never over 4. */
122#if (defined(__GNUC__) && (__GNUC__ >= 14) && !defined(__clang__))
123 [[assume(components <= 4)]];
124#endif
125
126#if BLI_HAVE_SSE4
127 if constexpr (std::is_same_v<T, uchar>) {
128 if (components == 4 && wrap_u == InterpWrapMode::Extend && wrap_v == InterpWrapMode::Extend) {
129 bicubic_interpolation_uchar_simd<filter>(src_buffer, output, width, height, u, v);
130 return;
131 }
132 }
133#endif
134
135 float4 out{0.0f};
136
137 /* Calculate pixel weights. */
140
141 /* Read 4x4 source pixels and blend them. */
142 for (int n = 0; n < 4; n++) {
143 int y1 = wrap_coord(v - 1 + float(n), height, wrap_v);
144 if (y1 < 0) {
145 continue;
146 }
147 for (int m = 0; m < 4; m++) {
148 int x1 = wrap_coord(u - 1 + float(m), width, wrap_u);
149 if (x1 < 0) {
150 continue;
151 }
152 float w = wx[m] * wy[n];
153
154 const T *data = src_buffer + (width * y1 + x1) * components;
155
156 if (components == 1) {
157 out[0] += data[0] * w;
158 }
159 else if (components == 2) {
160 out[0] += data[0] * w;
161 out[1] += data[1] * w;
162 }
163 else if (components == 3) {
164 out[0] += data[0] * w;
165 out[1] += data[1] * w;
166 out[2] += data[2] * w;
167 }
168 else {
169 out[0] += data[0] * w;
170 out[1] += data[1] * w;
171 out[2] += data[2] * w;
172 out[3] += data[3] * w;
173 }
174 }
175 }
176
177 /* Mitchell filter has negative lobes; prevent output from going out of range. */
178 if constexpr (filter == eCubicFilter::Mitchell) {
179 for (int i = 0; i < components; i++) {
180 out[i] = math::max(out[i], 0.0f);
181 if constexpr (std::is_same_v<T, uchar>) {
182 out[i] = math::min(out[i], 255.0f);
183 }
184 }
185 }
186
187 /* Write result. */
188 if constexpr (std::is_same_v<T, float>) {
189 if (components == 1) {
190 output[0] = out[0];
191 }
192 else if (components == 2) {
194 }
195 else if (components == 3) {
197 }
198 else {
200 }
201 }
202 else {
203 if (components == 1) {
204 output[0] = uchar(out[0] + 0.5f);
205 }
206 else if (components == 2) {
207 output[0] = uchar(out[0] + 0.5f);
208 output[1] = uchar(out[1] + 0.5f);
209 }
210 else if (components == 3) {
211 output[0] = uchar(out[0] + 0.5f);
212 output[1] = uchar(out[1] + 0.5f);
213 output[2] = uchar(out[2] + 0.5f);
214 }
215 else {
216 output[0] = uchar(out[0] + 0.5f);
217 output[1] = uchar(out[1] + 0.5f);
218 output[2] = uchar(out[2] + 0.5f);
219 output[3] = uchar(out[3] + 0.5f);
220 }
221 }
222}
223
224template<bool border>
225BLI_INLINE uchar4 bilinear_byte_impl(const uchar *buffer, int width, int height, float u, float v)
226{
227 BLI_assert(buffer);
228 uchar4 res;
229
230#if BLI_HAVE_SSE4
231 __m128 uvuv = _mm_set_ps(v, u, v, u);
232 __m128 uvuv_floor = _mm_floor_ps(uvuv);
233
234 /* x1, y1, x2, y2 */
235 __m128i xy12 = _mm_add_epi32(_mm_cvttps_epi32(uvuv_floor), _mm_set_epi32(1, 1, 0, 0));
236 /* Check whether any of the coordinates are outside of the image. */
237 __m128i size_minus_1 = _mm_sub_epi32(_mm_set_epi32(height, width, height, width),
238 _mm_set1_epi32(1));
239
240 /* Samples 1,2,3,4 will be in this order: x1y1, x1y2, x2y1, x2y2. */
241 __m128i x1234, y1234, invalid_1234;
242
243 if constexpr (border) {
244 /* Blend black colors for samples right outside the image: figure out
245 * which of the 4 samples were outside, set their coordinates to zero
246 * and later on put black color into their place. */
247 __m128i too_lo_xy12 = _mm_cmplt_epi32(xy12, _mm_setzero_si128());
248 __m128i too_hi_xy12 = _mm_cmplt_epi32(size_minus_1, xy12);
249 __m128i invalid_xy12 = _mm_or_si128(too_lo_xy12, too_hi_xy12);
250
251 /* Samples 1,2,3,4 are in this order: x1y1, x1y2, x2y1, x2y2 */
252 x1234 = _mm_shuffle_epi32(xy12, _MM_SHUFFLE(2, 2, 0, 0));
253 y1234 = _mm_shuffle_epi32(xy12, _MM_SHUFFLE(3, 1, 3, 1));
254 invalid_1234 = _mm_or_si128(_mm_shuffle_epi32(invalid_xy12, _MM_SHUFFLE(2, 2, 0, 0)),
255 _mm_shuffle_epi32(invalid_xy12, _MM_SHUFFLE(3, 1, 3, 1)));
256 /* Set x & y to zero for invalid samples. */
257 x1234 = _mm_andnot_si128(invalid_1234, x1234);
258 y1234 = _mm_andnot_si128(invalid_1234, y1234);
259 }
260 else {
261 /* Clamp samples to image edges. */
262 __m128i xy12_clamped = _mm_max_epi32(xy12, _mm_setzero_si128());
263 xy12_clamped = _mm_min_epi32(xy12_clamped, size_minus_1);
264 x1234 = _mm_shuffle_epi32(xy12_clamped, _MM_SHUFFLE(2, 2, 0, 0));
265 y1234 = _mm_shuffle_epi32(xy12_clamped, _MM_SHUFFLE(3, 1, 3, 1));
266 }
267
268 /* Read the four sample values. Do address calculations in C, since SSE
269 * before 4.1 makes it very cumbersome to do full integer multiplies. */
270 int xcoord[4];
271 int ycoord[4];
272 _mm_storeu_ps(reinterpret_cast<float *>(xcoord), _mm_castsi128_ps(x1234));
273 _mm_storeu_ps(reinterpret_cast<float *>(ycoord), _mm_castsi128_ps(y1234));
274 int sample1 = (reinterpret_cast<const int *>(buffer))[ycoord[0] * int64_t(width) + xcoord[0]];
275 int sample2 = (reinterpret_cast<const int *>(buffer))[ycoord[1] * int64_t(width) + xcoord[1]];
276 int sample3 = (reinterpret_cast<const int *>(buffer))[ycoord[2] * int64_t(width) + xcoord[2]];
277 int sample4 = (reinterpret_cast<const int *>(buffer))[ycoord[3] * int64_t(width) + xcoord[3]];
278 __m128i samples1234 = _mm_set_epi32(sample4, sample3, sample2, sample1);
279 if constexpr (border) {
280 /* Set samples to black for the ones that were actually invalid. */
281 samples1234 = _mm_andnot_si128(invalid_1234, samples1234);
282 }
283
284 /* Expand samples from packed 8-bit RGBA to full floats:
285 * spread to 16 bit values. */
286 __m128i rgba16_12 = _mm_unpacklo_epi8(samples1234, _mm_setzero_si128());
287 __m128i rgba16_34 = _mm_unpackhi_epi8(samples1234, _mm_setzero_si128());
288 /* Spread to 32 bit values and convert to float. */
289 __m128 rgba1 = _mm_cvtepi32_ps(_mm_unpacklo_epi16(rgba16_12, _mm_setzero_si128()));
290 __m128 rgba2 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba16_12, _mm_setzero_si128()));
291 __m128 rgba3 = _mm_cvtepi32_ps(_mm_unpacklo_epi16(rgba16_34, _mm_setzero_si128()));
292 __m128 rgba4 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba16_34, _mm_setzero_si128()));
293
294 /* Calculate interpolation factors: (1-a)*(1-b), (1-a)*b, a*(1-b), a*b */
295 __m128 abab = _mm_sub_ps(uvuv, uvuv_floor);
296 __m128 m_abab = _mm_sub_ps(_mm_set1_ps(1.0f), abab);
297 __m128 ab_mab = _mm_shuffle_ps(abab, m_abab, _MM_SHUFFLE(3, 2, 1, 0));
298 __m128 factors = _mm_mul_ps(_mm_shuffle_ps(ab_mab, ab_mab, _MM_SHUFFLE(0, 0, 2, 2)),
299 _mm_shuffle_ps(ab_mab, ab_mab, _MM_SHUFFLE(1, 3, 1, 3)));
300
301 /* Blend the samples. */
302 rgba1 = _mm_mul_ps(_mm_shuffle_ps(factors, factors, _MM_SHUFFLE(0, 0, 0, 0)), rgba1);
303 rgba2 = _mm_mul_ps(_mm_shuffle_ps(factors, factors, _MM_SHUFFLE(1, 1, 1, 1)), rgba2);
304 rgba3 = _mm_mul_ps(_mm_shuffle_ps(factors, factors, _MM_SHUFFLE(2, 2, 2, 2)), rgba3);
305 rgba4 = _mm_mul_ps(_mm_shuffle_ps(factors, factors, _MM_SHUFFLE(3, 3, 3, 3)), rgba4);
306 __m128 rgba13 = _mm_add_ps(rgba1, rgba3);
307 __m128 rgba24 = _mm_add_ps(rgba2, rgba4);
308 __m128 rgba = _mm_add_ps(rgba13, rgba24);
309 rgba = _mm_add_ps(rgba, _mm_set1_ps(0.5f));
310 /* Pack and write to destination: pack to 16 bit signed, then to 8 bit
311 * unsigned, then write resulting 32-bit value. */
312 __m128i rgba32 = _mm_cvttps_epi32(rgba);
313 __m128i rgba16 = _mm_packs_epi32(rgba32, _mm_setzero_si128());
314 __m128i rgba8 = _mm_packus_epi16(rgba16, _mm_setzero_si128());
315 _mm_store_ss(reinterpret_cast<float *>(&res), _mm_castsi128_ps(rgba8));
316
317#else
318
319 float uf = floorf(u);
320 float vf = floorf(v);
321
322 int x1 = int(uf);
323 int x2 = x1 + 1;
324 int y1 = int(vf);
325 int y2 = y1 + 1;
326
327 /* Completely outside of the image in bordered mode? */
328 if (border && (x2 < 0 || x1 >= width || y2 < 0 || y1 >= height)) {
329 return uchar4(0);
330 }
331
332 /* Sample locations. */
333 const uchar *row1, *row2, *row3, *row4;
334 uchar empty[4] = {0, 0, 0, 0};
335 if constexpr (border) {
336 row1 = (x1 < 0 || y1 < 0) ? empty : buffer + (int64_t(width) * y1 + x1) * 4;
337 row2 = (x1 < 0 || y2 > height - 1) ? empty : buffer + (int64_t(width) * y2 + x1) * 4;
338 row3 = (x2 > width - 1 || y1 < 0) ? empty : buffer + (int64_t(width) * y1 + x2) * 4;
339 row4 = (x2 > width - 1 || y2 > height - 1) ? empty : buffer + (int64_t(width) * y2 + x2) * 4;
340 }
341 else {
342 x1 = clamp(x1, 0, width - 1);
343 x2 = clamp(x2, 0, width - 1);
344 y1 = clamp(y1, 0, height - 1);
345 y2 = clamp(y2, 0, height - 1);
346 row1 = buffer + (int64_t(width) * y1 + x1) * 4;
347 row2 = buffer + (int64_t(width) * y2 + x1) * 4;
348 row3 = buffer + (int64_t(width) * y1 + x2) * 4;
349 row4 = buffer + (int64_t(width) * y2 + x2) * 4;
350 }
351
352 float a = u - uf;
353 float b = v - vf;
354 float a_b = a * b;
355 float ma_b = (1.0f - a) * b;
356 float a_mb = a * (1.0f - b);
357 float ma_mb = (1.0f - a) * (1.0f - b);
358
359 res.x = uchar(ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0] + 0.5f);
360 res.y = uchar(ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1] + 0.5f);
361 res.z = uchar(ma_mb * row1[2] + a_mb * row3[2] + ma_b * row2[2] + a_b * row4[2] + 0.5f);
362 res.w = uchar(ma_mb * row1[3] + a_mb * row3[3] + ma_b * row2[3] + a_b * row4[3] + 0.5f);
363#endif
364
365 return res;
366}
367
369 const uchar *buffer, int width, int height, float u, float v)
370{
371 return bilinear_byte_impl<true>(buffer, width, height, u, v);
372}
373
374uchar4 interpolate_bilinear_byte(const uchar *buffer, int width, int height, float u, float v)
375{
376 return bilinear_byte_impl<false>(buffer, width, height, u, v);
377}
378
379float4 interpolate_bilinear_border_fl(const float *buffer, int width, int height, float u, float v)
380{
381 float4 res;
383 buffer, res, width, height, 4, u, v, InterpWrapMode::Border, InterpWrapMode::Border);
384 return res;
385}
386
388 const float *buffer, float *output, int width, int height, int components, float u, float v)
389{
391 output,
392 width,
393 height,
394 components,
395 u,
396 v,
399}
400
401float4 interpolate_bilinear_fl(const float *buffer, int width, int height, float u, float v)
402{
403 float4 res;
405 buffer, res, width, height, 4, u, v, InterpWrapMode::Extend, InterpWrapMode::Extend);
406 return res;
407}
408
410 const float *buffer, float *output, int width, int height, int components, float u, float v)
411{
413 output,
414 width,
415 height,
416 components,
417 u,
418 v,
421}
422
423uchar4 interpolate_bilinear_wrap_byte(const uchar *buffer, int width, int height, float u, float v)
424{
425 int x1 = wrap_coord(u, width);
426 int x2 = wrap_coord(u + 1, width);
427 int y1 = wrap_coord(v, height);
428 int y2 = wrap_coord(v + 1, height);
429
430 BLI_assert(x1 >= 0 && x1 < width && y1 >= 0 && y1 < height);
431
432 float a = u - floorf(u);
433 float b = v - floorf(v);
434 float a_b = a * b;
435 float ma_b = (1.0f - a) * b;
436 float a_mb = a * (1.0f - b);
437 float ma_mb = (1.0f - a) * (1.0f - b);
438
439 /* Blend samples. */
440 const uchar *row1 = buffer + (int64_t(width) * y1 + x1) * 4;
441 const uchar *row2 = buffer + (int64_t(width) * y2 + x1) * 4;
442 const uchar *row3 = buffer + (int64_t(width) * y1 + x2) * 4;
443 const uchar *row4 = buffer + (int64_t(width) * y2 + x2) * 4;
444
445 uchar4 res;
446 res.x = uchar(ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0] + 0.5f);
447 res.y = uchar(ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1] + 0.5f);
448 res.z = uchar(ma_mb * row1[2] + a_mb * row3[2] + ma_b * row2[2] + a_b * row4[2] + 0.5f);
449 res.w = uchar(ma_mb * row1[3] + a_mb * row3[3] + ma_b * row2[3] + a_b * row4[3] + 0.5f);
450 return res;
451}
452
453float4 interpolate_bilinear_wrap_fl(const float *buffer, int width, int height, float u, float v)
454{
455 float4 res;
457 buffer, res, width, height, 4, u, v, InterpWrapMode::Repeat, InterpWrapMode::Repeat);
458 return res;
459}
460
461uchar4 interpolate_cubic_bspline_byte(const uchar *buffer, int width, int height, float u, float v)
462{
463 uchar4 res;
465 buffer, res, width, height, 4, u, v, InterpWrapMode::Extend, InterpWrapMode::Extend);
466 return res;
467}
468
469float4 interpolate_cubic_bspline_fl(const float *buffer, int width, int height, float u, float v)
470{
471 float4 res;
473 buffer, res, width, height, 4, u, v, InterpWrapMode::Extend, InterpWrapMode::Extend);
474 return res;
475}
476
478 const float *buffer, float *output, int width, int height, int components, float u, float v)
479{
481 output,
482 width,
483 height,
484 components,
485 u,
486 v,
489}
490
492 float *output,
493 int width,
494 int height,
495 int components,
496 float u,
497 float v,
500{
502 buffer, output, width, height, components, u, v, wrap_u, wrap_v);
503}
504
506 const uchar *buffer, int width, int height, float u, float v)
507{
508 uchar4 res;
510 buffer, res, width, height, 4, u, v, InterpWrapMode::Extend, InterpWrapMode::Extend);
511 return res;
512}
513
514float4 interpolate_cubic_mitchell_fl(const float *buffer, int width, int height, float u, float v)
515{
516 float4 res;
518 buffer, res, width, height, 4, u, v, InterpWrapMode::Extend, InterpWrapMode::Extend);
519 return res;
520}
521
523 const float *buffer, float *output, int width, int height, int components, float u, float v)
524{
526 output,
527 width,
528 height,
529 components,
530 u,
531 v,
534}
535
536} // namespace math
537
538/**************************************************************************
539 * Filtering method based on
540 * "Creating raster omnimax images from multiple perspective views
541 * using the elliptical weighted average filter"
542 * by Ned Greene and Paul S. Heckbert (1986)
543 ***************************************************************************/
544
545/* Table of `(exp(ar) - exp(a)) / (1 - exp(a))` for `r` in range [0, 1] and `a = -2`.
546 * used instead of actual gaussian,
547 * otherwise at high texture magnifications circular artifacts are visible. */
548#define EWA_MAXIDX 255
549const float EWA_WTS[EWA_MAXIDX + 1] = {
550 1.0f, 0.990965f, 0.982f, 0.973105f, 0.96428f, 0.955524f, 0.946836f,
551 0.938216f, 0.929664f, 0.921178f, 0.912759f, 0.904405f, 0.896117f, 0.887893f,
552 0.879734f, 0.871638f, 0.863605f, 0.855636f, 0.847728f, 0.839883f, 0.832098f,
553 0.824375f, 0.816712f, 0.809108f, 0.801564f, 0.794079f, 0.786653f, 0.779284f,
554 0.771974f, 0.76472f, 0.757523f, 0.750382f, 0.743297f, 0.736267f, 0.729292f,
555 0.722372f, 0.715505f, 0.708693f, 0.701933f, 0.695227f, 0.688572f, 0.68197f,
556 0.67542f, 0.66892f, 0.662471f, 0.656073f, 0.649725f, 0.643426f, 0.637176f,
557 0.630976f, 0.624824f, 0.618719f, 0.612663f, 0.606654f, 0.600691f, 0.594776f,
558 0.588906f, 0.583083f, 0.577305f, 0.571572f, 0.565883f, 0.56024f, 0.55464f,
559 0.549084f, 0.543572f, 0.538102f, 0.532676f, 0.527291f, 0.521949f, 0.516649f,
560 0.511389f, 0.506171f, 0.500994f, 0.495857f, 0.490761f, 0.485704f, 0.480687f,
561 0.475709f, 0.470769f, 0.465869f, 0.461006f, 0.456182f, 0.451395f, 0.446646f,
562 0.441934f, 0.437258f, 0.432619f, 0.428017f, 0.42345f, 0.418919f, 0.414424f,
563 0.409963f, 0.405538f, 0.401147f, 0.39679f, 0.392467f, 0.388178f, 0.383923f,
564 0.379701f, 0.375511f, 0.371355f, 0.367231f, 0.363139f, 0.359079f, 0.355051f,
565 0.351055f, 0.347089f, 0.343155f, 0.339251f, 0.335378f, 0.331535f, 0.327722f,
566 0.323939f, 0.320186f, 0.316461f, 0.312766f, 0.3091f, 0.305462f, 0.301853f,
567 0.298272f, 0.294719f, 0.291194f, 0.287696f, 0.284226f, 0.280782f, 0.277366f,
568 0.273976f, 0.270613f, 0.267276f, 0.263965f, 0.26068f, 0.257421f, 0.254187f,
569 0.250979f, 0.247795f, 0.244636f, 0.241502f, 0.238393f, 0.235308f, 0.232246f,
570 0.229209f, 0.226196f, 0.223206f, 0.220239f, 0.217296f, 0.214375f, 0.211478f,
571 0.208603f, 0.20575f, 0.20292f, 0.200112f, 0.197326f, 0.194562f, 0.191819f,
572 0.189097f, 0.186397f, 0.183718f, 0.18106f, 0.178423f, 0.175806f, 0.17321f,
573 0.170634f, 0.168078f, 0.165542f, 0.163026f, 0.16053f, 0.158053f, 0.155595f,
574 0.153157f, 0.150738f, 0.148337f, 0.145955f, 0.143592f, 0.141248f, 0.138921f,
575 0.136613f, 0.134323f, 0.132051f, 0.129797f, 0.12756f, 0.125341f, 0.123139f,
576 0.120954f, 0.118786f, 0.116635f, 0.114501f, 0.112384f, 0.110283f, 0.108199f,
577 0.106131f, 0.104079f, 0.102043f, 0.100023f, 0.0980186f, 0.09603f, 0.094057f,
578 0.0920994f, 0.0901571f, 0.08823f, 0.0863179f, 0.0844208f, 0.0825384f, 0.0806708f,
579 0.0788178f, 0.0769792f, 0.0751551f, 0.0733451f, 0.0715493f, 0.0697676f, 0.0679997f,
580 0.0662457f, 0.0645054f, 0.0627786f, 0.0610654f, 0.0593655f, 0.0576789f, 0.0560055f,
581 0.0543452f, 0.0526979f, 0.0510634f, 0.0494416f, 0.0478326f, 0.0462361f, 0.0446521f,
582 0.0430805f, 0.0415211f, 0.039974f, 0.0384389f, 0.0369158f, 0.0354046f, 0.0339052f,
583 0.0324175f, 0.0309415f, 0.029477f, 0.0280239f, 0.0265822f, 0.0251517f, 0.0237324f,
584 0.0223242f, 0.020927f, 0.0195408f, 0.0181653f, 0.0168006f, 0.0154466f, 0.0141031f,
585 0.0127701f, 0.0114476f, 0.0101354f, 0.00883339f, 0.00754159f, 0.00625989f, 0.00498819f,
586 0.00372644f, 0.00247454f, 0.00123242f, 0.0f,
587};
588
589static void radangle2imp(float a2, float b2, float th, float *A, float *B, float *C, float *F)
590{
591 float ct2 = cosf(th);
592 const float st2 = 1.0f - ct2 * ct2; /* <- sin(th)^2 */
593 ct2 *= ct2;
594 *A = a2 * st2 + b2 * ct2;
595 *B = (b2 - a2) * sinf(2.0f * th);
596 *C = a2 * ct2 + b2 * st2;
597 *F = a2 * b2;
598}
599
601 float A, float B, float C, float F, float *a, float *b, float *th, float *ecc)
602{
603 /* NOTE: all tests here are done to make sure possible overflows are hopefully minimized. */
604
605 if (F <= 1e-5f) { /* use arbitrary major radius, zero minor, infinite eccentricity */
606 *a = sqrtf(A > C ? A : C);
607 *b = 0.0f;
608 *ecc = 1e10f;
609 *th = 0.5f * (atan2f(B, A - C) + float(M_PI));
610 }
611 else {
612 const float AmC = A - C, ApC = A + C, F2 = F * 2.0f;
613 const float r = sqrtf(AmC * AmC + B * B);
614 float d = ApC - r;
615 *a = (d <= 0.0f) ? sqrtf(A > C ? A : C) : sqrtf(F2 / d);
616 d = ApC + r;
617 if (d <= 0.0f) {
618 *b = 0.0f;
619 *ecc = 1e10f;
620 }
621 else {
622 *b = sqrtf(F2 / d);
623 *ecc = *a / *b;
624 }
625 /* Increase theta by `0.5 * pi` (angle of major axis). */
626 *th = 0.5f * (atan2f(B, AmC) + float(M_PI));
627 }
628}
629
630void BLI_ewa_filter(const int width,
631 const int height,
632 const bool intpol,
633 const bool use_alpha,
634 const float uv[2],
635 const float du[2],
636 const float dv[2],
637 ewa_filter_read_pixel_cb read_pixel_cb,
638 void *userdata,
639 float result[4],
640 bool clip)
641{
642 /* Scaling `dxt` / `dyt` by full resolution can cause overflow because of huge A/B/C and esp.
643 * F values, scaling by aspect ratio alone does the opposite, so try something in between
644 * instead. */
645 const float ff2 = float(width), ff = sqrtf(ff2), q = float(height) / ff;
646 const float Ux = du[0] * ff, Vx = du[1] * q, Uy = dv[0] * ff, Vy = dv[1] * q;
647 float A = Vx * Vx + Vy * Vy;
648 float B = -2.0f * (Ux * Vx + Uy * Vy);
649 float C = Ux * Ux + Uy * Uy;
650 float F = A * C - B * B * 0.25f;
651 float a, b, th, ecc, a2, b2, ue, ve, U0, V0, DDQ, U, ac1, ac2, BU, d;
652 int u, v, u1, u2, v1, v2;
653
654 /* The so-called 'high' quality EWA method simply adds a constant of 1 to both A & C,
655 * so the ellipse always covers at least some texels. But since the filter is now always larger,
656 * it also means that everywhere else it's also more blurry then ideally should be the case.
657 * So instead here the ellipse radii are modified instead whenever either is too low.
658 * Use a different radius based on interpolation switch,
659 * just enough to anti-alias when interpolation is off,
660 * and slightly larger to make result a bit smoother than bilinear interpolation when
661 * interpolation is on (minimum values: `const float rmin = intpol ? 1.0f : 0.5f;`) */
662 const float rmin = (intpol ? 1.5625f : 0.765625f) / ff2;
663 BLI_ewa_imp2radangle(A, B, C, F, &a, &b, &th, &ecc);
664 if ((b2 = b * b) < rmin) {
665 if ((a2 = a * a) < rmin) {
666 UNUSED_VARS(a2, b2);
667 B = 0.0f;
668 A = C = rmin;
669 F = A * C;
670 }
671 else {
672 b2 = rmin;
673 radangle2imp(a2, b2, th, &A, &B, &C, &F);
674 }
675 }
676
677 ue = ff * sqrtf(C);
678 ve = ff * sqrtf(A);
679 d = float(EWA_MAXIDX + 1) / (F * ff2);
680 A *= d;
681 B *= d;
682 C *= d;
683
684 U0 = uv[0] * float(width);
685 V0 = uv[1] * float(height);
686 u1 = int(floorf(U0 - ue));
687 u2 = int(ceilf(U0 + ue));
688 v1 = int(floorf(V0 - ve));
689 v2 = int(ceilf(V0 + ve));
690
691 /* sane clamping to avoid unnecessarily huge loops */
692 /* NOTE: if eccentricity gets clamped (see above),
693 * the ue/ve limits can also be lowered accordingly
694 */
695 if (U0 - float(u1) > EWA_MAXIDX) {
696 u1 = int(U0) - EWA_MAXIDX;
697 }
698 if (float(u2) - U0 > EWA_MAXIDX) {
699 u2 = int(U0) + EWA_MAXIDX;
700 }
701 if (V0 - float(v1) > EWA_MAXIDX) {
702 v1 = int(V0) - EWA_MAXIDX;
703 }
704 if (float(v2) - V0 > EWA_MAXIDX) {
705 v2 = int(V0) + EWA_MAXIDX;
706 }
707
708 /* Early output check for cases the whole region is outside of the buffer. */
709 if (clip && ((u2 < 0 || u1 >= width) || (v2 < 0 || v1 >= height))) {
711 return;
712 }
713
714 U0 -= 0.5f;
715 V0 -= 0.5f;
716 DDQ = 2.0f * A;
717 U = float(u1) - U0;
718 ac1 = A * (2.0f * U + 1.0f);
719 ac2 = A * U * U;
720 BU = B * U;
721
722 d = 0.0f;
724 for (v = v1; v <= v2; v++) {
725 const float V = float(v) - V0;
726 float DQ = ac1 + B * V;
727 float Q = (C * V + BU) * V + ac2;
728 for (u = u1; u <= u2; u++) {
729 if (Q < float(EWA_MAXIDX + 1)) {
730 float tc[4];
731 const float wt = EWA_WTS[(Q < 0.0f) ? 0 : uint(Q)];
732 read_pixel_cb(userdata, u, v, tc);
733 madd_v3_v3fl(result, tc, wt);
734 result[3] += use_alpha ? tc[3] * wt : 0.0f;
735 d += wt;
736 }
737 Q += DQ;
738 DQ += DDQ;
739 }
740 }
741
742 /* `d` should hopefully never be zero anymore. */
743 d = 1.0f / d;
744 mul_v3_fl(result, d);
745 /* Clipping can be ignored if alpha used, `texr->trgba[3]` already includes filtered edge. */
746 result[3] = use_alpha ? result[3] * d : 1.0f;
747}
748
749} // namespace blender
#define M_PI
#define EWA_MAXIDX
#define C
Definition RandGen.cpp:29
#define CLAMP(a, b, c)
#define UNUSED_VARS(...)
#define BLI_INLINE
#define A
#define output
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define T
#define B
#define F
uchar4 interpolate_cubic_mitchell_byte(const uchar *buffer, int width, int height, float u, float v)
uchar4 interpolate_bilinear_wrap_byte(const uchar *buffer, int width, int height, float u, float v)
BLI_INLINE void bicubic_interpolation(const T *src_buffer, T *output, int width, int height, int components, float u, float v, InterpWrapMode wrap_u, InterpWrapMode wrap_v)
static float4 cubic_filter_coefficients(float f)
uchar4 interpolate_bilinear_byte(const uchar *buffer, int width, int height, float u, float v)
BLI_INLINE uchar4 bilinear_byte_impl(const uchar *buffer, int width, int height, float u, float v)
T clamp(const T &a, const T &min, const T &max)
T floor(const T &a)
float4 interpolate_bilinear_border_fl(const float *buffer, int width, int height, float u, float v)
float4 interpolate_bilinear_wrap_fl(const float *buffer, int width, int height, float u, float v)
float4 interpolate_cubic_bspline_fl(const float *buffer, int width, int height, float u, float v)
BLI_INLINE int32_t wrap_coord(float u, int32_t size, InterpWrapMode wrap)
T min(const T &a, const T &b)
float4 interpolate_bilinear_fl(const float *buffer, 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)
float4 interpolate_cubic_mitchell_fl(const float *buffer, int width, int height, float u, float v)
T max(const T &a, const T &b)
uchar4 interpolate_cubic_bspline_byte(const uchar *buffer, int width, int height, float u, float v)
uchar4 interpolate_bilinear_border_byte(const uchar *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.
UserDef U
Definition blender.cc:58
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
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
MINLINE void copy_v2_v2(float r[2], const float a[2])
static void radangle2imp(float a2, float b2, float th, float *A, float *B, float *C, float *F)
ATTR_WARN_UNUSED_RESULT const BMVert * v
MINLINE void mul_v3_fl(float r[3], float f)
VecBase< float, 4 > float4
MINLINE void zero_v4(float r[4])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
const float EWA_WTS[EWA_MAXIDX+1]
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void BLI_ewa_imp2radangle(float A, float B, float C, float F, float *a, float *b, float *th, float *ecc)
MINLINE void copy_v4_v4(float r[4], const float a[4])
#define floorf
#define sqrtf
#define sinf
#define cosf
#define ceilf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
CCL_NAMESPACE_BEGIN struct Window V