Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e78190f
implement read&write encoded string tags
IldarKhayrutdinov Jan 6, 2022
0082fd2
encoded string tags
IldarKhayrutdinov Jan 10, 2022
83547db
update test
IldarKhayrutdinov Jan 10, 2022
d0dbb8e
add hashCode method
IldarKhayrutdinov Jan 10, 2022
c1ebf13
add missing docs
IldarKhayrutdinov Jan 10, 2022
06e1d8b
Update src/ImageSharp/Metadata/Profiles/Exif/ExifConstants.cs
IldarKhayrutdinov Jan 12, 2022
53f85ac
build fixes
IldarKhayrutdinov Jan 12, 2022
aa2eba6
add test
IldarKhayrutdinov Jan 12, 2022
cb9d1b8
test fix
IldarKhayrutdinov Jan 12, 2022
d4fb48d
Merge remote-tracking branch 'upstream/master' into exif-encoded-strings
IldarKhayrutdinov Jan 12, 2022
2e219ed
renaming, cosmetics
IldarKhayrutdinov Jan 18, 2022
f954a84
move EncodedStringCode enum into EncodedString class
IldarKhayrutdinov Jan 18, 2022
b510cfb
Merge remote-tracking branch 'upstream/master' into exif-encoded-strings
IldarKhayrutdinov Jan 18, 2022
c71b2aa
small refactory EncodedString methods
IldarKhayrutdinov Jan 19, 2022
cbb4514
Change data types of UCS-2 tags: byte[] -> string
IldarKhayrutdinov Jan 19, 2022
5649128
correct reading UCS-2 encoded string tags
IldarKhayrutdinov Jan 19, 2022
918db05
add missing file
IldarKhayrutdinov Jan 19, 2022
14d7b60
support writing UCS-2 encoded string
IldarKhayrutdinov Jan 19, 2022
1960c25
Merge remote-tracking branch 'upstream/master' into exif-encoded-strings
JimBobSquarePants Jan 25, 2022
0fdcf04
Expand test and cleanup
JimBobSquarePants Jan 25, 2022
cda6b24
Bug fixes of reading and writing (UCS2 and EncodedString)
IldarKhayrutdinov Jan 30, 2022
2ed1b6d
format
IldarKhayrutdinov Jan 30, 2022
d3c635f
Merge branch 'exif-encoded-strings' of github.com:IldarKhayrutdinov/I…
IldarKhayrutdinov Jan 30, 2022
e700656
Merge remote-tracking branch 'upstream/master' into exif-encoded-strings
IldarKhayrutdinov Jan 30, 2022
ac9a98a
cleanup
IldarKhayrutdinov Jan 30, 2022
7b5dd17
cleanup
IldarKhayrutdinov Jan 30, 2022
3054c5b
add encoded string tags to common tests
IldarKhayrutdinov Jan 30, 2022
5dcbcfd
add test tag
IldarKhayrutdinov Jan 30, 2022
361eb85
comment
IldarKhayrutdinov Jan 30, 2022
62ca842
Add test
IldarKhayrutdinov Jan 30, 2022
19a7b8c
update JIS test
IldarKhayrutdinov Jan 30, 2022
c292e65
comment
IldarKhayrutdinov Jan 30, 2022
d01dfe8
change JIS encoding, update test
IldarKhayrutdinov Jan 30, 2022
ab3ca30
cleanup
IldarKhayrutdinov Jan 30, 2022
855d1aa
minor test
IldarKhayrutdinov Jan 30, 2022
5d50298
memory improvements
IldarKhayrutdinov Feb 1, 2022
a5182b0
Merge branch 'master' into exif-encoded-strings
JimBobSquarePants Feb 3, 2022
fddfee2
Update DC-X008-Translation-2019-E.pdf
JimBobSquarePants Feb 3, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ImageSharp/ImageSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

<ItemGroup>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) OR '$(TargetFramework)' == 'net472'">
Expand Down
Binary file not shown.
4 changes: 4 additions & 0 deletions src/ImageSharp/Metadata/Profiles/Exif/ExifConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.

using System;
using System.Text;

namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
Expand All @@ -22,5 +23,8 @@ internal static class ExifConstants
0x00,
0x2A
};

