[gimp] app: mypaint: Honor selection mask



commit 71e8ef6103329a26605c1ad34c16313a28e7db13
Author: Daniel Sabo <DanielSabo gmail com>
Date:   Wed Jan 27 08:29:20 2016 -0800

    app: mypaint: Honor selection mask

 app/paint/gimpmybrushcore.c    |    5 +++-
 app/paint/gimpmybrushsurface.c |   57 +++++++++++++++++++++++++++++++++++++++-
 app/paint/gimpmybrushsurface.h |    5 +++-
 3 files changed, 64 insertions(+), 3 deletions(-)
---
diff --git a/app/paint/gimpmybrushcore.c b/app/paint/gimpmybrushcore.c
index 8eb5964..24b52cc 100644
--- a/app/paint/gimpmybrushcore.c
+++ b/app/paint/gimpmybrushcore.c
@@ -190,7 +190,10 @@ gimp_mybrush_core_paint (GimpPaintCore    *paint_core,
       gimp_palettes_add_color_history (context->gimp, &fg);
 
       mybrush->private->surface = gimp_mypaint_surface_new (gimp_drawable_get_buffer (drawable),
-                                                            gimp_drawable_get_active_mask (drawable));
+                                                            gimp_drawable_get_active_mask (drawable),
+                                                            paint_core->mask_buffer,
+                                                            paint_core->mask_x_offset,
+                                                            paint_core->mask_y_offset);
 
       mybrush->private->brush = mypaint_brush_new ();
       mypaint_brush_from_defaults (mybrush->private->brush);
diff --git a/app/paint/gimpmybrushsurface.c b/app/paint/gimpmybrushsurface.c
index 5c5809d..6ba0c95 100644
--- a/app/paint/gimpmybrushsurface.c
+++ b/app/paint/gimpmybrushsurface.c
@@ -36,6 +36,9 @@ struct _GimpMybrushSurface
 {
   MyPaintSurface surface;
   GeglBuffer *buffer;
+  GeglBuffer *paint_mask;
+  gint        paint_mask_x;
+  gint        paint_mask_y;
   GeglRectangle dirty;
   GimpComponentMask component_mask;
 };
@@ -262,11 +265,27 @@ gimp_mypaint_surface_get_color (MyPaintSurface *base_surface,
                                                          babl_format ("R'aG'aB'aA float"),
                                                          GEGL_BUFFER_READ,
                                                          GEGL_ABYSS_CLAMP);
