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 #ifdef USE_M133_SKIA 36 #include "include/codec/SkEncodedImageFormat.h" 37 #else 38 #include "include/core/SkEncodedImageFormat.h" 39 #endif 40 41 #include "skia_color_space.h" 42 43 #include "image/image_info.h" 44 45 namespace OHOS { 46 namespace Rosen { 47 namespace Drawing { 48 class SkiaImageInfo { 49 public: ConvertToSkColorType(const ColorType & format)50 static SkColorType ConvertToSkColorType(const ColorType& format) 51 { 52 switch (format) { 53 case COLORTYPE_UNKNOWN: 54 return kUnknown_SkColorType; 55 case COLORTYPE_ALPHA_8: 56 return kAlpha_8_SkColorType; 57 case COLORTYPE_RGB_565: 58 return kRGB_565_SkColorType; 59 case COLORTYPE_ARGB_4444: 60 return kARGB_4444_SkColorType; 61 case COLORTYPE_RGBA_8888: 62 return kRGBA_8888_SkColorType; 63 case COLORTYPE_RGB_888X: 64 return kRGB_888x_SkColorType; 65 case COLORTYPE_BGRA_8888: 66 return kBGRA_8888_SkColorType; 67 case COLORTYPE_RGBA_F16: 68 return kRGBA_F16_SkColorType; 69 case COLORTYPE_N32: 70 return kN32_SkColorType; 71 case COLORTYPE_RGBA_1010102: 72 return kRGBA_1010102_SkColorType; 73 case COLORTYPE_GRAY_8: 74 return kGray_8_SkColorType; 75 default: 76 return kUnknown_SkColorType; 77 } 78 } 79 ConvertToSkAlphaType(const AlphaType & format)80 static SkAlphaType ConvertToSkAlphaType(const AlphaType& format) 81 { 82 switch (format) { 83 case ALPHATYPE_UNKNOWN: 84 return kUnknown_SkAlphaType; 85 case ALPHATYPE_OPAQUE: 86 return kOpaque_SkAlphaType; 87 case ALPHATYPE_PREMUL: 88 return kPremul_SkAlphaType; 89 case ALPHATYPE_UNPREMUL: 90 return kUnpremul_SkAlphaType; 91 default: 92 return kUnknown_SkAlphaType; 93 } 94 } 95 ConvertToSkImageInfo(const ImageInfo & imageInfo)96 static SkImageInfo ConvertToSkImageInfo(const ImageInfo& imageInfo) 97 { 98 auto colorSpace = imageInfo.GetColorSpace(); 99 auto colorSpaceImpl = colorSpace ? colorSpace->GetImpl<SkiaColorSpace>() : nullptr; 100 return SkImageInfo::Make(imageInfo.GetWidth(), imageInfo.GetHeight(), 101 ConvertToSkColorType(imageInfo.GetColorType()), 102 ConvertToSkAlphaType(imageInfo.GetAlphaType()), 103 colorSpaceImpl ? colorSpaceImpl->GetColorSpace() : nullptr); 104 } 105 ConvertToColorType(const SkColorType & format)106 static ColorType ConvertToColorType(const SkColorType& format) 107 { 108 switch (format) { 109 case kUnknown_SkColorType: 110 return COLORTYPE_UNKNOWN; 111 case kAlpha_8_SkColorType: 112 return COLORTYPE_ALPHA_8; 113 case kRGB_565_SkColorType: 114 return COLORTYPE_RGB_565; 115 case kARGB_4444_SkColorType: 116 return COLORTYPE_ARGB_4444; 117 case kRGBA_8888_SkColorType: 118 return COLORTYPE_RGBA_8888; 119 case kRGB_888x_SkColorType: 120 return COLORTYPE_RGB_888X; 121 case kBGRA_8888_SkColorType: 122 return COLORTYPE_BGRA_8888; 123 case kRGBA_F16_SkColorType: 124 return COLORTYPE_RGBA_F16; 125 case kRGBA_1010102_SkColorType: 126 return COLORTYPE_RGBA_1010102; 127 case kGray_8_SkColorType: 128 return COLORTYPE_GRAY_8; 129 default: 130 return COLORTYPE_UNKNOWN; 131 } 132 } 133 ConvertToAlphaType(const SkAlphaType & format)134 static AlphaType ConvertToAlphaType(const SkAlphaType& format) 135 { 136 switch (format) { 137 case kUnknown_SkAlphaType: 138 return ALPHATYPE_UNKNOWN; 139 case kOpaque_SkAlphaType: 140 return ALPHATYPE_OPAQUE; 141 case kPremul_SkAlphaType: 142 return ALPHATYPE_PREMUL; 143 case kUnpremul_SkAlphaType: 144 return ALPHATYPE_UNPREMUL; 145 default: 146 return ALPHATYPE_UNKNOWN; 147 } 148 } 149 ConvertToRSImageInfo(const SkImageInfo & skImageInfo)150 static ImageInfo ConvertToRSImageInfo(const SkImageInfo& skImageInfo) 151 { 152 std::shared_ptr<ColorSpace> colorSpace = std::make_shared<ColorSpace>(); 153 colorSpace->GetImpl<SkiaColorSpace>()->SetColorSpace(skImageInfo.refColorSpace()); 154 return {skImageInfo.width(), skImageInfo.height(), 155 ConvertToColorType(skImageInfo.colorType()), 156 ConvertToAlphaType(skImageInfo.alphaType()), 157 colorSpace}; 158 } 159 ConvertToSkEncodedImageFormat(const EncodedImageFormat & encodedImageFormat)160 static SkEncodedImageFormat ConvertToSkEncodedImageFormat(const EncodedImageFormat& encodedImageFormat) 161 { 162 switch (encodedImageFormat) { 163 case EncodedImageFormat::JPEG: 164 return SkEncodedImageFormat::kJPEG; 165 case EncodedImageFormat::PNG: 166 return SkEncodedImageFormat::kPNG; 167 case EncodedImageFormat::WEBP: 168 return SkEncodedImageFormat::kWEBP; 169 default: 170 return SkEncodedImageFormat::kJPEG; 171 } 172 } 173 }; 174 } // namespace Drawing 175 } // namespace Rosen 176 } // namespace OHOS 177 #endif