Face Engine SDK  4.6.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/Result.h>
6 #include <fsdk/Types/Sizer.h>
7 #include <fsdk/Types/SubImage.h>
8 
9 namespace fsdk
10 {
11 
12  struct IArchive;
13 
37  struct Image {
38 
42  enum class Type: uint8_t {
43  BMP,
44  JPG,
45  PNG,
46  PPM,
47  TIFF,
48  Unknown
49  };
50 
54  enum class TargetDevice {
55  CPU,
56  GPU
57  };
58 
65  enum class ImageCompression {
71  };
75  enum class Error: uint32_t {
76  Ok,
77  InvalidWidth,
81  InvalidImage,
83  InvalidPath,
84  InvalidType,
91  FailedToSave,
92  FailedToLoad,
95  };
96 
102  enum class MemoryResidence: uint32_t {
103  MemoryCPU,
104  MemoryGPU
105  };
106 
109  FSDK_API Image() noexcept;
110 
120  int width,
121  int height,
122  Format format,
123  MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
124 
136  int width,
137  int height,
138  Format format,
139  const void* data,
140  MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
141 
158  int width,
159  int height,
160  Format format,
161  void* data,
162  bool copy,
163  MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
164 
169  FSDK_API Image(const Image& other) noexcept;
170 
175  FSDK_API Image(Image&& other) noexcept;
176 
182  FSDK_API explicit Image(const SubImage& subImage) noexcept;
183 
184  FSDK_API ~Image();
185 
196  FSDK_API Result<Error> create(int width, int height, Format format, bool cleanup = true, MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
197 
209  FSDK_API Result<Error> create(int width, int height, Format format, const void* data, MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
210 
226  FSDK_API Result<Error> create(int width, int height, Format format, void* data, bool copy, MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
227 
235  FSDK_API Result<Error> create(const fsdk::Image& source, MemoryResidence residence) noexcept;
236 
250  FSDK_API Result<Error> set(int width, int height, Format format, const void* data) noexcept;
251 
269  FSDK_API Result<Error> set(int width, int height, Format format, void* data, bool copy) noexcept;
270 
276  FSDK_API Result<Error> set(const Image& other) noexcept;
277 
283  FSDK_API Result<Error> set(const SubImage& subImage) noexcept;
284 
296  FSDK_API SubImage map(int x, int y, int width, int height) const noexcept;
297 
304  SubImage map(const Rect& rect) const noexcept {
305  return map(rect.x, rect.y, rect.width, rect.height);
306  }
307 
316  SubImage map(const Size& size) const noexcept {
317  return map(Point2i(0, 0), size);
318  }
319 
327  SubImage map(const Point2i& origin, const Size& size) const noexcept {
328  return map(origin.x, origin.y, size.x, size.y);
329  }
330 
343  FSDK_API Image extract(int x, int y, int width, int height) const noexcept;
344 
352  Image extract(const Rect& rect) const noexcept {
353  return extract(rect.x, rect.y, rect.width, rect.height);
354  }
355 
365  Image extract(const Size& size) const noexcept {
366  return extract(Point2i(0, 0), size);
367  }
368 
377  Image extract(const Point2i& origin, const Size& size) const noexcept {
378  return extract(origin.x, origin.y, size.x, size.y);
379  }
380 
388  FSDK_API static Result<Error> guessTypeFromMemory(const void* data, const uint32_t sizeInBytes, Type& type) noexcept;
389 
395  Image clone() const noexcept {
396  return Image(
397  getWidth(),
398  getHeight(),
399  getFormat(),
400  getData());
401  }
402 
421  FSDK_API Result<Image::Error> convert(Image& dest, int x, int y, int width, int height, Format format, TargetDevice device = TargetDevice::CPU) const noexcept;
422 
437  Result<Image::Error> convert(Image& dest, const Point2i& origin, const Size& size, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
438  return convert(dest, origin.x, origin.y, size.x, size.y, format, device);
439  }
440 
456  Result<Image::Error> convert(Image& dest, const Size& size, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
457  return convert(dest, Point2i(0, 0), size, format, device);
458  }
459 
473  Result<Image::Error> convert(Image& dest, const Rect& rect, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
474  return convert(dest, rect.x, rect.y, rect.width, rect.height, format, device);
475  }
476 
487  Result<Image::Error> convert(Image& dest, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
488  return convert(dest, getRect(), format, device);
489  }
490 
496  FSDK_API Image rescale(float scale) const noexcept;
497 
508  fsdk::Image::ImageCompression additionalFlag =
510 
521  const char* path,
522  const Format format) const noexcept;
523 
535  IArchive* archive,
537 
549  Image::Type type,
550  IArchive* archive,
551  const Format format) const noexcept;
552 
564  FSDK_API Result<Error> load(const char* path) noexcept;
565 
575  const char* path,
576  const Format format) noexcept;
577 
591  const void* data,
592  const uint32_t sizeInBytes) noexcept;
593 
604  const void* data,
605  const uint32_t sizeInBytes,
606  const Format format) noexcept;
607 
623  const void* data,
624  const uint32_t sizeInBytes,
625  const Type type) noexcept;
626 
638  const void* data,
639  const uint32_t sizeInBytes,
640  const Type type,
641  const Format format) noexcept;
642 
646  Image& operator = (const Image& other) noexcept {
647  set(other);
648 
649  return *this;
650  }
651 
655  Image& operator = (Image&& other) noexcept {
656  if(this != &other) {
657  release();
658  swap(other);
659  }
660 
661  return *this;
662  }
663 
665  bool isNull() const noexcept {
666  return getData() == nullptr;
667  }
668 
672  bool isValid() const noexcept {
673  return !isNull() &&
674  getHeight() > 0 &&
675  getWidth() > 0 &&
676  getFormat().isValid();
677  }
678 
682  operator bool () const noexcept {
683  return isValid();
684  }
685 
690  FSDK_API void* getScanLine(int y) noexcept;
691 
696  FSDK_API const void* getScanLine(int y) const noexcept;
697 
700  FSDK_API int getDataSize() const noexcept;
701 
705  FSDK_API void getDataSize(Sizer& sizer) const noexcept;
706 
711  template<typename T>
712  T* getScanLineAs(int y) noexcept {
713  return reinterpret_cast<T*>(getScanLine(y));
714  }
715 
720  template<typename T>
721  const T* getScanLineAs(int y) const noexcept {
722  return reinterpret_cast<const T*>(getScanLine(y));
723  }
724 
727  void* getData() noexcept {
728  return m_data;
729  }
730 
733  const void* getData() const noexcept {
734  return m_data;
735  }
736 
739  template<typename T>
740  T* getDataAs() noexcept {
741  return reinterpret_cast<T*>(getData());
742  }
743 
746  template<typename T>
747  const T* getDataAs() const noexcept {
748  return reinterpret_cast<const T*>(getData());
749  }
750 
752  int getRowSize() const noexcept {
753  return getFormat().computePitch(getWidth());
754  }
755 
757  int getWidth() const noexcept {
758  return m_width;
759  }
760 
762  int getHeight() const noexcept {
763  return m_height;
764  }
765 
767  float getAspectRatio() const noexcept {
768  return
769  static_cast<float>(getWidth()) /
770  static_cast<float>(getHeight());
771  }
772 
774  Format getFormat() const noexcept {
775  return m_format;
776  }
777 
778  MemoryResidence getMemoryResidence() const noexcept {
779  return m_residence;
780  }
781 
783  Size getSize() const noexcept {
784  return Size(getWidth(), getHeight());
785  }
786 
790  Rect getRect() const noexcept {
791  return Rect(0, 0, getWidth(), getHeight());
792  }
793 
798  bool ownsData() const noexcept {
799  return !!m_ref;
800  }
801 
806  bool isSharedWith(const Image& other) const noexcept {
807  return getData() == other.getData();
808  }
809 
813  void swap(Image& other) noexcept {
814  std::swap(m_data, other.m_data);
815  std::swap(m_ref, other.m_ref);
816  std::swap(m_height, other.m_height);
817  std::swap(m_width, other.m_width);
818  std::swap(m_format, other.m_format);
819  std::swap(m_residence, other.m_residence);
820  }
821 
824  void reset() noexcept {
825  Image().swap(*this);
826  }
827 
828  FSDK_API void putPixel(uint32_t x, uint32_t y) noexcept;
829 
830  protected:
831  void* m_data;
832  int* m_ref;
833  int m_height;
834  int m_width;
835 
837  MemoryResidence m_residence;
838 
843  FSDK_API static void* allocate(int size) noexcept;
844 
848  FSDK_API static void deallocate(void* memory) noexcept;
849 
853  FSDK_API int retain() noexcept;
854 
858  FSDK_API int release() noexcept;
859 
863  FSDK_API int getRefCount() const noexcept;
864  };
865 
869  template<>
871 
872  static bool isOk(Image::Error error) noexcept {
873  return error == Image::Error::Ok;
874  }
875 
876  static const char* toString (Image::Error error) noexcept {
877  switch(error) {
878  case Image::Error::Ok: return "Ok";
879  case Image::Error::InvalidType: return "Unsupported type";
880  case Image::Error::InvalidPath: return "Invalid path";
881  case Image::Error::FailedToSave: return "Error during image saving";
882  case Image::Error::FailedToLoad: return "Error during image loading";
883  case Image::Error::InvalidImage: return "Invalid image";
884  case Image::Error::InvalidWidth: return "Invalid image width";
885  case Image::Error::InvalidHeight: return "Invalid image height";
886  case Image::Error::InvalidFormat: return "Unsupported format";
887  case Image::Error::InvalidMemory: return "Memory error";
888  case Image::Error::InvalidBitmap: return "Bitmap error";
889  case Image::Error::InvalidArchive: return "Archive error";
890  case Image::Error::InvalidDataPtr: return "Bad input data pointer";
891  case Image::Error::InvalidDataSize: return "Bad input data size";
892  case Image::Error::InvalidConversion: return "Requiered conversion not implemented";
893  case Image::Error::InvalidDevice: return "Selected Target Device is not supported for this system";
894  default: return "Unknown error";
895  }
896  }
897  };
898 
903  using ImageError = Image::Error;
906 
907 }
fsdk::Image::ownsData
bool ownsData() const noexcept
Definition: Image.h:798
fsdk::Image::m_width
int m_width
image width.
Definition: Image.h:834
fsdk::Image::m_height
int m_height
image height.
Definition: Image.h:833
fsdk::Image::getData
const void * getData() const noexcept
Definition: Image.h:733
fsdk::Image::getData
void * getData() noexcept
Definition: Image.h:727
fsdk::Image::MemoryResidence
MemoryResidence
Memory residence.
Definition: Image.h:102
fsdk::Image::getSize
Size getSize() const noexcept
Definition: Image.h:783
fsdk::Image::m_format
Format m_format
image format (
Definition: Image.h:836
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:473
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:456
fsdk::Image::Error::FailedToInitialize
@ FailedToInitialize
Error during initialization.
fsdk::Vector2< int >
fsdk::Image::getHeight
int getHeight() const noexcept
Definition: Image.h:762
fsdk::Image::swap
void swap(Image &other) noexcept
Swap contents with another image.
Definition: Image.h:813
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:487
fsdk::Image::Error::InvalidMemory
@ InvalidMemory
Error at memory opening.
fsdk::Image::getDataAs
const T * getDataAs() const noexcept
Definition: Image.h:747
fsdk::Image::isNull
bool isNull() const noexcept
Definition: Image.h:665
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:712
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:646
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:352
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:831
fsdk::Image::Error::InvalidDataSize
@ InvalidDataSize
Bad input data size.
fsdk::Image::getDataAs
T * getDataAs() noexcept
Definition: Image.h:740
fsdk::Image::ImageCompression
ImageCompression
Supported compression type is used only for jpg and png types.
Definition: Image.h:65
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:395
fsdk::Image::isSharedWith
bool isSharedWith(const Image &other) const noexcept
Definition: Image.h:806
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:365
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:752
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:757
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:824
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:437
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:327
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:377
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:767
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:790
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:774
fsdk::Image::isValid
bool isValid() const noexcept
Definition: Image.h:672
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:37
fsdk::Image::map
SubImage map(const Size &size) const noexcept
Map image contents to a given area.
Definition: Image.h:316
fsdk::Image::getScanLine
FSDK_API void * getScanLine(int y) noexcept
Get image scanline data.
fsdk::Image::Error
Error
Image error codes.
Definition: Image.h:75
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:832
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:54
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:721
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:42
fsdk::Image::Error::FailedToLoad
@ FailedToLoad
Error during image loading.
fsdk::Image::Error::InvalidWidth
@ InvalidWidth
Invalid width.