Move sRGB <-> linear conversion components to their own files.
authormtklein <mtklein@chromium.org>
Fri, 8 Jul 2016 13:33:16 +0000 (06:33 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 8 Jul 2016 13:33:16 +0000 (06:33 -0700)
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.android:Test-Android-GCC-Nexus5-CPU-NEON-Arm7-Release-Trybot;client.skia:Test-Ubuntu-GCC-GCE-CPU-AVX2-x86_64-Release-SKNX_NO_SIMD-Trybot

Committed: https://skia.googlesource.com/skia/+/45e58c8807179638980aae8503573b950b844e4c
Review-Url: https://codereview.chromium.org/2128893002

gyp/core.gypi
src/core/SkColorSpaceXform.cpp
src/core/SkSRGB.cpp [new file with mode: 0644]
src/core/SkSRGB.h [new file with mode: 0644]
src/opts/SkColorXform_opts.h
tests/SRGBTest.cpp [new file with mode: 0644]

index 91387a3..97e38c2 100644 (file)
         '<(skia_src_path)/core/SkStrokerPriv.h',
         '<(skia_src_path)/core/SkSurfacePriv.h',
         '<(skia_src_path)/core/SkSwizzle.cpp',
+        '<(skia_src_path)/core/SkSRGB.cpp',
         '<(skia_src_path)/core/SkTaskGroup.cpp',
         '<(skia_src_path)/core/SkTaskGroup.h',
         '<(skia_src_path)/core/SkTDPQueue.h',
index cb95c29..e913c55 100644 (file)
@@ -9,6 +9,7 @@
 #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) {
@@ -147,73 +148,6 @@ void SkFastXform<SkColorSpace::k2Dot2Curve_GammaNamed, SkColorSpace::k2Dot2Curve
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-extern const float sk_linear_from_srgb[256] = {
-        0.000000000000000000f, 0.000303526983548838f, 0.000607053967097675f, 0.000910580950646513f,
-        0.001214107934195350f, 0.001517634917744190f, 0.001821161901293030f, 0.002124688884841860f,
-        0.002428215868390700f, 0.002731742851939540f, 0.003034518678424960f, 0.003346535763899160f,
-        0.003676507324047440f, 0.004024717018496310f, 0.004391442037410290f, 0.004776953480693730f,
-        0.005181516702338390f, 0.005605391624202720f, 0.006048833022857060f, 0.006512090792594470f,
-        0.006995410187265390f, 0.007499032043226180f, 0.008023192985384990f, 0.008568125618069310f,
-        0.009134058702220790f, 0.009721217320237850f, 0.010329823029626900f, 0.010960094006488200f,
-        0.011612245179743900f, 0.012286488356915900f, 0.012983032342173000f, 0.013702083047289700f,
-        0.014443843596092500f, 0.015208514422912700f, 0.015996293365509600f, 0.016807375752887400f,
-        0.017641954488384100f, 0.018500220128379700f, 0.019382360956935700f, 0.020288563056652400f,
-        0.021219010376003600f, 0.022173884793387400f, 0.023153366178110400f, 0.024157632448504800f,
-        0.025186859627361600f, 0.026241221894849900f, 0.027320891639074900f, 0.028426039504420800f,
-        0.029556834437808800f, 0.030713443732993600f, 0.031896033073011500f, 0.033104766570885100f,
-        0.034339806808682200f, 0.035601314875020300f, 0.036889450401100000f, 0.038204371595346500f,
-        0.039546235276732800f, 0.040915196906853200f, 0.042311410620809700f, 0.043735029256973500f,
-        0.045186204385675500f, 0.046665086336880100f, 0.048171824226889400f, 0.049706565984127200f,
-        0.051269458374043200f, 0.052860647023180200f, 0.054480276442442400f, 0.056128490049600100f,
-        0.057805430191067200f, 0.059511238162981200f, 0.061246054231617600f, 0.063010017653167700f,
-        0.064803266692905800f, 0.066625938643772900f, 0.068478169844400200f, 0.070360095696595900f,
-        0.072271850682317500f, 0.074213568380149600f, 0.076185381481307900f, 0.078187421805186300f,
-        0.080219820314468300f, 0.082282707129814800f, 0.084376211544148800f, 0.086500462036549800f,
-        0.088655586285772900f, 0.090841711183407700f, 0.093058962846687500f, 0.095307466630964700f,
-        0.097587347141862500f, 0.099898728247113900f, 0.102241733088101000f, 0.104616484091104000f,
-        0.107023102978268000f, 0.109461710778299000f, 0.111932427836906000f, 0.114435373826974000f,
-        0.116970667758511000f, 0.119538427988346000f, 0.122138772229602000f, 0.124771817560950000f,
-        0.127437680435647000f, 0.130136476690364000f, 0.132868321553818000f, 0.135633329655206000f,
-        0.138431615032452000f, 0.141263291140272000f, 0.144128470858058000f, 0.147027266497595000f,
-        0.149959789810609000f, 0.152926151996150000f, 0.155926463707827000f, 0.158960835060880000f,
-        0.162029375639111000f, 0.165132194501668000f, 0.168269400189691000f, 0.171441100732823000f,
-        0.174647403655585000f, 0.177888415983629000f, 0.181164244249860000f, 0.184474994500441000f,
-        0.187820772300678000f, 0.191201682740791000f, 0.194617830441576000f, 0.198069319559949000f,
-        0.201556253794397000f, 0.205078736390317000f, 0.208636870145256000f, 0.212230757414055000f,
-        0.215860500113899000f, 0.219526199729269000f, 0.223227957316809000f, 0.226965873510098000f,
-        0.230740048524349000f, 0.234550582161005000f, 0.238397573812271000f, 0.242281122465555000f,
-        0.246201326707835000f, 0.250158284729953000f, 0.254152094330827000f, 0.258182852921596000f,
-        0.262250657529696000f, 0.266355604802862000f, 0.270497791013066000f, 0.274677312060385000f,
-        0.278894263476810000f, 0.283148740429992000f, 0.287440837726918000f, 0.291770649817536000f,
-        0.296138270798321000f, 0.300543794415777000f, 0.304987314069886000f, 0.309468922817509000f,
-        0.313988713375718000f, 0.318546778125092000f, 0.323143209112951000f, 0.327778098056542000f,
-        0.332451536346179000f, 0.337163615048330000f, 0.341914424908661000f, 0.346704056355030000f,
-        0.351532599500439000f, 0.356400144145944000f, 0.361306779783510000f, 0.366252595598840000f,
-        0.371237680474149000f, 0.376262122990906000f, 0.381326011432530000f, 0.386429433787049000f,
-        0.391572477749723000f, 0.396755230725627000f, 0.401977779832196000f, 0.407240211901737000f,
-        0.412542613483904000f, 0.417885070848138000f, 0.423267669986072000f, 0.428690496613907000f,
-        0.434153636174749000f, 0.439657173840919000f, 0.445201194516228000f, 0.450785782838223000f,
-        0.456411023180405000f, 0.462076999654407000f, 0.467783796112159000f, 0.473531496148010000f,
-        0.479320183100827000f, 0.485149940056070000f, 0.491020849847836000f, 0.496932995060870000f,
-        0.502886458032569000f, 0.508881320854934000f, 0.514917665376521000f, 0.520995573204354000f,
-        0.527115125705813000f, 0.533276404010505000f, 0.539479489012107000f, 0.545724461370187000f,
-        0.552011401512000000f, 0.558340389634268000f, 0.564711505704929000f, 0.571124829464873000f,
-        0.577580440429651000f, 0.584078417891164000f, 0.590618840919337000f, 0.597201788363763000f,
-        0.603827338855338000f, 0.610495570807865000f, 0.617206562419651000f, 0.623960391675076000f,
-        0.630757136346147000f, 0.637596873994033000f, 0.644479681970582000f, 0.651405637419824000f,
-        0.658374817279448000f, 0.665387298282272000f, 0.672443156957688000f, 0.679542469633094000f,
-        0.686685312435314000f, 0.693871761291990000f, 0.701101891932973000f, 0.708375779891687000f,
-        0.715693500506481000f, 0.723055128921969000f, 0.730460740090354000f, 0.737910408772731000f,
-        0.745404209540387000f, 0.752942216776078000f, 0.760524504675292000f, 0.768151147247507000f,
-        0.775822218317423000f, 0.783537791526194000f, 0.791297940332630000f, 0.799102738014409000f,
-        0.806952257669252000f, 0.814846572216101000f, 0.822785754396284000f, 0.830769876774655000f,
-        0.838799011740740000f, 0.846873231509858000f, 0.854992608124234000f, 0.863157213454102000f,
-        0.871367119198797000f, 0.879622396887832000f, 0.887923117881966000f, 0.896269353374266000f,
-        0.904661174391149000f, 0.913098651793419000f, 0.921581856277295000f, 0.930110858375424000f,
-        0.938685728457888000f, 0.947306536733200000f, 0.955973353249286000f, 0.964686247894465000f,
-        0.973445290398413000f, 0.982250550333117000f, 0.991102097113830000f, 1.000000000000000000f,
-};
-
 extern const float sk_linear_from_2dot2[256] = {
         0.000000000000000000f, 0.000005077051900662f, 0.000023328004666099f, 0.000056921765712193f,
         0.000107187362341244f, 0.000175123977503027f, 0.000261543754548491f, 0.000367136269815943f,
diff --git a/src/core/SkSRGB.cpp b/src/core/SkSRGB.cpp
new file mode 100644 (file)
index 0000000..57d2ae0
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkSRGB.h"
+
+const float sk_linear_from_srgb[256] = {
+        0.000000000000000000f, 0.000303526983548838f, 0.000607053967097675f, 0.000910580950646513f,
+        0.001214107934195350f, 0.001517634917744190f, 0.001821161901293030f, 0.002124688884841860f,
+        0.002428215868390700f, 0.002731742851939540f, 0.003034518678424960f, 0.003346535763899160f,
+        0.003676507324047440f, 0.004024717018496310f, 0.004391442037410290f, 0.004776953480693730f,
+        0.005181516702338390f, 0.005605391624202720f, 0.006048833022857060f, 0.006512090792594470f,
+        0.006995410187265390f, 0.007499032043226180f, 0.008023192985384990f, 0.008568125618069310f,
+        0.009134058702220790f, 0.009721217320237850f, 0.010329823029626900f, 0.010960094006488200f,
+        0.011612245179743900f, 0.012286488356915900f, 0.012983032342173000f, 0.013702083047289700f,
+        0.014443843596092500f, 0.015208514422912700f, 0.015996293365509600f, 0.016807375752887400f,
+        0.017641954488384100f, 0.018500220128379700f, 0.019382360956935700f, 0.020288563056652400f,
+        0.021219010376003600f, 0.022173884793387400f, 0.023153366178110400f, 0.024157632448504800f,
+        0.025186859627361600f, 0.026241221894849900f, 0.027320891639074900f, 0.028426039504420800f,
+        0.029556834437808800f, 0.030713443732993600f, 0.031896033073011500f, 0.033104766570885100f,
+        0.034339806808682200f, 0.035601314875020300f, 0.036889450401100000f, 0.038204371595346500f,
+        0.039546235276732800f, 0.040915196906853200f, 0.042311410620809700f, 0.043735029256973500f,
+        0.045186204385675500f, 0.046665086336880100f, 0.048171824226889400f, 0.049706565984127200f,
+        0.051269458374043200f, 0.052860647023180200f, 0.054480276442442400f, 0.056128490049600100f,
+        0.057805430191067200f, 0.059511238162981200f, 0.061246054231617600f, 0.063010017653167700f,
+        0.064803266692905800f, 0.066625938643772900f, 0.068478169844400200f, 0.070360095696595900f,
+        0.072271850682317500f, 0.074213568380149600f, 0.076185381481307900f, 0.078187421805186300f,
+        0.080219820314468300f, 0.082282707129814800f, 0.084376211544148800f, 0.086500462036549800f,
+        0.088655586285772900f, 0.090841711183407700f, 0.093058962846687500f, 0.095307466630964700f,
+        0.097587347141862500f, 0.099898728247113900f, 0.102241733088101000f, 0.104616484091104000f,
+        0.107023102978268000f, 0.109461710778299000f, 0.111932427836906000f, 0.114435373826974000f,
+        0.116970667758511000f, 0.119538427988346000f, 0.122138772229602000f, 0.124771817560950000f,
+        0.127437680435647000f, 0.130136476690364000f, 0.132868321553818000f, 0.135633329655206000f,
+        0.138431615032452000f, 0.141263291140272000f, 0.144128470858058000f, 0.147027266497595000f,
+        0.149959789810609000f, 0.152926151996150000f, 0.155926463707827000f, 0.158960835060880000f,
+        0.162029375639111000f, 0.165132194501668000f, 0.168269400189691000f, 0.171441100732823000f,
+        0.174647403655585000f, 0.177888415983629000f, 0.181164244249860000f, 0.184474994500441000f,
+        0.187820772300678000f, 0.191201682740791000f, 0.194617830441576000f, 0.198069319559949000f,
+        0.201556253794397000f, 0.205078736390317000f, 0.208636870145256000f, 0.212230757414055000f,
+        0.215860500113899000f, 0.219526199729269000f, 0.223227957316809000f, 0.226965873510098000f,
+        0.230740048524349000f, 0.234550582161005000f, 0.238397573812271000f, 0.242281122465555000f,
+        0.246201326707835000f, 0.250158284729953000f, 0.254152094330827000f, 0.258182852921596000f,
+        0.262250657529696000f, 0.266355604802862000f, 0.270497791013066000f, 0.274677312060385000f,
+        0.278894263476810000f, 0.283148740429992000f, 0.287440837726918000f, 0.291770649817536000f,
+        0.296138270798321000f, 0.300543794415777000f, 0.304987314069886000f, 0.309468922817509000f,
+        0.313988713375718000f, 0.318546778125092000f, 0.323143209112951000f, 0.327778098056542000f,
+        0.332451536346179000f, 0.337163615048330000f, 0.341914424908661000f, 0.346704056355030000f,
+        0.351532599500439000f, 0.356400144145944000f, 0.361306779783510000f, 0.366252595598840000f,
+        0.371237680474149000f, 0.376262122990906000f, 0.381326011432530000f, 0.386429433787049000f,
+        0.391572477749723000f, 0.396755230725627000f, 0.401977779832196000f, 0.407240211901737000f,
+        0.412542613483904000f, 0.417885070848138000f, 0.423267669986072000f, 0.428690496613907000f,
+        0.434153636174749000f, 0.439657173840919000f, 0.445201194516228000f, 0.450785782838223000f,
+        0.456411023180405000f, 0.462076999654407000f, 0.467783796112159000f, 0.473531496148010000f,
+        0.479320183100827000f, 0.485149940056070000f, 0.491020849847836000f, 0.496932995060870000f,
+        0.502886458032569000f, 0.508881320854934000f, 0.514917665376521000f, 0.520995573204354000f,
+        0.527115125705813000f, 0.533276404010505000f, 0.539479489012107000f, 0.545724461370187000f,
+        0.552011401512000000f, 0.558340389634268000f, 0.564711505704929000f, 0.571124829464873000f,
+        0.577580440429651000f, 0.584078417891164000f, 0.590618840919337000f, 0.597201788363763000f,
+        0.603827338855338000f, 0.610495570807865000f, 0.617206562419651000f, 0.623960391675076000f,
+        0.630757136346147000f, 0.637596873994033000f, 0.644479681970582000f, 0.651405637419824000f,
+        0.658374817279448000f, 0.665387298282272000f, 0.672443156957688000f, 0.679542469633094000f,
+        0.686685312435314000f, 0.693871761291990000f, 0.701101891932973000f, 0.708375779891687000f,
+        0.715693500506481000f, 0.723055128921969000f, 0.730460740090354000f, 0.737910408772731000f,
+        0.745404209540387000f, 0.752942216776078000f, 0.760524504675292000f, 0.768151147247507000f,
+        0.775822218317423000f, 0.783537791526194000f, 0.791297940332630000f, 0.799102738014409000f,
+        0.806952257669252000f, 0.814846572216101000f, 0.822785754396284000f, 0.830769876774655000f,
+        0.838799011740740000f, 0.846873231509858000f, 0.854992608124234000f, 0.863157213454102000f,
+        0.871367119198797000f, 0.879622396887832000f, 0.887923117881966000f, 0.896269353374266000f,
+        0.904661174391149000f, 0.913098651793419000f, 0.921581856277295000f, 0.930110858375424000f,
+        0.938685728457888000f, 0.947306536733200000f, 0.955973353249286000f, 0.964686247894465000f,
+        0.973445290398413000f, 0.982250550333117000f, 0.991102097113830000f, 1.000000000000000000f,
+};
diff --git a/src/core/SkSRGB.h b/src/core/SkSRGB.h
new file mode 100644 (file)
index 0000000..de80596
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkSRGB_DEFINED
+#define SkSRGB_DEFINED
+
+#include "SkNx.h"
+
+/** Components for building our canonical sRGB -> 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
index 74aa53c..9904b3e 100644 (file)
@@ -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,26 +26,6 @@ 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.
@@ -154,12 +134,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<sk_linear_from_srgb, linear_to_srgb>(dst, src, len, matrix);
+    color_xform_RGB1<sk_linear_from_srgb, sk_linear_to_srgb>(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<sk_linear_from_2dot2, linear_to_srgb>(dst, src, len, matrix);
+    color_xform_RGB1<sk_linear_from_2dot2, sk_linear_to_srgb>(dst, src, len, matrix);
 }
 
 }  // namespace SK_OPTS_NS
diff --git a/tests/SRGBTest.cpp b/tests/SRGBTest.cpp
new file mode 100644 (file)
index 0000000..95d2866
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkSRGB.h"
+#include "SkTypes.h"
+#include "Test.h"
+#include <math.h>
+
+static uint8_t linear_to_srgb(float l) {
+    return (uint8_t)roundf(sk_linear_to_srgb(Sk4f{l})[0]);
+}
+
+DEF_TEST(sk_linear_to_srgb, r) {
+    // Should map 0 -> 0 and 1 -> 1.
+    REPORTER_ASSERT(r,   0 == linear_to_srgb(0.0f));
+    REPORTER_ASSERT(r, 255 == linear_to_srgb(1.0f));
+
+    // Should be monotonic between 0 and 1.
+    // We don't bother checking denorm values.
+    int tolerated_regressions = 0;
+#if defined(SK_ARM_HAS_NEON)
+    // Values around 0.166016 are usually 72 but drop briefly (41 floats) down to 71.
+    tolerated_regressions = 1;
+#endif
+    uint8_t prev = 0;
+    for (float f = FLT_MIN; f <= 1.0f; ) {
+        uint8_t srgb = linear_to_srgb(f);
+
+        REPORTER_ASSERT(r, srgb >= prev || tolerated_regressions > 0);
+        if (srgb < prev) { tolerated_regressions--; }
+        prev = srgb;
+
+        union { float flt; uint32_t bits; } pun = { f };
+        pun.bits++;
+        f = pun.flt;
+    }
+}