[gimp/wip/alxsa/mypaint-brush-v2] Fix GEGL buffer leak, rearrange code



commit 5ecb00eda06637545deca84139492e977afae170
Author: Alx Sa <cmyk student gmail com>
Date:   Sun Oct 16 17:33:59 2022 +0000

    Fix GEGL buffer leak, rearrange code

 app/core/gimpmybrush-load.c    |  13 +-
 app/paint/gimpmybrushcore.c    |   2 +
 app/paint/gimpmybrushoptions.c |  23 +-
 app/paint/gimpmybrushsurface.c | 476 ++++++++++++++++++++++++++++++++---------
 4 files changed, 391 insertions(+), 123 deletions(-)
---
diff --git a/app/core/gimpmybrush-load.c b/app/core/gimpmybrush-load.c
index 61e1e32898..b9bd74b39f 100644
--- a/app/core/gimpmybrush-load.c
+++ b/app/core/gimpmybrush-load.c
@@ -148,20 +148,15 @@ gimp_mybrush_load (GimpContext   *context,
                                   MYPAINT_BRUSH_SETTING_OFFSET_BY_RANDOM);
 
   /* Version 2 MyPaint Brush options */
-  brush->priv->pigment =
-    mypaint_brush_get_base_value (mypaint_brush,
-                                  MYPAINT_BRUSH_SETTING_PAINT_MODE);
+  brush->priv->pigment = -0.1;
 
   brush->priv->posterize =
     mypaint_brush_get_base_value (mypaint_brush,
                                   MYPAINT_BRUSH_SETTING_POSTERIZE);
 
-  if (! mypaint_brush_is_constant (mypaint_brush, MYPAINT_BRUSH_SETTING_POSTERIZE_NUM))
-    brush->priv->posterize_num =
-      mypaint_brush_get_base_value (mypaint_brush,
-                                    MYPAINT_BRUSH_SETTING_POSTERIZE_NUM);
-  else
-    brush->priv->posterize_num = 1.0f;
+  brush->priv->posterize_num =
+    mypaint_brush_get_base_value (mypaint_brush,
+                                  MYPAINT_BRUSH_SETTING_POSTERIZE_NUM);
 
   mypaint_brush_unref (mypaint_brush);
 
