Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,66 @@ private static Encoding JIS0208Encoding
_ => UndefinedCodeBytes
};

public static Encoding GetEncoding(CharacterCode code) => code switch
public static Encoding GetEncoding(CharacterCode code, ByteOrder order) => code switch
{
CharacterCode.ASCII => Encoding.ASCII,
CharacterCode.JIS => JIS0208Encoding,
CharacterCode.Unicode => Encoding.Unicode,
CharacterCode.Unicode => order is ByteOrder.BigEndian ? Encoding.BigEndianUnicode : Encoding.Unicode,
CharacterCode.Undefined => Encoding.UTF8,
_ => Encoding.UTF8
};

public static bool TryParse(ReadOnlySpan<byte> buffer, out EncodedString encodedString)
public static bool TryParse(ReadOnlySpan<byte> buffer, ByteOrder order, out EncodedString encodedString)
{
if (TryDetect(buffer, out CharacterCode code))
{
string text = GetEncoding(code).GetString(buffer[CharacterCodeBytesLength..]);
encodedString = new EncodedString(code, text);
return true;
ReadOnlySpan<byte> textBuffer = buffer[CharacterCodeBytesLength..];
if (code == CharacterCode.Unicode && textBuffer.Length >= 2)
{
// Check BOM
if (textBuffer[0] == 0xFF && textBuffer[1] == 0xFE)
{
// Little-endian BOM
string text = Encoding.Unicode.GetString(textBuffer[2..]);
encodedString = new EncodedString(code, text);
return true;
}
else if (textBuffer[0] == 0xFE && textBuffer[1] == 0xFF)
{
// Big-endian BOM
string text = Encoding.BigEndianUnicode.GetString(textBuffer[2..]);
encodedString = new EncodedString(code, text);
return true;
}
else
{
// No BOM, use EXIF byte order
string text = GetEncoding(code, order).GetString(textBuffer);
encodedString = new EncodedString(code, text);
return true;
}
}
else
{
string text = GetEncoding(code, order).GetString(textBuffer);
encodedString = new EncodedString(code, text);
return true;
}
}

encodedString = default;
return false;
}

public static uint GetDataLength(EncodedString encodedString) =>
(uint)GetEncoding(encodedString.Code).GetByteCount(encodedString.Text) + CharacterCodeBytesLength;
(uint)GetEncoding(encodedString.Code, ByteOrder.LittleEndian).GetByteCount(encodedString.Text) + CharacterCodeBytesLength;

public static int Write(EncodedString encodedString, Span<byte> destination)
{
GetCodeBytes(encodedString.Code).CopyTo(destination);

string text = encodedString.Text;
int count = Write(GetEncoding(encodedString.Code), text, destination[CharacterCodeBytesLength..]);
int count = Write(GetEncoding(encodedString.Code, ByteOrder.LittleEndian), text, destination[CharacterCodeBytesLength..]);

return CharacterCodeBytesLength + count;
}
Expand Down
23 changes: 20 additions & 3 deletions src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ internal abstract class BaseExifReader
private readonly MemoryAllocator? allocator;
private readonly Stream data;
private List<ExifTag>? invalidTags;

private List<ulong>? subIfds;
private bool isBigEndian;

protected BaseExifReader(Stream stream, MemoryAllocator? allocator)
{
Expand All @@ -116,7 +116,17 @@ protected BaseExifReader(Stream stream, MemoryAllocator? allocator)
/// </summary>
public uint ThumbnailOffset { get; protected set; }

public bool IsBigEndian { get; protected set; }
public bool IsBigEndian
{
get => this.isBigEndian;
protected set
{
this.isBigEndian = value;
this.ByteOrder = value ? ByteOrder.BigEndian : ByteOrder.LittleEndian;
}
}

protected ByteOrder ByteOrder { get; private set; }

public List<(ulong Offset, ExifDataType DataType, ulong NumberOfComponents, ExifValue Exif)> BigValues { get; } = new();

Expand Down Expand Up @@ -485,7 +495,14 @@ private void ReadValue64(List<IExifValue> values, Span<byte> offsetBuffer)

private void Add(IList<IExifValue> values, IExifValue exif, object? value)
{
if (!exif.TrySetValue(value))
if (exif is ExifEncodedString encodedString)
{
if (!encodedString.TrySetValue(value, this.ByteOrder))
{
return;
}
}
else if (!exif.TrySetValue(value))
{
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/Metadata/Profiles/Exif/Tags/ExifTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public abstract partial class ExifTag : IEquatable<ExifTag>
/// </summary>
/// <param name="left">The first <see cref="ExifTag"/> to compare.</param>
/// <param name="right"> The second <see cref="ExifTag"/> to compare.</param>
public static bool operator ==(ExifTag left, ExifTag right) => Equals(left, right);
public static bool operator ==(ExifTag? left, ExifTag? right) => left?.Equals(right) == true;

/// <summary>
/// Determines whether the specified <see cref="ExifTag"/> instances are not considered equal.
/// </summary>
/// <param name="left">The first <see cref="ExifTag"/> to compare.</param>
/// <param name="right"> The second <see cref="ExifTag"/> to compare.</param>
public static bool operator !=(ExifTag left, ExifTag right) => !Equals(left, right);
public static bool operator !=(ExifTag? left, ExifTag? right) => !(left == right);

/// <inheritdoc/>
public override bool Equals(object? obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private ExifEncodedString(ExifEncodedString value)

protected override string StringValue => this.Value.Text;

public override bool TrySetValue(object? value)
public bool TrySetValue(object? value, ByteOrder order)
{
if (base.TrySetValue(value))
{
Expand All @@ -38,7 +38,7 @@ public override bool TrySetValue(object? value)
}
else if (value is byte[] buffer)
{
if (ExifEncodedStringHelpers.TryParse(buffer, out EncodedString encodedString))
if (ExifEncodedStringHelpers.TryParse(buffer, order, out EncodedString encodedString))
{
this.Value = encodedString;
return true;
Expand All @@ -48,5 +48,8 @@ public override bool TrySetValue(object? value)
return false;
}

public override bool TrySetValue(object? value)
=> this.TrySetValue(value, ByteOrder.LittleEndian);

public override IExifValue DeepClone() => new ExifEncodedString(this);
}
19 changes: 19 additions & 0 deletions tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
Expand Down Expand Up @@ -559,4 +560,22 @@ public void WebpDecoder_CanDecode_Issue2925<TPixel>(TestImageProvider<TPixel> pr
image.DebugSave(provider);
image.CompareToOriginal(provider, ReferenceDecoder);
}

[Theory]
[WithFile(Lossy.Issue2906, PixelTypes.Rgba32)]
public void WebpDecoder_CanDecode_Issue2906<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage(WebpDecoder.Instance);

ExifProfile exifProfile = image.Metadata.ExifProfile;
IExifValue<EncodedString> comment = exifProfile.GetValue(ExifTag.UserComment);

Assert.NotNull(comment);
Assert.Equal(EncodedString.CharacterCode.Unicode, comment.Value.Code);
Assert.StartsWith("1girl, pariya, ", comment.Value.Text);

image.DebugSave(provider);
image.CompareToOriginal(provider, ReferenceDecoder);
}
}
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ public static class Lossy
public const string Issue2801 = "Webp/issues/Issue2801.webp";
public const string Issue2866 = "Webp/issues/Issue2866.webp";
public const string Issue2925 = "Webp/issues/Issue2925.webp";
public const string Issue2906 = "Webp/issues/Issue2906.webp";
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Webp/issues/Issue2906.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading