Face Engine SDK  4.6.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/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 
119  FSDK_API Image(
120  int width,
121  int height,
122  Format format,
123  MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
124 
135  FSDK_API Image(
136  int width,
137  int height,
138  Format format,
139  const void* data,
140  MemoryResidence residence = MemoryResidence::MemoryCPU) noexcept;
141 
157  FSDK_API Image(
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 
507  FSDK_API Result <Image::Error> save(const char* path,
508  fsdk::Image::ImageCompression additionalFlag =
510 
521  const char* path,
522  const Format format) const noexcept;
523 
534  saveToMemory(Image::Type type,
535  IArchive* archive,
536  fsdk::Image::ImageCompression additionalFlag = fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept;
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 }
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:767
A structure that encapsulates an action result enumeration.
Definition: Result.h:29
void reset() noexcept
Reset image contents.
Definition: Image.h:824
static FSDK_API void deallocate(void *memory) noexcept
Free memory.
Vector2< int > Point2i
Definition: Vector2.h:292
Rect getRect() const noexcept
Definition: Image.h:790
Type height
Rectangle height.
Definition: Rect.h:16
int m_height
image height.
Definition: Image.h:833
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:665
Bad path for saving / loading.
FSDK_API Result< Error > load(const char *path) noexcept
Load image from file.
void * getData() noexcept
Definition: Image.h:727
Format getFormat() const noexcept
Definition: Image.h:774
const void * getData() const noexcept
Definition: Image.h:733
const T * getDataAs() const noexcept
Definition: Image.h:747
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:752
T * getScanLineAs(int y) noexcept
Get image scanline data.
Definition: Image.h:712
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:102
int getHeight() const noexcept
Definition: Image.h:762
Type width
Rectangle width.
Definition: Rect.h:15
Conversion not implemented.
Format m_format
image format (
Definition: Image.h:836
Common SDK definitions.
Invalid memory residence.
SubImage map(const Size &size) const noexcept
Map image contents to a given area.
Definition: Image.h:316
bool isSharedWith(const Image &other) const noexcept
Definition: Image.h:806
void * m_data
raw image data.
Definition: Image.h:831
Result< Image::Error > convert(Image &dest, const Size &size, Format format, TargetDevice device=TargetDevice::CPU) const noexcept
Convert image format.
Definition: Image.h:456
int * m_ref
reference counter. nullptr if image does not own data.
Definition: Image.h:832
const T * getScanLineAs(int y) const noexcept
Get image scanline data.
Definition: Image.h:721
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:377
static FSDK_API void * allocate(int size) noexcept
Allocate memory.
Type
Supported image types.
Definition: Image.h:42
void swap(Image &other) noexcept
Swap contents with another image.
Definition: Image.h:813
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:834
Image in Cuda device memory.
ImageCompression
Supported compression type is used only for jpg and png types.
Definition: Image.h:65
Image.
Definition: Image.h:37
compression with minimal (or without) quality loss (only for png or jpg image)
T * getDataAs() noexcept
Definition: Image.h:740
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:646
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:473
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:327
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:365
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:75
bool ownsData() const noexcept
Definition: Image.h:798
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:487
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:395
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:54
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:352
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:783
Type y
Upper left corner y-coordinate.
Definition: Rect.h:14
int getWidth() const noexcept
Definition: Image.h:757
Type x
Upper left corner x-coordinate.
Definition: Rect.h:13
bool isValid() const noexcept
Definition: Image.h:672
static FSDK_API Result< Error > guessTypeFromMemory(const void *data, const uint32_t sizeInBytes, Type &type) noexcept
Guess type of image written into memory.