Skip to content

Commit

Permalink
Merge pull request #2598 from SixLabors/js/resolution
Browse files Browse the repository at this point in the history
Read resolution from EXIF metadata in WebP
  • Loading branch information
JimBobSquarePants authored Dec 1, 2023
2 parents cae0a39 + 32c8baa commit 0b36698
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Buffers;
using System.Buffers.Binary;
using SixLabors.ImageSharp.Common.Helpers;
using SixLabors.ImageSharp.Formats.Webp.Lossless;
using SixLabors.ImageSharp.Formats.Webp.Lossy;
using SixLabors.ImageSharp.IO;
Expand Down Expand Up @@ -339,10 +340,33 @@ private void ReadExifProfile(BufferedReadStream stream, ImageMetadata metadata,
return;
}

metadata.ExifProfile = new ExifProfile(exifData);
ExifProfile exifProfile = new(exifData);

// Set the resolution from the metadata.
double horizontalValue = GetExifResolutionValue(exifProfile, ExifTag.XResolution);
double verticalValue = GetExifResolutionValue(exifProfile, ExifTag.YResolution);

if (horizontalValue > 0 && verticalValue > 0)
{
metadata.HorizontalResolution = horizontalValue;
metadata.VerticalResolution = verticalValue;
metadata.ResolutionUnits = UnitConverter.ExifProfileToResolutionUnit(exifProfile);
}

metadata.ExifProfile = exifProfile;
}
}

private static double GetExifResolutionValue(ExifProfile exifProfile, ExifTag<Rational> tag)
{
if (exifProfile.TryGetValue(tag, out IExifValue<Rational>? resolution))
{
return resolution.Value.ToDouble();
}

return 0;
}

/// <summary>
/// Reads the XMP profile the stream.
/// </summary>
Expand Down
29 changes: 29 additions & 0 deletions tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.Intrinsics.X86;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
Expand Down Expand Up @@ -493,4 +494,32 @@ private static void RunDecodeLossyWithComplexFilterTest()

[Fact]
public void DecodeLossyWithComplexFilterTest_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunDecodeLossyWithComplexFilterTest, HwIntrinsics.DisableHWIntrinsic);

[Theory]
[InlineData(Lossy.BikeWithExif)]
public void Decode_VerifyRatio(string imagePath)
{
TestFile testFile = TestFile.Create(imagePath);
using MemoryStream stream = new(testFile.Bytes, false);
using Image image = WebpDecoder.Instance.Decode(DecoderOptions.Default, stream);
ImageMetadata meta = image.Metadata;

Assert.Equal(37.8, meta.HorizontalResolution);
Assert.Equal(37.8, meta.VerticalResolution);
Assert.Equal(PixelResolutionUnit.PixelsPerCentimeter, meta.ResolutionUnits);
}

[Theory]
[InlineData(Lossy.BikeWithExif)]
public void Identify_VerifyRatio(string imagePath)
{
TestFile testFile = TestFile.Create(imagePath);
using MemoryStream stream = new(testFile.Bytes, false);
ImageInfo image = WebpDecoder.Instance.Identify(DecoderOptions.Default, stream);
ImageMetadata meta = image.Metadata;

Assert.Equal(37.8, meta.HorizontalResolution);
Assert.Equal(37.8, meta.VerticalResolution);
Assert.Equal(PixelResolutionUnit.PixelsPerCentimeter, meta.ResolutionUnits);
}
}

0 comments on commit 0b36698

Please sign in to comment.