From 7024deccab987d53bddc2a78cfaa00869dc9f1a5 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sun, 5 Feb 2017 13:14:35 +0100 Subject: [PATCH] Added method to update the profile data (based on #96) --- .../JpegEncoderCore.cs | 1 + src/ImageSharp/MetaData/ImageMetaData.cs | 29 ++++++++++++++++ .../MetaData/ImageMetaDataTests.cs | 33 +++++++++++++++++++ .../Profiles/Exif/ExifProfileTests.cs | 3 ++ 4 files changed, 66 insertions(+) diff --git a/src/ImageSharp.Formats.Jpeg/JpegEncoderCore.cs b/src/ImageSharp.Formats.Jpeg/JpegEncoderCore.cs index 9cc51c7772..657470f5cf 100644 --- a/src/ImageSharp.Formats.Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp.Formats.Jpeg/JpegEncoderCore.cs @@ -706,6 +706,7 @@ namespace ImageSharp.Formats private void WriteProfiles(Image image) where TColor : struct, IPackedPixel, IEquatable { + image.MetaData.SyncProfiles(); this.WriteProfile(image.MetaData.ExifProfile); } diff --git a/src/ImageSharp/MetaData/ImageMetaData.cs b/src/ImageSharp/MetaData/ImageMetaData.cs index a38899840e..0bd0d0f789 100644 --- a/src/ImageSharp/MetaData/ImageMetaData.cs +++ b/src/ImageSharp/MetaData/ImageMetaData.cs @@ -136,5 +136,34 @@ namespace ImageSharp /// 0 means to repeat indefinitely. /// public ushort RepeatCount { get; set; } + + /// + /// Synchronizes the profiles with the current meta data. + /// + internal void SyncProfiles() + { + this.SyncExifProfile(); + } + + private void SyncExifProfile() + { + if (this.ExifProfile == null) + { + return; + } + + this.SyncExifResolution(ExifTag.XResolution, this.HorizontalResolution); + this.SyncExifResolution(ExifTag.YResolution, this.VerticalResolution); + } + + private void SyncExifResolution(ExifTag tag, double resolution) + { + ExifValue value = this.ExifProfile.GetValue(tag); + if (value != null) + { + Rational newResolution = new Rational(resolution, false); + this.ExifProfile.SetValue(tag, newResolution); + } + } } } diff --git a/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs b/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs index 9034b88c09..d9bd52c730 100644 --- a/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs +++ b/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs @@ -38,5 +38,38 @@ namespace ImageSharp.Tests Assert.Equal(24, clone.Quality); Assert.Equal(1, clone.RepeatCount); } + + [Fact] + public void SyncProfiles() + { + ExifProfile exifProfile = new ExifProfile(); + exifProfile.SetValue(ExifTag.XResolution, new Rational(200)); + exifProfile.SetValue(ExifTag.YResolution, new Rational(300)); + + Image image = new Image(1, 1); + image.MetaData.ExifProfile = exifProfile; + image.MetaData.HorizontalResolution = 200; + image.MetaData.VerticalResolution = 300; + + image.MetaData.HorizontalResolution = 100; + + Assert.Equal(200, ((Rational)image.MetaData.ExifProfile.GetValue(ExifTag.XResolution).Value).ToDouble()); + Assert.Equal(300, ((Rational)image.MetaData.ExifProfile.GetValue(ExifTag.YResolution).Value).ToDouble()); + + image.MetaData.SyncProfiles(); + + Assert.Equal(100, ((Rational)image.MetaData.ExifProfile.GetValue(ExifTag.XResolution).Value).ToDouble()); + Assert.Equal(300, ((Rational)image.MetaData.ExifProfile.GetValue(ExifTag.YResolution).Value).ToDouble()); + + image.MetaData.VerticalResolution = 150; + + Assert.Equal(100, ((Rational)image.MetaData.ExifProfile.GetValue(ExifTag.XResolution).Value).ToDouble()); + Assert.Equal(300, ((Rational)image.MetaData.ExifProfile.GetValue(ExifTag.YResolution).Value).ToDouble()); + + image.MetaData.SyncProfiles(); + + Assert.Equal(100, ((Rational)image.MetaData.ExifProfile.GetValue(ExifTag.XResolution).Value).ToDouble()); + Assert.Equal(150, ((Rational)image.MetaData.ExifProfile.GetValue(ExifTag.YResolution).Value).ToDouble()); + } } } diff --git a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs index 61307da205..f4a2bcf401 100644 --- a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs +++ b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs @@ -150,6 +150,9 @@ namespace ImageSharp.Tests image.MetaData.ExifProfile.SetValue(ExifTag.XResolution, new Rational(150.0)); + // We also need to change this value because this overrides XResolution when the image is written. + image.MetaData.HorizontalResolution = 150.0; + value = image.MetaData.ExifProfile.GetValue(ExifTag.XResolution); TestValue(value, new Rational(150, 1));