blob: 535ec3b527e142de44850a557d4c58e5afaadeb9 [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"
msarette077e062016-05-24 10:16:53 -070012#include "SkTemplates.h"
msarettbb9f7742016-05-17 09:31:20 -070013
14struct SkGammaCurve {
15 bool isValue() const {
16 bool result = (0.0f != fValue);
17 SkASSERT(!result || (0 == fTableSize));
msarett61a999c2016-05-18 06:28:43 -070018 SkASSERT(!result || (0.0f == fG));
msarettbb9f7742016-05-17 09:31:20 -070019 return result;
20 }
21
22 bool isTable() const {
23 bool result = (0 != fTableSize);
24 SkASSERT(!result || (0.0f == fValue));
msarett61a999c2016-05-18 06:28:43 -070025 SkASSERT(!result || (0.0f == fG));
msarettbb9f7742016-05-17 09:31:20 -070026 SkASSERT(!result || fTable);
27 return result;
28 }
29
msarett61a999c2016-05-18 06:28:43 -070030 bool isParametric() const {
31 bool result = (0.0f != fG);
32 SkASSERT(!result || (0.0f == fValue));
33 SkASSERT(!result || (0 == fTableSize));
34 return result;
35 }
msarettbb9f7742016-05-17 09:31:20 -070036
37 // We have three different ways to represent gamma.
38 // (1) A single value:
39 float fValue;
40
41 // (2) A lookup table:
42 uint32_t fTableSize;
43 std::unique_ptr<float[]> fTable;
44
45 // (3) Parameters for a curve:
msarett61a999c2016-05-18 06:28:43 -070046 // Y = (aX + b)^g + c for X >= d
47 // Y = eX + f otherwise
48 float fG;
49 float fA;
50 float fB;
51 float fC;
52 float fD;
53 float fE;
54 float fF;
msarettbb9f7742016-05-17 09:31:20 -070055
56 SkGammaCurve() {
57 memset(this, 0, sizeof(struct SkGammaCurve));
58 }
59
60 SkGammaCurve(float value)
61 : fValue(value)
62 , fTableSize(0)
63 , fTable(nullptr)
msarett61a999c2016-05-18 06:28:43 -070064 , fG(0.0f)
65 , fA(0.0f)
66 , fB(0.0f)
67 , fC(0.0f)
68 , fD(0.0f)
69 , fE(0.0f)
70 , fF(0.0f)
msarettbb9f7742016-05-17 09:31:20 -070071 {}
72};
73
74struct SkGammas : public SkRefCnt {
75public:
76 bool isValues() const {
77 return fRed.isValue() && fGreen.isValue() && fBlue.isValue();
78 }
79
msarett264f88a2016-05-17 13:57:15 -070080 const SkGammaCurve fRed;
81 const SkGammaCurve fGreen;
82 const SkGammaCurve fBlue;
msarettbb9f7742016-05-17 09:31:20 -070083
84 SkGammas(float red, float green, float blue)
85 : fRed(red)
86 , fGreen(green)
87 , fBlue(blue)
88 {}
89
msarett264f88a2016-05-17 13:57:15 -070090 SkGammas(SkGammaCurve red, SkGammaCurve green, SkGammaCurve blue)
91 : fRed(std::move(red))
92 , fGreen(std::move(green))
93 , fBlue(std::move(blue))
94 {}
95
msarettbb9f7742016-05-17 09:31:20 -070096 SkGammas() {}
97
98 friend class SkColorSpace;
99};
100
101struct SkColorLookUpTable {
102 static const uint8_t kMaxChannels = 16;
103
104 uint8_t fInputChannels;
105 uint8_t fOutputChannels;
106 uint8_t fGridPoints[kMaxChannels];
107 std::unique_ptr<float[]> fTable;
108
109 SkColorLookUpTable() {
110 memset(this, 0, sizeof(struct SkColorLookUpTable));
111 }
112};
113
msarett8cc20912016-05-23 09:29:29 -0700114class SkColorSpace_Base : public SkColorSpace {
115public:
116
msarette077e062016-05-24 10:16:53 -0700117 const sk_sp<SkGammas>& gammas() const { return fGammas; }
msarett8cc20912016-05-23 09:29:29 -0700118
119private:
120
121 SkColorSpace_Base(sk_sp<SkGammas> gammas, const SkMatrix44& toXYZ, Named);
122
123 SkColorSpace_Base(sk_sp<SkGammas> gammas, GammaNamed gammaNamed, const SkMatrix44& toXYZ,
124 Named);
125
126 SkColorSpace_Base(SkColorLookUpTable* colorLUT, sk_sp<SkGammas> gammas,
127 const SkMatrix44& toXYZ);
128
129 SkAutoTDelete<SkColorLookUpTable> fColorLUT;
130 sk_sp<SkGammas> fGammas;
131
132 friend class SkColorSpace;
133 typedef SkColorSpace INHERITED;
134};
135
136static inline SkColorSpace_Base* as_CSB(SkColorSpace* colorSpace) {
137 return static_cast<SkColorSpace_Base*>(colorSpace);
138}
139
msarett888dc162016-05-23 10:21:17 -0700140static inline SkColorSpace_Base* as_CSB(const sk_sp<SkColorSpace>& colorSpace) {
141 return static_cast<SkColorSpace_Base*>(colorSpace.get());
142}
143
msarettbb9f7742016-05-17 09:31:20 -0700144#endif