Browse Source

Rename SaveComment to SetComment, add index, add clearcomments

pull/2641/head
Robert Mutniański 2 years ago
parent
commit
b3a8452edc
  1. 23
      src/ImageSharp/Formats/Jpeg/MetadataExtensions.cs
  2. 15
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs
  3. 15
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  4. 10
      tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs

23
src/ImageSharp/Formats/Jpeg/MetadataExtensions.cs

@ -20,22 +20,35 @@ public static partial class MetadataExtensions
public static JpegMetadata GetJpegMetadata(this ImageMetadata metadata) => metadata.GetFormatMetadata(JpegFormat.Instance);
/// <summary>
/// Saves the comment into <see cref="JpegMetadata"/>
/// Sets the comment in <see cref="JpegMetadata"/>
/// </summary>
/// <param name="metadata">The metadata this method extends.</param>
/// <param name="index">The index of comment to be inserted to.</param>
/// <param name="comment">The comment string.</param>
public static void SaveComment(this JpegMetadata metadata, string comment)
public static void SetComment(this JpegMetadata metadata, int index, string comment)
{
ASCIIEncoding encoding = new();
if (metadata.Comments == null)
{
return;
}
ASCIIEncoding encoding = new();
byte[] bytes = encoding.GetBytes(comment);
metadata.Comments?.Add(encoding.GetChars(bytes));
List<Memory<char>>? comments = metadata.Comments as List<Memory<char>>;
comments?.Insert(index, encoding.GetChars(bytes));
}
/// <summary>
/// Gets the comments from <see cref="JpegMetadata"/>
/// </summary>
/// <param name="metadata">The metadata this method extends.</param>
/// <param name="index">The index of comment.</param>
/// <returns>The IEnumerable string of comments.</returns>
public static IEnumerable<string>? GetComments(this JpegMetadata metadata) => metadata.Comments?.Select(x => x.ToString());
public static string? GetComment(this JpegMetadata metadata, int index) => metadata.Comments?.ElementAtOrDefault(index).ToString();
/// <summary>
/// Clears comments
/// </summary>
/// <param name="metadata">The <see cref="JpegMetadata"/>.</param>
public static void ClearComments(this JpegMetadata metadata) => metadata.Comments?.Clear();
}

15
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs

@ -425,6 +425,21 @@ public partial class JpegDecoderTests
VerifyEncodedStrings(exif);
}
[Theory]
[WithFile(TestImages.Jpeg.Issues.Issue2067_CommentMarker, PixelTypes.Rgba32)]
public void JpegDecoder_DecodeMetadataComment<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
string expectedComment = "TEST COMMENT";
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);
JpegMetadata metadata = image.Metadata.GetJpegMetadata();
Assert.Equal(1, metadata.Comments?.Count);
Assert.Equal(expectedComment, metadata.GetComment(0));
image.DebugSave(provider);
image.CompareToOriginal(provider);
}
private static void VerifyEncodedStrings(ExifProfile exif)
{
Assert.NotNull(exif);

15
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -364,19 +364,4 @@ public partial class JpegDecoderTests
image.DebugSave(provider);
image.CompareToOriginal(provider);
}
[Theory]
[WithFile(TestImages.Jpeg.Issues.Issue2067_CommentMarker, PixelTypes.Rgba32)]
public void JpegDecoder_DecodeMetadataComment<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
string expectedComment = "TEST COMMENT";
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);
JpegMetadata metadata = image.Metadata.GetJpegMetadata();
Assert.Equal(1, metadata.Comments?.Count);
Assert.Equal(expectedComment, metadata.GetComments()?.FirstOrDefault());
image.DebugSave(provider);
image.CompareToOriginal(provider);
}
}

10
tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs

@ -184,8 +184,8 @@ public partial class JpegEncoderTests
using var memStream = new MemoryStream();
// act
meta.SaveComment("First comment");
meta.SaveComment("Second Comment");
meta.SetComment(0, "First comment");
meta.SetComment(1, "Second Comment");
input.Save(memStream, JpegEncoder);
// assert
@ -193,9 +193,9 @@ public partial class JpegEncoderTests
using var output = Image.Load<Rgba32>(memStream);
JpegMetadata actual = output.Metadata.GetJpegMetadata();
Assert.NotEmpty(actual.Comments);
Assert.Equal(2, actual.Comments.Count);
Assert.Equal(meta.Comments.ElementAt(0).ToString(), actual.Comments.ElementAt(0).ToString());
Assert.Equal(meta.Comments.ElementAt(1).ToString(), actual.Comments.ElementAt(1).ToString());
Assert.Equal(2, actual.Comments?.Count);
Assert.Equal(meta.Comments?.ElementAt(0).ToString(), actual.Comments?.ElementAt(0).ToString());
Assert.Equal(meta.Comments?.ElementAt(1).ToString(), actual.Comments?.ElementAt(1).ToString());
}
[Theory]

Loading…
Cancel
Save