[gimp/gimp-2-10] app: implement pattern saving in the core



commit 5dc78980f4c1061ca58eab85ec5df47e55a3fff7
Author: Michael Natterer <mitch gimp org>
Date:   Mon Feb 11 20:56:07 2019 +0100

    app: implement pattern saving in the core
    
    Add GimpData::save() implementation to GimpPattern, and change some
    glue code to make patterns editable.
    
    Also implement GimpData::duplicate() in GimpPatternClipboard, which
    makes it possible to simply copy an area and duplicate the clipboard
    pattern to create a new persistent pattern.
    
    (cherry picked from commit e93fd73face1e0e26484c9f5f9a938479ac61a9d)

 app/core/Makefile.am                 |  2 +
 app/core/gimp-data-factories.c       |  2 +-
 app/core/gimppattern-save.c          | 81 ++++++++++++++++++++++++++++++++++++
 app/core/gimppattern-save.h          | 28 +++++++++++++
 app/core/gimppattern.c               |  2 +
 app/core/gimppatternclipboard.c      | 14 ++-----
 app/widgets/gimppatternfactoryview.c |  1 -
 7 files changed, 118 insertions(+), 12 deletions(-)
---
diff --git a/app/core/Makefile.am b/app/core/Makefile.am
index c66629e8a4..fac291db37 100644
--- a/app/core/Makefile.am
+++ b/app/core/Makefile.am
@@ -375,6 +375,8 @@ libappcore_a_sources = \
        gimppattern-header.h                    \
        gimppattern-load.c                      \
        gimppattern-load.h                      \
+       gimppattern-save.c                      \
+       gimppattern-save.h                      \
        gimppatternclipboard.c                  \
        gimppatternclipboard.h                  \
        gimppalette.c                           \
diff --git a/app/core/gimp-data-factories.c b/app/core/gimp-data-factories.c
index 41f4303a2e..ab96eb2efe 100644
--- a/app/core/gimp-data-factories.c
+++ b/app/core/gimp-data-factories.c
@@ -147,7 +147,7 @@ gimp_data_factories_init (Gimp *gimp)
                                        "GIMP Pattern",
                                        gimp_pattern_load,
                                        GIMP_PATTERN_FILE_EXTENSION,
-                                       FALSE);
+                                       TRUE);
   gimp_data_loader_factory_add_fallback (gimp->pattern_factory,
                                          "Pattern from GdkPixbuf",
                                          gimp_pattern_load_pixbuf);
diff --git a/app/core/gimppattern-save.c b/app/core/gimppattern-save.c
new file mode 100644
index 0000000000..a6ff08063b
--- /dev/null
+++ b/app/core/gimppattern-save.c
@@ -0,0 +1,81 @@
+/* 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 <https://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <cairo.h>
+#include <gegl.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
+
+#include "libgimpbase/gimpbase.h"
+#include "libgimpcolor/gimpcolor.h"
+
+#include "core-types.h"
+
+#include "gimppattern.h"
+#include "gimppattern-header.h"
+#include "gimppattern-save.h"
+#include "gimptempbuf.h"
+
+
+gboolean
+gimp_pattern_save (GimpData       *data,
+                   GOutputStream  *output,
+                   GError        **error)
+{
+  GimpPattern       *pattern = GIMP_PATTERN (data);
+  GimpTempBuf       *mask    = gimp_pattern_get_mask (pattern);
+  const Babl        *format  = gimp_temp_buf_get_format (mask);
+  GimpPatternHeader  header;
+  const gchar       *name;
+  gint               width;
+  gint               height;
+
+  name   = gimp_object_get_name (pattern);
+  width  = gimp_temp_buf_get_width  (mask);
+  height = gimp_temp_buf_get_height (mask);
+
+  header.header_size  = g_htonl (sizeof (GimpPatternHeader) +
+                                 strlen (name) + 1);
+  header.version      = g_htonl (1);
+  header.width        = g_htonl (width);
+  header.height       = g_htonl (height);
+  header.bytes        = g_htonl (babl_format_get_bytes_per_pixel (format));
+  header.magic_number = g_htonl (GIMP_PATTERN_MAGIC);
+
+  if (! g_output_stream_write_all (output, &header, sizeof (header),
+                                   NULL, NULL, error))
+    {
+      return FALSE;
+    }
+
+  if (! g_output_stream_write_all (output, name, strlen (name) + 1,
+                                   NULL, NULL, error))
+    {
+      return FALSE;
+    }
+
+  if (! g_output_stream_write_all (output,
+                                   gimp_temp_buf_get_data (mask),
+                                   gimp_temp_buf_get_data_size (mask),
+                                   NULL, NULL, error))
+    {
+      return FALSE;
+    }
+
+  return TRUE;
+}
diff --git a/app/core/gimppattern-save.h b/app/core/gimppattern-save.h
new file mode 100644
index 0000000000..d3c657c681
--- /dev/null
+++ b/app/core/gimppattern-save.h
@@ -0,0 +1,28 @@
+/* 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 <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_PATTERN_SAVE_H__
+#define __GIMP_PATTERN_SAVE_H__
+
+
+/*  don't call this function directly, use gimp_data_save() instead  */
+gboolean   gimp_pattern_save (GimpData       *data,
+                              GOutputStream  *output,
+                              GError        **error);
+
+
+#endif  /*  __GIMP_PATTERN_SAVE_H__  */
diff --git a/app/core/gimppattern.c b/app/core/gimppattern.c
index b9ec0bf5e9..bcc0332e3c 100644
--- a/app/core/gimppattern.c
+++ b/app/core/gimppattern.c
@@ -30,6 +30,7 @@
 
 #include "gimppattern.h"
 #include "gimppattern-load.h"
