1 /*
2  * Copyright (C) 2023 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 #include "image_type_converter.h"
16 
17 namespace OHOS {
18 namespace Media {
19 const static int DEFAULT_INDEX = 0;
20 using std::string;
21 struct PixelFormatPair {
22     SkColorType skColorType;
23     PixelFormat pixelFormat;
24     string skColorTypeName;
25     string pixelFormatName;
26 };
27 
28 struct AlphaTypePair {
29     SkAlphaType skAlphaType;
30     AlphaType alphaType;
31     string skAlphaTypeName;
32     string alphaTypeName;
33 };
34 
35 static PixelFormatPair g_pixelFormatPairs[] = {
36     {SkColorType::kUnknown_SkColorType, PixelFormat::UNKNOWN,
37         "kUnknown_SkColorType", "PixelFormat::UNKNOWN"},
38     {SkColorType::kRGBA_8888_SkColorType, PixelFormat::ARGB_8888,
39         "kRGBA_8888_SkColorType", "PixelFormat::ARGB_8888"},
40     {SkColorType::kRGBA_1010102_SkColorType, PixelFormat::RGBA_1010102,
41         "kRGBA_1010102_SkColorType", "PixelFormat::RGBA_1010102"},
42     {SkColorType::kAlpha_8_SkColorType, PixelFormat::ALPHA_8,
43         "kAlpha_8_SkColorType", "PixelFormat::ALPHA_8"},
44     {SkColorType::kRGB_565_SkColorType, PixelFormat::RGB_565,
45         "kRGB_565_SkColorType", "PixelFormat::RGB_565"},
46     {SkColorType::kRGBA_F16_SkColorType, PixelFormat::RGBA_F16,
47         "kRGBA_F16_SkColorType", "PixelFormat::RGBA_F16"},
48     {SkColorType::kRGBA_8888_SkColorType, PixelFormat::RGBA_8888,
49         "kRGBA_8888_SkColorType", "PixelFormat::RGBA_8888"},
50     {SkColorType::kBGRA_8888_SkColorType, PixelFormat::BGRA_8888,
51         "kBGRA_8888_SkColorType", "PixelFormat::BGRA_8888"},
52     {SkColorType::kRGB_888x_SkColorType, PixelFormat::RGB_888,
53         "kRGB_888x_SkColorType", "PixelFormat::RGB_888"},
54 };
55 
56 static AlphaTypePair g_alphaTypePairs[] = {
57     {SkAlphaType::kUnknown_SkAlphaType, AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN,
58         "kUnknown_SkAlphaType", "AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN"},
59     {SkAlphaType::kOpaque_SkAlphaType, AlphaType::IMAGE_ALPHA_TYPE_OPAQUE,
60         "kOpaque_SkAlphaType", "AlphaType::IMAGE_ALPHA_TYPE_OPAQUE"},
61     {SkAlphaType::kPremul_SkAlphaType, AlphaType::IMAGE_ALPHA_TYPE_PREMUL,
62         "kPremul_SkAlphaType", "AlphaType::IMAGE_ALPHA_TYPE_PREMUL"},
63     {SkAlphaType::kUnpremul_SkAlphaType, AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL,
64         "kUnpremul_SkAlphaType", "AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL"},
65 };
66 
67 template<typename T, typename C, unsigned L>
Find(T (& infos)[L],C compare)68 static T Find(T (&infos)[L], C compare)
69 {
70     for (T iter : infos) {
71         if (compare(iter)) {
72             return iter;
73         }
74     }
75     return infos[DEFAULT_INDEX];
76 }
77 
ToSkColorType(const PixelFormat pixelFormat)78 SkColorType ImageTypeConverter::ToSkColorType(const PixelFormat pixelFormat)
79 {
80     auto res = Find(g_pixelFormatPairs, [pixelFormat](PixelFormatPair iter) {
81         return (iter.pixelFormat == pixelFormat);
82     });
83     return res.skColorType;
84 }
85 
ToSkAlphaType(const AlphaType alphaType)86 SkAlphaType ImageTypeConverter::ToSkAlphaType(const AlphaType alphaType)
87 {
88     auto res = Find(g_alphaTypePairs, [alphaType](AlphaTypePair iter) {
89         return (iter.alphaType == alphaType);
90     });
91     return res.skAlphaType;
92 }
93 
ToPixelFormat(const SkColorType type)94 PixelFormat ImageTypeConverter::ToPixelFormat(const SkColorType type)
95 {
96     auto res = Find(g_pixelFormatPairs, [type](PixelFormatPair iter) {
97         return (iter.skColorType == type);
98     });
99     return res.pixelFormat;
100 }
101 
ToAlphaType(const SkAlphaType type)102 AlphaType ImageTypeConverter::ToAlphaType(const SkAlphaType type)
103 {
104     auto res = Find(g_alphaTypePairs, [type](AlphaTypePair iter) {
105         return (iter.skAlphaType == type);
106     });
107     return res.alphaType;
108 }
109 
ToName(const PixelFormat pixelFormat)110 const string ImageTypeConverter::ToName(const PixelFormat pixelFormat)
111 {
112     auto res = Find(g_pixelFormatPairs, [pixelFormat](PixelFormatPair iter) {
113         return (iter.pixelFormat == pixelFormat);
114     });
115     return res.pixelFormatName;
116 }
117 
ToName(const AlphaType alphaType)118 const string ImageTypeConverter::ToName(const AlphaType alphaType)
119 {
120     auto res = Find(g_alphaTypePairs, [alphaType](AlphaTypePair iter) {
121         return (iter.alphaType == alphaType);
122     });
123     return res.alphaTypeName;
124 }
125 
ToName(const SkColorType type)126 const string ImageTypeConverter::ToName(const SkColorType type)
127 {
128     auto res = Find(g_pixelFormatPairs, [type](PixelFormatPair iter) {
129         return (iter.skColorType == type);
130     });
131     return res.skColorTypeName;
132 }
133 
ToName(const SkAlphaType type)134 const string ImageTypeConverter::ToName(const SkAlphaType type)
135 {
136     auto res = Find(g_alphaTypePairs, [type](AlphaTypePair iter) {
137         return (iter.skAlphaType == type);
138     });
139     return res.skAlphaTypeName;
140 }
141 } // namespace Media
142 } // namespace OHOS