+    if (surface->paint_mask)
+      {
+        GeglRectangle mask_roi = dabRect;
+        mask_roi.x -= surface->paint_mask_x;
+        mask_roi.y -= surface->paint_mask_y;
+        gegl_buffer_iterator_add (iter, surface->paint_mask, &mask_roi, 0,
+                                  babl_format ("Y float"),
+                                  GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
+      }
+
     while (gegl_buffer_iterator_next (iter))
       {
         float *pixel = (float *)iter->data[0];
+        float *mask;
         int iy, ix;
 
+        if (surface->paint_mask)
+          mask = iter->data[1];
+        else
+          mask = NULL;
+
         for (iy = iter->roi[0].y; iy < iter->roi[0].y + iter->roi[0].height; iy++)
           {
             float yy = (iy + 0.5f - y);
@@ -278,6 +297,8 @@ gimp_mypaint_surface_get_color (MyPaintSurface *base_surface,
                 float pixel_weight = 0.0f;
                 if (rr <= 1.0f)
                   pixel_weight = 1.0f - rr;
+                if (mask)
+                  pixel_weight *= *mask;
 
                 sum_r += pixel_weight * pixel[RED];
                 sum_g += pixel_weight * pixel[GREEN];
@@ -286,6 +307,8 @@ gimp_mypaint_surface_get_color (MyPaintSurface *base_surface,
                 sum_weight += pixel_weight;
 
                 pixel += 4;
+                if (mask)
+                  mask += 1;
               }
           }
       }
@@ -366,11 +389,27 @@ gimp_mypaint_surface_draw_dab (MyPaintSurface *base_surface,
                                    babl_format ("R'G'B'A float"),
                                    GEGL_BUFFER_READWRITE,
                                    GEGL_ABYSS_NONE);
+  if (surface->paint_mask)
+    {
+      GeglRectangle mask_roi = dabRect;
+      mask_roi.x -= surface->paint_mask_x;
+      mask_roi.y -= surface->paint_mask_y;
+      gegl_buffer_iterator_add (iter, surface->paint_mask, &mask_roi, 0,
+                                babl_format ("Y float"),
+                                GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
+    }
 
   while (gegl_buffer_iterator_next (iter))
     {
       float *pixel = (float *)iter->data[0];
+      float *mask;
       int iy, ix;
+
+      if (surface->paint_mask)
+        mask = iter->data[1];
+      else
+        mask = NULL;
+
       for (iy = iter->roi[0].y; iy < iter->roi[0].y + iter->roi[0].height; iy++)
         {
           for (ix = iter->roi[0].x; ix < iter->roi[0].x +  iter->roi[0].width; ix++)
@@ -382,6 +421,8 @@ gimp_mypaint_surface_draw_dab (MyPaintSurface *base_surface,
                 rr = calculate_rr (ix, iy, x, y, aspect_ratio, sn, cs, one_over_radius2);
               base_alpha = calculate_alpha_for_rr (rr, hardness, segment1_slope, segment2_slope);
               alpha = base_alpha * normal_mode;
+              if (mask)
+                alpha *= *mask;
               dst_alpha = pixel[ALPHA];
               /* a = alpha * color_a + dst_alpha * (1.0f - alpha);
                * which converts to: */
@@ -447,6 +488,8 @@ gimp_mypaint_surface_draw_dab (MyPaintSurface *base_surface,
                 }
 
               pixel += 4;
+              if (mask)
+                mask += 1;
             }
         }
     }
@@ -478,11 +521,17 @@ gimp_mypaint_surface_destroy (MyPaintSurface *base_surface)
   GimpMybrushSurface *surface = (GimpMybrushSurface *)base_surface;
   g_object_unref (surface->buffer);
   surface->buffer = NULL;
+  if (surface->paint_mask)
+    g_object_unref (surface->paint_mask);
+  surface->paint_mask = NULL;
 }
 
 GimpMybrushSurface *
 gimp_mypaint_surface_new (GeglBuffer        *buffer,
-                          GimpComponentMask  component_mask)
+                          GimpComponentMask  component_mask,
+                          GeglBuffer        *paint_mask,
+                          gint               paint_mask_x,
+                          gint               paint_mask_y)
 {
   GimpMybrushSurface *surface = g_malloc0 (sizeof (GimpMybrushSurface));
   mypaint_surface_init ((MyPaintSurface *)surface);
@@ -493,6 +542,12 @@ gimp_mypaint_surface_new (GeglBuffer        *buffer,
   surface->surface.destroy = gimp_mypaint_surface_destroy;
   surface->component_mask = component_mask;
   surface->buffer = g_object_ref (buffer);
+  if (paint_mask)
+    surface->paint_mask = g_object_ref (paint_mask);
+  else
+    surface->paint_mask = NULL;
+  surface->paint_mask_x = paint_mask_x;
+  surface->paint_mask_y = paint_mask_y;
   surface->dirty = *GEGL_RECTANGLE (0, 0, 0, 0);
 
   return surface;
diff --git a/app/paint/gimpmybrushsurface.h b/app/paint/gimpmybrushsurface.h
index 42f0736..bf9c49c 100644
--- a/app/paint/gimpmybrushsurface.h
+++ b/app/paint/gimpmybrushsurface.h
@@ -23,7 +23,10 @@ typedef struct _GimpMybrushSurface GimpMybrushSurface;
 
 GimpMybrushSurface *
 gimp_mypaint_surface_new (GeglBuffer        *buffer,
-                          GimpComponentMask  component_mask);
+                          GimpComponentMask  component_mask,
+                          GeglBuffer        *paint_mask,
+                          gint               paint_mask_x,
+                          gint               paint_mask_y);
 
 
 #endif  /*  __GIMP_MYBRUSH_SURFACE_H__  */


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