Face Engine SDK  5.11.0
A face detection, recognition and tracking engine.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/Sizer.h>
8 #include <fsdk/Types/SubImage.h>
9 
10 namespace fsdk
11 {
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 
67  enum class ImageCompression {
73  };
77  enum class Error: uint32_t {
78  Ok,
79  InvalidWidth,
83  InvalidImage,
85  InvalidPath,
86  InvalidType,
93  FailedToSave,
94  FailedToLoad,
97  };
98 
104  enum class MemoryResidence: uint32_t {
105  MemoryCPU,
106  MemoryGPU,
107  MemoryNPU,
108  MemoryNPU_DPP,
109  };
110 
113  FSDK_API Image() noexcept;
114 
125  FSDK_API Image(
126  int width,
127  int height,
128  Format format,
129  MemoryResidence residence = MemoryResidence::MemoryCPU,
130  int deviceId = 0) noexcept;
131 
144  FSDK_API Image(
145  int width,
146  int height,
147  Format format,
148  const void* data,
149  MemoryResidence residence = MemoryResidence::MemoryCPU,
150  int deviceId = 0) noexcept;
151 
169  FSDK_API Image(
170  int width,
171  int height,
172  Format format,
173  void* data,
174  bool copy,
175  MemoryResidence residence = MemoryResidence::MemoryCPU,
176  int deviceId = 0) noexcept;
177 
182  FSDK_API Image(const Image& other) noexcept;
183 
188  FSDK_API Image(Image&& other) noexcept;
189 
195  FSDK_API explicit Image(const SubImage& subImage) noexcept;
196 
197  FSDK_API ~Image();
198 
214  int width,
215  int height,
216  Format format,
217  bool cleanup = true,
218  MemoryResidence residence = MemoryResidence::MemoryCPU,
219  int deviceId = 0) noexcept;
220 
237  int width,
238  int height,
239  Format format,
240  const void* data,
241  MemoryResidence residence = MemoryResidence::MemoryCPU,
242  int deviceId = 0) noexcept;
243 
264  int width,
265  int height,
266  Format format,
267  void* data,
268  bool copy,
269  MemoryResidence residence = MemoryResidence::MemoryCPU,
270  int deviceId = 0) noexcept;
271 
281  FSDK_API Result<Error> create(const fsdk::Image& source, MemoryResidence residence, int deviceId = 0) noexcept;
282 
301  int width,
302  int height,
303  Format format,
304  const void* data,
305  MemoryResidence residence = MemoryResidence::MemoryCPU,
306  int deviceId = 0) noexcept;
307 
330  int width,
331  int height,
332  Format format,
333  void* data,
334  bool copy,
335  MemoryResidence residence = MemoryResidence::MemoryCPU,
336  int deviceId = 0) noexcept;
337 
343  FSDK_API Result<Error> set(const Image& other) noexcept;
344 
350  FSDK_API Result<Error> set(const SubImage& subImage) noexcept;
351 
364  FSDK_API SubImage map(int x, int y, int width, int height) const noexcept;
365 
373  SubImage map(const Rect& rect) const noexcept {
374  return map(rect.x, rect.y, rect.width, rect.height);
375  }
376 
386  SubImage map(const Size& size) const noexcept {
387  return map(Point2i(0, 0), size);
388  }
389 
398  SubImage map(const Point2i& origin, const Size& size) const noexcept {
399  return map(origin.x, origin.y, size.x, size.y);
400  }
401 
415  FSDK_API Image extract(int x, int y, int width, int height) const noexcept;
416 
425  Image extract(const Rect& rect) const noexcept {
426  return extract(rect.x, rect.y, rect.width, rect.height);
427  }
428 
439  Image extract(const Size& size) const noexcept {
440  return extract(Point2i(0, 0), size);
441  }
442 
452  Image extract(const Point2i& origin, const Size& size) const noexcept {
453  return extract(origin.x, origin.y, size.x, size.y);
454  }
455 
463  FSDK_API static Result<Error> guessTypeFromMemory(const void* data, const uint32_t sizeInBytes, Type& type) noexcept;
464 
470  Image clone() const noexcept {
471  return {
472  getWidth(),
473  getHeight(),
474  getFormat(),
475  getData(),
476  getMemoryResidence(),
477  getDeviceId()
478  };
479  }
480 
499  FSDK_API Result<Image::Error> convert(Image& dest, int x, int y, int width, int height, Format format, TargetDevice device = TargetDevice::CPU) const noexcept;
500 
515  Result<Image::Error> convert(Image& dest, const Point2i& origin, const Size& size, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
516  return convert(dest, origin.x, origin.y, size.x, size.y, format, device);
517  }
518 
534  Result<Image::Error> convert(Image& dest, const Size& size, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
535  return convert(dest, Point2i(0, 0), size, format, device);
536  }
537 
551  Result<Image::Error> convert(Image& dest, const Rect& rect, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
552  return convert(dest, rect.x, rect.y, rect.width, rect.height, format, device);
553  }
554 
565  Result<Image::Error> convert(Image& dest, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
566  return convert(dest, getRect(), format, device);
567  }
568 
574  FSDK_API Image rescale(float scale) const noexcept;
575 
586  FSDK_API Result <Image::Error> save(const char* path,
587  fsdk::Image::ImageCompression additionalFlag =
589 
601  const char* path,
602  const Format format) const noexcept;
603 
615  saveToMemory(Image::Type type,
616  IArchive* archive,
617  fsdk::Image::ImageCompression additionalFlag = fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept;
618 
631  Image::Type type,
632  IArchive* archive,
633  const Format format) const noexcept;
634 
647  FSDK_API Result<Error> load(const char* path) noexcept;
648 
659  const char* path,
660  const Format format) noexcept;
661 
676  const void* data,
677  const uint32_t sizeInBytes) noexcept;
678 
690  const void* data,
691  const uint32_t sizeInBytes,
692  const Format format) noexcept;
693 
710  const void* data,
711  const uint32_t sizeInBytes,
712  const Type type) noexcept;
713 
726  const void* data,
727  const uint32_t sizeInBytes,
728  const Type type,
729  const Format format) noexcept;
730 
734  Image& operator = (const Image& other) noexcept {
735  set(other);
736 
737  return *this;
738  }
739 
743  Image& operator = (Image&& other) noexcept {
744  if(this != &other) {
745  release();
746  swap(other);
747  }
748 
749  return *this;
750  }
751 
753  bool isNull() const noexcept {
754  return getData() == nullptr;
755  }
756 
760  bool isValid() const noexcept {
761  return !isNull() &&
762  getHeight() > 0 &&
763  getWidth() > 0 &&
764  getFormat().isValid();
765  }
766 
770  operator bool () const noexcept {
771  return isValid();
772  }
773 
778  FSDK_API void* getScanLine(int y) noexcept;
779 
784  FSDK_API const void* getScanLine(int y) const noexcept;
785 
788  FSDK_API int getDataSize() const noexcept;
789 
793  FSDK_API void getDataSize(Sizer& sizer) const noexcept;
794 
799  template<typename T>
800  T* getScanLineAs(int y) noexcept {
801  return reinterpret_cast<T*>(getScanLine(y));
802  }
803 
808  template<typename T>
809  const T* getScanLineAs(int y) const noexcept {
810  return reinterpret_cast<const T*>(getScanLine(y));
811  }
812 
815  void* getData() noexcept {
816  return m_data;
817  }
818 
821  const void* getData() const noexcept {
822  return m_data;
823  }
824 
827  template<typename T>
828  T* getDataAs() noexcept {
829  return reinterpret_cast<T*>(getData());
830  }
831 
834  template<typename T>
835  const T* getDataAs() const noexcept {
836  return reinterpret_cast<const T*>(getData());
837  }
838 
840  int getRowSize() const noexcept {
841  return getFormat().computePitch(getWidth());
842  }
843 
845  int getWidth() const noexcept {
846  return m_width;
847  }
848 
850  int getHeight() const noexcept {
851  return m_height;
852  }
853 
855  float getAspectRatio() const noexcept {
856  return
857  static_cast<float>(getWidth()) /
858  static_cast<float>(getHeight());
859  }
860 
862  Format getFormat() const noexcept {
863  return m_format;
864  }
865 
866  MemoryResidence getMemoryResidence() const noexcept {
867  return m_residence;
868  }
869 
870  int getDeviceId() const noexcept {
871  return m_deviceId;
872  }
873 
875  Size getSize() const noexcept {
876  return Size(getWidth(), getHeight());
877  }
878 
882  Rect getRect() const noexcept {
883  return Rect(0, 0, getWidth(), getHeight());
884  }
885 
890  bool ownsData() const noexcept {
891  return !!m_ref;
892  }
893 
898  bool isSharedWith(const Image& other) const noexcept {
899  return getData() == other.getData();
900  }
901 
905  void swap(Image& other) noexcept {
906  std::swap(m_data, other.m_data);
907  std::swap(m_ref, other.m_ref);
908  std::swap(m_height, other.m_height);
909  std::swap(m_width, other.m_width);
910  std::swap(m_format, other.m_format);
911  std::swap(m_residence, other.m_residence);
912  }
913 
916  void reset() noexcept {
917  Image().swap(*this);
918  }
919 
925  bool equalWeak(const fsdk::Image& other) const noexcept {
926  return
927  m_width == other.getWidth() &&
928  m_height == other.getHeight() &&
929  m_format == other.getFormat() &&
930  m_residence == other.getMemoryResidence();
931  }
932 
938  bool equalStrong(const fsdk::Image& other) const noexcept {
939  return equalWeak(other) && m_data == other.getData();
940  }
941 
942 
943  FSDK_API void putPixel(uint32_t x, uint32_t y) noexcept;
944 
945  protected:
946  void* m_data;
947  int* m_ref;
948  int m_height;
949  int m_width;
952  MemoryResidence m_residence;
953 
958  FSDK_API static void* allocate(int size) noexcept;
959 
963  FSDK_API static void deallocate(void* memory) noexcept;
964 
968  FSDK_API int retain() noexcept;
969 
973  FSDK_API int release() noexcept;
974 
978  FSDK_API int getRefCount() const noexcept;
979  };
980 
984  template<>
986 
987  static bool isOk(Image::Error error) noexcept {
988  return error == Image::Error::Ok;
989  }
990 
991  static const char* toString (Image::Error error) noexcept {
992  switch(error) {
993  case Image::Error::Ok: return "Ok";
994  case Image::Error::InvalidType: return "Unsupported type";
995  case Image::Error::InvalidPath: return "Invalid path";
996  case Image::Error::FailedToSave: return "Error during image saving";
997  case Image::Error::FailedToLoad: return "Error during image loading";
998  case Image::Error::InvalidImage: return "Invalid image";
999  case Image::Error::InvalidWidth: return "Invalid image width";
1000  case Image::Error::InvalidHeight: return "Invalid image height";
1001  case Image::Error::InvalidFormat: return "Unsupported format";
1002  case Image::Error::InvalidMemory: return "Memory error";
1003  case Image::Error::InvalidBitmap: return "Bitmap error";
1004  case Image::Error::InvalidArchive: return "Archive error";
1005  case Image::Error::InvalidDataPtr: return "Bad input data pointer";
1006  case Image::Error::InvalidDataSize: return "Bad input data size";
1007  case Image::Error::InvalidConversion: return "Required conversion not implemented";
1008  case Image::Error::InvalidDevice: return "Selected Target Device is not supported for this system";
1009  case Image::Error::FailedToInitialize: return "Error during initialization";
1010  case Image::Error::ReleasedInOtherThread: return "Failed to retain image: it was released in another thread.";
1011  default: return "Unknown error";
1012  }
1013  }
1014  };
1015 
1020  using ImageError = Image::Error;
1023 
1024 }
Error at memory opening.
medium compression (only for png or jpg)
#define FSDK_API
Dummy.
Definition: Def.h:27
FSDK_API Result< Error > loadFromMemory(const void *data, const uint32_t sizeInBytes) noexcept
Load image from memory.
float getAspectRatio() const noexcept
Definition: Image.h:855
A structure that encapsulates an action result enumeration.
Definition: Result.h:29
void reset() noexcept
Reset image contents.
Definition: Image.h:916
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:938
Vector2< int > Point2i
Definition: Vector2.h:290
Rect getRect() const noexcept
Definition: Image.h:882
Image in NPU PreProcessing module memory.
Type height
Rectangle height.
Definition: Rect.h:16
int m_height
image height.
Definition: Image.h:948
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:121
bool isValid() const noexcept
Definition: Format.h:174
Image format.
Definition: Format.h:11
bool isNull() const noexcept
Definition: Image.h:753
Bad path for saving / loading.
FSDK_API Result< Error > load(const char *path) noexcept
Load image from file.
void * getData() noexcept
Definition: Image.h:815
Format getFormat() const noexcept
Definition: Image.h:862
const void * getData() const noexcept
Definition: Image.h:821
const T * getDataAs() const noexcept
Definition: Image.h:835
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:840
T * getScanLineAs(int y) noexcept
Get image scanline data.
Definition: Image.h:800
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.
FSDK_API Image rescale(float scale) const noexcept
Rescale image keeping proportions.
Definition: Result.h:9
Bitmap error occurred.
MemoryResidence
Memory residence.
Definition: Image.h:104
int getHeight() const noexcept
Definition: Image.h:850
Type width
Rectangle width.
Definition: Rect.h:15
Conversion not implemented.
Format m_format
image format (
Definition: Image.h:951
Common SDK definitions.
Invalid memory residence.
SubImage map(const Size &size) const noexcept
Map image contents to a given area.
Definition: Image.h:386
bool isSharedWith(const Image &other) const noexcept
Definition: Image.h:898
void * m_data
raw image data.
Definition: Image.h:946
Result< Image::Error > convert(Image &dest, const Size &size, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition: Image.h:534
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:947
const T * getScanLineAs(int y) const noexcept
Get image scanline data.
Definition: Image.h:809
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:950
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:452
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:925
void swap(Image &other) noexcept
Swap contents with another image.
Definition: Image.h:905
Error during image loading.
Failed to retain image: it was released in another thread.
int m_width
image width.
Definition: Image.h:949
Image in Cuda device memory.
ImageCompression
Supported compression type is used only for jpg and png types.
Definition: Image.h:67
Image.
Definition: Image.h:38
compression with minimal (or without) quality loss (only for png or jpg image)
T * getDataAs() noexcept
Definition: Image.h:828
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:734
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:551
Archive interface.
Definition: IObject.h:37
SubImage map(const Point2i &origin, const Size &size) const noexcept
Map image contents to a given area.
Definition: Image.h:398
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:439
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:77
bool ownsData() const noexcept
Definition: Image.h:890
Sub Image.
Definition: SubImage.h:10
Result< Image::Error > convert(Image &dest, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition: Image.h:565
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:470
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:425
Helper entity to measure size of dynamic objects in memory.
Definition: Sizer.h:10
FSDK_API int retain() noexcept
Increase reference count.
Vector2< int > Size
Definition: Vector2.h:299
Size getSize() const noexcept
Definition: Image.h:875
Type y
Upper left corner y-coordinate.
Definition: Rect.h:14
int getWidth() const noexcept
Definition: Image.h:845
Image in NPU device memory.
Type x
Upper left corner x-coordinate.
Definition: Rect.h:13
bool isValid() const noexcept
Definition: Image.h:760
static FSDK_API Result< Error > guessTypeFromMemory(const void *data, const uint32_t sizeInBytes, Type &type) noexcept
Guess type of image written into memory.