[gimp] plug-ins: port file-bmp to GimpPlugIn and libgimp objects



commit 71d56d8e6fbf1d0d95664693ca1d0bfb2272525a
Author: Michael Natterer <mitch gimp org>
Date:   Sat Aug 24 17:26:00 2019 +0200

    plug-ins: port file-bmp to GimpPlugIn and libgimp objects

 plug-ins/file-bmp/Makefile.am |   3 +-
 plug-ins/file-bmp/bmp-load.c  |  82 +++++-----
 plug-ins/file-bmp/bmp-load.h  |   4 +-
 plug-ins/file-bmp/bmp-save.c  |  20 +--
 plug-ins/file-bmp/bmp-save.h  |  10 +-
 plug-ins/file-bmp/bmp.c       | 353 +++++++++++++++++++++---------------------
 6 files changed, 238 insertions(+), 234 deletions(-)
---
diff --git a/plug-ins/file-bmp/Makefile.am b/plug-ins/file-bmp/Makefile.am
index bfcb896b5f..77e2204581 100644
--- a/plug-ins/file-bmp/Makefile.am
+++ b/plug-ins/file-bmp/Makefile.am
@@ -32,10 +32,9 @@ file_bmp_SOURCES = \
        bmp-save.h
 
 AM_CPPFLAGS = \
-       -DGIMP_DEPRECATED_REPLACE_NEW_API \
        -I$(top_srcdir) \
        $(GTK_CFLAGS)   \
-       $(GEGL_CFLAGS) \
+       $(GEGL_CFLAGS)  \
        -I$(includedir)
 
 LDADD = \
diff --git a/plug-ins/file-bmp/bmp-load.c b/plug-ins/file-bmp/bmp-load.c
index 4115b9a5c6..cf6fbba18c 100644
--- a/plug-ins/file-bmp/bmp-load.c
+++ b/plug-ins/file-bmp/bmp-load.c
@@ -44,18 +44,18 @@
 #endif
 
 
-static gint32 ReadImage (FILE                 *fd,
-                         const gchar          *filename,
-                         gint                  width,
-                         gint                  height,
-                         guchar                cmap[256][3],
-                         gint                  ncols,
-                         gint                  bpp,
-                         gint                  compression,
-                         gint                  rowbytes,
-                         gboolean              gray,
-                         const BitmapChannel  *masks,
-                         GError              **error);
+static GimpImage * ReadImage (FILE                 *fd,
+                              const gchar          *filename,
+                              gint                  width,
+                              gint                  height,
+                              guchar                cmap[256][3],
+                              gint                  ncols,
+                              gint                  bpp,
+                              gint                  compression,
+                              gint                  rowbytes,
+                              gboolean              gray,
+                              const BitmapChannel  *masks,
+                              GError              **error);
 
 
 static void
@@ -200,7 +200,7 @@ ReadChannelMasks (guint32       *tmp,
   return TRUE;
 }
 
