Face Engine SDK  4.9.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  };
59 
66  enum class ImageCompression {
72  };
76  enum class Error: uint32_t {
77  Ok,
78  InvalidWidth,
82  InvalidImage,
84  InvalidPath,
85  InvalidType,
92  FailedToSave,
93  FailedToLoad,
96  };
97 
103  enum class MemoryResidence: uint32_t {
104  MemoryCPU,
105  MemoryGPU
106  };
107 
110  FSDK_API Image() noexcept;
111 
120  FSDK_API Image(
121  int width,
122  int height,
123  Format format,
124  MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
125 
136  FSDK_API Image(
137  int width,
138  int height,
139  Format format,
140  const void* data,
141  MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
142 
158  FSDK_API Image(
159  int width,
160  int height,
161  Format format,
162  void* data,
163  bool copy,
164  MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
165 
170  FSDK_API Image(const Image& other) noexcept;
171 
176  FSDK_API Image(Image&& other) noexcept;
177 
183  FSDK_API explicit Image(const SubImage& subImage) noexcept;
184 
185  FSDK_API ~Image();
186 
197  FSDK_API Result<Error> create(int width, int height, Format format, bool cleanup = true, MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
198 
210  FSDK_API Result<Error> create(int width, int height, Format format, const void* data, MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
211 
227  FSDK_API Result<Error> create(int width, int height, Format format, void* data, bool copy, MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
228 
236  FSDK_API Result<Error> create(const fsdk::Image& source, MemoryResidence residence) noexcept;
237 
251  FSDK_API Result<Error> set(int width, int height, Format format, const void* data) noexcept;
252 
270  FSDK_API Result<Error> set(int width, int height, Format format, void* data, bool copy) noexcept;
271 
277  FSDK_API Result<Error> set(const Image& other) noexcept;
278 
284  FSDK_API Result<Error> set(const SubImage& subImage) noexcept;
285 
297  FSDK_API SubImage map(int x, int y, int width, int height) const noexcept;
298 
305  SubImage map(const Rect& rect) const noexcept {
306  return map(rect.x, rect.y, rect.width, rect.height);
307  }
308 
317  SubImage map(const Size& size) const noexcept {
318  return map(Point2i(0, 0), size);
319  }
320 
328  SubImage map(const Point2i& origin, const Size& size) const noexcept {
329  return map(origin.x, origin.y, size.x, size.y);
330  }
331 
344  FSDK_API Image extract(int x, int y, int width, int height) const noexcept;
345 
353  Image extract(const Rect& rect) const noexcept {
354  return extract(rect.x, rect.y, rect.width, rect.height);
355  }
356 
366  Image extract(const Size& size) const noexcept {
367  return extract(Point2i(0, 0), size);
368  }
369 
378  Image extract(const Point2i& origin, const Size& size) const noexcept {
379  return extract(origin.x, origin.y, size.x, size.y);
380  }
381 
389  FSDK_API static Result<Error> guessTypeFromMemory(const void* data, const uint32_t sizeInBytes, Type& type) noexcept;
390 
396  Image clone() const noexcept {
397  return Image(
398  getWidth(),
399  getHeight(),
400  getFormat(),
401  getData());
402  }
403 
422  FSDK_API Result<Image::Error> convert(Image& dest, int x, int y, int width, int height, Format format, TargetDevice device = TargetDevice::CPU) const noexcept;
423 
438  Result<Image::Error> convert(Image& dest, const Point2i& origin, const Size& size, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
439  return convert(dest, origin.x, origin.y, size.x, size.y, format, device);
440  }
441 
457  Result<Image::Error> convert(Image& dest, const Size& size, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
458  return convert(dest, Point2i(0, 0), size, format, device);
459  }
460 
474  Result<Image::Error> convert(Image& dest, const Rect& rect, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
475  return convert(dest, rect.x, rect.y, rect.width, rect.height, format, device);
476  }
477 
488  Result<Image::Error> convert(Image& dest, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
489  return convert(dest, getRect(), format, device);
490  }
491 
497  FSDK_API Image rescale(float scale) const noexcept;
498 
508  FSDK_API Result <Image::Error> save(const char* path,
509  fsdk::Image::ImageCompression additionalFlag =
511 
522  const char* path,
523  const Format format) const noexcept;
524 
535  saveToMemory(Image::Type type,
536  IArchive* archive,
537  fsdk::Image::ImageCompression additionalFlag = fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept;
538 
550  Image::Type type,
551  IArchive* archive,
552  const Format format) const noexcept;
553 
565  FSDK_API Result<Error> load(const char* path) noexcept;
566 
576  const char* path,
577  const Format format) noexcept;
578 
592  const void* data,
593  const uint32_t sizeInBytes) noexcept;
594 
605  const void* data,
606  const uint32_t sizeInBytes,
607  const Format format) noexcept;
608 
624  const void* data,
625  const uint32_t sizeInBytes,
626  const Type type) noexcept;
627 
639  const void* data,
640  const uint32_t sizeInBytes,
641  const Type type,
642  const Format format) noexcept;
643 
647  Image& operator = (const Image& other) noexcept {
648  set(other);
649 
650  return *this;
651  }
652 
656  Image& operator = (Image&& other) noexcept {
657  if(this != &other) {
658  release();
659  swap(other);
660  }
661 
662  return *this;
663  }
664 
666  bool isNull() const noexcept {
667  return getData() == nullptr;
668  }
669 
673  bool isValid() const noexcept {
674  return !isNull() &&
675  getHeight() > 0 &&
676  getWidth() > 0 &&
677  getFormat().isValid();
678  }
679 
683  operator bool () const noexcept {
684  return isValid();
685  }
686 
691  FSDK_API void* getScanLine(int y) noexcept;
692 
697  FSDK_API const void* getScanLine(int y) const noexcept;
698 
701  FSDK_API int getDataSize() const noexcept;
702 
706  FSDK_API void getDataSize(Sizer& sizer) const noexcept;
707 
712  template<typename T>
713  T* getScanLineAs(int y) noexcept {
714  return reinterpret_cast<T*>(getScanLine(y));
715  }
716 
721  template<typename T>
722  const T* getScanLineAs(int y) const noexcept {
723  return reinterpret_cast<const T*>(getScanLine(y));
724  }
725 
728  void* getData() noexcept {
729  return m_data;
730  }
731 
734  const void* getData() const noexcept {
735  return m_data;
736  }
737 
740  template<typename T>
741  T* getDataAs() noexcept {
742  return reinterpret_cast<T*>(getData());
743  }
744 
747  template<typename T>
748  const T* getDataAs() const noexcept {
749  return reinterpret_cast<const T*>(getData());
750  }
751 
753  int getRowSize() const noexcept {
754  return getFormat().computePitch(getWidth());
755  }
756 
758  int getWidth() const noexcept {
759  return m_width;
760  }
761 
763  int getHeight() const noexcept {
764  return m_height;
765  }
766 
768  float getAspectRatio() const noexcept {
769  return
770  static_cast<float>(getWidth()) /
771  static_cast<float>(getHeight());
772  }
773 
775  Format getFormat() const noexcept {
776  return m_format;
777  }
778 
779  MemoryResidence getMemoryResidence() const noexcept {
780  return m_residence;
781  }
782 
784  Size getSize() const noexcept {
785  return Size(getWidth(), getHeight());
786  }
787 
791  Rect getRect() const noexcept {
792  return Rect(0, 0, getWidth(), getHeight());
793  }
794 
799  bool ownsData() const noexcept {
800  return !!m_ref;
801  }
802 
807  bool isSharedWith(const Image& other) const noexcept {
808  return getData() == other.getData();
809  }
810 
814  void swap(Image& other) noexcept {
815  std::swap(m_data, other.m_data);
816  std::swap(m_ref, other.m_ref);
817  std::swap(m_height, other.m_height);
818  std::swap(m_width, other.m_width);
819  std::swap(m_format, other.m_format);
820  std::swap(m_residence, other.m_residence);
821  }
822 
825  void reset() noexcept {
826  Image().swap(*this);
827  }
828 
829  FSDK_API void putPixel(uint32_t x, uint32_t y) noexcept;
830 
831  protected:
832  void* m_data;
833  int* m_ref;
834  int m_height;
835  int m_width;
836 
838  MemoryResidence m_residence;
839 
844  FSDK_API static void* allocate(int size) noexcept;
845 
849  FSDK_API static void deallocate(void* memory) noexcept;
850 
854  FSDK_API int retain() noexcept;
855 
859  FSDK_API int release() noexcept;
860 
864  FSDK_API int getRefCount() const noexcept;
865  };
866 
870  template<>
872 
873  static bool isOk(Image::Error error) noexcept {
874  return error == Image::Error::Ok;
875  }
876 
877  static const char* toString (Image::Error error) noexcept {
878  switch(error) {
879  case Image::Error::Ok: return "Ok";
880  case Image::Error::InvalidType: return "Unsupported type";
881  case Image::Error::InvalidPath: return "Invalid path";
882  case Image::Error::FailedToSave: return "Error during image saving";
883  case Image::Error::FailedToLoad: return "Error during image loading";
884  case Image::Error::InvalidImage: return "Invalid image";
885  case Image::Error::InvalidWidth: return "Invalid image width";
886  case Image::Error::InvalidHeight: return "Invalid image height";
887  case Image::Error::InvalidFormat: return "Unsupported format";
888  case Image::Error::InvalidMemory: return "Memory error";
889  case Image::Error::InvalidBitmap: return "Bitmap error";
890  case Image::Error::InvalidArchive: return "Archive error";
891  case Image::Error::InvalidDataPtr: return "Bad input data pointer";
892  case Image::Error::InvalidDataSize: return "Bad input data size";
893  case Image::Error::InvalidConversion: return "Requiered conversion not implemented";
894  case Image::Error::InvalidDevice: return "Selected Target Device is not supported for this system";
895  default: return "Unknown error";
896  }
897  }
898  };
899 
904  using ImageError = Image::Error;
907 
908 }
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:768
A structure that encapsulates an action result enumeration.
Definition: Result.h:29
void reset() noexcept
Reset image contents.
Definition: Image.h:825
static FSDK_API void deallocate(void *memory) noexcept
Free memory.
Vector2< int > Point2i
Definition: Vector2.h:292
Rect getRect() const noexcept
Definition: Image.h:791
Type height
Rectangle height.
Definition: Rect.h:16
int m_height
image height.
Definition: Image.h:834
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)
FSDK_API Result< Error > create(int width, int height, Format format, bool cleanup=true, MemoryResidence residence=MemoryResidence::MemoryCPU) noexcept
Initializes an empty image and preallocates storage buffer of given size.
int computePitch(int rowWidth) const noexcept
Compute row size in bytes.
Definition: Format.h:117
bool isValid() const noexcept
Definition: Format.h:169
Image format.
Definition: Format.h:11
bool isNull() const noexcept
Definition: Image.h:666
Bad path for saving / loading.
FSDK_API Result< Error > load(const char *path) noexcept
Load image from file.
void * getData() noexcept
Definition: Image.h:728
Format getFormat() const noexcept
Definition: Image.h:775
const void * getData() const noexcept
Definition: Image.h:734
const T * getDataAs() const noexcept
Definition: Image.h:748
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.
int getRowSize() const noexcept
Definition: Image.h:753
T * getScanLineAs(int y) noexcept
Get image scanline data.
Definition: Image.h:713
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:103
int getHeight() const noexcept
Definition: Image.h:763
Type width
Rectangle width.
Definition: Rect.h:15
Conversion not implemented.
Format m_format
image format (
Definition: Image.h:837
Common SDK definitions.
Invalid memory residence.
SubImage map(const Size &size) const noexcept
Map image contents to a given area.
Definition: Image.h:317
bool isSharedWith(const Image &other) const noexcept
Definition: Image.h:807
void * m_data
raw image data.
Definition: Image.h:832
Result< Image::Error > convert(Image &dest, const Size &size, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition: Image.h:457
int * m_ref
reference counter. nullptr if image does not own data.
Definition: Image.h:833
const T * getScanLineAs(int y) const noexcept
Get image scanline data.
Definition: Image.h:722
FSDK_API int getRefCount() const noexcept
Obtain reference count.
Error during image saving.
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:378
static FSDK_API void * allocate(int size) noexcept
Allocate memory.
Type
Supported image types.
Definition: Image.h:43
void swap(Image &other) noexcept
Swap contents with another image.
Definition: Image.h:814
FSDK_API Result< Error > set(int width, int height, Format format, const void *data) noexcept
Initializes an image with provided data. If current image is not empty and it&#39;s size and format match...
Error during image loading.
Failed to retain image: it was released in another thread.
int m_width
image width.
Definition: Image.h:835
Image in Cuda device memory.
ImageCompression
Supported compression type is used only for jpg and png types.
Definition: Image.h:66
Image.
Definition: Image.h:38
compression with minimal (or without) quality loss (only for png or jpg image)
T * getDataAs() noexcept
Definition: Image.h:741
FSDK_API int getDataSize() const noexcept
Target Device is not supported for this system.
Image & operator=(const Image &other) noexcept
Assign an other image.
Definition: Image.h:647
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:474
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:328
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:366
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:76
bool ownsData() const noexcept
Definition: Image.h:799
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:488
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:396
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:353
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:301
Size getSize() const noexcept
Definition: Image.h:784
Type y
Upper left corner y-coordinate.
Definition: Rect.h:14
int getWidth() const noexcept
Definition: Image.h:758
Type x
Upper left corner x-coordinate.
Definition: Rect.h:13
bool isValid() const noexcept
Definition: Image.h:673
static FSDK_API Result< Error > guessTypeFromMemory(const void *data, const uint32_t sizeInBytes, Type &type) noexcept
Guess type of image written into memory.