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