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 class PixelMap; 32 33 class ImageUtils { 34 public: 35 static bool GetFileSize(const std::string &pathName, size_t &size); 36 static bool GetFileSize(const int fd, size_t &size); 37 static bool GetInputStreamSize(std::istream &inputStream, size_t &size); 38 static int32_t GetPixelBytes(const PixelFormat &pixelFormat); 39 static int32_t GetRowDataSizeByPixelFormat(int32_t width, PixelFormat format); 40 static bool PathToRealPath(const std::string &path, std::string &realPath); 41 static bool FloatCompareZero(float src); 42 static AlphaType GetValidAlphaTypeByFormat(const AlphaType &dstType, const PixelFormat &format); 43 static AllocatorType GetPixelMapAllocatorType(const Size &size, const PixelFormat &format, bool preferDma); 44 static bool IsValidImageInfo(const ImageInfo &info); 45 static bool IsWidthAligned(const int32_t &width); 46 static bool IsSizeSupportDma(const Size &size); 47 static bool IsFormatSupportDma(const PixelFormat &format); 48 static bool Is10Bit(const PixelFormat &format); 49 static MultimediaPlugin::PluginServer& GetPluginServer(); 50 static bool CheckMulOverflow(int32_t width, int32_t bytesPerPixel); 51 static bool CheckMulOverflow(int32_t width, int32_t height, int32_t bytesPerPixel); 52 static void BGRAToARGB(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount); 53 static void ARGBToBGRA(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount); 54 static int32_t SurfaceBuffer_Reference(void* buffer); 55 static int32_t SurfaceBuffer_Unreference(void* buffer); 56 static void DumpPixelMap(PixelMap* pixelMap, std::string customFileName, uint64_t imageId = 0); 57 static void DumpPixelMapIfDumpEnabled(std::unique_ptr<PixelMap>& pixelMap, uint64_t imageId = 0); 58 static void DumpPixelMapBeforeEncode(PixelMap& pixelMap); 59 static void DumpDataIfDumpEnabled(const char* data, const size_t& totalSize, const std::string& fileSuffix = "dat", 60 uint64_t imageId = 0); 61 static uint64_t GetNowTimeMilliSeconds(); 62 static uint64_t GetNowTimeMicroSeconds(); 63 static std::string GetCurrentProcessName(); 64 // BytesToXXX and xxxToBytes function will modify the offset value. 65 static uint16_t BytesToUint16(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true); 66 static uint32_t BytesToUint32(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true); 67 static int32_t BytesToInt32(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true); 68 static float BytesToFloat(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true); 69 static void Uint16ToBytes(uint16_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 70 static void Uint32ToBytes(uint32_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 71 static void FloatToBytes(float data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 72 static void Int32ToBytes(int32_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true); 73 static void ArrayToBytes(const uint8_t* data, uint32_t length, std::vector<uint8_t>& bytes, uint32_t& offset); 74 static int32_t KMPFind(const uint8_t* target, uint32_t targetLen, const uint8_t* pattern, uint32_t patternLen); 75 static void FlushSurfaceBuffer(PixelMap* pixelMap); 76 static void FlushContextSurfaceBuffer(ImagePlugin::DecodeContext& context); 77 static void InvalidateContextSurfaceBuffer(ImagePlugin::DecodeContext& context); 78 static bool IsAuxiliaryPictureTypeSupported(AuxiliaryPictureType auxiliaryPictureType); 79 static bool IsAuxiliaryPictureEncoded(AuxiliaryPictureType type); 80 static bool IsMetadataTypeSupported(MetadataType metadataType); 81 static const std::set<AuxiliaryPictureType> GetAllAuxiliaryPictureType(); 82 static size_t GetAstcBytesCount(const ImageInfo& imageInfo); 83 static bool StrToUint32(const std::string& str, uint32_t& value); 84 static bool IsInRange(uint32_t value, uint32_t minValue, uint32_t maxValue); 85 private: 86 static uint32_t RegisterPluginServer(); 87 static uint32_t SaveDataToFile(const std::string& fileName, const char* data, const size_t& totalSize); 88 static std::string GetLocalTime(); 89 static std::string GetPixelMapName(PixelMap* pixelMap); 90 }; 91 } // namespace Media 92 } // namespace OHOS 93 #endif // FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_UTILS_H 94