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" |
brianosman | 971cd49 | 2016-09-08 10:10:11 -0700 | [diff] [blame] | 13 | #include "SkOnce.h" |
msarett | e077e06 | 2016-05-24 10:16:53 -0700 | [diff] [blame] | 14 | #include "SkTemplates.h" |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 15 | |
msarett | 600c737 | 2016-09-07 12:03:53 -0700 | [diff] [blame] | 16 | enum SkGammaNamed : uint8_t { |
| 17 | kLinear_SkGammaNamed, |
| 18 | kSRGB_SkGammaNamed, |
| 19 | k2Dot2Curve_SkGammaNamed, |
| 20 | kNonStandard_SkGammaNamed, |
| 21 | }; |
| 22 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 23 | struct SkGammas : SkRefCnt { |
msarett | 2aec3ff | 2016-07-20 12:58:52 -0700 | [diff] [blame] | 24 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 25 | // There are four possible representations for gamma curves. kNone_Type is used |
| 26 | // as a placeholder until the struct is initialized. It is not a valid value. |
| 27 | enum class Type : uint8_t { |
| 28 | kNone_Type, |
| 29 | kNamed_Type, |
| 30 | kValue_Type, |
| 31 | kTable_Type, |
| 32 | kParam_Type, |
| 33 | }; |
msarett | 2aec3ff | 2016-07-20 12:58:52 -0700 | [diff] [blame] | 34 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 35 | // Contains information for a gamma table. |
| 36 | struct Table { |
| 37 | size_t fOffset; |
| 38 | int fSize; |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame] | 39 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 40 | const float* table(const SkGammas* base) const { |
| 41 | return SkTAddOffset<const float>(base, sizeof(SkGammas) + fOffset); |
| 42 | } |
| 43 | }; |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame] | 44 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 45 | // Contains the parameters for a parametric curve. |
| 46 | struct Params { |
| 47 | // Y = (aX + b)^g + c for X >= d |
| 48 | // Y = eX + f otherwise |
| 49 | float fG; |
| 50 | float fA; |
| 51 | float fB; |
| 52 | float fC; |
| 53 | float fD; |
| 54 | float fE; |
| 55 | float fF; |
| 56 | }; |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame] | 57 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 58 | // Contains the actual gamma curve information. Should be interpreted |
| 59 | // based on the type of the gamma curve. |
| 60 | union Data { |
| 61 | Data() |
| 62 | : fTable{ 0, 0 } |
| 63 | {} |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame] | 64 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 65 | inline bool operator==(const Data& that) const { |
| 66 | return this->fTable.fOffset == that.fTable.fOffset && |
| 67 | this->fTable.fSize == that.fTable.fSize; |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 68 | } |
| 69 | |
msarett | 600c737 | 2016-09-07 12:03:53 -0700 | [diff] [blame] | 70 | SkGammaNamed fNamed; |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 71 | float fValue; |
| 72 | Table fTable; |
| 73 | size_t fParamOffset; |
| 74 | |
| 75 | const Params& params(const SkGammas* base) const { |
| 76 | return *SkTAddOffset<const Params>(base, sizeof(SkGammas) + fParamOffset); |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 77 | } |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 78 | }; |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 79 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 80 | bool isNamed(int i) const { |
msarett | a714bc3 | 2016-07-29 08:58:33 -0700 | [diff] [blame] | 81 | return Type::kNamed_Type == this->type(i); |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 82 | } |
| 83 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 84 | bool isValue(int i) const { |
msarett | a714bc3 | 2016-07-29 08:58:33 -0700 | [diff] [blame] | 85 | return Type::kValue_Type == this->type(i); |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 86 | } |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 87 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 88 | bool isTable(int i) const { |
msarett | a714bc3 | 2016-07-29 08:58:33 -0700 | [diff] [blame] | 89 | return Type::kTable_Type == this->type(i); |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | bool isParametric(int i) const { |
msarett | a714bc3 | 2016-07-29 08:58:33 -0700 | [diff] [blame] | 93 | return Type::kParam_Type == this->type(i); |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | const Data& data(int i) const { |
msarett | a714bc3 | 2016-07-29 08:58:33 -0700 | [diff] [blame] | 97 | switch (i) { |
| 98 | case 0: |
| 99 | return fRedData; |
| 100 | case 1: |
| 101 | return fGreenData; |
| 102 | case 2: |
| 103 | return fBlueData; |
| 104 | default: |
| 105 | SkASSERT(false); |
| 106 | return fRedData; |
| 107 | } |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | const float* table(int i) const { |
| 111 | SkASSERT(isTable(i)); |
msarett | a714bc3 | 2016-07-29 08:58:33 -0700 | [diff] [blame] | 112 | return this->data(i).fTable.table(this); |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | const Params& params(int i) const { |
| 116 | SkASSERT(isParametric(i)); |
msarett | a714bc3 | 2016-07-29 08:58:33 -0700 | [diff] [blame] | 117 | return this->data(i).params(this); |
| 118 | } |
| 119 | |
| 120 | Type type(int i) const { |
| 121 | switch (i) { |
| 122 | case 0: |
| 123 | return fRedType; |
| 124 | case 1: |
| 125 | return fGreenType; |
| 126 | case 2: |
| 127 | return fBlueType; |
| 128 | default: |
| 129 | SkASSERT(false); |
| 130 | return fRedType; |
| 131 | } |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | SkGammas() |
| 135 | : fRedType(Type::kNone_Type) |
| 136 | , fGreenType(Type::kNone_Type) |
| 137 | , fBlueType(Type::kNone_Type) |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 138 | {} |
| 139 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 140 | // These fields should only be modified when initializing the struct. |
| 141 | Data fRedData; |
| 142 | Data fGreenData; |
| 143 | Data fBlueData; |
| 144 | Type fRedType; |
| 145 | Type fGreenType; |
| 146 | Type fBlueType; |
msarett | 456bf30 | 2016-07-20 16:14:16 -0700 | [diff] [blame] | 147 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 148 | // Objects of this type are sometimes created in a custom fashion using |
| 149 | // sk_malloc_throw and therefore must be sk_freed. We overload new to |
| 150 | // also call sk_malloc_throw so that memory can be unconditionally released |
| 151 | // using sk_free in an overloaded delete. Overloading regular new means we |
| 152 | // must also overload placement new. |
| 153 | void* operator new(size_t size) { return sk_malloc_throw(size); } |
| 154 | void* operator new(size_t, void* p) { return p; } |
| 155 | void operator delete(void* p) { sk_free(p); } |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
msarett | 0f83e01 | 2016-06-23 15:12:52 -0700 | [diff] [blame] | 158 | struct SkColorLookUpTable : public SkRefCnt { |
msarett | 959d45b | 2016-07-21 13:19:03 -0700 | [diff] [blame] | 159 | static constexpr uint8_t kOutputChannels = 3; |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 160 | |
msarett | 959d45b | 2016-07-21 13:19:03 -0700 | [diff] [blame] | 161 | uint8_t fInputChannels; |
| 162 | uint8_t fGridPoints[3]; |
| 163 | |
| 164 | const float* table() const { |
| 165 | return SkTAddOffset<const float>(this, sizeof(SkColorLookUpTable)); |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 166 | } |
msarett | 959d45b | 2016-07-21 13:19:03 -0700 | [diff] [blame] | 167 | |
| 168 | SkColorLookUpTable(uint8_t inputChannels, uint8_t gridPoints[3]) |
| 169 | : fInputChannels(inputChannels) |
| 170 | { |
| 171 | SkASSERT(3 == inputChannels); |
| 172 | memcpy(fGridPoints, gridPoints, 3 * sizeof(uint8_t)); |
| 173 | } |
| 174 | |
| 175 | // Objects of this type are created in a custom fashion using sk_malloc_throw |
| 176 | // and therefore must be sk_freed. |
| 177 | void* operator new(size_t size) = delete; |
| 178 | void* operator new(size_t, void* p) { return p; } |
| 179 | void operator delete(void* p) { sk_free(p); } |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 182 | class SkColorSpace_Base : public SkColorSpace { |
| 183 | public: |
| 184 | |
msarett | d9015a4 | 2016-08-22 12:29:31 -0700 | [diff] [blame] | 185 | static sk_sp<SkColorSpace> NewRGB(const float gammas[3], const SkMatrix44& toXYZD50); |
msarett | c4ce6b5 | 2016-06-16 07:37:41 -0700 | [diff] [blame] | 186 | |
msarett | 600c737 | 2016-09-07 12:03:53 -0700 | [diff] [blame] | 187 | SkGammaNamed gammaNamed() const { return fGammaNamed; } |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 188 | const SkGammas* gammas() const { return fGammas.get(); } |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 189 | |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 190 | const SkColorLookUpTable* colorLUT() const { return fColorLUT.get(); } |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 191 | |
msarett | 7802c3d | 2016-09-28 11:15:27 -0700 | [diff] [blame^] | 192 | const SkMatrix44& toXYZD50() const { return fToXYZD50; } |
brianosman | 971cd49 | 2016-09-08 10:10:11 -0700 | [diff] [blame] | 193 | const SkMatrix44& fromXYZD50() const; |
| 194 | |
msarett | c213f0d | 2016-08-01 14:23:32 -0700 | [diff] [blame] | 195 | private: |
| 196 | |
msarett | ab926f0 | 2016-05-25 08:53:40 -0700 | [diff] [blame] | 197 | /** |
msarett | c213f0d | 2016-08-01 14:23:32 -0700 | [diff] [blame] | 198 | * FIXME (msarett): |
| 199 | * Hiding this function until we can determine if we need it. Known issues include: |
| 200 | * Only writes 3x3 matrices |
| 201 | * Only writes float gammas |
| 202 | * Rejected by some parsers because the "profile description" is empty |
msarett | ab926f0 | 2016-05-25 08:53:40 -0700 | [diff] [blame] | 203 | */ |
| 204 | sk_sp<SkData> writeToICC() const; |
| 205 | |
msarett | 600c737 | 2016-09-07 12:03:53 -0700 | [diff] [blame] | 206 | static sk_sp<SkColorSpace> NewRGB(SkGammaNamed gammaNamed, const SkMatrix44& toXYZD50); |
msarett | ab926f0 | 2016-05-25 08:53:40 -0700 | [diff] [blame] | 207 | |
msarett | 600c737 | 2016-09-07 12:03:53 -0700 | [diff] [blame] | 208 | SkColorSpace_Base(SkGammaNamed gammaNamed, const SkMatrix44& toXYZ); |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 209 | |
msarett | 600c737 | 2016-09-07 12:03:53 -0700 | [diff] [blame] | 210 | SkColorSpace_Base(sk_sp<SkColorLookUpTable> colorLUT, SkGammaNamed gammaNamed, |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 211 | sk_sp<SkGammas> gammas, const SkMatrix44& toXYZ, sk_sp<SkData> profileData); |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 212 | |
msarett | 0f83e01 | 2016-06-23 15:12:52 -0700 | [diff] [blame] | 213 | sk_sp<SkColorLookUpTable> fColorLUT; |
msarett | 600c737 | 2016-09-07 12:03:53 -0700 | [diff] [blame] | 214 | const SkGammaNamed fGammaNamed; |
msarett | 0f83e01 | 2016-06-23 15:12:52 -0700 | [diff] [blame] | 215 | sk_sp<SkGammas> fGammas; |
| 216 | sk_sp<SkData> fProfileData; |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 217 | |
msarett | 7802c3d | 2016-09-28 11:15:27 -0700 | [diff] [blame^] | 218 | const SkMatrix44 fToXYZD50; |
brianosman | 971cd49 | 2016-09-08 10:10:11 -0700 | [diff] [blame] | 219 | mutable SkMatrix44 fFromXYZD50; |
| 220 | mutable SkOnce fFromXYZOnce; |
| 221 | |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 222 | friend class SkColorSpace; |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 223 | friend class ColorSpaceXformTest; |
msarett | c213f0d | 2016-08-01 14:23:32 -0700 | [diff] [blame] | 224 | friend class ColorSpaceTest; |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame] | 225 | typedef SkColorSpace INHERITED; |
| 226 | }; |
| 227 | |
| 228 | static inline SkColorSpace_Base* as_CSB(SkColorSpace* colorSpace) { |
| 229 | return static_cast<SkColorSpace_Base*>(colorSpace); |
| 230 | } |
| 231 | |
msarett | ab926f0 | 2016-05-25 08:53:40 -0700 | [diff] [blame] | 232 | static inline const SkColorSpace_Base* as_CSB(const SkColorSpace* colorSpace) { |
| 233 | return static_cast<const SkColorSpace_Base*>(colorSpace); |
| 234 | } |
| 235 | |
msarett | 888dc16 | 2016-05-23 10:21:17 -0700 | [diff] [blame] | 236 | static inline SkColorSpace_Base* as_CSB(const sk_sp<SkColorSpace>& colorSpace) { |
| 237 | return static_cast<SkColorSpace_Base*>(colorSpace.get()); |
| 238 | } |
| 239 | |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 240 | #endif |