// UTF-8 is better than ASCII, UTF-8 encodes the ASCII codes the same way
public static Encoding DefaultEncoding => Encoding.UTF8;
}
}
121 changes: 121 additions & 0 deletions src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Buffers.Binary;
using System.Text;
using static SixLabors.ImageSharp.Metadata.Profiles.Exif.EncodedString;

namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
internal static class ExifEncodedStringHelpers
{
public const int CharacterCodeBytesLength = 8;

private const ulong AsciiCode = 0x_00_00_00_49_49_43_53_41;
private const ulong JISCode = 0x_00_00_00_00_00_53_49_4A;
private const ulong UnicodeCode = 0x_45_44_4F_43_49_4E_55;
private const ulong UndefinedCode = 0x_00_00_00_00_00_00_00_00;

private static ReadOnlySpan<byte> AsciiCodeBytes => new byte[] { 0x41, 0x53, 0x43, 0x49, 0x49, 0, 0, 0 };

private static ReadOnlySpan<byte> JISCodeBytes => new byte[] { 0x4A, 0x49, 0x53, 0, 0, 0, 0, 0 };

private static ReadOnlySpan<byte> UnicodeCodeBytes => new byte[] { 0x55, 0x4E, 0x49, 0x43, 0x4F, 0x44, 0x45, 0 };

private static ReadOnlySpan<byte> UndefinedCodeBytes => new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };

// 20932 EUC-JP Japanese (JIS 0208-1990 and 0212-1990)
// https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding?view=net-6.0
private static Encoding JIS0208Encoding => CodePagesEncodingProvider.Instance.GetEncoding(20932);

public static bool IsEncodedString(ExifTagValue tag) => tag switch
{
ExifTagValue.UserComment or ExifTagValue.GPSProcessingMethod or ExifTagValue.GPSAreaInformation => true,
_ => false
};

public static ReadOnlySpan<byte> GetCodeBytes(CharacterCode code) => code switch
{
CharacterCode.ASCII => AsciiCodeBytes,
CharacterCode.JIS => JISCodeBytes,
CharacterCode.Unicode => UnicodeCodeBytes,
CharacterCode.Undefined => UndefinedCodeBytes,
_ => UndefinedCodeBytes
};

public static Encoding GetEncoding(CharacterCode code) => code switch
{
CharacterCode.ASCII => Encoding.ASCII,
CharacterCode.JIS => JIS0208Encoding,
CharacterCode.Unicode => Encoding.Unicode,
CharacterCode.Undefined => Encoding.UTF8,
_ => Encoding.UTF8
};

public static bool TryParse(ReadOnlySpan<byte> buffer, out EncodedString encodedString)
{
if (TryDetect(buffer, out CharacterCode code))
{
string text = GetEncoding(code).GetString(buffer.Slice(CharacterCodeBytesLength));
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;

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.Slice(CharacterCodeBytesLength));

return CharacterCodeBytesLength + count;
}

public static unsafe int Write(Encoding encoding, string value, Span<byte> destination)
{
fixed (char* c = value)
{
fixed (byte* b = destination)
{
return encoding.GetBytes(c, value.Length, b, destination.Length);
}
}
}

private static bool TryDetect(ReadOnlySpan<byte> buffer, out CharacterCode code)
{
if (buffer.Length >= CharacterCodeBytesLength)
{
ulong test = BinaryPrimitives.ReadUInt64LittleEndian(buffer);
switch (test)
{
case AsciiCode:
code = CharacterCode.ASCII;
return true;
case JISCode:
code = CharacterCode.JIS;
return true;
case UnicodeCode:
code = CharacterCode.Unicode;
return true;
case UndefinedCode:
code = CharacterCode.Undefined;
return true;
default:
break;
}
}

code = default;
return false;
}
}
}
19 changes: 7 additions & 12 deletions src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ private static TDataType[] ToArray<TDataType>(ExifDataType dataType, ReadOnlySpa
return result;
}

private byte ConvertToByte(ReadOnlySpan<byte> buffer) => buffer[0];

private string ConvertToString(ReadOnlySpan<byte> buffer)
private static string ConvertToString(Encoding encoding, ReadOnlySpan<byte> buffer)
{
int nullCharIndex = buffer.IndexOf((byte)0);

Expand All @@ -252,9 +250,11 @@ private string ConvertToString(ReadOnlySpan<byte> buffer)
buffer = buffer.Slice(0, nullCharIndex);
}

return Encoding.UTF8.GetString(buffer);
return encoding.GetString(buffer);
}

