Blender V5.3
node_geo_image_texture.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BKE_image.hh"
8
10#include "BLI_threads.hh"
11
13#include "IMB_imbuf.hh"
14#include "IMB_imbuf_types.hh"
15
17#include "UI_resources.hh"
18
20
22
24{
25 b.add_input<decl::Image>("Image"_ustr).optional_label();
26 b.add_input<decl::Vector>("Vector"_ustr)
27 .structure_type(StructureType::Dynamic)
28 .default_input_type(NODE_DEFAULT_INPUT_POSITION_FIELD)
29 .description("Texture coordinates from 0 to 1");
30 b.add_input<decl::Int>("Frame"_ustr).min(0).max(MAXFRAMEF);
31 b.add_output<decl::Color>("Color"_ustr)
32 .no_muted_links()
33 .inferred_structure_type()
34 .propagate_references();
35 b.add_output<decl::Float>("Alpha"_ustr)
36 .no_muted_links()
37 .inferred_structure_type()
38 .propagate_references();
39}
40
41static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
42{
43 layout.prop(ptr, "interpolation", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
44 layout.prop(ptr, "extension", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
45}
46
47static void node_init(bNodeTree * /*tree*/, bNode *node)
48{
49 NodeGeometryImageTexture *tex = MEM_new<NodeGeometryImageTexture>(__func__);
52 node->storage = tex;
53}
54
55class ImageFieldsFunction : public mf::MultiFunction {
56 private:
57 const int8_t interpolation_;
58 const int8_t extension_;
59 Image &image_;
60 ImageUser image_user_;
61 void *image_lock_;
62 ImBuf *image_buffer_;
63
64 public:
65 ImageFieldsFunction(const int8_t interpolation,
66 const int8_t extension,
67 Image &image,
68 ImageUser image_user)
69 : interpolation_(interpolation),
70 extension_(extension),
71 image_(image),
72 image_user_(image_user)
73 {
74 static const mf::Signature signature = []() {
75 mf::Signature signature;
76 mf::SignatureBuilder builder{"ImageFunction", signature};
77 builder.single_input<float3>("Vector");
78 builder.single_output<ColorGeometry4f>("Color");
79 builder.single_output<float>("Alpha", mf::ParamFlag::SupportsUnusedOutput);
80 return signature;
81 }();
83
84 image_buffer_ = BKE_image_acquire_ibuf(&image_, &image_user_, &image_lock_);
85 if (image_buffer_ == nullptr) {
86 throw std::runtime_error("cannot acquire image buffer");
87 }
88 }
89
91 {
92 BKE_image_release_ibuf(&image_, image_buffer_, image_lock_);
93 }
94
95 static int wrap_periodic(int x, const int width)
96 {
97 x %= width;
98 if (x < 0) {
99 x += width;
100 }
101 return x;
102 }
103
104 static int wrap_clamp(const int x, const int width)
105 {
106 return std::clamp(x, 0, width - 1);
107 }
108
109 static int wrap_mirror(const int x, const int width)
110 {
111 const int m = std::abs(x + (x < 0)) % (2 * width);
112 if (m >= width) {
113 return 2 * width - m - 1;
114 }
115 return m;
116 }
117
119 const float *buffer, const int width, const int height, const int px, const int py)
120 {
121 if (px < 0 || py < 0 || px >= width || py >= height) {
122 return float4(0.0f, 0.0f, 0.0f, 0.0f);
123 }
124 return ((const float4 *)buffer)[size_t(px) + size_t(py) * size_t(width)];
125 }
126
128 const uint8_t *buffer, const int width, const int height, const int px, const int py)
129 {
130 if (px < 0 || py < 0 || px >= width || py >= height) {
131 return float4(0.0f, 0.0f, 0.0f, 0.0f);
132 }
133 float4 float_array;
134 const uint8_t *pixel_start = &buffer[(size_t(px) + size_t(py) * size_t(width)) * 4];
135 rgba_uchar_to_float(float_array, pixel_start);
136 return float_array;
137 }
138
139 static float frac(const float x, int *ix)
140 {
141 const int i = int(x) - ((x < 0.0f) ? 1 : 0);
142 *ix = i;
143 return x - float(i);
144 }
145
146 template<typename BufferT>
147 static float4 image_cubic_texture_lookup(const BufferT *buffer,
148 const int width,
149 const int height,
150 const float2 coord,
151 const int extension)
152 {
153 int pix, piy, nix, niy;
154 const float tx = frac(coord.x * float(width) - 0.5f, &pix);
155 const float ty = frac(coord.y * float(height) - 0.5f, &piy);
156 int ppix, ppiy, nnix, nniy;
157
158 switch (extension) {
160 pix = wrap_periodic(pix, width);
161 piy = wrap_periodic(piy, height);
162 ppix = wrap_periodic(pix - 1, width);
163 ppiy = wrap_periodic(piy - 1, height);
164 nix = wrap_periodic(pix + 1, width);
165 niy = wrap_periodic(piy + 1, height);
166 nnix = wrap_periodic(pix + 2, width);
167 nniy = wrap_periodic(piy + 2, height);
168 break;
169 }
171 ppix = pix - 1;
172 ppiy = piy - 1;
173 nix = pix + 1;
174 niy = piy + 1;
175 nnix = pix + 2;
176 nniy = piy + 2;
177 break;
178 }
180 ppix = wrap_clamp(pix - 1, width);
181 ppiy = wrap_clamp(piy - 1, height);
182 nix = wrap_clamp(pix + 1, width);
183 niy = wrap_clamp(piy + 1, height);
184 nnix = wrap_clamp(pix + 2, width);
185 nniy = wrap_clamp(piy + 2, height);
186 pix = wrap_clamp(pix, width);
187 piy = wrap_clamp(piy, height);
188 break;
189 }
191 ppix = wrap_mirror(pix - 1, width);
192 ppiy = wrap_mirror(piy - 1, height);
193 nix = wrap_mirror(pix + 1, width);
194 niy = wrap_mirror(piy + 1, height);
195 nnix = wrap_mirror(pix + 2, width);
196 nniy = wrap_mirror(piy + 2, height);
197 pix = wrap_mirror(pix, width);
198 piy = wrap_mirror(piy, height);
199 break;
200 }
201 default:
202 return float4(0.0f, 0.0f, 0.0f, 0.0f);
203 }
204
205 const int xc[4] = {ppix, pix, nix, nnix};
206 const int yc[4] = {ppiy, piy, niy, nniy};
207 float u[4], v[4];
208
209 u[0] = (((-1.0f / 6.0f) * tx + 0.5f) * tx - 0.5f) * tx + (1.0f / 6.0f);
210 u[1] = ((0.5f * tx - 1.0f) * tx) * tx + (2.0f / 3.0f);
211 u[2] = ((-0.5f * tx + 0.5f) * tx + 0.5f) * tx + (1.0f / 6.0f);
212 u[3] = (1.0f / 6.0f) * tx * tx * tx;
213
214 v[0] = (((-1.0f / 6.0f) * ty + 0.5f) * ty - 0.5f) * ty + (1.0f / 6.0f);
215 v[1] = ((0.5f * ty - 1.0f) * ty) * ty + (2.0f / 3.0f);
216 v[2] = ((-0.5f * ty + 0.5f) * ty + 0.5f) * ty + (1.0f / 6.0f);
217 v[3] = (1.0f / 6.0f) * ty * ty * ty;
218
219 return (v[0] * (u[0] * image_pixel_lookup(buffer, width, height, xc[0], yc[0]) +
220 u[1] * image_pixel_lookup(buffer, width, height, xc[1], yc[0]) +
221 u[2] * image_pixel_lookup(buffer, width, height, xc[2], yc[0]) +
222 u[3] * image_pixel_lookup(buffer, width, height, xc[3], yc[0]))) +
223 (v[1] * (u[0] * image_pixel_lookup(buffer, width, height, xc[0], yc[1]) +
224 u[1] * image_pixel_lookup(buffer, width, height, xc[1], yc[1]) +
225 u[2] * image_pixel_lookup(buffer, width, height, xc[2], yc[1]) +
226 u[3] * image_pixel_lookup(buffer, width, height, xc[3], yc[1]))) +
227 (v[2] * (u[0] * image_pixel_lookup(buffer, width, height, xc[0], yc[2]) +
228 u[1] * image_pixel_lookup(buffer, width, height, xc[1], yc[2]) +
229 u[2] * image_pixel_lookup(buffer, width, height, xc[2], yc[2]) +
230 u[3] * image_pixel_lookup(buffer, width, height, xc[3], yc[2]))) +
231 (v[3] * (u[0] * image_pixel_lookup(buffer, width, height, xc[0], yc[3]) +
232 u[1] * image_pixel_lookup(buffer, width, height, xc[1], yc[3]) +
233 u[2] * image_pixel_lookup(buffer, width, height, xc[2], yc[3]) +
234 u[3] * image_pixel_lookup(buffer, width, height, xc[3], yc[3])));
235 }
236
237 template<typename BufferT>
238 static float4 image_linear_texture_lookup(const BufferT *buffer,
239 const int width,
240 const int height,
241 const float2 coord,
242 const int8_t extension)
243 {
244 int pix, piy, nix, niy;
245 const float nfx = frac(coord.x * float(width) - 0.5f, &pix);
246 const float nfy = frac(coord.y * float(height) - 0.5f, &piy);
247
248 switch (extension) {
250 nix = pix + 1;
251 niy = piy + 1;
252 break;
253 }
255 nix = wrap_clamp(pix + 1, width);
256 niy = wrap_clamp(piy + 1, height);
257 pix = wrap_clamp(pix, width);
258 piy = wrap_clamp(piy, height);
259 break;
260 }
262 nix = wrap_mirror(pix + 1, width);
263 niy = wrap_mirror(piy + 1, height);
264 pix = wrap_mirror(pix, width);
265 piy = wrap_mirror(piy, height);
266 break;
267 default:
269 pix = wrap_periodic(pix, width);
270 piy = wrap_periodic(piy, height);
271 nix = wrap_periodic(pix + 1, width);
272 niy = wrap_periodic(piy + 1, height);
273 break;
274 }
275
276 const float ptx = 1.0f - nfx;
277 const float pty = 1.0f - nfy;
278
279 return image_pixel_lookup(buffer, width, height, pix, piy) * ptx * pty +
280 image_pixel_lookup(buffer, width, height, nix, piy) * nfx * pty +
281 image_pixel_lookup(buffer, width, height, pix, niy) * ptx * nfy +
282 image_pixel_lookup(buffer, width, height, nix, niy) * nfx * nfy;
283 }
284
285 template<typename BufferT>
286 static float4 image_closest_texture_lookup(const BufferT *buffer,
287 const int width,
288 const int height,
289 const float2 coord,
290 const int extension)
291 {
292 int ix, iy;
293 const float tx = frac(coord.x * float(width), &ix);
294 const float ty = frac(coord.y * float(height), &iy);
295
296 switch (extension) {
298 ix = wrap_periodic(ix, width);
299 iy = wrap_periodic(iy, height);
300 return image_pixel_lookup(buffer, width, height, ix, iy);
301 }
303 if (tx < 0.0f || ty < 0.0f || tx > 1.0f || ty > 1.0f) {
304 return float4(0.0f, 0.0f, 0.0f, 0.0f);
305 }
306 if (ix < 0 || iy < 0 || ix > width || iy > height) {
307 return float4(0.0f, 0.0f, 0.0f, 0.0f);
308 }
310 }
312 ix = wrap_clamp(ix, width);
313 iy = wrap_clamp(iy, height);
314 return image_pixel_lookup(buffer, width, height, ix, iy);
315 }
317 ix = wrap_mirror(ix, width);
318 iy = wrap_mirror(iy, height);
319 return image_pixel_lookup(buffer, width, height, ix, iy);
320 }
321 default:
322 return float4(0.0f, 0.0f, 0.0f, 0.0f);
323 }
324 }
325
326 template<typename BufferT>
327 void sample_buffer(const BufferT *buffer,
328 const IndexMask &mask,
329 const VArray<float3> &vectors,
330 int width,
331 int height,
332 MutableSpan<float4> &color_data) const
333 {
334 /* Sample image texture. */
335 switch (interpolation_) {
337 mask.foreach_index([&](const int64_t i) {
338 const float3 p = vectors[i];
339 color_data[i] = image_linear_texture_lookup(buffer, width, height, p.xy(), extension_);
340 });
341 break;
343 mask.foreach_index([&](const int64_t i) {
344 const float3 p = vectors[i];
345 color_data[i] = image_closest_texture_lookup(buffer, width, height, p.xy(), extension_);
346 });
347 break;
348 case SHD_INTERP_CUBIC:
349 case SHD_INTERP_SMART:
350 mask.foreach_index([&](const int64_t i) {
351 const float3 p = vectors[i];
352 color_data[i] = image_cubic_texture_lookup(buffer, width, height, p.xy(), extension_);
353 });
354 break;
355 }
356 }
357
358 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
359 {
360 const VArray<float3> &vectors = params.readonly_single_input<float3>(0, "Vector");
361 MutableSpan<ColorGeometry4f> r_color = params.uninitialized_single_output<ColorGeometry4f>(
362 1, "Color");
363 MutableSpan<float> r_alpha = params.uninitialized_single_output_if_required<float>(2, "Alpha");
364
365 MutableSpan<float4> color_data{reinterpret_cast<float4 *>(r_color.data()), r_color.size()};
366
367 const int width = image_buffer_->x;
368 const int height = image_buffer_->y;
369
370 if (image_buffer_->float_data()) {
371 sample_buffer(image_buffer_->float_data(), mask, vectors, width, height, color_data);
372 }
373 else if (image_buffer_->byte_data()) {
374 sample_buffer(image_buffer_->byte_data(), mask, vectors, width, height, color_data);
375 }
376 else {
378 }
379
380 int alpha_mode = image_.alpha_mode;
381 if (IMB_colormanagement_space_name_is_data(image_.colorspace_settings.name)) {
382 alpha_mode = IMA_ALPHA_CHANNEL_PACKED;
383 }
384
385 switch (alpha_mode) {
386 case IMA_ALPHA_STRAIGHT: {
387 /* #ColorGeometry expects premultiplied alpha, so convert from straight to that. */
388 mask.foreach_index([&](const int64_t i) { straight_to_premul_v4(color_data[i]); });
389 break;
390 }
391 case IMA_ALPHA_PREMUL: {
392 /* Alpha is premultiplied already, nothing to do. */
393 break;
394 }
396 /* Color and alpha channels shouldn't interact with each other, nothing to do. */
397 break;
398 }
399 case IMA_ALPHA_IGNORE: {
400 /* The image should be treated as being opaque. */
401 mask.foreach_index_optimized<int64_t>([&](const int64_t i) { color_data[i].w = 1.0f; });
402 break;
403 }
404 }
405
406 if (!r_alpha.is_empty()) {
407 mask.foreach_index_optimized<int64_t>([&](const int64_t i) { r_alpha[i] = r_color[i].a; });
408 }
409 }
410};
411
413{
414 Image *image = params.extract_input<Image *>("Image"_ustr);
415 if (image == nullptr) {
417 return;
418 }
419
420 const NodeGeometryImageTexture &storage = node_storage(params.node());
421
422 ImageUser image_user;
423 BKE_imageuser_default(&image_user);
424 image_user.cycl = false;
425 image_user.frames = INT_MAX;
426 image_user.sfra = 1;
427 image_user.framenr = BKE_image_is_animated(image) ? params.extract_input<int>("Frame"_ustr) : 0;
428
429 std::unique_ptr<ImageFieldsFunction> image_fn;
430 try {
431 image_fn = std::make_unique<ImageFieldsFunction>(
432 storage.interpolation, storage.extension, *image, image_user);
433 }
434 catch (const std::runtime_error &) {
436 return;
437 }
438
439 auto sample_uv = params.extract_input<bke::SocketValueVariant>("Vector"_ustr);
440
441 std::string error_message;
445 std::move(image_fn), {&sample_uv}, {&color, &alpha}, params.user_data(), error_message))
446 {
448 params.error_message_add(NodeWarningType::Error, std::move(error_message));
449 return;
450 }
451
452 params.set_output("Color"_ustr, std::move(color));
453 params.set_output("Alpha"_ustr, std::move(alpha));
454}
455
456static void node_register()
457{
458 static bke::bNodeType ntype;
459
460 geo_node_type_base(&ntype, "GeometryNodeImageTexture"_ustr, GEO_NODE_IMAGE_TEXTURE);
461 ntype.ui_name = "Image Texture";
462 ntype.ui_description = "Sample values from an image texture";
463 ntype.enum_name_legacy = "IMAGE_TEXTURE";
465 ntype.declare = node_declare;
467 ntype.initfunc = node_init;
469 ntype, "NodeGeometryImageTexture", node_free_standard_storage, node_copy_standard_storage);
472
474}
476
477} // namespace blender::nodes::node_geo_image_texture_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define GEO_NODE_IMAGE_TEXTURE
#define ATTR_FALLTHROUGH
#define MAXFRAMEF
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
btScalar max
void set_signature(const Signature *signature)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
GeoNodesUserData * user_data() const
void set_output(const UString identifier, T &&value)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
static float4 image_linear_texture_lookup(const BufferT *buffer, const int width, const int height, const float2 coord, const int8_t extension)
static float4 image_pixel_lookup(const uint8_t *buffer, const int width, const int height, const int px, const int py)
ImageFieldsFunction(const int8_t interpolation, const int8_t extension, Image &image, ImageUser image_user)
static float4 image_pixel_lookup(const float *buffer, const int width, const int height, const int px, const int py)
void sample_buffer(const BufferT *buffer, const IndexMask &mask, const VArray< float3 > &vectors, int width, int height, MutableSpan< float4 > &color_data) const
static float4 image_closest_texture_lookup(const BufferT *buffer, const int width, const int height, const float2 coord, const int extension)
static float4 image_cubic_texture_lookup(const BufferT *buffer, const int width, const int height, const float2 coord, const int extension)
#define INT_MAX
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
bool execute_multi_function_on_value_variant(const MultiFunction &fn, const std::shared_ptr< MultiFunction > &owned_fn, const Span< SocketValueVariant * > input_values, const Span< SocketValueVariant * > output_values, GeoNodesUserData *user_data, std::string &r_error_message)
bool BKE_image_is_animated(Image *image)
__int64 int64_t
Definition BKE_text.h:90
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
@ IMA_ALPHA_CHANNEL_PACKED
@ SHD_IMAGE_EXTENSION_EXTEND
@ SHD_IMAGE_EXTENSION_CLIP
@ SHD_IMAGE_EXTENSION_MIRROR
@ SHD_IMAGE_EXTENSION_REPEAT
ATTR_WARN_UNUSED_RESULT const BMVert * v
VecBase< float, 4 > float4
VecBase< float, 2 > float2
unsigned char uint8_t
Definition BKE_text.h:79
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
bool IMB_colormanagement_space_name_is_data(const char *name)
void BKE_imageuser_default(ImageUser *iuser)
MINLINE void straight_to_premul_v4(float color[4])
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed char int8_t
Definition BKE_text.h:76
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static constexpr int _240
Definition BKE_node.hh:250
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)