[gimp] plug-ins: port align-layers to new API + libgimp objects.



commit f75b63fa80e3635572ba34b8a0357be8c4f78382
Author: Jehan <jehan girinstud io>
Date:   Wed Aug 28 11:50:38 2019 +0200

    plug-ins: port align-layers to new API + libgimp objects.
    
    Note: there were 2 additional arguments which were totally unused in the
    existing implementation. So I commented them out. Anyone is welcome to
    actually implement their usage.

 plug-ins/common/Makefile.am    |   2 -
 plug-ins/common/align-layers.c | 394 ++++++++++++++++++++++-------------------
 plug-ins/common/plugin-defs.pl |   2 +-
 3 files changed, 214 insertions(+), 184 deletions(-)
---
diff --git a/plug-ins/common/Makefile.am b/plug-ins/common/Makefile.am
index fab2cc15ca..c75d475cb0 100644
--- a/plug-ins/common/Makefile.am
+++ b/plug-ins/common/Makefile.am
@@ -225,8 +225,6 @@ install-%: %
          $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(INSTALL_PROGRAM) $$p 
$(DESTDIR)$(gimpplugindir)/plug-ins/$$p/$$f || exit 1; \
        else :; fi
 
-align_layers_CPPFLAGS = $(AM_CPPFLAGS) -DGIMP_DEPRECATED_REPLACE_NEW_API
-
 align_layers_SOURCES = \
        align-layers.c
 
diff --git a/plug-ins/common/align-layers.c b/plug-ins/common/align-layers.c
index 90dbffb8b8..1edc3686c3 100644
--- a/plug-ins/common/align-layers.c
+++ b/plug-ins/common/align-layers.c
@@ -71,51 +71,65 @@ typedef struct
   gint base_y;
 } AlignData;
 
+typedef struct _AlignLayers      AlignLayers;
+typedef struct _AlignLayersClass AlignLayersClass;
 
-static void     query   (void);
-static void     run     (const gchar      *name,
-                         gint              nparams,
-                         const GimpParam  *param,
-                         gint             *nreturn_vals,
-                         GimpParam       **return_vals);
-
-/* Main function */
-static GimpPDBStatusType align_layers                (gint32  image_id);
+struct _AlignLayers
+{
+  GimpPlugIn      parent_instance;
+};
 
-/* Helpers and internal functions */
-static gint      align_layers_count_visibles_layers  (gint     *layers,
-                                                      gint      length);
-static gint      align_layers_find_last_layer        (gint     *layers,
-                                                      gint      layers_num,
-                                                      gboolean *found);
-static gint      align_layers_spread_visibles_layers (gint     *layers,
-                                                      gint      layers_num,
-                                                      gint     *layers_array);
-static gint    * align_layers_spread_image           (gint32    image_id,
-                                                      gint     *layer_num);
-static gint      align_layers_find_background        (gint32    image_id);
-static AlignData align_layers_gather_data            (gint     *layers,
-                                                      gint      layer_num,
-                                                      gint      background);
-static void      align_layers_perform_alignment      (gint     *layers,
-                                                      gint      layer_num,
-                                                      AlignData data);
-static void      align_layers_get_align_offsets      (gint32    drawable_id,
-                                                      gint     *x,
-                                                      gint     *y);
-static gint      align_layers_dialog                 (void);
-
-
-
-const GimpPlugInInfo PLUG_IN_INFO =
+struct _AlignLayersClass
 {
-  NULL,  /* init_proc  */
-  NULL,  /* quit_proc  */
-  query, /* query_proc */
-  run,   /* run_proc   */
+  GimpPlugInClass parent_class;
 };
 
 
