Browse Source

ToByteArray skips the first 6 bytes if includeExifCode is false, added unit test for that

af/merge-core
popow 8 years ago
parent
commit
3aa00199f1
  1. 33
      src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
  2. 26
      tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs

33
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);
}
/// <summary>
/// Checks if a byte array start with the Exif Code: ASCII "Exif" followed by two zeros.
/// </summary>
/// <param name="exifBytes">The byte array to check for the Exif Code.</param>
/// <returns>True, if the byte array starts with the Exif Code</returns>
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;
}
/// <summary>
/// Synchronizes the profiles with the specified meta data.
/// </summary>

26
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();

Loading…
Cancel
Save