Blender V5.0
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.h"
26#include "BLI_math_base.hh"
28
29namespace blender::math {
30
43
44/* -------------------------------------------------------------------- */
45/* Nearest (point) sampling. */
46
56
58 const uchar *buffer, uchar *output, int width, int height, float u, float v)
59{
60 BLI_assert(buffer);
61 int x = int(u);
62 int y = int(v);
63
64 /* Outside image? */
65 if (x < 0 || x >= width || y < 0 || y >= height) {
66 output[0] = output[1] = output[2] = output[3] = 0;
67 return;
68 }
69
70 const uchar *data = buffer + (int64_t(width) * y + x) * 4;
71 output[0] = data[0];
72 output[1] = data[1];
73 output[2] = data[2];
74 output[3] = data[3];
75}
76
78 const uchar *buffer, int width, int height, float u, float v)
79{
80 uchar4 res;
81 interpolate_nearest_border_byte(buffer, res, width, height, u, v);
82 return res;
83}
84
86 const float *buffer, float *output, int width, int height, int components, float u, float v)
87{
88 BLI_assert(buffer);
89 int x = int(u);
90 int y = int(v);
91
92 /* Outside image? */
93 if (x < 0 || x >= width || y < 0 || y >= height) {
94 for (int i = 0; i < components; i++) {
95 output[i] = 0.0f;
96 }
97 return;
98 }
99
100 const float *data = buffer + (int64_t(width) * y + x) * components;
101 for (int i = 0; i < components; i++) {
102 output[i] = data[i];
103 }
104}
105
107 const float *buffer, int width, int height, float u, float v)
108{
109 float4 res;
110 interpolate_nearest_border_fl(buffer, res, width, height, 4, u, v);
111 return res;
112}
113
123
125 const uchar *buffer, uchar *output, int width, int height, float u, float v)
126{
127 BLI_assert(buffer);
128 const int x = math::clamp(int(u), 0, width - 1);
129 const int y = math::clamp(int(v), 0, height - 1);
130
131 const uchar *data = buffer + (int64_t(width) * y + x) * 4;
132 output[0] = data[0];
133 output[1] = data[1];
134 output[2] = data[2];
135 output[3] = data[3];
136}
137
138[[nodiscard]] inline uchar4 interpolate_nearest_byte(
139 const uchar *buffer, int width, int height, float u, float v)
140{
141 uchar4 res;
142 interpolate_nearest_byte(buffer, res, width, height, u, v);
143 return res;
144}
145
147 const float *buffer, float *output, int width, int height, int components, float u, float v)
148{
149 BLI_assert(buffer);
150 const int x = math::clamp(int(u), 0, width - 1);
151 const int y = math::clamp(int(v), 0, height - 1);
152
153 const float *data = buffer + (int64_t(width) * y + x) * components;
154 for (int i = 0; i < components; i++) {
155 output[i] = data[i];
156 }
157}
158
159[[nodiscard]] inline float4 interpolate_nearest_fl(
160 const float *buffer, int width, int height, float u, float v)
161{
162 float4 res;
163 interpolate_nearest_fl(buffer, res, width, height, 4, u, v);
164 return res;
165}
166
170
172 const uchar *buffer, uchar *output, int width, int height, float u, float v)
173{
174 BLI_assert(buffer);
175 u = floored_fmod(u, float(width));
176 v = floored_fmod(v, float(height));
177 int x = int(u);
178 int y = int(v);
179 BLI_assert(x >= 0 && y >= 0 && x < width && y < height);
180
181 const uchar *data = buffer + (int64_t(width) * y + x) * 4;
182 output[0] = data[0];
183 output[1] = data[1];
184 output[2] = data[2];
185 output[3] = data[3];
186}
187
189 const uchar *buffer, int width, int height, float u, float v)
190{
191 uchar4 res;
192 interpolate_nearest_wrap_byte(buffer, res, width, height, u, v);
193 return res;
194}
195
197 const float *buffer, float *output, int width, int height, int components, float u, float v)
198{
199 BLI_assert(buffer);
200 u = floored_fmod(u, float(width));
201 v = floored_fmod(v, float(height));
202 int x = int(u);
203 int y = int(v);
204 BLI_assert(x >= 0 && y >= 0 && x < width && y < height);
205
206 const float *data = buffer + (int64_t(width) * y + x) * components;
207 for (int i = 0; i < components; i++) {
208 output[i] = data[i];
209 }
210}
211
213 const float *buffer, int width, int height, float u, float v)
214{
215 float4 res;
216 interpolate_nearest_wrap_fl(buffer, res, width, height, 4, u, v);
217 return res;
218}
219
220void interpolate_nearest_wrapmode_fl(const float *buffer,
221 float *output,
222 int width,
223 int height,
224 int components,
225 float u,
226 float v,
227 InterpWrapMode wrap_u,
228 InterpWrapMode wrap_v);
229
230/* -------------------------------------------------------------------- */
231/* Bilinear sampling. */
232
243
245 const uchar *buffer, int width, int height, float u, float v);
246
248 const float *buffer, int width, int height, float u, float v);
249
251 const float *buffer, float *output, int width, int height, int components, float u, float v);
252
263
265 const uchar *buffer, int width, int height, float u, float v);
266
267[[nodiscard]] float4 interpolate_bilinear_fl(
268 const float *buffer, int width, int height, float u, float v);
269
271 const float *buffer, float *output, int width, int height, int components, float u, float v);
272
277
279 const uchar *buffer, int width, int height, float u, float v);
280
282 const float *buffer, int width, int height, float u, float v);
283
284void interpolate_bilinear_wrapmode_fl(const float *buffer,
285 float *output,
286 int width,
287 int height,
288 int components,
289 float u,
290 float v,
291 InterpWrapMode wrap_u,
292 InterpWrapMode wrap_v);
293
294/* -------------------------------------------------------------------- */
295/* Cubic sampling. */
296
308
310 const uchar *buffer, int width, int height, float u, float v);
311
313 const float *buffer, int width, int height, float u, float v);
314
316 const float *buffer, float *output, int width, int height, int components, float u, float v);
317
318void interpolate_cubic_bspline_wrapmode_fl(const float *buffer,
319 float *output,
320 int width,
321 int height,
322 int components,
323 float u,
324 float v,
325 InterpWrapMode wrap_u,
326 InterpWrapMode wrap_v);
327
339
341 const uchar *buffer, int width, int height, float u, float v);
342
344 const float *buffer, int width, int height, float u, float v);
345
347 const float *buffer, float *output, int width, int height, int components, float u, float v);
348
349} // namespace blender::math
350
351/* -------------------------------------------------------------------- */
352/* EWA sampling. */
353
354#define EWA_MAXIDX 255
355extern const float EWA_WTS[EWA_MAXIDX + 1];
356
357using ewa_filter_read_pixel_cb = void (*)(void *userdata, int x, int y, float result[4]);
358
360 float A, float B, float C, float F, float *a, float *b, float *th, float *ecc);
361
366void BLI_ewa_filter(int width,
367 int height,
368 bool intpol,
369 bool use_alpha,
370 const float uv[2],
371 const float du[2],
372 const float dv[2],
373 ewa_filter_read_pixel_cb read_pixel_cb,
374 void *userdata,
375 float result[4]);
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE float floored_fmod(float f, float n)
void(*)(void *userdata, int x, int y, float result[4]) ewa_filter_read_pixel_cb
#define EWA_MAXIDX
const float EWA_WTS[EWA_MAXIDX+1]
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])
void BLI_ewa_imp2radangle(float A, float B, float C, float F, float *a, float *b, float *th, float *ecc)
unsigned char uchar
#define C
Definition RandGen.cpp:29
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define A
long long int int64_t
#define output
#define B
#define F
uchar4 interpolate_cubic_mitchell_byte(const uchar *buffer, int width, int height, float u, float v)
void interpolate_nearest_wrap_fl(const float *buffer, float *output, int width, int height, int components, float u, float v)
uchar4 interpolate_bilinear_wrap_byte(const uchar *buffer, int width, int height, float u, float v)
uchar4 interpolate_bilinear_byte(const uchar *buffer, int width, int height, float u, float v)
T clamp(const T &a, const T &min, const T &max)
void interpolate_nearest_fl(const float *buffer, float *output, int width, int height, int components, float u, float v)
void interpolate_nearest_border_fl(const float *buffer, float *output, int width, int height, int components, float u, float v)
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)
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)
void interpolate_nearest_byte(const uchar *buffer, uchar *output, int width, int height, float u, float v)
float4 interpolate_bilinear_fl(const float *buffer, int width, int height, float u, float 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_bilinear_wrapmode_fl(const float *buffer, float *output, int width, int height, int components, float u, float v, InterpWrapMode wrap_u, InterpWrapMode wrap_v)
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_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)
blender::VecBase< uint8_t, 4 > uchar4
VecBase< float, 4 > float4
i
Definition text_draw.cc:230