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