-
-
Notifications
You must be signed in to change notification settings - Fork 878
Support for XMP metadata #1918
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 23 commits into
SixLabors:master
from
ynse01:read-xmp-from-webp
Jan 11, 2022
Merged
Support for XMP metadata #1918
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b8bed9f
All image formats round trip
ynse01 fa3e6ff
More robust equals implementation
ynse01 32aa75b
Fix build on .NET core 2.1
ynse01 06beffb
Performance improvements
ynse01 f451013
Use memory allocator in Gif encoder
ynse01 f64f2cc
Skip over iTXT header
ynse01 ded8b20
Read only API for XmpProfile
ynse01 12263f9
Delete XElementEqualityComparer.cs
ynse01 b443e82
Gif: Check on frame count before writing repeat count
ynse01 3d981fe
Merge branch 'master' into read-xmp-from-webp
JimBobSquarePants 559edde
Don't throw when Position is out of range for BufferedReadStream
ynse01 760785e
Fix casing of test images file names
ynse01 e3ef796
Fix casing for Webp test image
ynse01 1ee34ba
Prevent memory allocations
ynse01 f356c61
Renaming the hasProfile fields
ynse01 12cd6bb
Merge branch 'master' into read-xmp-from-webp
JimBobSquarePants dd31636
Correct missing frame counter check
ynse01 d879dd8
Another allocation optimization
ynse01 de9a645
Merge branch 'read-xmp-from-webp' of https://github.com/ynse01/ImageS…
ynse01 c05fd21
Explicitly control lifetime of memory owner
ynse01 c654079
Zero out the iTxt header
ynse01 4904f32
Use memory allocator for all text chunks
ynse01 8630d19
Merge branch 'master' into read-xmp-from-webp
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
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
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
97 changes: 97 additions & 0 deletions
97
src/ImageSharp/Formats/Gif/Sections/GifXmpApplicationExtension.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,97 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using SixLabors.ImageSharp.Metadata.Profiles.Xmp; | ||
|
||
namespace SixLabors.ImageSharp.Formats.Gif | ||
{ | ||
internal readonly struct GifXmpApplicationExtension : IGifExtension | ||
{ | ||
public GifXmpApplicationExtension(byte[] data) => this.Data = data; | ||
|
||
public byte Label => GifConstants.ApplicationExtensionLabel; | ||
|
||
public int ContentLength => this.Data.Length + 269; // 12 + Data Length + 1 + 256 | ||
|
||
/// <summary> | ||
/// Gets the raw Data. | ||
/// </summary> | ||
public byte[] Data { get; } | ||
|
||
/// <summary> | ||
/// Reads the XMP metadata from the specified stream. | ||
/// </summary> | ||
/// <param name="stream">The stream to read from.</param> | ||
/// <returns>The XMP metadata</returns> | ||
/// <exception cref="ImageFormatException">Thrown if the XMP block is not properly terminated.</exception> | ||
public static GifXmpApplicationExtension Read(Stream stream) | ||
{ | ||
// Read data in blocks, until an \0 character is encountered. | ||
// We overshoot, indicated by the terminatorIndex variable. | ||
const int bufferSize = 256; | ||
var list = new List<byte[]>(); | ||
ynse01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int terminationIndex = -1; | ||
while (terminationIndex < 0) | ||
{ | ||
byte[] temp = new byte[bufferSize]; | ||
int bytesRead = stream.Read(temp); | ||
list.Add(temp); | ||
terminationIndex = Array.IndexOf(temp, (byte)1); | ||
} | ||
|
||
// Pack all the blocks (except magic trailer) into one single array again. | ||
int dataSize = ((list.Count - 1) * bufferSize) + terminationIndex; | ||
byte[] buffer = new byte[dataSize]; | ||
Span<byte> bufferSpan = buffer; | ||
int pos = 0; | ||
for (int j = 0; j < list.Count - 1; j++) | ||
{ | ||
list[j].CopyTo(bufferSpan.Slice(pos)); | ||
pos += bufferSize; | ||
} | ||
|
||
// Last one only needs the portion until terminationIndex copied over. | ||
Span<byte> lastBytes = list[list.Count - 1]; | ||
lastBytes.Slice(0, terminationIndex).CopyTo(bufferSpan.Slice(pos)); | ||
|
||
// Skip the remainder of the magic trailer. | ||
stream.Skip(258 - (bufferSize - terminationIndex)); | ||
return new GifXmpApplicationExtension(buffer); | ||
} | ||
|
||
public int WriteTo(Span<byte> buffer) | ||
{ | ||
int totalSize = this.ContentLength; | ||
if (buffer.Length < totalSize) | ||
{ | ||
throw new InsufficientMemoryException("Unable to write XMP metadata to GIF image"); | ||
} | ||
|
||
int bytesWritten = 0; | ||
buffer[bytesWritten++] = GifConstants.ApplicationBlockSize; | ||
|
||
// Write "XMP DataXMP" | ||
ReadOnlySpan<byte> idBytes = GifConstants.XmpApplicationIdentificationBytes; | ||
idBytes.CopyTo(buffer.Slice(bytesWritten)); | ||
bytesWritten += idBytes.Length; | ||
|
||
// XMP Data itself | ||
this.Data.CopyTo(buffer.Slice(bytesWritten)); | ||
bytesWritten += this.Data.Length; | ||
|
||
// Write the Magic Trailer | ||
buffer[bytesWritten++] = 0x01; | ||
for (byte i = 255; i > 0; i--) | ||
{ | ||
buffer[bytesWritten++] = i; | ||
} | ||
|
||
buffer[bytesWritten++] = 0x00; | ||
|
||
return totalSize; | ||
} | ||
} | ||
} |
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
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.