Skip to content

Commit d225128

Browse files
committed
Introduce JpegComData.cs
1 parent d9169c5 commit d225128

File tree

7 files changed

+46
-14
lines changed

7 files changed

+46
-14
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
namespace SixLabors.ImageSharp.Formats.Jpeg;
5+
6+
/// <summary>
7+
/// Contains JPEG comment
8+
/// </summary>
9+
public readonly struct JpegComData
10+
{
11+
/// <summary>
12+
/// Converts string to <see cref="JpegComData"/>
13+
/// </summary>
14+
/// <param name="value">The comment string.</param>
15+
/// <returns>The <see cref="JpegComData"/></returns>
16+
public static JpegComData FromString(string value) => new(value.AsMemory());
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="JpegComData"/> struct.
20+
/// </summary>
21+
/// <param name="value">The comment ReadOnlyMemory of chars.</param>
22+
public JpegComData(ReadOnlyMemory<char> value)
23+
=> this.Value = value;
24+
25+
public ReadOnlyMemory<char> Value { get; }
26+
27+
/// <summary>
28+
/// Converts Value to string
29+
/// </summary>
30+
/// <returns>The comment string.</returns>
31+
public override string ToString() => this.Value.ToString();
32+
}

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ private void ProcessComMarker(BufferedReadStream stream, int markerContentByteSi
529529
stream.Read(temp);
530530
string comment = Encoding.ASCII.GetString(temp);
531531

532-
metadata.Comments.Add(comment);
532+
metadata.Comments.Add(JpegComData.FromString(comment));
533533
}
534534

535535
/// <summary>

src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ private void WriteComment(JpegMetadata metadata)
186186

187187
for (int i = 0; i < metadata.Comments.Count; i++)
188188
{
189-
string comment = metadata.Comments[i];
189+
string comment = metadata.Comments[i].ToString();
190190

191191
if (comment.Length > maxCommentLength)
192192
{
193193
string splitComment = comment.Substring(maxCommentLength, comment.Length - maxCommentLength);
194-
metadata.Comments.Insert(i + 1, splitComment);
194+
metadata.Comments.Insert(i + 1, JpegComData.FromString(splitComment));
195195

196196
// We don't want to keep the extra bytes
197197
comment = comment.Substring(0, maxCommentLength);

src/ImageSharp/Formats/Jpeg/JpegMetadata.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class JpegMetadata : IDeepCloneable
1515
/// </summary>
1616
public JpegMetadata()
1717
{
18-
this.Comments = new List<string>();
18+
this.Comments = new List<JpegComData>();
1919
}
2020

2121
/// <summary>
@@ -106,7 +106,7 @@ public int Quality
106106
/// <summary>
107107
/// Gets the comments.
108108
/// </summary>
109-
public IList<string> Comments { get; }
109+
public IList<JpegComData> Comments { get; }
110110

111111
/// <inheritdoc/>
112112
public IDeepCloneable DeepClone() => new JpegMetadata(this);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public void JpegDecoder_DecodeMetadataComment<TPixel>(TestImageProvider<TPixel>
435435
JpegMetadata metadata = image.Metadata.GetJpegMetadata();
436436

437437
Assert.Equal(1, metadata.Comments.Count);
438-
Assert.Equal(expectedComment, metadata.Comments.ElementAtOrDefault(0));
438+
Assert.Equal(expectedComment, metadata.Comments.ElementAtOrDefault(0).ToString());
439439
image.DebugSave(provider);
440440
image.CompareToOriginal(provider);
441441
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public void Encode_PreservesComments<TPixel>(TestImageProvider<TPixel> provider)
172172
JpegMetadata actual = output.Metadata.GetJpegMetadata();
173173
Assert.NotEmpty(actual.Comments);
174174
Assert.Equal(1, actual.Comments.Count);
175-
Assert.Equal("TEST COMMENT", actual.Comments.ElementAtOrDefault(0));
175+
Assert.Equal("TEST COMMENT", actual.Comments.ElementAtOrDefault(0).ToString());
176176
}
177177

178178
[Fact]
@@ -184,8 +184,8 @@ public void Encode_SavesMultipleComments()
184184
using var memStream = new MemoryStream();
185185

186186
// act
187-
meta.Comments.Add("First comment");
188-
meta.Comments.Add("Second Comment");
187+
meta.Comments.Add(JpegComData.FromString("First comment"));
188+
meta.Comments.Add(JpegComData.FromString("Second Comment"));
189189
input.Save(memStream, JpegEncoder);
190190

191191
// assert
@@ -194,8 +194,8 @@ public void Encode_SavesMultipleComments()
194194
JpegMetadata actual = output.Metadata.GetJpegMetadata();
195195
Assert.NotEmpty(actual.Comments);
196196
Assert.Equal(2, actual.Comments.Count);
197-
Assert.Equal(meta.Comments.ElementAtOrDefault(0), actual.Comments.ElementAtOrDefault(0));
198-
Assert.Equal(meta.Comments.ElementAtOrDefault(1), actual.Comments.ElementAtOrDefault(1));
197+
Assert.Equal(meta.Comments.ElementAtOrDefault(0).ToString(), actual.Comments.ElementAtOrDefault(0).ToString());
198+
Assert.Equal(meta.Comments.ElementAtOrDefault(1).ToString(), actual.Comments.ElementAtOrDefault(1).ToString());
199199
}
200200

201201
[Theory]

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void Comment_EmptyComment()
6464
{
6565
var meta = new JpegMetadata();
6666

67-
Assert.True(Array.Empty<string>().SequenceEqual(meta.Comments));
67+
Assert.True(Array.Empty<JpegComData>().SequenceEqual(meta.Comments));
6868
}
6969

7070
[Fact]
@@ -74,9 +74,9 @@ public void Comment_OnlyComment()
7474
var expectedCollection = new Collection<string> { comment };
7575

7676
var meta = new JpegMetadata();
77-
meta.Comments.Add(comment);
77+
meta.Comments.Add(JpegComData.FromString(comment));
7878

7979
Assert.Equal(1, meta.Comments.Count);
80-
Assert.True(expectedCollection.FirstOrDefault() == meta.Comments.FirstOrDefault());
80+
Assert.True(expectedCollection.FirstOrDefault() == meta.Comments.FirstOrDefault().ToString());
8181
}
8282
}

0 commit comments

Comments
 (0)