-
-
Notifications
You must be signed in to change notification settings - Fork 852
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1918 from ynse01/read-xmp-from-webp
Support for XMP metadata
- Loading branch information
Showing
36 changed files
with
1,032 additions
and
145 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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[]>(); | ||
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 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 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.