Skip to content

Commit b3a8452

Browse files
committed
Rename SaveComment to SetComment, add index, add clearcomments
1 parent bb9ed65 commit b3a8452

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

src/ImageSharp/Formats/Jpeg/MetadataExtensions.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,35 @@ public static partial class MetadataExtensions
2020
public static JpegMetadata GetJpegMetadata(this ImageMetadata metadata) => metadata.GetFormatMetadata(JpegFormat.Instance);
2121

2222
/// <summary>
23-
/// Saves the comment into <see cref="JpegMetadata"/>
23+
/// Sets the comment in <see cref="JpegMetadata"/>
2424
/// </summary>
2525
/// <param name="metadata">The metadata this method extends.</param>
26+
/// <param name="index">The index of comment to be inserted to.</param>
2627
/// <param name="comment">The comment string.</param>
27-
public static void SaveComment(this JpegMetadata metadata, string comment)
28+
public static void SetComment(this JpegMetadata metadata, int index, string comment)
2829
{
29-
ASCIIEncoding encoding = new();
30+
if (metadata.Comments == null)
31+
{
32+
return;
33+
}
3034

35+
ASCIIEncoding encoding = new();
3136
byte[] bytes = encoding.GetBytes(comment);
32-
metadata.Comments?.Add(encoding.GetChars(bytes));
37+
List<Memory<char>>? comments = metadata.Comments as List<Memory<char>>;
38+
comments?.Insert(index, encoding.GetChars(bytes));
3339
}
3440

3541
/// <summary>
3642
/// Gets the comments from <see cref="JpegMetadata"/>
3743
/// </summary>
3844
/// <param name="metadata">The metadata this method extends.</param>
45+
/// <param name="index">The index of comment.</param>
3946
/// <returns>The IEnumerable string of comments.</returns>
40-
public static IEnumerable<string>? GetComments(this JpegMetadata metadata) => metadata.Comments?.Select(x => x.ToString());
47+
public static string? GetComment(this JpegMetadata metadata, int index) => metadata.Comments?.ElementAtOrDefault(index).ToString();
48+
49+
/// <summary>
50+
/// Clears comments
51+
/// </summary>
52+
/// <param name="metadata">The <see cref="JpegMetadata"/>.</param>
53+
public static void ClearComments(this JpegMetadata metadata) => metadata.Comments?.Clear();
4154
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,21 @@ public void EncodedStringTags_Read()
425425
VerifyEncodedStrings(exif);
426426
}
427427

428+
[Theory]
429+
[WithFile(TestImages.Jpeg.Issues.Issue2067_CommentMarker, PixelTypes.Rgba32)]
430+
public void JpegDecoder_DecodeMetadataComment<TPixel>(TestImageProvider<TPixel> provider)
431+
where TPixel : unmanaged, IPixel<TPixel>
432+
{
433+
string expectedComment = "TEST COMMENT";
434+
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);
435+
JpegMetadata metadata = image.Metadata.GetJpegMetadata();
436+
437+
Assert.Equal(1, metadata.Comments?.Count);
438+
Assert.Equal(expectedComment, metadata.GetComment(0));
439+
image.DebugSave(provider);
440+
image.CompareToOriginal(provider);
441+
}
442+
428443
private static void VerifyEncodedStrings(ExifProfile exif)
429444
{
430445
Assert.NotNull(exif);

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -364,19 +364,4 @@ public void Issue2517_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
364364
image.DebugSave(provider);
365365
image.CompareToOriginal(provider);
366366
}
367-
368-
[Theory]
369-
[WithFile(TestImages.Jpeg.Issues.Issue2067_CommentMarker, PixelTypes.Rgba32)]
370-
public void JpegDecoder_DecodeMetadataComment<TPixel>(TestImageProvider<TPixel> provider)
371-
where TPixel : unmanaged, IPixel<TPixel>
372-
{
373-
string expectedComment = "TEST COMMENT";
374-
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);
375-
JpegMetadata metadata = image.Metadata.GetJpegMetadata();
376-
377-
Assert.Equal(1, metadata.Comments?.Count);
378-
Assert.Equal(expectedComment, metadata.GetComments()?.FirstOrDefault());
379-
image.DebugSave(provider);
380-
image.CompareToOriginal(provider);
381-
}
382367
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,18 @@ public void Encode_SavesMultipleComments()
184184
using var memStream = new MemoryStream();
185185

186186
// act
187-
meta.SaveComment("First comment");
188-
meta.SaveComment("Second Comment");
187+
meta.SetComment(0, "First comment");
188+
meta.SetComment(1, "Second Comment");
189189
input.Save(memStream, JpegEncoder);
190190

191191
// assert
192192
memStream.Position = 0;
193193
using var output = Image.Load<Rgba32>(memStream);
194194
JpegMetadata actual = output.Metadata.GetJpegMetadata();
195195
Assert.NotEmpty(actual.Comments);
196-
Assert.Equal(2, actual.Comments.Count);
197-
Assert.Equal(meta.Comments.ElementAt(0).ToString(), actual.Comments.ElementAt(0).ToString());
198-
Assert.Equal(meta.Comments.ElementAt(1).ToString(), actual.Comments.ElementAt(1).ToString());
196+
Assert.Equal(2, actual.Comments?.Count);
197+
Assert.Equal(meta.Comments?.ElementAt(0).ToString(), actual.Comments?.ElementAt(0).ToString());
198+
Assert.Equal(meta.Comments?.ElementAt(1).ToString(), actual.Comments?.ElementAt(1).ToString());
199199
}
200200

201201
[Theory]

0 commit comments

Comments
 (0)