Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
64 changes: 64 additions & 0 deletions src/ImageSharp/Metadata/Profiles/Exif/ExifConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@
// Licensed under the Apache License, Version 2.0.

using System;
using System.Buffers.Binary;
using System.Text;

namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
internal static class ExifConstants
{
private const ulong AsciiCode = 0x_41_53_43_49_49_00_00_00;
private const ulong JISCode = 0x_4A_49_53_00_00_00_00_00;
private const ulong UnicodeCode = 0x_55_4E_49_43_4F_44_45_00;
private const ulong UndefinedCode = 0x_00_00_00_00_00_00_00_00;

private static readonly byte[] AsciiCodeBytes = { 0x41, 0x53, 0x43, 0x49, 0x49, 0, 0, 0 };
private static readonly byte[] JISCodeBytes = { 0x4A, 0x49, 0x53, 0, 0, 0, 0, 0 };
private static readonly byte[] UnicodeCodeBytes = { 0x55, 0x4E, 0x49, 0x43, 0x4F, 0x44, 0x45, 0 };
private static readonly byte[] UndefinedCodeBytes = { 0, 0, 0, 0, 0, 0, 0, 0 };

public const int CharacterCodeBytesLength = 8;

public static ReadOnlySpan<byte> LittleEndianByteOrderMarker => new byte[]
{
(byte)'I',
Expand All @@ -22,5 +36,55 @@ internal static class ExifConstants
0x00,
0x2A
};

public static Encoding DefaultAsciiEncoding => Encoding.UTF8;

public static Encoding JIS0208Encoding => Encoding.GetEncoding(932);

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

code = default;
return false;
}

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

public static Encoding GetEncoding(EncodedStringCode code) => code switch
{
EncodedStringCode.ASCII => Encoding.ASCII,
EncodedStringCode.JIS => JIS0208Encoding,
EncodedStringCode.Unicode => Encoding.Unicode,
EncodedStringCode.Undefined => Encoding.UTF8,
_ => Encoding.UTF8
};
}
}
9 changes: 8 additions & 1 deletion src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private string ConvertToString(ReadOnlySpan<byte> buffer)
buffer = buffer.Slice(0, nullCharIndex);
}

return Encoding.UTF8.GetString(buffer);
return ExifConstants.DefaultAsciiEncoding.GetString(buffer);
}

private object ConvertValue(ExifDataType dataType, ReadOnlySpan<byte> buffer, bool isArray)
Expand Down Expand Up @@ -360,6 +360,13 @@ private object ConvertValue(ExifDataType dataType, ReadOnlySpan<byte> buffer, bo
return this.ConvertToByte(buffer);
}

// ext processing
if (ExifConstants.TryDetect(buffer, out EncodedStringCode code))
{
string text = ExifConstants.GetEncoding(code).GetString(buffer.Slice(ExifConstants.CharacterCodeBytesLength));
return new EncodedString(text, code);
}

return buffer.ToArray();
default:
throw new NotSupportedException($"Data type {dataType} is not supported.");
Expand Down
36 changes: 27 additions & 9 deletions src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,12 @@ internal static uint GetNumberOfComponents(IExifValue exifValue)

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

if (value is EncodedString encodedString)
{
return (uint)ExifConstants.GetEncoding(encodedString.Code).GetByteCount(encodedString.Text) + 8;
}

if (value is Array arrayValue)
Expand All @@ -289,11 +294,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,13 +378,31 @@ 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.DefaultAsciiEncoding.GetBytes((string)value), destination, offset);
destination[offset] = 0;
return offset + 1;
case ExifDataType.Byte:
case ExifDataType.Undefined:
destination[offset] = (byte)value;
return offset + 1;
case ExifDataType.Undefined:
if (value is EncodedString encodedString)
{
ReadOnlySpan<byte> codeBytes = ExifConstants.GetCodeBytes(encodedString.Code);
codeBytes.CopyTo(destination.Slice(offset));
offset += codeBytes.Length;

ReadOnlySpan<byte> dataBytes = ExifConstants.GetEncoding(encodedString.Code).GetBytes(encodedString.Text);
dataBytes.CopyTo(destination.Slice(offset));
offset += dataBytes.Length;

return offset;
}
else
{
destination[offset] = (byte)value;
return offset + 1;
}

case ExifDataType.DoubleFloat:
return WriteDouble((double)value, destination, offset);
case ExifDataType.Short:
Expand Down Expand Up @@ -427,7 +445,7 @@ private static int WriteValue(ExifDataType dataType, object value, Span<byte> de

internal static int WriteValue(IExifValue value, Span<byte> destination, int offset)
{
if (value.IsArray && value.DataType != ExifDataType.Ascii)
if (value.IsArray)
{
return WriteArray(value, destination, offset);
}
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);
}
}
15 changes: 0 additions & 15 deletions src/ImageSharp/Metadata/Profiles/Exif/Tags/ExifTag.Undefined.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public abstract partial class ExifTag
/// </summary>
public static ExifTag<byte[]> MakerNote { get; } = new ExifTag<byte[]>(ExifTagValue.MakerNote);

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

/// <summary>
/// Gets the FlashpixVersion exif tag.
/// </summary>
Expand Down Expand Up @@ -71,16 +66,6 @@ public abstract partial class ExifTag
/// </summary>
public static ExifTag<byte[]> ImageSourceData { get; } = new ExifTag<byte[]>(ExifTagValue.ImageSourceData);

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

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

