[gegl] oilify: clamp input pixel values to avoid nan values in the output



commit 33ca55b5546f368b75f490c1cbc257d38d0ec0fd
Author: Thomas Manni <thomas manni free fr>
Date:   Thu Apr 14 20:20:39 2022 +0200

    oilify: clamp input pixel values to avoid nan values in the output

 opencl/oilify.cl                 | 12 ++++++++----
 operations/common-gpl3+/oilify.c | 13 +++++++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)
---
diff --git a/opencl/oilify.cl b/opencl/oilify.cl
index 29237bf35..659e829f1 100644
--- a/opencl/oilify.cl
+++ b/opencl/oilify.cl
@@ -29,10 +29,10 @@ kernel void kernel_oilify(global float4 *in,
         if (i*i + j*j <= radius_sq)
           {
             temp_pixel = in[x + i + (y + j) * src_width];
-            hist[(int)(temp_pixel.x * (intensities - 1))].x+=1;
-            hist[(int)(temp_pixel.y * (intensities - 1))].y+=1;
-            hist[(int)(temp_pixel.z * (intensities - 1))].z+=1;
-            hist[(int)(temp_pixel.w * (intensities - 1))].w+=1;
+            hist[(int)(clamp(temp_pixel.x, 0.f, 1.f) * (intensities - 1))].x+=1;
+            hist[(int)(clamp(temp_pixel.y, 0.f, 1.f) * (intensities - 1))].y+=1;
+            hist[(int)(clamp(temp_pixel.z, 0.f, 1.f) * (intensities - 1))].z+=1;
+            hist[(int)(clamp(temp_pixel.w, 0.f, 1.f) * (intensities - 1))].w+=1;
           }
       }
   }
@@ -89,6 +89,10 @@ kernel void kernel_oilify_inten(global float4 *in,
         if (i*i + j*j <= radius_sq)
           {
             temp_pixel = in[x + i + (y + j) * src_width];
+            temp_pixel.x = clamp(temp_pixel.x, 0.f, 1.f);
+            temp_pixel.y = clamp(temp_pixel.y, 0.f, 1.f);
+            temp_pixel.z = clamp(temp_pixel.z, 0.f, 1.f);
+            temp_pixel.w = clamp(temp_pixel.w, 0.f, 1.f);
             /*Calculate intensity on the fly, GPU does it fast*/
             intensity = (int)((0.299 * temp_pixel.x
                       +0.587 * temp_pixel.y
diff --git a/operations/common-gpl3+/oilify.c b/operations/common-gpl3+/oilify.c
index 45f258f79..1a4406d69 100644
--- a/operations/common-gpl3+/oilify.c
+++ b/operations/common-gpl3+/oilify.c
@@ -54,6 +54,17 @@ property_boolean (use_inten, _("Intensity Mode"), TRUE)
 
 #define NUM_INTENSITIES       256
 
+static void
+clamp_buffer_values (gfloat  *buf,
+                     gint     n_components,
+                     gint     n_pixels)
+{
+  gint  i;
+
+  for (i = 0; i < n_pixels * n_components; i++)
+    buf[i] = CLAMP (buf[i], 0.f, 1.f);
+}
+
 /* Get the pixel from x, y offset from the center pixel src_pix */
 
 static void
@@ -479,6 +490,7 @@ process (GeglOperation       *operation,
 
   gegl_buffer_get (input, &src_rect, 1.0, format, src_buf,
                    GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);
+  clamp_buffer_values (src_buf, 4, total_pixels);
 
   if (o->use_inten)
     {
@@ -486,6 +498,7 @@ process (GeglOperation       *operation,
 
       gegl_buffer_get (input, &src_rect, 1.0, y_format, inten_buf,
                        GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);
+      clamp_buffer_values (inten_buf, 1, total_pixels);
     }
 
   if (aux)


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