raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkColorSpace_A2B_DEFINED |
| 9 | #define SkColorSpace_A2B_DEFINED |
| 10 | |
| 11 | #include "SkColorSpace_Base.h" |
| 12 | |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 15 | // An alternative SkColorSpace that represents all the color space data that |
| 16 | // is stored in an A2B0 ICC tag. This allows us to use alternative profile |
| 17 | // connection spaces (CIELAB instead of just CIEXYZ), use color-lookup-tables |
| 18 | // to do color space transformations not representable as TRC functions or |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 19 | // matrix operations, as well as have multiple TRC functions. The CLUT also |
| 20 | // allows conversion between non-3-channel input color spaces ie CMYK(4) to |
| 21 | // a workable PCS (ie XYZ). |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 22 | // |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 23 | // AtoBType, lut8Type and lut16Type A2B0 tag types are supported. There are |
| 24 | // also MPET (multi-processing-elements) A2B0 tags in the standard which allow |
| 25 | // you to combine these 3 primitives (TRC, CLUT, matrix) in any order/quantity. |
| 26 | // MPET tags are currently unsupported by the MakeICC parser, could be supported |
| 27 | // here by the nature of the design. |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 28 | class SkColorSpace_A2B : public SkColorSpace_Base { |
| 29 | public: |
| 30 | const SkMatrix44* toXYZD50() const override { |
| 31 | // the matrix specified in A2B0 profiles is not necessarily |
| 32 | // a to-XYZ matrix, as to-Lab is supported as well so returning |
| 33 | // that could be misleading. Additionally, B-curves are applied |
| 34 | // after the matrix is, but a toXYZD50 matrix is the last thing |
| 35 | // applied in order to get into the (XYZ) profile connection space. |
| 36 | return nullptr; |
| 37 | } |
| 38 | |
Brian Osman | bbf251b | 2016-10-19 14:56:07 -0400 | [diff] [blame] | 39 | uint32_t toXYZD50Hash() const override { |
| 40 | // See toXYZD50()'s comment. |
| 41 | return 0; |
| 42 | } |
| 43 | |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 44 | const SkMatrix44* fromXYZD50() const override { |
| 45 | // See toXYZD50()'s comment. Also, A2B0 profiles are not supported |
| 46 | // as destination color spaces, so an inverse matrix is never wanted. |
| 47 | return nullptr; |
| 48 | } |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 49 | |
Ravi Mistry | 113d05f | 2016-12-17 01:31:03 +0000 | [diff] [blame] | 50 | // There is no single gamma curve in an A2B0 profile |
| 51 | bool onGammaCloseToSRGB() const override { return false; } |
| 52 | bool onGammaIsLinear() const override { return false; } |
| 53 | bool onIsNumericalTransferFn(SkColorSpaceTransferFn* coeffs) const override { return false; } |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 54 | |
Matt Sarett | 7f15b68 | 2017-02-24 17:22:09 -0500 | [diff] [blame] | 55 | bool onIsCMYK() const override { return kCMYK_ICCTypeFlag == fICCType; } |
| 56 | |
Brian Osman | 12313f0 | 2016-11-07 09:42:37 -0500 | [diff] [blame] | 57 | sk_sp<SkColorSpace> makeLinearGamma() override { |
| 58 | // TODO: Analyze the extrema of our projection into XYZ and use suitable primaries? |
| 59 | // For now, just fall back to a default, because we don't have a good answer. |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 60 | return SkColorSpace::MakeSRGBLinear(); |
Brian Osman | 12313f0 | 2016-11-07 09:42:37 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | sk_sp<SkColorSpace> makeSRGBGamma() override { |
| 64 | // See comment in makeLinearGamma |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 65 | return SkColorSpace::MakeSRGB(); |
Brian Osman | 12313f0 | 2016-11-07 09:42:37 -0500 | [diff] [blame] | 66 | } |
| 67 | |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 68 | Type type() const override { return Type::kA2B; } |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 69 | |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 70 | class Element { |
| 71 | public: |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 72 | Element(SkGammaNamed gammaNamed, int channelCount) |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 73 | : fType(Type::kGammaNamed) |
| 74 | , fGammaNamed(gammaNamed) |
| 75 | , fMatrix(SkMatrix44::kUninitialized_Constructor) |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 76 | , fInputChannels(channelCount) |
raftias | e8ea07a | 2016-12-05 14:21:45 -0500 | [diff] [blame] | 77 | , fOutputChannels(channelCount) { |
| 78 | SkASSERT(gammaNamed != kNonStandard_SkGammaNamed); |
| 79 | } |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 80 | |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 81 | explicit Element(sk_sp<SkGammas> gammas) |
| 82 | : fType(Type::kGammas) |
| 83 | , fGammas(std::move(gammas)) |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 84 | , fMatrix(SkMatrix44::kUninitialized_Constructor) |
| 85 | , fInputChannels(fGammas->channels()) |
raftias | 197e311 | 2016-12-01 15:31:29 -0500 | [diff] [blame] | 86 | , fOutputChannels(fGammas->channels()) { |
| 87 | for (int i = 0; i < fGammas->channels(); ++i) { |
| 88 | if (SkGammas::Type::kTable_Type == fGammas->type(i)) { |
| 89 | SkASSERT(fGammas->data(i).fTable.fSize >= 2); |
| 90 | } |
| 91 | } |
| 92 | } |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 93 | |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 94 | explicit Element(sk_sp<SkColorLookUpTable> colorLUT) |
| 95 | : fType(Type::kCLUT) |
| 96 | , fCLUT(std::move(colorLUT)) |
| 97 | , fMatrix(SkMatrix44::kUninitialized_Constructor) |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 98 | , fInputChannels(fCLUT->inputChannels()) |
| 99 | , fOutputChannels(fCLUT->outputChannels()) |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 100 | {} |
| 101 | |
| 102 | explicit Element(const SkMatrix44& matrix) |
| 103 | : fType(Type::kMatrix) |
| 104 | , fMatrix(matrix) |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 105 | , fInputChannels(3) |
| 106 | , fOutputChannels(3) |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 107 | {} |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 108 | |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 109 | enum class Type { |
| 110 | kGammaNamed, |
| 111 | kGammas, |
| 112 | kCLUT, |
| 113 | kMatrix |
| 114 | }; |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 115 | |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 116 | Type type() const { return fType; } |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 117 | |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 118 | SkGammaNamed gammaNamed() const { |
| 119 | SkASSERT(Type::kGammaNamed == fType); |
| 120 | return fGammaNamed; |
| 121 | } |
| 122 | |
| 123 | const SkGammas& gammas() const { |
| 124 | SkASSERT(Type::kGammas == fType); |
| 125 | return *fGammas; |
| 126 | } |
| 127 | |
| 128 | const SkColorLookUpTable& colorLUT() const { |
| 129 | SkASSERT(Type::kCLUT == fType); |
| 130 | return *fCLUT; |
| 131 | } |
| 132 | |
| 133 | const SkMatrix44& matrix() const { |
| 134 | SkASSERT(Type::kMatrix == fType); |
| 135 | return fMatrix; |
| 136 | } |
| 137 | |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 138 | int inputChannels() const { return fInputChannels; } |
| 139 | |
| 140 | int outputChannels() const { return fOutputChannels; } |
| 141 | |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 142 | private: |
| 143 | Type fType; |
| 144 | SkGammaNamed fGammaNamed; |
| 145 | sk_sp<SkGammas> fGammas; |
| 146 | sk_sp<SkColorLookUpTable> fCLUT; |
| 147 | SkMatrix44 fMatrix; |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 148 | int fInputChannels; |
| 149 | int fOutputChannels; |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 150 | }; |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 151 | const Element& element(int i) const { return fElements[i]; } |
| 152 | |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 153 | int count() const { return (int)fElements.size(); } |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 154 | |
| 155 | // the intermediate profile connection space that this color space |
| 156 | // represents the transformation to |
| 157 | enum class PCS : uint8_t { |
| 158 | kLAB, // CIELAB |
| 159 | kXYZ // CIEXYZ |
| 160 | }; |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 161 | |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 162 | PCS pcs() const { return fPCS; } |
| 163 | |
Matt Sarett | 523116d | 2017-01-12 18:36:38 -0500 | [diff] [blame] | 164 | ICCTypeFlag iccType() const { return fICCType; } |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 165 | |
Matt Sarett | 523116d | 2017-01-12 18:36:38 -0500 | [diff] [blame] | 166 | SkColorSpace_A2B(ICCTypeFlag iccType, std::vector<Element> elements, PCS pcs, |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 167 | sk_sp<SkData> profileData); |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 168 | |
Matt Sarett | 595599f | 2016-12-15 13:05:53 -0500 | [diff] [blame] | 169 | private: |
Matt Sarett | 523116d | 2017-01-12 18:36:38 -0500 | [diff] [blame] | 170 | ICCTypeFlag fICCType; |
Mike Klein | 62458a6 | 2016-12-01 16:38:16 +0000 | [diff] [blame] | 171 | std::vector<Element> fElements; |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 172 | PCS fPCS; |
raftias | 026f223 | 2016-10-24 09:52:26 -0700 | [diff] [blame] | 173 | |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 174 | friend class SkColorSpace_Base; |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 175 | friend class ColorSpaceXformTest; |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 176 | typedef SkColorSpace_Base INHERITED; |
| 177 | }; |
| 178 | |
| 179 | #endif |