[gimp] app: add GimpObjectQueue



commit 3ee5054eb7591e559bb79a038a744ad5d69b7553
Author: Ell <ell_se yahoo com>
Date:   Sun Mar 25 10:04:35 2018 -0400

    app: add GimpObjectQueue
    
    GimpObjectQueue implements a queue of GimpObjects.  It derives from
    GimpSubProgress, and hence can be used as a GimpProgress object.
    It keeps track of the total memsize of the objects that were
    pushed-to and popped-from the queue, and uses these numbers to set
    the corresponding subrange of the progress object when an object is
    popped.
    
    This provides an easy way to perform an operation on a set of
    objects, correctly reporting progress based on the relative sizes
    of the objects, which is assumed to be a good estimate of the
    relative cost of processing each object.

 app/core/Makefile.am       |    2 +
 app/core/core-types.h      |    1 +
 app/core/gimpobjectqueue.c |  198 ++++++++++++++++++++++++++++++++++++++++++++
 app/core/gimpobjectqueue.h |   66 +++++++++++++++
 4 files changed, 267 insertions(+), 0 deletions(-)
---
diff --git a/app/core/Makefile.am b/app/core/Makefile.am
index a751ffb..0e19108 100644
--- a/app/core/Makefile.am
+++ b/app/core/Makefile.am
@@ -327,6 +327,8 @@ libappcore_a_sources = \
        gimpmybrush-private.h                   \
        gimpobject.c                            \
        gimpobject.h                            \
+       gimpobjectqueue.c                       \
+       gimpobjectqueue.h                       \
        gimppaintinfo.c                         \
        gimppaintinfo.h                         \
        gimppattern.c                           \
diff --git a/app/core/core-types.h b/app/core/core-types.h
index a7f6f33..79d34af 100644
--- a/app/core/core-types.h
+++ b/app/core/core-types.h
@@ -178,6 +178,7 @@ typedef struct _GimpHistogram       GimpHistogram;
 typedef struct _GimpIdTable         GimpIdTable;
 typedef struct _GimpImagefile       GimpImagefile;
 typedef struct _GimpInterpreterDB   GimpInterpreterDB;
+typedef struct _GimpObjectQueue     GimpObjectQueue;
 typedef struct _GimpParasiteList    GimpParasiteList;
 typedef struct _GimpPdbProgress     GimpPdbProgress;
 typedef struct _GimpProjection      GimpProjection;
