| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkColorSpace_Base_DEFINED | 8 #ifndef SkColorSpace_Base_DEFINED |
| 9 #define SkColorSpace_Base_DEFINED | 9 #define SkColorSpace_Base_DEFINED |
| 10 | 10 |
| 11 #include "SkColorSpace.h" | 11 #include "SkColorSpace.h" |
| 12 #include "SkData.h" | 12 #include "SkData.h" |
| 13 #include "SkTemplates.h" | 13 #include "SkTemplates.h" |
| 14 | 14 |
| 15 struct SkGammas : SkRefCnt { | 15 struct SkGammaCurve { |
| 16 bool isNamed() const { |
| 17 bool result = (SkColorSpace::kNonStandard_GammaNamed != fNamed); |
| 18 SkASSERT(!result || (0.0f == fValue)); |
| 19 SkASSERT(!result || (0 == fTableSize)); |
| 20 SkASSERT(!result || (0.0f == fG && 0.0f == fE)); |
| 21 return result; |
| 22 } |
| 16 | 23 |
| 17 // There are four possible representations for gamma curves. kNone_Type is
used | 24 bool isValue() const { |
| 18 // as a placeholder until the struct is initialized. It is not a valid valu
e. | 25 bool result = (0.0f != fValue); |
| 19 enum class Type : uint8_t { | 26 SkASSERT(!result || SkColorSpace::kNonStandard_GammaNamed == fNamed); |
| 20 kNone_Type, | 27 SkASSERT(!result || (0 == fTableSize)); |
| 21 kNamed_Type, | 28 SkASSERT(!result || (0.0f == fG && 0.0f == fE)); |
| 22 kValue_Type, | 29 return result; |
| 23 kTable_Type, | 30 } |
| 24 kParam_Type, | |
| 25 }; | |
| 26 | 31 |
| 27 // Contains information for a gamma table. | 32 bool isTable() const { |
| 28 struct Table { | 33 bool result = (0 != fTableSize); |
| 29 size_t fOffset; | 34 SkASSERT(!result || SkColorSpace::kNonStandard_GammaNamed == fNamed); |
| 30 int fSize; | 35 SkASSERT(!result || (0.0f == fValue)); |
| 36 SkASSERT(!result || (0.0f == fG && 0.0f == fE)); |
| 37 SkASSERT(!result || fTable); |
| 38 return result; |
| 39 } |
| 31 | 40 |
| 32 const float* table(const SkGammas* base) const { | 41 bool isParametric() const { |
| 33 return SkTAddOffset<const float>(base, sizeof(SkGammas) + fOffset); | 42 bool result = (0.0f != fG || 0.0f != fE); |
| 34 } | 43 SkASSERT(!result || SkColorSpace::kNonStandard_GammaNamed == fNamed); |
| 35 }; | 44 SkASSERT(!result || (0.0f == fValue)); |
| 45 SkASSERT(!result || (0 == fTableSize)); |
| 46 return result; |
| 47 } |
| 36 | 48 |
| 37 // Contains the parameters for a parametric curve. | 49 // We have four different ways to represent gamma. |
| 38 struct Params { | 50 // (1) A known, named type: |
| 39 // Y = (aX + b)^g + c for X >= d | 51 SkColorSpace::GammaNamed fNamed; |
| 40 // Y = eX + f otherwise | |
| 41 float fG; | |
| 42 float fA; | |
| 43 float fB; | |
| 44 float fC; | |
| 45 float fD; | |
| 46 float fE; | |
| 47 float fF; | |
| 48 }; | |
| 49 | 52 |
| 50 // Contains the actual gamma curve information. Should be interpreted | 53 // (2) A single value: |
| 51 // based on the type of the gamma curve. | 54 float fValue; |
| 52 union Data { | |
| 53 Data() | |
| 54 : fTable{ 0, 0 } | |
| 55 {} | |
| 56 | 55 |
| 57 inline bool operator==(const Data& that) const { | 56 // (3) A lookup table: |
| 58 return this->fTable.fOffset == that.fTable.fOffset && | 57 uint32_t fTableSize; |
| 59 this->fTable.fSize == that.fTable.fSize; | 58 std::unique_ptr<float[]> fTable; |
| 59 |
| 60 // (4) Parameters for a curve: |
| 61 // Y = (aX + b)^g + c for X >= d |
| 62 // Y = eX + f otherwise |
| 63 float fG; |
| 64 float fA; |
| 65 float fB; |
| 66 float fC; |
| 67 float fD; |
| 68 float fE; |
| 69 float fF; |
| 70 |
| 71 SkGammaCurve() |
| 72 : fNamed(SkColorSpace::kNonStandard_GammaNamed) |
| 73 , fValue(0.0f) |
| 74 , fTableSize(0) |
| 75 , fTable(nullptr) |
| 76 , fG(0.0f) |
| 77 , fA(0.0f) |
| 78 , fB(0.0f) |
| 79 , fC(0.0f) |
| 80 , fD(0.0f) |
| 81 , fE(0.0f) |
| 82 , fF(0.0f) |
| 83 {} |
| 84 |
| 85 bool quickEquals(const SkGammaCurve& that) const { |
| 86 return (this->fNamed == that.fNamed) && (this->fValue == that.fValue) && |
| 87 (this->fTableSize == that.fTableSize) && (this->fTable == that.f
Table) && |
| 88 (this->fG == that.fG) && (this->fA == that.fA) && (this->fB == t
hat.fB) && |
| 89 (this->fC == that.fC) && (this->fD == that.fD) && (this->fE == t
hat.fE) && |
| 90 (this->fF == that.fF); |
| 91 } |
| 92 }; |
| 93 |
| 94 struct SkGammas : public SkRefCnt { |
| 95 public: |
| 96 static SkColorSpace::GammaNamed Named(SkGammaCurve curves[3]) { |
| 97 if (SkColorSpace::kLinear_GammaNamed == curves[0].fNamed && |
| 98 SkColorSpace::kLinear_GammaNamed == curves[1].fNamed && |
| 99 SkColorSpace::kLinear_GammaNamed == curves[2].fNamed) |
| 100 { |
| 101 return SkColorSpace::kLinear_GammaNamed; |
| 60 } | 102 } |
| 61 | 103 |
| 62 SkColorSpace::GammaNamed fNamed; | 104 if (SkColorSpace::kSRGB_GammaNamed == curves[0].fNamed && |
| 63 float fValue; | 105 SkColorSpace::kSRGB_GammaNamed == curves[1].fNamed && |
| 64 Table fTable; | 106 SkColorSpace::kSRGB_GammaNamed == curves[2].fNamed) |
| 65 size_t fParamOffset; | 107 { |
| 108 return SkColorSpace::kSRGB_GammaNamed; |
| 109 } |
| 66 | 110 |
| 67 const Params& params(const SkGammas* base) const { | 111 if (SkColorSpace::k2Dot2Curve_GammaNamed == curves[0].fNamed && |
| 68 return *SkTAddOffset<const Params>(base, sizeof(SkGammas) + fParamOf
fset); | 112 SkColorSpace::k2Dot2Curve_GammaNamed == curves[1].fNamed && |
| 113 SkColorSpace::k2Dot2Curve_GammaNamed == curves[2].fNamed) |
| 114 { |
| 115 return SkColorSpace::k2Dot2Curve_GammaNamed; |
| 69 } | 116 } |
| 70 }; | |
| 71 | 117 |
| 72 bool isNamed(int i) const { | 118 return SkColorSpace::kNonStandard_GammaNamed; |
| 73 SkASSERT(0 <= i && i < 3); | |
| 74 return (&fRedType)[i] == Type::kNamed_Type; | |
| 75 } | 119 } |
| 76 | 120 |
| 77 bool isValue(int i) const { | 121 const SkGammaCurve& operator[](int i) const { |
| 78 SkASSERT(0 <= i && i < 3); | 122 SkASSERT(0 <= i && i < 3); |
| 79 return (&fRedType)[i] == Type::kValue_Type; | 123 return (&fRed)[i]; |
| 80 } | 124 } |
| 81 | 125 |
| 82 bool isTable(int i) const { | 126 const SkGammaCurve fRed; |
| 83 SkASSERT(0 <= i && i < 3); | 127 const SkGammaCurve fGreen; |
| 84 return (&fRedType)[i] == Type::kTable_Type; | 128 const SkGammaCurve fBlue; |
| 85 } | |
| 86 | 129 |
| 87 bool isParametric(int i) const { | 130 SkGammas(SkGammaCurve red, SkGammaCurve green, SkGammaCurve blue) |
| 88 SkASSERT(0 <= i && i < 3); | 131 : fRed(std::move(red)) |
| 89 return (&fRedType)[i] == Type::kParam_Type; | 132 , fGreen(std::move(green)) |
| 90 } | 133 , fBlue(std::move(blue)) |
| 91 | |
| 92 const Data& data(int i) const { | |
| 93 SkASSERT(0 <= i && i < 3); | |
| 94 return (&fRedData)[i]; | |
| 95 } | |
| 96 | |
| 97 const float* table(int i) const { | |
| 98 SkASSERT(isTable(i)); | |
| 99 return (&fRedData)[i].fTable.table(this); | |
| 100 } | |
| 101 | |
| 102 const Params& params(int i) const { | |
| 103 SkASSERT(isParametric(i)); | |
| 104 return (&fRedData)[i].params(this); | |
| 105 } | |
| 106 | |
| 107 SkGammas() | |
| 108 : fRedType(Type::kNone_Type) | |
| 109 , fGreenType(Type::kNone_Type) | |
| 110 , fBlueType(Type::kNone_Type) | |
| 111 {} | 134 {} |
| 112 | 135 |
| 113 // These fields should only be modified when initializing the struct. | 136 SkGammas() {} |
| 114 Data fRedData; | |
| 115 Data fGreenData; | |
| 116 Data fBlueData; | |
| 117 Type fRedType; | |
| 118 Type fGreenType; | |
| 119 Type fBlueType; | |
| 120 | 137 |
| 121 // Objects of this type are sometimes created in a custom fashion using | 138 friend class SkColorSpace; |
| 122 // sk_malloc_throw and therefore must be sk_freed. We overload new to | |
| 123 // also call sk_malloc_throw so that memory can be unconditionally released | |
| 124 // using sk_free in an overloaded delete. Overloading regular new means we | |
| 125 // must also overload placement new. | |
| 126 void* operator new(size_t size) { return sk_malloc_throw(size); } | |
| 127 void* operator new(size_t, void* p) { return p; } | |
| 128 void operator delete(void* p) { sk_free(p); } | |
| 129 }; | 139 }; |
| 130 | 140 |
| 131 struct SkColorLookUpTable : public SkRefCnt { | 141 struct SkColorLookUpTable : public SkRefCnt { |
| 132 uint8_t fInputChannels; | 142 uint8_t fInputChannels; |
| 133 uint8_t fOutputChannels; | 143 uint8_t fOutputChannels; |
| 134 uint8_t fGridPoints[3]; | 144 uint8_t fGridPoints[3]; |
| 135 std::unique_ptr<float[]> fTable; | 145 std::unique_ptr<float[]> fTable; |
| 136 | 146 |
| 137 SkColorLookUpTable() | 147 SkColorLookUpTable() |
| 138 : fInputChannels(0) | 148 : fInputChannels(0) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 156 * Writes this object as an ICC profile. | 166 * Writes this object as an ICC profile. |
| 157 */ | 167 */ |
| 158 sk_sp<SkData> writeToICC() const; | 168 sk_sp<SkData> writeToICC() const; |
| 159 | 169 |
| 160 private: | 170 private: |
| 161 | 171 |
| 162 static sk_sp<SkColorSpace> NewRGB(GammaNamed gammaNamed, const SkMatrix44& t
oXYZD50); | 172 static sk_sp<SkColorSpace> NewRGB(GammaNamed gammaNamed, const SkMatrix44& t
oXYZD50); |
| 163 | 173 |
| 164 SkColorSpace_Base(GammaNamed gammaNamed, const SkMatrix44& toXYZ, Named name
d); | 174 SkColorSpace_Base(GammaNamed gammaNamed, const SkMatrix44& toXYZ, Named name
d); |
| 165 | 175 |
| 166 SkColorSpace_Base(sk_sp<SkColorLookUpTable> colorLUT, GammaNamed gammaNamed, | 176 SkColorSpace_Base(sk_sp<SkColorLookUpTable> colorLUT, sk_sp<SkGammas> gammas
, |
| 167 sk_sp<SkGammas> gammas, const SkMatrix44& toXYZ, sk_sp<SkD
ata> profileData); | 177 const SkMatrix44& toXYZ, sk_sp<SkData> profileData); |
| 168 | 178 |
| 169 sk_sp<SkColorLookUpTable> fColorLUT; | 179 sk_sp<SkColorLookUpTable> fColorLUT; |
| 170 sk_sp<SkGammas> fGammas; | 180 sk_sp<SkGammas> fGammas; |
| 171 sk_sp<SkData> fProfileData; | 181 sk_sp<SkData> fProfileData; |
| 172 | 182 |
| 173 friend class SkColorSpace; | 183 friend class SkColorSpace; |
| 174 friend class ColorSpaceXformTest; | 184 friend class ColorSpaceXformTest; |
| 175 typedef SkColorSpace INHERITED; | 185 typedef SkColorSpace INHERITED; |
| 176 }; | 186 }; |
| 177 | 187 |
| 178 static inline SkColorSpace_Base* as_CSB(SkColorSpace* colorSpace) { | 188 static inline SkColorSpace_Base* as_CSB(SkColorSpace* colorSpace) { |
| 179 return static_cast<SkColorSpace_Base*>(colorSpace); | 189 return static_cast<SkColorSpace_Base*>(colorSpace); |
| 180 } | 190 } |
| 181 | 191 |
| 182 static inline const SkColorSpace_Base* as_CSB(const SkColorSpace* colorSpace) { | 192 static inline const SkColorSpace_Base* as_CSB(const SkColorSpace* colorSpace) { |
| 183 return static_cast<const SkColorSpace_Base*>(colorSpace); | 193 return static_cast<const SkColorSpace_Base*>(colorSpace); |
| 184 } | 194 } |
| 185 | 195 |
| 186 static inline SkColorSpace_Base* as_CSB(const sk_sp<SkColorSpace>& colorSpace) { | 196 static inline SkColorSpace_Base* as_CSB(const sk_sp<SkColorSpace>& colorSpace) { |
| 187 return static_cast<SkColorSpace_Base*>(colorSpace.get()); | 197 return static_cast<SkColorSpace_Base*>(colorSpace.get()); |
| 188 } | 198 } |
| 189 | 199 |
| 190 #endif | 200 #endif |
| OLD | NEW |