-
-
Notifications
You must be signed in to change notification settings - Fork 895
Preserve color profile when encoding PNG images #2110
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
1c474c7
9b5d56f
d645ba4
35d1473
cc9c167
8176d4b
b025d29
2dd3598
d43ec49
a0e38c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| using SixLabors.ImageSharp.Memory; | ||
| using SixLabors.ImageSharp.Metadata; | ||
| using SixLabors.ImageSharp.Metadata.Profiles.Exif; | ||
| using SixLabors.ImageSharp.Metadata.Profiles.Icc; | ||
| using SixLabors.ImageSharp.Metadata.Profiles.Xmp; | ||
| using SixLabors.ImageSharp.PixelFormats; | ||
|
|
||
|
|
@@ -205,6 +206,9 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken | |
| this.MergeOrSetExifProfile(metadata, new ExifProfile(exifData), replaceExistingKeys: true); | ||
| } | ||
|
|
||
| break; | ||
| case PngChunkType.EmbeddedColorProfile: | ||
| this.ReadColorProfileChunk(metadata, chunk.Data.GetSpan()); | ||
| break; | ||
| case PngChunkType.End: | ||
| goto EOF; | ||
|
|
@@ -1174,6 +1178,58 @@ private bool TryReadLegacyExifTextChunk(ImageMetadata metadata, string data) | |
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reads the color profile chunk. The data is stored similar to the zTXt chunk. | ||
| /// </summary> | ||
| /// <param name="metadata">The metadata.</param> | ||
| /// <param name="data">The bytes containing the profile.</param> | ||
| private void ReadColorProfileChunk(ImageMetadata metadata, ReadOnlySpan<byte> data) | ||
| { | ||
| int zeroIndex = data.IndexOf((byte)0); | ||
| if (zeroIndex < PngConstants.MinTextKeywordLength || zeroIndex > PngConstants.MaxTextKeywordLength) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| byte compressionMethod = data[zeroIndex + 1]; | ||
| if (compressionMethod != 0) | ||
| { | ||
| // Only compression method 0 is supported (zlib datastream with deflate compression). | ||
| return; | ||
| } | ||
|
|
||
| ReadOnlySpan<byte> keywordBytes = data.Slice(0, zeroIndex); | ||
| if (!this.TryReadTextKeyword(keywordBytes, out string name)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| ReadOnlySpan<byte> compressedData = data.Slice(zeroIndex + 2); | ||
|
|
||
| using (var memoryStream = new MemoryStream(compressedData.ToArray())) | ||
| using (var bufferedStream = new BufferedReadStream(this.Configuration, memoryStream)) | ||
| using (var inflateStream = new ZlibInflateStream(bufferedStream)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can avoid the allocation here. |
||
| { | ||
| if (!inflateStream.AllocateNewBytes(compressedData.Length, false)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var uncompressedBytes = new List<byte>(); | ||
|
|
||
| // Note: this uses a buffer which is only 4 bytes long to read the stream, maybe allocating a larger buffer makes sense here. | ||
| int bytesRead = inflateStream.CompressedStream.Read(this.buffer, 0, this.buffer.Length); | ||
| while (bytesRead != 0) | ||
| { | ||
| uncompressedBytes.AddRange(this.buffer.AsSpan(0, bytesRead).ToArray()); | ||
| bytesRead = inflateStream.CompressedStream.Read(this.buffer, 0, this.buffer.Length); | ||
| } | ||
|
|
||
| byte[] iccpProfileBytes = uncompressedBytes.ToArray(); | ||
| metadata.IccProfile = new IccProfile(iccpProfileBytes); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compares two ReadOnlySpan<char>s in a case-insensitive method. | ||
| /// This is only needed because older frameworks are missing the extension method. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,11 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable | |
| /// </summary> | ||
| private IMemoryOwner<byte> currentScanline; | ||
|
|
||
| /// <summary> | ||
| /// The color profile name. | ||
| /// </summary> | ||
| private const string ColorProfileName = "ICC Profile"; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PngEncoderCore" /> class. | ||
| /// </summary> | ||
|
|
@@ -134,6 +139,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken | |
|
|
||
| this.WriteHeaderChunk(stream); | ||
| this.WriteGammaChunk(stream); | ||
| this.WriteColorProfileChunk(stream, metadata); | ||
| this.WritePaletteChunk(stream, quantized); | ||
| this.WriteTransparencyChunk(stream, pngMetadata); | ||
| this.WritePhysicalChunk(stream, metadata); | ||
|
|
@@ -656,7 +662,7 @@ private void WriteExifChunk(Stream stream, ImageMetadata meta) | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes an iTXT chunk, containing the XMP metdata to the stream, if such profile is present in the metadata. | ||
| /// Writes an iTXT chunk, containing the XMP metadata to the stream, if such profile is present in the metadata. | ||
| /// </summary> | ||
| /// <param name="stream">The <see cref="Stream"/> containing image data.</param> | ||
| /// <param name="meta">The image metadata.</param> | ||
|
|
@@ -673,7 +679,7 @@ private void WriteXmpChunk(Stream stream, ImageMetadata meta) | |
| return; | ||
| } | ||
|
|
||
| var xmpData = meta.XmpProfile.Data; | ||
| byte[] xmpData = meta.XmpProfile.Data; | ||
|
|
||
| if (xmpData.Length == 0) | ||
| { | ||
|
|
@@ -687,19 +693,47 @@ private void WriteXmpChunk(Stream stream, ImageMetadata meta) | |
| PngConstants.XmpKeyword.CopyTo(payload); | ||
| int bytesWritten = PngConstants.XmpKeyword.Length; | ||
|
|
||
| // Write the iTxt header (all zeros in this case) | ||
| // Write the iTxt header (all zeros in this case). | ||
| payload[bytesWritten++] = 0; | ||
| payload[bytesWritten++] = 0; | ||
| payload[bytesWritten++] = 0; | ||
| payload[bytesWritten++] = 0; | ||
| payload[bytesWritten++] = 0; | ||
|
brianpopow marked this conversation as resolved.
Outdated
|
||
|
|
||
| // And the XMP data itself | ||
| // And the XMP data itself. | ||
| xmpData.CopyTo(payload.Slice(bytesWritten)); | ||
| this.WriteChunk(stream, PngChunkType.InternationalText, payload); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes the color profile chunk. | ||
| /// </summary> | ||
| /// <param name="stream">The stream to write to.</param> | ||
| /// <param name="metaData">The image meta data.</param> | ||
| private void WriteColorProfileChunk(Stream stream, ImageMetadata metaData) | ||
| { | ||
| if (metaData.IccProfile is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| byte[] iccProfileBytes = metaData.IccProfile.ToByteArray(); | ||
|
|
||
| byte[] compressedData = this.GetZlibCompressedBytes(iccProfileBytes); | ||
| int payloadLength = ColorProfileName.Length + compressedData.Length + 2; | ||
| using (IMemoryOwner<byte> owner = this.memoryAllocator.Allocate<byte>(payloadLength)) | ||
| { | ||
| Span<byte> outputBytes = owner.GetSpan(); | ||
| PngConstants.Encoding.GetBytes(ColorProfileName).CopyTo(outputBytes); | ||
| int bytesWritten = ColorProfileName.Length; | ||
| outputBytes[bytesWritten++] = 0; // Null separator. | ||
| outputBytes[bytesWritten++] = 0; // Compression. | ||
|
Comment on lines
+732
to
+733
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here the above "trick" doesn't make sense, as for slicing there's an argument validation, so we just would exchange one comparison for the bound-check against the argument validation. So no net-win. |
||
| compressedData.CopyTo(outputBytes.Slice(bytesWritten)); | ||
| this.WriteChunk(stream, PngChunkType.EmbeddedColorProfile, outputBytes); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes a text chunk to the stream. Can be either a tTXt, iTXt or zTXt chunk, | ||
| /// depending whether the text contains any latin characters or should be compressed. | ||
|
|
@@ -727,13 +761,12 @@ private void WriteTextChunks(Stream stream, PngMetadata meta) | |
| } | ||
| } | ||
|
|
||
| if (hasUnicodeCharacters || (!string.IsNullOrWhiteSpace(textData.LanguageTag) || | ||
| !string.IsNullOrWhiteSpace(textData.TranslatedKeyword))) | ||
| if (hasUnicodeCharacters || (!string.IsNullOrWhiteSpace(textData.LanguageTag) || !string.IsNullOrWhiteSpace(textData.TranslatedKeyword))) | ||
| { | ||
| // Write iTXt chunk. | ||
| byte[] keywordBytes = PngConstants.Encoding.GetBytes(textData.Keyword); | ||
| byte[] textBytes = textData.Value.Length > this.options.TextCompressionThreshold | ||
| ? this.GetCompressedTextBytes(PngConstants.TranslatedEncoding.GetBytes(textData.Value)) | ||
| ? this.GetZlibCompressedBytes(PngConstants.TranslatedEncoding.GetBytes(textData.Value)) | ||
| : PngConstants.TranslatedEncoding.GetBytes(textData.Value); | ||
|
|
||
| byte[] translatedKeyword = PngConstants.TranslatedEncoding.GetBytes(textData.TranslatedKeyword); | ||
|
|
@@ -772,18 +805,17 @@ private void WriteTextChunks(Stream stream, PngMetadata meta) | |
| if (textData.Value.Length > this.options.TextCompressionThreshold) | ||
| { | ||
| // Write zTXt chunk. | ||
| byte[] compressedData = | ||
| this.GetCompressedTextBytes(PngConstants.Encoding.GetBytes(textData.Value)); | ||
| byte[] compressedData = this.GetZlibCompressedBytes(PngConstants.Encoding.GetBytes(textData.Value)); | ||
| int payloadLength = textData.Keyword.Length + compressedData.Length + 2; | ||
| using (IMemoryOwner<byte> owner = this.memoryAllocator.Allocate<byte>(payloadLength)) | ||
| { | ||
| Span<byte> outputBytes = owner.GetSpan(); | ||
| PngConstants.Encoding.GetBytes(textData.Keyword).CopyTo(outputBytes); | ||
| int bytesWritten = textData.Keyword.Length; | ||
| outputBytes[bytesWritten++] = 0; | ||
| outputBytes[bytesWritten++] = 0; | ||
| outputBytes[bytesWritten++] = 0; // Null separator. | ||
| outputBytes[bytesWritten++] = 0; // Compression. | ||
| compressedData.CopyTo(outputBytes.Slice(bytesWritten)); | ||
| this.WriteChunk(stream, PngChunkType.CompressedText, outputBytes.ToArray()); | ||
| this.WriteChunk(stream, PngChunkType.CompressedText, outputBytes); | ||
| } | ||
| } | ||
| else | ||
|
|
@@ -796,9 +828,8 @@ private void WriteTextChunks(Stream stream, PngMetadata meta) | |
| PngConstants.Encoding.GetBytes(textData.Keyword).CopyTo(outputBytes); | ||
| int bytesWritten = textData.Keyword.Length; | ||
| outputBytes[bytesWritten++] = 0; | ||
| PngConstants.Encoding.GetBytes(textData.Value) | ||
| .CopyTo(outputBytes.Slice(bytesWritten)); | ||
| this.WriteChunk(stream, PngChunkType.Text, outputBytes.ToArray()); | ||
| PngConstants.Encoding.GetBytes(textData.Value).CopyTo(outputBytes.Slice(bytesWritten)); | ||
| this.WriteChunk(stream, PngChunkType.Text, outputBytes); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -808,15 +839,15 @@ private void WriteTextChunks(Stream stream, PngMetadata meta) | |
| /// <summary> | ||
| /// Compresses a given text using Zlib compression. | ||
| /// </summary> | ||
| /// <param name="textBytes">The text bytes to compress.</param> | ||
| /// <returns>The compressed text byte array.</returns> | ||
| private byte[] GetCompressedTextBytes(byte[] textBytes) | ||
| /// <param name="dataBytes">The bytes to compress.</param> | ||
| /// <returns>The compressed byte array.</returns> | ||
| private byte[] GetZlibCompressedBytes(byte[] dataBytes) | ||
| { | ||
| using (var memoryStream = new MemoryStream()) | ||
| { | ||
| using (var deflateStream = new ZlibDeflateStream(this.memoryAllocator, memoryStream, this.options.CompressionLevel)) | ||
| { | ||
| deflateStream.Write(textBytes); | ||
| deflateStream.Write(dataBytes); | ||
| } | ||
|
|
||
| return memoryStream.ToArray(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.