[dia] formatting: cleanup DiaImage



commit b6e34f48ca4ca9d7f0232538ddb7d73e25789c0b
Author: Zander Brown <zbrown gnome org>
Date:   Tue Jul 23 01:05:21 2019 +0100

    formatting: cleanup DiaImage

 lib/dia_image.c | 304 +++++++++++++++++++++++++++++---------------------------
 lib/dia_image.h |  43 ++++----
 2 files changed, 183 insertions(+), 164 deletions(-)
---
diff --git a/lib/dia_image.c b/lib/dia_image.c
index 1c6612f4..e353fb69 100644
--- a/lib/dia_image.c
+++ b/lib/dia_image.c
@@ -26,18 +26,6 @@
 #include <gdk-pixbuf/gdk-pixbuf.h>
 #include <gtk/gtk.h>
 
-#define SCALING_CACHE
-
-GType dia_image_get_type (void);
-#define DIA_TYPE_IMAGE (dia_image_get_type())
-#define DIA_IMAGE(object) (G_TYPE_CHECK_INSTANCE_CAST((object), DIA_TYPE_IMAGE, DiaImage))
-#define DIA_IMAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DIA_TYPE_IMAGE, DiaImageClass))
-
-typedef struct _DiaImageClass DiaImageClass;
-struct _DiaImageClass {
-  GObjectClass parent_class;
-};
-
 /*!
  * \brief DiaImage is a thin wrapper around GdkPixbuf
  *
@@ -55,48 +43,47 @@ struct _DiaImage {
   GdkPixbuf *image;
   gchar *filename;
   gchar *mime_type; /* optional */
-#ifdef SCALING_CACHE
   GdkPixbuf *scaled; /* a cache of the last scaled version */
   int scaled_width, scaled_height;
-#endif
+  cairo_surface_t *surface;
 };
 
-static void dia_image_class_init(DiaImageClass* class);
-static void dia_image_finalize(GObject* object);
-static void dia_image_init_instance(DiaImage*);
+G_DEFINE_TYPE (DiaImage, dia_image, G_TYPE_OBJECT)
 
-GType
-dia_image_get_type (void)
+/*!
+ * \brief Destructor
+ * \memberof _DiaImage
+ */
+static void
+dia_image_finalize (GObject* object)
 {
-    static GType object_type = 0;
-
-    if (!object_type) {
-        static const GTypeInfo object_info =
-            {
-                sizeof (DiaImageClass),
-                (GBaseInitFunc) NULL,
-                (GBaseFinalizeFunc) NULL,
-                (GClassInitFunc) dia_image_class_init, /* class_init */
-                NULL,           /* class_finalize */
-                NULL,           /* class_data */
-                sizeof (DiaImage),
-                0,              /* n_preallocs */
-                (GInstanceInitFunc)dia_image_init_instance
-            };
-        object_type = g_type_register_static (G_TYPE_OBJECT,
-                                              "DiaImage",
-                                              &object_info, 0);
-    }
-    return object_type;
-}
+  DiaImage *image = DIA_IMAGE(object);
+
+  if (image->scaled) {
+    g_object_unref (image->scaled);
+  }
+  image->scaled = NULL;
 
-static gpointer parent_class;
+  if (image->image) {
+    g_object_unref (image->image);
+  }
+  image->image = NULL;
+
+  g_free (image->filename);
+  image->filename = NULL;
+
+  g_free (image->mime_type);
+  image->mime_type = NULL;
+
+  cairo_surface_destroy (image->surface);
+  image->surface = NULL;
+}
 
 static void
-dia_image_class_init(DiaImageClass* klass)
+dia_image_class_init (DiaImageClass* klass)
 {
-  GObjectClass* object_class = G_OBJECT_CLASS(klass);
-  parent_class = g_type_class_peek_parent(klass);
+  GObjectClass* object_class = G_OBJECT_CLASS (klass);
+
   object_class->finalize = dia_image_finalize;
 }
 
@@ -105,32 +92,11 @@ dia_image_class_init(DiaImageClass* klass)
  * \memberof _DiaImage
  */
 static void
