Face Engine SDK  5.23.1
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             ReleasedInOtherThread 
00100         };
00101 
00107         enum class MemoryResidence : uint32_t {
00108             MemoryCPU,     
00109             MemoryGPU,     
00110             MemoryNPU,     
00111             MemoryNPU_DPP, 
00112         };
00113 
00116         FSDK_API Image() noexcept;
00117 
00128         FSDK_API Image(
00129             int width,
00130             int height,
00131             Format format,
00132             MemoryResidence residence = MemoryResidence::MemoryCPU,
00133             int deviceId = 0) noexcept;
00134 
00147         FSDK_API Image(
00148             int width,
00149             int height,
00150             Format format,
00151             const void* data,
00152             MemoryResidence residence = MemoryResidence::MemoryCPU,
00153             int deviceId = 0) noexcept;
00154 
00172         FSDK_API Image(
00173             int width,
00174             int height,
00175             Format format,
00176             void* data,
00177             bool copy,
00178             MemoryResidence residence = MemoryResidence::MemoryCPU,
00179             int deviceId = 0) noexcept;
00180 
00185         FSDK_API Image(const Image& other) noexcept;
00186 
00191         FSDK_API Image(Image&& other) noexcept;
00192 
00198         FSDK_API explicit Image(const SubImage& subImage) noexcept;
00199 
00200         FSDK_API ~Image();
00201 
00216         FSDK_API Result<Error> create(
00217             int width,
00218             int height,
00219             Format format,
00220             bool cleanup = true,
00221             MemoryResidence residence = MemoryResidence::MemoryCPU,
00222             int deviceId = 0) noexcept;
00223 
00239         FSDK_API Result<Error> create(
00240             int width,
00241             int height,
00242             Format format,
00243             const void* data,
00244             MemoryResidence residence = MemoryResidence::MemoryCPU,
00245             int deviceId = 0) noexcept;
00246 
00266         FSDK_API Result<Error> create(
00267             int width,
00268             int height,
00269             Format format,
00270             void* data,
00271             bool copy,
00272             MemoryResidence residence = MemoryResidence::MemoryCPU,
00273             int deviceId = 0) noexcept;
00274 
00284         FSDK_API Result<Error>
00285         create(const fsdk::Image& source, MemoryResidence residence, int deviceId = 0) noexcept;
00286 
00304         FSDK_API Result<Error> set(
00305             int width,
00306             int height,
00307             Format format,
00308             const void* data,
00309             MemoryResidence residence = MemoryResidence::MemoryCPU,
00310             int deviceId = 0) noexcept;
00311 
00333         FSDK_API Result<Error> set(
00334             int width,
00335             int height,
00336             Format format,
00337             void* data,
00338             bool copy,
00339             MemoryResidence residence = MemoryResidence::MemoryCPU,
00340             int deviceId = 0) noexcept;
00341 
00347         FSDK_API Result<Error> set(const Image& other) noexcept;
00348 
00354         FSDK_API Result<Error> set(const SubImage& subImage) noexcept;
00355 
00368         FSDK_API SubImage map(int x, int y, int width, int height) const noexcept;
00369 
00377         SubImage map(const Rect& rect) const noexcept {
00378             return map(rect.x, rect.y, rect.width, rect.height);
00379         }
00380 
00390         SubImage map(const Size& size) const noexcept {
00391             return map(Point2i(0, 0), size);
00392         }
00393 
00402         SubImage map(const Point2i& origin, const Size& size) const noexcept {
00403             return map(origin.x, origin.y, size.x, size.y);
00404         }
00405 
00419         FSDK_API Image extract(int x, int y, int width, int height) const noexcept;
00420 
00429         Image extract(const Rect& rect) const noexcept {
00430             return extract(rect.x, rect.y, rect.width, rect.height);
00431         }
00432 
00443         Image extract(const Size& size) const noexcept {
00444             return extract(Point2i(0, 0), size);
00445         }
00446 
00456         Image extract(const Point2i& origin, const Size& size) const noexcept {
00457             return extract(origin.x, origin.y, size.x, size.y);
00458         }
00459 
00467         FSDK_API static Result<Error>
00468         guessTypeFromMemory(const void* data, const uint32_t sizeInBytes, Type& type) noexcept;
00469 
00475         Image clone() const noexcept {
00476             return {getWidth(), getHeight(), getFormat(), getData(), getMemoryResidence(), getDeviceId()};
00477         }
00478 
00497         FSDK_API Result<Image::Error> convert(
00498             Image& dest,
00499             int x,
00500             int y,
00501             int width,
00502             int height,
00503             Format format,
00504             TargetDevice device = TargetDevice::CPU) const noexcept;
00505 
00520         Result<Image::Error> convert(
00521             Image& dest,
00522             const Point2i& origin,
00523             const Size& size,
00524             Format format,
00525             TargetDevice device = TargetDevice::CPU) const noexcept {
00526             return convert(dest, origin.x, origin.y, size.x, size.y, format, device);
00527         }
00528 
00544         Result<Image::Error>
00545         convert(Image& dest, const Size& size, Format format, TargetDevice device = TargetDevice::CPU)
00546             const noexcept {
00547             return convert(dest, Point2i(0, 0), size, format, device);
00548         }
00549 
00563         Result<Image::Error>
00564         convert(Image& dest, const Rect& rect, Format format, TargetDevice device = TargetDevice::CPU)
00565             const noexcept {
00566             return convert(dest, rect.x, rect.y, rect.width, rect.height, format, device);
00567         }
00568 
00579         Result<Image::Error>
00580         convert(Image& dest, Format format, TargetDevice device = TargetDevice::CPU) const noexcept {
00581             return convert(dest, getRect(), format, device);
00582         }
00583 
00589         FSDK_API Image rescale(float scale) const noexcept;
00590 
00601         FSDK_API Result<Image::Error> save(
00602             const char* path,
00603             fsdk::Image::ImageCompression additionalFlag =
00604                 fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept;
00605 
00616         FSDK_API Result<Error> save(const char* path, const Format format) const noexcept;
00617 
00628         FSDK_API Result<Image::Error> saveToMemory(
00629             Image::Type type,
00630             IArchive* archive,
00631             fsdk::Image::ImageCompression additionalFlag =
00632                 fsdk::Image::ImageCompression::IC_NO_COMPRESSION) const noexcept;
00633 
00645         FSDK_API Result<Image::Error>
00646         saveToMemory(Image::Type type, IArchive* archive, const Format format) const noexcept;
00647 
00660         FSDK_API Result<Error> load(const char* path) noexcept;
00661 
00671         FSDK_API Result<Error> load(const char* path, const Format format) noexcept;
00672 
00686         FSDK_API Result<Error> loadFromMemory(const void* data, const uint32_t sizeInBytes) noexcept;
00687 
00697         FSDK_API Result<Error> rotate(Image& dest, RotationType rotationType) const noexcept;
00698 
00710         FSDK_API Result<Error>
00711         loadFromMemory(const void* data, const uint32_t sizeInBytes, const Format format) noexcept;
00712 
00728         FSDK_API Result<Error>
00729         loadFromMemoryOfType(const void* data, const uint32_t sizeInBytes, const Type type) noexcept;
00730 
00742         FSDK_API Result<Error> loadFromMemoryOfType(
00743             const void* data,
00744             const uint32_t sizeInBytes,
00745             const Type type,
00746             const Format format) noexcept;
00747 
00751         Image& operator=(const Image& other) noexcept {
00752             set(other);
00753 
00754             return *this;
00755         }
00756 
00760         Image& operator=(Image&& other) noexcept {
00761             if(this != &other) {
00762                 release();
00763                 swap(other);
00764             }
00765 
00766             return *this;
00767         }
00768 
00770         bool isNull() const noexcept {
00771             return getData() == nullptr;
00772         }
00773 
00777         bool isValid() const noexcept {
00778             return !isNull() && getHeight() > 0 && getWidth() > 0 && getFormat().isValid();
00779         }
00780 
00784         operator bool() const noexcept {
00785             return isValid();
00786         }
00787 
00792         FSDK_API void* getScanLine(int y) noexcept;
00793 
00798         FSDK_API const void* getScanLine(int y) const noexcept;
00799 
00802         FSDK_API int getDataSize() const noexcept;
00803 
00807         FSDK_API void getDataSize(Sizer& sizer) const noexcept;
00808 
00813         template <typename T>
00814         T* getScanLineAs(int y) noexcept {
00815             return reinterpret_cast<T*>(getScanLine(y));
00816         }
00817 
00822         template <typename T>
00823         const T* getScanLineAs(int y) const noexcept {
00824             return reinterpret_cast<const T*>(getScanLine(y));
00825         }
00826 
00829         void* getData() noexcept {
00830             return m_data;
00831         }
00832 
00835         const void* getData() const noexcept {
00836             return m_data;
00837         }
00838 
00841         template <typename T>
00842         T* getDataAs() noexcept {
00843             return reinterpret_cast<T*>(getData());
00844         }
00845 
00848         template <typename T>
00849         const T* getDataAs() const noexcept {
00850             return reinterpret_cast<const T*>(getData());
00851         }
00852 
00854         int getRowSize() const noexcept {
00855             return getFormat().computePitch(getWidth());
00856         }
00857 
00859         int getWidth() const noexcept {
00860             return m_width;
00861         }
00862 
00864         int getHeight() const noexcept {
00865             return m_height;
00866         }
00867 
00869         float getAspectRatio() const noexcept {
00870             return static_cast<float>(getWidth()) / static_cast<float>(getHeight());
00871         }
00872 
00874         Format getFormat() const noexcept {
00875             return m_format;
00876         }
00877 
00878         MemoryResidence getMemoryResidence() const noexcept {
00879             return m_residence;
00880         }
00881 
00882         int getDeviceId() const noexcept {
00883             return m_deviceId;
00884         }
00885 
00887         Size getSize() const noexcept {
00888             return Size(getWidth(), getHeight());
00889         }
00890 
00894         Rect getRect() const noexcept {
00895             return Rect(0, 0, getWidth(), getHeight());
00896         }
00897 
00902         bool ownsData() const noexcept {
00903             return !!m_ref;
00904         }
00905 
00910         bool isSharedWith(const Image& other) const noexcept {
00911             return getData() == other.getData();
00912         }
00913 
00917         void swap(Image& other) noexcept {
00918             std::swap(m_data, other.m_data);
00919             std::swap(m_ref, other.m_ref);
00920             std::swap(m_height, other.m_height);
00921             std::swap(m_width, other.m_width);
00922             std::swap(m_format, other.m_format);
00923             std::swap(m_residence, other.m_residence);
00924         }
00925 
00928         void reset() noexcept {
00929             Image().swap(*this);
00930         }
00931 
00937         bool equalWeak(const fsdk::Image& other) const noexcept {
00938             return m_width == other.getWidth() && m_height == other.getHeight() && m_format == other.getFormat() &&
00939                          m_residence == other.getMemoryResidence();
00940         }
00941 
00947         bool equalStrong(const fsdk::Image& other) const noexcept {
00948             return equalWeak(other) && m_data == other.getData();
00949         }
00950 
00951         FSDK_API void putPixel(uint32_t x, uint32_t y) noexcept;
00952 
00953     protected:
00954         void* m_data;    
00955         int* m_ref;      
00956         int m_height;    
00957         int m_width;     
00958         int m_deviceId;  
00959         Format m_format; 
00960         MemoryResidence m_residence;
00961 
00966         FSDK_API static void* allocate(int size) noexcept;
00967 
00971         FSDK_API static void deallocate(void* memory) noexcept;
00972 
00976         FSDK_API int retain() noexcept;
00977 
00981         FSDK_API int release() noexcept;
00982 
00986         FSDK_API int getRefCount() const noexcept;
00987     };
00988 
00993     const char* toString(Image::MemoryResidence residence);
00994 
00998     template <>
00999     struct ErrorTraits<Image::Error> {
01000 
01001         static bool isOk(Image::Error error) noexcept {
01002             return error == Image::Error::Ok;
01003         }
01004 
01005         static const char* toString(Image::Error error) noexcept {
01006             switch(error) {
01007             case Image::Error::Ok:
01008                 return "Ok";
01009             case Image::Error::InvalidType:
01010                 return "Unsupported type";
01011             case Image::Error::InvalidPath:
01012                 return "Invalid path";
01013             case Image::Error::FailedToSave:
01014                 return "Error during image saving";
01015             case Image::Error::FailedToLoad:
01016                 return "Error during image loading";
01017             case Image::Error::InvalidImage:
01018                 return "Invalid image";
01019             case Image::Error::InvalidWidth:
01020                 return "Invalid image width";
01021             case Image::Error::InvalidHeight:
01022                 return "Invalid image height";
01023             case Image::Error::InvalidFormat:
01024                 return "Unsupported format";
01025             case Image::Error::InvalidMemory:
01026                 return "Memory error";
01027             case Image::Error::InvalidBitmap:
01028                 return "Bitmap error";
01029             case Image::Error::InvalidArchive:
01030                 return "Archive error";
01031             case Image::Error::InvalidDataPtr:
01032                 return "Bad input data pointer";
01033             case Image::Error::InvalidDataSize:
01034                 return "Bad input data size";
01035             case Image::Error::InvalidConversion:
01036                 return "Required conversion not implemented";
01037             case Image::Error::InvalidDevice:
01038                 return "Selected Target Device is not supported for this system";
01039             case Image::Error::FailedToInitialize:
01040                 return "Error during initialization";
01041             case Image::Error::ReleasedInOtherThread:
01042                 return "Failed to retain image: it was released in another thread.";
01043             default:
01044                 return "Unknown error";
01045             }
01046         }
01047     };
01048 
01052     using ImageType = Image::Type;
01053     using ImageError = Image::Error;
01054     using TargetDevice = Image::TargetDevice;
01055     using ImageCompression = Image::ImageCompression;
01056 
01057 } // namespace fsdk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines