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 #include "format_helper.h"
17 
18 namespace {
19     const float YUV_BYTES_PER_PIXEL = 1.5f;
20     const int32_t RGBA_8888_BYTES_PER_PIXEL = 4;
21 }
22 
23 namespace OHOS {
24 namespace Media {
25 namespace Effect {
CalculateDataRowCount(uint32_t height,IEffectFormat format)26 uint32_t FormatHelper::CalculateDataRowCount(uint32_t height, IEffectFormat format)
27 {
28     switch (format) {
29         case IEffectFormat::RGBA8888:
30         case IEffectFormat::RGBA_1010102:
31             return height;
32         case IEffectFormat::YUVNV12:
33         case IEffectFormat::YUVNV21:
34             return static_cast<uint32_t>(height * YUV_BYTES_PER_PIXEL);
35         default:
36             return height;
37     }
38 }
39 
CalculateRowStride(uint32_t width,IEffectFormat format)40 uint32_t FormatHelper::CalculateRowStride(uint32_t width, IEffectFormat format)
41 {
42     switch (format) {
43         case IEffectFormat::RGBA8888:
44         case IEffectFormat::RGBA_1010102:
45             return width * RGBA_8888_BYTES_PER_PIXEL;
46         case IEffectFormat::YUVNV12:
47         case IEffectFormat::YUVNV21:
48             return width;
49         default:
50             return width;
51     }
52 }
53 
CalculateSize(uint32_t width,uint32_t height,IEffectFormat format)54 uint32_t FormatHelper::CalculateSize(uint32_t width, uint32_t height, IEffectFormat format)
55 {
56     return CalculateDataRowCount(height, format) * CalculateRowStride(width, format);
57 }
58 } // namespace Effect
59 } // namespace Media
60 } // namespace OHOS