[gnome-photos] base-item, export-dialog: Calculate the size & zoom in guess_save_sizes



commit e0faa7b45d51f0fa589acc437fd73eddb45bb19a
Author: Rafael Fonseca <r4f4rfs gmail com>
Date:   Mon Jun 27 15:53:06 2016 +0200

    base-item, export-dialog: Calculate the size & zoom in guess_save_sizes
    
    By making photos_guess_save_sizes_finish return a more descriptive
    data, we avoid having the caller deal with a lot of detail. Like
    checking if downscaling is needed and by how much, and interpolating
    the dimensions based on it.
    
    This will be useful for adding support for exporting albums and
    multiple images.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=760839

 src/photos-base-item.c     |   45 ++++++++++++++++++++++++++++++++---
 src/photos-base-item.h     |   14 +++++++++-
 src/photos-export-dialog.c |   56 +++++++++----------------------------------
 3 files changed, 65 insertions(+), 50 deletions(-)
---
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index 7598988..02049d6 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -142,6 +142,7 @@ struct _PhotosBaseItemSaveData
 static GdkPixbuf *failed_icon;
 static GdkPixbuf *thumbnailing_icon;
 static GThreadPool *create_thumbnail_pool;
+static const gint PIXEL_SIZES[] = {2048, 1024};
 
 
 static void photos_base_item_populate_from_cursor (PhotosBaseItem *self, TrackerSparqlCursor *cursor);