-dia_image_init_instance(DiaImage *image)
+dia_image_init (DiaImage *image)
 {
   /* GObject *gobject = G_OBJECT(image);  */
   /* zero intialization should be good for us */
-}
-
-/*!
- * \brief Destructor
- * \memberof _DiaImage
- */
-static void
-dia_image_finalize(GObject* object)
-{
-  DiaImage *image = DIA_IMAGE(object);
-#ifdef SCALING_CACHE
-  if (image->scaled)
-    g_object_unref (image->scaled);
-  image->scaled = NULL;
-#endif
-  if (image->image)
-    g_object_unref (image->image);
-  image->image = NULL;
-  g_free (image->filename);
-  image->filename = NULL;
-  g_free (image->mime_type);
-  image->mime_type = NULL;
+  image->surface = NULL;
 }
 
 /*!
@@ -140,12 +106,12 @@ dia_image_finalize(GObject* object)
  * \memberof _DiaImage
  */
 DiaImage *
-dia_image_get_broken(void)
+dia_image_get_broken (void)
 {
   static GdkPixbuf *broken = NULL;
   DiaImage *image;
 
-  image = DIA_IMAGE(g_object_new(DIA_TYPE_IMAGE, NULL));
+  image = DIA_IMAGE (g_object_new (DIA_TYPE_IMAGE, NULL));
   if (broken == NULL) {
     broken = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                        "image-missing",
@@ -155,9 +121,8 @@ dia_image_get_broken(void)
   image->image = g_object_ref (broken);
   /* Kinda hard to export :) */
   image->filename = g_strdup("<broken>");
-#ifdef SCALING_CACHE
   image->scaled = NULL;
-#endif
+
   return image;
 }
 
@@ -169,27 +134,28 @@ dia_image_get_broken(void)
  * \memberof _DiaImage
  */
 DiaImage *
-dia_image_load(const gchar *filename)
+dia_image_load (const gchar *filename)
 {
   DiaImage *dia_img;
   GdkPixbuf *image;
   GError *error = NULL;
 
-  image = gdk_pixbuf_new_from_file(filename, &error);
+  image = gdk_pixbuf_new_from_file (filename, &error);
   if (image == NULL) {
     /* dia_image_load() function is also (mis)used to check file
      * existence. Don't warn if the file is simply not there but
      * only if there is something else wrong while loading it.
      */
-    if (g_file_test(filename, G_FILE_TEST_EXISTS))
+    if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
       message_warning ("%s\n", error->message);
+    }
     g_error_free (error);
     return NULL;
   }
 
-  dia_img = DIA_IMAGE(g_object_new(DIA_TYPE_IMAGE, NULL));
+  dia_img = DIA_IMAGE (g_object_new (DIA_TYPE_IMAGE, NULL));
   dia_img->image = image;
-  dia_img->filename = g_strdup(filename);
+  dia_img->filename = g_strdup (filename);
   /* the pixbuf does not know anymore where it came from */
   {
     GdkPixbufFormat *format = gdk_pixbuf_get_file_info (filename, NULL, NULL);
@@ -197,9 +163,8 @@ dia_image_load(const gchar *filename)
     dia_img->mime_type = g_strdup (mime_types[0]);
     g_strfreev (mime_types);
   }
-#ifdef SCALING_CACHE
   dia_img->scaled = NULL;
-#endif
+
   return dia_img;
 }
 
@@ -217,11 +182,12 @@ dia_image_new_from_pixbuf (GdkPixbuf *pixbuf)
   DiaImage *dia_img;
   const gchar *mime_type;
 
-  dia_img = DIA_IMAGE(g_object_new(DIA_TYPE_IMAGE, NULL));
+  dia_img = DIA_IMAGE (g_object_new (DIA_TYPE_IMAGE, NULL));
   dia_img->image = g_object_ref (pixbuf);
   mime_type = g_object_get_data (G_OBJECT (pixbuf), "mime-type");
-  if (mime_type)
+  if (mime_type) {
     dia_img->mime_type = g_strdup (mime_type);
+  }
 
   return dia_img;
 }