+#define ALIGN_LAYERS_TYPE  (align_layers_get_type ())
+#define ALIGN_LAYERS (obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ALIGN_LAYERS_TYPE, AlignLayers))
+
+GType                   align_layers_get_type               (void) G_GNUC_CONST;
+
+static GList          * align_layers_query_procedures       (GimpPlugIn           *plug_in);
+static GimpProcedure  * align_layers_create_procedure       (GimpPlugIn           *plug_in,
+                                                             const gchar          *name);
+
+static GimpValueArray * align_layers_run                    (GimpProcedure        *procedure,
+                                                             GimpRunMode           run_mode,
+                                                             GimpImage            *image,
+                                                             GimpDrawable         *drawable,
+                                                             const GimpValueArray *args,
+                                                             gpointer              run_data);
+
+
+/* Main function */
+static GimpPDBStatusType align_layers                       (GimpImage            *image);
+
+/* Helpers and internal functions */
+static gint             align_layers_count_visibles_layers  (GList                *layers);
+static GimpLayer      * align_layers_find_last_layer        (GList                *layers,
+                                                             gboolean             *found);
+static gint             align_layers_spread_visibles_layers (GList                *layers,
+                                                             GimpLayer           **layers_array);
+static GimpLayer     ** align_layers_spread_image           (GimpImage            *image,
+                                                             gint                 *layer_num);
+static GimpLayer      * align_layers_find_background        (GimpImage            *image);
+static AlignData        align_layers_gather_data            (GimpLayer           **layers,
+                                                             gint                  layer_num,
+                                                             GimpLayer            *background);
+static void             align_layers_perform_alignment      (GimpLayer           **layers,
+                                                             gint                  layer_num,
+                                                             AlignData             data);
+static void             align_layers_get_align_offsets      (GimpDrawable         *drawable,
+                                                             gint                 *x,
+                                                             gint                 *y);
+static gint             align_layers_dialog                 (void);
+
+
+G_DEFINE_TYPE (AlignLayers, align_layers, GIMP_TYPE_PLUG_IN)
+
+GIMP_MAIN (ALIGN_LAYERS_TYPE)
+
 /* dialog variables */
 typedef struct
 {
@@ -139,78 +153,105 @@ static ValueType VALS =
   10
 };
 
+static void
+align_layers_class_init (AlignLayersClass *klass)
+{
+  GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
 
-MAIN ()
+  plug_in_class->query_procedures = align_layers_query_procedures;
+  plug_in_class->create_procedure = align_layers_create_procedure;
+}
 
 static void
-query (void)
+align_layers_init (AlignLayers *film)
 {
-  static const GimpParamDef args [] =
-  {
-    { GIMP_PDB_INT32,    "run-mode",             "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) 
}"},
-    { GIMP_PDB_IMAGE,    "image",                "Input image"},
-    { GIMP_PDB_DRAWABLE, "drawable",             "Input drawable (not used)"},
-    { GIMP_PDB_INT32,    "link-after-alignment", "Link the visible layers after alignment { TRUE, FALSE }"},
-    { GIMP_PDB_INT32,    "use-bottom",           "use the bottom layer as the base of alignment { TRUE, 
FALSE }"}
-  };
-
-  gimp_install_procedure (PLUG_IN_PROC,
-                          N_("Align all visible layers of the image"),
-                          "Align visible layers",
-                          "Shuji Narazaki <narazaki InetQ or jp>",
-                          "Shuji Narazaki",
-                          "1997",
-                          N_("Align Visi_ble Layers..."),
-                          "RGB*,GRAY*,INDEXED*",
-                          GIMP_PLUGIN,
-                          G_N_ELEMENTS (args), 0,
-                          args, NULL);
-
-  gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Image/Arrange");
 }
 
-static void
-run (const gchar      *name,
-     gint              nparams,
-     const GimpParam  *param,
-     gint             *nreturn_vals,
-     GimpParam       **return_vals)
+static GList *
+align_layers_query_procedures (GimpPlugIn *plug_in)
 {
-  static GimpParam  values[2];
-  GimpPDBStatusType status = GIMP_PDB_EXECUTION_ERROR;
-  GimpRunMode       run_mode;
-  gint              image_id, layer_num;
-  gint             *layers;
+  return g_list_append (NULL, g_strdup (PLUG_IN_PROC));
+}
 
-  run_mode = param[0].data.d_int32;
-  image_id = param[1].data.d_int32;
+static GimpProcedure *
+align_layers_create_procedure (GimpPlugIn  *plug_in,
+                               const gchar *name)
+{
+  GimpProcedure *procedure = NULL;
 
-  INIT_I18N ();
+  if (! strcmp (name, PLUG_IN_PROC))
+    {
+      procedure = gimp_image_procedure_new (plug_in, name, GIMP_PLUGIN,
+                                            align_layers_run, NULL, NULL);
+
+      gimp_procedure_set_image_types (procedure, "*");
+
+      gimp_procedure_set_menu_label (procedure, N_("Align Visi_ble Layers..."));
+      gimp_procedure_add_menu_path (procedure, "<Image>/Image/Arrange");
+
+      gimp_procedure_set_documentation (procedure,
+                                        N_("Align all visible layers of the image"),
+                                        "Align visible layers",
+                                        name);
+      gimp_procedure_set_attribution (procedure,
+                                      "Shuji Narazaki <narazaki InetQ or jp>",
+                                      "Shuji Narazaki",
+                                      "1997");
+
+      /* TODO: these 2 arguments existed in the original procedure but
+       * were actually unused. If we want to keep them, let's at least
+       * implement the documented behavior, or else let's just drop
+       * them.
+       */
+      /*
+      GIMP_PROC_ARG_BOOLEAN (procedure,
+                             "link-after-alignment",
+                             "Link the visible layers after alignment",
+                             FALSE,
+                             G_PARAM_READWRITE);
+      GIMP_PROC_ARG_BOOLEAN (procedure,
+                             "use-bottom",
+                             "use the bottom layer as the base of alignment",
+                             TRUE,
+                             G_PARAM_READWRITE);
+                             */
+    }
 
-  *nreturn_vals = 1;
-  *return_vals  = values;
+  return procedure;
+}
 
