Face Engine SDK  4.7.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/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 
121  int width,
122  int height,
123  Format format,
124  MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
125 
137  int width,
138  int height,
139  Format format,
140  const void* data,
141  MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
142 
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 
509  fsdk::Image::ImageCompression additionalFlag =
511 
522  const char* path,
523  const Format format) const noexcept;
524 
536  IArchive* archive,
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 }
fsdk::Image::ownsData
bool ownsData() const noexcept
Definition: Image.h:799
fsdk::Image::m_width
int m_width
image width.
Definition: Image.h:835
fsdk::Image::m_height
int m_height
image height.
Definition: Image.h:834
fsdk::Image::getData
const void * getData() const noexcept
Definition: Image.h:734
fsdk::Image::getData
void * getData() noexcept
Definition: Image.h:728
fsdk::Image::MemoryResidence
MemoryResidence
Memory residence.
Definition: Image.h:103
fsdk::Image::getSize
Size getSize() const noexcept
Definition: Image.h:784
fsdk::Image::m_format
Format m_format
image format (
Definition: Image.h:837
fsdk::Image::loadFromMemory
FSDK_API Result< Error > loadFromMemory(const void *data, const uint32_t sizeInBytes) noexcept
Load image from memory.
fsdk::Image::MemoryResidence::MemoryCPU
@ MemoryCPU
Image in Host device memory.
fsdk::Image::Error::InvalidConversion
@ InvalidConversion
Conversion not implemented.
fsdk::Image::convert
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.
fsdk::Image::ImageCompression::IC_BEST_COMPRESSION
@ IC_BEST_COMPRESSION
maximum compression level (only for png or jpg)
fsdk::Image::map
FSDK_API SubImage map(int x, int y, int width, int height) const noexcept
Map image contents to a given area.
fsdk::Image::Error::InvalidDataPtr
@ InvalidDataPtr
Bad input data pointer.
fsdk::Image::load
FSDK_API Result< Error > load(const char *path) noexcept
Load image from file.
fsdk::Image::convert
Result< Image::Error > convert(Image &dest, const Rect &rect, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition: Image.h:474
fsdk::Image::convert
Result< Image::Error > convert(Image &dest, const Size &size, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition: Image.h:457
fsdk::Image::Error::FailedToInitialize
@ FailedToInitialize
Error during initialization.
fsdk::Vector2< int >
fsdk::Image::getHeight
int getHeight() const noexcept
Definition: Image.h:763
fsdk::Image::swap
void swap(Image &other) noexcept
Swap contents with another image.
Definition: Image.h:814
fsdk::Image::Error::InvalidHeight
@ InvalidHeight
Invalid height.
fsdk::Image::convert
Result< Image::Error > convert(Image &dest, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition: Image.h:488
fsdk::Image::Error::InvalidMemory
@ InvalidMemory
Error at memory opening.
fsdk::Image::getDataAs
const T * getDataAs() const noexcept
Definition: Image.h:748
fsdk::Image::isNull
bool isNull() const noexcept
Definition: Image.h:666
fsdk::Image::load
FSDK_API Result< Error > load(const char *path, const Format format) noexcept
Load image from file and convert it to required format.
fsdk
SDK namespace.
Definition: IAGSEstimator.h:8
fsdk::BaseRect< int >
fsdk::Image::getScanLineAs
T * getScanLineAs(int y) noexcept
Get image scanline data.
Definition: Image.h:713
fsdk::Image::set
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's size and format match...
fsdk::Image::operator=
Image & operator=(const Image &other) noexcept
Assign an other image.
Definition: Image.h:647
fsdk::Image::allocate
static FSDK_API void * allocate(int size) noexcept
Allocate memory.
fsdk::Image::extract
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:353
fsdk::Image::Error::InvalidDevice
@ InvalidDevice
Target Device is not supported for this system.
fsdk::Image::Error::InvalidPath
@ InvalidPath
Bad path for saving / loading.
fsdk::Image::Error::InvalidResidence
@ InvalidResidence
Invalid memory residence.
fsdk::Image::m_data
void * m_data
raw image data.
Definition: Image.h:832
fsdk::Image::Error::InvalidDataSize
@ InvalidDataSize
Bad input data size.
fsdk::Image::getDataAs
T * getDataAs() noexcept
Definition: Image.h:741
fsdk::Image::ImageCompression
ImageCompression
Supported compression type is used only for jpg and png types.
Definition: Image.h:66
fsdk::Image::clone
Image clone() const noexcept
Create a copy of this image. The new image will have it's own reference count.
Definition: Image.h:396
fsdk::Image::isSharedWith
bool isSharedWith(const Image &other) const noexcept
Definition: Image.h:807
fsdk::Format
Image format.
Definition: Format.h:11
fsdk::Image::extract
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:366
fsdk::Image::loadFromMemoryOfType
FSDK_API Result< Error > loadFromMemoryOfType(const void *data, const uint32_t sizeInBytes, const Type type) noexcept
Load image from memory of specific type.
fsdk::Image::ImageCompression::IC_NO_COMPRESSION
@ IC_NO_COMPRESSION
no compression (only for png or jpg image)
fsdk::Image::getRowSize
int getRowSize() const noexcept
Definition: Image.h:753
fsdk::Format::computePitch
int computePitch(int rowWidth) const noexcept
Compute row size in bytes.
Definition: Format.h:117
fsdk::Format::isValid
bool isValid() const noexcept
Definition: Format.h:169
FSDK_API
#define FSDK_API
Dummy.
Definition: Def.h:27
fsdk::Image::loadFromMemory
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.
fsdk::Image::save
FSDK_API Result< Error > save(const char *path, const Format format) const noexcept
Convert and Save image.
fsdk::Image::getDataSize
FSDK_API int getDataSize() const noexcept
fsdk::Size
Vector2< int > Size
Definition: Vector2.h:301
fsdk::Image::getWidth
int getWidth() const noexcept
Definition: Image.h:758
fsdk::Image::saveToMemory
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::Image::ImageCompression::IC_HARD_COMPRESSION
@ IC_HARD_COMPRESSION
more than medium compression (only for png or jpg)
fsdk::Image::extract
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.
fsdk::Image::reset
void reset() noexcept
Reset image contents.
Definition: Image.h:825
fsdk::Image::loadFromMemoryOfType
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.
fsdk::Image::retain
FSDK_API int retain() noexcept
Increase reference count.
fsdk::Image::release
FSDK_API int release() noexcept
Decrease reference count.
fsdk::Image::convert
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:438
fsdk::Image::Error::FailedToSave
@ FailedToSave
Error during image saving.
fsdk::Image::Error::InvalidImage
@ InvalidImage
Invalid image.
fsdk::Image::map
SubImage map(const Point2i &origin, const Size &size) const noexcept
Map image contents to a given area.
Definition: Image.h:328
fsdk::IArchive
Archive interface.
Definition: IObject.h:37
fsdk::Point2i
Vector2< int > Point2i
Definition: Vector2.h:292
fsdk::Image::extract
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:378
fsdk::Image::Error::InvalidBitmap
@ InvalidBitmap
Bitmap error occurred.
fsdk::ErrorTraits
Definition: Result.h:10
fsdk::Image::create
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.
fsdk::Image::getAspectRatio
float getAspectRatio() const noexcept
Definition: Image.h:768
fsdk::Image::save
FSDK_API Result< Image::Error > save(const char *path, fsdk::Image::ImageCompression additionalFlag=fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept
Save image.
fsdk::Image::Image
FSDK_API Image() noexcept
Initializes an empty image.
fsdk::Image::getRect
Rect getRect() const noexcept
Definition: Image.h:791
fsdk::Result
A structure that encapsulates an action result enumeration.
Definition: Result.h:30
fsdk::Image::deallocate
static FSDK_API void deallocate(void *memory) noexcept
Free memory.
fsdk::Image::rescale
FSDK_API Image rescale(float scale) const noexcept
Rescale image keeping proportions.
fsdk::Image::guessTypeFromMemory
static FSDK_API Result< Error > guessTypeFromMemory(const void *data, const uint32_t sizeInBytes, Type &type) noexcept
Guess type of image written into memory.
fsdk::Image::getFormat
Format getFormat() const noexcept
Definition: Image.h:775
fsdk::Image::isValid
bool isValid() const noexcept
Definition: Image.h:673
fsdk::Image::MemoryResidence::MemoryGPU
@ MemoryGPU
Image in Cuda device memory.
fsdk::Image::Error::Ok
@ Ok
Ok.
fsdk::Image::getScanLine
FSDK_API const void * getScanLine(int y) const noexcept
Get image scanline data.
fsdk::Image
Image.
Definition: Image.h:38
fsdk::Image::map
SubImage map(const Size &size) const noexcept
Map image contents to a given area.
Definition: Image.h:317
fsdk::Image::getScanLine
FSDK_API void * getScanLine(int y) noexcept
Get image scanline data.
fsdk::Image::Error
Error
Image error codes.
Definition: Image.h:76
fsdk::Image::saveToMemory
FSDK_API Result< Image::Error > saveToMemory(Image::Type type, IArchive *archive, const Format format) const noexcept
Convert and save image to memory buffer.
fsdk::Image::Error::InvalidType
@ InvalidType
Unsupported type.
fsdk::Image::m_ref
int * m_ref
reference counter. nullptr if image does not own data.
Definition: Image.h:833
fsdk::SubImage
Sub Image.
Definition: SubImage.h:11
fsdk::Image::Error::ReleasedInOtherThread
@ ReleasedInOtherThread
Failed to retain image: it was released in another thread.
fsdk::Image::getRefCount
FSDK_API int getRefCount() const noexcept
Obtain reference count.
fsdk::Image::TargetDevice
TargetDevice
Target device that fsdk::Image algorithms will be running on.
Definition: Image.h:55
fsdk::Image::Error::InvalidFormat
@ InvalidFormat
Unsupported format.
fsdk::Image::ImageCompression::IC_SMALL_COMPRESSION
@ IC_SMALL_COMPRESSION
compression with minimal (or without) quality loss (only for png or jpg image)
fsdk::Image::Error::InvalidArchive
@ InvalidArchive
Archive error.
fsdk::Image::getScanLineAs
const T * getScanLineAs(int y) const noexcept
Get image scanline data.
Definition: Image.h:722
fsdk::Sizer
Helper entity to measure size of dynamic objects in memory.
Definition: Sizer.h:10
fsdk::Image::ImageCompression::IC_MEDIUM_COMPRESSION
@ IC_MEDIUM_COMPRESSION
medium compression (only for png or jpg)
fsdk::Image::Type
Type
Supported image types.
Definition: Image.h:43
fsdk::Image::Error::FailedToLoad
@ FailedToLoad
Error during image loading.
fsdk::Image::Error::InvalidWidth
@ InvalidWidth
Invalid width.