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

Unified Diff: ui/gfx/skia_color_space_util.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 side-by-side diff with in-line comments
Download patch
« ui/gfx/color_transform.cc ('K') | « ui/gfx/skia_color_space_util.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/skia_color_space_util.cc
diff --git a/ui/gfx/skia_color_space_util.cc b/ui/gfx/skia_color_space_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1b226a37db22d06fcf5205faf28d3e1252709c60
--- /dev/null
+++ b/ui/gfx/skia_color_space_util.cc
@@ -0,0 +1,100 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/gfx/skia_color_space_util.h"
+
+#include <algorithm>
+#include <cmath>
+
+namespace gfx {
+
+namespace {
+
+const float kEpsilon = 1.f / 256.f;
+}
+
+float EvalSkTransferFn(const SkColorSpaceTransferFn& fn, float x) {
+ if (x < 0.f)
+ return 0.f;
+ if (x < fn.fD)
+ return fn.fC * x + fn.fF;
+ return std::pow(fn.fA * x + fn.fB, fn.fG) + fn.fE;
+}
+
+SkColorSpaceTransferFn SkTransferFnInverse(const SkColorSpaceTransferFn& fn) {
+ SkColorSpaceTransferFn fn_inv = {0};
+ if (fn.fA > 0 && fn.fG > 0) {
+ double a_to_the_g = std::pow(fn.fA, fn.fG);
+ fn_inv.fA = 1.f / a_to_the_g;
+ fn_inv.fB = -fn.fE / a_to_the_g;
+ fn_inv.fG = 1.f / fn.fG;
+ }
+ fn_inv.fD = fn.fC * fn.fD + fn.fF;
+ fn_inv.fE = -fn.fB / fn.fA;
+ if (fn.fC != 0) {
+ fn_inv.fC = 1.f / fn.fC;
+ fn_inv.fF = -fn.fF / fn.fC;
+ }
+ return fn_inv;
+}
+
+bool SkTransferFnIsApproximatelyIdentity(const SkColorSpaceTransferFn& fn) {
+ const float kEpsilon = 1.f / 256.f;
+ if (fn.fD > 0.f) {
+ if (std::abs(fn.fC - 1.f) > kEpsilon)
+ return false;
+ if (std::abs(fn.fF) > kEpsilon)
+ return false;
+ }
+ if (std::abs(fn.fA - 1.f) > kEpsilon)
+ return false;
+ if (std::abs(fn.fB) > kEpsilon)
+ return false;
+ if (std::abs(fn.fE) > kEpsilon)
+ return false;
+ if (std::abs(fn.fG - 1.f) > kEpsilon)
+ return false;
+ return true;
+}
+
+bool SkTransferFnsApproximatelyCancel(const SkColorSpaceTransferFn& a,
+ const SkColorSpaceTransferFn& b) {
+ const float kStep = 1.f / 256.f;
+ SkColorSpaceTransferFn b_inv = SkTransferFnInverse(b);
+
+ // Evaluate the linear sections explicitly.
+ float linear_segment_max = std::max(a.fD, b.fD);
+ for (float x = 0.f; x < linear_segment_max; x += kStep) {
+ float a_of_x = EvalSkTransferFn(a, x);
+ float b_inv_of_x = EvalSkTransferFn(b, a_of_x);
+ if (std::abs(a_of_x - b_inv_of_x) > kEpsilon)
+ return false;
+ }
+
+ // Compare the coefficients of the pow sections.
+ if (std::abs(a.fA - b_inv.fA) > kEpsilon)
+ return false;
+ if (std::abs(a.fB - b_inv.fB) > kEpsilon)
+ return false;
+ if (std::abs(a.fE - b_inv.fE) > kEpsilon)
+ return false;
+ if (std::abs(a.fG - b_inv.fG) > kEpsilon)
+ return false;
+
+ return true;
+}
+
+bool SkMatrixIsApproximatelyIdentity(const SkMatrix44& m) {
+ for (int i = 0; i < 4; ++i) {
+ for (int j = 0; j < 4; ++j) {
+ float identity_value = i == j ? 1 : 0;
+ float value = m.get(i, j);
+ if (std::abs(identity_value - value) > kEpsilon)
+ return false;
+ }
+ }
+ return true;
+}
+
+} // namespace gfx
« ui/gfx/color_transform.cc ('K') | « ui/gfx/skia_color_space_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698