[gnome-photos/wip/rishi/misc-fixes: 21/22] base-item: Add photos_base_item_save_guess_sizes_async



commit 07c8900482c3df5ee08edda0e6fc983bb8f6ef5b
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Dec 23 16:49:13 2015 +0100

    base-item: Add photos_base_item_save_guess_sizes_async
    
    https://bugzilla.gnome.org/show_bug.cgi?id=759363

 src/photos-base-item.c |  117 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/photos-base-item.h |   11 +++++
 2 files changed, 128 insertions(+), 0 deletions(-)
---
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index ffbbdf9..ee850a5 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -970,6 +970,53 @@ photos_base_item_process_idle (gpointer user_data)
 
 
 static void
+photos_base_item_save_guess_sizes_from_buffer (GCancellable *cancellable,
+                                               GeglBuffer *buffer,
+                                               gsize *out_size,
+                                               gsize *out_size_1)
+{
+  GeglNode *buffer_source;
+  GeglNode *graph;
+  GeglNode *guess_sizes;
+  gsize sizes[0];
+
+  graph = gegl_node_new ();
+  buffer_source = gegl_node_new_child (graph, "operation", "gegl:buffer-source", "buffer", buffer, NULL);
+  guess_sizes = gegl_node_new_child (graph,
+                                     "operation", "photos:jpg-guess-sizes",
+                                     "optimize", FALSE,
+                                     "progressive", FALSE,
+                                     "sampling", TRUE,
+                                     NULL);
+  gegl_node_link (buffer_source, guess_sizes);
+  gegl_node_process (guess_sizes);
+
+  gegl_node_get (guess_sizes, "size", &sizes[0], "size-1", &sizes[1], NULL);
+  if (out_size != NULL)
+    *out_size = sizes[0];
+  if (out_size_1 != NULL)
+    *out_size_1 = sizes[1];
+
+  g_object_unref (graph);
+}
+
+
+static void
+photos_base_item_save_guess_sizes_in_thread_func (GTask *task,
+                                                  gpointer source_object,
+                                                  gpointer task_data,
+                                                  GCancellable *cancellable)
+{
+  GeglBuffer *buffer = GEGL_BUFFER (task_data);
+  gsize *sizes;
+
+  sizes = g_malloc0_n (2, sizeof (gsize));
+  photos_base_item_save_guess_sizes_from_buffer (cancellable, buffer, &sizes[0], &sizes[1]);
+  g_task_return_pointer (task, sizes, g_free);
+}
+
+
+static void
 photos_base_item_save_save_to_stream (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
   GTask *task = G_TASK (user_data);
@@ -2125,6 +2172,76 @@ photos_base_item_save_finish (PhotosBaseItem *self, GAsyncResult *res, GError **
 
 
 void
+photos_base_item_save_guess_sizes_async (PhotosBaseItem *self,
+                                         GCancellable *cancellable,
+                                         GAsyncReadyCallback callback,
+                                         gpointer user_data)
+{
+  PhotosBaseItemPrivate *priv;
+  const Babl *format;
+  GeglBuffer *buffer;
+  GeglNode *graph;
+  GeglRectangle bbox;
+  GTask *task;
+
+  g_return_if_fail (PHOTOS_IS_BASE_ITEM (self));
+  priv = self->priv;
+
+  g_return_if_fail (priv->edit_graph != NULL);
+  g_return_if_fail (priv->filename != NULL && priv->filename[0] != '\0');
+  g_return_if_fail (priv->load_graph != NULL);
+  g_return_if_fail (priv->processor != NULL);
+  g_return_if_fail (!gegl_processor_work (priv->processor, NULL));
+
+  graph = photos_pipeline_get_graph (priv->pipeline);
+  bbox = gegl_node_get_bounding_box (graph);
+  format = babl_format ("R'G'B' u8");
+  buffer = gegl_buffer_new (&bbox, format);
+  gegl_node_blit_buffer (graph, buffer, &bbox, 0, GEGL_ABYSS_NONE);
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, photos_base_item_save_guess_sizes_async);
+  g_task_set_task_data (task, g_object_ref (buffer), g_object_unref);
+
+  g_task_run_in_thread (task, photos_base_item_save_guess_sizes_in_thread_func);
+
+  g_object_unref (buffer);
+  g_object_unref (task);
+}
+
+
+gboolean
+photos_base_item_save_guess_sizes_finish (PhotosBaseItem *self,
+                                          GAsyncResult *res,
+                                          gsize *out_size,
+                                          gsize *out_size_1,
+                                          GError **error)
+{
+  GTask *task = G_TASK (res);
+  gboolean ret_val = FALSE;
+  gsize *sizes;
+
+  g_return_val_if_fail (g_task_is_valid (res, self), FALSE);
+  g_return_val_if_fail (g_task_get_source_tag (task) == photos_base_item_save_guess_sizes_async, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  sizes = g_task_propagate_pointer (task, error);
+  if (g_task_had_error (task))
+    goto out;
+
+  ret_val = TRUE;
+
+  if (out_size != NULL)
+    *out_size = sizes[0];
+  if (out_size_1 != NULL)
+    *out_size_1 = sizes[1];
+
+ out:
+  return ret_val;
+}
+
+
+void
 photos_base_item_save_to_dir_async (PhotosBaseItem *self,
                                     GFile *dir,
                                     GCancellable *cancellable,
diff --git a/src/photos-base-item.h b/src/photos-base-item.h
index 792f60f..ad13e13 100644
--- a/src/photos-base-item.h
+++ b/src/photos-base-item.h
@@ -231,6 +231,17 @@ gboolean            photos_base_item_save_finish             (PhotosBaseItem *se
                                                               GAsyncResult *res,
                                                               GError **error);
 
+void                photos_base_item_save_guess_sizes_async  (PhotosBaseItem *self,
+                                                              GCancellable *cancellable,
+                                                              GAsyncReadyCallback callback,
+                                                              gpointer user_data);
+
+gboolean            photos_base_item_save_guess_sizes_finish (PhotosBaseItem *self,
+                                                              GAsyncResult *res,
+                                                              gsize *out_size,
+                                                              gsize *out_size_1,
+                                                              GError **error);
+
 void                photos_base_item_save_to_dir_async       (PhotosBaseItem *self,
                                                               GFile *dir,
                                                               GCancellable *cancellable,


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