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

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

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

Powered by Google App Engine
This is Rietveld 408576698