[gimp] app: add gimp_drawable_calculate_histogram_async()



commit 1923fa8b44551828fc628a2cdcdd4b7f92758434
Author: Ell <ell_se yahoo com>
Date:   Fri May 11 11:52:49 2018 -0400

    app: add gimp_drawable_calculate_histogram_async()
    
    ... which is similar to gimp_drawable_calculate_histogram(),
    calculating the histogram asynchronously on a separate thread.
    
    Note that when calculating the histogram of the drawable's source
    node, the node is rendered synchronously on the main thread (if
    necessary), and the histogram of the result is calculated
    asynchronously on a separate thread.

 app/core/gimpdrawable-histogram.c |  110 +++++++++++++++++++++++++++++++------
 app/core/gimpdrawable-histogram.h |    9 ++-
 2 files changed, 99 insertions(+), 20 deletions(-)
---
diff --git a/app/core/gimpdrawable-histogram.c b/app/core/gimpdrawable-histogram.c
index 8b36de0..9b3713d 100644
--- a/app/core/gimpdrawable-histogram.c
+++ b/app/core/gimpdrawable-histogram.c
@@ -28,6 +28,7 @@
 #include "gegl/gimp-gegl-nodes.h"
 #include "gegl/gimptilehandlervalidate.h"
 
+#include "gimpasync.h"
 #include "gimpchannel.h"
 #include "gimpdrawable-filters.h"
 #include "gimpdrawable-histogram.h"
@@ -36,21 +37,30 @@
 #include "gimpprojectable.h"
 
 
-void
-gimp_drawable_calculate_histogram (GimpDrawable  *drawable,
-                                   GimpHistogram *histogram,
-                                   gboolean       with_filters)
+/*  local function prototypes  */
+
+static GimpAsync * gimp_drawable_calculate_histogram_internal (GimpDrawable  *drawable,
+                                                               GimpHistogram *histogram,
+                                                               gboolean       with_filters,
+                                                               gboolean       run_async);
+
+
+/*  private functions  */
+
+
+static GimpAsync *
+gimp_drawable_calculate_histogram_internal (GimpDrawable  *drawable,
+                                            GimpHistogram *histogram,
+                                            gboolean       with_filters,
+                                            gboolean       run_async)
 {
+  GimpAsync   *async = NULL;
   GimpImage   *image;
   GimpChannel *mask;
   gint         x, y, width, height;
 
-  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
-  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
-  g_return_if_fail (histogram != NULL);
-
   if (! gimp_item_mask_intersect (GIMP_ITEM (drawable), &x, &y, &width, &height))
-    return;
+    goto end;
 
   image = gimp_item_get_image (GIMP_ITEM (drawable));
   mask  = gimp_image_get_mask (image);
@@ -161,17 +171,41 @@ gimp_drawable_calculate_histogram (GimpDrawable  *drawable,
 
           gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
 
-          gimp_histogram_calculate (histogram, buffer,
-                                    GEGL_RECTANGLE (x, y, width, height),
-                                    gimp_drawable_get_buffer (GIMP_DRAWABLE (mask)),
-                                    GEGL_RECTANGLE (x + off_x, y + off_y,
-                                                    width, height));
+          if (run_async)
+            {
+              async = gimp_histogram_calculate_async (
+                histogram, buffer,
+                GEGL_RECTANGLE (x, y, width, height),
+                gimp_drawable_get_buffer (GIMP_DRAWABLE (mask)),
+                GEGL_RECTANGLE (x + off_x, y + off_y,
+                                width, height));
+            }
+          else
+            {
+              gimp_histogram_calculate (
+                histogram, buffer,
+                GEGL_RECTANGLE (x, y, width, height),
+                gimp_drawable_get_buffer (GIMP_DRAWABLE (mask)),
+                GEGL_RECTANGLE (x + off_x, y + off_y,
+                                width, height));
+            }
         }
       else
         {
-          gimp_histogram_calculate (histogram, buffer,
-                                    GEGL_RECTANGLE (x, y, width, height),
-                                    NULL, NULL);
+          if (run_async)
+            {
+              async = gimp_histogram_calculate_async (
+                histogram, buffer,
+                GEGL_RECTANGLE (x, y, width, height),
+                NULL, NULL);
+            }
+          else
+            {
+              gimp_histogram_calculate (
+                histogram, buffer,
+                GEGL_RECTANGLE (x, y, width, height),
+                NULL, NULL);
+            }
         }
 
       if (projectable)
@@ -179,4 +213,46 @@ gimp_drawable_calculate_histogram (GimpDrawable  *drawable,
 
       g_object_unref (buffer);
     }
+
+end:
+  if (run_async && ! async)
+    {
+      async = gimp_async_new ();
+
+      gimp_async_finish (async, NULL);
+    }
+
+  return async;
+}
+
+
+/*  public functions  */
+
+
+void
+gimp_drawable_calculate_histogram (GimpDrawable  *drawable,
+                                   GimpHistogram *histogram,
+                                   gboolean       with_filters)
+{
+  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
+  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
+  g_return_if_fail (histogram != NULL);
+
+  gimp_drawable_calculate_histogram_internal (drawable,
+                                              histogram, with_filters,
+                                              FALSE);
+}
+
+GimpAsync *
+gimp_drawable_calculate_histogram_async (GimpDrawable  *drawable,
+                                         GimpHistogram *histogram,
+                                         gboolean       with_filters)
+{
+  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
+  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
+  g_return_val_if_fail (histogram != NULL, NULL);
+
+  return gimp_drawable_calculate_histogram_internal (drawable,
+                                                     histogram, with_filters,
+                                                     TRUE);
 }
diff --git a/app/core/gimpdrawable-histogram.h b/app/core/gimpdrawable-histogram.h
index 49ef52e..2794e0a 100644
--- a/app/core/gimpdrawable-histogram.h
+++ b/app/core/gimpdrawable-histogram.h
@@ -21,9 +21,12 @@
 #define __GIMP_DRAWABLE_HISTOGRAM_H__
 
 
-void   gimp_drawable_calculate_histogram (GimpDrawable  *drawable,
-                                          GimpHistogram *histogram,
-                                          gboolean       with_filters);
+void        gimp_drawable_calculate_histogram       (GimpDrawable  *drawable,
+                                                     GimpHistogram *histogram,
+                                                     gboolean       with_filters);
+GimpAsync * gimp_drawable_calculate_histogram_async (GimpDrawable  *drawable,
+                                                     GimpHistogram *histogram,
+                                                     gboolean       with_filters);
 
 
 #endif /* __GIMP_HISTOGRAM_H__ */


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