From e930459a18ea099859f7d0076802458be00a6b4c Mon Sep 17 00:00:00 2001 From: mtklein Date: Thu, 7 Jul 2016 18:34:06 -0700 Subject: Revert of Move sRGB <-> linear conversion components to their own files. (patchset #5 id:80001 of https://codereview.chromium.org/2128893002/ ) Reason for revert: Monotonicity assert is failing on ARM. (Different rsqrt() and invert() precision?) Will investigate a bit tomorrow... might reland with the test TODO. Original issue's description: > Move sRGB <-> linear conversion components to their own files. > > This makes them a little easier to use outside SkColorXform code. > > I've added some notes about how best to use them and their eccentricities, and added a test. > > Ultimately any software sRGB <-> linear conversion should funnel somehow through here. > > BUG=skia: > GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2128893002 > CQ_EXTRA_TRYBOTS=client.skia:Test-Ubuntu-GCC-GCE-CPU-AVX2-x86_64-Release-SKNX_NO_SIMD-Trybot > > Committed: https://skia.googlesource.com/skia/+/45e58c8807179638980aae8503573b950b844e4c TBR=reed@google.com,msarett@google.com,mtklein@chromium.org # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=skia: Review-Url: https://codereview.chromium.org/2131793002 --- src/core/SkColorSpaceXform.cpp | 68 +++++++++++++++++++++++++++++++++++++- src/core/SkSRGB.cpp | 75 ------------------------------------------ src/core/SkSRGB.h | 50 ---------------------------- src/opts/SkColorXform_opts.h | 26 +++++++++++++-- 4 files changed, 90 insertions(+), 129 deletions(-) delete mode 100644 src/core/SkSRGB.cpp delete mode 100644 src/core/SkSRGB.h (limited to 'src') diff --git a/src/core/SkColorSpaceXform.cpp b/src/core/SkColorSpaceXform.cpp index e913c5555a..cb95c2999d 100644 --- a/src/core/SkColorSpaceXform.cpp +++ b/src/core/SkColorSpaceXform.cpp @@ -9,7 +9,6 @@ #include "SkColorSpace_Base.h" #include "SkColorSpaceXform.h" #include "SkOpts.h" -#include "SkSRGB.h" static inline bool compute_gamut_xform(SkMatrix44* srcToDst, const SkMatrix44& srcToXYZ, const SkMatrix44& dstToXYZ) { @@ -148,6 +147,73 @@ void SkFastXform linear and linear -> sRGB transformations. - * - * Current best practices: - * - for sRGB -> linear, lookup R,G,B in sk_linear_from_srgb; - * - for linear -> sRGB, call sk_linear_to_srgb() for R,G,B, clamp to 255, and round; - * - the alpha channel is linear in both formats, needing at most *(1/255.0f) or *255.0f. - * - * sk_linear_to_srgb()'s output requires rounding; it does not round for you. - * - * Given inputs in [0,1], sk_linear_to_srgb() will not underflow 0 but may overflow 255. - * The overflow is small enough that you can safely either clamp then round or round then clamp. - * (If you don't trust the inputs are in [0,1], you'd better clamp both sides immediately.) - * - * sk_linear_to_srgb() will run a little faster than usual when compiled with SSE4.1+. - */ - -extern const float sk_linear_from_srgb[256]; - -static inline Sk4f sk_linear_to_srgb(const Sk4f& x) { - // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixels). - // For 0.00000f <= x < 0.00349f, 12.92 * x - // For 0.00349f <= x <= 1.00000f, 0.679*(x.^0.5) + 0.423*x.^(0.25) - 0.101 - // Note that 0.00349 was selected because it is a point where both functions produce the - // same pixel value when rounded. - auto rsqrt = x.rsqrt(), - sqrt = rsqrt.invert(), - ftrt = rsqrt.rsqrt(); - - auto lo = (12.92f * 255.0f) * x; - - auto hi = (-0.101115084998961f * 255.0f) + - (+0.678513029959381f * 255.0f) * sqrt + - (+0.422602055039580f * 255.0f) * ftrt; - - return (x < 0.00349f).thenElse(lo, hi); -} - -#endif//SkSRGB_DEFINED diff --git a/src/opts/SkColorXform_opts.h b/src/opts/SkColorXform_opts.h index 9904b3ef93..74aa53ce7a 100644 --- a/src/opts/SkColorXform_opts.h +++ b/src/opts/SkColorXform_opts.h @@ -10,8 +10,8 @@ #include "SkNx.h" #include "SkColorPriv.h" -#include "SkSRGB.h" +extern const float sk_linear_from_srgb[256]; extern const float sk_linear_from_2dot2[256]; namespace SK_OPTS_NS { @@ -26,6 +26,26 @@ static Sk4f linear_to_2dot2(const Sk4f& x) { return 255.0f * x2.invert() * x32 * x64.invert(); } +static Sk4f linear_to_srgb(const Sk4f& x) { + // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixels). + // For 0.00000f <= x < 0.00349f, 12.92 * x + // For 0.00349f <= x <= 1.00000f, 0.679*(x.^0.5) + 0.423*x.^(0.25) - 0.101 + // Note that 0.00349 was selected because it is a point where both functions produce the + // same pixel value when rounded. + auto rsqrt = x.rsqrt(), + sqrt = rsqrt.invert(), + ftrt = rsqrt.rsqrt(); + + auto hi = (-0.101115084998961f * 255.0f) + + (+0.678513029959381f * 255.0f) * sqrt + + (+0.422602055039580f * 255.0f) * ftrt; + + auto lo = (12.92f * 255.0f) * x; + + auto mask = (x < 0.00349f); + return mask.thenElse(lo, hi); +} + static Sk4f clamp_0_to_255(const Sk4f& x) { // The order of the arguments is important here. We want to make sure that NaN // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. @@ -134,12 +154,12 @@ static void color_xform_RGB1_2dot2_to_2dot2(uint32_t* dst, const uint32_t* src, static void color_xform_RGB1_srgb_to_srgb(uint32_t* dst, const uint32_t* src, int len, const float matrix[16]) { - color_xform_RGB1(dst, src, len, matrix); + color_xform_RGB1(dst, src, len, matrix); } static void color_xform_RGB1_2dot2_to_srgb(uint32_t* dst, const uint32_t* src, int len, const float matrix[16]) { - color_xform_RGB1(dst, src, len, matrix); + color_xform_RGB1(dst, src, len, matrix); } } // namespace SK_OPTS_NS -- cgit v1.2.3