[gimp] libgimpbase: fix gimp_metadata_set_resolution() to only set rationals



commit ce57aef6ee22937a6ed3b693b74914dcff0adfb9
Author: Michael Natterer <mitch gimp org>
Date:   Fri Nov 1 17:36:26 2013 +0100

    libgimpbase: fix gimp_metadata_set_resolution() to only set rationals
    
    Rationals have integers as nominator and denominator, so we can't set
    something like "300.5/1". Instead, multiply the nominator and
    denominator of non-integer ppi values until the error is smaller than
    0.01, or the denomiator reaches 100 ("300.5/1" becomes "601/2").

 libgimpbase/gimpmetadata.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)
---
diff --git a/libgimpbase/gimpmetadata.c b/libgimpbase/gimpmetadata.c
index df68254..55e8baa 100644
--- a/libgimpbase/gimpmetadata.c
+++ b/libgimpbase/gimpmetadata.c
@@ -28,6 +28,8 @@
 #include <gio/gio.h>
 #include <gexiv2/gexiv2.h>
 
+#include "libgimpmath/gimpmath.h"
+
 #include "gimpbasetypes.h"
 
 #include "gimplimits.h"
@@ -821,6 +823,7 @@ gimp_metadata_set_resolution (GimpMetadata *metadata,
 {
   gchar buffer[32];
   gint  exif_unit;
+  gint  factor;
 
   g_return_if_fail (GEXIV2_IS_METADATA (metadata));
 
@@ -836,11 +839,20 @@ gimp_metadata_set_resolution (GimpMetadata *metadata,
       exif_unit = 2;
     }
 
-  g_ascii_formatd (buffer, sizeof (buffer), "%.0f/1", xres);
-  gexiv2_metadata_set_tag_string (metadata, "Exif.Image.XResolution", buffer);
+  for (factor = 1; factor <= 100 /* arbitrary */; factor++)
+    {
+      if (fabs (xres * factor - ROUND (xres * factor)) < 0.01 &&
+          fabs (yres * factor - ROUND (yres * factor)) < 0.01)
+        break;
+    }
+
+  gexiv2_metadata_set_exif_tag_rational (metadata,
+                                         "Exif.Image.XResolution",
+                                         ROUND (xres * factor), factor);
 
-  g_ascii_formatd (buffer, sizeof (buffer), "%.0f/1", yres);
-  gexiv2_metadata_set_tag_string (metadata, "Exif.Image.YResolution", buffer);
+  gexiv2_metadata_set_exif_tag_rational (metadata,
+                                         "Exif.Image.YResolution",
+                                         ROUND (yres * factor), factor);
 
   g_snprintf (buffer, sizeof (buffer), "%d", exif_unit);
   gexiv2_metadata_set_tag_string (metadata, "Exif.Image.ResolutionUnit", buffer);


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