Browse Source

Identify reads now exif data

pull/1574/head
Brian Popow 6 years ago
parent
commit
b9307edd9d
  1. 9
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 58
      tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs
  3. 4
      tests/Images/Input/Png/PngWithMetaData.png

9
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -278,6 +278,15 @@ namespace SixLabors.ImageSharp.Formats.Png
break;
case PngChunkType.InternationalText:
this.ReadInternationalTextChunk(pngMetadata, chunk.Data.Array.AsSpan(0, chunk.Length));
break;
case PngChunkType.Exif:
if (!this.ignoreMetadata)
{
var exifData = new byte[chunk.Length];
Buffer.BlockCopy(chunk.Data.Array, 0, exifData, 0, chunk.Length);
metadata.ExifProfile = new ExifProfile(exifData);
}
break;
case PngChunkType.End:
this.isEndChunkReached = true;

58
tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs

@ -6,6 +6,7 @@ using System.IO;
using System.Linq;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.PixelFormats;
using Xunit;
@ -129,6 +130,40 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
}
}
[Theory]
[WithFile(TestImages.Png.PngWithMetadata, PixelTypes.Rgba32)]
public void Decode_ReadsExifData<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
var decoder = new PngDecoder
{
IgnoreMetadata = false
};
using (Image<TPixel> image = provider.GetImage(decoder))
{
Assert.NotNull(image.Metadata.ExifProfile);
ExifProfile exif = image.Metadata.ExifProfile;
VerifyExifDataIsPresent(exif);
}
}
[Theory]
[WithFile(TestImages.Png.PngWithMetadata, PixelTypes.Rgba32)]
public void Decode_IgnoresExifData_WhenIgnoreMetadataIsTrue<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
var decoder = new PngDecoder
{
IgnoreMetadata = true
};
using (Image<TPixel> image = provider.GetImage(decoder))
{
Assert.Null(image.Metadata.ExifProfile);
}
}
[Fact]
public void Decode_IgnoreMetadataIsFalse_TextChunkIsRead()
{
@ -215,6 +250,29 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
}
}
[Theory]
[InlineData(TestImages.Png.PngWithMetadata)]
public void Identify_ReadsExifData(string imagePath)
{
var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false))
{
IImageInfo imageInfo = Image.Identify(stream);
Assert.NotNull(imageInfo);
Assert.NotNull(imageInfo.Metadata.ExifProfile);
ExifProfile exif = imageInfo.Metadata.ExifProfile;
VerifyExifDataIsPresent(exif);
}
}
private static void VerifyExifDataIsPresent(ExifProfile exif)
{
Assert.Equal(1, exif.Values.Count);
IExifValue<string> software = exif.GetValue(ExifTag.Software);
Assert.NotNull(software);
Assert.Equal("ImageSharp", software.Value);
}
private static void VerifyTextDataIsPresent(PngMetadata meta)
{
Assert.NotNull(meta);

4
tests/Images/Input/Png/PngWithMetaData.png

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c0490f627b22a3487b78e2797ebb65f5741fdbabfd4a3d9db806ca624f62fe8c
size 805
oid sha256:a2350c200d0d05c18ac70f3d6dcb2adb16a49aca6407dc02bb6a865e0b5b836d
size 751

Loading…
Cancel
Save