diff --git a/app/paint/gimpmybrushcore.c b/app/paint/gimpmybrushcore.c
index 08249bd841..b7e5910cf6 100644
--- a/app/paint/gimpmybrushcore.c
+++ b/app/paint/gimpmybrushcore.c
@@ -241,6 +241,8 @@ gimp_mybrush_core_paint (GimpPaintCore    *paint_core,
 
     case GIMP_PAINT_STATE_FINISH:
       gimp_symmetry_set_stateful (sym, FALSE);
+      mypaint_surface_unref (mypaint_surface2_to_surface (
+                             (MyPaintSurface2 *) mybrush->private->surface));
       mybrush->private->surface = NULL;
 
       g_list_free_full (mybrush->private->brushes,
diff --git a/app/paint/gimpmybrushoptions.c b/app/paint/gimpmybrushoptions.c
index 9ef71d50e3..b208b2582c 100644
--- a/app/paint/gimpmybrushoptions.c
+++ b/app/paint/gimpmybrushoptions.c
@@ -182,10 +182,11 @@ gimp_mybrush_options_set_property (GObject      *object,
   switch (property_id)
     {
     case PROP_VIEW_ZOOM:
-      options->view_zoom = g_value_get_double (value);
+      options->view_zoom = g_value_get_double (value) > 0.0f ?
+                           g_value_get_double (value) : 0.0001f;
       break;
     case PROP_VIEW_ROTATION:
-      options->view_rotation = g_value_get_double (value);
+      options->view_rotation = CLAMP (g_value_get_double (value), 0.0f, 360.0f);
       break;
     case PROP_RADIUS:
       options->radius = g_value_get_double (value);
@@ -271,15 +272,15 @@ gimp_mybrush_options_mybrush_changed (GimpContext *context,
 {
   if (brush)
     g_object_set (context,
-                  "viewzoom",      1.0f,
-                  "viewrotation",  0.0f,
-                  "radius",        gimp_mybrush_get_radius (brush),
-                  "opaque",        gimp_mybrush_get_opaque (brush),
-                  "hardness",      gimp_mybrush_get_hardness (brush),
-                  "pigment",       gimp_mybrush_get_pigment (brush),
-                  "posterize",     gimp_mybrush_get_posterize (brush),
-                  "posterizenum",  gimp_mybrush_get_posterize_num (brush),
-                  "eraser",        gimp_mybrush_get_is_eraser (brush),
+                  "viewzoom",       1.0f,
+                  "viewrotation",   0.0f,
+                  "radius",         gimp_mybrush_get_radius (brush),
+                  "opaque",         gimp_mybrush_get_opaque (brush),
+                  "hardness",       gimp_mybrush_get_hardness (brush),
+                  "pigment",        gimp_mybrush_get_pigment (brush),
+                  "posterize",      gimp_mybrush_get_posterize (brush),
+                  "posterizenum",   gimp_mybrush_get_posterize_num (brush),
+                  "eraser",         gimp_mybrush_get_is_eraser (brush),
                   NULL);
 }
 
diff --git a/app/paint/gimpmybrushsurface.c b/app/paint/gimpmybrushsurface.c
index f2bd130717..be691353f8 100644
--- a/app/paint/gimpmybrushsurface.c
+++ b/app/paint/gimpmybrushsurface.c
@@ -86,17 +86,16 @@ float
 spectral_blend_factor            (float     x);
 
 void
-get_color_pixels_legacy          (float    *mask,
+get_color_pixels_legacy          (float     mask,
                                   float    *pixel,
                                   float    *sum_weight,
                                   float    *sum_r,
                                   float    *sum_g,
                                   float    *sum_b,
-                                  float    *sum_a,
-                                  float     pixel_weight);
+                                  float    *sum_a);
 
 void
-get_color_pixels_accumulate      (float    *mask,
+get_color_pixels_accumulate      (float     mask,
                                   float    *pixel,
                                   float    *sum_weight,
                                   float    *sum_r,
@@ -104,11 +103,72 @@ get_color_pixels_accumulate      (float    *mask,
                                   float    *sum_b,
                                   float    *sum_a,
                                   float     paint,
-                                  float     pixel_weight,
                                   uint16_t  sample_interval,
                                   float     random_sample_rate,
                                   uint16_t  interval_counter);
 
+void
+draw_dab_pixels_BlendMode_Normal (float    *mask,
+                                  float    *pixel,
+                                  float     color_r,
+                                  float     color_g,
+                                  float     color_b,
+                                  float     opacity);
+
+void
+draw_dab_pixels_BlendMode_Normal_Paint
+                                 (float    *mask,
+                                  float    *pixel,
+                                  float     color_r,
+                                  float     color_g,
+                                  float     color_b,
+                                  float     opacity);
+
+void
+draw_dab_pixels_BlendMode_Normal_and_Eraser
+                                 (float    *mask,
+                                  float    *pixel,
+                                  float     color_r,
+                                  float     color_g,
+                                  float     color_b,
+                                  float     color_a,
+                                  float     opacity);
+
+void
+draw_dab_pixels_BlendMode_Normal_and_Eraser_Paint
+                                 (float    *mask,
+                                  float    *pixel,
+                                  float     color_r,
+                                  float     color_g,
+                                  float     color_b,
+                                  float     color_a,
+                                  float     opacity);
+
+void
+draw_dab_pixels_BlendMode_LockAlpha
+                                 (float    *mask,
+                                  float    *pixel,
+                                  float     color_r,
+                                  float     color_g,
+                                  float     color_b,
+                                  float     opacity);
+
+void
+draw_dab_pixels_BlendMode_LockAlpha_Paint
+                                 (float    *mask,
+                                  float    *pixel,
+                                  float     color_r,
+                                  float     color_g,
+                                  float     color_b,
+                                  float     opacity);
+
+void
+draw_dab_pixels_BlendMode_Posterize
+                                 (float    *mask,
+                                  float    *pixel,
+                                  float     opacity,
+                                  float     posterize_num);
+
 /* --- Taken from mypaint-tiled-surface.c --- */
 static inline float
 calculate_rr (int   xp,
@@ -317,24 +377,23 @@ spectral_to_rgb (float *spectral,
 
 /* -- Taken from brushmode.c -- */
 void
-get_color_pixels_legacy (float *mask,
+get_color_pixels_legacy (float  mask,
                          float *pixel,
                          float *sum_weight,
                          float *sum_r,
                          float *sum_g,
                          float *sum_b,
-                         float *sum_a,
-                         float  pixel_weight)
+                         float *sum_a)
 {
-  *sum_r += pixel_weight * pixel[RED];
-  *sum_g += pixel_weight * pixel[GREEN];
-  *sum_b += pixel_weight * pixel[BLUE];
-  *sum_a += pixel_weight * pixel[ALPHA];
-  *sum_weight += pixel_weight;
+  *sum_r += mask * pixel[RED];
+  *sum_g += mask * pixel[GREEN];
+  *sum_b += mask * pixel[BLUE];
+  *sum_a += mask * pixel[ALPHA];
+  *sum_weight += mask;
 }
 
 void
-get_color_pixels_accumulate (float   *mask,
+get_color_pixels_accumulate (float    mask,
                              float   *pixel,
                              float   *sum_weight,
                              float   *sum_r,
@@ -342,7 +401,6 @@ get_color_pixels_accumulate (float   *mask,
                              float   *sum_b,
                              float   *sum_a,
                              float    paint,
-                             float    pixel_weight,
                              uint16_t sample_interval,
                              float    random_sample_rate,
                              uint16_t interval_counter)
@@ -356,7 +414,7 @@ get_color_pixels_accumulate (float   *mask,
   if (paint < 0.0f)
     {
       get_color_pixels_legacy (mask, pixel, sum_weight,
-                               sum_r, sum_g, sum_b, sum_a, pixel_weight);
+                               sum_r, sum_g, sum_b, sum_a);
       return;
     }
 
@@ -365,10 +423,10 @@ get_color_pixels_accumulate (float   *mask,
   if (interval_counter == 0 || rand() < random_sample_threshold)
     {
       float fac_a, fac_b;
-      float a = pixel_weight * pixel[ALPHA];
+      float a = mask * pixel[ALPHA];
       float alpha_sums = a + *sum_a;
 
-      *sum_weight += pixel_weight;
+      *sum_weight += mask;
 
       fac_a = fac_b = 1.0f;
       if (alpha_sums > 0.0f)
@@ -416,6 +474,251 @@ spectral_blend_factor (float x)
   const float b = x * hor_fac - hor_offs;
   return 0.5 + b / (1 + fabsf(b) * ver_fac);
 }
+
+void
+draw_dab_pixels_BlendMode_Normal (float *mask,
+                                  float *pixel,
+                                  float  color_r,
+                                  float  color_g,
+                                  float  color_b,
+                                  float  opacity)
+{
+  float src_term = *mask * opacity;
+  float dst_term = 1.0f - src_term;
+
+  pixel[RED]   = color_r * src_term + pixel[RED] * dst_term;
+  pixel[GREEN] = color_g * src_term + pixel[GREEN] * dst_term;
+  pixel[BLUE]  = color_b * src_term + pixel[BLUE] * dst_term;
+}
+
+void
+draw_dab_pixels_BlendMode_Normal_Paint (float *mask,
+                                        float *pixel,
+                                        float  color_r,
+                                        float  color_g,
+                                        float  color_b,
+                                        float  opacity)
+{
+  float spectral_a[10]      = {0};
+  float spectral_b[10]      = {0};
+  float spectral_result[10] = {0};
+  float rgb_result[3]       = {0};
+  float src_term;
+  float dst_term;
+  float fac_a;
+  float fac_b;
+
+  rgb_to_spectral (color_r, color_g, color_b, spectral_a);
+  opacity = MAX (opacity, 1.5);
+
+  src_term = *mask * opacity;
+  dst_term = 1.0f - src_term;
+
+  if (pixel[ALPHA] <= 0)
+    {
+      pixel[ALPHA] = src_term + dst_term * pixel[ALPHA];
+      pixel[RED]   = src_term * color_r + dst_term * pixel[RED];
+      pixel[GREEN] = src_term * color_g + dst_term * pixel[GREEN];
+      pixel[BLUE]  = src_term * color_b + dst_term * pixel[BLUE];
+    }
+  else
+    {
+      fac_a = src_term / (src_term + dst_term * pixel[ALPHA]);
+      fac_b = 1.0f - fac_a;
+
+      rgb_to_spectral (pixel[RED] / pixel[ALPHA],
+                       pixel[GREEN] / pixel[ALPHA],
+                       pixel[BLUE] / pixel[ALPHA],
+                       spectral_b);
+
+      for (int i = 0; i < 10; i++)
+        spectral_result[i] =
+          fastpow (spectral_a[i], fac_a) * fastpow (spectral_b[i], fac_b);
+
+      spectral_to_rgb (spectral_result, rgb_result);
+      pixel[ALPHA] = src_term + dst_term * pixel[ALPHA];
+
+      pixel[RED]   = (rgb_result[0] * pixel[ALPHA]);
+      pixel[GREEN] = (rgb_result[1] * pixel[ALPHA]);
+      pixel[BLUE]  = (rgb_result[2] * pixel[ALPHA]);
+    }
+}
+
+void
+draw_dab_pixels_BlendMode_Normal_and_Eraser (float *mask,
+                                             float *pixel,
+                                             float  color_r,
+                                             float  color_g,
+                                             float  color_b,
+                                             float  color_a,
+                                             float  opacity)
+{
+  float src_term = *mask * opacity;
+  float dst_term = 1.0f - src_term;
+
+  src_term *= color_a;
+
+  pixel[RED]   = color_r * src_term + pixel[RED] * dst_term;
+  pixel[GREEN] = color_g * src_term + pixel[GREEN] * dst_term;
+  pixel[BLUE]  = color_b * src_term + pixel[BLUE] * dst_term;
+}
+
+void
+draw_dab_pixels_BlendMode_Normal_and_Eraser_Paint (float *mask,
+                                                   float *pixel,
+                                                   float  color_r,
+                                                   float  color_g,
+                                                   float  color_b,
+                                                   float  color_a,
+                                                   float  opacity)
+{
+  float spectral_a[10]      = {0};
+  float spectral_b[10]      = {0};
+  float spectral_result[10] = {0};
+  float rgb[3]              = {0};
+  float rgb_result[3]       = {0};
+  float src_term            = *mask * opacity;
+  float dst_term            = 1.0f - src_term;
+  float src_term2           = src_term * color_a;
+  float out_term            = src_term2 + dst_term * pixel[ALPHA];
+  float fac_a;
+  float fac_b;
+  float spectral_factor;
+  float additive_factor;
+
+  rgb_to_spectral (color_r, color_g, color_b, spectral_a);
+
+  spectral_factor =
+    CLAMP (spectral_blend_factor (pixel[ALPHA]), 0.0f, 1.0f);
+  additive_factor = 1.0 - spectral_factor;
+  if (additive_factor)
+    {
+      rgb[0] = src_term2 * color_r + dst_term * pixel[RED];
+      rgb[1] = src_term2 * color_g + dst_term * pixel[GREEN];
+      rgb[2] = src_term2 * color_b + dst_term * pixel[BLUE];
+    }
+
+  if (spectral_factor && pixel[ALPHA] != 0)
+    {
+      /* Convert straightened tile pixel color to a spectral */
+      rgb_to_spectral (pixel[RED] / pixel[ALPHA],
+                       pixel[GREEN] / pixel[ALPHA],
+                       pixel[BLUE] / pixel[ALPHA],
+                       spectral_b);
+
+      fac_a = src_term / (src_term + dst_term * pixel[ALPHA]);
+      fac_a *= color_a;
+      fac_b = 1.0 - fac_a;
+
+      /* Mix input and tile pixel colors using WGM */
+      for (int i = 0; i < 10; i++) {
+        spectral_result[i] =
+          fastpow (spectral_a[i], fac_a) * fastpow (spectral_b[i], fac_b);
+      }
+
+      /* Convert back to RGB */
+      spectral_to_rgb (spectral_result, rgb_result);
+
+      for (int i = 0; i < 3; i++)
+        rgb[i] = (additive_factor * rgb[i]) +
+          (spectral_factor * rgb_result[i] * out_term);
+    }
+
+  pixel[ALPHA] = out_term;
+  pixel[RED] = rgb[0];
+  pixel[GREEN] = rgb[1];
+  pixel[BLUE] = rgb[2];
+}
+
+void
+draw_dab_pixels_BlendMode_LockAlpha (float *mask,
+                                     float *pixel,
+                                     float  color_r,
+                                     float  color_g,
+                                     float  color_b,
+                                     float  opacity)
+{
+  float src_term = *mask * opacity;
+  float dst_term = 1.0f - src_term;
+
+  src_term /= pixel[ALPHA];
+
+  pixel[RED]   = color_r * src_term + pixel[RED] * dst_term;
+  pixel[GREEN] = color_g * src_term + pixel[GREEN] * dst_term;
+  pixel[BLUE]  = color_b * src_term + pixel[BLUE] * dst_term;
+}
+
+void draw_dab_pixels_BlendMode_LockAlpha_Paint (float *mask,
+                                                float    *pixel,
+                                                float     color_r,
+                                                float     color_g,
+                                                float     color_b,
+                                                float     opacity)
+{
+  float spectral_a[10]      = {0};
+  float spectral_b[10]      = {0};
+  float spectral_result[10] = {0};
+  float rgb_result[3]       = {0};
+  float src_term;
+  float dst_term;
+  float fac_a;
+  float fac_b;
+
+  rgb_to_spectral (color_r, color_g, color_b, spectral_a);
+  opacity = MAX (opacity, 1.5);
+
+  src_term = *mask * opacity;
+  dst_term = 1.0f - src_term;
+  src_term *= pixel[ALPHA];
+
+  if (pixel[ALPHA] <= 0)
+    {
+      pixel[RED]   = src_term * color_r + dst_term * pixel[RED];
+      pixel[GREEN] = src_term * color_g + dst_term * pixel[GREEN];
+      pixel[BLUE]  = src_term * color_b + dst_term * pixel[BLUE];
+    }
+  else
+    {
+      fac_a = src_term / (src_term + dst_term * pixel[ALPHA]);
+      fac_b = 1.0f - fac_a;
+
+      rgb_to_spectral (pixel[RED] / pixel[ALPHA],
+                       pixel[GREEN] / pixel[ALPHA],
+                       pixel[BLUE] / pixel[ALPHA],
+                       spectral_b);
+
+      for (int i = 0; i < 10; i++)
+        spectral_result[i] =
+          fastpow (spectral_a[i], fac_a) * fastpow (spectral_b[i], fac_b);
+
+      spectral_to_rgb (spectral_result, rgb_result);
+      pixel[ALPHA] = src_term + dst_term * pixel[ALPHA];
+
+      pixel[RED]   = rgb_result[0] * pixel[ALPHA];
+      pixel[GREEN] = rgb_result[1] * pixel[ALPHA];
+      pixel[BLUE]  = rgb_result[2] * pixel[ALPHA];
+    }
+}
+
+void
+draw_dab_pixels_BlendMode_Posterize (float *mask,
+                                     float *pixel,
+                                     float  opacity,
+                                     float  posterize_num)
+{
+  float post_r = ROUND (pixel[RED] * posterize_num) / posterize_num;
+  float post_g = ROUND (pixel[GREEN] * posterize_num) / posterize_num;
+  float post_b = ROUND (pixel[BLUE] * posterize_num) / posterize_num;
+
+  float src_term = *mask * opacity;
+  float dst_term = 1 - src_term;
+
+  pixel[RED]   = src_term * post_r + dst_term * pixel[RED];
+  pixel[GREEN] = src_term * post_g + dst_term * pixel[GREEN];
+  pixel[BLUE]  = src_term * post_b + dst_term * pixel[BLUE];
+}
+
+
 /* -- end mypaint code */
 
 static inline float
@@ -564,79 +867,52 @@ gimp_mypaint_surface_draw_dab_2 (MyPaintSurface2 *base_surface,
               g = pixel[GREEN];
               b = pixel[BLUE];
 
-              if (paint < 0.0f)
+              /* v1 Brush code */
+              if (paint < 1.0f)
                 {
-                  if (a > 0.0f)
-                    {
-                      /* By definition the ratio between each color[] and pixel[] component in a 
non-pre-multipled blend always sums to 1.0f.
-                       * Originally this would have been "(color[n] * alpha * color_a + pixel[n] * dst_alpha 
* (1.0f - alpha)) / a",
-                       * instead we only calculate the cheaper term. */
-                      float src_term = (alpha * color_a) / a;
-                      float dst_term = 1.0f - src_term;
-                      r = color_r * src_term + r * dst_term;
-                      g = color_g * src_term + g * dst_term;
-                      b = color_b * src_term + b * dst_term;
-                    }
+                  if (color_a == 1.0f)
+                    draw_dab_pixels_BlendMode_Normal (&alpha, pixel,
+                                                      color_r, color_g, color_b,
+                                                      normal_mode * opaque * (1 - paint));
+                  else
+                    draw_dab_pixels_BlendMode_Normal_and_Eraser (&alpha, pixel,
+                                                                 color_r, color_g,
+                                                                 color_b, color_a,
+                                                                 normal_mode * opaque * (1 - paint));
+
+                  if (lock_alpha > 0.0f && color_a != 0)
+                    draw_dab_pixels_BlendMode_LockAlpha (&alpha, pixel,
+                                                         color_r, color_g, color_b,
+                                                         lock_alpha * opaque * (1 - colorize) *
+                                                         (1 - posterize) * (1 - paint));
+
+                  r = pixel[RED];
+                  g = pixel[GREEN];
+                  b = pixel[BLUE];
                 }
-              else
+
+              /* v2 Brush code */
+              if (paint > 0.0f)
                 {
-                  if (a > 0.0f)
-                    {
-                      float spectral_a[10]      = {0};
-                      float spectral_b[10]      = {0};
-                      float spectral_result[10] = {0};
-                      float rgb[3]              = {0};
-                      float rgb_result[3]       = {0};
-                      float src_term            = alpha * MAX (normal_mode, 1.5);
-                      float dst_term            = 1.0f - src_term;
-                      float src_term2           = src_term * color_a;
-                      float out_term            = src_term2 + dst_term * pixel[ALPHA];
-                      float fac_a, fac_b;
-                      float spectral_factor, additive_factor;
-
-                      rgb_to_spectral (color_r, color_g, color_b, spectral_a);
-
-                      spectral_factor =
-                        CLAMP (spectral_blend_factor (pixel[ALPHA]), 0.0f, 1.0f);
-                      additive_factor = 1.0 - spectral_factor;
-                      if (additive_factor)
-                        {
-                          rgb[0] = src_term2 * color_r + dst_term * r;
-                          rgb[1] = src_term2 * color_g + dst_term * g;
-                          rgb[2] = src_term2 * color_b + dst_term * b;
-                        }
-
-                      if (spectral_factor && pixel[ALPHA] != 0)
-                        {
-                          /* Convert straightened tile pixel color to a spectral */
-                          rgb_to_spectral (r / pixel[ALPHA],
-                                           g / pixel[ALPHA],
-                                           b / pixel[ALPHA],
-                                           spectral_b);
-
-                          fac_a = src_term / (src_term + dst_term * pixel[ALPHA]);
-                          fac_a *= color_a;
-                          fac_b = 1.0 - fac_a;
-
-                          /* Mix input and tile pixel colors using WGM */
-                          for (int i = 0; i < 10; i++) {
-                            spectral_result[i] =
-                              fastpow (spectral_a[i], fac_a) * fastpow (spectral_b[i], fac_b);
-                          }
-
-                          /* Convert back to RGB */
-                          spectral_to_rgb (spectral_result, rgb_result);
-
-                          for (int i = 0; i < 3; i++)
-                            rgb[i] = (additive_factor * rgb[i]) +
-                              (spectral_factor * rgb_result[i] * out_term);
-                        }
-
-                      pixel[ALPHA] = out_term;
-                      r = rgb[0];
-                      g = rgb[1];
-                      b = rgb[2];
-                    }
+                  if (color_a == 1.0f)
+                    draw_dab_pixels_BlendMode_Normal_Paint (&alpha, pixel,
+                                                            color_r, color_g, color_b,
+                                                            normal_mode * opaque * paint);
+                  else
+                    draw_dab_pixels_BlendMode_Normal_and_Eraser_Paint (&alpha, pixel,
+                                                                       color_r, color_g,
+                                                                       color_b, color_a,
+                                                                       normal_mode * opaque * paint);
+
+                  if (lock_alpha > 0.0f && color_a != 0)
+                    draw_dab_pixels_BlendMode_LockAlpha_Paint (&alpha, pixel,
+                                                               color_r, color_g, color_b,
+                                                               lock_alpha * opaque * (1 - colorize) *
+                                                               (1 - posterize) * paint);
+
+                  r = pixel[RED];
+                  g = pixel[GREEN];
+                  b = pixel[BLUE];
                 }
 
               if (colorize > 0.0f && base_alpha > 0.0f)
@@ -665,17 +941,9 @@ gimp_mypaint_surface_draw_dab_2 (MyPaintSurface2 *base_surface,
                 }
 
               if (posterize > 0.0f)
-                {
-                  float post_r = ROUND (r * posterize_num) / posterize_num;
-                  float post_g = ROUND (g * posterize_num) / posterize_num;
-                  float post_b = ROUND (b * posterize_num) / posterize_num;
-                  float src_term = opaque * posterize;
-                  float dst_term = 1 - src_term;
-
-                  r = src_term * post_r + dst_term * r;
-                  g = src_term * post_g + dst_term * g;
-                  b = src_term * post_b + dst_term * b;
-                }
+                draw_dab_pixels_BlendMode_Posterize (&alpha, pixel,
+                                                     opaque, posterize_num);
+
 
               if (surface->options->no_erasing)
                 a = MAX (a, pixel[ALPHA]);
@@ -771,7 +1039,7 @@ gimp_mypaint_surface_get_color_2 (MyPaintSurface2 *base_surface,
     float sum_b = 0.0f;
     float sum_a = 0.0f;
 
-     /* Read in clamp mode to avoid transparency bleeding in at the edges */
+    /* Read in clamp mode to avoid transparency bleeding in at the edges */
     GeglBufferIterator *iter = gegl_buffer_iterator_new (surface->buffer, &dabRect, 0,
                                                          babl_format ("R'aG'aB'aA float"),
                                                          GEGL_BUFFER_READ,
@@ -818,10 +1086,10 @@ gimp_mypaint_surface_get_color_2 (MyPaintSurface2 *base_surface,
                 #ifdef _OPENMP
                 #pragma omp critical
                 #endif
-                get_color_pixels_accumulate (mask, pixel, &sum_weight,
+                get_color_pixels_accumulate (pixel_weight, pixel, &sum_weight,
                                              &sum_r, &sum_g, &sum_b, &sum_a, paint,
-                                             pixel_weight, sample_interval,
-                                             random_sample_rate, interval_counter);
+                                             sample_interval, random_sample_rate,
+                                             interval_counter);
 
                 interval_counter = (interval_counter + 1) % sample_interval;
 
@@ -923,6 +1191,8 @@ gimp_mypaint_surface_new (GeglBuffer         *buffer,
   s->parent.get_color     = gimp_mypaint_surface_get_color_wrapper;
   s->parent.end_atomic    = gimp_mypaint_surface_end_atomic_wrapper;
 
+  s->parent.destroy       = gimp_mypaint_surface_destroy;
+
   surface->component_mask = component_mask;
   surface->options        = options;
   surface->buffer         = g_object_ref (buffer);


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