[gnome-photos/wip/rishi/unit-tests-gegl: 4/4] gegl: Add photos_gegl_pixbuf_new_from_buffer



commit df3f543b0075df8e5cec9f978ecd091fec3ee8b3
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Nov 28 19:08:37 2018 +0100

    gegl: Add photos_gegl_pixbuf_new_from_buffer

 src/photos-gegl.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 src/photos-gegl.h |  2 ++
 2 files changed, 45 insertions(+)
---
diff --git a/src/photos-gegl.c b/src/photos-gegl.c
index 171d1a76..a32d49ba 100644
--- a/src/photos-gegl.c
+++ b/src/photos-gegl.c
@@ -701,6 +701,49 @@ photos_gegl_init_fishes (void)
 }
 
 
+GdkPixbuf *
+photos_gegl_pixbuf_new_from_buffer (GeglBuffer *buffer)
+{
+  const Babl *format_buffer;
+  const Babl *format_pixbuf;
+  g_autoptr (GBytes) bytes = NULL;
+  GdkPixbuf *pixbuf = NULL;
+  GeglRectangle bbox;
+  gboolean has_alpha;
+  gint bpp;
+  gint stride;
+  gpointer buf = NULL;
+  gsize size;
+
+  g_return_val_if_fail (GEGL_IS_BUFFER (buffer), NULL);
+
+  bbox = *gegl_buffer_get_extent (buffer);
+  format_buffer = gegl_buffer_get_format (buffer);
+  has_alpha = babl_format_has_alpha (format_buffer);
+
+  if (has_alpha)
+    format_pixbuf = babl_format ("R'G'B'A u8");
+  else
+    format_pixbuf = babl_format ("R'G'B' u8");
+
+  bpp = babl_format_get_bytes_per_pixel (format_pixbuf);
+  if (bpp > 0 && bbox.width > 0 && bbox.width > G_MAXINT / bpp)
+    goto out;
+
+  stride = bpp * bbox.width;
+
+  buf = g_malloc0_n ((gsize) bbox.height, (gsize) stride);
+  gegl_buffer_get (buffer, &bbox, 1.0, format_pixbuf, buf, stride, GEGL_ABYSS_NONE);
+
+  size = (gsize) bbox.height * (gsize) stride;
+  bytes = g_bytes_new_take (buf, size);
+  pixbuf = gdk_pixbuf_new_from_bytes (bytes, GDK_COLORSPACE_RGB, has_alpha, 8, bbox.width, bbox.height, 
stride);
+
+ out:
+  return pixbuf;
+}
+
+
 static gboolean
 photos_gegl_processor_process_idle (gpointer user_data)
 {
diff --git a/src/photos-gegl.h b/src/photos-gegl.h
index 716458b3..550a1dd8 100644
--- a/src/photos-gegl.h
+++ b/src/photos-gegl.h
@@ -53,6 +53,8 @@ void             photos_gegl_init                         (void);
 
 void             photos_gegl_init_fishes                  (void);
 
+GdkPixbuf       *photos_gegl_pixbuf_new_from_buffer       (GeglBuffer *buffer);
+
 void             photos_gegl_processor_process_async      (GeglProcessor *processor,
                                                            GCancellable *cancellable,
                                                            GAsyncReadyCallback callback,


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