blob: dbfe7338cd006bbc2238f88dbfa8f07e29f0cfc0 [file] [log] [blame]
msarettbb9f7742016-05-17 09:31:20 -07001/*
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
msarett8cc20912016-05-23 09:29:29 -07008#ifndef SkColorSpace_Base_DEFINED
9#define SkColorSpace_Base_DEFINED
10
11#include "SkColorSpace.h"
msarettbb9f7742016-05-17 09:31:20 -070012
13struct SkGammaCurve {
14 bool isValue() const {
15 bool result = (0.0f != fValue);
16 SkASSERT(!result || (0 == fTableSize));
msarett61a999c2016-05-18 06:28:43 -070017 SkASSERT(!result || (0.0f == fG));
msarettbb9f7742016-05-17 09:31:20 -070018 return result;
19 }
20
21 bool isTable() const {
22 bool result = (0 != fTableSize);
23 SkASSERT(!result || (0.0f == fValue));
msarett61a999c2016-05-18 06:28:43 -070024 SkASSERT(!result || (0.0f == fG));
msarettbb9f7742016-05-17 09:31:20 -070025 SkASSERT(!result || fTable);
26 return result;
27 }
28
msarett61a999c2016-05-18 06:28:43 -070029 bool isParametric() const {
30 bool result = (0.0f != fG);
31 SkASSERT(!result || (0.0f == fValue));
32 SkASSERT(!result || (0 == fTableSize));
33 return result;
34 }
msarettbb9f7742016-05-17 09:31:20 -070035
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:
msarett61a999c2016-05-18 06:28:43 -070045 // 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;
msarettbb9f7742016-05-17 09:31:20 -070054
55 SkGammaCurve() {
56 memset(this, 0, sizeof(struct SkGammaCurve));
57 }
58
59 SkGammaCurve(float value)
60 : fValue(value)
61 , fTableSize(0)
62 , fTable(nullptr)
msarett61a999c2016-05-18 06:28:43 -070063 , 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)
msarettbb9f7742016-05-17 09:31:20 -070070 {}
71};
72
73struct SkGammas : public SkRefCnt {
74public:
75 bool isValues() const {
76 return fRed.isValue() && fGreen.isValue() && fBlue.isValue();
77 }
78
msarett264f88a2016-05-17 13:57:15 -070079 const SkGammaCurve fRed;
80 const SkGammaCurve fGreen;
81 const SkGammaCurve fBlue;
msarettbb9f7742016-05-17 09:31:20 -070082
83 SkGammas(float red, float green, float blue)
84 : fRed(red)
85 , fGreen(green)
86 , fBlue(blue)
87 {}
88
msarett264f88a2016-05-17 13:57:15 -070089 SkGammas(SkGammaCurve red, SkGammaCurve green, SkGammaCurve blue)
90 : fRed(std::move(red))
91 , fGreen(std::move(green))
92 , fBlue(std::move(blue))
93 {}
94
msarettbb9f7742016-05-17 09:31:20 -070095 SkGammas() {}
96
97 friend class SkColorSpace;
98};
99
100struct 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
msarett8cc20912016-05-23 09:29:29 -0700113class SkColorSpace_Base : public SkColorSpace {
114public:
115
116 SkGammas* gammas() const { return fGammas.get(); }
117
118private:
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
135static inline SkColorSpace_Base* as_CSB(SkColorSpace* colorSpace) {
136 return static_cast<SkColorSpace_Base*>(colorSpace);
137}
138
msarettbb9f7742016-05-17 09:31:20 -0700139#endif