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 16 /* 17 * Copyright (c) 2023 Huawei Device Co., Ltd. 18 * Licensed under the Apache License, Version 2.0 (the "License"); 19 * you may not use this file except in compliance with the License. 20 * You may obtain a copy of the License at 21 * 22 * http://www.apache.org/licenses/LICENSE-2.0 23 * 24 * Unless required by applicable law or agreed to in writing, software 25 * distributed under the License is distributed on an "AS IS" BASIS, 26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 27 * See the License for the specific language governing permissions and 28 * limitations under the License. 29 */ 30 31 #ifndef SKIA_IMAGE_INFO_H 32 #define SKIA_IMAGE_INFO_H 33 34 #include "include/core/SkImageInfo.h" 35 #include "include/core/SkEncodedImageFormat.h" 36 37 #include "skia_color_space.h" 38 39 #include "image/image_info.h" 40 41 namespace OHOS { 42 namespace Rosen { 43 namespace Drawing { 44 class SkiaImageInfo { 45 public: ConvertToSkColorType(const ColorType & format)46 static SkColorType ConvertToSkColorType(const ColorType& format) 47 { 48 switch (format) { 49 case COLORTYPE_UNKNOWN: 50 return kUnknown_SkColorType; 51 case COLORTYPE_ALPHA_8: 52 return kAlpha_8_SkColorType; 53 case COLORTYPE_RGB_565: 54 return kRGB_565_SkColorType; 55 case COLORTYPE_ARGB_4444: 56 return kARGB_4444_SkColorType; 57 case COLORTYPE_RGBA_8888: 58 return kRGBA_8888_SkColorType; 59 case COLORTYPE_RGB_888X: 60 return kRGB_888x_SkColorType; 61 case COLORTYPE_BGRA_8888: 62 return kBGRA_8888_SkColorType; 63 case COLORTYPE_RGBA_F16: 64 return kRGBA_F16_SkColorType; 65 case COLORTYPE_N32: 66 return kN32_SkColorType; 67 case COLORTYPE_RGBA_1010102: 68 return kRGBA_1010102_SkColorType; 69 case COLORTYPE_GRAY_8: 70 return kGray_8_SkColorType; 71 default: 72 return kUnknown_SkColorType; 73 } 74 } 75 ConvertToSkAlphaType(const AlphaType & format)76 static SkAlphaType ConvertToSkAlphaType(const AlphaType& format) 77 { 78 switch (format) { 79 case ALPHATYPE_UNKNOWN: 80 return kUnknown_SkAlphaType; 81 case ALPHATYPE_OPAQUE: 82 return kOpaque_SkAlphaType; 83 case ALPHATYPE_PREMUL: 84 return kPremul_SkAlphaType; 85 case ALPHATYPE_UNPREMUL: 86 return kUnpremul_SkAlphaType; 87 default: 88 return kUnknown_SkAlphaType; 89 } 90 } 91 ConvertToSkImageInfo(const ImageInfo & imageInfo)92 static SkImageInfo ConvertToSkImageInfo(const ImageInfo& imageInfo) 93 { 94 auto colorSpace = imageInfo.GetColorSpace(); 95 auto colorSpaceImpl = colorSpace ? colorSpace->GetImpl<SkiaColorSpace>() : nullptr; 96 return SkImageInfo::Make(imageInfo.GetWidth(), imageInfo.GetHeight(), 97 ConvertToSkColorType(imageInfo.GetColorType()), 98 ConvertToSkAlphaType(imageInfo.GetAlphaType()), 99 colorSpaceImpl ? colorSpaceImpl->GetColorSpace() : nullptr); 100 } 101 ConvertToColorType(const SkColorType & format)102 static ColorType ConvertToColorType(const SkColorType& format) 103 { 104 switch (format) { 105 case kUnknown_SkColorType: 106 return COLORTYPE_UNKNOWN; 107 case kAlpha_8_SkColorType: 108 return COLORTYPE_ALPHA_8; 109 case kRGB_565_SkColorType: 110 return COLORTYPE_RGB_565; 111 case kARGB_4444_SkColorType: 112 return COLORTYPE_ARGB_4444; 113 case kRGBA_8888_SkColorType: 114 return COLORTYPE_RGBA_8888; 115 case kRGB_888x_SkColorType: 116 return COLORTYPE_RGB_888X; 117 case kBGRA_8888_SkColorType: 118 return COLORTYPE_BGRA_8888; 119 case kRGBA_F16_SkColorType: 120 return COLORTYPE_RGBA_F16; 121 case kRGBA_1010102_SkColorType: 122 return COLORTYPE_RGBA_1010102; 123 case kGray_8_SkColorType: 124 return COLORTYPE_GRAY_8; 125 default: 126 return COLORTYPE_UNKNOWN; 127 } 128 } 129 ConvertToAlphaType(const SkAlphaType & format)130 static AlphaType ConvertToAlphaType(const SkAlphaType& format) 131 { 132 switch (format) { 133 case kUnknown_SkAlphaType: 134 return ALPHATYPE_UNKNOWN; 135 case kOpaque_SkAlphaType: 136 return ALPHATYPE_OPAQUE; 137 case kPremul_SkAlphaType: 138 return ALPHATYPE_PREMUL; 139 case kUnpremul_SkAlphaType: 140 return ALPHATYPE_UNPREMUL; 141 default: 142 return ALPHATYPE_UNKNOWN; 143 } 144 } 145 ConvertToRSImageInfo(const SkImageInfo & skImageInfo)146 static ImageInfo ConvertToRSImageInfo(const SkImageInfo& skImageInfo) 147 { 148 std::shared_ptr<SkiaColorSpace> skiaColorSpace = std::make_shared<SkiaColorSpace>(); 149 skiaColorSpace->SetColorSpace(skImageInfo.refColorSpace()); 150 return {skImageInfo.width(), skImageInfo.height(), 151 ConvertToColorType(skImageInfo.colorType()), 152 ConvertToAlphaType(skImageInfo.alphaType()), 153 ColorSpace::CreateFromImpl(skiaColorSpace)}; 154 } 155 ConvertToSkEncodedImageFormat(const EncodedImageFormat & encodedImageFormat)156 static SkEncodedImageFormat ConvertToSkEncodedImageFormat(const EncodedImageFormat& encodedImageFormat) 157 { 158 switch (encodedImageFormat) { 159 case EncodedImageFormat::JPEG: 160 return SkEncodedImageFormat::kJPEG; 161 case EncodedImageFormat::PNG: 162 return SkEncodedImageFormat::kPNG; 163 case EncodedImageFormat::WEBP: 164 return SkEncodedImageFormat::kWEBP; 165 default: 166 return SkEncodedImageFormat::kJPEG; 167 } 168 } 169 }; 170 } // namespace Drawing 171 } // namespace Rosen 172 } // namespace OHOS 173 #endif