Face Engine SDK  5.30.2
A face detection, recognition and tracking engine.
include/fsdk/Types/Image.h
00001 #pragma once
00002 
00003 #include <fsdk/Def.h>
00004 #include <fsdk/Types/Format.h>
00005 #include <fsdk/Types/Rect.h>
00006 #include <fsdk/Types/Result.h>
00007 #include <fsdk/Types/RotationType.h>
00008 #include <fsdk/Types/Sizer.h>
00009 #include <fsdk/Types/SubImage.h>
00010 
00011 namespace fsdk {
00012 
00013     struct IArchive;
00014 
00038     struct Image {
00039 
00043         enum class Type : uint8_t {
00044             BMP,
00045             JPG,
00046             PNG,
00047             PPM,
00048             TIFF,
00049             Unknown
00050         };
00051 
00055         enum class TargetDevice {
00056             CPU,
00057             GPU,
00058             NPU,
00059         };
00060 
00068         enum class ImageCompression {
00069             IC_NO_COMPRESSION,     
00070             IC_SMALL_COMPRESSION,  
00071 
00072             IC_MEDIUM_COMPRESSION, 
00073             IC_HARD_COMPRESSION,   
00074             IC_BEST_COMPRESSION,   
00075         };
00079         enum class Error : uint32_t {
00080             Ok,                   
00081             InvalidWidth,         
00082             InvalidHeight,        
00083             InvalidDataPtr,       
00084             InvalidDataSize,      
00085             InvalidImage,         
00086             InvalidArchive,       
00087             InvalidPath,          
00088             InvalidType,          
00089             InvalidFormat,        
00090             InvalidBitmap,        
00091             InvalidMemory,        
00092             InvalidConversion,    
00093             InvalidResidence,     
00094             InvalidDevice,        
00095             InvalidCudaKernel,    
00096             FailedToSave,         
00097             FailedToLoad,         
00098             FailedToInitialize,   
00099             InvalidInput,         
00100             ReleasedInOtherThread 
00101         };
00102 
00108         enum class MemoryResidence : uint32_t {
00109             MemoryCPU,     
00110             MemoryGPU,     
00111             MemoryNPU,     
00112             MemoryNPU_DPP, 
00113         };
00114 
00117         FSDK_API Image() noexcept;
00118 
00129         FSDK_API Image(
00130             int width,
00131             int height,
00132             Format format,
00133             MemoryResidence residence = MemoryResidence::MemoryCPU,
00134             int deviceId = 0) noexcept;
00135 
00148         FSDK_API Image(
00149             int width,
00150             int height,
00151             Format format,
00152             const void* data,
00153             MemoryResidence residence = MemoryResidence::MemoryCPU,
00154             int deviceId = 0) noexcept;
00155 
00173         FSDK_API Image(
00174             int width,
00175             int height,
00176             Format format,
00177             void* data,
00178             bool copy,
00179             MemoryResidence residence = MemoryResidence::MemoryCPU,
00180             int deviceId = 0) noexcept;
00181 
00186         FSDK_API Image(const Image& other) noexcept;
00187 
00192         FSDK_API Image(Image&& other) noexcept;
00193 
00199         FSDK_API explicit Image(const SubImage& subImage) noexcept;
00200 
00201         FSDK_API ~Image();
00202 
00217         FSDK_API Result<Error> create(
00218             int width,
00219             int height,
00220             Format format,
00221             bool cleanup = true,
00222             MemoryResidence residence = MemoryResidence::MemoryCPU,
00223             int deviceId = 0) noexcept;
00224 
00240         FSDK_API Result<Error> create(
00241             int width,
00242             int height,
00243             Format format,
00244             const void* data,
00245             MemoryResidence residence = MemoryResidence::MemoryCPU,
00246             int deviceId = 0) noexcept;
00247 
00267         FSDK_API Result<Error> create(
00268             int width,
00269             int height,
00270             Format format,
00271             void* data,
00272             bool copy,
00273             MemoryResidence residence = MemoryResidence::MemoryCPU,
00274             int deviceId = 0) noexcept;
00275 
00285         FSDK_API Result<Error>
00286         create(const fsdk::Image& source, MemoryResidence residence, int deviceId = 0) noexcept;
00287 
00305         FSDK_API Result<Error> set(
00306             int width,
00307             int height,
00308             Format format,
00309             const void* data,
00310             MemoryResidence residence = MemoryResidence::MemoryCPU,
00311             int deviceId = 0) noexcept;
00312 
00334         FSDK_API Result<Error> set(
00335             int width,
00336             int height,
00337             Format format,
00338             void* data,
00339             bool copy,
00340             MemoryResidence residence = MemoryResidence::MemoryCPU,
00341             int deviceId = 0) noexcept;
00342 
00348         FSDK_API Result<Error> set(const Image& other) noexcept;
00349 
00355         FSDK_API Result<Error> set(const SubImage& subImage) noexcept;
00356 
00369         FSDK_API SubImage map(int x, int y, int width, int height) const noexcept;
00370 
00378         SubImage map(const Rect& rect) const noexcept {
00379             return map(rect.x, rect.y, rect.width, rect.height);
00380         }
00381 
00391         SubImage map(const Size& size) const noexcept {
00392             return map(Point2i(0, 0), size);
00393         }
00394 
00403         SubImage map(const Point2i& origin, const Size& size) const noexcept {
00404             return map(origin.x, origin.y, size.x, size.y);
00405         }
00406 
00421         FSDK_API Image extract(int x, int y, int width, int height) const noexcept;
00422 
00436         FSDK_API Result<Image::Error> extract(Image& dest, int x, int y, int width, int height) const noexcept;
00437 
00449         FSDK_API Result<Image::Error> extract(Image& dest, const Rect& rect) const noexcept;
00450 
00458         FSDK_API Result<Error> rescale(Image& dest, float scale) const noexcept;
00459 
00469         Image extract(const Rect& rect) const noexcept {
00470             return extract(rect.x, rect.y, rect.width, rect.height);
00471         }
00472 
00484         Image extract(const Size& size) const noexcept {
00485             return extract(Point2i(0, 0), size);
00486         }
00487 
00498         Image extract(const Point2i& origin, const Size& size) const noexcept {
00499             return extract(origin.x, origin.y, size.x, size.y);
00500         }
00501 
00509         FSDK_API static Result<Error>
00510         guessTypeFromMemory(const void* data, const uint32_t sizeInBytes, Type& type) noexcept;
00511 
00517         Image clone() const noexcept {
00518             return {getWidth(), getHeight(), getFormat(), getData(), getMemoryResidence(), getDeviceId()};
00519         }
00520 
00539         FSDK_API Result<Image::Error> convert(
00540             Image& dest,
00541             int x,
00542             int y,
00543             int width,
00544             int height,
00545             Format format,
00546             TargetDevice device = TargetDevice::CPU) const noexcept;
00547 
00562         Result<Image::Error> convert(
00563             Image& dest,
00564             const Point2i& origin,
00565             const Size& size,
00566             Format format,
00567             TargetDevice device = TargetDevice::CPU) const noexcept {
00568             return convert(dest, origin.x, origin.y, size.x, size.y, format, device);
00569         }
00570 
00586         Result<Image::Error>
00587         convert(Image& dest, const Size& size, Format format, TargetDevice device = TargetDevice::CPU)
00588             const noexcept {
00589             return convert(dest, Point2i(0, 0), size, format, device);
00590         }
00591 
00605         Result<Image::Error>
00606         convert(Image& dest, const Rect& rect, Format format, TargetDevice device = TargetDevice::CPU)
00607             const noexcept {
00608             return convert(dest, rect.x, rect.y, rect.width, rect.height, format, device);
00609         }
00610 
00621         Result<Image::Error>
00622         convert(Image& dest, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
00623             return convert(dest, getRect(), format, device);
00624         }
00625 
00631         FSDK_API Image rescale(float scale) const noexcept;
00632 
00643         FSDK_API Result<Image::Error> save(
00644             const char* path,
00645             fsdk::Image::ImageCompression additionalFlag =
00646                 fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept;
00647 
00658         FSDK_API Result<Error> save(const char* path, const Format format) const noexcept;
00659 
00670         FSDK_API Result<Image::Error> saveToMemory(
00671             Image::Type type,
00672             IArchive* archive,
00673             fsdk::Image::ImageCompression additionalFlag =
00674                 fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept;
00675 
00687         FSDK_API Result<Image::Error>
00688         saveToMemory(Image::Type type, IArchive* archive, const Format format) const noexcept;
00689 
00702         FSDK_API Result<Error> load(const char* path) noexcept;
00703 
00713         FSDK_API Result<Error> load(const char* path, const Format format) noexcept;
00714 
00728         FSDK_API Result<Error> loadFromMemory(const void* data, const uint32_t sizeInBytes) noexcept;
00729 
00739         FSDK_API Result<Error> rotate(Image& dest, RotationType rotationType) const noexcept;
00740 
00752         FSDK_API Result<Error>
00753         loadFromMemory(const void* data, const uint32_t sizeInBytes, const Format format) noexcept;
00754 
00770         FSDK_API Result<Error>
00771         loadFromMemoryOfType(const void* data, const uint32_t sizeInBytes, const Type type) noexcept;
00772 
00784         FSDK_API Result<Error> loadFromMemoryOfType(
00785             const void* data,
00786             const uint32_t sizeInBytes,
00787             const Type type,
00788             const Format format) noexcept;
00789 
00793         Image& operator=(const Image& other) noexcept {
00794             set(other);
00795 
00796             return *this;
00797         }
00798 
00802         Image& operator=(Image&& other) noexcept {
00803             if(this != &other) {
00804                 release();
00805                 swap(other);
00806             }
00807 
00808             return *this;
00809         }
00810 
00812         bool isNull() const noexcept {
00813             return getData() == nullptr;
00814         }
00815 
00819         bool isValid() const noexcept {
00820             return !isNull() && getHeight() > 0 && getWidth() > 0 && getFormat().isValid();
00821         }
00822 
00826         operator bool() const noexcept {
00827             return isValid();
00828         }
00829 
00834         FSDK_API void* getScanLine(int y) noexcept;
00835 
00840         FSDK_API const void* getScanLine(int y) const noexcept;
00841 
00844         FSDK_API int getDataSize() const noexcept;
00845 
00849         FSDK_API void getDataSize(Sizer& sizer) const noexcept;
00850 
00855         template <typename T>
00856         T* getScanLineAs(int y) noexcept {
00857             return reinterpret_cast<T*>(getScanLine(y));
00858         }
00859 
00864         template <typename T>
00865         const T* getScanLineAs(int y) const noexcept {
00866             return reinterpret_cast<const T*>(getScanLine(y));
00867         }
00868 
00871         void* getData() noexcept {
00872             return m_data;
00873         }
00874 
00877         const void* getData() const noexcept {
00878             return m_data;
00879         }
00880 
00883         template <typename T>
00884         T* getDataAs() noexcept {
00885             return reinterpret_cast<T*>(getData());
00886         }
00887 
00890         template <typename T>
00891         const T* getDataAs() const noexcept {
00892             return reinterpret_cast<const T*>(getData());
00893         }
00894 
00896         int getRowSize() const noexcept {
00897             return getFormat().computePitch(getWidth());
00898         }
00899 
00901         int getWidth() const noexcept {
00902             return m_width;
00903         }
00904 
00906         int getHeight() const noexcept {
00907             return m_height;
00908         }
00909 
00911         float getAspectRatio() const noexcept {
00912             return static_cast<float>(getWidth()) / static_cast<float>(getHeight());
00913         }
00914 
00916         Format getFormat() const noexcept {
00917             return m_format;
00918         }
00919 
00920         MemoryResidence getMemoryResidence() const noexcept {
00921             return m_residence;
00922         }
00923 
00924         int getDeviceId() const noexcept {
00925             return m_deviceId;
00926         }
00927 
00929         Size getSize() const noexcept {
00930             return Size(getWidth(), getHeight());
00931         }
00932 
00936         Rect getRect() const noexcept {
00937             return Rect(0, 0, getWidth(), getHeight());
00938         }
00939 
00944         bool ownsData() const noexcept {
00945             return !!m_ref;
00946         }
00947 
00952         bool isSharedWith(const Image& other) const noexcept {
00953             return getData() == other.getData();
00954         }
00955 
00959         void swap(Image& other) noexcept {
00960             std::swap(m_data, other.m_data);
00961             std::swap(m_ref, other.m_ref);
00962             std::swap(m_height, other.m_height);
00963             std::swap(m_width, other.m_width);
00964             std::swap(m_format, other.m_format);
00965             std::swap(m_residence, other.m_residence);
00966         }
00967 
00970         void reset() noexcept {
00971             Image().swap(*this);
00972         }
00973 
00979         bool equalWeak(const fsdk::Image& other) const noexcept {
00980             return m_width == other.getWidth() && m_height == other.getHeight() && m_format == other.getFormat() &&
00981                          m_residence == other.getMemoryResidence();
00982         }
00983 
00989         bool equalStrong(const fsdk::Image& other) const noexcept {
00990             return equalWeak(other) && m_data == other.getData();
00991         }
00992 
00993         FSDK_API void putPixel(uint32_t x, uint32_t y) noexcept;
00994 
00995     protected:
00996         void* m_data;    
00997         int* m_ref;      
00998         int m_height;    
00999         int m_width;     
01000         int m_deviceId;  
01001         Format m_format; 
01002         MemoryResidence m_residence;
01003 
01008         FSDK_API static void* allocate(int size) noexcept;
01009 
01013         FSDK_API static void deallocate(void* memory) noexcept;
01014 
01018         FSDK_API int retain() noexcept;
01019 
01023         FSDK_API int release() noexcept;
01024 
01028         FSDK_API int getRefCount() const noexcept;
01029     };
01030 
01035     const char* toString(Image::MemoryResidence residence);
01036 
01040     template <>
01041     struct ErrorTraits<Image::Error> {
01042 
01043         static bool isOk(Image::Error error) noexcept {
01044             return error == Image::Error::Ok;
01045         }
01046 
01047         static const char* toString(Image::Error error) noexcept {
01048             switch(error) {
01049             case Image::Error::Ok:
01050                 return "Ok";
01051             case Image::Error::InvalidType:
01052                 return "Unsupported type";
01053             case Image::Error::InvalidPath:
01054                 return "Invalid path";
01055             case Image::Error::FailedToSave:
01056                 return "Error during image saving";
01057             case Image::Error::FailedToLoad:
01058                 return "Error during image loading";
01059             case Image::Error::InvalidImage:
01060                 return "Invalid image";
01061             case Image::Error::InvalidWidth:
01062                 return "Invalid image width";
01063             case Image::Error::InvalidHeight:
01064                 return "Invalid image height";
01065             case Image::Error::InvalidFormat:
01066                 return "Unsupported format";
01067             case Image::Error::InvalidMemory:
01068                 return "Memory error";
01069             case Image::Error::InvalidBitmap:
01070                 return "Bitmap error";
01071             case Image::Error::InvalidArchive:
01072                 return "Archive error";
01073             case Image::Error::InvalidDataPtr:
01074                 return "Bad input data pointer";
01075             case Image::Error::InvalidDataSize:
01076                 return "Bad input data size";
01077             case Image::Error::InvalidConversion:
01078                 return "Required conversion not implemented";
01079             case Image::Error::InvalidDevice:
01080                 return "Selected Target Device is not supported for this system";
01081             case Image::Error::FailedToInitialize:
01082                 return "Error during initialization";
01083             case Image::Error::ReleasedInOtherThread:
01084                 return "Failed to retain image: it was released in another thread.";
01085             case Image::Error::InvalidInput:
01086                 return "InvalidInput";
01087             default:
01088                 return "Unknown error";
01089             }
01090         }
01091     };
01092 
01096     using ImageType = Image::Type;
01097     using ImageError = Image::Error;
01098     using TargetDevice = Image::TargetDevice;
01099     using ImageCompression = Image::ImageCompression;
01100 
01101 } // namespace fsdk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines