-
-
Notifications
You must be signed in to change notification settings - Fork 886
[EXIF] Support UCS2 and 8-byte encoded string tags #1935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JimBobSquarePants
merged 38 commits into
SixLabors:master
from
IldarKhayrutdinov:exif-encoded-strings
Feb 3, 2022
Merged
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 0082fd2
encoded string tags
IldarKhayrutdinov 83547db
update test
IldarKhayrutdinov d0dbb8e
add hashCode method
IldarKhayrutdinov c1ebf13
add missing docs
IldarKhayrutdinov 06e1d8b
Update src/ImageSharp/Metadata/Profiles/Exif/ExifConstants.cs
IldarKhayrutdinov 53f85ac
build fixes
IldarKhayrutdinov aa2eba6
add test
IldarKhayrutdinov cb9d1b8
test fix
IldarKhayrutdinov d4fb48d
Merge remote-tracking branch 'upstream/master' into exif-encoded-strings
IldarKhayrutdinov 2e219ed
renaming, cosmetics
IldarKhayrutdinov f954a84
move EncodedStringCode enum into EncodedString class
IldarKhayrutdinov b510cfb
Merge remote-tracking branch 'upstream/master' into exif-encoded-strings
IldarKhayrutdinov c71b2aa
small refactory EncodedString methods
IldarKhayrutdinov cbb4514
Change data types of UCS-2 tags: byte[] -> string
IldarKhayrutdinov 5649128
correct reading UCS-2 encoded string tags
IldarKhayrutdinov 918db05
add missing file
IldarKhayrutdinov 14d7b60
support writing UCS-2 encoded string
IldarKhayrutdinov 1960c25
Merge remote-tracking branch 'upstream/master' into exif-encoded-strings
JimBobSquarePants 0fdcf04
Expand test and cleanup
JimBobSquarePants cda6b24
Bug fixes of reading and writing (UCS2 and EncodedString)
IldarKhayrutdinov 2ed1b6d
format
IldarKhayrutdinov d3c635f
Merge branch 'exif-encoded-strings' of github.com:IldarKhayrutdinov/I…
IldarKhayrutdinov e700656
Merge remote-tracking branch 'upstream/master' into exif-encoded-strings
IldarKhayrutdinov ac9a98a
cleanup
IldarKhayrutdinov 7b5dd17
cleanup
IldarKhayrutdinov 3054c5b
add encoded string tags to common tests
IldarKhayrutdinov 5dcbcfd
add test tag
IldarKhayrutdinov 361eb85
comment
IldarKhayrutdinov 62ca842
Add test
IldarKhayrutdinov 19a7b8c
update JIS test
IldarKhayrutdinov c292e65
comment
IldarKhayrutdinov d01dfe8
change JIS encoding, update test
IldarKhayrutdinov ab3ca30
cleanup
IldarKhayrutdinov 855d1aa
minor test
IldarKhayrutdinov 5d50298
memory improvements
IldarKhayrutdinov a5182b0
Merge branch 'master' into exif-encoded-strings
JimBobSquarePants fddfee2
Update DC-X008-Translation-2019-E.pdf
JimBobSquarePants 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+92 KB
(100%)
src/ImageSharp/Metadata/Profiles/Exif/DC-X008-Translation-2019-E.pdf
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/ImageSharp/Metadata/Profiles/Exif/ExifUcs2StringHelpers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/ImageSharp/Metadata/Profiles/Exif/Tags/ExifTag.EncodedString.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
34
src/ImageSharp/Metadata/Profiles/Exif/Tags/ExifTag.Ucs2String.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.