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").
This commit is contained in:
Michael Natterer 2013-11-01 17:36:26 +01:00
parent e03fd943bf
commit ce57aef6ee
1 changed files with 16 additions and 4 deletions

View File

@ -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;
}
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.XResolution",
ROUND (xres * factor), factor);
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);