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_FORMAT_HELPER_H 17 #define IMAGE_EFFECT_FORMAT_HELPER_H 18 19 #include <unordered_set> 20 21 #include "effect_info.h" 22 #include "image_effect_marco_define.h" 23 #include "effect_buffer.h" 24 #include "error_code.h" 25 26 #define UNSIGHED_CHAR_MAX 255 27 28 namespace OHOS { 29 namespace Media { 30 namespace Effect { 31 struct FormatConverterInfo { 32 BufferInfo bufferInfo; 33 void *buffer = nullptr; 34 }; 35 36 class FormatHelper { 37 public: 38 IMAGE_EFFECT_EXPORT static uint32_t CalculateDataRowCount(uint32_t height, IEffectFormat format); 39 IMAGE_EFFECT_EXPORT static uint32_t CalculateRowStride(uint32_t width, IEffectFormat format); 40 IMAGE_EFFECT_EXPORT static uint32_t CalculateSize(uint32_t width, uint32_t height, IEffectFormat format); 41 IMAGE_EFFECT_EXPORT static std::unordered_set<IEffectFormat> GetAllSupportedFormats(); 42 IMAGE_EFFECT_EXPORT static bool IsSupportConvert(IEffectFormat srcFormat, IEffectFormat dstFormat); 43 IMAGE_EFFECT_EXPORT static ErrorCode ConvertFormat(FormatConverterInfo &src, FormatConverterInfo &dst); 44 Clip(int a,int aMin,int aMax)45 static inline int Clip(int a, int aMin, int aMax) 46 { 47 return a > aMax ? aMax : (a < aMin ? aMin : a); 48 } 49 RGBToY(uint8_t r,uint8_t g,uint8_t b)50 static inline uint8_t RGBToY(uint8_t r, uint8_t g, uint8_t b) 51 { 52 int y = (54 * r + 183 * g + 18 * b) >> 8; 53 return Clip(y, 0, UNSIGHED_CHAR_MAX); 54 } 55 RGBToU(uint8_t r,uint8_t g,uint8_t b)56 static inline uint8_t RGBToU(uint8_t r, uint8_t g, uint8_t b) 57 { 58 int u = ((-29 * r - 99 * g + 128 * b) >> 8) + 128; 59 return Clip(u, 0, UNSIGHED_CHAR_MAX); 60 } 61 RGBToV(uint8_t r,uint8_t g,uint8_t b)62 static inline uint8_t RGBToV(uint8_t r, uint8_t g, uint8_t b) 63 { 64 int v = ((128 * r - 116 * g - 12 * b) >> 8) + 128; 65 return Clip(v, 0, UNSIGHED_CHAR_MAX); 66 } 67 YuvToR(uint8_t y,uint8_t u,uint8_t v)68 static inline uint8_t YuvToR(uint8_t y, uint8_t u, uint8_t v) 69 { 70 int r = (y + ((403 * (v - 128)) >> 8)); 71 return Clip(r, 0, UNSIGHED_CHAR_MAX); 72 } 73 YuvToG(uint8_t y,uint8_t u,uint8_t v)74 static inline uint8_t YuvToG(uint8_t y, uint8_t u, uint8_t v) 75 { 76 int g = (y - ((48 * (u - 128) + 120 * (v - 128)) >> 8)); 77 return Clip(g, 0, UNSIGHED_CHAR_MAX); 78 } 79 YuvToB(uint8_t y,uint8_t u,uint8_t v)80 static inline uint8_t YuvToB(uint8_t y, uint8_t u, uint8_t v) 81 { 82 int b = (y + ((475 * (u - 128)) >> 8)); 83 return Clip(b, 0, UNSIGHED_CHAR_MAX); 84 } 85 }; 86 } // namespace Effect 87 } // namespace Media 88 } // namespace OHOS 89 #endif // IMAGE_EFFECT_FORMAT_HELPER_H 90