OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/skia_color_space_util.h" | 5 #include "ui/gfx/skia_color_space_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 namespace gfx { | 10 namespace gfx { |
11 | 11 |
12 namespace { | 12 namespace { |
13 | |
14 const float kEpsilon = 1.f / 256.f; | |
15 } | 13 } |
16 | 14 |
17 float EvalSkTransferFn(const SkColorSpaceTransferFn& fn, float x) { | 15 float SkTransferFnEval(const SkColorSpaceTransferFn& fn, float x) { |
18 if (x < 0.f) | 16 if (x < 0.f) |
19 return 0.f; | 17 return 0.f; |
20 if (x < fn.fD) | 18 if (x < fn.fD) |
21 return fn.fC * x + fn.fF; | 19 return fn.fC * x + fn.fF; |
22 return std::pow(fn.fA * x + fn.fB, fn.fG) + fn.fE; | 20 return std::pow(fn.fA * x + fn.fB, fn.fG) + fn.fE; |
23 } | 21 } |
24 | 22 |
25 SkColorSpaceTransferFn SkTransferFnInverse(const SkColorSpaceTransferFn& fn) { | 23 SkColorSpaceTransferFn SkTransferFnInverse(const SkColorSpaceTransferFn& fn) { |
26 SkColorSpaceTransferFn fn_inv = {0}; | 24 SkColorSpaceTransferFn fn_inv = {0}; |
27 if (fn.fA > 0 && fn.fG > 0) { | 25 if (fn.fA > 0 && fn.fG > 0) { |
28 double a_to_the_g = std::pow(fn.fA, fn.fG); | 26 double a_to_the_g = std::pow(fn.fA, fn.fG); |
29 fn_inv.fA = 1.f / a_to_the_g; | 27 fn_inv.fA = 1.f / a_to_the_g; |
30 fn_inv.fB = -fn.fE / a_to_the_g; | 28 fn_inv.fB = -fn.fE / a_to_the_g; |
31 fn_inv.fG = 1.f / fn.fG; | 29 fn_inv.fG = 1.f / fn.fG; |
32 } | 30 } |
33 fn_inv.fD = fn.fC * fn.fD + fn.fF; | 31 fn_inv.fD = fn.fC * fn.fD + fn.fF; |
34 fn_inv.fE = -fn.fB / fn.fA; | 32 fn_inv.fE = -fn.fB / fn.fA; |
35 if (fn.fC != 0) { | 33 if (fn.fC != 0) { |
36 fn_inv.fC = 1.f / fn.fC; | 34 fn_inv.fC = 1.f / fn.fC; |
37 fn_inv.fF = -fn.fF / fn.fC; | 35 fn_inv.fF = -fn.fF / fn.fC; |
38 } | 36 } |
39 return fn_inv; | 37 return fn_inv; |
40 } | 38 } |
41 | 39 |
| 40 bool SkTransferFnsApproximatelyCancel(const SkColorSpaceTransferFn& a, |
| 41 const SkColorSpaceTransferFn& b) { |
| 42 const float kStep = 1.f / 8.f; |
| 43 const float kEpsilon = 2.5f / 256.f; |
| 44 for (float x = 0; x <= 1.f; x += kStep) { |
| 45 float a_of_x = SkTransferFnEval(a, x); |
| 46 float b_of_a_of_x = SkTransferFnEval(b, a_of_x); |
| 47 if (std::abs(b_of_a_of_x - x) > kEpsilon) |
| 48 return false; |
| 49 } |
| 50 return true; |
| 51 } |
| 52 |
| 53 bool SkTransferFnIsApproximatelyIdentity(const SkColorSpaceTransferFn& a) { |
| 54 const float kStep = 1.f / 8.f; |
| 55 const float kEpsilon = 2.5f / 256.f; |
| 56 for (float x = 0; x <= 1.f; x += kStep) { |
| 57 float a_of_x = SkTransferFnEval(a, x); |
| 58 if (std::abs(a_of_x - x) > kEpsilon) |
| 59 return false; |
| 60 } |
| 61 return true; |
| 62 } |
| 63 |
42 bool SkMatrixIsApproximatelyIdentity(const SkMatrix44& m) { | 64 bool SkMatrixIsApproximatelyIdentity(const SkMatrix44& m) { |
| 65 const float kEpsilon = 1.f / 256.f; |
43 for (int i = 0; i < 4; ++i) { | 66 for (int i = 0; i < 4; ++i) { |
44 for (int j = 0; j < 4; ++j) { | 67 for (int j = 0; j < 4; ++j) { |
45 float identity_value = i == j ? 1 : 0; | 68 float identity_value = i == j ? 1 : 0; |
46 float value = m.get(i, j); | 69 float value = m.get(i, j); |
47 if (std::abs(identity_value - value) > kEpsilon) | 70 if (std::abs(identity_value - value) > kEpsilon) |
48 return false; | 71 return false; |
49 } | 72 } |
50 } | 73 } |
51 return true; | 74 return true; |
52 } | 75 } |
53 | 76 |
54 } // namespace gfx | 77 } // namespace gfx |
OLD | NEW |