1 /*
2  * Copyright (C) 2024 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 IMAGE_EFFECT_COMMON_UTILS_H
17 #define IMAGE_EFFECT_COMMON_UTILS_H
18 
19 #include <memory>
20 
21 #include "any.h"
22 #include "display_type.h"
23 #include "effect_buffer.h"
24 #include "effect_log.h"
25 #include "error_code.h"
26 #include "image_type.h"
27 #include "pixel_map.h"
28 #include "surface.h"
29 #include "effect_memory_manager.h"
30 #include "effect_context.h"
31 #include "image_effect_marco_define.h"
32 #include "effect_json_helper.h"
33 #include "picture.h"
34 
35 namespace OHOS {
36 namespace Media {
37 namespace Effect {
38 class CommonUtils {
39 public:
40     static const int32_t RGBA_BYTES_PER_PIXEL = 4;
41     IMAGE_EFFECT_EXPORT static ErrorCode LockPixelMap(PixelMap *pixelMap, std::shared_ptr<EffectBuffer> &effectBuffer);
42     static ErrorCode ParseSurfaceData(OHOS::SurfaceBuffer *surfaceBuffer,
43         std::shared_ptr<EffectBuffer> &effectBuffer, const DataType &dataType, int64_t timestamp = 0);
44     static std::string UrlToPath(const std::string &url);
45     static ErrorCode ParseUri(std::string &uri, std::shared_ptr<EffectBuffer> &effectBuffer, bool isOutputData);
46     static ErrorCode ParsePath(std::string &path, std::shared_ptr<EffectBuffer> &effectBuffer, bool isOutputData);
47     IMAGE_EFFECT_EXPORT static void UnlockPixelMap(const PixelMap *pixelMap);
48     static ErrorCode ParseAnyAndAddToJson(const std::string &key, Plugin::Any &any, EffectJsonPtr &result);
49     static bool EndsWithJPG(const std::string &input);
50     static bool EndsWithHEIF(const std::string &input);
51     static ErrorCode ModifyPixelMapProperty(PixelMap *pixelMap, const std::shared_ptr<EffectBuffer> &buffer,
52         const std::shared_ptr<EffectMemoryManager> &memoryManager, bool isUpdateExif = true);
53     static ErrorCode ModifyPixelMapPropertyForTexture(PixelMap *pixelMap, const std::shared_ptr<EffectBuffer> &buffer,
54         const std::shared_ptr<EffectContext> &context, bool isUpdateExif = true);
55     static ErrorCode ParseNativeWindowData(std::shared_ptr<EffectBuffer> &effectBuffer, const DataType &dataType);
56     static void UpdateImageExifDateTime(PixelMap *pixelMap);
57     static void UpdateImageExifDateTime(Picture *picture);
58     static void UpdateImageExifInfo(PixelMap *pixelMap);
59     static void UpdateImageExifInfo(Picture *picture);
60     static ErrorCode ParsePicture(Picture *picture, std::shared_ptr<EffectBuffer> &effectBuffer);
61 
ParseAny(Plugin::Any any, ValueType &value)62     template <class ValueType> static ErrorCode ParseAny(Plugin::Any any, ValueType &value)
63     {
64         auto result = Plugin::AnyCast<ValueType>(&any);
65         if (result == nullptr) {
66             EFFECT_LOGE("value type is not match!");
67             return ErrorCode::ERR_ANY_CAST_TYPE_NOT_MATCH;
68         }
69         EFFECT_LOGD("value get success!");
70 
71         value = *result;
72         return ErrorCode::SUCCESS;
73     }
74 
75     template <class ValueType>
GetValue(const std::string &key, std::map<std::string, Plugin::Any> &valueMap, ValueType &value)76     static ErrorCode GetValue(const std::string &key, std::map<std::string, Plugin::Any> &valueMap, ValueType &value)
77     {
78         auto it = valueMap.find(key);
79         if (it == valueMap.end()) {
80             EFFECT_LOGE("key is not set! key=%{public}s", key.c_str());
81             return ErrorCode::ERR_NO_VALUE_KEY;
82         }
83 
84         auto result = Plugin::AnyCast<ValueType>(&it->second);
85         if (result == nullptr) {
86             EFFECT_LOGE("value type is not match! key=%{public}s", key.c_str());
87             return ErrorCode::ERR_ANY_CAST_TYPE_NOT_MATCH;
88         }
89         EFFECT_LOGD("value get success! key=%{public}s", key.c_str());
90 
91         value = *result;
92         return ErrorCode::SUCCESS;
93     }
94 
Clip(float a, float aMin, float aMax)95     static inline float Clip(float a, float aMin, float aMax)
96     {
97         return a > aMax ? aMax : (a < aMin ? aMin : a);
98     }
99 
100     static IEffectFormat SwitchToEffectFormat(GraphicPixelFormat pixelFormat);
101     static IEffectFormat SwitchToEffectFormat(PixelFormat pixelFormat);
102     static GraphicPixelFormat SwitchToGraphicPixelFormat(IEffectFormat formatType);
103     static PixelFormat SwitchToPixelFormat(IEffectFormat formatType);
104     static BufferType SwitchToEffectBuffType(AllocatorType allocatorType);
105 
106 private:
107     static const std::unordered_map<PixelFormat, IEffectFormat> pixelFmtToEffectFmt_;
108     static const std::unordered_map<GraphicPixelFormat, IEffectFormat> surfaceBufferFmtToEffectFmt_;
109     static const std::unordered_map<AllocatorType, BufferType> allocatorTypeToEffectBuffType_;
110 };
111 } // namespace Effect
112 } // namespace Media
113 } // namespace OHOS
114 
115 #endif // IMAGE_EFFECT_COMMON_UTILS_H