+#include "gimppattern-save.h"
 #include "gimptagged.h"
 #include "gimptempbuf.h"
 
@@ -83,6 +84,7 @@ gimp_pattern_class_init (GimpPatternClass *klass)
   viewable_class->get_new_preview   = gimp_pattern_get_new_preview;
   viewable_class->get_description   = gimp_pattern_get_description;
 
+  data_class->save                  = gimp_pattern_save;
   data_class->get_extension         = gimp_pattern_get_extension;
   data_class->copy                  = gimp_pattern_copy;
 }
diff --git a/app/core/gimppatternclipboard.c b/app/core/gimppatternclipboard.c
index b503807253..9dd6a1f3ee 100644
--- a/app/core/gimppatternclipboard.c
+++ b/app/core/gimppatternclipboard.c
@@ -55,9 +55,7 @@ static void       gimp_pattern_clipboard_get_property (GObject      *object,
                                                        guint         property_id,
                                                        GValue       *value,
                                                        GParamSpec   *pspec);
-#if 0
 static GimpData * gimp_pattern_clipboard_duplicate    (GimpData     *data);
-#endif
 
 static void       gimp_pattern_clipboard_changed      (Gimp         *gimp,
                                                        GimpPattern  *pattern);
@@ -72,17 +70,13 @@ static void
 gimp_pattern_clipboard_class_init (GimpPatternClipboardClass *klass)
 {
   GObjectClass  *object_class = G_OBJECT_CLASS (klass);
-#if 0
   GimpDataClass *data_class   = GIMP_DATA_CLASS (klass);
-#endif
 
   object_class->constructed  = gimp_pattern_clipboard_constructed;
   object_class->set_property = gimp_pattern_clipboard_set_property;
   object_class->get_property = gimp_pattern_clipboard_get_property;
 
-#if 0
   data_class->duplicate      = gimp_pattern_clipboard_duplicate;
-#endif
 
   g_object_class_install_property (object_class, PROP_GIMP,
                                    g_param_spec_object ("gimp", NULL, NULL,
@@ -150,15 +144,15 @@ gimp_pattern_clipboard_get_property (GObject    *object,
     }
 }
 
-#if 0
 static GimpData *
 gimp_pattern_clipboard_duplicate (GimpData *data)
 {
-  GimpPatternClipboard *pattern = GIMP_PATTERN_CLIPBOARD (data);
+  GimpData *new = g_object_new (GIMP_TYPE_PATTERN, NULL);
 
-  return gimp_pattern_clipboard_new (pattern->gimp);
+  gimp_data_copy (new, data);
+
+  return new;
 }
-#endif
 
 GimpData *
 gimp_pattern_clipboard_new (Gimp *gimp)
diff --git a/app/widgets/gimppatternfactoryview.c b/app/widgets/gimppatternfactoryview.c
index f756e99ecd..3f1a9e303e 100644
--- a/app/widgets/gimppatternfactoryview.c
+++ b/app/widgets/gimppatternfactoryview.c
@@ -91,7 +91,6 @@ gimp_pattern_factory_view_new (GimpViewType      view_type,
                                  NULL);
 
   gtk_widget_hide (gimp_data_factory_view_get_edit_button (GIMP_DATA_FACTORY_VIEW (factory_view)));
-  gtk_widget_hide (gimp_data_factory_view_get_duplicate_button (GIMP_DATA_FACTORY_VIEW (factory_view)));
 
   return GTK_WIDGET (factory_view);
 }


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