1 /* 2 * Copyright (c) 2021-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 #include "skia_color_space.h" 17 18 #include "skia_image.h" 19 20 #include "image/image.h" 21 22 namespace OHOS { 23 namespace Rosen { 24 namespace Drawing { SkiaColorSpace()25SkiaColorSpace::SkiaColorSpace() noexcept : colorSpace_(nullptr) {} 26 InitWithSRGB()27void SkiaColorSpace::InitWithSRGB() 28 { 29 colorSpace_ = SkColorSpace::MakeSRGB(); 30 } 31 InitWithSRGBLinear()32void SkiaColorSpace::InitWithSRGBLinear() 33 { 34 colorSpace_ = SkColorSpace::MakeSRGBLinear(); 35 } 36 InitWithImage(const Image & image)37void SkiaColorSpace::InitWithImage(const Image& image) 38 { 39 auto i = image.GetImpl<SkiaImage>(); 40 if (i != nullptr) { 41 const sk_sp<SkImage> skiaImage = i->GetImage(); 42 colorSpace_ = skiaImage->refColorSpace(); 43 } 44 } 45 ConvertToSkCMSTransferFunction(const CMSTransferFuncType & func)46static inline skcms_TransferFunction ConvertToSkCMSTransferFunction(const CMSTransferFuncType& func) 47 { 48 switch (func) { 49 case CMSTransferFuncType::SRGB: 50 return SkNamedTransferFn::kSRGB; 51 case CMSTransferFuncType::DOT2: 52 return SkNamedTransferFn::k2Dot2; 53 case CMSTransferFuncType::LINEAR: 54 return SkNamedTransferFn::kLinear; 55 case CMSTransferFuncType::REC2020: 56 return SkNamedTransferFn::kRec2020; 57 } 58 } 59 ConvertToSkCMSMatrix3x3(const CMSMatrixType & matrix)60static inline skcms_Matrix3x3 ConvertToSkCMSMatrix3x3(const CMSMatrixType& matrix) 61 { 62 switch (matrix) { 63 case CMSMatrixType::SRGB: 64 return SkNamedGamut::kSRGB; 65 case CMSMatrixType::ADOBE_RGB: 66 return SkNamedGamut::kAdobeRGB; 67 case CMSMatrixType::DCIP3: 68 #ifdef NEW_SKIA 69 return SkNamedGamut::kDisplayP3; 70 #else 71 return SkNamedGamut::kDCIP3; 72 #endif 73 case CMSMatrixType::REC2020: 74 return SkNamedGamut::kRec2020; 75 case CMSMatrixType::XYZ: 76 return SkNamedGamut::kXYZ; 77 } 78 } 79 InitWithRGB(const CMSTransferFuncType & func,const CMSMatrixType & matrix)80void SkiaColorSpace::InitWithRGB(const CMSTransferFuncType& func, const CMSMatrixType& matrix) 81 { 82 colorSpace_ = SkColorSpace::MakeRGB(ConvertToSkCMSTransferFunction(func), ConvertToSkCMSMatrix3x3(matrix)); 83 } 84 GetColorSpace() const85sk_sp<SkColorSpace> SkiaColorSpace::GetColorSpace() const 86 { 87 return colorSpace_; 88 } 89 } // namespace Drawing 90 } // namespace Rosen 91 } // namespace OHOS