From 3aa00199f1d9aba96da203347a4d2277b1e64ec7 Mon Sep 17 00:00:00 2001 From: popow Date: Sun, 17 Jun 2018 14:46:28 +0200 Subject: [PATCH] ToByteArray skips the first 6 bytes if includeExifCode is false, added unit test for that --- .../MetaData/Profiles/Exif/ExifProfile.cs | 33 +++++++++++++++++++ .../Profiles/Exif/ExifProfileTests.cs | 26 +++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs index 5ba1946349..4a73e8a4be 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Primitives; @@ -239,6 +240,12 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif { if (this.values == null) { + if (!includeExifIdCode && this.StartsWithExifIdCode(this.data)) + { + // skip the first 6 bytes (the Exif Code) + return this.data.Skip(6).ToArray(); + } + return this.data; } @@ -251,6 +258,32 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif return writer.GetData(includeExifIdCode); } + /// + /// Checks if a byte array start with the Exif Code: ASCII "Exif" followed by two zeros. + /// + /// The byte array to check for the Exif Code. + /// True, if the byte array starts with the Exif Code + private bool StartsWithExifIdCode(byte[] exifBytes) + { + if (exifBytes.Length < 6) + { + return false; + } + + // The EXIF ID code: ASCII "Exif" followed by two zeros. + byte[] exifCode = { 69, 120, 105, 102, 0, 0 }; + int exifLength = exifCode.Length; + for (int i = 0; i < exifCode.Length; i++) + { + if (exifBytes[i] != exifCode[i]) + { + return false; + } + } + + return true; + } + /// /// Synchronizes the profiles with the specified meta data. /// diff --git a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs index 6d3eb56317..98113f3921 100644 --- a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs +++ b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs @@ -351,6 +351,32 @@ namespace SixLabors.ImageSharp.Tests } } + [Theory] + [InlineData(false)] + [InlineData(true)] + public void TestProfileToByteArrayWorks(bool includeExifIdCode) + { + // arrange + byte[] exifBytesWithExifCode = new byte[] { 69, 120, 105, 102, 0, 0, 73, 73, 42, 0}; + byte[] exifBytesWithoutExifCode = new byte[] { 73, 73, 42, 0 }; + var profile = new ExifProfile(exifBytesWithExifCode); + + // act + byte[] actual = profile.ToByteArray(includeExifIdCode); + + // assert + Assert.NotNull(actual); + Assert.NotEmpty(actual); + if (includeExifIdCode) + { + Assert.Equal(exifBytesWithExifCode, actual); + } + else + { + Assert.Equal(exifBytesWithoutExifCode, actual); + } + } + private static ExifProfile CreateExifProfile() { var profile = new ExifProfile();