msarett | bb9f774 | 2016-05-17 09:31:20 -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 | |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 8 | #ifndef SkColorSpace_Base_DEFINED |
| 9 | #define SkColorSpace_Base_DEFINED |
| 10 | |
| 11 | #include "SkColorSpace.h" |
msarett | ab926f0 | 2016-05-25 08:53:40 -0700 | [diff] [blame] | 12 | #include "SkData.h" |
msarett | e077e06 | 2016-05-24 10:16:53 -0700 | [diff] [blame] | 13 | #include "SkTemplates.h" |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 14 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 15 | struct SkGammas : SkRefCnt { |
msarett | 2aec3ff | 2016-07-20 12:58:52 -0700 | [diff] [blame] | 16 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 17 | // There are four possible representations for gamma curves. kNone_Type is used |
| 18 | // as a placeholder until the struct is initialized. It is not a valid value. |
| 19 | enum class Type : uint8_t { |
| 20 | kNone_Type, |
| 21 | kNamed_Type, |
| 22 | kValue_Type, |
| 23 | kTable_Type, |
| 24 | kParam_Type, |
| 25 | }; |
msarett | 2aec3ff | 2016-07-20 12:58:52 -0700 | [diff] [blame] | 26 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 27 | // Contains information for a gamma table. |
| 28 | struct Table { |
| 29 | size_t fOffset; |
| 30 | int fSize; |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame] | 31 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 32 | const float* table(const SkGammas* base) const { |
| 33 | return SkTAddOffset<const float>(base, sizeof(SkGammas) + fOffset); |
| 34 | } |
| 35 | }; |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame] | 36 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 37 | // Contains the parameters for a parametric curve. |
| 38 | struct Params { |
| 39 | // Y = (aX + b)^g + c for X >= d |
| 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 | }; |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame] | 49 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 50 | // Contains the actual gamma curve information. Should be interpreted |
| 51 | // based on the type of the gamma curve. |
| 52 | union Data { |
| 53 | Data() |
| 54 | : fTable{ 0, 0 } |
| 55 | {} |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame] | 56 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 57 | inline bool operator==(const Data& that) const { |
| 58 | return this->fTable.fOffset == that.fTable.fOffset && |
| 59 | this->fTable.fSize == that.fTable.fSize; |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 60 | } |
| 61 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 62 | SkColorSpace::GammaNamed fNamed; |
| 63 | float fValue; |
| 64 | Table fTable; |
| 65 | size_t fParamOffset; |
| 66 | |
| 67 | const Params& params(const SkGammas* base) const { |
| 68 | return *SkTAddOffset<const Params>(base, sizeof(SkGammas) + fParamOffset); |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 69 | } |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 70 | }; |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 71 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 72 | bool isNamed(int i) const { |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 73 | SkASSERT(0 <= i && i < 3); |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 74 | return (&fRedType)[i] == Type::kNamed_Type; |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 75 | } |
| 76 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 77 | bool isValue(int i) const { |
| 78 | SkASSERT(0 <= i && i < 3); |
| 79 | return (&fRedType)[i] == Type::kValue_Type; |
| 80 | } |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 81 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 82 | bool isTable(int i) const { |
| 83 | SkASSERT(0 <= i && i < 3); |
| 84 | return (&fRedType)[i] == Type::kTable_Type; |
| 85 | } |
| 86 | |
| 87 | bool isParametric(int i) const { |
| 88 | SkASSERT(0 <= i && i < 3); |
| 89 | return (&fRedType)[i] == Type::kParam_Type; |
| 90 | } |
| 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) |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 111 | {} |
| 112 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 113 | // These fields should only be modified when initializing the struct. |
| 114 | Data fRedData; |
| 115 | Data fGreenData; |
| 116 | Data fBlueData; |
| 117 | Type fRedType; |
| 118 | Type fGreenType; |
| 119 | Type fBlueType; |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 120 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 121 | // Objects of this type are sometimes created in a custom fashion using |
| 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); } |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
msarett | 0f83e01 | 2016-06-23 15:12:52 -0700 | [diff] [blame] | 131 | struct SkColorLookUpTable : public SkRefCnt { |
msarett | 959d45b | 2016-07-21 13:19:03 -0700 | [diff] [blame^] | 132 | static constexpr uint8_t kOutputChannels = 3; |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 133 | |
msarett | 959d45b | 2016-07-21 13:19:03 -0700 | [diff] [blame^] | 134 | uint8_t fInputChannels; |
| 135 | uint8_t fGridPoints[3]; |
| 136 | |
| 137 | const float* table() const { |
| 138 | return SkTAddOffset<const float>(this, sizeof(SkColorLookUpTable)); |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 139 | } |
msarett | 959d45b | 2016-07-21 13:19:03 -0700 | [diff] [blame^] | 140 | |
| 141 | SkColorLookUpTable(uint8_t inputChannels, uint8_t gridPoints[3]) |
| 142 | : fInputChannels(inputChannels) |
| 143 | { |
| 144 | SkASSERT(3 == inputChannels); |
| 145 | memcpy(fGridPoints, gridPoints, 3 * sizeof(uint8_t)); |
| 146 | } |
| 147 | |
| 148 | // Objects of this type are created in a custom fashion using sk_malloc_throw |
| 149 | // and therefore must be sk_freed. |
| 150 | void* operator new(size_t size) = delete; |
| 151 | void* operator new(size_t, void* p) { return p; } |
| 152 | void operator delete(void* p) { sk_free(p); } |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 155 | class SkColorSpace_Base : public SkColorSpace { |
| 156 | public: |
| 157 | |
msarett | c4ce6b5 | 2016-06-16 07:37:41 -0700 | [diff] [blame] | 158 | static sk_sp<SkColorSpace> NewRGB(float gammas[3], const SkMatrix44& toXYZD50); |
| 159 | |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 160 | const SkGammas* gammas() const { return fGammas.get(); } |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 161 | |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 162 | const SkColorLookUpTable* colorLUT() const { return fColorLUT.get(); } |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 163 | |
msarett | ab926f0 | 2016-05-25 08:53:40 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Writes this object as an ICC profile. |
| 166 | */ |
| 167 | sk_sp<SkData> writeToICC() const; |
| 168 | |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 169 | private: |
| 170 | |
msarett | 111a42d | 2016-06-22 08:18:54 -0700 | [diff] [blame] | 171 | static sk_sp<SkColorSpace> NewRGB(GammaNamed gammaNamed, const SkMatrix44& toXYZD50); |
msarett | ab926f0 | 2016-05-25 08:53:40 -0700 | [diff] [blame] | 172 | |
msarett | 111a42d | 2016-06-22 08:18:54 -0700 | [diff] [blame] | 173 | SkColorSpace_Base(GammaNamed gammaNamed, const SkMatrix44& toXYZ, Named named); |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 174 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 175 | SkColorSpace_Base(sk_sp<SkColorLookUpTable> colorLUT, GammaNamed gammaNamed, |
| 176 | sk_sp<SkGammas> gammas, const SkMatrix44& toXYZ, sk_sp<SkData> profileData); |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 177 | |
msarett | 0f83e01 | 2016-06-23 15:12:52 -0700 | [diff] [blame] | 178 | sk_sp<SkColorLookUpTable> fColorLUT; |
| 179 | sk_sp<SkGammas> fGammas; |
| 180 | sk_sp<SkData> fProfileData; |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 181 | |
| 182 | friend class SkColorSpace; |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 183 | friend class ColorSpaceXformTest; |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 184 | typedef SkColorSpace INHERITED; |
| 185 | }; |
| 186 | |
| 187 | static inline SkColorSpace_Base* as_CSB(SkColorSpace* colorSpace) { |
| 188 | return static_cast<SkColorSpace_Base*>(colorSpace); |
| 189 | } |
| 190 | |
msarett | ab926f0 | 2016-05-25 08:53:40 -0700 | [diff] [blame] | 191 | static inline const SkColorSpace_Base* as_CSB(const SkColorSpace* colorSpace) { |
| 192 | return static_cast<const SkColorSpace_Base*>(colorSpace); |
| 193 | } |
| 194 | |
msarett | 888dc16 | 2016-05-23 10:21:17 -0700 | [diff] [blame] | 195 | static inline SkColorSpace_Base* as_CSB(const sk_sp<SkColorSpace>& colorSpace) { |
| 196 | return static_cast<SkColorSpace_Base*>(colorSpace.get()); |
| 197 | } |
| 198 | |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 199 | #endif |