[gnome-photos/wip/rishi/guess-sizes: 2/3] base-item: use a more descriptive struct for save_sizes.



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

    base-item: use a more descriptive struct for save_sizes.
    
    By making guest_save_sizes_finish return a more descriptive data, we
    avoid having the caller deal with edited bbox besides hiding the
    interpolation for the reduced size calculation inside base-item.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=760839

 src/photos-base-item.c     |   44 +++++++++++++++++++++++++++++++---
 src/photos-base-item.h     |   13 ++++++++-
 src/photos-export-dialog.c |   56 +++++++++----------------------------------
 3 files changed, 63 insertions(+), 50 deletions(-)
---
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index 6dc1851..36aad5c 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -120,6 +120,8 @@ enum
 
 static guint signals[LAST_SIGNAL] = { 0 };
 
+static const gint PIXEL_SIZES[] = {2048, 1024};
+
 static void photos_base_item_filterable_iface_init (PhotosFilterableInterface *iface);
 
 
@@ -2330,13 +2332,18 @@ photos_base_item_guess_save_sizes_async (PhotosBaseItem *self,
 gboolean
 photos_base_item_guess_save_sizes_finish (PhotosBaseItem *self,
                                           GAsyncResult *res,
-                                          gsize *full_size,
-                                          gsize *reduced_size,
+                                          PhotosBaseItemSize *full_size,
+                                          PhotosBaseItemSize *reduced_size,
                                           GError **error)
 {
   GTask *task = G_TASK (res);
   gboolean ret_val = FALSE;
   gsize *sizes;
+  GeglRectangle bbox;
+  gboolean got_bbox_edited;
+  gint max_dimension;
+  gdouble reduced_zoom;
+  guint i;
 
   g_return_val_if_fail (PHOTOS_IS_BASE_ITEM (self), FALSE);
   g_return_val_if_fail (g_task_is_valid (res, self), FALSE);
@@ -2347,12 +2354,41 @@ photos_base_item_guess_save_sizes_finish (PhotosBaseItem *self,
   if (g_task_had_error (task))
     goto out;
 
+  got_bbox_edited = photos_base_item_get_bbox_edited (self, &bbox);
+  if (!got_bbox_edited)
+    goto out;
+
   ret_val = TRUE;
 
+  max_dimension = MAX (bbox.height, bbox.width);
+  reduced_zoom = -1;
+  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 (full_size != NULL)
-    *full_size = sizes[0];
+    {
+      full_size->height = bbox.height;
+      full_size->width = bbox.width;
+      full_size->bytes = sizes[0];
+      full_size->zoom = 1;
+    }
+
   if (reduced_size != NULL)
-    *reduced_size = sizes[1];
+    {
+      reduced_size->zoom = reduced_zoom;
+      if (reduced_zoom > 0.0)
+        {
+          reduced_size->height = (gint) ((gdouble) bbox.height * reduced_zoom + 0.5);
+          reduced_size->width = (gint) ((gdouble) bbox.width * reduced_zoom + 0.5);
+          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 281ba34..d4b5620 100644
--- a/src/photos-base-item.h
+++ b/src/photos-base-item.h
@@ -59,6 +59,7 @@ G_BEGIN_DECLS
 typedef struct _PhotosBaseItem        PhotosBaseItem;
 typedef struct _PhotosBaseItemClass   PhotosBaseItemClass;
 typedef struct _PhotosBaseItemPrivate PhotosBaseItemPrivate;
+typedef struct _PhotosBaseItemSize    PhotosBaseItemSize;
 
 struct _PhotosBaseItem
 {
@@ -89,6 +90,14 @@ struct _PhotosBaseItemClass
   void        (*info_updated)               (PhotosBaseItem *self);
 };
 
+struct _PhotosBaseItemSize
+{
+  gint height;
+  gint width;
+  gsize bytes;
+  gdouble zoom;
+};
+
 GType               photos_base_item_get_type                (void) G_GNUC_CONST;
 
 gboolean            photos_base_item_can_edit                (PhotosBaseItem *self);
@@ -176,8 +185,8 @@ void                photos_base_item_guess_save_sizes_async  (PhotosBaseItem *se
 
 gboolean            photos_base_item_guess_save_sizes_finish (PhotosBaseItem *self,
                                                               GAsyncResult *res,
-                                                              gsize *full_size,
-                                                              gsize *reduced_size,
+                                                              PhotosBaseItemSize *full_size,
+                                                              PhotosBaseItemSize *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..d664abb 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)
 {
@@ -135,10 +132,11 @@ photos_export_dialog_guess_sizes (GObject *source_object, GAsyncResult *res, gpo
   PhotosBaseItem *item = PHOTOS_BASE_ITEM (source_object);
   GError *error;
   gboolean success = TRUE;
-  gsize sizes[2];
+  PhotosBaseItemSize full;
+  PhotosBaseItemSize reduced;
 
   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]