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 useDMA);
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 MultimediaPlugin::PluginServer& GetPluginServer();
49     static bool CheckMulOverflow(int32_t width, int32_t bytesPerPixel);
50     static bool CheckMulOverflow(int32_t width, int32_t height, int32_t bytesPerPixel);
51     static void BGRAToARGB(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount);
52     static void ARGBToBGRA(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount);
53     static int32_t SurfaceBuffer_Reference(void* buffer);
54     static int32_t SurfaceBuffer_Unreference(void* buffer);
55     static void DumpPixelMap(PixelMap* pixelMap, std::string customFileName, uint64_t imageId = 0);
56     static void DumpPixelMapIfDumpEnabled(std::unique_ptr<PixelMap>& pixelMap, uint64_t imageId = 0);
57     static void DumpPixelMapBeforeEncode(PixelMap& pixelMap);
58     static void DumpDataIfDumpEnabled(const char* data, const size_t& totalSize, const std::string& fileSuffix = "dat",
59         uint64_t imageId = 0);
60     static uint64_t GetNowTimeMilliSeconds();
61     static uint64_t GetNowTimeMicroSeconds();
62     static std::string GetCurrentProcessName();
63     // BytesToXXX and xxxToBytes function will modify the offset value.
64     static uint16_t BytesToUint16(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true);
65     static uint32_t BytesToUint32(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true);
66     static int32_t BytesToInt32(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true);
67     static float BytesToFloat(uint8_t* bytes, uint32_t& offset, bool isBigEndian = true);
68     static void Uint16ToBytes(uint16_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true);
69     static void Uint32ToBytes(uint32_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true);
70     static void FloatToBytes(float data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true);
71     static void Int32ToBytes(int32_t data, std::vector<uint8_t>& bytes, uint32_t& offset, bool isBigEndian = true);
72     static void ArrayToBytes(const uint8_t* data, uint32_t length, std::vector<uint8_t>& bytes, uint32_t& offset);
73     static void FlushSurfaceBuffer(PixelMap* pixelMap);
74     static void FlushContextSurfaceBuffer(ImagePlugin::DecodeContext& context);
75     static void InvalidateContextSurfaceBuffer(ImagePlugin::DecodeContext& context);
76     static size_t GetAstcBytesCount(const ImageInfo& imageInfo);
77     static bool IsAuxiliaryPictureTypeSupported(AuxiliaryPictureType auxiliaryPictureType);
78     static bool IsAuxiliaryPictureEncoded(AuxiliaryPictureType type);
79     static bool IsMetadataTypeSupported(MetadataType metadataType);
80     static const std::set<AuxiliaryPictureType> GetAllAuxiliaryPictureType();
81     static bool StrToUint32(const std::string& str, uint32_t& value);
82     static bool IsInRange(uint32_t value, uint32_t minValue, uint32_t maxValue);
83     static bool HasOverflowed(uint32_t num1, uint32_t num2);
84     static int32_t GetAPIVersion();
85     static std::string GetEncodedHeifFormat();
86 private:
87     static uint32_t RegisterPluginServer();
88     static uint32_t SaveDataToFile(const std::string& fileName, const char* data, const size_t& totalSize);
89     static std::string GetLocalTime();
90     static std::string GetPixelMapName(PixelMap* pixelMap);
91 };
92 } // namespace Media
93 } // namespace OHOS
94 #endif // FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_UTILS_H
95