@@ -2330,12 +2331,16 @@ photos_base_item_guess_save_sizes_async (PhotosBaseItem *self,
 gboolean
 photos_base_item_guess_save_sizes_finish (PhotosBaseItem *self,
                                           GAsyncResult *res,
-                                          gsize *out_full_size,
-                                          gsize *out_reduced_size,
+                                          PhotosBaseItemSize *out_full_size,
+                                          PhotosBaseItemSize *out_reduced_size,
                                           GError **error)
 {
   GTask *task = G_TASK (res);
+  GeglRectangle bbox;
   gboolean ret_val = FALSE;
+  gint max_dimension;
+  gdouble reduced_zoom = -1.0;
+  guint i;
   gsize *sizes;
 
   g_return_val_if_fail (PHOTOS_IS_BASE_ITEM (self), FALSE);
@@ -2347,12 +2352,44 @@ photos_base_item_guess_save_sizes_finish (PhotosBaseItem *self,
   if (g_task_had_error (task))
     goto out;
 
+  if (!photos_base_item_get_bbox_edited (self, &bbox))
+    {
+      g_set_error (error, PHOTOS_ERROR, 0, "Failed to get the bounding box");
+      goto out;
+    }
+
   ret_val = TRUE;
 
+  max_dimension = MAX (bbox.height, bbox.width);
+  for (i = 0; i < G_N_ELEMENTS (PIXEL_SIZES); i++)
+    {
+      if (max_dimension > PIXEL_SIZES[i])
+        {
+          reduced_zoom = (gdouble) PIXEL_SIZES[i] / (gdouble) max_dimension;
+          break;
+        }
+    }
+
   if (out_full_size != NULL)
-    *out_full_size = sizes[0];
+    {
+      out_full_size->height = bbox.height;
+      out_full_size->width = bbox.width;
+      out_full_size->bytes = sizes[0];
+      out_full_size->zoom = 1.0;
+    }
+
   if (out_reduced_size != NULL)
-    *out_reduced_size = sizes[1];
+    {
+      out_reduced_size->zoom = reduced_zoom;
+      if (reduced_zoom > 0.0)
+        {
+          out_reduced_size->height = (gint) ((gdouble) bbox.height * reduced_zoom + 0.5);
+          out_reduced_size->width = (gint) ((gdouble) bbox.width * reduced_zoom + 0.5);
+          out_reduced_size->bytes = (gsize) (sizes[1]
+                                             + (sizes[0] - sizes[1]) * (reduced_zoom - 0.5) / (1.0 - 0.5)
+                                             + 0.5);
+        }
+    }
 
  out:
   return ret_val;
diff --git a/src/photos-base-item.h b/src/photos-base-item.h
index 29a98ae..b2c454f 100644
--- a/src/photos-base-item.h
+++ b/src/photos-base-item.h
@@ -56,6 +56,16 @@ G_BEGIN_DECLS
   (G_TYPE_INSTANCE_GET_CLASS ((obj), \
    PHOTOS_TYPE_BASE_ITEM, PhotosBaseItemClass))
 
+typedef struct _PhotosBaseItemSize PhotosBaseItemSize;
+
+struct _PhotosBaseItemSize
+{
+  gdouble zoom;
+  gint height;
+  gint width;
+  gsize bytes;
+};
+
 typedef struct _PhotosBaseItem        PhotosBaseItem;
 typedef struct _PhotosBaseItemClass   PhotosBaseItemClass;
 typedef struct _PhotosBaseItemPrivate PhotosBaseItemPrivate;
@@ -176,8 +186,8 @@ void                photos_base_item_guess_save_sizes_async  (PhotosBaseItem *se
 
 gboolean            photos_base_item_guess_save_sizes_finish (PhotosBaseItem *self,
                                                               GAsyncResult *res,
-                                                              gsize *out_full_size,
-                                                              gsize *out_reduced_size,
+                                                              PhotosBaseItemSize *out_full_size,
+                                                              PhotosBaseItemSize *out_reduced_size,
                                                               GError **error);
 
 gboolean            photos_base_item_is_collection           (PhotosBaseItem *self);
diff --git a/src/photos-export-dialog.c b/src/photos-export-dialog.c
index bbaaf37..8d76632 100644
--- a/src/photos-export-dialog.c
+++ b/src/photos-export-dialog.c
@@ -59,9 +59,6 @@ enum
 G_DEFINE_TYPE (PhotosExportDialog, photos_export_dialog, GTK_TYPE_DIALOG);
 
 
-static const gint PIXEL_SIZES[] = {2048, 1024};
-
-
 static gchar *
 photos_export_dialog_create_size_str (gint height, gint width, guint64 size)
 {
@@ -134,11 +131,12 @@ photos_export_dialog_guess_sizes (GObject *source_object, GAsyncResult *res, gpo
   PhotosExportDialog *self;
   PhotosBaseItem *item = PHOTOS_BASE_ITEM (source_object);
   GError *error;
+  PhotosBaseItemSize full;
+  PhotosBaseItemSize reduced;
   gboolean success = TRUE;
-  gsize sizes[2];
 
   error = NULL;
-  if (!photos_base_item_guess_save_sizes_finish (item, res, &sizes[0], &sizes[1], &error))
+  if (!photos_base_item_guess_save_sizes_finish (item, res, &full, &reduced, &error))
     {
       success = FALSE;
 
@@ -158,32 +156,19 @@ photos_export_dialog_guess_sizes (GObject *source_object, GAsyncResult *res, gpo
 
   if (success)
     {
-      GeglRectangle bbox;
-      gboolean got_bbox_edited;
       gchar *size_str;
       gchar *size_str_markup;
 
-      got_bbox_edited = photos_base_item_get_bbox_edited (self->item, &bbox);
-      g_return_if_fail (got_bbox_edited);
-
-      size_str = photos_export_dialog_create_size_str (bbox.height, bbox.width, (guint64) sizes[0]);
+      size_str = photos_export_dialog_create_size_str (full.height, full.width, (guint64) full.bytes);
       size_str_markup = g_strdup_printf ("<small>%s</small>", size_str);
       gtk_label_set_markup (GTK_LABEL (self->full_label), size_str_markup);
       g_free (size_str);
       g_free (size_str_markup);
 
+      self->reduced_zoom = reduced.zoom;
       if (self->reduced_zoom > 0.0)
         {
-          gint reduced_height;
-          gint reduced_width;
-          gsize reduced_size;
-
-          reduced_height = (gint) ((gdouble) bbox.height * self->reduced_zoom + 0.5);
-          reduced_width = (gint) ((gdouble) bbox.width * self->reduced_zoom + 0.5);
-          reduced_size = (gsize) (sizes[1]
-                                  + (sizes[0] - sizes[1]) * (self->reduced_zoom - 0.5) / (1.0 - 0.5)
-                                  + 0.5);
-          size_str = photos_export_dialog_create_size_str (reduced_height, reduced_width, (guint64) 
reduced_size);
+          size_str = photos_export_dialog_create_size_str (reduced.height, reduced.width, (guint64) 
reduced.bytes);
           size_str_markup = g_strdup_printf ("<small>%s</small>", size_str);
           gtk_label_set_markup (GTK_LABEL (self->reduced_label), size_str_markup);
           g_free (size_str);
@@ -191,7 +176,7 @@ photos_export_dialog_guess_sizes (GObject *source_object, GAsyncResult *res, gpo
         }
     }
 
-  photos_export_dialog_show_size_options (self, TRUE, FALSE);
+  photos_export_dialog_show_size_options (self, self->reduced_zoom > 0.0, FALSE);
 }
 
 
@@ -225,28 +210,11 @@ photos_export_dialog_load (GObject *source_object, GAsyncResult *result, gpointe
 
   if (node != NULL)
     {
-      GeglRectangle bbox;
-      gboolean got_bbox_edited;
-      gint max_dimension;
-      guint i;
-
-      got_bbox_edited = photos_base_item_get_bbox_edited (self->item, &bbox);
-      g_return_if_fail (got_bbox_edited);
-
-      max_dimension = MAX (bbox.height, bbox.width);
-      for (i = 0; i < G_N_ELEMENTS (PIXEL_SIZES); i++)
-        {
-          if (max_dimension > PIXEL_SIZES[i])
-            {
-              self->reduced_zoom = (gdouble) PIXEL_SIZES[i] / (gdouble) max_dimension;
-              photos_export_dialog_show_size_options (self, FALSE, TRUE);
-              photos_base_item_guess_save_sizes_async (self->item,
-                                                       self->cancellable,
-                                                       photos_export_dialog_guess_sizes,
-                                                       self);
-              break;
-            }
-        }
+      photos_export_dialog_show_size_options (self, FALSE, TRUE);
+      photos_base_item_guess_save_sizes_async (self->item,
+                                               self->cancellable,
+                                               photos_export_dialog_guess_sizes,
+                                               self);
     }
 
  out:


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