Browse Source

Added method to update the profile data (based on #96)

pull/99/head
Dirk Lemstra 9 years ago
parent
commit
00f08396d7
  1. 1
      src/ImageSharp.Formats.Jpeg/JpegEncoderCore.cs
  2. 29
      src/ImageSharp/MetaData/ImageMetaData.cs
  3. 33
      tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs
  4. 3
      tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs

1
src/ImageSharp.Formats.Jpeg/JpegEncoderCore.cs

@ -706,6 +706,7 @@ namespace ImageSharp.Formats
private void WriteProfiles<TColor>(Image<TColor> image)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
image.MetaData.SyncProfiles();
this.WriteProfile(image.MetaData.ExifProfile);
}

29
src/ImageSharp/MetaData/ImageMetaData.cs

@ -136,5 +136,34 @@ namespace ImageSharp
/// <remarks>0 means to repeat indefinitely.</remarks>
/// </summary>
public ushort RepeatCount { get; set; }
/// <summary>
/// Synchronizes the profiles with the current meta data.
/// </summary>
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);
}
}
}
}

33
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());
}
}
}

3
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));

Loading…
Cancel
Save