/// <summary>
/// Gets the FileSource exif tag.
/// </summary>
Expand Down
59 changes: 59 additions & 0 deletions src/ImageSharp/Metadata/Profiles/Exif/Values/EncodedString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;

namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
/// <summary>
/// The EXIF encoded string structure.
/// </summary>
public readonly struct EncodedString : IEquatable<EncodedString>
{
/// <summary>
/// Initializes a new instance of the <see cref="EncodedString" /> struct.
/// </summary>
/// <param name="text">The text.</param>
public EncodedString(string text)
: this(text, EncodedStringCode.Unicode)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="EncodedString" /> struct.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="code">The code.</param>
public EncodedString(string text, EncodedStringCode code)
{
this.Text = text;
this.Code = code;
}

/// <summary>
/// Gets the text.
/// </summary>
public string Text { get; }

/// <summary>
/// Gets the character ode.
/// </summary>
public EncodedStringCode Code { get; }

/// <inheritdoc/>
public override bool Equals(object obj) => obj is EncodedString other && this.Equals(other);

/// <inheritdoc/>
public bool Equals(EncodedString other)
{
return this.Text == other.Text && this.Code == other.Code;
}

/// <inheritdoc/>
public override int GetHashCode() =>
HashCode.Combine(this.Text, this.Code);

/// <inheritdoc/>
public override string ToString() => this.Text;
}
}
31 changes: 31 additions & 0 deletions src/ImageSharp/Metadata/Profiles/Exif/Values/EncodedStringCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
/// <summary>
/// The 8-byte The character code enum.
/// </summary>
public enum EncodedStringCode
{
/// <summary>
/// The ASCII ITU-T T.50 IA5 character code.
/// </summary>
ASCII,

/// <summary>
/// The JIS X208-1990 character code.
/// </summary>
JIS,

/// <summary>
/// The Unicode character code.
/// </summary>
Unicode,

/// <summary>
/// The undefined character code.
/// </summary>
Undefined
}
}
51 changes: 51 additions & 0 deletions src/ImageSharp/Metadata/Profiles/Exif/Values/ExifEncodedString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System.Globalization;

namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
internal sealed class ExifEncodedString : ExifValue<EncodedString>
{
public ExifEncodedString(ExifTag<EncodedString> tag)
: base(tag)
{
}

public ExifEncodedString(ExifTagValue tag)
: base(tag)
{
}

private ExifEncodedString(ExifEncodedString value)
: base(value)
{
}

public override ExifDataType DataType => ExifDataType.Undefined;

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

public override bool TrySetValue(object value)
{
if (base.TrySetValue(value))
{
return true;
}

switch (value)
{
case string stringValue:
this.Value = new EncodedString(stringValue);
return true;
case byte[] bytes:
this.Value = new EncodedString(ExifConstants.DefaultAsciiEncoding.GetString(bytes));
return true;
default:
return false;
}
}

public override IExifValue DeepClone() => new ExifEncodedString(this);
}
}
6 changes: 3 additions & 3 deletions src/ImageSharp/Metadata/Profiles/Exif/Values/ExifValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,16 @@ private static object CreateValue(ExifTagValue tag)
case ExifTagValue.ExifVersion: return new ExifByteArray(ExifTag.ExifVersion, ExifDataType.Undefined);
case ExifTagValue.ComponentsConfiguration: return new ExifByteArray(ExifTag.ComponentsConfiguration, ExifDataType.Undefined);
case ExifTagValue.MakerNote: return new ExifByteArray(ExifTag.MakerNote, ExifDataType.Undefined);
case ExifTagValue.UserComment: return new ExifByteArray(ExifTag.UserComment, ExifDataType.Undefined);
case ExifTagValue.UserComment: return new ExifEncodedString(ExifTag.UserComment);
case ExifTagValue.FlashpixVersion: return new ExifByteArray(ExifTag.FlashpixVersion, ExifDataType.Undefined);
case ExifTagValue.SpatialFrequencyResponse: return new ExifByteArray(ExifTag.SpatialFrequencyResponse, ExifDataType.Undefined);
case ExifTagValue.SpatialFrequencyResponse2: return new ExifByteArray(ExifTag.SpatialFrequencyResponse2, ExifDataType.Undefined);
case ExifTagValue.Noise: return new ExifByteArray(ExifTag.Noise, ExifDataType.Undefined);
case ExifTagValue.CFAPattern: return new ExifByteArray(ExifTag.CFAPattern, ExifDataType.Undefined);
case ExifTagValue.DeviceSettingDescription: return new ExifByteArray(ExifTag.DeviceSettingDescription, ExifDataType.Undefined);
case ExifTagValue.ImageSourceData: return new ExifByteArray(ExifTag.ImageSourceData, ExifDataType.Undefined);
case ExifTagValue.GPSProcessingMethod: return new ExifByteArray(ExifTag.GPSProcessingMethod, ExifDataType.Undefined);
case ExifTagValue.GPSAreaInformation: return new ExifByteArray(ExifTag.GPSAreaInformation, ExifDataType.Undefined);
case ExifTagValue.GPSProcessingMethod: return new ExifEncodedString(ExifTag.GPSProcessingMethod);
case ExifTagValue.GPSAreaInformation: return new ExifEncodedString(ExifTag.GPSAreaInformation);

default: return null;
}
Expand Down
Loading