Repository navigation
Add JPEG COM marker support - #2641
Conversation
|
Thanks for this! I'll review it asap. |
| /// <param name="markerContentByteSize">The remaining bytes in the segment block.</param> | ||
| private void ProcessComMarker(BufferedReadStream stream, int markerContentByteSize) | ||
| { | ||
| Span<byte> temp = stackalloc byte[markerContentByteSize]; |
There was a problem hiding this comment.
We should put a limit on when to use stackalloc, variable length can be dangerous. See the following for really useful advice.
https://vcsjones.dev/stackalloc/
| where TPixel : unmanaged, IPixel<TPixel> | ||
| { | ||
| // arrange | ||
| using var input = provider.GetImage(JpegDecoder.Instance); |
There was a problem hiding this comment.
Please favour explicit types and target typed new expressions
| /// <summary> | ||
| /// Gets the comments. | ||
| /// </summary> | ||
| public ICollection<Memory<char>>? Comments { get; } |
There was a problem hiding this comment.
I don't think this should be nullable. Collections should always have a backing value in a publi API. I would use IList also to prevent all the casting and maintain consistency with the PngMetadata.TextDataProperty
On that note we could use a specific struct JpegComData which would contain the backing memory and can have static FromString(string value)
public readonly struct JpegComData
{
public static JpegComData FromString(string value) => new(value.AsMemory());
public JpegComData(ReadOnlyMemory<char> value)
=> this.Value = value;
public ReadOnlyMemory<char> Value { get; }
public override string ToString() => this.Value.ToString();
}This would also remove the need for any extension methods.
There was a problem hiding this comment.
Hi!
I'm in favor of an empty collection instead of a null one - it's better from a user perspective.
Not so sure about JpegComData, do we really need to expose comments as ReadOnlyMemory<char> instead of a plain string?
There was a problem hiding this comment.
I thought we couldn't guarantee that the data was always an ASCII string and that's why we had a collection of char?
There was a problem hiding this comment.
I thought we couldn't guarantee that the data was always an ASCII string and that's why we had a collection of char?
True, if I recall correctly jpeg specification doesn't specify anything about comment section contents.
Why would we need FromString(string value) method though?
There was a problem hiding this comment.
Sorry @br3aker I was confinced I'd replied to this!!
I'm just covering the most common use case, ASCII strings, in a nice manner. Saves using String.AsMemory() which feels a little clunky.
| /// <param name="metadata">The image metadata.</param> | ||
| private void WriteComment(JpegMetadata metadata) | ||
| { | ||
| if (metadata.Comments is { Count: 0 }) |
There was a problem hiding this comment.
Can't we just use metadata.Comments.Length == 0?
| } | ||
|
|
||
| // Length (comment strings lengths) + (comments markers with payload sizes) | ||
| int commentsBytes = metadata.Comments.Sum(x => x.Length) + (metadata.Comments.Count * 4); |
There was a problem hiding this comment.
I'm against using LINQ at all. While this won't slow down jpeg codec overall it's slower than calculating sum yourself :)
There was a problem hiding this comment.
We shouldn't use Linq in the decoders/encoders. We also need to ensure the length of the comment does not exceed the maximum.
There was a problem hiding this comment.
Ah yes, too big comment data must be scattered amongst multiple markers if I recall correctly.
| /// <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 SetComment(this JpegMetadata metadata, int index, string comment) |
There was a problem hiding this comment.
Do we really need such methods if comments are exposed as IList?
IList has all these methods so users can do whatever they want.
commented
Feb 6, 2024
|
I've made changes as you mentioned in PR comments. I left them open, because I don't know what the usual practice is for closing them (by the reviewer or contributor) on your repo. |
| JpegMetadata metadata = this.Metadata.GetFormatMetadata(JpegFormat.Instance); | ||
|
|
||
| stream.Read(temp); | ||
| string comment = Encoding.ASCII.GetString(temp); |
There was a problem hiding this comment.
We're assuming here that the input is always ASCII when we do not actually know that. I would create a char[] cast it to byte[], read the stream and pass it to the JpegComData constructor.
| if (comment.Length > maxCommentLength) | ||
| { | ||
| string splitComment = comment.Substring(maxCommentLength, comment.Length - maxCommentLength); | ||
| metadata.Comments.Insert(i + 1, JpegComData.FromString(splitComment)); |
There was a problem hiding this comment.
We should not be changing the metadata here. Encoding should leave the image unchanged.
|
|
||
| int commentLength = comment.Length + 4; | ||
|
|
||
| Span<byte> commentSpan = new byte[commentLength]; |
There was a problem hiding this comment.
You should be able to avoid the allocation here by using a single buffer of the maximum length plus markers outside of the loop and slicing.
| payloadSize[0] = (byte)((comWithoutMarker >> 8) & 0xFF); | ||
| payloadSize[1] = (byte)(comWithoutMarker & 0xFF); | ||
|
|
||
| Encoding.ASCII.GetBytes(comment, payload); |
There was a problem hiding this comment.
We don't know it's ASCII. I would cast and write.
left a comment
There was a problem hiding this comment.
@RobertMut I reworked your implementation to remove the extra allocations. Thanks for your help here!
Prerequisites
Description
This PR adds support for COM marker in JPEG as mentioned in #2067 and #2061
Implemented:
ICollection<Memory<char>>metadata property (Does Jpeg format need comment marker support? #2067),