| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/gfx/skia_color_space_util.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <cmath> |
| 9 |
| 10 namespace gfx { |
| 11 |
| 12 namespace { |
| 13 |
| 14 const float kEpsilon = 1.f / 256.f; |
| 15 } |
| 16 |
| 17 float EvalSkTransferFn(const SkColorSpaceTransferFn& fn, float x) { |
| 18 if (x < 0.f) |
| 19 return 0.f; |
| 20 if (x < fn.fD) |
| 21 return fn.fC * x + fn.fF; |
| 22 return std::pow(fn.fA * x + fn.fB, fn.fG) + fn.fE; |
| 23 } |
| 24 |
| 25 SkColorSpaceTransferFn SkTransferFnInverse(const SkColorSpaceTransferFn& fn) { |
| 26 SkColorSpaceTransferFn fn_inv = {0}; |
| 27 if (fn.fA > 0 && fn.fG > 0) { |
| 28 double a_to_the_g = std::pow(fn.fA, fn.fG); |
| 29 fn_inv.fA = 1.f / a_to_the_g; |
| 30 fn_inv.fB = -fn.fE / a_to_the_g; |
| 31 fn_inv.fG = 1.f / fn.fG; |
| 32 } |
| 33 fn_inv.fD = fn.fC * fn.fD + fn.fF; |
| 34 fn_inv.fE = -fn.fB / fn.fA; |
| 35 if (fn.fC != 0) { |
| 36 fn_inv.fC = 1.f / fn.fC; |
| 37 fn_inv.fF = -fn.fF / fn.fC; |
| 38 } |
| 39 return fn_inv; |
| 40 } |
| 41 |
| 42 bool SkTransferFnIsApproximatelyIdentity(const SkColorSpaceTransferFn& fn) { |
| 43 const float kEpsilon = 1.f / 256.f; |
| 44 if (fn.fD > 0.f) { |
| 45 if (std::abs(fn.fC - 1.f) > kEpsilon) |
| 46 return false; |
| 47 if (std::abs(fn.fF) > kEpsilon) |
| 48 return false; |
| 49 } |
| 50 if (std::abs(fn.fA - 1.f) > kEpsilon) |
| 51 return false; |
| 52 if (std::abs(fn.fB) > kEpsilon) |
| 53 return false; |
| 54 if (std::abs(fn.fE) > kEpsilon) |
| 55 return false; |
| 56 if (std::abs(fn.fG - 1.f) > kEpsilon) |
| 57 return false; |
| 58 return true; |
| 59 } |
| 60 |
| 61 bool SkTransferFnsApproximatelyCancel(const SkColorSpaceTransferFn& a, |
| 62 const SkColorSpaceTransferFn& b) { |
| 63 const float kStep = 1.f / 256.f; |
| 64 SkColorSpaceTransferFn b_inv = SkTransferFnInverse(b); |
| 65 |
| 66 // Evaluate the linear sections explicitly. |
| 67 float linear_segment_max = std::max(a.fD, b.fD); |
| 68 for (float x = 0.f; x < linear_segment_max; x += kStep) { |
| 69 float a_of_x = EvalSkTransferFn(a, x); |
| 70 float b_inv_of_x = EvalSkTransferFn(b, a_of_x); |
| 71 if (std::abs(a_of_x - b_inv_of_x) > kEpsilon) |
| 72 return false; |
| 73 } |
| 74 |
| 75 // Compare the coefficients of the pow sections. |
| 76 if (std::abs(a.fA - b_inv.fA) > kEpsilon) |
| 77 return false; |
| 78 if (std::abs(a.fB - b_inv.fB) > kEpsilon) |
| 79 return false; |
| 80 if (std::abs(a.fE - b_inv.fE) > kEpsilon) |
| 81 return false; |
| 82 if (std::abs(a.fG - b_inv.fG) > kEpsilon) |
| 83 return false; |
| 84 |
| 85 return true; |
| 86 } |
| 87 |
| 88 bool SkMatrixIsApproximatelyIdentity(const SkMatrix44& m) { |
| 89 for (int i = 0; i < 4; ++i) { |
| 90 for (int j = 0; j < 4; ++j) { |
| 91 float identity_value = i == j ? 1 : 0; |
| 92 float value = m.get(i, j); |
| 93 if (std::abs(identity_value - value) > kEpsilon) |
| 94 return false; |
| 95 } |
| 96 } |
| 97 return true; |
| 98 } |
| 99 |
| 100 } // namespace gfx |
| OLD | NEW |