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 "effect_info.h"
20 #include "image_effect_marco_define.h"
21 
22 #define UNSIGHED_CHAR_MAX 255
23 
24 namespace OHOS {
25 namespace Media {
26 namespace Effect {
27 class FormatHelper {
28 public:
29     IMAGE_EFFECT_EXPORT static uint32_t CalculateDataRowCount(uint32_t height, IEffectFormat format);
30     IMAGE_EFFECT_EXPORT static uint32_t CalculateRowStride(uint32_t width, IEffectFormat format);
31     IMAGE_EFFECT_EXPORT static uint32_t CalculateSize(uint32_t width, uint32_t height, IEffectFormat format);
32 
Clip(int a,int aMin,int aMax)33     static inline int Clip(int a, int aMin, int aMax)
34     {
35         return a > aMax ? aMax : (a < aMin ? aMin : a);
36     }
37 
RGBToY(uint8_t r,uint8_t g,uint8_t b)38     static inline uint8_t RGBToY(uint8_t r, uint8_t g, uint8_t b)
39     {
40         int y = (54 * r + 183 * g + 18 * b) >> 8;
41         return Clip(y, 0, UNSIGHED_CHAR_MAX);
42     }
43 
RGBToU(uint8_t r,uint8_t g,uint8_t b)44     static inline uint8_t RGBToU(uint8_t r, uint8_t g, uint8_t b)
45     {
46         int u = ((-29 * r - 99 * g + 128 * b) >> 8) + 128;
47         return Clip(u, 0, UNSIGHED_CHAR_MAX);
48     }
49 
RGBToV(uint8_t r,uint8_t g,uint8_t b)50     static inline uint8_t RGBToV(uint8_t r, uint8_t g, uint8_t b)
51     {
52         int v = ((128 * r - 116 * g - 12 * b) >> 8) + 128;
53         return Clip(v, 0, UNSIGHED_CHAR_MAX);
54     }
55 
YuvToR(uint8_t y,uint8_t u,uint8_t v)56     static inline uint8_t YuvToR(uint8_t y, uint8_t u, uint8_t v)
57     {
58         int r = (y + ((403 * (v - 128)) >> 8));
59         return Clip(r, 0, UNSIGHED_CHAR_MAX);
60     }
61 
YuvToG(uint8_t y,uint8_t u,uint8_t v)62     static inline uint8_t YuvToG(uint8_t y, uint8_t u, uint8_t v)
63     {
64         int g = (y - ((48 * (u - 128) + 120 * (v - 128)) >> 8));
65         return Clip(g, 0, UNSIGHED_CHAR_MAX);
66     }
67 
YuvToB(uint8_t y,uint8_t u,uint8_t v)68     static inline uint8_t YuvToB(uint8_t y, uint8_t u, uint8_t v)
69     {
70         int b = (y + ((475 * (u - 128)) >> 8));
71         return Clip(b, 0, UNSIGHED_CHAR_MAX);
72     }
73 };
74 } // namespace Effect
75 } // namespace Media
76 } // namespace OHOS
77 #endif // IMAGE_EFFECT_FORMAT_HELPER_H
78