@@ -230,18 +196,20 @@ dia_image_new_from_pixbuf (GdkPixbuf *pixbuf)
  * @param image Image that we want a reference to.
  */
 void
-dia_image_add_ref(DiaImage *image)
+dia_image_add_ref (DiaImage *image)
 {
-  g_object_ref(image);
+  // TODO: Drop
+  g_object_ref (image);
 }
 
 /** Release a reference to an image.
  * @param image Image to unreference.
  */
 void
-dia_image_unref(DiaImage *image)
+dia_image_unref (DiaImage *image)
 {
-  g_object_unref(image);
+  // TODO: Drop
+  g_object_unref (image);
 }
 
 /*!
@@ -252,31 +220,30 @@ dia_image_unref(DiaImage *image)
  * \memberof _DiaImage
  */
 GdkPixbuf *
-dia_image_get_scaled_pixbuf(DiaImage *image, int width, int height)
+dia_image_get_scaled_pixbuf (DiaImage *image, int width, int height)
 {
   GdkPixbuf *scaled;
 
-  if (width < 1 || height < 1)
+  if (width < 1 || height < 1) {
     return NULL;
-  if (gdk_pixbuf_get_width(image->image) > width ||
-      gdk_pixbuf_get_height(image->image) > height) {
+  }
+  if (gdk_pixbuf_get_width (image->image) > width ||
+      gdk_pixbuf_get_height (image->image) > height) {
     /* Using TILES to make it look more like PostScript */
-#ifdef SCALING_CACHE
     if (image->scaled == NULL ||
-       image->scaled_width != width || image->scaled_height != height) {
-      if (image->scaled)
-       g_object_unref(image->scaled);
-      image->scaled = gdk_pixbuf_scale_simple(image->image, width, height,
-       /* dont waste interpolation time if it wont be seen anyway */
-       (width * height > 256) ? GDK_INTERP_TILES : GDK_INTERP_NEAREST);
+        image->scaled_width != width || image->scaled_height != height) {
+      if (image->scaled) {
+        g_object_unref(image->scaled);
+      }
+      image->scaled = gdk_pixbuf_scale_simple (image->image,
+                                               width,
+                                               height,
+                                               /* dont waste interpolation time if it wont be seen anyway */
+                                               (width * height > 256) ? GDK_INTERP_TILES : 
GDK_INTERP_NEAREST);
       image->scaled_width = width;
       image->scaled_height = height;
     }
     scaled = image->scaled;
-#else
-    scaled = gdk_pixbuf_scale_simple(image->image, width, height,
-                                    GDK_INTERP_TILES);
-#endif
   } else {
     scaled = image->image;
   }
@@ -292,10 +259,11 @@ _guess_format (const gchar *filename)
   GSList* sl;
   gchar *type = NULL;
 
-  if (test)
+  if (test) {
     ++test;
-  else
+  } else {
     test = "png";
+  }
 
   for (sl = formats; sl != NULL; sl = g_slist_next (sl)) {
     GdkPixbufFormat* format = (GdkPixbufFormat*)sl->data;
@@ -308,14 +276,15 @@ _guess_format (const gchar *filename)
       for (i = 0; extensions[i] != NULL; ++i) {
         const gchar *ext = extensions[i];
         if (strcmp (test, ext) == 0) {
-         type = g_strdup (name);
-         break;
-       }
+          type = g_strdup (name);
+          break;
+        }
       }
       g_strfreev (extensions);
     }
-    if (type)
+    if (type) {
       break;
+    }
   }
   g_slist_free (formats);
   return type;
@@ -328,7 +297,7 @@ _guess_format (const gchar *filename)
  * \memberof _DiaImage
  */
 gboolean