-  values[0].type          = GIMP_PDB_STATUS;
-  values[0].data.d_status = status;
+static GimpValueArray *
+align_layers_run (GimpProcedure        *procedure,
+                  GimpRunMode           run_mode,
+                  GimpImage            *image,
+                  GimpDrawable         *drawable,
+                  const GimpValueArray *args,
+                  gpointer              run_data)
+{
+  GimpValueArray    *return_vals = NULL;
+  GimpPDBStatusType  status = GIMP_PDB_EXECUTION_ERROR;
+  GError            *error  = NULL;
+  GList             *layers;
+  gint               layer_num;
+
+  INIT_I18N ();
 
   switch ( run_mode )
     {
     case GIMP_RUN_INTERACTIVE:
-      layers = gimp_image_get_layers (image_id, &layer_num);
-      layer_num = align_layers_count_visibles_layers (layers,
-                                                      layer_num);
-      g_free (layers);
+      layers = gimp_image_list_layers (image);
+      layer_num = align_layers_count_visibles_layers (layers);
+      g_list_free (layers);
       if (layer_num < 2)
         {
-          *nreturn_vals = 2;
-          values[1].type          = GIMP_PDB_STRING;
-          values[1].data.d_string = _("There are not enough layers to align.");
-          return;
+          error = g_error_new_literal (0, 0,
+                                       _("There are not enough layers to align."));
+          break;
         }
       gimp_get_data (PLUG_IN_PROC, &VALS);
       VALS.grid_size = MAX (VALS.grid_size, 1);
       if (! align_layers_dialog ())
-        return;
+        status = GIMP_PDB_CANCEL;
       break;
 
     case GIMP_RUN_NONINTERACTIVE:
@@ -221,36 +262,42 @@ run (const gchar      *name,
       break;
     }
 
-  status = align_layers (image_id);
+  if (status != GIMP_PDB_CANCEL)
+    {
+      status = align_layers (image);
 
-  if (run_mode != GIMP_RUN_NONINTERACTIVE)
-    gimp_displays_flush ();
+      if (run_mode != GIMP_RUN_NONINTERACTIVE)
+        gimp_displays_flush ();
+    }
 
   if (run_mode == GIMP_RUN_INTERACTIVE && status == GIMP_PDB_SUCCESS)
     gimp_set_data (PLUG_IN_PROC, &VALS, sizeof (ValueType));
 
-  values[0].data.d_status = status;
+  return_vals = gimp_procedure_new_return_values (procedure, status,
+                                                  error);
+
+  return return_vals;
 }
 
 /*
  * Main function
  */
 static GimpPDBStatusType
-align_layers (gint32 image_id)
+align_layers (GimpImage *image)
 {
   gint       layer_num  = 0;
-  gint      *layers     = NULL;
-  gint       background = 0;
+  GimpLayer  **layers     = NULL;
+  GimpLayer   *background = 0;
   AlignData  data;
 
-  layers = align_layers_spread_image (image_id, &layer_num);
+  layers = align_layers_spread_image (image, &layer_num);
   if (layer_num < 2)
     {
       g_free (layers);
       return GIMP_PDB_EXECUTION_ERROR;
     }
 
-  background = align_layers_find_background (image_id);
+  background = align_layers_find_background (image);
 
   /* If we want to ignore the bottom layer and if it's visible */
   if (VALS.ignore_bottom && background == layers[layer_num - 1])
@@ -262,13 +309,13 @@ align_layers (gint32 image_id)
                                    layer_num,
                                    background);
 
-  gimp_image_undo_group_start (image_id);
+  gimp_image_undo_group_start (image);
 
   align_layers_perform_alignment (layers,
                                   layer_num,
                                   data);
 
-  gimp_image_undo_group_end (image_id);
+  gimp_image_undo_group_end (image);
 
   g_free (layers);
 
@@ -279,58 +326,52 @@ align_layers (gint32 image_id)
  * Find the bottommost layer, visible or not
  * The image must contain at least one layer.
  */
-static gint
-align_layers_find_last_layer (gint     *layers,
-                              gint      layers_num,
+static GimpLayer *
+align_layers_find_last_layer (GList    *layers,
                               gboolean *found)
 {
-  gint i;
+  GList *last = g_list_last (layers);
 
-  for (i = layers_num - 1; i >= 0; i--)
+  for (; last; last = last->prev)
     {
-      gint item = layers[i];
+      GimpItem *item = last->data;
 
       if (gimp_item_is_group (item))
         {
-          gint *children;
-          gint  children_num;
-          gint  last_layer;
+          GList     *children;
+          GimpLayer *last_layer;
 
-          children = gimp_item_get_children (item, &children_num);
+          children = gimp_item_list_children (item);
           last_layer = align_layers_find_last_layer (children,
-                                                     children_num,
                                                      found);
-          g_free (children);
+          g_list_free (children);
           if (*found)
             return last_layer;
         }
       else if (gimp_item_is_layer (item))
         {
           *found = TRUE;
-          return item;
+          return GIMP_LAYER (item);
         }
     }
 
   /* should never happen */
-  return -1;
+  return NULL;
 }
 
 /*
  * Return the bottom layer.
  */
-static gint
-align_layers_find_background (gint32 image_id)
+static GimpLayer *
+align_layers_find_background (GimpImage *image)
 {
-  gint    *layers;
-  gint     layers_num;
-  gint     background;
-  gboolean found = FALSE;
-
-  layers = gimp_image_get_layers (image_id, &layers_num);
-  background = align_layers_find_last_layer (layers,
-                                             layers_num,
-                                             &found);
-  g_free (layers);
+  GList     *layers;
+  GimpLayer *background;
+  gboolean   found = FALSE;
+
+  layers = gimp_image_list_layers (image);
+  background = align_layers_find_last_layer (layers, &found);
+  g_list_free (layers);
 
   return background;
 }
@@ -340,33 +381,30 @@ align_layers_find_background (gint32 image_id)
  * layers_array needs to be allocated before the call
  */
 static gint
-align_layers_spread_visibles_layers (gint *layers,
-                                     gint  layers_num,
-                                     gint *layers_array)
+align_layers_spread_visibles_layers (GList      *layers,
+                                     GimpLayer **layers_array)
 {
-  gint i;
-  gint index = 0;
+  GList *iter;
+  gint   index = 0;
 
-  for (i = 0; i < layers_num; i++)
+  for (iter = layers; iter; iter = iter->next)
     {
-      gint item = layers[i];
+      GimpItem *item = iter->data;
 
       if (gimp_item_get_visible (item))
         {
           if (gimp_item_is_group (item))
             {
-              gint *children;
-              gint  children_num;
+              GList *children;
 
-              children = gimp_item_get_children (item, &children_num);
+              children = gimp_item_list_children (item);
               index += align_layers_spread_visibles_layers (children,
-                                                            children_num,
                                                             &(layers_array[index]));
-              g_free (children);
+              g_list_free (children);
             }
           else if (gimp_item_is_layer (item))
             {
-              layers_array[index] = item;
+              layers_array[index] = GIMP_LAYER (item);
               index++;
             }
         }
@@ -378,50 +416,44 @@ align_layers_spread_visibles_layers (gint *layers,
 /*
  * Return a contiguous array of all visible layers
  */
-static gint *
-align_layers_spread_image (gint32  image_id,
-                           gint   *layer_num)
+static GimpLayer **
+align_layers_spread_image (GimpImage *image,
+                           gint      *layer_num)
 {
-  gint *layers;
-  gint *layers_array;
-  gint  layer_num_loc;
+  GList      *layers;
+  GimpLayer **layers_array;
 
-  layers = gimp_image_get_layers (image_id, &layer_num_loc);
-  *layer_num = align_layers_count_visibles_layers (layers,
-                                                   layer_num_loc);
+  layers = gimp_image_list_layers (image);
+  *layer_num = align_layers_count_visibles_layers (layers);
 
-  layers_array = g_malloc (sizeof (gint) * *layer_num);
+  layers_array = g_malloc (sizeof (GimpLayer *) * *layer_num);
 
   align_layers_spread_visibles_layers (layers,
-                                       layer_num_loc,
                                        layers_array);
-  g_free (layers);
+  g_list_free (layers);
 
   return layers_array;
 }
 
 static gint
-align_layers_count_visibles_layers (gint *layers,
-                                    gint  length)
+align_layers_count_visibles_layers (GList *layers)
 {
-  gint i;
-  gint count = 0;
+  GList *iter;
+  gint   count = 0;
 
-  for (i = 0; i<length; i++)
+  for (iter = layers; iter; iter = iter->next)
     {
-      gint item = layers[i];
+      GimpItem *item = iter->data;
 
       if (gimp_item_get_visible (item))
         {
           if (gimp_item_is_group (item))
             {
-              gint *children;
-              gint  children_num;
+              GList *children;
 
-              children = gimp_item_get_children (item, &children_num);
-              count += align_layers_count_visibles_layers (children,
-                                                           children_num);
-              g_free (children);
+              children = gimp_item_list_children (item);
+              count += align_layers_count_visibles_layers (children);
+              g_list_free (children);
             }
           else if (gimp_item_is_layer (item))
             {
@@ -434,9 +466,9 @@ align_layers_count_visibles_layers (gint *layers,
 }
 
 static AlignData
-align_layers_gather_data (gint *layers,
-                          gint  layer_num,
-                          gint  background)
+align_layers_gather_data (GimpLayer **layers,
+                          gint        layer_num,
+                          GimpLayer  *background)
 {
   AlignData data;
   gint   min_x = G_MAXINT;
@@ -457,9 +489,9 @@ align_layers_gather_data (gint *layers,
   /* 0 is the top layer */
   for (index = 0; index < layer_num; index++)
     {
-      gimp_drawable_offsets (layers[index], &orig_x, &orig_y);
+      gimp_drawable_offsets (GIMP_DRAWABLE (layers[index]), &orig_x, &orig_y);
 
-      align_layers_get_align_offsets (layers[index],
+      align_layers_get_align_offsets (GIMP_DRAWABLE (layers[index]),
                                       &offset_x,
                                       &offset_y);
       orig_x += offset_x;
@@ -473,9 +505,9 @@ align_layers_gather_data (gint *layers,
 
   if (VALS.base_is_bottom_layer)
     {
-      gimp_drawable_offsets (background, &orig_x, &orig_y);
+      gimp_drawable_offsets (GIMP_DRAWABLE (background), &orig_x, &orig_y);
 
-      align_layers_get_align_offsets (background,
+      align_layers_get_align_offsets (GIMP_DRAWABLE (background),
                                       &offset_x,
                                       &offset_y);
       orig_x += offset_x;
@@ -504,9 +536,9 @@ align_layers_gather_data (gint *layers,
  * according to data.
  */
 static void
-align_layers_perform_alignment (gint      *layers,
-                                gint       layer_num,
-                                AlignData  data)
+align_layers_perform_alignment (GimpLayer **layers,
+                                gint        layer_num,
+                                AlignData   data)
 {
   gint index;
 
@@ -519,9 +551,9 @@ align_layers_perform_alignment (gint      *layers,
       gint offset_x;
       gint offset_y;
 
-      gimp_drawable_offsets (layers[index], &orig_x, &orig_y);
+      gimp_drawable_offsets (GIMP_DRAWABLE (layers[index]), &orig_x, &orig_y);
 
-      align_layers_get_align_offsets (layers[index],
+      align_layers_get_align_offsets (GIMP_DRAWABLE (layers[index]),
                                       &offset_x,
                                       &offset_y);
       switch (VALS.h_style)
@@ -571,12 +603,12 @@ align_layers_perform_alignment (gint      *layers,
 }
 
 static void
-align_layers_get_align_offsets (gint32  drawable_id,
-                                gint   *x,
-                                gint   *y)
+align_layers_get_align_offsets (GimpDrawable *drawable,
+                                gint         *x,
+                                gint         *y)
 {
-  gint width  = gimp_drawable_width  (drawable_id);
-  gint height = gimp_drawable_height (drawable_id);
+  gint width  = gimp_drawable_width  (drawable);
+  gint height = gimp_drawable_height (drawable);
 
   switch (VALS.h_base)
     {
diff --git a/plug-ins/common/plugin-defs.pl b/plug-ins/common/plugin-defs.pl
index 51d8535d07..6ed8ef3e10 100644
--- a/plug-ins/common/plugin-defs.pl
+++ b/plug-ins/common/plugin-defs.pl
@@ -1,5 +1,5 @@
 %plugins = (
-    'align-layers' => { ui => 1, old_api => 1 },
+    'align-layers' => { ui => 1 },
     'animation-optimize' => { gegl => 1, old_api => 1 },
     'animation-play' => { ui => 1, gegl => 1 },
     'blinds' => { ui => 1, gegl => 1 },


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