Face Engine SDK  5.27.0
A face detection, recognition and tracking engine.
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 
11 namespace 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 
68  enum class ImageCompression {
75  };
79  enum class Error : uint32_t {
80  Ok,
81  InvalidWidth,
85  InvalidImage,
87  InvalidPath,
88  InvalidType,
96  FailedToSave,
97  FailedToLoad,
99  InvalidInput,
101  };
102 
108  enum class MemoryResidence : uint32_t {
109  MemoryCPU,
110  MemoryGPU,
111  MemoryNPU,
112  MemoryNPU_DPP,
113  };
114 
117  FSDK_API Image() noexcept;
118 
129  FSDK_API Image(
130  int width,
131  int height,
132  Format format,
133  MemoryResidence residence = MemoryResidence::MemoryCPU,
134  int deviceId = 0) noexcept;
135 
148  FSDK_API Image(
149  int width,
150  int height,
151  Format format,
152  const void* data,
153  MemoryResidence residence = MemoryResidence::MemoryCPU,
154  int deviceId = 0) noexcept;
155 
173  FSDK_API Image(
174  int width,
175  int height,
176  Format format,
177  void* data,
178  bool copy,
179  MemoryResidence residence = MemoryResidence::MemoryCPU,
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,
222  MemoryResidence residence = MemoryResidence::MemoryCPU,
223  int deviceId = 0) noexcept;
224 
241  int width,
242  int height,
243  Format format,
244  const void* data,
245  MemoryResidence residence = MemoryResidence::MemoryCPU,
246  int deviceId = 0) noexcept;
247 
268  int width,
269  int height,
270  Format format,
271  void* data,
272  bool copy,
273  MemoryResidence residence = MemoryResidence::MemoryCPU,
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,
310  MemoryResidence residence = MemoryResidence::MemoryCPU,
311  int deviceId = 0) noexcept;
312 
335  int width,
336  int height,
337  Format format,
338  void* data,
339  bool copy,
340  MemoryResidence residence = MemoryResidence::MemoryCPU,
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 
509  FSDK_API static Result<Error>
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 =
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 =
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;
998  int m_height;
999  int m_width;
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
Error at memory opening.
medium compression (only for png or jpg)
Error during apply cuda kernel.
#define FSDK_API
Dummy.
Definition: Def.h:27
SDK namespace.
Definition: IAGSEstimator.h:8
FSDK_API Result< Error > loadFromMemory(const void *data, const uint32_t sizeInBytes) noexcept
Load image from memory.
float getAspectRatio() const noexcept
Definition: Image.h:911
A structure that encapsulates an action result enumeration.
Definition: Result.h:27
void reset() noexcept
Reset image contents.
Definition: Image.h:970
static FSDK_API void deallocate(void *memory) noexcept
Free memory.
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
Vector2< int > Point2i
Definition: Vector2.h:294
Rect getRect() const noexcept
Definition: Image.h:936
Image in NPU PreProcessing module memory.
int m_height
image height.
Definition: Image.h:998
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.
maximum compression level (only for png or jpg)
int computePitch(int rowWidth) const noexcept
Compute row size in bytes.
Definition: Format.h:200
bool isValid() const noexcept
Definition: Format.h:253
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
Image format.
Definition: Format.h:11
bool isNull() const noexcept
Definition: Image.h:812
Bad path for saving / loading.
FSDK_API Result< Error > load(const char *path) noexcept
Load image from file.
void * getData() noexcept
Definition: Image.h:871
Format getFormat() const noexcept
Definition: Image.h:916
const void * getData() const noexcept
Definition: Image.h:877
const T * getDataAs() const noexcept
Definition: Image.h:891
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.
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&#39;s size and format match...
int getRowSize() const noexcept
Definition: Image.h:896
T * getScanLineAs(int y) noexcept
Get image scanline data.
Definition: Image.h:856
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&#39;s own reference count.
RotationType
Image rotation type.
Definition: RotationType.h:10
Definition: Result.h:8
Bitmap error occurred.
MemoryResidence
Memory residence.
Definition: Image.h:108
int getHeight() const noexcept
Definition: Image.h:906
Conversion not implemented.
Format m_format
image format (
Definition: Image.h:1001
Common SDK definitions.
Invalid memory residence.
SubImage map(const Size &size) const noexcept
Map image contents to a given area.
Definition: Image.h:391
bool isSharedWith(const Image &other) const noexcept
Definition: Image.h:952
void * m_data
raw image data.
Definition: Image.h:996
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 > 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_ref
reference counter. nullptr if image does not own data.
Definition: Image.h:997
const T * getScanLineAs(int y) const noexcept
Get image scanline data.
Definition: Image.h:865
FSDK_API int getRefCount() const noexcept
Obtain reference count.
Error during image saving.
int m_deviceId
id of the device where image memory is allocated
Definition: Image.h:1000
FSDK_API SubImage map(int x, int y, int width, int height) const noexcept
Map image contents to a given area.
Image extract(const Point2i &origin, const Size &size) const noexcept
Extract a sub image of this image. The new image will have it&#39;s own reference count.
Definition: Image.h:498
static FSDK_API void * allocate(int size) noexcept
Allocate memory.
Type
Supported image types.
Definition: Image.h:43
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
void swap(Image &other) noexcept
Swap contents with another image.
Definition: Image.h:959
Error during image loading.
Failed to retain image: it was released in another thread.
int m_width
image width.
Definition: Image.h:999
Image in Cuda device memory.
ImageCompression
Supported compression type is used only for jpg and png types.
Definition: Image.h:68
Image.
Definition: Image.h:38
T * getDataAs() noexcept
Definition: Image.h:884
FSDK_API int getDataSize() const noexcept
Target Device is not supported for this system.
Image & operator=(const Image &other) noexcept
Assign other image.
Definition: Image.h:793
FSDK_API int release() noexcept
Decrease reference count.
Image in Host device memory.
no compression (only for png or jpg image)
Result< Image::Error > convert(Image &dest, const Rect &rect, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition: Image.h:606
Archive interface.
Definition: IObject.h:38
SubImage map(const Point2i &origin, const Size &size) const noexcept
Map image contents to a given area.
Definition: Image.h:403
const char * toString(Image::MemoryResidence residence)
Returns a string representation of a MemoryResidence type.
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...
Image extract(const Size &size) const noexcept
Extract a sub image of this image. The new image will have it&#39;s own reference count.
Definition: Image.h:484
Bad input data pointer.
FSDK_API Result< Error > loadFromMemoryOfType(const void *data, const uint32_t sizeInBytes, const Type type) noexcept
Load image from memory of specific type.
FSDK_API void * getScanLine(int y) noexcept
Get image scanline data.
Error
Image error codes.
Definition: Image.h:79
bool ownsData() const noexcept
Definition: Image.h:944
Sub Image.
Definition: SubImage.h:13
Result< Image::Error > convert(Image &dest, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition: Image.h:622
FSDK_API Image() noexcept
Initializes an empty image.
more than medium compression (only for png or jpg)
Error during initialization.
Image clone() const noexcept
Create a copy of this image. The new image will have it&#39;s own reference count.
Definition: Image.h:517
FSDK_API Result< Image::Error > save(const char *path, fsdk::Image::ImageCompression additionalFlag=fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept
Save image.
TargetDevice
Target device that fsdk::Image algorithms will be running on.
Definition: Image.h:55
Image extract(const Rect &rect) const noexcept
Extract a sub image of this image. The new image will have it&#39;s own reference count.
Definition: Image.h:469
Helper entity to measure size of dynamic objects in memory.
Definition: Sizer.h:9
FSDK_API int retain() noexcept
Increase reference count.
Vector2< int > Size
Definition: Vector2.h:303
Size getSize() const noexcept
Definition: Image.h:929
Image & operator=(Image &&other) noexcept
Move other image.
Definition: Image.h:802
int getWidth() const noexcept
Definition: Image.h:901
Image in NPU device memory.
bool isValid() const noexcept
Definition: Image.h:819
FSDK_API Result< Error > rescale(Image &dest, float scale) const noexcept
Rescale image keeping proportions.
static FSDK_API Result< Error > guessTypeFromMemory(const void *data, const uint32_t sizeInBytes, Type &type) noexcept
Guess type of image written into memory.