-dia_image_save(DiaImage *image, const gchar *filename)
+dia_image_save (DiaImage *image, const gchar *filename)
 {
   gboolean saved = FALSE;
 
@@ -336,19 +305,21 @@ dia_image_save(DiaImage *image, const gchar *filename)
     GError *error = NULL;
     gchar *type = _guess_format (filename);
 
-    if (type) /* XXX: consider image->mime_type */
+    if (type) {
+      /* XXX: consider image->mime_type */
       saved = gdk_pixbuf_save (image->image, filename, type, &error, NULL);
+    }
     if (saved) {
       g_free (image->filename);
       image->filename = g_strdup (filename);
     } else if (!type) {
       /* pathologic case - pixbuf not even supporting PNG? */
-      message_error(_("Unsupported file format for saving:\n%s\n"),
-                    dia_message_filename(filename));
+      message_error (_("Unsupported file format for saving:\n%s\n"),
+                     dia_message_filename (filename));
     } else {
-      message_warning(_("Could not save file:\n%s\n%s\n"),
-                     dia_message_filename(filename),
-                      error->message);
+      message_warning (_("Could not save file:\n%s\n%s\n"),
+                       dia_message_filename(filename),
+                       error->message);
       g_error_free (error);
     }
 
@@ -364,10 +335,11 @@ dia_image_save(DiaImage *image, const gchar *filename)
  * \memberof _DiaImage
  */
 int
-dia_image_width(const DiaImage *image)
+dia_image_width (const DiaImage *image)
 {
   g_return_val_if_fail (image != NULL, 0);
-  return gdk_pixbuf_get_width(image->image);
+
+  return gdk_pixbuf_get_width (image->image);
 }
 
 /*!
@@ -377,10 +349,11 @@ dia_image_width(const DiaImage *image)
  * \memberof _DiaImage
  */
 int
-dia_image_height(const DiaImage *image)
+dia_image_height (const DiaImage *image)
 {
   g_return_val_if_fail (image != NULL, 0);
-  return gdk_pixbuf_get_height(image->image);
+
+  return gdk_pixbuf_get_height (image->image);
 }
 
 /*!
@@ -390,10 +363,11 @@ dia_image_height(const DiaImage *image)
  * \memberof _DiaImage
  */
 int
-dia_image_rowstride(const DiaImage *image)
+dia_image_rowstride (const DiaImage *image)
 {
   g_return_val_if_fail (image != NULL, 0);
-  return gdk_pixbuf_get_rowstride(image->image);
+
+  return gdk_pixbuf_get_rowstride (image->image);
 }
 /*!
  * \brief Direct const access to the underlying GdkPixbuf
@@ -404,8 +378,10 @@ dia_image_rowstride(const DiaImage *image)
 const GdkPixbuf*
 dia_image_pixbuf (const DiaImage *image)
 {
-  if (!image)
+  if (!image) {
     return NULL;
+  }
+
   return image->image;
 }
 
@@ -418,10 +394,13 @@ dia_image_pixbuf (const DiaImage *image)
 const gchar *
 dia_image_get_mime_type (const DiaImage *image)
 {
-  if (image->mime_type)
+  if (image->mime_type) {
     return image->mime_type;
+  }
+
   return "image/png";
 }
+
 /*!
  * \brief Set the mime-type for the image
  * @param image An image object
@@ -433,6 +412,7 @@ void
 dia_image_set_mime_type (DiaImage *image, const gchar *mime_type)
 {
   g_free (image->mime_type);
+
   image->mime_type = g_strdup (mime_type);
 }
 
@@ -444,33 +424,35 @@ dia_image_set_mime_type (DiaImage *image, const gchar *mime_type)
  * \memberof _DiaImage
  */
 guint8 *
-dia_image_rgb_data(const DiaImage *image)
+dia_image_rgb_data (const DiaImage *image)
 {
-  int width = dia_image_width(image);
-  int height = dia_image_height(image);
-  int rowstride = dia_image_rowstride(image);
-  int size = height*rowstride;
-  guint8 *rgb_pixels = g_try_malloc(size);
+  int width = dia_image_width (image);
+  int height = dia_image_height (image);
+  int rowstride = dia_image_rowstride (image);
+  int size = height * rowstride;
+  guint8 *rgb_pixels = g_try_malloc (size);
 
-  if (!rgb_pixels)
+  if (!rgb_pixels) {
     return NULL;
+  }
 
   g_return_val_if_fail (image != NULL, NULL);
-  if (gdk_pixbuf_get_has_alpha(image->image)) {
-    guint8 *pixels = gdk_pixbuf_get_pixels(image->image);
+  if (gdk_pixbuf_get_has_alpha (image->image)) {
+    guint8 *pixels = gdk_pixbuf_get_pixels (image->image);
     int i, j;
     for (i = 0; i < height; i++) {
       for (j = 0; j < width; j++) {
-       rgb_pixels[i*rowstride+j*3] = pixels[i*rowstride+j*4];
-       rgb_pixels[i*rowstride+j*3+1] = pixels[i*rowstride+j*4+1];
-       rgb_pixels[i*rowstride+j*3+2] = pixels[i*rowstride+j*4+2];
+        rgb_pixels[i*rowstride+j*3] = pixels[i*rowstride+j*4];
+        rgb_pixels[i*rowstride+j*3+1] = pixels[i*rowstride+j*4+1];
+        rgb_pixels[i*rowstride+j*3+2] = pixels[i*rowstride+j*4+2];
       }
     }
     return rgb_pixels;
   } else {
-    guint8 *pixels = gdk_pixbuf_get_pixels(image->image);
+    guint8 *pixels = gdk_pixbuf_get_pixels (image->image);
+
+    g_memmove (rgb_pixels, pixels, height*rowstride);
 
-    g_memmove(rgb_pixels, pixels, height*rowstride);
     return rgb_pixels;
   }
 }
@@ -483,28 +465,30 @@ dia_image_rgb_data(const DiaImage *image)
  * \memberof _DiaImage
  */
 guint8 *
-dia_image_mask_data(const DiaImage *image)
+dia_image_mask_data (const DiaImage *image)
 {
   guint8 *pixels;
   guint8 *mask;
   int i, size;
 
-  if (!gdk_pixbuf_get_has_alpha(image->image)) {
+  if (!gdk_pixbuf_get_has_alpha (image->image)) {
     return NULL;
   }
 
-  pixels = gdk_pixbuf_get_pixels(image->image);
+  pixels = gdk_pixbuf_get_pixels (image->image);
 
-  size = gdk_pixbuf_get_width(image->image)*
-    gdk_pixbuf_get_height(image->image);
+  size = gdk_pixbuf_get_width (image->image) *
+                                       gdk_pixbuf_get_height(image->image);
 
-  mask = g_try_malloc(size);
-  if (!mask)
+  mask = g_try_malloc (size);
+  if (!mask) {
     return NULL;
+  }
 
   /* Pick every fourth byte (the alpha channel) into mask */
-  for (i = 0; i < size; i++)
+  for (i = 0; i < size; i++) {
     mask[i] = pixels[i*4+3];
+  }
 
   return mask;
 }
@@ -517,11 +501,11 @@ dia_image_mask_data(const DiaImage *image)
  * \memberof _DiaImage
  */
 const guint8 *
-dia_image_rgba_data(const DiaImage *image)
+dia_image_rgba_data (const DiaImage *image)
 {
   g_return_val_if_fail (image != NULL, 0);
-  if (gdk_pixbuf_get_has_alpha(image->image)) {
-    const guint8 *pixels = gdk_pixbuf_get_pixels(image->image);
+  if (gdk_pixbuf_get_has_alpha (image->image)) {
+    const guint8 *pixels = gdk_pixbuf_get_pixels (image->image);
 
     return pixels;
   } else {
@@ -537,9 +521,35 @@ dia_image_rgba_data(const DiaImage *image)
  * \memberof _DiaImage
  */
 const char *
-dia_image_filename(const DiaImage *image)
+dia_image_filename (const DiaImage *image)
 {
-  if (!image->filename)
+  if (!image->filename) {
     return "(null)";
+  }
+
   return image->filename;
 }
+
+cairo_surface_t *
+dia_image_get_surface (DiaImage *self)
+{
+  cairo_t *ctx = NULL;
+
+  g_return_val_if_fail (self != NULL, NULL);
+  g_return_val_if_fail (DIA_IS_IMAGE (self), NULL);
+
+  if (self->surface != NULL) {
+    return self->surface;
+  }
+
+  self->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+                                              dia_image_width (self),
+                                              dia_image_height (self));
+  ctx = cairo_create (self->surface);
+
+  gdk_cairo_set_source_pixbuf (ctx, dia_image_pixbuf (self), 0.0, 0.0);
+
+  cairo_paint (ctx);
+
+  return self->surface;
+}
diff --git a/lib/dia_image.h b/lib/dia_image.h
index 756ed796..e30fe01c 100644
--- a/lib/dia_image.h
+++ b/lib/dia_image.h
@@ -26,37 +26,46 @@
 
 G_BEGIN_DECLS
 
-DiaImage *dia_image_get_broken(void);
+#define DIA_TYPE_IMAGE dia_image_get_type ()
 
-DiaImage *dia_image_load(const gchar *filename);
-DiaImage *dia_image_new_from_pixbuf(GdkPixbuf *pixbuf);
-void dia_image_add_ref(DiaImage *image);
-void dia_image_unref(DiaImage *image);
+G_DECLARE_FINAL_TYPE (DiaImage, dia_image, DIA, IMAGE, GObject)
 
-gboolean dia_image_save(DiaImage *image, const gchar *filename);
+DiaImage        *dia_image_get_broken        (void);
 
-int dia_image_width(const DiaImage *image);
-int dia_image_rowstride(const DiaImage *image);
-int dia_image_height(const DiaImage *image);
-/** Returns a copy of the RGB data in this image with any alpha stripped 
+DiaImage        *dia_image_load              (const gchar    *filename);
+DiaImage        *dia_image_new_from_pixbuf   (GdkPixbuf      *pixbuf);
+void             dia_image_add_ref           (DiaImage       *image);
+void             dia_image_unref             (DiaImage       *image);
+
+gboolean         dia_image_save              (DiaImage       *image,
+                                              const gchar    *filename);
+
+int              dia_image_width             (const DiaImage *image);
+int              dia_image_rowstride         (const DiaImage *image);
+int              dia_image_height            (const DiaImage *image);
+/** Returns a copy of the RGB data in this image with any alpha stripped
  * The returned buffer must be freed after use.
  * The buffer is laid out as dia_image_width*dia_image_rowstride*3 bytes.
  */
-guint8 *dia_image_rgb_data(const DiaImage *image);
+guint8          *dia_image_rgb_data          (const DiaImage *image);
 /** Returns a copy of the alpha data in this image, or NULL if none
  * The returned buffer must be freed after use.
  * The buffer is laid out as dia_image_width*dia_image_height bytes.
  */
-guint8 *dia_image_mask_data(const DiaImage *image);
+guint8          *dia_image_mask_data         (const DiaImage *image);
 /** Returns the RGBA data in this image, or NULL if there's no alpha.
  * Note that this is the raw data, not a copy.
  */
-const guint8 *dia_image_rgba_data(const DiaImage *image);
-const char *dia_image_filename(const DiaImage *image);
-const GdkPixbuf *dia_image_pixbuf (const DiaImage *image);
-const gchar *dia_image_get_mime_type (const DiaImage *image);
-void dia_image_set_mime_type (DiaImage *image, const gchar *mime_type);
+const guint8    *dia_image_rgba_data         (const DiaImage *image);
+const char      *dia_image_filename          (const DiaImage *image);
+const GdkPixbuf *dia_image_pixbuf            (const DiaImage *image);
+const gchar     *dia_image_get_mime_type     (const DiaImage *image);
+void             dia_image_set_mime_type     (DiaImage       *image,
+                                              const gchar    *mime_type);
 
+GdkPixbuf       *dia_image_get_scaled_pixbuf (DiaImage       *image,
+                                              int             width,
+                                              int             height);
 GdkPixbuf *dia_image_get_scaled_pixbuf (DiaImage *image, int width, int height);
 
 G_END_DECLS


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