UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ImageBuilder.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "MathUtil.h"
6#include "VectorTypes.h"
9
10namespace UE
11{
12namespace Geometry
13{
14
16{
17 Clamp,
18 Wrap
19};
20
24template<typename PixelType>
26{
27protected:
30
31public:
32
38
40 {
41 return Dimensions;
42 }
43
47 void Clear(const PixelType& ClearValue)
48 {
49 Image.AssignAll(ClearValue);
50 }
51
56 {
57 return X >= 0 && Y >= 0 && X < Dimensions.GetWidth() && Y < Dimensions.GetHeight();
58 }
59
64 {
65 return ImageCoords.X >= 0 && ImageCoords.Y >= 0 &&
67 }
68
72 PixelType& GetPixel(int32 X, int32 Y)
73 {
74 int64 LinearIndex = Dimensions.GetIndex(FVector2i(X, Y));
75 return Image[LinearIndex];
76 }
77
81 PixelType& GetPixel(const FVector2i& ImageCoords)
82 {
83 int64 LinearIndex = Dimensions.GetIndex(ImageCoords);
84 return Image[LinearIndex];
85 }
86
90 PixelType& GetPixel(int64 LinearIndex)
91 {
92 return Image[LinearIndex];
93 }
94
98 const PixelType& GetPixel(int32 X, int32 Y) const
99 {
100 int64 LinearIndex = Dimensions.GetIndex(FVector2i(X,Y));
101 return Image[LinearIndex];
102 }
103
107 const PixelType& GetPixel(const FVector2i& ImageCoords) const
108 {
109 int64 LinearIndex = Dimensions.GetIndex(ImageCoords);
110 return Image[LinearIndex];
111 }
112
116 const PixelType& GetPixel(int64 LinearIndex) const
117 {
118 return Image[LinearIndex];
119 }
120
126
132
136 void SetPixel(int32 X, int32 Y, const PixelType& NewValue)
137 {
138 int64 LinearIndex = Dimensions.GetIndex(FVector2i(X,Y));
139 Image[LinearIndex] = NewValue;
140 }
141
145 void SetPixel(const FVector2i& ImageCoords, const PixelType& NewValue)
146 {
147 int64 LinearIndex = Dimensions.GetIndex(ImageCoords);
148 Image[LinearIndex] = NewValue;
149 }
150
151
155 void SetPixel(int64 LinearIndex, const PixelType& NewValue)
156 {
157 Image[LinearIndex] = NewValue;
158 }
159
160
165 {
166 Image[ToLinearIndex] = Image[FromLinearIndex];
167 }
168
169
173 template<typename OtherType>
174 void Convert(TFunctionRef<OtherType(const PixelType&)> ConvertFunc,
176 {
177 ConvertedImageOut.SetDimensions(Dimensions);
179 for (int64 k = 0; k < NumPixels; ++k)
180 {
181 ConvertedImageOut.SetPixel(k, ConvertFunc(Image[k]));
182 }
183 }
184
185
190 template<typename ScalarType, EImageTilingMethod TilingMethod=EImageTilingMethod::Clamp>
191 PixelType BilinearSample(const FVector2d& PixelCoords, const PixelType& InvalidValue) const
192 {
194 if (PixelCoords.X < 0.0 || PixelCoords.X > Max.X || PixelCoords.Y < 0.0 || PixelCoords.Y > Max.Y)
195 {
196 return InvalidValue;
197 }
198
199 // Compute bounding pixel corners
204
205 // Compute interpolation factors in [0,1] range.
206 // This must be done before wrapped tiling to ensure the [0,1] range.
207 // Rather than transforming the two pixel corners (XY0,XY1), we shift
208 // our PixelCoords by half a pixel to account for pixel centers.
209 const FVector2d A = PixelCoords - FVector2d(0.5) - XY0d;
211
212 if constexpr (TilingMethod == EImageTilingMethod::Wrap)
213 {
214 XY0.X = XY0.X < 0 ? XY0.X + Max.X : XY0.X;
215 XY0.Y = XY0.Y < 0 ? XY0.Y + Max.Y : XY0.Y;
216 XY1.X = XY1.X >= Max.X ? 0 : XY1.X;
217 XY1.Y = XY1.Y >= Max.Y ? 0 : XY1.Y;
218 }
219 else // TilingMethod == EImageTilingMethod::Clamp
220 {
221 XY0.X = FMath::Max(0, XY0.X);
222 XY0.Y = FMath::Max(0, XY0.Y);
223 XY1.X = FMath::Min(Max.X - 1, XY1.X);
224 XY1.Y = FMath::Min(Max.Y - 1, XY1.Y);
225 }
226
227 PixelType V00 = GetPixel(XY0);
228 PixelType V10 = GetPixel(FVector2i(XY1.X, XY0.Y));
229 PixelType V01 = GetPixel(FVector2i(XY0.X, XY1.Y));
230 PixelType V11 = GetPixel(XY1);
231
232 return V00 * static_cast<ScalarType>(OneMinusA.X * OneMinusA.Y) +
233 V01 * static_cast<ScalarType>(OneMinusA.X * A.Y) +
234 V10 * static_cast<ScalarType>(A.X * OneMinusA.Y) +
235 V11 * static_cast<ScalarType>(A.X * A.Y);
236 }
237
238
242 template<typename ScalarType, EImageTilingMethod TilingMethod=EImageTilingMethod::Clamp>
243 PixelType BilinearSampleUV(const FVector2d& UVCoords, const PixelType& InvalidValue) const
244 {
246
248 UV.X * (double)Dimensions.GetWidth(),
249 UV.Y * (double)Dimensions.GetHeight());
250
252 }
253
257 template<EImageTilingMethod TilingMethod=EImageTilingMethod::Clamp>
258 PixelType NearestSampleUV(const FVector2d& UVCoords) const
259 {
261 const int X = (int)(UV.X * (double)Dimensions.GetWidth());
262 const int Y = (int)(UV.Y * (double)Dimensions.GetHeight());
263 return GetPixel(X, Y);
264 }
265
266
270 bool IsConstantValue() const
271 {
273 if (Num >= 1)
274 {
275 PixelType InitialValue = Image[0];
276 for (int64 k = 1; k < Num; ++k)
277 {
278 if (Image[k] != InitialValue)
279 {
280 return false;
281 }
282 }
283 }
284 return true;
285 }
286
287
296 const PixelType& ZeroValue,
297 TFunctionRef<PixelType(PixelType, int)> AverageFunc) const
298 {
300 int32 Width = Dimensions.GetWidth();
301 int32 Height = Dimensions.GetHeight();
302
303 // can only fast-downsample by even multiple of image size
304 if (ensure((Width % SubSteps == 0) && (Height % SubSteps == 0)) == false)
305 {
306 DownsampleImage = *this;
307 return DownsampleImage;
308 }
309
310 int32 SubWidth = Width / SubSteps;
311 int32 SubHeight = Height / SubSteps;
312
314 DownsampleImage.SetDimensions(SubDimensions);
315
317 {
319 for (int32 xi = 0; xi < SubWidth; ++xi)
320 {
322
323 PixelType AccumBasePixels = ZeroValue;
324 for (int32 dy = 0; dy < SubSteps; ++dy)
325 {
326 for (int32 dx = 0; dx < SubSteps; ++dx)
327 {
329 }
330 }
332 DownsampleImage.SetPixel(xi, yi, SubPixel);
333 }
334 });
335
336 return DownsampleImage;
337 }
338
339protected:
340 template<EImageTilingMethod TilingMethod>
342 {
344 if constexpr (TilingMethod == EImageTilingMethod::Wrap)
345 {
347 }
348 else // TilingMethod == EImageTilingMethod::Clamp
349 {
350 UV.X = FMathd::Clamp(UV.X, 0.0, 1.0);
351 UV.Y = FMathd::Clamp(UV.Y, 0.0, 1.0);
352 }
353 return UV;
354 }
355
356};
357
358
367{
368public:
369
370 enum class EImageType
371 {
372 Float1,
373 Float3,
374 Float4
375 };
376
382
388
394
396 {
397 switch (ImageType)
398 {
400 Image1f->SetDimensions(Dimensions); break;
402 Image3f->SetDimensions(Dimensions); break;
404 Image4f->SetDimensions(Dimensions); break;
405 }
406 }
407
409 {
410 switch (ImageType)
411 {
413 return Image1f->GetDimensions();
415 return Image3f->GetDimensions();
417 return Image4f->GetDimensions();
418 }
419 return FImageDimensions();
420 }
421
423 {
424 switch (ImageType)
425 {
428 break;
431 break;
433 Image4f->SetPixel(PixelCoords, FloatPixel);
434 break;
435 }
436 }
437
439 {
440 switch (ImageType)
441 {
444 break;
447 break;
450 break;
451 }
452 }
453
454 FVector4f GetPixel(int64 LinearIndex) const
455 {
456 switch (ImageType)
457 {
459 {
460 float Value = Image1f->GetPixel(LinearIndex);
461 return FVector4f(Value, Value, Value, 1.0f);
462 }
464 {
465 FVector3f Value = Image3f->GetPixel(LinearIndex);
466 return FVector4f(Value.X, Value.Y, Value.Z, 1.0f);
467 }
469 return Image4f->GetPixel(LinearIndex);
470 }
471 return FVector4f::One();
472 }
473
475 {
476 switch (ImageType)
477 {
479 {
481 return FVector4f(Value, Value, Value, 1.0f);
482 }
484 {
485 FVector3f Value = Image3f->GetPixel(PixelCoords);
486 return FVector4f(Value.X, Value.Y, Value.Z, 1.0f);
487 }
489 return Image4f->GetPixel(PixelCoords);
490 }
491 return FVector4f::One();
492 }
493
494
495protected:
497
501};
502
503
504
505} // end namespace UE::Geometry
506} // end namespace UE
#define ensure( InExpression)
Definition AssertionMacros.h:464
void ParallelFor(int32 Num, TFunctionRef< void(int32)> Body, bool bForceSingleThread, bool bPumpRenderingThread=false)
Definition ParallelFor.h:481
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define X(Name, Desc)
Definition FormatStringSan.h:47
UE::Math::TVector< float > FVector3f
Definition MathFwd.h:73
UE::Math::TVector2< double > FVector2d
Definition MathFwd.h:61
UE::Math::TVector4< float > FVector4f
Definition MathFwd.h:75
@ Num
Definition MetalRHIPrivate.h:234
Definition AssetRegistryState.h:50
static RealType Clamp(const RealType Value, const RealType ClampMin, const RealType ClampMax)
Definition MathUtil.h:222
static RealType Floor(const RealType Value)
Definition MathUtil.h:384
Definition ImageBuilder.h:367
TImageBuilder< FVector3f > * Image3f
Definition ImageBuilder.h:499
FVector4f GetPixel(const FVector2i &PixelCoords) const
Definition ImageBuilder.h:474
EImageType
Definition ImageBuilder.h:371
void SetDimensions(FImageDimensions Dimensions)
Definition ImageBuilder.h:395
void SetPixel(const FVector2i &PixelCoords, const FVector4f &FloatPixel)
Definition ImageBuilder.h:422
void SetPixel(const FVector2i &PixelCoords, const FLinearColor &FloatPixel)
Definition ImageBuilder.h:438
FImageDimensions GetDimensions() const
Definition ImageBuilder.h:408
TImageBuilder< float > * Image1f
Definition ImageBuilder.h:498
FImageAdapter(TImageBuilder< float > *Image)
Definition ImageBuilder.h:377
FImageAdapter(TImageBuilder< FVector4f > *Image)
Definition ImageBuilder.h:389
FVector4f GetPixel(int64 LinearIndex) const
Definition ImageBuilder.h:454
EImageType ImageType
Definition ImageBuilder.h:496
TImageBuilder< FVector4f > * Image4f
Definition ImageBuilder.h:500
FImageAdapter(TImageBuilder< FVector3f > *Image)
Definition ImageBuilder.h:383
Definition ImageDimensions.h:18
int32 GetHeight() const
Definition ImageDimensions.h:40
int64 Num() const
Definition ImageDimensions.h:42
int32 GetWidth() const
Definition ImageDimensions.h:38
int64 GetIndex(int32 X, int32 Y) const
Definition ImageDimensions.h:68
Definition DenseGrid2.h:23
void AssignAll(ElemType Value)
Definition DenseGrid2.h:78
TArray64< ElemType > & GridValues()
Definition DenseGrid2.h:138
void Resize(int32 DimX, int32 DimY, EAllowShrinking AllowShrinking=EAllowShrinking::Default)
Definition DenseGrid2.h:66
Definition ImageBuilder.h:26
PixelType BilinearSample(const FVector2d &PixelCoords, const PixelType &InvalidValue) const
Definition ImageBuilder.h:191
void Convert(TFunctionRef< OtherType(const PixelType &)> ConvertFunc, TImageBuilder< OtherType > &ConvertedImageOut) const
Definition ImageBuilder.h:174
PixelType BilinearSampleUV(const FVector2d &UVCoords, const PixelType &InvalidValue) const
Definition ImageBuilder.h:243
bool ContainsPixel(const FVector2i &ImageCoords) const
Definition ImageBuilder.h:63
void SetPixel(int64 LinearIndex, const PixelType &NewValue)
Definition ImageBuilder.h:155
TArrayView64< PixelType > GetImageBuffer()
Definition ImageBuilder.h:121
static FVector2d GetTiledUV(const FVector2d &UVCoords)
Definition ImageBuilder.h:341
void SetPixel(const FVector2i &ImageCoords, const PixelType &NewValue)
Definition ImageBuilder.h:145
void Clear(const PixelType &ClearValue)
Definition ImageBuilder.h:47
const PixelType & GetPixel(const FVector2i &ImageCoords) const
Definition ImageBuilder.h:107
PixelType NearestSampleUV(const FVector2d &UVCoords) const
Definition ImageBuilder.h:258
const FImageDimensions & GetDimensions() const
Definition ImageBuilder.h:39
TConstArrayView64< PixelType > GetImageBuffer() const
Definition ImageBuilder.h:127
const PixelType & GetPixel(int64 LinearIndex) const
Definition ImageBuilder.h:116
PixelType & GetPixel(int32 X, int32 Y)
Definition ImageBuilder.h:72
TImageBuilder< PixelType > FastDownsample(int32 SubSteps, const PixelType &ZeroValue, TFunctionRef< PixelType(PixelType, int)> AverageFunc) const
Definition ImageBuilder.h:294
PixelType & GetPixel(int64 LinearIndex)
Definition ImageBuilder.h:90
TDenseGrid2< PixelType > Image
Definition ImageBuilder.h:29
PixelType & GetPixel(const FVector2i &ImageCoords)
Definition ImageBuilder.h:81
void CopyPixel(int64 FromLinearIndex, int64 ToLinearIndex)
Definition ImageBuilder.h:164
void SetDimensions(FImageDimensions DimensionsIn)
Definition ImageBuilder.h:33
void SetPixel(int32 X, int32 Y, const PixelType &NewValue)
Definition ImageBuilder.h:136
const PixelType & GetPixel(int32 X, int32 Y) const
Definition ImageBuilder.h:98
bool ContainsPixel(int32 X, int32 Y) const
Definition ImageBuilder.h:55
FImageDimensions Dimensions
Definition ImageBuilder.h:28
bool IsConstantValue() const
Definition ImageBuilder.h:270
EImageTilingMethod
Definition ImageBuilder.h:16
Definition AdvancedWidgetsModule.cpp:13
Definition Color.h:48
Definition BlockCodingHelpers.cpp:66
Definition IntVectorTypes.h:20
static TVector2< double > One()
Definition Vector2D.h:80
T X
Definition Vector2D.h:49
static TVector4< float > One()
Definition Vector4.h:185