diff --git a/app/core/gimpobjectqueue.c b/app/core/gimpobjectqueue.c
new file mode 100644
index 0000000..42643b6
--- /dev/null
+++ b/app/core/gimpobjectqueue.c
@@ -0,0 +1,198 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <glib-object.h>
+
+#include "core-types.h"
+
+#include "gimpcontainer.h"
+#include "gimpobject.h"
+#include "gimpobjectqueue.h"
+#include "gimpprogress.h"
+
+
+typedef struct
+{
+  GimpObject *object;
+  gint64      memsize;
+} Item;
+
+
+static void   gimp_object_queue_dispose      (GObject         *object);
+
+static void   gimp_object_queue_push_swapped (gpointer         object,
+                                              GimpObjectQueue *queue);
+
+static Item * gimp_object_queue_item_new     (GimpObject      *object);
+static void   gimp_object_queue_item_free    (Item            *item);
+
+
+G_DEFINE_TYPE (GimpObjectQueue, gimp_object_queue, GIMP_TYPE_SUB_PROGRESS);
+
+#define parent_class gimp_object_queue_parent_class
+
+
+/*  private functions  */
+
+
+static void
+gimp_object_queue_class_init (GimpObjectQueueClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = gimp_object_queue_dispose;
+}
+
+static void
+gimp_object_queue_init (GimpObjectQueue *queue)
+{
+  g_queue_init (&queue->items);
+
+  queue->processed_memsize = 0;
+  queue->total_memsize     = 0;
+}
+
+static void
+gimp_object_queue_dispose (GObject *object)
+{
+  gimp_object_queue_clear (GIMP_OBJECT_QUEUE (object));
+
+  G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static void
+gimp_object_queue_push_swapped (gpointer         object,
+                                GimpObjectQueue *queue)
+{
+  gimp_object_queue_push (queue, object);
+}
+
+static Item *
+gimp_object_queue_item_new (GimpObject *object)
+{
+  Item *item = g_slice_new (Item);
+
+  item->object  = object;
+  item->memsize = gimp_object_get_memsize (object, NULL);
+
+  return item;
+}
+
+static void
+gimp_object_queue_item_free (Item *item)
+{
+  g_slice_free (Item, item);
+}
+
+
+/*  public functions  */
+
+
+GimpObjectQueue *
+gimp_object_queue_new (GimpProgress *progress)
+{
+  g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
+
+  return g_object_new (GIMP_TYPE_OBJECT_QUEUE,
+                       "progress", progress,
+                       NULL);
+}
+
+void
+gimp_object_queue_clear (GimpObjectQueue *queue)
+{
+  Item *item;
+
+  g_return_if_fail (GIMP_IS_OBJECT_QUEUE (queue));
+
+  while ((item = g_queue_pop_head (&queue->items)))
+    gimp_object_queue_item_free (item);
+
+  queue->processed_memsize = 0;
+  queue->total_memsize     = 0;
+
+  gimp_sub_progress_set_range (GIMP_SUB_PROGRESS (queue), 0.0, 1.0);
+}
+
+void
+gimp_object_queue_push (GimpObjectQueue *queue,
+                        gpointer         object)
+{
+  Item *item;
+
+  g_return_if_fail (GIMP_IS_OBJECT_QUEUE (queue));
+  g_return_if_fail (GIMP_IS_OBJECT (object));
+
+  item = gimp_object_queue_item_new (GIMP_OBJECT (object));
+
+  g_queue_push_tail (&queue->items, item);
+
+  queue->total_memsize += item->memsize;
+}
+
+void
+gimp_object_queue_push_container (GimpObjectQueue *queue,
+                                  GimpContainer   *container)
+{
+  g_return_if_fail (GIMP_IS_OBJECT_QUEUE (queue));
+  g_return_if_fail (GIMP_IS_CONTAINER (container));
+
+  gimp_container_foreach (container,
+                          (GFunc) gimp_object_queue_push_swapped,
+                          queue);
+}
+
+void
+gimp_object_queue_push_list (GimpObjectQueue *queue,
+                             GList           *list)
+{
+  g_return_if_fail (GIMP_IS_OBJECT_QUEUE (queue));
+
+  g_list_foreach (list,
+                  (GFunc) gimp_object_queue_push_swapped,
+                  queue);
+}
+
+gpointer
+gimp_object_queue_pop (GimpObjectQueue *queue)
+{
+  Item       *item;
+  GimpObject *object;
+
+  g_return_val_if_fail (GIMP_IS_OBJECT_QUEUE (queue), NULL);
+
+  item = g_queue_pop_head (&queue->items);
+
+  if (! item)
+    return NULL;
+
+  object = item->object;
+
+  gimp_sub_progress_set_range (GIMP_SUB_PROGRESS (queue),
+                               (gdouble) queue->processed_memsize  /
+                               (gdouble) queue->total_memsize,
+                               (gdouble) (queue->processed_memsize +
+                                          item->memsize)              /
+                               (gdouble) queue->total_memsize);
+  queue->processed_memsize += item->memsize;
+
+  gimp_object_queue_item_free (item);
+
+  return object;
+}
diff --git a/app/core/gimpobjectqueue.h b/app/core/gimpobjectqueue.h
new file mode 100644
index 0000000..40f1555
--- /dev/null
+++ b/app/core/gimpobjectqueue.h
@@ -0,0 +1,66 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_OBJECT_QUEUE_H__
+#define __GIMP_OBJECT_QUEUE_H__
+
+
+#include "gimpsubprogress.h"
+
+
+#define GIMP_TYPE_OBJECT_QUEUE            (gimp_object_queue_get_type ())
+#define GIMP_OBJECT_QUEUE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_OBJECT_QUEUE, 
GimpObjectQueue))
+#define GIMP_OBJECT_QUEUE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_OBJECT_QUEUE, 
GimpObjectQueueClass))
+#define GIMP_IS_OBJECT_QUEUE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_OBJECT_QUEUE))
+#define GIMP_IS_OBJECT_QUEUE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_OBJECT_QUEUE))
+#define GIMP_OBJECT_QUEUE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_OBJECT_QUEUE, 
GimpObjectQueueClass))
+
+
+typedef struct _GimpObjectQueueClass GimpObjectQueueClass;
+
+struct _GimpObjectQueue
+{
+  GimpSubProgress  parent_instance;
+
+  GQueue           items;
+  gint64           processed_memsize;
+  gint64           total_memsize;
+};
+
+struct _GimpObjectQueueClass
+{
+  GimpSubProgressClass  parent_class;
+};
+
+
+GType             gimp_object_queue_get_type       (void) G_GNUC_CONST;
+
+GimpObjectQueue * gimp_object_queue_new            (GimpProgress    *progress);
+
+void              gimp_object_queue_clear          (GimpObjectQueue *queue);
+
+void              gimp_object_queue_push           (GimpObjectQueue *queue,
+                                                    gpointer         object);
+void              gimp_object_queue_push_container (GimpObjectQueue *queue,
+                                                    GimpContainer   *container);
+void              gimp_object_queue_push_list      (GimpObjectQueue *queue,
+                                                    GList           *list);
+
+gpointer          gimp_object_queue_pop            (GimpObjectQueue *queue);
+
+
+#endif /* __GIMP_OBJECT_QUEUE_H__ */


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