Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: ui/gfx/color_transform.cc

Issue 2696603003: color: Towards ColorTransform optimizations and code generation (Closed)
Patch Set: Remove unneeded refactor Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/color_transform.h" 5 #include "ui/gfx/color_transform.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath>
hubbe 2017/02/13 22:03:18 verbose :)
ccameron 2017/02/13 23:18:42 Agree :/ ...but I got a whole bunch of presubmit
8 #include <vector> 9 #include <vector>
9 10
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "third_party/qcms/src/qcms.h"
12 #include "ui/gfx/color_space.h" 14 #include "ui/gfx/color_space.h"
13 #include "ui/gfx/icc_profile.h" 15 #include "ui/gfx/icc_profile.h"
16 #include "ui/gfx/skia_color_space_util.h"
14 #include "ui/gfx/transform.h" 17 #include "ui/gfx/transform.h"
15 #include "third_party/qcms/src/qcms.h"
16 18
17 #ifndef THIS_MUST_BE_INCLUDED_AFTER_QCMS_H 19 #ifndef THIS_MUST_BE_INCLUDED_AFTER_QCMS_H
18 extern "C" { 20 extern "C" {
19 #include "third_party/qcms/src/chain.h" 21 #include "third_party/qcms/src/chain.h"
20 }; 22 };
21 #endif 23 #endif
22 24
23 namespace gfx { 25 namespace gfx {
24 26
25 float EvalSkTransferFn(const SkColorSpaceTransferFn& fn, float x) { 27 namespace {
26 if (x < 0) 28
27 return 0; 29 // Helper for scoped QCMS profiles.
28 if (x < fn.fD) 30 struct QcmsProfileDeleter {
29 return fn.fC * x + fn.fF; 31 void operator()(qcms_profile* p) {
30 return powf(fn.fA * x + fn.fB, fn.fG) + fn.fE; 32 if (p) {
31 } 33 qcms_profile_release(p);
34 }
35 }
36 };
37 using ScopedQcmsProfile = std::unique_ptr<qcms_profile, QcmsProfileDeleter>;
32 38
33 Transform Invert(const Transform& t) { 39 Transform Invert(const Transform& t) {
34 Transform ret = t; 40 Transform ret = t;
35 if (!t.GetInverse(&ret)) { 41 if (!t.GetInverse(&ret)) {
36 LOG(ERROR) << "Inverse should alsways be possible."; 42 LOG(ERROR) << "Inverse should alsways be possible.";
37 } 43 }
38 return ret; 44 return ret;
39 } 45 }
40 46
41 float FromLinear(ColorSpace::TransferID id, float v) { 47 float FromLinear(ColorSpace::TransferID id, float v) {
42 switch (id) { 48 switch (id) {
43 case ColorSpace::TransferID::SMPTEST2084_NON_HDR: 49 case ColorSpace::TransferID::SMPTEST2084_NON_HDR:
44 // Should already be handled. 50 // Should already be handled.
45 break; 51 break;
46 52
47 case ColorSpace::TransferID::LOG: 53 case ColorSpace::TransferID::LOG:
48 if (v < 0.01f) 54 if (v < 0.01f)
49 return 0.0f; 55 return 0.0f;
50 return 1.0f + log(v) / log(10.0f) / 2.0f; 56 return 1.0f + std::log(v) / std::log(10.0f) / 2.0f;
51 57
52 case ColorSpace::TransferID::LOG_SQRT: 58 case ColorSpace::TransferID::LOG_SQRT:
53 if (v < sqrt(10.0f) / 1000.0f) 59 if (v < std::sqrt(10.0f) / 1000.0f)
54 return 0.0f; 60 return 0.0f;
55 return 1.0f + log(v) / log(10.0f) / 2.5f; 61 return 1.0f + std::log(v) / std::log(10.0f) / 2.5f;
56 62
57 case ColorSpace::TransferID::IEC61966_2_4: { 63 case ColorSpace::TransferID::IEC61966_2_4: {
58 float a = 1.099296826809442f; 64 float a = 1.099296826809442f;
59 float b = 0.018053968510807f; 65 float b = 0.018053968510807f;
60 if (v < -b) { 66 if (v < -b) {
61 return -a * powf(-v, 0.45f) + (a - 1.0f); 67 return -a * std::pow(-v, 0.45f) + (a - 1.0f);
62 } else if (v <= b) { 68 } else if (v <= b) {
63 return 4.5f * v; 69 return 4.5f * v;
64 } else { 70 } else {
65 return a * powf(v, 0.45f) - (a - 1.0f); 71 return a * std::pow(v, 0.45f) - (a - 1.0f);
66 } 72 }
67 } 73 }
68 74
69 case ColorSpace::TransferID::BT1361_ECG: { 75 case ColorSpace::TransferID::BT1361_ECG: {
70 float a = 1.099f; 76 float a = 1.099f;
71 float b = 0.018f; 77 float b = 0.018f;
72 float l = 0.0045f; 78 float l = 0.0045f;
73 if (v < -l) { 79 if (v < -l) {
74 return -(a * powf(-4.0f * v, 0.45f) + (a - 1.0f)) / 4.0f; 80 return -(a * std::pow(-4.0f * v, 0.45f) + (a - 1.0f)) / 4.0f;
75 } else if (v <= b) { 81 } else if (v <= b) {
76 return 4.5f * v; 82 return 4.5f * v;
77 } else { 83 } else {
78 return a * powf(v, 0.45f) - (a - 1.0f); 84 return a * std::pow(v, 0.45f) - (a - 1.0f);
79 } 85 }
80 } 86 }
81 87
82 case ColorSpace::TransferID::SMPTEST2084: { 88 case ColorSpace::TransferID::SMPTEST2084: {
83 // Go from scRGB levels to 0-1. 89 // Go from scRGB levels to 0-1.
84 v *= 80.0f / 10000.0f; 90 v *= 80.0f / 10000.0f;
85 v = fmax(0.0f, v); 91 v = std::max(0.0f, v);
86 float m1 = (2610.0f / 4096.0f) / 4.0f; 92 float m1 = (2610.0f / 4096.0f) / 4.0f;
87 float m2 = (2523.0f / 4096.0f) * 128.0f; 93 float m2 = (2523.0f / 4096.0f) * 128.0f;
88 float c1 = 3424.0f / 4096.0f; 94 float c1 = 3424.0f / 4096.0f;
89 float c2 = (2413.0f / 4096.0f) * 32.0f; 95 float c2 = (2413.0f / 4096.0f) * 32.0f;
90 float c3 = (2392.0f / 4096.0f) * 32.0f; 96 float c3 = (2392.0f / 4096.0f) * 32.0f;
91 return powf((c1 + c2 * powf(v, m1)) / (1.0f + c3 * powf(v, m1)), m2); 97 return std::pow(
98 (c1 + c2 * std::pow(v, m1)) / (1.0f + c3 * std::pow(v, m1)), m2);
92 } 99 }
93 100
94 // Spec: http://www.arib.or.jp/english/html/overview/doc/2-STD-B67v1_0.pdf 101 // Spec: http://www.arib.or.jp/english/html/overview/doc/2-STD-B67v1_0.pdf
95 case ColorSpace::TransferID::ARIB_STD_B67: { 102 case ColorSpace::TransferID::ARIB_STD_B67: {
96 const float a = 0.17883277f; 103 const float a = 0.17883277f;
97 const float b = 0.28466892f; 104 const float b = 0.28466892f;
98 const float c = 0.55991073f; 105 const float c = 0.55991073f;
99 v = fmax(0.0f, v); 106 v = std::max(0.0f, v);
100 if (v <= 1) 107 if (v <= 1)
101 return 0.5f * sqrtf(v); 108 return 0.5f * sqrtf(v);
102 else 109 else
103 return a * log(v - b) + c; 110 return a * std::log(v - b) + c;
104 } 111 }
105 112
106 default: 113 default:
107 // Handled by SkColorSpaceTransferFn. 114 // Handled by SkColorSpaceTransferFn.
108 break; 115 break;
109 } 116 }
110 NOTREACHED(); 117 NOTREACHED();
111 return 0; 118 return 0;
112 } 119 }
113 120
114 float ToLinear(ColorSpace::TransferID id, float v) { 121 float ToLinear(ColorSpace::TransferID id, float v) {
115 switch (id) { 122 switch (id) {
116 case ColorSpace::TransferID::LOG: 123 case ColorSpace::TransferID::LOG:
117 if (v < 0.0f) 124 if (v < 0.0f)
118 return 0.0f; 125 return 0.0f;
119 return powf(10.0f, (v - 1.0f) * 2.0f); 126 return std::pow(10.0f, (v - 1.0f) * 2.0f);
120 127
121 case ColorSpace::TransferID::LOG_SQRT: 128 case ColorSpace::TransferID::LOG_SQRT:
122 if (v < 0.0f) 129 if (v < 0.0f)
123 return 0.0f; 130 return 0.0f;
124 return powf(10.0f, (v - 1.0f) * 2.5f); 131 return std::pow(10.0f, (v - 1.0f) * 2.5f);
125 132
126 case ColorSpace::TransferID::IEC61966_2_4: { 133 case ColorSpace::TransferID::IEC61966_2_4: {
127 float a = 1.099296826809442f; 134 float a = 1.099296826809442f;
128 float b = 0.018053968510807f; 135 float c = -1.047844f; // FromLinear(IEC61966_2_4, -a)
129 if (v < FromLinear(ColorSpace::TransferID::IEC61966_2_4, -a)) { 136 float d = 0.081243f; // FromLinear(IEC61966_2_4, b));
130 return -powf((a - 1.0f - v) / a, 1.0f / 0.45f); 137 if (v < c) {
131 } else if (v <= FromLinear(ColorSpace::TransferID::IEC61966_2_4, b)) { 138 return -std::pow((a - 1.0f - v) / a, 1.0f / 0.45f);
139 } else if (v <= d) {
132 return v / 4.5f; 140 return v / 4.5f;
133 } else { 141 } else {
134 return powf((v + a - 1.0f) / a, 1.0f / 0.45f); 142 return std::pow((v + a - 1.0f) / a, 1.0f / 0.45f);
135 } 143 }
136 } 144 }
137 145
138 case ColorSpace::TransferID::BT1361_ECG: { 146 case ColorSpace::TransferID::BT1361_ECG: {
139 float a = 1.099f; 147 float a = 1.099f;
140 float b = 0.018f; 148 float c = -0.02025f; // FromLinear(BT1361_ECG, -l)
hubbe 2017/02/13 22:03:18 Not a fan of refactoring and adding slight modific
ccameron 2017/02/13 23:18:42 This should be functionally identical -- it just r
141 float l = 0.0045f; 149 float d = 0.081f; // FromLinear(BT1361_ECG, b)
142 if (v < FromLinear(ColorSpace::TransferID::BT1361_ECG, -l)) { 150 if (v < c) {
143 return -powf((1.0f - a - v * 4.0f) / a, 1.0f / 0.45f) / 4.0f; 151 return -std::pow((1.0f - a - v * 4.0f) / a, 1.0f / 0.45f) / 4.0f;
144 } else if (v <= FromLinear(ColorSpace::TransferID::BT1361_ECG, b)) { 152 } else if (v <= d) {
145 return v / 4.5f; 153 return v / 4.5f;
146 } else { 154 } else {
147 return powf((v + a - 1.0f) / a, 1.0f / 0.45f); 155 return std::pow((v + a - 1.0f) / a, 1.0f / 0.45f);
148 } 156 }
149 } 157 }
150 158
151 case ColorSpace::TransferID::SMPTEST2084: { 159 case ColorSpace::TransferID::SMPTEST2084: {
152 v = fmax(0.0f, v); 160 v = std::max(0.0f, v);
153 float m1 = (2610.0f / 4096.0f) / 4.0f; 161 float m1 = (2610.0f / 4096.0f) / 4.0f;
154 float m2 = (2523.0f / 4096.0f) * 128.0f; 162 float m2 = (2523.0f / 4096.0f) * 128.0f;
155 float c1 = 3424.0f / 4096.0f; 163 float c1 = 3424.0f / 4096.0f;
156 float c2 = (2413.0f / 4096.0f) * 32.0f; 164 float c2 = (2413.0f / 4096.0f) * 32.0f;
157 float c3 = (2392.0f / 4096.0f) * 32.0f; 165 float c3 = (2392.0f / 4096.0f) * 32.0f;
158 v = powf( 166 v = std::pow(std::max(std::pow(v, 1.0f / m2) - c1, 0.0f) /
159 fmax(powf(v, 1.0f / m2) - c1, 0) / (c2 - c3 * powf(v, 1.0f / m2)), 167 (c2 - c3 * std::pow(v, 1.0f / m2)),
160 1.0f / m1); 168 1.0f / m1);
161 // This matches the scRGB definition that 1.0 means 80 nits. 169 // This matches the scRGB definition that 1.0 means 80 nits.
162 // TODO(hubbe): It would be *nice* if 1.0 meant more than that, but 170 // TODO(hubbe): It would be *nice* if 1.0 meant more than that, but
163 // that might be difficult to do right now. 171 // that might be difficult to do right now.
164 v *= 10000.0f / 80.0f; 172 v *= 10000.0f / 80.0f;
165 return v; 173 return v;
166 } 174 }
167 175
168 case ColorSpace::TransferID::SMPTEST2084_NON_HDR: 176 case ColorSpace::TransferID::SMPTEST2084_NON_HDR:
169 v = fmax(0.0f, v); 177 v = std::max(0.0f, v);
170 return fmin(2.3f * pow(v, 2.8f), v / 5.0f + 0.8f); 178 return std::min(2.3f * pow(v, 2.8f), v / 5.0f + 0.8f);
171 179
172 // Spec: http://www.arib.or.jp/english/html/overview/doc/2-STD-B67v1_0.pdf 180 // Spec: http://www.arib.or.jp/english/html/overview/doc/2-STD-B67v1_0.pdf
173 case ColorSpace::TransferID::ARIB_STD_B67: { 181 case ColorSpace::TransferID::ARIB_STD_B67: {
174 v = fmax(0.0f, v); 182 v = std::max(0.0f, v);
175 const float a = 0.17883277f; 183 const float a = 0.17883277f;
176 const float b = 0.28466892f; 184 const float b = 0.28466892f;
177 const float c = 0.55991073f; 185 const float c = 0.55991073f;
178 float v_ = 0.0f; 186 float v_ = 0.0f;
179 if (v <= 0.5f) { 187 if (v <= 0.5f) {
180 v_ = (v * 2.0f) * (v * 2.0f); 188 v_ = (v * 2.0f) * (v * 2.0f);
181 } else { 189 } else {
182 v_ = exp((v - c) / a) + b; 190 v_ = std::exp((v - c) / a) + b;
183 } 191 }
184 return v_; 192 return v_;
185 } 193 }
186 194
187 default: 195 default:
188 // Handled by SkColorSpaceTransferFn. 196 // Handled by SkColorSpaceTransferFn.
189 break; 197 break;
190 } 198 }
191 NOTREACHED(); 199 NOTREACHED();
192 return 0; 200 return 0;
193 } 201 }
194 202
195 Transform GetTransferMatrix(const gfx::ColorSpace& color_space) { 203 Transform GetTransferMatrix(const gfx::ColorSpace& color_space) {
196 SkMatrix44 transfer_matrix; 204 SkMatrix44 transfer_matrix;
197 color_space.GetTransferMatrix(&transfer_matrix); 205 color_space.GetTransferMatrix(&transfer_matrix);
198 return Transform(transfer_matrix); 206 return Transform(transfer_matrix);
199 } 207 }
200 208
201 Transform GetRangeAdjustMatrix(const gfx::ColorSpace& color_space) { 209 Transform GetRangeAdjustMatrix(const gfx::ColorSpace& color_space) {
202 SkMatrix44 range_adjust_matrix; 210 SkMatrix44 range_adjust_matrix;
203 color_space.GetRangeAdjustMatrix(&range_adjust_matrix); 211 color_space.GetRangeAdjustMatrix(&range_adjust_matrix);
204 return Transform(range_adjust_matrix); 212 return Transform(range_adjust_matrix);
205 } 213 }
206 214
215 Transform GetPrimaryTransform(const gfx::ColorSpace& color_space) {
216 SkMatrix44 primary_matrix;
217 color_space.GetPrimaryMatrix(&primary_matrix);
218 return Transform(primary_matrix);
219 }
220
221 } // namespace
222
207 class ColorTransformMatrix; 223 class ColorTransformMatrix;
208 class ColorTransformToLinear;
209 class ColorTransformFromLinear; 224 class ColorTransformFromLinear;
210 class ColorTransformToBT2020CL; 225 class ColorTransformToBT2020CL;
211 class ColorTransformFromBT2020CL; 226 class ColorTransformFromBT2020CL;
212 class ColorTransformNull; 227 class ColorTransformNull;
228 class QCMSColorTransform;
213 229
214 class ColorTransformInternal : public ColorTransform { 230 class ColorTransform::Step {
215 public: 231 public:
216 // Visitor pattern, Prepend() calls return prev->Join(this). 232 virtual ~Step() {}
217 virtual bool Prepend(ColorTransformInternal* prev) = 0; 233 virtual ColorTransformFromLinear* GetFromLinear() { return nullptr; }
234 virtual ColorTransformToBT2020CL* GetToBT2020CL() { return nullptr; }
235 virtual ColorTransformFromBT2020CL* GetFromBT2020CL() { return nullptr; }
236 virtual ColorTransformMatrix* GetMatrix() { return nullptr; }
237 virtual ColorTransformNull* GetNull() { return nullptr; }
238 virtual QCMSColorTransform* GetQCMS() { return nullptr; }
218 239
219 // Join methods, returns true if the |next| transform was successfully 240 // Join methods, returns true if the |next| transform was successfully
220 // assimilated into |this|. 241 // assimilated into |this|.
221 // If Join() returns true, |next| is no longer needed and can be deleted. 242 // If Join() returns true, |next| is no longer needed and can be deleted.
222 virtual bool Join(const ColorTransformToLinear& next) { return false; } 243 virtual bool Join(Step* next) { return false; }
223 virtual bool Join(const ColorTransformFromLinear& next) { return false; }
224 virtual bool Join(const ColorTransformToBT2020CL& next) { return false; }
225 virtual bool Join(const ColorTransformFromBT2020CL& next) { return false; }
226 virtual bool Join(const ColorTransformMatrix& next) { return false; }
227 virtual bool Join(const ColorTransformNull& next) { return true; }
228 244
229 // Return true if this is a null transform. 245 // Return true if this is a null transform.
230 virtual bool IsNull() { return false; } 246 virtual bool IsNull() { return false; }
247
248 virtual void transform(ColorTransform::TriStim* color, size_t num) = 0;
231 }; 249 };
232 250
233 class ColorTransformNull : public ColorTransformInternal { 251 class ColorTransformNull : public ColorTransform::Step {
234 public: 252 public:
235 bool Prepend(ColorTransformInternal* prev) override { 253 ColorTransformNull* GetNull() override { return this; }
236 return prev->Join(*this);
237 }
238 bool IsNull() override { return true; } 254 bool IsNull() override { return true; }
239 void transform(ColorTransform::TriStim* color, size_t num) override {} 255 void transform(ColorTransform::TriStim* color, size_t num) override {}
240 }; 256 };
241 257
242 class ColorTransformMatrix : public ColorTransformInternal { 258 class ColorTransformMatrix : public ColorTransform::Step {
243 public: 259 public:
244 explicit ColorTransformMatrix(const Transform& matrix) : matrix_(matrix) {} 260 explicit ColorTransformMatrix(const Transform& matrix) : matrix_(matrix) {}
245 261 ColorTransformMatrix* GetMatrix() override { return this; }
246 bool Prepend(ColorTransformInternal* prev) override { 262 bool Join(ColorTransform::Step* next_untyped) override {
247 return prev->Join(*this); 263 ColorTransformMatrix* next = next_untyped->GetMatrix();
248 } 264 if (!next)
249 265 return false;
250 bool Join(const ColorTransformMatrix& next) override { 266 Transform tmp = next->matrix_;
251 Transform tmp = next.matrix_;
252 tmp *= matrix_; 267 tmp *= matrix_;
253 matrix_ = tmp; 268 matrix_ = tmp;
254 return true; 269 return true;
255 } 270 }
256 271
257 bool IsNull() override { 272 bool IsNull() override {
258 // Returns true if we're very close to an identity matrix. 273 return SkMatrixIsApproximatelyIdentity(matrix_.matrix());
259 for (int i = 0; i < 4; i++) {
260 for (int j = 0; j < 4; j++) {
261 float expected = i == j ? 1.0f : 0.0f;
262 if (fabs(matrix_.matrix().get(i, j) - expected) > 0.00001f) {
263 return false;
264 }
265 }
266 }
267 return true;
268 } 274 }
269 275
270 void transform(ColorTransform::TriStim* colors, size_t num) override { 276 void transform(ColorTransform::TriStim* colors, size_t num) override {
271 for (size_t i = 0; i < num; i++) 277 for (size_t i = 0; i < num; i++)
272 matrix_.TransformPoint(colors + i); 278 matrix_.TransformPoint(colors + i);
273 } 279 }
274 280
275 private: 281 private:
276 Transform matrix_; 282 Transform matrix_;
277 }; 283 };
278 284
279 class ColorTransformFromLinear : public ColorTransformInternal { 285 class ColorTransformFromLinear : public ColorTransform::Step {
280 public: 286 public:
281 explicit ColorTransformFromLinear(ColorSpace::TransferID transfer, 287 explicit ColorTransformFromLinear(ColorSpace::TransferID transfer,
282 const SkColorSpaceTransferFn& fn, 288 const SkColorSpaceTransferFn& fn,
283 bool fn_valid) 289 bool fn_valid)
284 : transfer_(transfer), fn_(fn), fn_valid_(fn_valid) { 290 : transfer_(transfer), fn_(fn), fn_valid_(fn_valid) {
285 if (transfer_ == ColorSpace::TransferID::LINEAR_HDR) 291 if (transfer_ == ColorSpace::TransferID::LINEAR_HDR)
286 transfer_ = ColorSpace::TransferID::LINEAR; 292 transfer_ = ColorSpace::TransferID::LINEAR;
287 } 293 }
288 bool Prepend(ColorTransformInternal* prev) override { 294 ColorTransformFromLinear* GetFromLinear() override { return this; }
289 return prev->Join(*this);
290 }
291
292 bool IsNull() override { return transfer_ == ColorSpace::TransferID::LINEAR; } 295 bool IsNull() override { return transfer_ == ColorSpace::TransferID::LINEAR; }
293
294 void transform(ColorTransform::TriStim* colors, size_t num) override { 296 void transform(ColorTransform::TriStim* colors, size_t num) override {
295 if (fn_valid_) { 297 if (fn_valid_) {
296 for (size_t i = 0; i < num; i++) { 298 for (size_t i = 0; i < num; i++) {
297 colors[i].set_x(EvalSkTransferFn(fn_, colors[i].x())); 299 colors[i].set_x(EvalSkTransferFn(fn_, colors[i].x()));
298 colors[i].set_y(EvalSkTransferFn(fn_, colors[i].y())); 300 colors[i].set_y(EvalSkTransferFn(fn_, colors[i].y()));
299 colors[i].set_z(EvalSkTransferFn(fn_, colors[i].z())); 301 colors[i].set_z(EvalSkTransferFn(fn_, colors[i].z()));
300 } 302 }
301 } else { 303 } else {
302 for (size_t i = 0; i < num; i++) { 304 for (size_t i = 0; i < num; i++) {
303 colors[i].set_x(FromLinear(transfer_, colors[i].x())); 305 colors[i].set_x(FromLinear(transfer_, colors[i].x()));
304 colors[i].set_y(FromLinear(transfer_, colors[i].y())); 306 colors[i].set_y(FromLinear(transfer_, colors[i].y()));
305 colors[i].set_z(FromLinear(transfer_, colors[i].z())); 307 colors[i].set_z(FromLinear(transfer_, colors[i].z()));
306 } 308 }
307 } 309 }
308 } 310 }
309 311
310 private: 312 private:
311 friend class ColorTransformToLinear; 313 friend class ColorTransformToLinear;
312 ColorSpace::TransferID transfer_; 314 ColorSpace::TransferID transfer_;
313 SkColorSpaceTransferFn fn_; 315 SkColorSpaceTransferFn fn_;
314 bool fn_valid_ = false; 316 bool fn_valid_ = false;
315 }; 317 };
316 318
317 class ColorTransformToLinear : public ColorTransformInternal { 319 class ColorTransformToLinear : public ColorTransform::Step {
318 public: 320 public:
319 explicit ColorTransformToLinear(ColorSpace::TransferID transfer, 321 explicit ColorTransformToLinear(ColorSpace::TransferID transfer,
320 const SkColorSpaceTransferFn& fn, 322 const SkColorSpaceTransferFn& fn,
321 bool fn_valid) 323 bool fn_valid)
322 : transfer_(transfer), fn_(fn), fn_valid_(fn_valid) { 324 : transfer_(transfer), fn_(fn), fn_valid_(fn_valid) {
323 if (transfer_ == ColorSpace::TransferID::LINEAR_HDR) 325 if (transfer_ == ColorSpace::TransferID::LINEAR_HDR)
324 transfer_ = ColorSpace::TransferID::LINEAR; 326 transfer_ = ColorSpace::TransferID::LINEAR;
325 } 327 }
326 328
327 bool Prepend(ColorTransformInternal* prev) override {
328 return prev->Join(*this);
329 }
330
331 static bool IsGamma22(ColorSpace::TransferID transfer) { 329 static bool IsGamma22(ColorSpace::TransferID transfer) {
332 switch (transfer) { 330 switch (transfer) {
333 // We don't need to check BT709 here because it's been translated into 331 // We don't need to check BT709 here because it's been translated into
334 // SRGB in ColorSpaceToColorSpaceTransform::ColorSpaceToLinear below. 332 // SRGB in ColorSpaceToColorSpaceTransform::ColorSpaceToLinear below.
335 case ColorSpace::TransferID::GAMMA22: 333 case ColorSpace::TransferID::GAMMA22:
336 case ColorSpace::TransferID::IEC61966_2_1: // SRGB 334 case ColorSpace::TransferID::IEC61966_2_1: // SRGB
337 return true; 335 return true;
338 336
339 default: 337 default:
340 return false; 338 return false;
341 } 339 }
342 } 340 }
343 341
344 bool Join(const ColorTransformFromLinear& next) override { 342 bool Join(ColorTransform::Step* next_untyped) override {
345 if (transfer_ == next.transfer_ || 343 ColorTransformFromLinear* next = next_untyped->GetFromLinear();
346 (IsGamma22(transfer_) && IsGamma22(next.transfer_))) { 344 if (!next)
345 return false;
346 // TODO(ccameron): Use SkTransferFnsApproximatelyCancel and
347 // SkTransferFnIsApproximatelyIdentity to merge parametric transfer
348 // functions.
349 if (transfer_ == next->transfer_ ||
350 (IsGamma22(transfer_) && IsGamma22(next->transfer_))) {
347 transfer_ = ColorSpace::TransferID::LINEAR; 351 transfer_ = ColorSpace::TransferID::LINEAR;
348 return true; 352 return true;
349 } 353 }
350 return false; 354 return false;
351 } 355 }
352 356
353 bool IsNull() override { return transfer_ == ColorSpace::TransferID::LINEAR; } 357 bool IsNull() override { return transfer_ == ColorSpace::TransferID::LINEAR; }
354 358
355 // Assumes BT2020 primaries. 359 // Assumes BT2020 primaries.
356 static float Luma(const ColorTransform::TriStim& c) { 360 static float Luma(const ColorTransform::TriStim& c) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 // on the RGB values. However, running the transfer function 417 // on the RGB values. However, running the transfer function
414 // on the U and V values doesn't make any sense since they 418 // on the U and V values doesn't make any sense since they
415 // are centered at 0.5. To work around this, the transfer function 419 // are centered at 0.5. To work around this, the transfer function
416 // is applied to the Y, R and B values, and then the U and V 420 // is applied to the Y, R and B values, and then the U and V
417 // values are calculated from that. 421 // values are calculated from that.
418 // In our implementation, the YUV->RGB matrix is used to 422 // In our implementation, the YUV->RGB matrix is used to
419 // convert YUV to RYB (the G value is replaced with an Y value.) 423 // convert YUV to RYB (the G value is replaced with an Y value.)
420 // Then we run the transfer function like normal, and finally 424 // Then we run the transfer function like normal, and finally
421 // this class is inserted as an extra step which takes calculates 425 // this class is inserted as an extra step which takes calculates
422 // the U and V values. 426 // the U and V values.
423 class ColorTransformToBT2020CL : public ColorTransformInternal { 427 class ColorTransformToBT2020CL : public ColorTransform::Step {
424 public: 428 public:
425 bool Prepend(ColorTransformInternal* prev) override { 429 bool Join(ColorTransform::Step* next_untyped) override {
426 return prev->Join(*this); 430 ColorTransformFromBT2020CL* next = next_untyped->GetFromBT2020CL();
427 } 431 if (!next)
428 432 return false;
429 bool Join(const ColorTransformFromBT2020CL& next) override {
430 if (null_) 433 if (null_)
431 return false; 434 return false;
432 null_ = true; 435 null_ = true;
433 return true; 436 return true;
434 } 437 }
435 438
436 bool IsNull() override { return null_; } 439 bool IsNull() override { return null_; }
437 440
438 void transform(ColorTransform::TriStim* RYB, size_t num) override { 441 void transform(ColorTransform::TriStim* RYB, size_t num) override {
439 for (size_t i = 0; i < num; i++) { 442 for (size_t i = 0; i < num; i++) {
(...skipping 12 matching lines...) Expand all
452 } 455 }
453 RYB[i] = ColorTransform::TriStim(RYB[i].y(), U, V); 456 RYB[i] = ColorTransform::TriStim(RYB[i].y(), U, V);
454 } 457 }
455 } 458 }
456 459
457 private: 460 private:
458 bool null_ = false; 461 bool null_ = false;
459 }; 462 };
460 463
461 // Inverse of ColorTransformToBT2020CL, see comment above for more info. 464 // Inverse of ColorTransformToBT2020CL, see comment above for more info.
462 class ColorTransformFromBT2020CL : public ColorTransformInternal { 465 class ColorTransformFromBT2020CL : public ColorTransform::Step {
463 public: 466 public:
464 bool Prepend(ColorTransformInternal* prev) override { 467 bool Join(ColorTransform::Step* next_untyped) override {
465 return prev->Join(*this); 468 ColorTransformToBT2020CL* next = next_untyped->GetToBT2020CL();
466 } 469 if (!next)
467 470 return false;
468 bool Join(const ColorTransformToBT2020CL& next) override {
469 if (null_) 471 if (null_)
470 return false; 472 return false;
471 null_ = true; 473 null_ = true;
472 return true; 474 return true;
473 } 475 }
474 476
475 bool IsNull() override { return null_; } 477 bool IsNull() override { return null_; }
476 478
477 void transform(ColorTransform::TriStim* YUV, size_t num) override { 479 void transform(ColorTransform::TriStim* YUV, size_t num) override {
478 if (null_) 480 if (null_)
(...skipping 15 matching lines...) Expand all
494 } 496 }
495 // Return an RYB value, later steps will fix it. 497 // Return an RYB value, later steps will fix it.
496 YUV[i] = ColorTransform::TriStim(R_Y + Y, YUV[i].x(), B_Y + Y); 498 YUV[i] = ColorTransform::TriStim(R_Y + Y, YUV[i].x(), B_Y + Y);
497 } 499 }
498 } 500 }
499 501
500 private: 502 private:
501 bool null_ = false; 503 bool null_ = false;
502 }; 504 };
503 505
504 class ChainColorTransform : public ColorTransform { 506 // static
505 public: 507 void ColorTransform::Append(ColorSpace from,
506 ChainColorTransform(std::unique_ptr<ColorTransform> a, 508 const ColorSpace& to,
507 std::unique_ptr<ColorTransform> b) 509 ColorTransform::Intent intent,
508 : a_(std::move(a)), b_(std::move(b)) {} 510 StepList* builder) {
511 if (intent == ColorTransform::Intent::INTENT_PERCEPTUAL) {
512 switch (from.transfer_) {
513 case ColorSpace::TransferID::UNSPECIFIED:
514 case ColorSpace::TransferID::BT709:
515 case ColorSpace::TransferID::SMPTE170M:
516 // SMPTE 1886 suggests that we should be using gamma 2.4 for BT709
517 // content. However, most displays actually use a gamma of 2.2, and
518 // user studies shows that users don't really care. Using the same
519 // gamma as the display will let us optimize a lot more, so lets stick
520 // with using the SRGB transfer function.
521 from.transfer_ = ColorSpace::TransferID::IEC61966_2_1;
522 break;
509 523
510 private: 524 case ColorSpace::TransferID::SMPTEST2084:
511 void transform(TriStim* colors, size_t num) override { 525 if (!to.IsHDR()) {
512 a_->transform(colors, num); 526 // We don't have an HDR display, so replace SMPTE 2084 with
513 b_->transform(colors, num); 527 // something that returns ranges more or less suitable for a normal
514 } 528 // display.
515 std::unique_ptr<ColorTransform> a_; 529 from.transfer_ = ColorSpace::TransferID::SMPTEST2084_NON_HDR;
516 std::unique_ptr<ColorTransform> b_; 530 }
517 }; 531 break;
518 532
519 class TransformBuilder { 533 case ColorSpace::TransferID::ARIB_STD_B67:
520 public: 534 if (!to.IsHDR()) {
521 void Append(std::unique_ptr<ColorTransformInternal> transform) { 535 // Interpreting HLG using a gamma 2.4 works reasonably well for SDR
522 if (!disable_optimizations_ && transform->IsNull()) 536 // displays.
523 return; // Null transform 537 from.transfer_ = ColorSpace::TransferID::GAMMA24;
524 transforms_.push_back(std::move(transform)); 538 }
525 if (disable_optimizations_)
526 return;
527 while (transforms_.size() >= 2 &&
528 transforms_.back()->Prepend(
529 transforms_[transforms_.size() - 2].get())) {
530 transforms_.pop_back();
531 if (transforms_.back()->IsNull()) {
532 transforms_.pop_back();
533 break; 539 break;
534 } 540
541 default: // Do nothing
542 break;
535 } 543 }
544
545 // TODO(hubbe): shrink gamuts here (never stretch gamuts)
536 } 546 }
537 547
538 std::unique_ptr<ColorTransform> GetTransform() { 548 builder->push_back(
539 if (transforms_.empty()) 549 base::MakeUnique<ColorTransformMatrix>(GetRangeAdjustMatrix(from)));
540 return base::MakeUnique<ColorTransformNull>();
541 std::unique_ptr<ColorTransform> ret(std::move(transforms_.back()));
542 transforms_.pop_back();
543 550
544 while (!transforms_.empty()) { 551 builder->push_back(
545 ret = std::unique_ptr<ColorTransform>(new ChainColorTransform( 552 base::MakeUnique<ColorTransformMatrix>(Invert(GetTransferMatrix(from))));
546 std::move(transforms_.back()), std::move(ret)));
547 transforms_.pop_back();
548 }
549 553
550 return ret; 554 SkColorSpaceTransferFn to_linear_fn;
555 bool to_linear_fn_valid = from.GetTransferFunction(&to_linear_fn);
556 builder->push_back(base::MakeUnique<ColorTransformToLinear>(
557 from.transfer_, to_linear_fn, to_linear_fn_valid));
558
559 if (from.matrix_ == ColorSpace::MatrixID::BT2020_CL) {
560 // BT2020 CL is a special case.
561 builder->push_back(base::MakeUnique<ColorTransformFromBT2020CL>());
562 }
563 builder->push_back(
564 base::MakeUnique<ColorTransformMatrix>(GetPrimaryTransform(from)));
565
566 builder->push_back(
567 base::MakeUnique<ColorTransformMatrix>(Invert(GetPrimaryTransform(to))));
568 if (to.matrix_ == ColorSpace::MatrixID::BT2020_CL) {
569 // BT2020 CL is a special case.
570 builder->push_back(base::MakeUnique<ColorTransformToBT2020CL>());
551 } 571 }
552 572
553 void disable_optimizations() { disable_optimizations_ = true; } 573 SkColorSpaceTransferFn from_linear_fn;
574 bool from_linear_fn_valid = to.GetInverseTransferFunction(&from_linear_fn);
575 builder->push_back(base::MakeUnique<ColorTransformFromLinear>(
576 to.transfer_, from_linear_fn, from_linear_fn_valid));
554 577
555 private: 578 builder->push_back(
556 bool disable_optimizations_ = false; 579 base::MakeUnique<ColorTransformMatrix>(GetTransferMatrix(to)));
557 std::vector<std::unique_ptr<ColorTransformInternal>> transforms_;
558 };
559 580
560 class ColorSpaceToColorSpaceTransform { 581 builder->push_back(
561 public: 582 base::MakeUnique<ColorTransformMatrix>(Invert(GetRangeAdjustMatrix(to))));
562 static Transform GetPrimaryTransform(const ColorSpace& c) { 583 }
563 SkMatrix44 sk_matrix;
564 c.GetPrimaryMatrix(&sk_matrix);
565 return Transform(sk_matrix);
566 }
567 584
568 static void ColorSpaceToColorSpace(ColorSpace from, 585 class QCMSColorTransform : public ColorTransform::Step {
569 ColorSpace to,
570 ColorTransform::Intent intent,
571 TransformBuilder* builder) {
572 if (intent == ColorTransform::Intent::INTENT_PERCEPTUAL) {
573 switch (from.transfer_) {
574 case ColorSpace::TransferID::UNSPECIFIED:
575 case ColorSpace::TransferID::BT709:
576 case ColorSpace::TransferID::SMPTE170M:
577 // SMPTE 1886 suggests that we should be using gamma 2.4 for BT709
578 // content. However, most displays actually use a gamma of 2.2, and
579 // user studies shows that users don't really care. Using the same
580 // gamma as the display will let us optimize a lot more, so lets stick
581 // with using the SRGB transfer function.
582 from.transfer_ = ColorSpace::TransferID::IEC61966_2_1;
583 break;
584
585 case ColorSpace::TransferID::SMPTEST2084:
586 if (!to.IsHDR()) {
587 // We don't have an HDR display, so replace SMPTE 2084 with
588 // something that returns ranges more or less suitable for a normal
589 // display.
590 from.transfer_ = ColorSpace::TransferID::SMPTEST2084_NON_HDR;
591 }
592 break;
593
594 case ColorSpace::TransferID::ARIB_STD_B67:
595 if (!to.IsHDR()) {
596 // Interpreting HLG using a gamma 2.4 works reasonably well for SDR
597 // displays.
598 from.transfer_ = ColorSpace::TransferID::GAMMA24;
599 }
600 break;
601
602 default: // Do nothing
603 break;
604 }
605
606 // TODO(hubbe): shrink gamuts here (never stretch gamuts)
607 }
608
609 builder->Append(base::MakeUnique<ColorTransformMatrix>(
610 GetRangeAdjustMatrix(from)));
611
612 builder->Append(base::MakeUnique<ColorTransformMatrix>(
613 Invert(GetTransferMatrix(from))));
614
615 SkColorSpaceTransferFn to_linear_fn;
616 bool to_linear_fn_valid = from.GetTransferFunction(&to_linear_fn);
617 builder->Append(base::MakeUnique<ColorTransformToLinear>(
618 from.transfer_, to_linear_fn, to_linear_fn_valid));
619
620 if (from.matrix_ == ColorSpace::MatrixID::BT2020_CL) {
621 // BT2020 CL is a special case.
622 builder->Append(base::MakeUnique<ColorTransformFromBT2020CL>());
623 }
624 builder->Append(
625 base::MakeUnique<ColorTransformMatrix>(GetPrimaryTransform(from)));
626
627 builder->Append(base::MakeUnique<ColorTransformMatrix>(
628 Invert(GetPrimaryTransform(to))));
629 if (to.matrix_ == ColorSpace::MatrixID::BT2020_CL) {
630 // BT2020 CL is a special case.
631 builder->Append(base::MakeUnique<ColorTransformToBT2020CL>());
632 }
633
634 SkColorSpaceTransferFn from_linear_fn;
635 bool from_linear_fn_valid = to.GetInverseTransferFunction(&from_linear_fn);
636 builder->Append(base::MakeUnique<ColorTransformFromLinear>(
637 to.transfer_, from_linear_fn, from_linear_fn_valid));
638
639 builder->Append(
640 base::MakeUnique<ColorTransformMatrix>(GetTransferMatrix(to)));
641
642 builder->Append(base::MakeUnique<ColorTransformMatrix>(
643 Invert(GetRangeAdjustMatrix(to))));
644 }
645 };
646
647 class QCMSColorTransform : public ColorTransformInternal {
648 public: 586 public:
649 // Takes ownership of the profiles 587 // Takes ownership of the profiles
650 QCMSColorTransform(qcms_profile* from, qcms_profile* to) 588 QCMSColorTransform(ScopedQcmsProfile from, ScopedQcmsProfile to)
651 : from_(from), to_(to) {} 589 : from_(std::move(from)), to_(std::move(to)) {}
652 ~QCMSColorTransform() override { 590 ~QCMSColorTransform() override {}
653 qcms_profile_release(from_); 591 QCMSColorTransform* GetQCMS() override { return this; }
654 qcms_profile_release(to_); 592 bool Join(ColorTransform::Step* next_untyped) override {
655 } 593 QCMSColorTransform* next = next_untyped->GetQCMS();
656 bool Prepend(ColorTransformInternal* prev) override { 594 if (!next)
657 // Not currently optimizable. 595 return false;
596 if (qcms_profile_match(to_.get(), next->from_.get())) {
597 to_ = std::move(next->to_);
598 return true;
599 }
658 return false; 600 return false;
659 } 601 }
660 bool IsNull() override { return from_ == to_; } 602 bool IsNull() override {
661 void transform(TriStim* colors, size_t num) override { 603 if (qcms_profile_match(from_.get(), to_.get()))
662 CHECK(sizeof(TriStim) == sizeof(float[3])); 604 return true;
605 return false;
606 }
607 void transform(ColorTransform::TriStim* colors, size_t num) override {
608 CHECK(sizeof(ColorTransform::TriStim) == sizeof(float[3]));
663 // QCMS doesn't like numbers outside 0..1 609 // QCMS doesn't like numbers outside 0..1
664 for (size_t i = 0; i < num; i++) { 610 for (size_t i = 0; i < num; i++) {
665 colors[i].set_x(fmin(1.0f, fmax(0.0f, colors[i].x()))); 611 colors[i].set_x(std::min(1.0f, std::max(0.0f, colors[i].x())));
666 colors[i].set_y(fmin(1.0f, fmax(0.0f, colors[i].y()))); 612 colors[i].set_y(std::min(1.0f, std::max(0.0f, colors[i].y())));
667 colors[i].set_z(fmin(1.0f, fmax(0.0f, colors[i].z()))); 613 colors[i].set_z(std::min(1.0f, std::max(0.0f, colors[i].z())));
668 } 614 }
669 qcms_chain_transform(from_, to_, reinterpret_cast<float*>(colors), 615 qcms_chain_transform(from_.get(), to_.get(),
616 reinterpret_cast<float*>(colors),
670 reinterpret_cast<float*>(colors), num * 3); 617 reinterpret_cast<float*>(colors), num * 3);
671 } 618 }
672 619
673 private: 620 private:
674 qcms_profile *from_, *to_; 621 ScopedQcmsProfile from_;
622 ScopedQcmsProfile to_;
675 }; 623 };
676 624
677 qcms_profile* GetQCMSProfileIfAvailable(const ColorSpace& color_space) { 625 ScopedQcmsProfile GetQCMSProfileIfAvailable(const ColorSpace& color_space) {
678 ICCProfile icc_profile = ICCProfile::FromColorSpace(color_space); 626 ICCProfile icc_profile = ICCProfile::FromColorSpace(color_space);
679 if (icc_profile.GetData().empty()) 627 if (icc_profile.GetData().empty())
680 return nullptr; 628 return nullptr;
681 return qcms_profile_from_memory(icc_profile.GetData().data(), 629 return ScopedQcmsProfile(qcms_profile_from_memory(
682 icc_profile.GetData().size()); 630 icc_profile.GetData().data(), icc_profile.GetData().size()));
683 } 631 }
684 632
685 qcms_profile* GetXYZD50Profile() { 633 ScopedQcmsProfile GetXYZD50Profile() {
686 // QCMS is trixy, it has a datatype called qcms_CIE_xyY, but what it expects 634 // QCMS is trixy, it has a datatype called qcms_CIE_xyY, but what it expects
687 // is in fact not xyY color coordinates, it just wants the x/y values of the 635 // is in fact not xyY color coordinates, it just wants the x/y values of the
688 // primaries with Y equal to 1.0. 636 // primaries with Y equal to 1.0.
689 qcms_CIE_xyYTRIPLE xyz; 637 qcms_CIE_xyYTRIPLE xyz;
690 qcms_CIE_xyY w; 638 qcms_CIE_xyY w;
691 xyz.red.x = 1.0f; 639 xyz.red.x = 1.0f;
692 xyz.red.y = 0.0f; 640 xyz.red.y = 0.0f;
693 xyz.red.Y = 1.0f; 641 xyz.red.Y = 1.0f;
694 xyz.green.x = 0.0f; 642 xyz.green.x = 0.0f;
695 xyz.green.y = 1.0f; 643 xyz.green.y = 1.0f;
696 xyz.green.Y = 1.0f; 644 xyz.green.Y = 1.0f;
697 xyz.blue.x = 0.0f; 645 xyz.blue.x = 0.0f;
698 xyz.blue.y = 0.0f; 646 xyz.blue.y = 0.0f;
699 xyz.blue.Y = 1.0f; 647 xyz.blue.Y = 1.0f;
700 w.x = 0.34567f; 648 w.x = 0.34567f;
701 w.y = 0.35850f; 649 w.y = 0.35850f;
702 w.Y = 1.0f; 650 w.Y = 1.0f;
703 return qcms_profile_create_rgb_with_gamma(w, xyz, 1.0f); 651 return ScopedQcmsProfile(qcms_profile_create_rgb_with_gamma(w, xyz, 1.0f));
704 } 652 }
705 653
706 std::unique_ptr<ColorTransform> ColorTransform::NewColorTransform( 654 std::unique_ptr<ColorTransform> ColorTransform::NewColorTransform(
707 const ColorSpace& from, 655 const ColorSpace& from,
708 const ColorSpace& to, 656 const ColorSpace& to,
709 Intent intent) { 657 Intent intent) {
710 TransformBuilder builder; 658 std::list<std::unique_ptr<Step>> builder;
711 if (intent == Intent::TEST_NO_OPT) { 659
712 builder.disable_optimizations(); 660 ScopedQcmsProfile from_profile = GetQCMSProfileIfAvailable(from);
661 ScopedQcmsProfile to_profile = GetQCMSProfileIfAvailable(to);
662 bool has_from_profile = !!from_profile;
663 bool has_to_profile = !!to_profile;
664
665 if (from_profile) {
666 builder.push_back(base::MakeUnique<QCMSColorTransform>(
667 std::move(from_profile), GetXYZD50Profile()));
713 } 668 }
714 669
715 qcms_profile* from_profile = GetQCMSProfileIfAvailable(from); 670 Append(has_from_profile ? ColorSpace::CreateXYZD50() : from,
716 qcms_profile* to_profile = GetQCMSProfileIfAvailable(to); 671 has_to_profile ? ColorSpace::CreateXYZD50() : to, intent, &builder);
717 672
718 if (from_profile && to_profile) {
719 return std::unique_ptr<ColorTransform>(
720 new QCMSColorTransform(from_profile, to_profile));
721 }
722 if (from_profile) {
723 builder.Append(std::unique_ptr<ColorTransformInternal>(
724 new QCMSColorTransform(from_profile, GetXYZD50Profile())));
725 }
726 ColorSpaceToColorSpaceTransform::ColorSpaceToColorSpace(
727 from_profile ? ColorSpace::CreateXYZD50() : from,
728 to_profile ? ColorSpace::CreateXYZD50() : to, intent, &builder);
729 if (to_profile) { 673 if (to_profile) {
730 builder.Append(std::unique_ptr<ColorTransformInternal>( 674 builder.push_back(base::MakeUnique<QCMSColorTransform>(
731 new QCMSColorTransform(GetXYZD50Profile(), to_profile))); 675 GetXYZD50Profile(), std::move(to_profile)));
732 } 676 }
733 677
734 return builder.GetTransform(); 678 if (intent != Intent::TEST_NO_OPT)
679 Simplify(&builder);
680
681 return std::unique_ptr<ColorTransform>(
682 new ColorTransform(std::move(builder)));
735 } 683 }
736 684
737 // static 685 // static
738 float ColorTransform::ToLinearForTesting(ColorSpace::TransferID transfer, 686 void ColorTransform::Simplify(StepList* steps) {
739 float v) { 687 for (auto iter = steps->begin(); iter != steps->end();) {
740 ColorSpace space(ColorSpace::PrimaryID::BT709, transfer, 688 std::unique_ptr<Step>& this_step = *iter;
741 ColorSpace::MatrixID::RGB, ColorSpace::RangeID::FULL); 689
742 SkColorSpaceTransferFn to_linear_fn; 690 // Try to Join |next_step| into |this_step|. If successful, re-visit the
743 bool to_linear_fn_valid = space.GetTransferFunction(&to_linear_fn); 691 // step before |this_step|.
744 ColorTransformToLinear to_linear_transform(transfer, to_linear_fn, 692 auto iter_next = iter;
745 to_linear_fn_valid); 693 iter_next++;
746 TriStim color(v, v, v); 694 if (iter_next != steps->end()) {
747 to_linear_transform.transform(&color, 1); 695 std::unique_ptr<Step>& next_step = *iter_next;
748 return color.x(); 696 if (this_step->Join(next_step.get())) {
697 steps->erase(iter_next);
698 if (iter != steps->begin())
699 --iter;
700 continue;
701 }
702 }
703
704 // If |this_step| step is a no-op, remove it, and re-visit the step before
705 // |this_step|.
706 if (this_step->IsNull()) {
707 iter = steps->erase(iter);
708 if (iter != steps->begin())
709 --iter;
710 continue;
711 }
712
713 ++iter;
714 }
749 } 715 }
750 716
751 // static 717 void ColorTransform::transform(ColorTransform::TriStim* colors, size_t num) {
752 float ColorTransform::FromLinearForTesting(ColorSpace::TransferID transfer, 718 for (const auto& step : steps_)
753 float v) { 719 step->transform(colors, num);
754 ColorSpace space(ColorSpace::PrimaryID::BT709, transfer,
755 ColorSpace::MatrixID::RGB, ColorSpace::RangeID::FULL);
756 SkColorSpaceTransferFn from_linear_fn;
757 bool from_linear_fn_valid = space.GetInverseTransferFunction(&from_linear_fn);
758
759 ColorTransformFromLinear from_linear_transform(transfer, from_linear_fn,
760 from_linear_fn_valid);
761 TriStim color(v, v, v);
762 from_linear_transform.transform(&color, 1);
763 return color.x();
764 } 720 }
765 721
722 ColorTransform::ColorTransform(ColorTransform::StepList steps)
723 : steps_(std::move(steps)) {}
724
725 ColorTransform::~ColorTransform() {}
726
766 } // namespace gfx 727 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698