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 | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 12 | |
| 13 | struct SkGammaCurve { |
| 14 | bool isValue() const { |
| 15 | bool result = (0.0f != fValue); |
| 16 | SkASSERT(!result || (0 == fTableSize)); |
msarett | 61a999c | 2016-05-18 06:28:43 -0700 | [diff] [blame] | 17 | SkASSERT(!result || (0.0f == fG)); |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 18 | return result; |
| 19 | } |
| 20 | |
| 21 | bool isTable() const { |
| 22 | bool result = (0 != fTableSize); |
| 23 | SkASSERT(!result || (0.0f == fValue)); |
msarett | 61a999c | 2016-05-18 06:28:43 -0700 | [diff] [blame] | 24 | SkASSERT(!result || (0.0f == fG)); |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 25 | SkASSERT(!result || fTable); |
| 26 | return result; |
| 27 | } |
| 28 | |
msarett | 61a999c | 2016-05-18 06:28:43 -0700 | [diff] [blame] | 29 | bool isParametric() const { |
| 30 | bool result = (0.0f != fG); |
| 31 | SkASSERT(!result || (0.0f == fValue)); |
| 32 | SkASSERT(!result || (0 == fTableSize)); |
| 33 | return result; |
| 34 | } |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 35 | |
| 36 | // We have three different ways to represent gamma. |
| 37 | // (1) A single value: |
| 38 | float fValue; |
| 39 | |
| 40 | // (2) A lookup table: |
| 41 | uint32_t fTableSize; |
| 42 | std::unique_ptr<float[]> fTable; |
| 43 | |
| 44 | // (3) Parameters for a curve: |
msarett | 61a999c | 2016-05-18 06:28:43 -0700 | [diff] [blame] | 45 | // Y = (aX + b)^g + c for X >= d |
| 46 | // Y = eX + f otherwise |
| 47 | float fG; |
| 48 | float fA; |
| 49 | float fB; |
| 50 | float fC; |
| 51 | float fD; |
| 52 | float fE; |
| 53 | float fF; |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 54 | |
| 55 | SkGammaCurve() { |
| 56 | memset(this, 0, sizeof(struct SkGammaCurve)); |
| 57 | } |
| 58 | |
| 59 | SkGammaCurve(float value) |
| 60 | : fValue(value) |
| 61 | , fTableSize(0) |
| 62 | , fTable(nullptr) |
msarett | 61a999c | 2016-05-18 06:28:43 -0700 | [diff] [blame] | 63 | , fG(0.0f) |
| 64 | , fA(0.0f) |
| 65 | , fB(0.0f) |
| 66 | , fC(0.0f) |
| 67 | , fD(0.0f) |
| 68 | , fE(0.0f) |
| 69 | , fF(0.0f) |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 70 | {} |
| 71 | }; |
| 72 | |
| 73 | struct SkGammas : public SkRefCnt { |
| 74 | public: |
| 75 | bool isValues() const { |
| 76 | return fRed.isValue() && fGreen.isValue() && fBlue.isValue(); |
| 77 | } |
| 78 | |
msarett | 264f88a | 2016-05-17 13:57:15 -0700 | [diff] [blame] | 79 | const SkGammaCurve fRed; |
| 80 | const SkGammaCurve fGreen; |
| 81 | const SkGammaCurve fBlue; |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 82 | |
| 83 | SkGammas(float red, float green, float blue) |
| 84 | : fRed(red) |
| 85 | , fGreen(green) |
| 86 | , fBlue(blue) |
| 87 | {} |
| 88 | |
msarett | 264f88a | 2016-05-17 13:57:15 -0700 | [diff] [blame] | 89 | SkGammas(SkGammaCurve red, SkGammaCurve green, SkGammaCurve blue) |
| 90 | : fRed(std::move(red)) |
| 91 | , fGreen(std::move(green)) |
| 92 | , fBlue(std::move(blue)) |
| 93 | {} |
| 94 | |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 95 | SkGammas() {} |
| 96 | |
| 97 | friend class SkColorSpace; |
| 98 | }; |
| 99 | |
| 100 | struct SkColorLookUpTable { |
| 101 | static const uint8_t kMaxChannels = 16; |
| 102 | |
| 103 | uint8_t fInputChannels; |
| 104 | uint8_t fOutputChannels; |
| 105 | uint8_t fGridPoints[kMaxChannels]; |
| 106 | std::unique_ptr<float[]> fTable; |
| 107 | |
| 108 | SkColorLookUpTable() { |
| 109 | memset(this, 0, sizeof(struct SkColorLookUpTable)); |
| 110 | } |
| 111 | }; |
| 112 | |
msarett | 8cc2091 | 2016-05-23 09:29:29 -0700 | [diff] [blame^] | 113 | class SkColorSpace_Base : public SkColorSpace { |
| 114 | public: |
| 115 | |
| 116 | SkGammas* gammas() const { return fGammas.get(); } |
| 117 | |
| 118 | private: |
| 119 | |
| 120 | SkColorSpace_Base(sk_sp<SkGammas> gammas, const SkMatrix44& toXYZ, Named); |
| 121 | |
| 122 | SkColorSpace_Base(sk_sp<SkGammas> gammas, GammaNamed gammaNamed, const SkMatrix44& toXYZ, |
| 123 | Named); |
| 124 | |
| 125 | SkColorSpace_Base(SkColorLookUpTable* colorLUT, sk_sp<SkGammas> gammas, |
| 126 | const SkMatrix44& toXYZ); |
| 127 | |
| 128 | SkAutoTDelete<SkColorLookUpTable> fColorLUT; |
| 129 | sk_sp<SkGammas> fGammas; |
| 130 | |
| 131 | friend class SkColorSpace; |
| 132 | typedef SkColorSpace INHERITED; |
| 133 | }; |
| 134 | |
| 135 | static inline SkColorSpace_Base* as_CSB(SkColorSpace* colorSpace) { |
| 136 | return static_cast<SkColorSpace_Base*>(colorSpace); |
| 137 | } |
| 138 | |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 139 | #endif |