[gegl] shadows-highlights-correction: Optimize by avoiding NOP overlays



commit add04bc0d5a7f866396d62513207d36276282ea0
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Jan 2 07:26:42 2018 +0100

    shadows-highlights-correction: Optimize by avoiding NOP overlays
    
    Overlaying the highlights or shadows layer becomes a NOP if the
    corresponding xform value is 0.0. Unless the value of the "compress"
    property is less than 50.0, the default being 50.0, it means that for
    any given pixel only one of the layers have an effect.
    
    Earlier it took approximately 2.5s to process an 18 megapixel JPEG with
    shadows=100.0 and highlights=-100.0. Now, it takes 1.9s.

 .../workshop/shadows-highlights-correction.c       |  100 ++++++++++----------
 1 files changed, 52 insertions(+), 48 deletions(-)
---
diff --git a/operations/workshop/shadows-highlights-correction.c 
b/operations/workshop/shadows-highlights-correction.c
index b9be1e5..56f7b37 100644
--- a/operations/workshop/shadows-highlights-correction.c
+++ b/operations/workshop/shadows-highlights-correction.c
@@ -124,10 +124,6 @@ process (GeglOperation       *operation,
     {
       gfloat ta[3];
       gfloat tb0;
-      gfloat highlights2;
-      gfloat highlights_xform;
-      gfloat shadows2;
-      gfloat shadows_xform;
 
       ta[0] = src[0] / 100.f;
       ta[1] = src[1] / 128.f;
@@ -138,65 +134,73 @@ process (GeglOperation       *operation,
       ta[0] = ta[0] > 0.0f ? ta[0] / whitepoint : ta[0];
       tb0 = tb0 > 0.0f ? tb0 / whitepoint : tb0;
 
-      highlights2 = highlights * highlights;
-      highlights_xform = CLAMP(1.0f - tb0 / (1.0f - compress), 0.0f, 1.0f);
-
-      while (highlights2 > 0.0f)
+      if (tb0 < 1.0f - compress)
         {
-          gfloat lref, href;
-          gfloat chunk, optrans;
+          gfloat highlights2 = highlights * highlights;;
+          gfloat highlights_xform;
 
-          gfloat la = ta[0];
-          gfloat lb = (tb0 - 0.5f) * SIGN(-highlights) * SIGN(1.0f - la) + 0.5f;
+          highlights_xform = fminf(1.0f - tb0 / (1.0f - compress), 1.0f);
 
-          lref = copysignf(fabsf(la) > low_approximation ? 1.0f / fabsf(la) : 1.0f / low_approximation, la);
-          href = copysignf(
-              fabsf(1.0f - la) > low_approximation ? 1.0f / fabsf(1.0f - la) : 1.0f / low_approximation, 
1.0f - la);
+          while (highlights2 > 0.0f)
+            {
+              gfloat lref, href;
+              gfloat chunk, optrans;
 
-          chunk = highlights2 > 1.0f ? 1.0f : highlights2;
-          optrans = chunk * highlights_xform;
-          highlights2 -= 1.0f;
+              gfloat la = ta[0];
+              gfloat lb = (tb0 - 0.5f) * SIGN(-highlights) * SIGN(1.0f - la) + 0.5f;
 
-          ta[0] = la * (1.0 - optrans)
-                  + (la > 0.5f ? 1.0f - (1.0f - 2.0f * (la - 0.5f)) * (1.0f - lb) : 2.0f * la * lb) * 
optrans;
+              lref = copysignf(fabsf(la) > low_approximation ? 1.0f / fabsf(la) : 1.0f / low_approximation, 
la);
+              href = copysignf(fabsf(1.0f - la) > low_approximation ? 1.0f / fabsf(1.0f - la) : 1.0f / 
low_approximation, 1.0f - la);
 
-          ta[1] = ta[1] * (1.0f - optrans)
-                  + ta[1] * (ta[0] * lref * (1.0f - highlights_ccorrect)
-                             + (1.0f - ta[0]) * href * highlights_ccorrect) * optrans;
+              chunk = highlights2 > 1.0f ? 1.0f : highlights2;
+              optrans = chunk * highlights_xform;
+              highlights2 -= 1.0f;
 
-          ta[2] = ta[2] * (1.0f - optrans)
-                  + ta[2] * (ta[0] * lref * (1.0f - highlights_ccorrect)
-                             + (1.0f - ta[0]) * href * highlights_ccorrect) * optrans;
-        }
+              ta[0] = la * (1.0 - optrans)
+                      + (la > 0.5f ? 1.0f - (1.0f - 2.0f * (la - 0.5f)) * (1.0f - lb) : 2.0f * la * lb) * 
optrans;
+
+              ta[1] = ta[1] * (1.0f - optrans)
+                      + ta[1] * (ta[0] * lref * (1.0f - highlights_ccorrect)
+                                 + (1.0f - ta[0]) * href * highlights_ccorrect) * optrans;
 
-    shadows2 = shadows * shadows;
-    shadows_xform = CLAMP(tb0 / (1.0f - compress) - compress / (1.0f - compress), 0.0f, 1.0f);
+              ta[2] = ta[2] * (1.0f - optrans)
+                      + ta[2] * (ta[0] * lref * (1.0f - highlights_ccorrect)
+                                 + (1.0f - ta[0]) * href * highlights_ccorrect) * optrans;
+            }
+        }
 
-    while (shadows2 > 0.0f)
+    if (tb0 > compress)
       {
-        gfloat lref, href;
-        gfloat chunk, optrans;
+        gfloat shadows2 = shadows * shadows;
+        gfloat shadows_xform;
+
+        shadows_xform = fminf(tb0 / (1.0f - compress) - compress / (1.0f - compress), 1.0f);
+
+        while (shadows2 > 0.0f)
+          {
+            gfloat lref, href;
+            gfloat chunk, optrans;
 
-        gfloat la = ta[0];
-        gfloat lb = (tb0 - 0.5f) * SIGN(shadows) * SIGN(1.0f - la) + 0.5f;
-        lref = copysignf(fabsf(la) > low_approximation ? 1.0f / fabsf(la) : 1.0f / low_approximation, la);
-        href = copysignf(
-            fabsf(1.0f - la) > low_approximation ? 1.0f / fabsf(1.0f - la) : 1.0f / low_approximation, 1.0f 
- la);
+            gfloat la = ta[0];
+            gfloat lb = (tb0 - 0.5f) * SIGN(shadows) * SIGN(1.0f - la) + 0.5f;
+            lref = copysignf(fabsf(la) > low_approximation ? 1.0f / fabsf(la) : 1.0f / low_approximation, 
la);
+            href = copysignf(fabsf(1.0f - la) > low_approximation ? 1.0f / fabsf(1.0f - la) : 1.0f / 
low_approximation, 1.0f - la);
 
-        chunk = shadows2 > 1.0f ? 1.0f : shadows2;
-        optrans = chunk * shadows_xform;
-        shadows2 -= 1.0f;
+            chunk = shadows2 > 1.0f ? 1.0f : shadows2;
+            optrans = chunk * shadows_xform;
+            shadows2 -= 1.0f;
 
-        ta[0] = la * (1.0 - optrans)
-                + (la > 0.5f ? 1.0f - (1.0f - 2.0f * (la - 0.5f)) * (1.0f - lb) : 2.0f * la * lb) * optrans;
+            ta[0] = la * (1.0 - optrans)
+                    + (la > 0.5f ? 1.0f - (1.0f - 2.0f * (la - 0.5f)) * (1.0f - lb) : 2.0f * la * lb) * 
optrans;
 
-        ta[1] = ta[1] * (1.0f - optrans)
-                + ta[1] * (ta[0] * lref * shadows_ccorrect
-                           + (1.0f - ta[0]) * href * (1.0f - shadows_ccorrect)) * optrans;
+            ta[1] = ta[1] * (1.0f - optrans)
+                    + ta[1] * (ta[0] * lref * shadows_ccorrect
+                               + (1.0f - ta[0]) * href * (1.0f - shadows_ccorrect)) * optrans;
 
-        ta[2] = ta[2] * (1.0f - optrans)
-                + ta[2] * (ta[0] * lref * shadows_ccorrect
-                           + (1.0f - ta[0]) * href * (1.0f - shadows_ccorrect)) * optrans;
+            ta[2] = ta[2] * (1.0f - optrans)
+                    + ta[2] * (ta[0] * lref * shadows_ccorrect
+                               + (1.0f - ta[0]) * href * (1.0f - shadows_ccorrect)) * optrans;
+          }
       }
 
       dst[0] = ta[0] * 100.f;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]