[gnome-photos/wip/rishi/buffer-decoder: 1/7] Add PhotosGeglBufferLoaderBuilder



commit 1d5cf72a9fcb811fc11f5f2789430c2e684d9b3e
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/Makefile.am                         |   2 +
 src/meson.build                         |   1 +
 src/photos-gegl-buffer-loader-builder.c | 144 ++++++++++++++++++++++++++
 src/photos-gegl-buffer-loader-builder.h |  53 ++++++++++
 tests/unit/Makefile.am                  |  18 ++++
 tests/unit/meson.build                  |   3 +
 tests/unit/photos-test-gegl-buffer.c    | 176 ++++++++++++++++++++++++++++++++
 7 files changed, 397 insertions(+)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 344761d4..161bcf93 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -30,6 +30,8 @@ libgnome_photos_la_SOURCES = \
        photos-gegl-buffer-codec.h \
        photos-gegl-buffer-loader.c \
        photos-gegl-buffer-loader.h \
+       photos-gegl-buffer-loader-builder.c \
+       photos-gegl-buffer-loader-builder.h \
        photos-jpeg-count.c \
        photos-jpeg-count.h \
        photos-operation-insta-common.h \
diff --git a/src/meson.build b/src/meson.build
index 9e3caee0..30c58d4c 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-jpeg-count.c',
   'photos-operation-insta-curve.c',
   'photos-operation-insta-filter.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/Makefile.am b/tests/unit/Makefile.am
index c180591e..c9421551 100644
--- a/tests/unit/Makefile.am
+++ b/tests/unit/Makefile.am
@@ -11,6 +11,7 @@ dist_test_data = \
 
 test_programs = \
        photos-test-gegl \
+       photos-test-gegl-buffer \
        photos-test-pipeline \
        $(NULL)
 
@@ -31,6 +32,23 @@ photos_test_gegl_LDADD = \
        $(top_builddir)/src/libgnome-photos.la \
        $(NULL)
 
+photos_test_gegl_buffer_CPPFLAGS = \
+       $(GDK_PIXBUF_CFLAGS) \
+       $(GEGL_CFLAGS) \
+       $(GIO_CFLAGS) \
+       $(GLIB_CFLAGS) \
+       -I$(top_builddir)/src \
+       -I$(top_srcdir)/src \
+       $(NULL)
+
+photos_test_gegl_buffer_LDADD = \
+       $(GDK_PIXBUF_LIBS) \
+       $(GEGL_LIBS) \
+       $(GIO_LIBS) \
+       $(GLIB_LIBS) \
+       $(top_builddir)/src/libgnome-photos.la \
+       $(NULL)
+
 photos_test_pipeline_CPPFLAGS = \
        $(GDK_PIXBUF_CFLAGS) \
        $(GEGL_CFLAGS) \
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 623fbe55..888567e7 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -15,6 +15,9 @@ tests = {
   'photos-test-gegl': {
     'dependencies': [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..03ef5e40
--- /dev/null
+++ b/tests/unit/photos-test-gegl-buffer.c
@@ -0,0 +1,176 @@
+/*
+ * 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 <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"
+
+
+typedef struct _PhotosTestGeglBufferFixture PhotosTestGeglBufferFixture;
+
+struct _PhotosTestGeglBufferFixture
+{
+  GAsyncResult *res;
+  GMainContext *context;
+  GMainLoop *loop;
+};
+
+
+static gchar *
+photos_test_gegl_buffer_filename_to_uri (const gchar *filename)
+{
+  g_autoptr (GFile) file = NULL;
+  g_autofree gchar *path_relative = NULL;
+  gchar *uri = NULL;
+
+  path_relative = g_test_build_filename (G_TEST_DIST, filename, NULL);
+  file = g_file_new_for_path (path_relative);
+  uri = g_file_get_uri (file);
+  return uri;
+}
+
+
+static void
+photos_test_gegl_buffer_setup (PhotosTestGeglBufferFixture *fixture, gconstpointer user_data)
+{
+  fixture->context = g_main_context_new ();
+  g_main_context_push_thread_default (fixture->context);
+  fixture->loop = g_main_loop_new (fixture->context, FALSE);
+}
+
+
+static void
+photos_test_gegl_buffer_teardown (PhotosTestGeglBufferFixture *fixture, gconstpointer user_data)
+{
+  g_clear_object (&fixture->res);
+  g_main_context_pop_thread_default (fixture->context);
+  g_main_context_unref (fixture->context);
+  g_main_loop_unref (fixture->loop);
+}
+
+
+static void
+photos_test_gegl_buffer_async (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+  PhotosTestGeglBufferFixture *fixture = (PhotosTestGeglBufferFixture *) user_data;
+
+  g_assert_null (fixture->res);
+  fixture->res = g_object_ref (res);
+  g_main_loop_quit (fixture->loop);
+}
+
+
+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]