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 "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 ErrorCode ModifyPixelMapProperty(PixelMap *pixelMap, const std::shared_ptr<EffectBuffer> &buffer, 51 const std::shared_ptr<EffectMemoryManager> &memoryManager, bool isUpdateExif = true); 52 static ErrorCode ModifyPixelMapPropertyForTexture(PixelMap *pixelMap, const std::shared_ptr<EffectBuffer> &buffer, 53 const std::shared_ptr<EffectContext> &context, bool isUpdateExif = true); 54 static ErrorCode ParseNativeWindowData(std::shared_ptr<EffectBuffer> &effectBuffer, const DataType &dataType); 55 static void UpdateImageExifDateTime(PixelMap *pixelMap); 56 static void UpdateImageExifDateTime(Picture *picture); 57 static void UpdateImageExifInfo(PixelMap *pixelMap); 58 static void UpdateImageExifInfo(Picture *picture); 59 static ErrorCode ParsePicture(Picture *picture, std::shared_ptr<EffectBuffer> &effectBuffer); 60 ParseAny(Plugin::Any any,ValueType & value)61 template <class ValueType> static ErrorCode ParseAny(Plugin::Any any, ValueType &value) 62 { 63 auto result = Plugin::AnyCast<ValueType>(&any); 64 if (result == nullptr) { 65 EFFECT_LOGE("value type is not match!"); 66 return ErrorCode::ERR_ANY_CAST_TYPE_NOT_MATCH; 67 } 68 EFFECT_LOGD("value get success!"); 69 70 value = *result; 71 return ErrorCode::SUCCESS; 72 } 73 74 template <class ValueType> GetValue(const std::string & key,std::map<std::string,Plugin::Any> & valueMap,ValueType & value)75 static ErrorCode GetValue(const std::string &key, std::map<std::string, Plugin::Any> &valueMap, ValueType &value) 76 { 77 auto it = valueMap.find(key); 78 if (it == valueMap.end()) { 79 EFFECT_LOGE("key is not set! key=%{public}s", key.c_str()); 80 return ErrorCode::ERR_NO_VALUE_KEY; 81 } 82 83 auto result = Plugin::AnyCast<ValueType>(&it->second); 84 if (result == nullptr) { 85 EFFECT_LOGE("value type is not match! key=%{public}s", key.c_str()); 86 return ErrorCode::ERR_ANY_CAST_TYPE_NOT_MATCH; 87 } 88 EFFECT_LOGD("value get success! key=%{public}s", key.c_str()); 89 90 value = *result; 91 return ErrorCode::SUCCESS; 92 } 93 Clip(float a,float aMin,float aMax)94 static inline float Clip(float a, float aMin, float aMax) 95 { 96 return a > aMax ? aMax : (a < aMin ? aMin : a); 97 } 98 99 static IEffectFormat SwitchToEffectFormat(GraphicPixelFormat pixelFormat); 100 static IEffectFormat SwitchToEffectFormat(PixelFormat pixelFormat); 101 static GraphicPixelFormat SwitchToGraphicPixelFormat(IEffectFormat formatType); 102 static PixelFormat SwitchToPixelFormat(IEffectFormat formatType); 103 static BufferType SwitchToEffectBuffType(AllocatorType allocatorType); 104 105 private: 106 static const std::unordered_map<PixelFormat, IEffectFormat> pixelFmtToEffectFmt_; 107 static const std::unordered_map<GraphicPixelFormat, IEffectFormat> surfaceBufferFmtToEffectFmt_; 108 static const std::unordered_map<AllocatorType, BufferType> allocatorTypeToEffectBuffType_; 109 }; 110 } // namespace Effect 111 } // namespace Media 112 } // namespace OHOS 113 114 #endif // IMAGE_EFFECT_COMMON_UTILS_H