[gimp/pippin/linear-is-the-new-black: 5/17] app: gimp_blend_composite; split compositing out to helper functions



commit 35be02c4d8309c89dfab6b2b38cf0d2bb2a15e21
Author: Øyvind Kolås <pippin gimp org>
Date:   Wed Jan 18 19:29:48 2017 +0100

    app: gimp_blend_composite; split compositing out to helper functions

 .../layer-modes/gimpoperationpointlayermode.h      |  242 ++++++++++----------
 1 files changed, 123 insertions(+), 119 deletions(-)
---
diff --git a/app/operations/layer-modes/gimpoperationpointlayermode.h 
b/app/operations/layer-modes/gimpoperationpointlayermode.h
index e3a90c8..b9f2463 100644
--- a/app/operations/layer-modes/gimpoperationpointlayermode.h
+++ b/app/operations/layer-modes/gimpoperationpointlayermode.h
@@ -102,6 +102,122 @@ gimp_operation_layer_composite (const gfloat *in,
     }
 }
 
+static inline void
+compfun_src_atop (gfloat *in,
+                  gfloat *layer,
+                  gfloat *mask,
+                  gfloat  opacity,
+                  gfloat *out,
+                  gint    samples)
+{
+  while (samples--)
+    {
+      gfloat comp_alpha = layer[ALPHA] * opacity;
+      if (mask)
+        comp_alpha *= *mask;
+ 
+      if (comp_alpha == 0.0f)
+        {
+          out[RED]   = in[RED];
+          out[GREEN] = in[GREEN];
+          out[BLUE]  = in[BLUE];
+        }
+        else
+        {
+          gint   b;
+          for (b = RED; b < ALPHA; b++)
+            out[b] = layer[b] * comp_alpha + in[b] * (1.0f - comp_alpha);
+        }
+      out[ALPHA] = in[ALPHA];
+ 
+      in    += 4;
+      out   += 4;
+      layer += 4;
+ 
+      if (mask)
+        mask++;
+    }
+}
+
+static inline void
+compfun_src_over (gfloat *in,
+                  gfloat *layer,
+                  gfloat *mask,
+                  gfloat  opacity,
+                  gfloat *out,
+                  gint    samples)
+{
+  while (samples--)
+    {
+      gfloat new_alpha;
+      gfloat comp_alpha = layer[ALPHA] * opacity;
+      if (mask)
+        comp_alpha *= *mask;
+ 
+      new_alpha = comp_alpha + (1.0f - comp_alpha) * in[ALPHA];
+ 
+      if (comp_alpha == 0.0f || new_alpha == 0.0f)
+        {
+          out[RED]   = in[RED];
+          out[GREEN] = in[GREEN];
+          out[BLUE]  = in[BLUE];
+        }
+        else
+        {
+          gint   b;
+          //gfloat ratio = comp_alpha / new_alpha;
+          for (b = RED; b < ALPHA; b++)
+            //out[b] = ratio * (in[ALPHA] * (layer[b] - layer[b]) + layer[b] - in[b]) + in[b];
+            out[b] = layer[b] * comp_alpha + in[b] * (1.0f - comp_alpha);
+        }
+ 
+      out[ALPHA] = new_alpha;
+ 
+      in    += 4;
+      layer += 4;
+      out   += 4;
+ 
+      if (mask)
+        mask++;
+    }
+}
+
+
+static inline void
+compfun_src_in (gfloat *in,
+                gfloat *layer,
+                gfloat *mask,
+                gfloat  opacity,
+                gfloat *out,
+                gint    samples)
+{
+  while (samples--)
+    {
+      gfloat new_alpha = in[ALPHA] * layer[ALPHA] * opacity;
+      if (mask)
+        new_alpha *= *mask;
+ 
+      if (new_alpha == 0.0f)
+        {
+          out[RED]   = in[RED];
+          out[GREEN] = in[GREEN];
+          out[BLUE]  = in[BLUE];
+        }
+      else
+        {
+          out[RED]   = layer[RED];
+          out[GREEN] = layer[GREEN];
+          out[BLUE]  = layer[BLUE];
+        }
+      out[ALPHA] = new_alpha;
+      in    += 4;
+      out   += 4;
+      layer += 4;
+ 
+      if (mask)
+        mask++;
+    }
+}
 
 static inline void
 gimp_composite_blend (gfloat                *in,
@@ -118,13 +234,12 @@ gimp_composite_blend (gfloat                *in,
                                               float *out,
                                               int samples))
 {
-  int samples_backup  = samples;
   gfloat *blend_in    = in;
   gfloat *blend_layer = layer;
   gfloat *blend_out   = out;
 
-  const Babl *fish_to_blend = NULL;
-  const Babl *fish_to_composite = NULL;
+  const Babl *fish_to_blend       = NULL;
+  const Babl *fish_to_composite   = NULL;
   const Babl *fish_from_composite = NULL;
  
   switch (blend_trc)
@@ -210,136 +325,25 @@ gimp_composite_blend (gfloat                *in,
   {
     case GIMP_LAYER_COMPOSITE_SRC_ATOP:
     default:
-      while (samples--)
-        {
-          gfloat comp_alpha = blend_out[ALPHA] * opacity;
-          if (mask)
-            comp_alpha *= *mask;
- 
-          if (comp_alpha == 0.0f)
-            {
-              out[RED]   = blend_in[RED];
-              out[GREEN] = blend_in[GREEN];
-              out[BLUE]  = blend_in[BLUE];
-            }
-            else
-            {
-              gint   b;
-              for (b = RED; b < ALPHA; b++)
-                out[b] = blend_out[b] * comp_alpha + blend_in[b] * (1.0f - comp_alpha);
-            }
-          out[ALPHA] = blend_in[ALPHA];
- 
-          in          += 4;
-          out         += 4;
-          blend_out   += 4;
- 
-          if (mask)
-            mask++;
-        }
+      compfun_src_atop (blend_in, blend_out, mask, opacity, out, samples);
       break;
     case GIMP_LAYER_COMPOSITE_SRC_OVER:
-      while (samples--)
-        {
-          gfloat new_alpha;
-          gfloat comp_alpha = blend_out[ALPHA] * opacity;
-          if (mask)
-            comp_alpha *= *mask;
- 
-          new_alpha = comp_alpha + (1.0f - comp_alpha) * blend_in[ALPHA];
- 
-          if (comp_alpha == 0.0f || new_alpha == 0.0f)
-            {
-              out[RED]   = blend_in[RED];
-              out[GREEN] = blend_in[GREEN];
-              out[BLUE]  = blend_in[BLUE];
-            }
-            else
-            {
-              gint   b;
-              //gfloat ratio = comp_alpha / new_alpha;
-              for (b = RED; b < ALPHA; b++)
-                //out[b] = ratio * (in[ALPHA] * (blend_out[b] - layer[b]) + layer[b] - in[b]) + in[b];
-                out[b] = blend_out[b] * comp_alpha + blend_in[b] * (1.0f - comp_alpha);
-            }
- 
-          out[ALPHA] = new_alpha;
- 
-          blend_in    += 4;
-          blend_out   += 4;
-          out         += 4;
- 
-          if (mask)
-            mask++;
-        }
+      compfun_src_over (blend_in, blend_out, mask, opacity, out, samples);
       break;
     case GIMP_LAYER_COMPOSITE_DST_ATOP:
-
       if (fish_to_composite)
         babl_process (fish_to_composite, blend_layer, blend_layer,  samples);
 
-      while (samples--)
-        {
-          gfloat comp_alpha = blend_in[ALPHA];
- 
-          out[ALPHA] = blend_out[ALPHA] * opacity;
-          if (mask)
-            out[ALPHA] *= *mask;
- 
-          if (comp_alpha == 0.0f)
-            {
-              out[RED]   = blend_layer[RED];
-              out[GREEN] = blend_layer[GREEN];
-              out[BLUE]  = blend_layer[BLUE];
-            }
-            else
-            {
-              gint   b;
-              for (b = RED; b < ALPHA; b++)
-                out[b] = blend_layer[b] * (1.0f -comp_alpha) + blend_out[b] * (comp_alpha);
-            }
-          blend_in    += 4;
-          blend_layer += 4;
-          out         += 4;
-          blend_out   += 4;
- 
-          if (mask)
-            mask++;
-        }
+      compfun_src_atop (blend_out, blend_in, mask, opacity, out, samples); /* swapped arguments */
       break;
     case GIMP_LAYER_COMPOSITE_SRC_IN:
-      while (samples--)
-        {
-          gfloat new_alpha = blend_in[ALPHA] * blend_out[ALPHA] * opacity;
-          if (mask)
-            new_alpha *= *mask;
- 
-          if (new_alpha == 0.0f)
-            {
-              out[RED]   = blend_in[RED];
-              out[GREEN] = blend_in[GREEN];
-              out[BLUE]  = blend_in[BLUE];
-            }
-          else
-            {
-              out[RED]   = blend_out[RED];
-              out[GREEN] = blend_out[GREEN];
-              out[BLUE]  = blend_out[BLUE];
-            }
-          out[ALPHA] = new_alpha;
-          blend_in    += 4;
-          blend_out   += 4;
- 
-          if (mask)
-            mask++;
-        }
+      compfun_src_in (blend_in, blend_out, mask, opacity, out, samples);
       break;
   }
 
   if (fish_from_composite)
   {
-    out -= 4 * samples_backup;
-    babl_process (fish_from_composite, out, out, samples_backup);
+    babl_process (fish_from_composite, out, out, samples);
   }
 }
  


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