diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index 77a7243c4..904fa5e6c 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using System.Linq; using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Formats.Jpeg.Components; @@ -611,12 +612,16 @@ namespace SixLabors.ImageSharp.Formats.Jpeg { const int MaxBytesApp1 = 65533; const int MaxBytesWithExifId = 65527; - byte[] data = exifProfile?.ToByteArray(ProfileResolver.ExifMarker); + + byte[] data = exifProfile?.ToByteArray(); + if (data == null || data.Length == 0) { return; } + data = ProfileResolver.ExifMarker.Concat(data).ToArray(); + int remaining = data.Length; int bytesToWrite = remaining > MaxBytesApp1 ? MaxBytesApp1 : remaining; int app1Length = bytesToWrite + 2; diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs index 1f2695c5e..77ce13535 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs @@ -245,10 +245,8 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif /// /// Converts this instance to a byte array. /// - /// 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. /// The - public byte[] ToByteArray(ReadOnlySpan exifIdCode = default) + public byte[] ToByteArray() { if (this.values == null) { @@ -261,7 +259,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif } var writer = new ExifWriter(this.values, this.Parts); - return writer.GetData(exifIdCode); + return writer.GetData(); } /// diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs index 1de5fbd5c..dc75697e2 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs @@ -41,15 +41,12 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif /// /// Returns the EXIF data. /// - /// 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. /// /// The . /// - public byte[] GetData(ReadOnlySpan exifIdCode) + public byte[] GetData() { - uint exifIdCodeLength = exifIdCode.IsEmpty ? 0 : (uint)exifIdCode.Length; - uint startIndex = exifIdCodeLength; + uint startIndex = 0; uint length; int exifIndex = -1; int gpsIndex = -1; @@ -85,8 +82,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif 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 length += (uint)ExifConstants.LittleEndianByteOrderMarker.Length; @@ -95,11 +90,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif byte[] result = new byte[length]; 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 ExifConstants.LittleEndianByteOrderMarker.AsSpan().CopyTo(result.AsSpan(start: i)); diff --git a/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs b/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs index 7d0686aa7..8934ebc36 100644 --- a/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs +++ b/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs @@ -33,7 +33,6 @@ namespace SixLabors.ImageSharp.Tests ImageMetaData clone = metaData.Clone(); 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(2, clone.VerticalResolution); Assert.Equal(imageProperty, clone.Properties[0]); diff --git a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs index 7190fea44..3deb382ea 100644 --- a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs +++ b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs @@ -339,8 +339,8 @@ namespace SixLabors.ImageSharp.Tests // Force parsing of the profile. Assert.Equal(24, profile.Values.Count); - byte[] bytes = profile.ToByteArray(ProfileResolver.ExifMarker); - Assert.Equal(495, bytes.Length); + byte[] bytes = profile.ToByteArray(); + Assert.Equal(489, bytes.Length); } [Theory] @@ -367,10 +367,8 @@ namespace SixLabors.ImageSharp.Tests } } - [Theory] - [InlineData(false)] - [InlineData(true)] - public void ProfileToByteArray(bool includeExifIdCode) + [Fact] + public void ProfileToByteArray() { // arrange 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(); // act - byte[] actualBytes = expectedProfile.ToByteArray(includeExifIdCode ? ProfileResolver.ExifMarker : default(ReadOnlySpan)); + byte[] actualBytes = expectedProfile.ToByteArray(); var actualProfile = new ExifProfile(actualBytes); // assert Assert.NotNull(actualBytes); Assert.NotEmpty(actualBytes); - if (includeExifIdCode) - { - Assert.Equal(exifBytesWithExifCode, actualBytes.Take(exifBytesWithExifCode.Length).ToArray()); - } - else - { - Assert.Equal(exifBytesWithoutExifCode, actualBytes.Take(exifBytesWithoutExifCode.Length).ToArray()); - } + Assert.Equal(exifBytesWithoutExifCode, actualBytes.Take(exifBytesWithoutExifCode.Length).ToArray()); foreach(ExifTag expectedProfileTag in expectedProfileTags) { ExifValue actualProfileValue = actualProfile.GetValue(expectedProfileTag); diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/AutoOrientTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/AutoOrientTests.cs index 85c803ae6..9b37fb266 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/AutoOrientTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/AutoOrientTests.cs @@ -64,7 +64,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms var profile = new ExifProfile(); profile.SetValue(ExifTag.JPEGTables, orientation); - byte[] bytes = profile.ToByteArray(ProfileResolver.ExifMarker); + byte[] bytes = profile.ToByteArray(); // Change the tag into ExifTag.Orientation bytes[16] = 18; bytes[17] = 1;