Browse Source

removed ExifIdCode parameter from ExifProfile.ToByteArray, because this is jpeg specific and should be handled by the jpeg encoder

pull/616/head
popow 8 years ago
parent
commit
35aaaeefad
  1. 7
      src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
  2. 6
      src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
  3. 14
      src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs
  4. 1
      tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs
  5. 21
      tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs
  6. 2
      tests/ImageSharp.Tests/Processing/Processors/Transforms/AutoOrientTests.cs

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

@ -3,6 +3,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats.Jpeg.Components; using SixLabors.ImageSharp.Formats.Jpeg.Components;
@ -611,12 +612,16 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
{ {
const int MaxBytesApp1 = 65533; const int MaxBytesApp1 = 65533;
const int MaxBytesWithExifId = 65527; const int MaxBytesWithExifId = 65527;
byte[] data = exifProfile?.ToByteArray(ProfileResolver.ExifMarker);
byte[] data = exifProfile?.ToByteArray();
if (data == null || data.Length == 0) if (data == null || data.Length == 0)
{ {
return; return;
} }
data = ProfileResolver.ExifMarker.Concat(data).ToArray();
int remaining = data.Length; int remaining = data.Length;
int bytesToWrite = remaining > MaxBytesApp1 ? MaxBytesApp1 : remaining; int bytesToWrite = remaining > MaxBytesApp1 ? MaxBytesApp1 : remaining;
int app1Length = bytesToWrite + 2; int app1Length = bytesToWrite + 2;

6
src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs

@ -245,10 +245,8 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
/// <summary> /// <summary>
/// Converts this instance to a byte array. /// Converts this instance to a byte array.
/// </summary> /// </summary>
/// <param name="exifIdCode">The Exif Id Code is part of the JPEG APP1 segment (Exif00). Those bytes will be written at
/// the beginning of the array. This Exif ID code should not be included in case of PNG's.</param>
/// <returns>The <see cref="T:byte[]"/></returns> /// <returns>The <see cref="T:byte[]"/></returns>
public byte[] ToByteArray(ReadOnlySpan<byte> exifIdCode = default) public byte[] ToByteArray()
{ {
if (this.values == null) if (this.values == null)
{ {
@ -261,7 +259,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
} }
var writer = new ExifWriter(this.values, this.Parts); var writer = new ExifWriter(this.values, this.Parts);
return writer.GetData(exifIdCode); return writer.GetData();
} }
/// <summary> /// <summary>

14
src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs

@ -41,15 +41,12 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
/// <summary> /// <summary>
/// Returns the EXIF data. /// Returns the EXIF data.
/// </summary> /// </summary>
/// <param name="exifIdCode">The Exif Id Code is part of the JPEG APP1 segment (Exif00). Those bytes will be written at
/// the beginning of the array. This Exif ID code should not be included in case of PNG's.</param>
/// <returns> /// <returns>
/// The <see cref="T:byte[]"/>. /// The <see cref="T:byte[]"/>.
/// </returns> /// </returns>
public byte[] GetData(ReadOnlySpan<byte> exifIdCode) public byte[] GetData()
{ {
uint exifIdCodeLength = exifIdCode.IsEmpty ? 0 : (uint)exifIdCode.Length; uint startIndex = 0;
uint startIndex = exifIdCodeLength;
uint length; uint length;
int exifIndex = -1; int exifIndex = -1;
int gpsIndex = -1; int gpsIndex = -1;
@ -85,8 +82,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
return null; return null;
} }
length += exifIdCodeLength;
// two bytes for the byte Order marker 'II', followed by the number 42 (0x2A) and a 0, making 4 bytes total // two bytes for the byte Order marker 'II', followed by the number 42 (0x2A) and a 0, making 4 bytes total
length += (uint)ExifConstants.LittleEndianByteOrderMarker.Length; length += (uint)ExifConstants.LittleEndianByteOrderMarker.Length;
@ -95,11 +90,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
byte[] result = new byte[length]; byte[] result = new byte[length];
int i = 0; int i = 0;
if (!exifIdCode.IsEmpty)
{
exifIdCode.CopyTo(result); // 0-5
i += exifIdCode.Length;
}
// the byte order marker for little-endian, followed by the number 42 and a 0 // the byte order marker for little-endian, followed by the number 42 and a 0
ExifConstants.LittleEndianByteOrderMarker.AsSpan().CopyTo(result.AsSpan(start: i)); ExifConstants.LittleEndianByteOrderMarker.AsSpan().CopyTo(result.AsSpan(start: i));

1
tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs

@ -33,7 +33,6 @@ namespace SixLabors.ImageSharp.Tests
ImageMetaData clone = metaData.Clone(); ImageMetaData clone = metaData.Clone();
Assert.Equal(exifProfile.ToByteArray(), clone.ExifProfile.ToByteArray()); Assert.Equal(exifProfile.ToByteArray(), clone.ExifProfile.ToByteArray());
Assert.Equal(exifProfile.ToByteArray(ProfileResolver.ExifMarker), clone.ExifProfile.ToByteArray(ProfileResolver.ExifMarker));
Assert.Equal(4, clone.HorizontalResolution); Assert.Equal(4, clone.HorizontalResolution);
Assert.Equal(2, clone.VerticalResolution); Assert.Equal(2, clone.VerticalResolution);
Assert.Equal(imageProperty, clone.Properties[0]); Assert.Equal(imageProperty, clone.Properties[0]);

21
tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs

@ -339,8 +339,8 @@ namespace SixLabors.ImageSharp.Tests
// Force parsing of the profile. // Force parsing of the profile.
Assert.Equal(24, profile.Values.Count); Assert.Equal(24, profile.Values.Count);
byte[] bytes = profile.ToByteArray(ProfileResolver.ExifMarker); byte[] bytes = profile.ToByteArray();
Assert.Equal(495, bytes.Length); Assert.Equal(489, bytes.Length);
} }
[Theory] [Theory]
@ -367,10 +367,8 @@ namespace SixLabors.ImageSharp.Tests
} }
} }
[Theory] [Fact]
[InlineData(false)] public void ProfileToByteArray()
[InlineData(true)]
public void ProfileToByteArray(bool includeExifIdCode)
{ {
// arrange // arrange
byte[] exifBytesWithExifCode = ProfileResolver.ExifMarker.Concat(ExifConstants.LittleEndianByteOrderMarker).ToArray(); byte[] exifBytesWithExifCode = ProfileResolver.ExifMarker.Concat(ExifConstants.LittleEndianByteOrderMarker).ToArray();
@ -379,20 +377,13 @@ namespace SixLabors.ImageSharp.Tests
var expectedProfileTags = expectedProfile.Values.Select(x => x.Tag).ToList(); var expectedProfileTags = expectedProfile.Values.Select(x => x.Tag).ToList();
// act // act
byte[] actualBytes = expectedProfile.ToByteArray(includeExifIdCode ? ProfileResolver.ExifMarker : default(ReadOnlySpan<byte>)); byte[] actualBytes = expectedProfile.ToByteArray();
var actualProfile = new ExifProfile(actualBytes); var actualProfile = new ExifProfile(actualBytes);
// assert // assert
Assert.NotNull(actualBytes); Assert.NotNull(actualBytes);
Assert.NotEmpty(actualBytes); Assert.NotEmpty(actualBytes);
if (includeExifIdCode) Assert.Equal(exifBytesWithoutExifCode, actualBytes.Take(exifBytesWithoutExifCode.Length).ToArray());
{
Assert.Equal(exifBytesWithExifCode, actualBytes.Take(exifBytesWithExifCode.Length).ToArray());
}
else
{
Assert.Equal(exifBytesWithoutExifCode, actualBytes.Take(exifBytesWithoutExifCode.Length).ToArray());
}
foreach(ExifTag expectedProfileTag in expectedProfileTags) foreach(ExifTag expectedProfileTag in expectedProfileTags)
{ {
ExifValue actualProfileValue = actualProfile.GetValue(expectedProfileTag); ExifValue actualProfileValue = actualProfile.GetValue(expectedProfileTag);

2
tests/ImageSharp.Tests/Processing/Processors/Transforms/AutoOrientTests.cs

@ -64,7 +64,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
var profile = new ExifProfile(); var profile = new ExifProfile();
profile.SetValue(ExifTag.JPEGTables, orientation); profile.SetValue(ExifTag.JPEGTables, orientation);
byte[] bytes = profile.ToByteArray(ProfileResolver.ExifMarker); byte[] bytes = profile.ToByteArray();
// Change the tag into ExifTag.Orientation // Change the tag into ExifTag.Orientation
bytes[16] = 18; bytes[16] = 18;
bytes[17] = 1; bytes[17] = 1;

Loading…
Cancel
Save