/* * Copyright (C) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_UTILS_H #define FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_UTILS_H #include #include #include #include "image_type.h" #include "iosfwd" namespace OHOS { namespace MultimediaPlugin { class PluginServer; } } namespace OHOS { namespace ImagePlugin { struct DecodeContext; } } namespace OHOS { namespace Media { const std::string IMAGE_ENCODE_FORMAT = "encodeFormat"; constexpr uint32_t MALLOC_MAX_LENTH = 0x40000000; constexpr int32_t APIVERSION_13 = 13; class PixelMap; class ImageUtils { public: static bool GetFileSize(const std::string &pathName, size_t &size); static bool GetFileSize(const int fd, size_t &size); static bool GetInputStreamSize(std::istream &inputStream, size_t &size); static int32_t GetPixelBytes(const PixelFormat &pixelFormat); static int32_t GetRowDataSizeByPixelFormat(const int32_t &width, const PixelFormat &format); static bool PathToRealPath(const std::string &path, std::string &realPath); static bool FloatCompareZero(float src); static AlphaType GetValidAlphaTypeByFormat(const AlphaType &dstType, const PixelFormat &format); static AllocatorType GetPixelMapAllocatorType(const Size &size, const PixelFormat &format, bool useDMA); static bool IsValidImageInfo(const ImageInfo &info); static bool IsValidAuxiliaryInfo(const std::shared_ptr &pixelMap, const AuxiliaryPictureInfo &info); static bool IsAstc(PixelFormat format); static MultimediaPlugin::PluginServer& GetPluginServer(); static bool CheckMulOverflow(int32_t width, int32_t bytesPerPixel); static bool CheckMulOverflow(int32_t width, int32_t height, int32_t bytesPerPixel); static void BGRAToARGB(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount); static void ARGBToBGRA(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount); static int32_t SurfaceBuffer_Reference(void* buffer); static int32_t SurfaceBuffer_Unreference(void* buffer); static void DumpPixelMap(PixelMap* pixelMap, std::string customFileName, uint64_t imageId = 0); static void DumpPixelMapIfDumpEnabled(std::unique_ptr& pixelMap, uint64_t imageId = 0); static void DumpPixelMapBeforeEncode(PixelMap& pixelMap); static void DumpDataIfDumpEnabled(const char* data, const size_t& totalSize, const std::string& fileSuffix = "dat", uint64_t imageId = 0); static uint64_t GetNowTimeMilliSeconds(); static uint64_t GetNowTimeMicroSeconds(); static std::string GetCurrentProcessName(); // BytesToXXX and xxxToBytes function will modify the offset value. static uint16_t BytesToUint16(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true); static uint32_t BytesToUint32(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true); static int32_t BytesToInt32(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true); static float BytesToFloat(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true); static void Uint16ToBytes(uint16_t data, std::vector& bytes, uint32_t& offset, bool isBigEndian = true); static void Uint32ToBytes(uint32_t data, std::vector& bytes, uint32_t& offset, bool isBigEndian = true); static void FloatToBytes(float data, std::vector& bytes, uint32_t& offset, bool isBigEndian = true); static void Int32ToBytes(int32_t data, std::vector& bytes, uint32_t& offset, bool isBigEndian = true); static void ArrayToBytes(const uint8_t* data, uint32_t length, std::vector& bytes, uint32_t& offset); static void FlushSurfaceBuffer(PixelMap* pixelMap); static void FlushContextSurfaceBuffer(ImagePlugin::DecodeContext& context); static void InvalidateContextSurfaceBuffer(ImagePlugin::DecodeContext& context); static size_t GetAstcBytesCount(const ImageInfo& imageInfo); static bool IsAuxiliaryPictureTypeSupported(AuxiliaryPictureType auxiliaryPictureType); static bool IsAuxiliaryPictureEncoded(AuxiliaryPictureType type); static bool IsMetadataTypeSupported(MetadataType metadataType); static const std::set GetAllAuxiliaryPictureType(); static bool StrToUint32(const std::string& str, uint32_t& value); static bool IsInRange(uint32_t value, uint32_t minValue, uint32_t maxValue); static bool HasOverflowed(uint32_t num1, uint32_t num2); static int32_t GetAPIVersion(); static std::string GetEncodedHeifFormat(); private: static uint32_t RegisterPluginServer(); static uint32_t SaveDataToFile(const std::string& fileName, const char* data, const size_t& totalSize); static std::string GetLocalTime(); static std::string GetPixelMapName(PixelMap* pixelMap); }; } // namespace Media } // namespace OHOS #endif // FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_UTILS_H