• 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 #include <cstdarg>
21 
22 #include "any.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 
43     static void CopyBufferInfo(const BufferInfo& src, BufferInfo& dst);
44     static void CopyExtraInfo(const ExtraInfo& src, ExtraInfo& dst);
45     static void CopyAuxiliaryBufferInfos(const EffectBuffer *src, EffectBuffer *dst);
46 
47     static MetaDataMap GetMetaData(SurfaceBuffer *surfaceBuffer);
48     static void SetMetaData(MetaDataMap &metaData, SurfaceBuffer *surfaceBuffer);
49 
50     static ErrorCode ParsePixelMapData(PixelMap *pixelMap, std::shared_ptr<EffectBuffer> &effectBuffer);
51     IMAGE_EFFECT_EXPORT static ErrorCode LockPixelMap(PixelMap *pixelMap, std::shared_ptr<EffectBuffer> &effectBuffer);
52     static ErrorCode ParseSurfaceData(OHOS::SurfaceBuffer *surfaceBuffer, std::shared_ptr<EffectBuffer> &effectBuffer,
53         const DataType &dataType, LOG_STRATEGY strategy = LOG_STRATEGY::NORMAL, int64_t timestamp = 0);
54     static std::string UrlToPath(const std::string &url);
55     static ErrorCode ParseUri(std::string &uri, std::shared_ptr<EffectBuffer> &effectBuffer, bool isOutputData,
56         IEffectFormat format);
57     static ErrorCode ParsePath(std::string &path, std::shared_ptr<EffectBuffer> &effectBuffer, bool isOutputData,
58         IEffectFormat format);
59     IMAGE_EFFECT_EXPORT static ErrorCode ParseTex(unsigned int textureId, unsigned int colorSpace,
60         std::shared_ptr<EffectBuffer> &effectBuffer);
61     IMAGE_EFFECT_EXPORT static void UnlockPixelMap(const PixelMap *pixelMap);
62     static ErrorCode ParseAnyAndAddToJson(const std::string &key, Any &any, EffectJsonPtr &result);
63     static bool EndsWithJPG(const std::string &input);
64     static bool EndsWithHEIF(const std::string &input);
65     static ErrorCode ModifyPixelMapProperty(PixelMap *pixelMap, const std::shared_ptr<EffectBuffer> &buffer,
66         const std::shared_ptr<EffectContext> &context, bool isUpdateExif = true);
67     static ErrorCode ModifyPixelMapPropertyForTexture(PixelMap *pixelMap, const std::shared_ptr<EffectBuffer> &buffer,
68         const std::shared_ptr<EffectContext> &context, bool isUpdateExif = true);
69     static ErrorCode ParseNativeWindowData(std::shared_ptr<EffectBuffer> &effectBuffer, const DataType &dataType);
70     static void UpdateImageExifDateTime(PixelMap *pixelMap);
71     static void UpdateImageExifDateTime(Picture *picture);
72     static void UpdateImageExifInfo(PixelMap *pixelMap);
73     static void UpdateImageExifInfo(Picture *picture);
74     static ErrorCode ParsePicture(Picture *picture, std::shared_ptr<EffectBuffer> &effectBuffer);
75     static bool IsEnableCopyMetaData(int numBuffers, ...);
76     IMAGE_EFFECT_EXPORT static void CopyTexture(const std::shared_ptr<EffectContext> &context, RenderTexturePtr input,
77         RenderTexturePtr output);
78 
79     static std::shared_ptr<ImageSource> GetImageSourceFromPath(std::string path);
80 
ParseAny(Any any,ValueType & value)81     template <class ValueType> static ErrorCode ParseAny(Any any, ValueType &value)
82     {
83         auto result = AnyCast<ValueType>(&any);
84         if (result == nullptr) {
85             EFFECT_LOGE("value type is not match!");
86             return ErrorCode::ERR_ANY_CAST_TYPE_NOT_MATCH;
87         }
88         EFFECT_LOGD("value get success!");
89 
90         value = *result;
91         return ErrorCode::SUCCESS;
92     }
93 
94     template <class ValueType>
GetValue(const std::string & key,std::map<std::string,Any> & valueMap,ValueType & value)95     static ErrorCode GetValue(const std::string &key, std::map<std::string, Any> &valueMap, ValueType &value)
96     {
97         auto it = valueMap.find(key);
98         if (it == valueMap.end()) {
99             EFFECT_LOGE("key is not set! key=%{public}s", key.c_str());
100             return ErrorCode::ERR_NO_VALUE_KEY;
101         }
102 
103         auto result = AnyCast<ValueType>(&it->second);
104         if (result == nullptr) {
105             EFFECT_LOGE("value type is not match! key=%{public}s", key.c_str());
106             return ErrorCode::ERR_ANY_CAST_TYPE_NOT_MATCH;
107         }
108         EFFECT_LOGD("value get success! key=%{public}s", key.c_str());
109 
110         value = *result;
111         return ErrorCode::SUCCESS;
112     }
113 
Clip(float a,float aMin,float aMax)114     static inline float Clip(float a, float aMin, float aMax)
115     {
116         return a > aMax ? aMax : (a < aMin ? aMin : a);
117     }
118 
119     static IEffectFormat SwitchToEffectFormat(GraphicPixelFormat pixelFormat);
120     static IEffectFormat SwitchToEffectFormat(PixelFormat pixelFormat);
121     static GraphicPixelFormat SwitchToGraphicPixelFormat(IEffectFormat formatType);
122     static PixelFormat SwitchToPixelFormat(IEffectFormat formatType);
123     static BufferType SwitchToEffectBuffType(AllocatorType allocatorType);
124     static PixelFormat SwitchGLFormatToPixelFormat(unsigned int formatType);
125     static IEffectFormat SwitchGLFormatToEffectFormat(unsigned int formatType);
126 
127 private:
128     static const std::unordered_map<PixelFormat, IEffectFormat> pixelFmtToEffectFmt_;
129     static const std::unordered_map<GraphicPixelFormat, IEffectFormat> surfaceBufferFmtToEffectFmt_;
130     static const std::unordered_map<AllocatorType, BufferType> allocatorTypeToEffectBuffType_;
131     static const std::unordered_map<unsigned int, PixelFormat> glFmtToPixelFmt_;
132     static const std::unordered_map<unsigned int, IEffectFormat> glFmtToEffectFmt_;
133 };
134 } // namespace Effect
135 } // namespace Media
136 } // namespace OHOS
137 
138 #endif // IMAGE_EFFECT_COMMON_UTILS_H
139