From 1c58e179b59408ed813a767f4d02096a3f4aca6b Mon Sep 17 00:00:00 2001 From: Kelsey Gilbert Date: Mon, 6 Feb 2023 19:58:54 +0000 Subject: [PATCH] Bug 1799258 - No-op equal tfs rather than inverting. r=bradwerth Differential Revision: https://phabricator.services.mozilla.com/D167471 --- gfx/gl/Colorspaces.cpp | 31 ++++++++++++++++++++----------- gfx/gl/Colorspaces.h | 13 ++++++++----- gfx/gl/gtest/TestColorspaces.cpp | 2 +- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/gfx/gl/Colorspaces.cpp b/gfx/gl/Colorspaces.cpp index 44b96b53a596..c7c40b2abaa8 100644 --- a/gfx/gl/Colorspaces.cpp +++ b/gfx/gl/Colorspaces.cpp @@ -409,17 +409,26 @@ ColorProfileConversionDesc ColorProfileConversionDesc::From( dstLinearRgbFromXyzd65 * desc.src.xyzd65FromLinearRgb, .dstTfFromDstLinear = {}, }; - const auto Invert = [](const std::vector& linearFromTf, - std::vector* const tfFromLinear) { - const auto size = linearFromTf.size(); - MOZ_ASSERT(size != 1); // Less than two is uninvertable. - if (size < 2) return; - (*tfFromLinear).resize(size); - InvertLut(linearFromTf, &*tfFromLinear); - }; - Invert(desc.dst.linearFromTf.r, &ret.dstTfFromDstLinear.r); - Invert(desc.dst.linearFromTf.g, &ret.dstTfFromDstLinear.g); - Invert(desc.dst.linearFromTf.b, &ret.dstTfFromDstLinear.b); + bool sameTF = true; + sameTF &= desc.src.linearFromTf.r == desc.dst.linearFromTf.r; + sameTF &= desc.src.linearFromTf.g == desc.dst.linearFromTf.g; + sameTF &= desc.src.linearFromTf.b == desc.dst.linearFromTf.b; + if (sameTF) { + ret.srcLinearFromSrcTf = {}; + ret.dstTfFromDstLinear = {}; + } else { + const auto Invert = [](const std::vector& linearFromTf, + std::vector* const tfFromLinear) { + const auto size = linearFromTf.size(); + MOZ_ASSERT(size != 1); // Less than two is uninvertable. + if (size < 2) return; + (*tfFromLinear).resize(size); + InvertLut(linearFromTf, &*tfFromLinear); + }; + Invert(desc.dst.linearFromTf.r, &ret.dstTfFromDstLinear.r); + Invert(desc.dst.linearFromTf.g, &ret.dstTfFromDstLinear.g); + Invert(desc.dst.linearFromTf.b, &ret.dstTfFromDstLinear.b); + } return ret; } diff --git a/gfx/gl/Colorspaces.h b/gfx/gl/Colorspaces.h index fb7f346ebe40..1b4f6f9ba48b 100644 --- a/gfx/gl/Colorspaces.h +++ b/gfx/gl/Colorspaces.h @@ -81,7 +81,7 @@ struct PiecewiseGammaDesc final { 4.5, }; } - // static constexpr auto Rec2020_10bit() { return Rec709(); } + // FYI: static constexpr auto Rec2020_10bit() { return Rec709(); } static constexpr auto Rec2020_12bit() { return PiecewiseGammaDesc{ 1.0993, @@ -704,7 +704,7 @@ mat3 XyzAFromXyzB_BradfordLinear(const vec2 xyA, const vec2 xyB); struct ColorProfileDesc { // ICC profiles are phrased as PCS-from-encoded (PCS is CIEXYZ-D50) // However, all of our colorspaces are D65, so let's normalize to that, - // even though it's a reverseable transform. + // even though it's a reversible transform. color::mat4 rgbFromYcbcr = color::mat4::Identity(); RgbTransferTables linearFromTf; color::mat3 xyzd65FromLinearRgb = color::mat3::Identity(); @@ -743,7 +743,7 @@ inline float SampleInByOut(const C& outByIn, const float out) { const auto d_in = float(1) / (outByIn.size() - 1); const auto d_out = *(out0_itr + 1) - *out0_itr; - // printf("%f - (%f / %f) * (%f - %f)\n", in0, d_in, d_out, out, out0); + // printf("%f + (%f / %f) * (%f - %f)\n", in0, d_in, d_out, out, out0); const auto in = in0 + (d_in / d_out) * (out - out0); // printf("SampleInByOut(%f)->%f\n", out, in); return in; @@ -786,10 +786,13 @@ struct TwoPoints { } p1; T y(const T x) const { - return p0.y + (x - p0.x) / (p1.x - p0.x) * (p1.y - p0.y); + const auto dx = p1.x - p0.x; + const auto dy = p1.y - p0.y; + return p0.y + dy / dx * (x - p0.x); } }; +/// Fills `vals` with `x:[0..vals.size()-1] => line.y(x)`. template static void LinearFill(T& vals, const TwoPoints& line) { float x = -1; @@ -837,7 +840,7 @@ inline void DequantizeMonotonic(const Span vals) { // E.g. [0,1,1,2] // ^^^ body // => f(0.5)->0.5, f(2.5)->1.5 - // => f(x) = f(x0) + (x-x0) * (f(x1) - f(x0)) / (x1-x1) + // => f(x) = f(x0) + (x-x0) * (f(x1) - f(x0)) / (x1-x0) // => f(x) = f(x0) + (x-x0) * dfdx const auto head_end = *body_first; diff --git a/gfx/gl/gtest/TestColorspaces.cpp b/gfx/gl/gtest/TestColorspaces.cpp index 19886814e61c..c437e204af02 100644 --- a/gfx/gl/gtest/TestColorspaces.cpp +++ b/gfx/gl/gtest/TestColorspaces.cpp @@ -528,7 +528,7 @@ TEST(Colorspaces, GuessGamma) { EXPECT_NEAR(GuessGamma(MakeGamma(1, 11)), 1.0, 0); EXPECT_NEAR(GuessGamma(MakeGamma(2.2, 11)), 2.2, 4.8e-8); - EXPECT_NEAR(GuessGamma(MakeGamma(1 / 2.2, 11)), 1 / 2.2, 1.1e-7); + EXPECT_NEAR(GuessGamma(MakeGamma(1 / 2.2, 11)), 1 / 2.2, 1.7e-7); } // - -- 2.11.4.GIT