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 25 namespace OHOS { namespace MultimediaPlugin { class PluginServer; } } 26 namespace OHOS { namespace ImagePlugin { struct DecodeContext; } } 27 namespace OHOS { 28 namespace Media { 29 const std::string IMAGE_ENCODE_FORMAT = "encodeFormat"; 30 constexpr uint32_t MALLOC_MAX_LENTH = 0x40000000; 31 constexpr int32_t APIVERSION_13 = 13; 32 class PixelMap; 33 34 class ImageUtils { 35 public: 36 static bool GetFileSize(const std::string &pathName, size_t &size); 37 static bool GetFileSize(const int fd, size_t &size); 38 static bool GetInputStreamSize(std::istream &inputStream, size_t &size); 39 static int32_t GetPixelBytes(const PixelFormat &pixelFormat); 40 static int32_t GetRowDataSizeByPixelFormat(const int32_t &width, const PixelFormat &format); 41 static bool PathToRealPath(const std::string &path, std::string &realPath); 42 static bool FloatCompareZero(float src); 43 static AlphaType GetValidAlphaTypeByFormat(const AlphaType &dstType, const PixelFormat &format); 44 static AllocatorType GetPixelMapAllocatorType(const Size &size, const PixelFormat &format, bool preferDma); 45 static bool IsValidImageInfo(const ImageInfo &info); 46 static bool IsValidAuxiliaryInfo(const std::shared_ptr<PixelMap> &pixelMap, const AuxiliaryPictureInfo &info); 47 static bool IsAstc(PixelFormat format); 48 static bool IsWidthAligned(const int32_t &width); 49 static bool IsSizeSupportDma(const Size &size); 50 static bool IsFormatSupportDma(const PixelFormat &format); 51 static bool Is10Bit(const PixelFormat &format); 52 static MultimediaPlugin::PluginServer& GetPluginServer(); 53 static bool CheckMulOverflow(int32_t width, int32_t bytesPerPixel); 54 static bool CheckMulOverflow(int32_t width, int32_t height, int32_t bytesPerPixel); 55 static void BGRAToARGB(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount); 56 static void ARGBToBGRA(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount); 57 static int32_t SurfaceBuffer_Reference(void* buffer); 58 static int32_t SurfaceBuffer_Unreference(void* buffer); 59 static void DumpPixelMap(PixelMap* pixelMap, std::string customFileName, uint64_t imageId = 0); 60 static void DumpPixelMapIfDumpEnabled(std::unique_ptr<PixelMap>& pixelMap, uint64_t imageId = 0); 61 static void DumpPixelMapBeforeEncode(PixelMap& pixelMap); 62 static void DumpDataIfDumpEnabled(const char* data, const size_t& totalSize, const std::string& fileSuffix = "dat", 63 uint64_t imageId = 0); 64 static uint64_t GetNowTimeMilliSeconds(); 65 static uint64_t GetNowTimeMicroSeconds(); 66 static std::string GetCurrentProcessName(); 67 // BytesToXXX and xxxToBytes function will modify the offset value. 68 static uint16_t BytesToUint16(uint8_t* bytes, uint32_t& offset, uint32_t size, bool isBigEndian = true); 69 static uint32_t BytesToUint32(uint8_t* bytes, uint32_t& offset, uint32_t size, bool isBigEndian = true); 70 static int32_t BytesToInt32(uint8_t* bytes, uint32_t& offset, uint32_t size, bool isBigEndian = true); 71 static float BytesToFloat(uint8_t* bytes, uint32_t& offset, uint32_t size, bool isBigEndian = true); 72 static void Uint16ToBytes(uint16_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 73 static void Uint32ToBytes(uint32_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 74 static void FloatToBytes(float data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 75 static void Int32ToBytes(int32_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 76 static void ArrayToBytes(const uint8_t* data, uint32_t length, std::vector<uint8_t>& bytes, uint32_t& offset); 77 static void FlushSurfaceBuffer(PixelMap* pixelMap); 78 static void FlushContextSurfaceBuffer(ImagePlugin::DecodeContext& context); 79 static void InvalidateContextSurfaceBuffer(ImagePlugin::DecodeContext& context); 80 static bool IsAuxiliaryPictureTypeSupported(AuxiliaryPictureType auxiliaryPictureType); 81 static bool IsAuxiliaryPictureEncoded(AuxiliaryPictureType type); 82 static bool IsMetadataTypeSupported(MetadataType metadataType); 83 static const std::set<AuxiliaryPictureType> GetAllAuxiliaryPictureType(); 84 static size_t GetAstcBytesCount(const ImageInfo& imageInfo); 85 static bool StrToUint32(const std::string& str, uint32_t& value); 86 static bool IsInRange(uint32_t value, uint32_t minValue, uint32_t maxValue); 87 static bool IsInRange(int32_t value, int32_t minValue, int32_t maxValue); 88 static bool IsEven(int32_t value); 89 static bool HasOverflowed(uint32_t num1, uint32_t num2); 90 static int32_t GetAPIVersion(); 91 static std::string GetEncodedHeifFormat(); 92 static void UpdateSdrYuvStrides(const ImageInfo &imageInfo, YUVStrideInfo &dstStrides, 93 void *context, AllocatorType dstType); 94 static bool CanReusePixelMap(ImagePlugin::DecodeContext& context, int width, 95 int height, const std::shared_ptr<PixelMap> &reusePixelmap); 96 static bool CanReusePixelMapHdr(ImagePlugin::DecodeContext& context, int width, 97 int height, const std::shared_ptr<PixelMap> &reusePixelmap); 98 static bool CanReusePixelMapSdr(ImagePlugin::DecodeContext& context, int width, 99 int height, const std::shared_ptr<PixelMap> &reusePixelmap); 100 static bool IsHdrPixelMapReuseSuccess(ImagePlugin::DecodeContext& context, int width, 101 int height, const std::shared_ptr<PixelMap> &reusePixelmap); 102 static void SetContextHdr(ImagePlugin::DecodeContext& context, uint32_t format); 103 static void SetReuseContextBuffer(ImagePlugin::DecodeContext& context, 104 AllocatorType type, uint8_t* ptr, uint64_t count, void* fd); 105 static bool IsSdrPixelMapReuseSuccess(ImagePlugin::DecodeContext& context, int width, 106 int height, const std::shared_ptr<PixelMap> &reusePixelmap); 107 static bool IsReuseYUV(ImagePlugin::DecodeContext& context, const std::shared_ptr<PixelMap> &reusePixelmap); 108 static bool IsReuseRGB(ImagePlugin::DecodeContext& context, const std::shared_ptr<PixelMap> &reusePixelmap); 109 static bool IsYUV(PixelFormat format); 110 static bool IsRGBX(PixelFormat format); 111 static bool PixelMapCreateCheckFormat(PixelFormat format); 112 static bool CheckTlvSupportedFormat(PixelFormat format); 113 static uint16_t GetReusePixelRefCount(const std::shared_ptr<PixelMap> &reusePixelmap); 114 115 template<typename T> CheckMulOverflow(const T & num1,const T & num2)116 static bool CheckMulOverflow(const T& num1, const T& num2) 117 { 118 if (num1 == 0 || num2 == 0) { 119 return true; 120 } 121 T mulNum = num1 * num2; 122 if ((mulNum / num1) != num2) { 123 return true; 124 } 125 return false; 126 } 127 128 template<typename T> CheckMulOverflow(const T & num1,const T & num2,const T & num3)129 static bool CheckMulOverflow(const T& num1, const T& num2, const T& num3) 130 { 131 if (num1 == 0 || num2 == 0 || num3 == 0) { 132 return true; 133 } 134 T mulNum1 = num1 * num2; 135 if ((mulNum1 / num1) != num2) { 136 return true; 137 } 138 T mulNum2 = mulNum1 * num3; 139 if ((mulNum2 / num3) != mulNum1) { 140 return true; 141 } 142 return false; 143 } 144 private: 145 static uint32_t RegisterPluginServer(); 146 static uint32_t SaveDataToFile(const std::string& fileName, const char* data, const size_t& totalSize); 147 static std::string GetLocalTime(); 148 static std::string GetPixelMapName(PixelMap* pixelMap); 149 }; 150 } // namespace Media 151 } // namespace OHOS 152 #endif // FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_UTILS_H 153