[gnome-photos/wip/rishi/buffer-decoder: 5/12] Add PhotosGeglBufferLoaderBuilder




commit 6fc89a265a03818da2e5b59a84328726fa8e0351
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Aug 1 17:16:47 2018 +0200

    Add PhotosGeglBufferLoaderBuilder
    
    https://gitlab.gnome.org/GNOME/gnome-photos/issues/63

 src/meson.build                         |   1 +
 src/photos-gegl-buffer-loader-builder.c | 144 ++++++++++++++++++++++++++++++++
 src/photos-gegl-buffer-loader-builder.h |  53 ++++++++++++
 tests/unit/meson.build                  |   3 +
 tests/unit/photos-test-gegl-buffer.c    | 122 +++++++++++++++++++++++++++
 5 files changed, 323 insertions(+)
---
diff --git a/src/meson.build b/src/meson.build
index 83b1a152..552270fa 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -6,6 +6,7 @@ sources = files(
   'photos-gegl.c',
   'photos-gegl-buffer-codec.c',
   'photos-gegl-buffer-loader.c',
+  'photos-gegl-buffer-loader-builder.c',
   'photos-glib.c',
   'photos-jpeg-count.c',
   'photos-operation-insta-clarendon.c',
diff --git a/src/photos-gegl-buffer-loader-builder.c b/src/photos-gegl-buffer-loader-builder.c
new file mode 100644
index 00000000..0146564f
--- /dev/null
+++ b/src/photos-gegl-buffer-loader-builder.c
@@ -0,0 +1,144 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2018 Red Hat, Inc.
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "config.h"
+
+#include "photos-gegl-buffer-loader-builder.h"
+
+
+struct _PhotosGeglBufferLoaderBuilder
+{
+  GObject parent_instance;
+  GFile *file;
+  gboolean keep_aspect_ratio;
+  gboolean sealed;
+  gint height;
+  gint width;
+};
+
+
+G_DEFINE_TYPE (PhotosGeglBufferLoaderBuilder, photos_gegl_buffer_loader_builder, G_TYPE_OBJECT);
+
+
+static void
+photos_gegl_buffer_loader_builder_dispose (GObject *object)
+{
+  PhotosGeglBufferLoaderBuilder *self = PHOTOS_GEGL_BUFFER_LOADER_BUILDER (object);
+
+  g_clear_object (&self->file);
+
+  G_OBJECT_CLASS (photos_gegl_buffer_loader_builder_parent_class)->dispose (object);
+}
+
+
+static void
+photos_gegl_buffer_loader_builder_init (PhotosGeglBufferLoaderBuilder *self)
+{
+  self->keep_aspect_ratio = TRUE;
+  self->height = -1;
+  self->width = -1;
+}
+
+
+static void
+photos_gegl_buffer_loader_builder_class_init (PhotosGeglBufferLoaderBuilderClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->dispose = photos_gegl_buffer_loader_builder_dispose;
+}
+
+
+PhotosGeglBufferLoaderBuilder *
+photos_gegl_buffer_loader_builder_new (void)
+{
+  return g_object_new (PHOTOS_TYPE_GEGL_BUFFER_LOADER_BUILDER, NULL);
+}
+
+
+PhotosGeglBufferLoaderBuilder *
+photos_gegl_buffer_loader_builder_set_file (PhotosGeglBufferLoaderBuilder *self, GFile *file)
+{
+  g_return_val_if_fail (PHOTOS_IS_GEGL_BUFFER_LOADER_BUILDER (self), NULL);
+  g_return_val_if_fail (!self->sealed, NULL);
+
+  g_return_val_if_fail (G_IS_FILE (file), NULL);
+
+  g_set_object (&self->file, file);
+  return self;
+}
+
+
+PhotosGeglBufferLoaderBuilder *
+photos_gegl_buffer_loader_builder_set_height (PhotosGeglBufferLoaderBuilder *self, gint height)
+{
+  g_return_val_if_fail (PHOTOS_IS_GEGL_BUFFER_LOADER_BUILDER (self), NULL);
+  g_return_val_if_fail (height >= -1, NULL);
+  g_return_val_if_fail (height != 0, NULL);
+  g_return_val_if_fail (!self->sealed, NULL);
+
+  self->height = height;
+  return self;
+}
+
+
+PhotosGeglBufferLoaderBuilder *
+photos_gegl_buffer_loader_builder_set_keep_aspect_ratio (PhotosGeglBufferLoaderBuilder *self,
+                                                         gboolean keep_aspect_ratio)
+{
+  g_return_val_if_fail (PHOTOS_IS_GEGL_BUFFER_LOADER_BUILDER (self), NULL);
+  g_return_val_if_fail (!self->sealed, NULL);
+
+  self->keep_aspect_ratio = keep_aspect_ratio;
+  return self;
+}
+
+
+PhotosGeglBufferLoaderBuilder *
+photos_gegl_buffer_loader_builder_set_width (PhotosGeglBufferLoaderBuilder *self, gint width)
+{
+  g_return_val_if_fail (PHOTOS_IS_GEGL_BUFFER_LOADER_BUILDER (self), NULL);
+  g_return_val_if_fail (width >= -1, NULL);
+  g_return_val_if_fail (width != 0, NULL);
+  g_return_val_if_fail (!self->sealed, NULL);
+
+  self->width = width;
+  return self;
+}
+
+
+PhotosGeglBufferLoader *
+photos_gegl_buffer_loader_builder_to_loader (PhotosGeglBufferLoaderBuilder *self)
+{
+  PhotosGeglBufferLoader *loader;
+
+  g_return_val_if_fail (PHOTOS_IS_GEGL_BUFFER_LOADER_BUILDER (self), NULL);
+  g_return_val_if_fail (!self->sealed, NULL);
+
+  self->sealed = TRUE;
+  loader = g_object_new (PHOTOS_TYPE_GEGL_BUFFER_LOADER,
+                         "file", self->file,
+                         "height", self->height,
+                         "keep-aspect-ratio", self->keep_aspect_ratio,
+                         "width", self->width,
+                         NULL);
+
+  g_return_val_if_fail (self->sealed, NULL);
+  return loader;
+}
diff --git a/src/photos-gegl-buffer-loader-builder.h b/src/photos-gegl-buffer-loader-builder.h
new file mode 100644
index 00000000..cfb8aac6
--- /dev/null
+++ b/src/photos-gegl-buffer-loader-builder.h
@@ -0,0 +1,53 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2018 Red Hat, Inc.
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PHOTOS_GEGL_BUFFER_LOADER_BUILDER_H
+#define PHOTOS_GEGL_BUFFER_LOADER_BUILDER_H
+
+#include <gio/gio.h>
+
+#include "photos-gegl-buffer-loader.h"
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_GEGL_BUFFER_LOADER_BUILDER (photos_gegl_buffer_loader_builder_get_type ())
+G_DECLARE_FINAL_TYPE (PhotosGeglBufferLoaderBuilder,
+                      photos_gegl_buffer_loader_builder,
+                      PHOTOS,
+                      GEGL_BUFFER_LOADER_BUILDER,
+                      GObject);
+
+PhotosGeglBufferLoaderBuilder *photos_gegl_buffer_loader_builder_new         (void);
+
+PhotosGeglBufferLoaderBuilder *photos_gegl_buffer_loader_builder_set_file    (PhotosGeglBufferLoaderBuilder 
*self,
+                                                                              GFile *file);
+
+PhotosGeglBufferLoaderBuilder *photos_gegl_buffer_loader_builder_set_height  (PhotosGeglBufferLoaderBuilder 
*self,
+                                                                              gint height);
+
+PhotosGeglBufferLoaderBuilder *photos_gegl_buffer_loader_builder_set_keep_aspect_ratio 
(PhotosGeglBufferLoaderBuilder *self,
+                                                                                        gboolean 
keep_aspect_ratio);
+
+PhotosGeglBufferLoaderBuilder *photos_gegl_buffer_loader_builder_set_width   (PhotosGeglBufferLoaderBuilder 
*self,
+                                                                              gint width);
+
+PhotosGeglBufferLoader        *photos_gegl_buffer_loader_builder_to_loader   (PhotosGeglBufferLoaderBuilder 
*self);
+
+G_END_DECLS
+
+#endif /* PHOTOS_GEGL_BUFFER_LOADER_BUILDER_H */
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 96c3ae02..f6390fb2 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -15,6 +15,9 @@ tests = {
   'photos-test-gegl': {
     'dependencies': [babl_dep, gdk_pixbuf_dep, gegl_dep, gio_dep, gio_unix_dep, glib_dep, 
libgnome_photos_dep]
   },
+  'photos-test-gegl-buffer': {
+    'dependencies': [gdk_pixbuf_dep, gegl_dep, gio_dep, gio_unix_dep, glib_dep, libgnome_photos_dep]
+  },
   'photos-test-pipeline': {
     'dependencies': [gdk_pixbuf_dep, gegl_dep, gio_dep, gio_unix_dep, glib_dep, libgnome_photos_dep]
   },
diff --git a/tests/unit/photos-test-gegl-buffer.c b/tests/unit/photos-test-gegl-buffer.c
new file mode 100644
index 00000000..8c51d499
--- /dev/null
+++ b/tests/unit/photos-test-gegl-buffer.c
@@ -0,0 +1,122 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2018 – 2019 Red Hat, Inc.
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "config.h"
+
+#include <locale.h>
+
+#include <gegl.h>
+#include <gio/gio.h>
+#include <glib.h>
+
+#include "photos-debug.h"
+#include "photos-gegl.h"
+#include "photos-gegl-buffer-loader.h"
+#include "photos-gegl-buffer-loader-builder.h"
+
+
+static void
+photos_test_gegl_buffer_loader_builder (void)
+{
+  GFile *file_loader;
+  g_autoptr (GFile) file_builder = NULL;
+  GeglBuffer *buffer;
+  g_autoptr (PhotosGeglBufferLoader) loader = NULL;
+  g_autoptr (PhotosGeglBufferLoaderBuilder) builder = NULL;
+  gint height;
+  gint width;
+
+  builder = photos_gegl_buffer_loader_builder_new ();
+
+  file_builder = g_file_new_for_uri ("resource:///org/gnome/Photos/gegl/vignette.png");
+  photos_gegl_buffer_loader_builder_set_file (builder, file_builder);
+
+  photos_gegl_buffer_loader_builder_set_height (builder, 200);
+  photos_gegl_buffer_loader_builder_set_keep_aspect_ratio (builder, FALSE);
+  photos_gegl_buffer_loader_builder_set_width (builder, 300);
+
+  loader = photos_gegl_buffer_loader_builder_to_loader (builder);
+  g_assert_true (PHOTOS_IS_GEGL_BUFFER_LOADER (loader));
+
+  buffer = photos_gegl_buffer_loader_get_buffer (loader);
+  g_assert_null (buffer);
+
+  file_loader = photos_gegl_buffer_loader_get_file (loader);
+  g_assert_true (file_loader == file_builder);
+
+  height = photos_gegl_buffer_loader_get_height (loader);
+  g_assert_cmpint (height, ==, 200);
+
+  g_assert_false (photos_gegl_buffer_loader_get_keep_aspect_ratio (loader));
+
+  width = photos_gegl_buffer_loader_get_width (loader);
+  g_assert_cmpint (width, ==, 300);
+}
+
+
+static void
+photos_test_gegl_buffer_loader_builder_defaults (void)
+{
+  GFile *file = NULL;
+  GeglBuffer *buffer;
+  g_autoptr (PhotosGeglBufferLoader) loader = NULL;
+  g_autoptr (PhotosGeglBufferLoaderBuilder) builder = NULL;
+  gint height;
+  gint width;
+
+  builder = photos_gegl_buffer_loader_builder_new ();
+  loader = photos_gegl_buffer_loader_builder_to_loader (builder);
+  g_assert_true (PHOTOS_IS_GEGL_BUFFER_LOADER (loader));
+
+  buffer = photos_gegl_buffer_loader_get_buffer (loader);
+  g_assert_null (buffer);
+
+  file = photos_gegl_buffer_loader_get_file (loader);
+  g_assert_null (file);
+
+  height = photos_gegl_buffer_loader_get_height (loader);
+  g_assert_cmpint (height, ==, -1);
+
+  g_assert_true (photos_gegl_buffer_loader_get_keep_aspect_ratio (loader));
+
+  width = photos_gegl_buffer_loader_get_width (loader);
+  g_assert_cmpint (width, ==, -1);
+}
+
+
+gint
+main (gint argc, gchar *argv[])
+{
+  gint exit_status;
+
+  setlocale (LC_ALL, "");
+  g_setenv ("GEGL_THREADS", "1", FALSE);
+  g_test_init (&argc, &argv, NULL);
+  photos_debug_init ();
+  photos_gegl_init ();
+  photos_gegl_ensure_builtins ();
+
+  g_test_add_func ("/gegl/buffer/loader-builder", photos_test_gegl_buffer_loader_builder);
+  g_test_add_func ("/gegl/buffer/loader-builder-defaults", photos_test_gegl_buffer_loader_builder_defaults);
+
+  exit_status = g_test_run ();
+
+  gegl_exit ();
+  return exit_status;
+}


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