-gint32
+GimpImage *
 load_image (const gchar  *filename,
             GError      **error)
 {
@@ -211,7 +211,7 @@ load_image (const gchar  *filename,
   gint            ColormapSize, rowbytes, Maps;
   gboolean        gray = FALSE;
   guchar          ColorMap[256][3];
-  gint32          image_ID = -1;
+  GimpImage      *image = NULL;
   gchar           magick[2];
   BitmapChannel   masks[4];
 
@@ -588,21 +588,21 @@ load_image (const gchar  *filename,
 
   fseek (fd, bitmap_file_head.bfOffs, SEEK_SET);
 
-  /* Get the Image and return the ID or -1 on error*/
-  image_ID = ReadImage (fd,
-                        filename,
-                        bitmap_head.biWidth,
-                        ABS (bitmap_head.biHeight),
-                        ColorMap,
-                        bitmap_head.biClrUsed,
-                        bitmap_head.biBitCnt,
-                        bitmap_head.biCompr,
-                        rowbytes,
-                        gray,
-                        masks,
-                        error);
-
-  if (image_ID < 0)
+  /* Get the Image and return the image or NULL on error*/
+  image = ReadImage (fd,
+                     filename,
+                     bitmap_head.biWidth,
+                     ABS (bitmap_head.biHeight),
+                     ColorMap,
+                     bitmap_head.biClrUsed,
+                     bitmap_head.biBitCnt,
+                     bitmap_head.biCompr,
+                     rowbytes,
+                     gray,
+                     masks,
+                     error);
+
+  if (! image)
     goto out;
 
   if (bitmap_head.biXPels > 0 &&
@@ -621,20 +621,20 @@ load_image (const gchar  *filename,
       xresolution = bitmap_head.biXPels * 0.0254;
       yresolution = bitmap_head.biYPels * 0.0254;
 
-      gimp_image_set_resolution (image_ID, xresolution, yresolution);
+      gimp_image_set_resolution (image, xresolution, yresolution);
     }
 
   if (bitmap_head.biHeight < 0)
-    gimp_image_flip (image_ID, GIMP_ORIENTATION_VERTICAL);
+    gimp_image_flip (image, GIMP_ORIENTATION_VERTICAL);
 
 out:
   if (fd)
     fclose (fd);
 
-  return image_ID;
+  return image;
 }
 
-static gint32
+static GimpImage *
 ReadImage (FILE                 *fd,
            const gchar          *filename,
            gint                  width,
@@ -651,8 +651,8 @@ ReadImage (FILE                 *fd,
   guchar             v, n;
   gint               xpos = 0;
   gint               ypos = 0;
-  gint32             image;
-  gint32             layer;
+  GimpImage         *image;
+  GimpLayer         *layer;
   GeglBuffer        *buffer;
   guchar            *dest, *temp, *row_buf;
   guchar             gimp_cmap[768];
@@ -673,7 +673,7 @@ ReadImage (FILE                 *fd,
       g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                    "%s",
                    _("Unrecognized or invalid BMP compression format."));
-      return -1;
+      return NULL;
     }
 
   /* Make a new image in GIMP */
@@ -715,19 +715,19 @@ ReadImage (FILE                 *fd,
 
     default:
       g_message (_("Unsupported or invalid bitdepth."));
-      return -1;
+      return NULL;
     }
 
   if ((width < 0) || (width > GIMP_MAX_IMAGE_SIZE))
     {
       g_message (_("Unsupported or invalid image width: %d"), width);
-      return -1;
+      return NULL;
     }
 
   if ((height < 0) || (height > GIMP_MAX_IMAGE_SIZE))
     {
       g_message (_("Unsupported or invalid image height: %d"), height);
-      return -1;
+      return NULL;
     }
 
   image = gimp_image_new (width, height, base_type);
@@ -738,7 +738,7 @@ ReadImage (FILE                 *fd,
 
   gimp_image_set_filename (image, filename);
 
-  gimp_image_insert_layer (image, layer, -1, 0);
+  gimp_image_insert_layer (image, layer, NULL, 0);
 
   /* use g_malloc0 to initialize the dest buffer so that unspecified
      pixels in RLE bitmaps show up as the zeroth element in the palette.
@@ -1005,7 +1005,7 @@ ReadImage (FILE                 *fd,
         gimp_cmap[j++] = cmap[i][2];
       }
 
-  buffer = gimp_drawable_get_buffer (layer);
+  buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer));
 
   gegl_buffer_set (buffer, GEGL_RECTANGLE (0, 0, width, height), 0,
                    NULL, dest, GEGL_AUTO_ROWSTRIDE);
diff --git a/plug-ins/file-bmp/bmp-load.h b/plug-ins/file-bmp/bmp-load.h
index 7d80abe3cc..4ee93c78c6 100644
--- a/plug-ins/file-bmp/bmp-load.h
+++ b/plug-ins/file-bmp/bmp-load.h
@@ -20,8 +20,8 @@
 #define __BMP_LOAD_H__
 
 
-gint32   load_image (const gchar  *filename,
-                     GError      **error);
+GimpImage * load_image (const gchar  *filename,
+                        GError      **error);
 
 
 #endif /* __BMP_LOAD_H__ */
diff --git a/plug-ins/file-bmp/bmp-save.c b/plug-ins/file-bmp/bmp-save.c
index 32fe00a2c6..6890e240fc 100644
--- a/plug-ins/file-bmp/bmp-save.c
+++ b/plug-ins/file-bmp/bmp-save.c
@@ -124,11 +124,11 @@ warning_dialog (const gchar *primary,
 }
 
 GimpPDBStatusType
-save_image (const gchar  *filename,
-            gint32        image,
-            gint32        drawable_ID,
-            GimpRunMode   run_mode,
-            GError      **error)
+save_image (const gchar   *filename,
+            GimpImage     *image,
+            GimpDrawable  *drawable,
+            GimpRunMode    run_mode,
+            GError       **error)
 {
   FILE           *outfile;
   BitmapFileHead  bitmap_file_head;
@@ -151,11 +151,11 @@ save_image (const gchar  *filename,
   gint            color_space_size;
   guint32         Mask[4];
 
-  buffer = gimp_drawable_get_buffer (drawable_ID);
+  buffer = gimp_drawable_get_buffer (drawable);
 
-  drawable_type   = gimp_drawable_type   (drawable_ID);
-  drawable_width  = gimp_drawable_width  (drawable_ID);
-  drawable_height = gimp_drawable_height (drawable_ID);
+  drawable_type   = gimp_drawable_type   (drawable);
+  drawable_width  = gimp_drawable_width  (drawable);
+  drawable_height = gimp_drawable_height (drawable);
 
   switch (drawable_type)
     {
@@ -220,7 +220,7 @@ save_image (const gchar  *filename,
      /* fallthrough */
 
     case GIMP_INDEXED_IMAGE:
-      format   = gimp_drawable_get_format (drawable_ID);
+      format   = gimp_drawable_get_format (drawable);
       cmap     = gimp_image_get_colormap (image, &colors);
       MapSize  = 4 * colors;
 
diff --git a/plug-ins/file-bmp/bmp-save.h b/plug-ins/file-bmp/bmp-save.h
index 550d66dcbf..189d63161e 100644
--- a/plug-ins/file-bmp/bmp-save.h
+++ b/plug-ins/file-bmp/bmp-save.h
@@ -20,11 +20,11 @@
 #define __BMP_SAVE_H__
 
 
-GimpPDBStatusType   save_image (const gchar  *filename,
-                                gint32        image,
-                                gint32        drawable_ID,
-                                GimpRunMode   run_mode,
-                                GError      **error);
+GimpPDBStatusType   save_image (const gchar   *filename,
+                                GimpImage     *image,
+                                GimpDrawable  *drawable,
+                                GimpRunMode    run_mode,
+                                GError       **error);
 
 
 #endif /* __BMP_SAVE_H__ */
diff --git a/plug-ins/file-bmp/bmp.c b/plug-ins/file-bmp/bmp.c
index 2bd2660287..8b860a29aa 100644
--- a/plug-ins/file-bmp/bmp.c
+++ b/plug-ins/file-bmp/bmp.c
@@ -66,205 +66,210 @@
 #include "libgimp/stdplugins-intl.h"
 
 
-/* Declare some local functions.
- */
-static void   query (void);
-static void   run   (const gchar      *name,
-                     gint              nparams,
-                     const GimpParam  *param,
-                     gint             *nreturn_vals,
-                     GimpParam       **return_vals);
+typedef struct _Bmp      Bmp;
+typedef struct _BmpClass BmpClass;
 
+struct _Bmp
+{
+  GimpPlugIn      parent_instance;
+};
 
-const GimpPlugInInfo PLUG_IN_INFO =
+struct _BmpClass
 {
-  NULL,  /* init_proc  */
-  NULL,  /* quit_proc  */
-  query, /* query_proc */
-  run,   /* run_proc   */
+  GimpPlugInClass parent_class;
 };
 
 
-MAIN ()
+#define BMP_TYPE  (bmp_get_type ())
+#define BMP (obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BMP_TYPE, Bmp))
+
+GType                   bmp_get_type         (void) G_GNUC_CONST;
+
+static GList          * bmp_query_procedures (GimpPlugIn           *plug_in);
+static GimpProcedure  * bmp_create_procedure (GimpPlugIn           *plug_in,
+                                              const gchar          *name);
+
+static GimpValueArray * bmp_load             (GimpProcedure        *procedure,
+                                              GimpRunMode           run_mode,
+                                              GFile                *file,
+                                              const GimpValueArray *args,
+                                              gpointer              run_data);
+static GimpValueArray * bmp_save             (GimpProcedure        *procedure,
+                                              GimpRunMode           run_mode,
+                                              GimpImage            *image,
+                                              GimpDrawable         *drawable,
+                                              GFile                *file,
+                                              const GimpValueArray *args,
+                                              gpointer              run_data);
+
+
+
+G_DEFINE_TYPE (Bmp, bmp, GIMP_TYPE_PLUG_IN)
+
+GIMP_MAIN (BMP_TYPE)
 
 
 static void
-query (void)
+bmp_class_init (BmpClass *klass)
 {
-  static const GimpParamDef load_args[] =
-  {
-    { GIMP_PDB_INT32,    "run-mode",     "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
-    { GIMP_PDB_STRING,   "filename",     "The name of the file to load" },
-    { GIMP_PDB_STRING,   "raw-filename", "The name entered" },
-  };
-  static const GimpParamDef load_return_vals[] =
-  {
-    { GIMP_PDB_IMAGE, "image", "Output image" },
-  };
-
-  static const GimpParamDef save_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",     "Drawable to save" },
-    { GIMP_PDB_STRING,   "filename",     "The name of the file to save the image in" },
-    { GIMP_PDB_STRING,   "raw-filename", "The name entered" },
-  };
-
-  gimp_install_procedure (LOAD_PROC,
-                          "Loads files of Windows BMP file format",
-                          "Loads files of Windows BMP file format",
-                          "Alexander Schulz",
-                          "Alexander Schulz",
-                          "1997",
-                          N_("Windows BMP image"),
-                          NULL,
-                          GIMP_PLUGIN,
-                          G_N_ELEMENTS (load_args),
-                          G_N_ELEMENTS (load_return_vals),
-                          load_args, load_return_vals);
-
-  gimp_register_file_handler_mime (LOAD_PROC, "image/bmp");
-  gimp_register_magic_load_handler (LOAD_PROC,
-                                    "bmp",
-                                    "",
-                                    "0,string,BM");
-
-  gimp_install_procedure (SAVE_PROC,
-                          "Saves files in Windows BMP file format",
-                          "Saves files in Windows BMP file format",
-                          "Alexander Schulz",
-                          "Alexander Schulz",
-                          "1997",
-                          N_("Windows BMP image"),
-                          "INDEXED, GRAY, RGB*",
-                          GIMP_PLUGIN,
-                          G_N_ELEMENTS (save_args), 0,
-                          save_args, NULL);
-
-  gimp_register_file_handler_mime (SAVE_PROC, "image/bmp");
-  gimp_register_save_handler (SAVE_PROC, "bmp", "");
+  GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
+
+  plug_in_class->query_procedures = bmp_query_procedures;
+  plug_in_class->create_procedure = bmp_create_procedure;
 }
 
 static void
-run (const gchar      *name,
-     gint              nparams,
-     const GimpParam  *param,
-     gint             *nreturn_vals,
-     GimpParam       **return_vals)
+bmp_init (Bmp *bmp)
 {
-  static GimpParam   values[2];
-  GimpRunMode        run_mode;
-  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
-  GError            *error  = NULL;
+}
 
-  INIT_I18N ();
-  gegl_init (NULL, NULL);
+static GList *
+bmp_query_procedures (GimpPlugIn *plug_in)
+{
+  GList *list = NULL;
 
-  run_mode = param[0].data.d_int32;
+  list = g_list_append (list, g_strdup (LOAD_PROC));
+  list = g_list_append (list, g_strdup (SAVE_PROC));
 
-  *nreturn_vals = 1;
-  *return_vals  = values;
+  return list;
+}
 
-  values[0].type          = GIMP_PDB_STATUS;
-  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
+static GimpProcedure *
+bmp_create_procedure (GimpPlugIn  *plug_in,
+                      const gchar *name)
+{
+  GimpProcedure *procedure = NULL;
 
-  if (strcmp (name, LOAD_PROC) == 0)
-    {
-      switch (run_mode)
-        {
-        case GIMP_RUN_INTERACTIVE:
-          break;
-
-        case GIMP_RUN_NONINTERACTIVE:
-          /*  Make sure all the arguments are there!  */
-          if (nparams != 3)
-            status = GIMP_PDB_CALLING_ERROR;
-          break;
-
-        default:
-          break;
-        }
-
-      if (status == GIMP_PDB_SUCCESS)
-        {
-          GFile *file = g_file_new_for_uri (param[1].data.d_string);
-
-          gint32 image_ID = load_image (g_file_get_path (file),
-                                        &error);
-
-          if (image_ID != -1)
-            {
-              *nreturn_vals = 2;
-              values[1].type         = GIMP_PDB_IMAGE;
-              values[1].data.d_image = image_ID;
-            }
-          else
-            {
-              status = GIMP_PDB_EXECUTION_ERROR;
-            }
-        }
-    }
-  else if (strcmp (name, SAVE_PROC) == 0)
+  if (! strcmp (name, LOAD_PROC))
     {
-      gint32           image_ID    = param[1].data.d_int32;
-      gint32           drawable_ID = param[2].data.d_int32;
-      GimpExportReturn export      = GIMP_EXPORT_CANCEL;
-
-      /*  eventually export the image */
-      switch (run_mode)
-        {
-        case GIMP_RUN_INTERACTIVE:
-        case GIMP_RUN_WITH_LAST_VALS:
-          gimp_ui_init (PLUG_IN_BINARY, FALSE);
-
-          export = gimp_export_image (&image_ID, &drawable_ID, "BMP",
-                                      GIMP_EXPORT_CAN_HANDLE_RGB   |
-                                      GIMP_EXPORT_CAN_HANDLE_GRAY  |
-                                      GIMP_EXPORT_CAN_HANDLE_ALPHA |
-                                      GIMP_EXPORT_CAN_HANDLE_INDEXED);
-
-          if (export == GIMP_EXPORT_CANCEL)
-            {
-              values[0].data.d_status = GIMP_PDB_CANCEL;
-              return;
-            }
-          break;
-
-        case GIMP_RUN_NONINTERACTIVE:
-          /*  Make sure all the arguments are there!  */
-          if (nparams != 5)
-            status = GIMP_PDB_CALLING_ERROR;
-          break;
-
-        default:
-          break;
-        }
-
-      if (status == GIMP_PDB_SUCCESS)
-        {
-          GFile *file = g_file_new_for_uri (param[3].data.d_string);
-
-          status = save_image (g_file_get_path (file),
-                               image_ID, drawable_ID,
-                               run_mode,
-                               &error);
-        }
-
-      if (export == GIMP_EXPORT_EXPORT)
-        gimp_image_delete (image_ID);
+      procedure = gimp_load_procedure_new (plug_in, name, GIMP_PLUGIN,
+                                           bmp_load, NULL, NULL);
+
+      gimp_procedure_set_menu_label (procedure, N_("Windows BMP image"));
+
+      gimp_procedure_set_documentation (procedure,
+                                        "Loads files of Windows BMP file format",
+                                        "Loads files of Windows BMP file format",
+                                        name);
+      gimp_procedure_set_attribution (procedure,
+                                      "Alexander Schulz",
+                                      "Alexander Schulz",
+                                      "1997");
+
+      gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
+                                          "image/bmp");
+      gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
+                                          "bmp");
+      gimp_file_procedure_set_magics (GIMP_FILE_PROCEDURE (procedure),
+                                      "0,string,BM");
     }
-  else
+  else if (! strcmp (name, SAVE_PROC))
     {
-      status = GIMP_PDB_CALLING_ERROR;
+      procedure = gimp_save_procedure_new (plug_in, name, GIMP_PLUGIN,
+                                           bmp_save, NULL, NULL);
+
+      gimp_procedure_set_image_types (procedure, "INDEXED, GRAY, RGB*");
+
+      gimp_procedure_set_menu_label (procedure, N_("Windows BMP image"));
+
+      gimp_procedure_set_documentation (procedure,
+                                        "Saves files in Windows BMP file format",
+                                        "Saves files in Windows BMP file format",
+                                        name);
+      gimp_procedure_set_attribution (procedure,
+                                      "Alexander Schulz",
+                                      "Alexander Schulz",
+                                      "1997");
+
+      gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
+                                          "image/bmp");
+      gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
+                                          "bmp");
     }
 
-  if (status != GIMP_PDB_SUCCESS && error)
+  return procedure;
+}
+
+static GimpValueArray *
+bmp_load (GimpProcedure        *procedure,
+          GimpRunMode           run_mode,
+          GFile                *file,
+          const GimpValueArray *args,
+          gpointer              run_data)
+{
+  GimpValueArray *return_vals;
+  GimpImage      *image;
+  gchar          *filename;
+  GError         *error = NULL;
+
+  INIT_I18N ();
+  gegl_init (NULL, NULL);
+
+  filename = g_file_get_path (file);
+
+  image = load_image (filename, &error);
+
+  g_free (filename);
+
+  if (! image)
+    return gimp_procedure_new_return_values (procedure,
+                                             GIMP_PDB_EXECUTION_ERROR,
+                                             error);
+
+  return_vals = gimp_procedure_new_return_values (procedure,
+                                                  GIMP_PDB_SUCCESS,
+                                                  NULL);
+
+  GIMP_VALUES_SET_IMAGE (return_vals, 1, image);
+
+  return return_vals;
+}
+
+static GimpValueArray *
+bmp_save (GimpProcedure        *procedure,
+          GimpRunMode           run_mode,
+          GimpImage            *image,
+          GimpDrawable         *drawable,
+          GFile                *file,
+          const GimpValueArray *args,
+          gpointer              run_data)
+{
+  GimpPDBStatusType      status = GIMP_PDB_SUCCESS;
+  GimpExportReturn       export = GIMP_EXPORT_CANCEL;
+  GError                *error = NULL;
+
+  INIT_I18N ();
+  gegl_init (NULL, NULL);
+
+  switch (run_mode)
     {
-      *nreturn_vals = 2;
-      values[1].type          = GIMP_PDB_STRING;
-      values[1].data.d_string = error->message;
+    case GIMP_RUN_INTERACTIVE:
+    case GIMP_RUN_WITH_LAST_VALS:
+      gimp_ui_init (PLUG_IN_BINARY, FALSE);
+
+      export = gimp_export_image (&image, &drawable, "BMP",
+                                  GIMP_EXPORT_CAN_HANDLE_RGB   |
+                                  GIMP_EXPORT_CAN_HANDLE_GRAY  |
+                                  GIMP_EXPORT_CAN_HANDLE_ALPHA |
+                                  GIMP_EXPORT_CAN_HANDLE_INDEXED);
+
+      if (export == GIMP_EXPORT_CANCEL)
+        return gimp_procedure_new_return_values (procedure,
+                                                 GIMP_PDB_CANCEL,
+                                                 NULL);
+      break;
+
+    default:
+      break;
     }
 
-  values[0].data.d_status = status;
+  status = save_image (g_file_get_path (file),
+                       image, drawable,
+                       run_mode,
+                       &error);
+
+  if (export == GIMP_EXPORT_EXPORT)
+    gimp_image_delete (image);
+
+  return gimp_procedure_new_return_values (procedure, status, error);
 }


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