private byte ConvertToByte(ReadOnlySpan<byte> buffer) => buffer[0];

private object ConvertValue(ExifDataType dataType, ReadOnlySpan<byte> buffer, bool isArray)
{
if (buffer.Length == 0)
Expand All @@ -267,8 +267,9 @@ private object ConvertValue(ExifDataType dataType, ReadOnlySpan<byte> buffer, bo
case ExifDataType.Unknown:
return null;
case ExifDataType.Ascii:
return this.ConvertToString(buffer);
return ConvertToString(ExifConstants.DefaultEncoding, buffer);
case ExifDataType.Byte:
case ExifDataType.Undefined:
if (!isArray)
{
return this.ConvertToByte(buffer);
Expand Down Expand Up @@ -354,13 +355,7 @@ private object ConvertValue(ExifDataType dataType, ReadOnlySpan<byte> buffer, bo
}

return ToArray(dataType, buffer, this.ConvertToUInt64);
case ExifDataType.Undefined:
if (!isArray)
{
return this.ConvertToByte(buffer);
}

return buffer.ToArray();
default:
throw new NotSupportedException($"Data type {dataType} is not supported.");
}
Expand Down Expand Up @@ -453,7 +448,7 @@ private void ReadValue64(List<IExifValue> values, Span<byte> offsetBuffer)
// Likewise, tags that point to other IFDs, like e.g. the SubIFDs tag, are now allowed to have the datatype TIFF_IFD8 in BigTIFF.
// Again, the old datatypes TIFF_IFD, and the hardly recommendable TIFF_LONG, are still valid, too.
// https://www.awaresystems.be/imaging/tiff/bigtiff.html
ExifValue exifValue = null;
ExifValue exifValue;
switch (tag)
{
case ExifTagValue.StripOffsets:
Expand Down
21 changes: 21 additions & 0 deletions src/ImageSharp/Metadata/Profiles/Exif/ExifUcs2StringHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Text;

namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
internal static class ExifUcs2StringHelpers
{
public static Encoding Ucs2Encoding => Encoding.GetEncoding("UCS-2");

public static bool IsUcs2Tag(ExifTagValue tag) => tag switch
{
ExifTagValue.XPAuthor or ExifTagValue.XPComment or ExifTagValue.XPKeywords or ExifTagValue.XPSubject or ExifTagValue.XPTitle => true,
_ => false,
};

public static int Write(string value, Span<byte> destination) => ExifEncodedStringHelpers.Write(Ucs2Encoding, value, destination);
}
}
39 changes: 27 additions & 12 deletions src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Text;

namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
Expand Down Expand Up @@ -274,9 +273,19 @@ internal static uint GetNumberOfComponents(IExifValue exifValue)
{
object value = exifValue.GetValue();

if (ExifUcs2StringHelpers.IsUcs2Tag((ExifTagValue)(ushort)exifValue.Tag))
{
return (uint)ExifUcs2StringHelpers.Ucs2Encoding.GetByteCount((string)value);
}

if (value is EncodedString encodedString)
{
return ExifEncodedStringHelpers.GetDataLength(encodedString);
}

if (exifValue.DataType == ExifDataType.Ascii)
{
return (uint)Encoding.UTF8.GetBytes((string)value).Length + 1;
return (uint)ExifConstants.DefaultEncoding.GetByteCount((string)value) + 1;
}

if (value is Array arrayValue)
Expand All @@ -289,11 +298,6 @@ internal static uint GetNumberOfComponents(IExifValue exifValue)

private static int WriteArray(IExifValue value, Span<byte> destination, int offset)
{
if (value.DataType == ExifDataType.Ascii)
{
return WriteValue(ExifDataType.Ascii, value.GetValue(), destination, offset);
}

int newOffset = offset;
foreach (object obj in (Array)value.GetValue())
{
Expand Down Expand Up @@ -378,7 +382,7 @@ private static int WriteValue(ExifDataType dataType, object value, Span<byte> de
switch (dataType)
{
case ExifDataType.Ascii:
offset = Write(Encoding.UTF8.GetBytes((string)value), destination, offset);
offset = Write(ExifConstants.DefaultEncoding.GetBytes((string)value), destination, offset);
destination[offset] = 0;
return offset + 1;
case ExifDataType.Byte:
Expand Down Expand Up @@ -425,14 +429,25 @@ private static int WriteValue(ExifDataType dataType, object value, Span<byte> de
}
}

internal static int WriteValue(IExifValue value, Span<byte> destination, int offset)
internal static int WriteValue(IExifValue exifValue, Span<byte> destination, int offset)
{
if (value.IsArray && value.DataType != ExifDataType.Ascii)
object value = exifValue.GetValue();

if (ExifUcs2StringHelpers.IsUcs2Tag((ExifTagValue)(ushort)exifValue.Tag))
{
return offset + ExifUcs2StringHelpers.Write((string)value, destination.Slice(offset));
}
else if (value is EncodedString encodedString)
{
return offset + ExifEncodedStringHelpers.Write(encodedString, destination.Slice(offset));
}

if (exifValue.IsArray)
{
return WriteArray(value, destination, offset);
return WriteArray(exifValue, destination, offset);
}

return WriteValue(value.DataType, value.GetValue(), destination, offset);
return WriteValue(exifValue.DataType, value, destination, offset);
}
}
}
25 changes: 0 additions & 25 deletions src/ImageSharp/Metadata/Profiles/Exif/Tags/ExifTag.ByteArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,6 @@ public abstract partial class ExifTag
/// </summary>
public static ExifTag<byte[]> TIFFEPStandardID => new ExifTag<byte[]>(ExifTagValue.TIFFEPStandardID);

/// <summary>
/// Gets the XPTitle exif tag.
/// </summary>
public static ExifTag<byte[]> XPTitle => new ExifTag<byte[]>(ExifTagValue.XPTitle);

/// <summary>
/// Gets the XPComment exif tag.
/// </summary>
public static ExifTag<byte[]> XPComment => new ExifTag<byte[]>(ExifTagValue.XPComment);

/// <summary>
/// Gets the XPAuthor exif tag.
/// </summary>
public static ExifTag<byte[]> XPAuthor => new ExifTag<byte[]>(ExifTagValue.XPAuthor);

/// <summary>
/// Gets the XPKeywords exif tag.
/// </summary>
public static ExifTag<byte[]> XPKeywords => new ExifTag<byte[]>(ExifTagValue.XPKeywords);

/// <summary>
/// Gets the XPSubject exif tag.
/// </summary>
public static ExifTag<byte[]> XPSubject => new ExifTag<byte[]>(ExifTagValue.XPSubject);

/// <summary>
/// Gets the GPSVersionID exif tag.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
/// <content/>
public abstract partial class ExifTag
{
/// <summary>
/// Gets the UserComment exif tag.
/// </summary>
public static ExifTag<EncodedString> UserComment { get; } = new ExifTag<EncodedString>(ExifTagValue.UserComment);

/// <summary>
/// Gets the GPSProcessingMethod exif tag.
/// </summary>
public static ExifTag<EncodedString> GPSProcessingMethod { get; } = new ExifTag<EncodedString>(ExifTagValue.GPSProcessingMethod);

/// <summary>
/// Gets the GPSAreaInformation exif tag.
/// </summary>
public static ExifTag<EncodedString> GPSAreaInformation { get; } = new ExifTag<EncodedString>(ExifTagValue.GPSAreaInformation);
}
}
34 changes: 34 additions & 0 deletions src/ImageSharp/Metadata/Profiles/Exif/Tags/ExifTag.Ucs2String.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
/// <content/>
public abstract partial class ExifTag
{
/// <summary>
/// Gets the title tag used by Windows (encoded in UCS2).
/// </summary>
public static ExifTag<string> XPTitle => new ExifTag<string>(ExifTagValue.XPTitle);

/// <summary>
/// Gets the comment tag used by Windows (encoded in UCS2).
/// </summary>
public static ExifTag<string> XPComment => new ExifTag<string>(ExifTagValue.XPComment);

/// <summary>
/// Gets the author tag used by Windows (encoded in UCS2).
/// </summary>
public static ExifTag<string> XPAuthor => new ExifTag<string>(ExifTagValue.XPAuthor);

/// <summary>
/// Gets the keywords tag used by Windows (encoded in UCS2).
/// </summary>
public static ExifTag<string> XPKeywords => new ExifTag<string>(ExifTagValue.XPKeywords);

/// <summary>
/// Gets the subject tag used by Windows (encoded in UCS2).
/// </summary>
public static ExifTag<string> XPSubject => new ExifTag<string>(ExifTagValue.XPSubject);
}
}
Loading