Face Engine SDK 5.30.2
A face detection, recognition and tracking engine.
Loading...
Searching...
No Matches
Image.h
1#pragma once
2
3#include <fsdk/Def.h>
4#include <fsdk/Types/Format.h>
5#include <fsdk/Types/Rect.h>
6#include <fsdk/Types/Result.h>
7#include <fsdk/Types/RotationType.h>
8#include <fsdk/Types/Sizer.h>
9#include <fsdk/Types/SubImage.h>
10
11namespace fsdk {
12
13 struct IArchive;
14
38 struct Image {
39
43 enum class Type : uint8_t {
44 BMP,
45 JPG,
46 PNG,
47 PPM,
48 TIFF,
49 Unknown
50 };
51
55 enum class TargetDevice {
56 CPU,
57 GPU,
58 NPU,
59 };
60
102
108 enum class MemoryResidence : uint32_t {
109 MemoryCPU,
110 MemoryGPU,
111 MemoryNPU,
113 };
114
117 FSDK_API Image() noexcept;
118
130 int width,
131 int height,
132 Format format,
134 int deviceId = 0) noexcept;
135
149 int width,
150 int height,
151 Format format,
152 const void* data,
154 int deviceId = 0) noexcept;
155
174 int width,
175 int height,
176 Format format,
177 void* data,
178 bool copy,
180 int deviceId = 0) noexcept;
181
186 FSDK_API Image(const Image& other) noexcept;
187
192 FSDK_API Image(Image&& other) noexcept;
193
199 FSDK_API explicit Image(const SubImage& subImage) noexcept;
200
201 FSDK_API ~Image();
202
218 int width,
219 int height,
220 Format format,
221 bool cleanup = true,
223 int deviceId = 0) noexcept;
224
241 int width,
242 int height,
243 Format format,
244 const void* data,
246 int deviceId = 0) noexcept;
247
268 int width,
269 int height,
270 Format format,
271 void* data,
272 bool copy,
274 int deviceId = 0) noexcept;
275
286 create(const fsdk::Image& source, MemoryResidence residence, int deviceId = 0) noexcept;
287
306 int width,
307 int height,
308 Format format,
309 const void* data,
311 int deviceId = 0) noexcept;
312
335 int width,
336 int height,
337 Format format,
338 void* data,
339 bool copy,
341 int deviceId = 0) noexcept;
342
348 FSDK_API Result<Error> set(const Image& other) noexcept;
349
355 FSDK_API Result<Error> set(const SubImage& subImage) noexcept;
356
369 FSDK_API SubImage map(int x, int y, int width, int height) const noexcept;
370
378 SubImage map(const Rect& rect) const noexcept {
379 return map(rect.x, rect.y, rect.width, rect.height);
380 }
381
391 SubImage map(const Size& size) const noexcept {
392 return map(Point2i(0, 0), size);
393 }
394
403 SubImage map(const Point2i& origin, const Size& size) const noexcept {
404 return map(origin.x, origin.y, size.x, size.y);
405 }
406
421 FSDK_API Image extract(int x, int y, int width, int height) const noexcept;
422
436 FSDK_API Result<Image::Error> extract(Image& dest, int x, int y, int width, int height) const noexcept;
437
449 FSDK_API Result<Image::Error> extract(Image& dest, const Rect& rect) const noexcept;
450
458 FSDK_API Result<Error> rescale(Image& dest, float scale) const noexcept;
459
469 Image extract(const Rect& rect) const noexcept {
470 return extract(rect.x, rect.y, rect.width, rect.height);
471 }
472
484 Image extract(const Size& size) const noexcept {
485 return extract(Point2i(0, 0), size);
486 }
487
498 Image extract(const Point2i& origin, const Size& size) const noexcept {
499 return extract(origin.x, origin.y, size.x, size.y);
500 }
501
510 guessTypeFromMemory(const void* data, const uint32_t sizeInBytes, Type& type) noexcept;
511
517 Image clone() const noexcept {
518 return {getWidth(), getHeight(), getFormat(), getData(), getMemoryResidence(), getDeviceId()};
519 }
520
540 Image& dest,
541 int x,
542 int y,
543 int width,
544 int height,
545 Format format,
546 TargetDevice device = TargetDevice::CPU) const noexcept;
547
563 Image& dest,
564 const Point2i& origin,
565 const Size& size,
566 Format format,
567 TargetDevice device = TargetDevice::CPU) const noexcept {
568 return convert(dest, origin.x, origin.y, size.x, size.y, format, device);
569 }
570
587 convert(Image& dest, const Size& size, Format format, TargetDevice device = TargetDevice::CPU)
588 const noexcept {
589 return convert(dest, Point2i(0, 0), size, format, device);
590 }
591
606 convert(Image& dest, const Rect& rect, Format format, TargetDevice device = TargetDevice::CPU)
607 const noexcept {
608 return convert(dest, rect.x, rect.y, rect.width, rect.height, format, device);
609 }
610
622 convert(Image& dest, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
623 return convert(dest, getRect(), format, device);
624 }
625
631 FSDK_API Image rescale(float scale) const noexcept;
632
644 const char* path,
645 fsdk::Image::ImageCompression additionalFlag =
646 fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept;
647
658 FSDK_API Result<Error> save(const char* path, const Format format) const noexcept;
659
671 Image::Type type,
672 IArchive* archive,
673 fsdk::Image::ImageCompression additionalFlag =
674 fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept;
675
688 saveToMemory(Image::Type type, IArchive* archive, const Format format) const noexcept;
689
702 FSDK_API Result<Error> load(const char* path) noexcept;
703
713 FSDK_API Result<Error> load(const char* path, const Format format) noexcept;
714
728 FSDK_API Result<Error> loadFromMemory(const void* data, const uint32_t sizeInBytes) noexcept;
729
739 FSDK_API Result<Error> rotate(Image& dest, RotationType rotationType) const noexcept;
740
753 loadFromMemory(const void* data, const uint32_t sizeInBytes, const Format format) noexcept;
754
771 loadFromMemoryOfType(const void* data, const uint32_t sizeInBytes, const Type type) noexcept;
772
785 const void* data,
786 const uint32_t sizeInBytes,
787 const Type type,
788 const Format format) noexcept;
789
793 Image& operator=(const Image& other) noexcept {
794 set(other);
795
796 return *this;
797 }
798
802 Image& operator=(Image&& other) noexcept {
803 if(this != &other) {
804 release();
805 swap(other);
806 }
807
808 return *this;
809 }
810
812 bool isNull() const noexcept {
813 return getData() == nullptr;
814 }
815
819 bool isValid() const noexcept {
820 return !isNull() && getHeight() > 0 && getWidth() > 0 && getFormat().isValid();
821 }
822
826 operator bool() const noexcept {
827 return isValid();
828 }
829
834 FSDK_API void* getScanLine(int y) noexcept;
835
840 FSDK_API const void* getScanLine(int y) const noexcept;
841
844 FSDK_API int getDataSize() const noexcept;
845
849 FSDK_API void getDataSize(Sizer& sizer) const noexcept;
850
855 template <typename T>
856 T* getScanLineAs(int y) noexcept {
857 return reinterpret_cast<T*>(getScanLine(y));
858 }
859
864 template <typename T>
865 const T* getScanLineAs(int y) const noexcept {
866 return reinterpret_cast<const T*>(getScanLine(y));
867 }
868
871 void* getData() noexcept {
872 return m_data;
873 }
874
877 const void* getData() const noexcept {
878 return m_data;
879 }
880
883 template <typename T>
884 T* getDataAs() noexcept {
885 return reinterpret_cast<T*>(getData());
886 }
887
890 template <typename T>
891 const T* getDataAs() const noexcept {
892 return reinterpret_cast<const T*>(getData());
893 }
894
896 int getRowSize() const noexcept {
897 return getFormat().computePitch(getWidth());
898 }
899
901 int getWidth() const noexcept {
902 return m_width;
903 }
904
906 int getHeight() const noexcept {
907 return m_height;
908 }
909
911 float getAspectRatio() const noexcept {
912 return static_cast<float>(getWidth()) / static_cast<float>(getHeight());
913 }
914
916 Format getFormat() const noexcept {
917 return m_format;
918 }
919
920 MemoryResidence getMemoryResidence() const noexcept {
921 return m_residence;
922 }
923
924 int getDeviceId() const noexcept {
925 return m_deviceId;
926 }
927
929 Size getSize() const noexcept {
930 return Size(getWidth(), getHeight());
931 }
932
936 Rect getRect() const noexcept {
937 return Rect(0, 0, getWidth(), getHeight());
938 }
939
944 bool ownsData() const noexcept {
945 return !!m_ref;
946 }
947
952 bool isSharedWith(const Image& other) const noexcept {
953 return getData() == other.getData();
954 }
955
959 void swap(Image& other) noexcept {
960 std::swap(m_data, other.m_data);
961 std::swap(m_ref, other.m_ref);
962 std::swap(m_height, other.m_height);
963 std::swap(m_width, other.m_width);
964 std::swap(m_format, other.m_format);
965 std::swap(m_residence, other.m_residence);
966 }
967
970 void reset() noexcept {
971 Image().swap(*this);
972 }
973
979 bool equalWeak(const fsdk::Image& other) const noexcept {
980 return m_width == other.getWidth() && m_height == other.getHeight() && m_format == other.getFormat() &&
981 m_residence == other.getMemoryResidence();
982 }
983
989 bool equalStrong(const fsdk::Image& other) const noexcept {
990 return equalWeak(other) && m_data == other.getData();
991 }
992
993 FSDK_API void putPixel(uint32_t x, uint32_t y) noexcept;
994
995 protected:
996 void* m_data;
997 int* m_ref;
1002 MemoryResidence m_residence;
1003
1008 FSDK_API static void* allocate(int size) noexcept;
1009
1013 FSDK_API static void deallocate(void* memory) noexcept;
1014
1018 FSDK_API int retain() noexcept;
1019
1023 FSDK_API int release() noexcept;
1024
1028 FSDK_API int getRefCount() const noexcept;
1029 };
1030
1035 const char* toString(Image::MemoryResidence residence);
1036
1040 template <>
1042
1043 static bool isOk(Image::Error error) noexcept {
1044 return error == Image::Error::Ok;
1045 }
1046
1047 static const char* toString(Image::Error error) noexcept {
1048 switch(error) {
1049 case Image::Error::Ok:
1050 return "Ok";
1052 return "Unsupported type";
1054 return "Invalid path";
1056 return "Error during image saving";
1058 return "Error during image loading";
1060 return "Invalid image";
1062 return "Invalid image width";
1064 return "Invalid image height";
1066 return "Unsupported format";
1068 return "Memory error";
1070 return "Bitmap error";
1072 return "Archive error";
1074 return "Bad input data pointer";
1076 return "Bad input data size";
1078 return "Required conversion not implemented";
1080 return "Selected Target Device is not supported for this system";
1082 return "Error during initialization";
1084 return "Failed to retain image: it was released in another thread.";
1086 return "InvalidInput";
1087 default:
1088 return "Unknown error";
1089 }
1090 }
1091 };
1092
1097 using ImageError = Image::Error;
1100
1101} // namespace fsdk
#define FSDK_API
Dummy.
Definition Def.h:27
SDK namespace.
Definition IAGSEstimator.h:8
RotationType
Image rotation type.
Definition RotationType.h:10
Vector2< int > Size
Definition Vector2.h:303
Vector2< int > Point2i
Definition Vector2.h:294
const char * toString(Image::MemoryResidence residence)
Returns a string representation of a MemoryResidence type.
Definition Result.h:8
Image format.
Definition Format.h:11
bool isValid() const noexcept
Definition Format.h:253
int computePitch(int rowWidth) const noexcept
Compute row size in bytes.
Definition Format.h:200
Archive interface.
Definition IObject.h:38
Image.
Definition Image.h:38
Image extract(const Point2i &origin, const Size &size) const noexcept
Extract a sub image of this image. The new image will have it's own reference count.
Definition Image.h:498
int getRowSize() const noexcept
Definition Image.h:896
Result< Image::Error > convert(Image &dest, const Rect &rect, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition Image.h:606
bool equalWeak(const fsdk::Image &other) const noexcept
Performs weak comparison of this image with other. Weak in this context means only image parameters s...
Definition Image.h:979
float getAspectRatio() const noexcept
Definition Image.h:911
FSDK_API Result< Error > create(int width, int height, Format format, bool cleanup=true, MemoryResidence residence=MemoryResidence::MemoryCPU, int deviceId=0) noexcept
Initializes an empty image and preallocates storage buffer of given size.
int m_width
image width.
Definition Image.h:999
FSDK_API int release() noexcept
Decrease reference count.
const T * getDataAs() const noexcept
Definition Image.h:891
bool isSharedWith(const Image &other) const noexcept
Definition Image.h:952
void * getData() noexcept
Definition Image.h:871
FSDK_API Result< Image::Error > convert(Image &dest, int x, int y, int width, int height, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
void * m_data
raw image data.
Definition Image.h:996
static FSDK_API Result< Error > guessTypeFromMemory(const void *data, const uint32_t sizeInBytes, Type &type) noexcept
Guess type of image written into memory.
Format getFormat() const noexcept
Definition Image.h:916
bool equalStrong(const fsdk::Image &other) const noexcept
Performs strong comparison of this image with other. Beside weak comparison strong comparison also ch...
Definition Image.h:989
FSDK_API Result< Image::Error > extract(Image &dest, const Rect &rect) const noexcept
Extract a sub image of this image. The new image will have it's own reference count.
Image extract(const Rect &rect) const noexcept
Extract a sub image of this image. The new image will have it's own reference count.
Definition Image.h:469
FSDK_API Result< Error > load(const char *path, const Format format) noexcept
Load image from file and convert it to required format.
bool isValid() const noexcept
Definition Image.h:819
int * m_ref
reference counter. nullptr if image does not own data.
Definition Image.h:997
FSDK_API int getRefCount() const noexcept
Obtain reference count.
Result< Image::Error > convert(Image &dest, const Size &size, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition Image.h:587
FSDK_API Result< Error > loadFromMemoryOfType(const void *data, const uint32_t sizeInBytes, const Type type) noexcept
Load image from memory of specific type.
Image extract(const Size &size) const noexcept
Extract a sub image of this image. The new image will have it's own reference count.
Definition Image.h:484
Image & operator=(Image &&other) noexcept
Move other image.
Definition Image.h:802
Image clone() const noexcept
Create a copy of this image. The new image will have it's own reference count.
Definition Image.h:517
Rect getRect() const noexcept
Definition Image.h:936
T * getScanLineAs(int y) noexcept
Get image scanline data.
Definition Image.h:856
SubImage map(const Point2i &origin, const Size &size) const noexcept
Map image contents to a given area.
Definition Image.h:403
int getWidth() const noexcept
Definition Image.h:901
void swap(Image &other) noexcept
Swap contents with another image.
Definition Image.h:959
FSDK_API Image rescale(float scale) const noexcept
Rescale image keeping proportions.
FSDK_API void * getScanLine(int y) noexcept
Get image scanline data.
static FSDK_API void deallocate(void *memory) noexcept
Free memory.
FSDK_API Result< Error > loadFromMemoryOfType(const void *data, const uint32_t sizeInBytes, const Type type, const Format format) noexcept
Load image from memory of specific type and convert it to required format.
Error
Image error codes.
Definition Image.h:79
@ ReleasedInOtherThread
Failed to retain image: it was released in another thread.
@ FailedToLoad
Error during image loading.
@ InvalidDataSize
Bad input data size.
@ InvalidImage
Invalid image.
@ InvalidResidence
Invalid memory residence.
@ InvalidDevice
Target Device is not supported for this system.
@ InvalidArchive
Archive error.
@ InvalidDataPtr
Bad input data pointer.
@ InvalidPath
Bad path for saving / loading.
@ InvalidWidth
Invalid width.
@ FailedToSave
Error during image saving.
@ InvalidMemory
Error at memory opening.
@ InvalidConversion
Conversion not implemented.
@ InvalidHeight
Invalid height.
@ InvalidBitmap
Bitmap error occurred.
@ InvalidType
Unsupported type.
@ InvalidInput
Invalid input.
@ InvalidFormat
Unsupported format.
@ FailedToInitialize
Error during initialization.
@ InvalidCudaKernel
Error during apply cuda kernel.
static FSDK_API void * allocate(int size) noexcept
Allocate memory.
const void * getData() const noexcept
Definition Image.h:877
int m_deviceId
id of the device where image memory is allocated
Definition Image.h:1000
Image & operator=(const Image &other) noexcept
Assign other image.
Definition Image.h:793
FSDK_API Result< Error > loadFromMemory(const void *data, const uint32_t sizeInBytes, const Format format) noexcept
Load image from memory and convert it to required format.
Result< Image::Error > convert(Image &dest, const Point2i &origin, const Size &size, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition Image.h:562
TargetDevice
Target device that fsdk::Image algorithms will be running on.
Definition Image.h:55
FSDK_API Result< Error > rescale(Image &dest, float scale) const noexcept
Rescale image keeping proportions.
T * getDataAs() noexcept
Definition Image.h:884
FSDK_API Result< Error > set(int width, int height, Format format, const void *data, MemoryResidence residence=MemoryResidence::MemoryCPU, int deviceId=0) noexcept
Initializes an image with provided data. If current image is not empty and it's size and format match...
bool isNull() const noexcept
Definition Image.h:812
const T * getScanLineAs(int y) const noexcept
Get image scanline data.
Definition Image.h:865
FSDK_API Result< Image::Error > extract(Image &dest, int x, int y, int width, int height) const noexcept
Extract a sub image of this image. The new image will have it's own reference count.
SubImage map(const Size &size) const noexcept
Map image contents to a given area.
Definition Image.h:391
int m_height
image height.
Definition Image.h:998
Result< Image::Error > convert(Image &dest, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition Image.h:622
FSDK_API int retain() noexcept
Increase reference count.
void reset() noexcept
Reset image contents.
Definition Image.h:970
FSDK_API Result< Image::Error > save(const char *path, fsdk::Image::ImageCompression additionalFlag=fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept
Save image.
FSDK_API Result< Image::Error > saveToMemory(Image::Type type, IArchive *archive, const Format format) const noexcept
Convert and save image to memory buffer.
FSDK_API const void * getScanLine(int y) const noexcept
Get image scanline data.
MemoryResidence
Memory residence.
Definition Image.h:108
@ MemoryNPU_DPP
Image in NPU PreProcessing module memory.
@ MemoryCPU
Image in Host device memory.
@ MemoryNPU
Image in NPU device memory.
@ MemoryGPU
Image in Cuda device memory.
Size getSize() const noexcept
Definition Image.h:929
FSDK_API Result< Error > save(const char *path, const Format format) const noexcept
Convert and Save image.
ImageCompression
Supported compression type is used only for jpg and png types.
Definition Image.h:68
@ IC_HARD_COMPRESSION
more than medium compression (only for png or jpg)
@ IC_NO_COMPRESSION
no compression (only for png or jpg image)
@ IC_BEST_COMPRESSION
maximum compression level (only for png or jpg)
@ IC_MEDIUM_COMPRESSION
medium compression (only for png or jpg)
FSDK_API Image() noexcept
Initializes an empty image.
FSDK_API Result< Error > load(const char *path) noexcept
Load image from file.
int getHeight() const noexcept
Definition Image.h:906
Type
Supported image types.
Definition Image.h:43
FSDK_API int getDataSize() const noexcept
FSDK_API Result< Error > rotate(Image &dest, RotationType rotationType) const noexcept
Rotates an image on 90, 180 and 270 degrees. Allocates a new buffer of given size and fills image con...
FSDK_API Image extract(int x, int y, int width, int height) const noexcept
Extract a sub image of this image. The new image will have it's own reference count.
bool ownsData() const noexcept
Definition Image.h:944
FSDK_API SubImage map(int x, int y, int width, int height) const noexcept
Map image contents to a given area.
Format m_format
image format (
Definition Image.h:1001
FSDK_API Result< Error > loadFromMemory(const void *data, const uint32_t sizeInBytes) noexcept
Load image from memory.
FSDK_API Result< Image::Error > saveToMemory(Image::Type type, IArchive *archive, fsdk::Image::ImageCompression additionalFlag=fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept
Save image to memory buffer.
A structure that encapsulates an action result enumeration.
Definition Result.h:27
Helper entity to measure size of dynamic objects in memory.
Definition Sizer.h:9
Sub Image.
Definition SubImage.h:13