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