1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_UTILS_H 17 #define FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_UTILS_H 18 19 #include <cstdlib> 20 #include <cstdio> 21 #include <string> 22 #include "image_type.h" 23 #include "iosfwd" 24 #if !defined(CROSS_PLATFORM) 25 #include "surface_type.h" 26 #include "surface_buffer.h" 27 #endif 28 29 namespace OHOS { namespace MultimediaPlugin { class PluginServer; } } 30 namespace OHOS { namespace ImagePlugin { struct DecodeContext; } } 31 namespace OHOS { 32 namespace Media { 33 const std::string IMAGE_ENCODE_FORMAT = "encodeFormat"; 34 constexpr uint32_t MALLOC_MAX_LENTH = 0x40000000; 35 constexpr int32_t APIVERSION_13 = 13; 36 constexpr int32_t APIVERSION_20 = 20; 37 class PixelMap; 38 struct InitializationOptions; 39 40 class ImageUtils { 41 public: 42 static bool GetFileSize(const std::string &pathName, size_t &size); 43 static bool GetFileSize(const int fd, size_t &size); 44 static bool GetInputStreamSize(std::istream &inputStream, size_t &size); 45 static int32_t GetPixelBytes(const PixelFormat &pixelFormat); 46 static int32_t GetRowDataSizeByPixelFormat(const int32_t &width, const PixelFormat &format); 47 static bool PathToRealPath(const std::string &path, std::string &realPath); 48 static bool FloatCompareZero(float src); 49 static AlphaType GetValidAlphaTypeByFormat(const AlphaType &dstType, const PixelFormat &format); 50 static AllocatorType GetPixelMapAllocatorType(const Size &size, const PixelFormat &format, bool preferDma); 51 static bool IsValidImageInfo(const ImageInfo &info); 52 static bool IsValidAuxiliaryInfo(const std::shared_ptr<PixelMap> &pixelMap, const AuxiliaryPictureInfo &info); 53 static bool IsAstc(PixelFormat format); 54 static bool IsWidthAligned(const int32_t &width); 55 static bool IsSizeSupportDma(const Size &size); 56 static bool IsFormatSupportDma(const PixelFormat &format); 57 static bool Is10Bit(const PixelFormat &format); 58 static MultimediaPlugin::PluginServer& GetPluginServer(); 59 static bool CheckMulOverflow(int32_t width, int32_t bytesPerPixel); 60 static bool CheckMulOverflow(int32_t width, int32_t height, int32_t bytesPerPixel); 61 static void BGRAToARGB(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount); 62 static void ARGBToBGRA(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount); 63 static int32_t SurfaceBuffer_Reference(void* buffer); 64 static int32_t SurfaceBuffer_Unreference(void* buffer); 65 static void DumpPixelMap(PixelMap* pixelMap, std::string customFileName, uint64_t imageId = 0); 66 static void DumpPixelMapIfDumpEnabled(std::unique_ptr<PixelMap>& pixelMap, uint64_t imageId = 0); 67 static void DumpPixelMapIfDumpEnabled(PixelMap& pixelMap, std::string func); 68 static void DumpPixelMapBeforeEncode(PixelMap& pixelMap); 69 static void DumpDataIfDumpEnabled(const char* data, const size_t& totalSize, const std::string& fileSuffix = "dat", 70 uint64_t imageId = 0); 71 static uint64_t GetNowTimeMilliSeconds(); 72 static uint64_t GetNowTimeMicroSeconds(); 73 static std::string GetCurrentProcessName(); 74 static bool SetInitializationOptionAutoMem(InitializationOptions &option); 75 static bool SetInitializationOptionDmaMem(InitializationOptions &option); 76 static bool SetInitializationOptionAllocatorType(InitializationOptions &option, int32_t allocatorType); 77 // BytesToXXX and xxxToBytes function will modify the offset value. 78 static uint16_t BytesToUint16(uint8_t* bytes, uint32_t& offset, uint32_t size, bool isBigEndian = true); 79 static uint32_t BytesToUint32(uint8_t* bytes, uint32_t& offset, uint32_t size, bool isBigEndian = true); 80 static int32_t BytesToInt32(uint8_t* bytes, uint32_t& offset, uint32_t size, bool isBigEndian = true); 81 static float BytesToFloat(uint8_t* bytes, uint32_t& offset, uint32_t size, bool isBigEndian = true); 82 static void Uint16ToBytes(uint16_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 83 static void Uint32ToBytes(uint32_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 84 static void FloatToBytes(float data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 85 static void Int32ToBytes(int32_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 86 static void ArrayToBytes(const uint8_t* data, uint32_t length, std::vector<uint8_t>& bytes, uint32_t& offset); 87 static void FlushSurfaceBuffer(PixelMap* pixelMap); 88 static void FlushContextSurfaceBuffer(ImagePlugin::DecodeContext& context); 89 static void InvalidateContextSurfaceBuffer(ImagePlugin::DecodeContext& context); 90 static bool IsAuxiliaryPictureTypeSupported(AuxiliaryPictureType auxiliaryPictureType); 91 static bool IsAuxiliaryPictureEncoded(AuxiliaryPictureType type); 92 static bool IsMetadataTypeSupported(MetadataType metadataType); 93 static const std::set<AuxiliaryPictureType> GetAllAuxiliaryPictureType(); 94 static size_t GetAstcBytesCount(const ImageInfo& imageInfo); 95 static bool StrToUint32(const std::string& str, uint32_t& value); 96 static bool IsInRange(uint32_t value, uint32_t minValue, uint32_t maxValue); 97 static bool IsInRange(int32_t value, int32_t minValue, int32_t maxValue); 98 static bool IsEven(int32_t value); 99 static bool HasOverflowed(uint32_t num1, uint32_t num2); 100 static int32_t GetAPIVersion(); 101 static std::string GetEncodedHeifFormat(); 102 static void UpdateSdrYuvStrides(const ImageInfo &imageInfo, YUVStrideInfo &dstStrides, 103 void *context, AllocatorType dstType); 104 static bool CanReusePixelMap(ImagePlugin::DecodeContext& context, int width, 105 int height, const std::shared_ptr<PixelMap> &reusePixelmap); 106 static bool CanReusePixelMapHdr(ImagePlugin::DecodeContext& context, int width, 107 int height, const std::shared_ptr<PixelMap> &reusePixelmap); 108 static bool CanReusePixelMapSdr(ImagePlugin::DecodeContext& context, int width, 109 int height, const std::shared_ptr<PixelMap> &reusePixelmap); 110 static bool IsHdrPixelMapReuseSuccess(ImagePlugin::DecodeContext& context, int width, 111 int height, const std::shared_ptr<PixelMap> &reusePixelmap); 112 static void SetContextHdr(ImagePlugin::DecodeContext& context, uint32_t format); 113 static void SetReuseContextBuffer(ImagePlugin::DecodeContext& context, 114 AllocatorType type, uint8_t* ptr, uint64_t count, void* fd); 115 static bool IsSdrPixelMapReuseSuccess(ImagePlugin::DecodeContext& context, int width, 116 int height, const std::shared_ptr<PixelMap> &reusePixelmap); 117 static bool IsReuseYUV(ImagePlugin::DecodeContext& context, const std::shared_ptr<PixelMap> &reusePixelmap); 118 static bool IsReuseRGB(ImagePlugin::DecodeContext& context, const std::shared_ptr<PixelMap> &reusePixelmap); 119 static bool IsYUV(PixelFormat format); 120 static bool IsRGBX(PixelFormat format); 121 static bool PixelMapCreateCheckFormat(PixelFormat format); 122 static bool CheckTlvSupportedFormat(PixelFormat format); 123 static uint16_t GetReusePixelRefCount(const std::shared_ptr<PixelMap> &reusePixelmap); 124 static bool CheckRowDataSizeIsVaild(int32_t &rowDataSize, ImageInfo &imgInfo); 125 static bool CheckBufferSizeIsVaild(int32_t &bufferSize, uint64_t &expectedBufferSize, 126 AllocatorType &allocatorType); 127 static bool GetAlignedNumber(int32_t& number, int32_t align); 128 static int32_t GetByteCount(ImageInfo imageInfo); 129 static int32_t GetYUVByteCount(const ImageInfo& info); 130 131 template<typename T> CheckMulOverflow(const T & num1,const T & num2)132 static bool CheckMulOverflow(const T& num1, const T& num2) 133 { 134 if (num1 == 0 || num2 == 0) { 135 return true; 136 } 137 T mulNum = num1 * num2; 138 if ((mulNum / num1) != num2) { 139 return true; 140 } 141 return false; 142 } 143 144 template<typename T> CheckMulOverflow(const T & num1,const T & num2,const T & num3)145 static bool CheckMulOverflow(const T& num1, const T& num2, const T& num3) 146 { 147 if (num1 == 0 || num2 == 0 || num3 == 0) { 148 return true; 149 } 150 T mulNum1 = num1 * num2; 151 if ((mulNum1 / num1) != num2) { 152 return true; 153 } 154 T mulNum2 = mulNum1 * num3; 155 if ((mulNum2 / num3) != mulNum1) { 156 return true; 157 } 158 return false; 159 } 160 static uint16_t GetRGBA1010102ColorR(uint32_t color); 161 static uint16_t GetRGBA1010102ColorG(uint32_t color); 162 static uint16_t GetRGBA1010102ColorB(uint32_t color); 163 static uint16_t GetRGBA1010102ColorA(uint32_t color); 164 #if !defined(CROSS_PLATFORM) 165 static void FlushSurfaceBuffer(sptr<SurfaceBuffer>& surfaceBuffer); 166 #endif 167 private: 168 static uint32_t RegisterPluginServer(); 169 static uint32_t SaveDataToFile(const std::string& fileName, const char* data, const size_t& totalSize); 170 static std::string GetLocalTime(); 171 static std::string GetPixelMapName(PixelMap* pixelMap); 172 static int32_t GetAPIVersionInner(); 173 }; 174 } // namespace Media 175 } // namespace OHOS 176 #endif // FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_UTILS_H 177