From 49d41fff88c313e6b6ff5696592fc0efbfbd80ba Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 18 Apr 2018 16:39:24 -0700 Subject: [PATCH] Add TryGetProperty to ImageMetadata and remove Linq call from GifEncoder --- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 18 +++++++-------- .../Formats/Gif/IGifEncoderOptions.cs | 2 +- src/ImageSharp/MetaData/ImageMetaData.cs | 23 +++++++++++++++++++ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 9c0ae9821..14bfa6fd0 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -4,7 +4,6 @@ using System; using System.Buffers.Binary; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -23,7 +22,7 @@ namespace SixLabors.ImageSharp.Formats.Gif private readonly MemoryManager memoryManager; /// - /// The temp buffer used to reduce allocations. + /// A reusable buffer used to reduce allocations. /// private readonly byte[] buffer = new byte[20]; @@ -129,7 +128,8 @@ namespace SixLabors.ImageSharp.Formats.Gif { // Transparent pixels are much more likely to be found at the end of a palette int index = -1; - var trans = default(Rgba32); + Rgba32 trans = default; + ref TPixel paletteRef = ref MemoryMarshal.GetReference(quantized.Palette.AsSpan()); for (int i = quantized.Palette.Length - 1; i >= 0; i--) { @@ -221,8 +221,7 @@ namespace SixLabors.ImageSharp.Formats.Gif return; } - ImageProperty property = metadata.Properties.FirstOrDefault(p => p.Name == GifConstants.Comments); - if (property == null || string.IsNullOrEmpty(property.Value)) + if (!metadata.TryGetProperty(GifConstants.Comments, out ImageProperty property) || string.IsNullOrEmpty(property.Value)) { return; } @@ -313,17 +312,15 @@ namespace SixLabors.ImageSharp.Formats.Gif private void WriteColorTable(QuantizedFrame image, Stream stream) where TPixel : struct, IPixel { - // Grab the palette and write it to the stream. int pixelCount = image.Palette.Length; - // Get max colors for bit depth. - int colorTableLength = (int)Math.Pow(2, this.bitDepth) * 3; - var rgb = default(Rgb24); + int colorTableLength = (int)Math.Pow(2, this.bitDepth) * 3; // The maximium number of colors for the bit depth + Rgb24 rgb = default; + using (IManagedByteBuffer colorTable = this.memoryManager.AllocateManagedByteBuffer(colorTableLength)) { ref TPixel paletteRef = ref MemoryMarshal.GetReference(image.Palette.AsSpan()); ref Rgb24 rgb24Ref = ref Unsafe.As(ref MemoryMarshal.GetReference(colorTable.Span)); - for (int i = 0; i < pixelCount; i++) { ref TPixel entry = ref Unsafe.Add(ref paletteRef, i); @@ -331,6 +328,7 @@ namespace SixLabors.ImageSharp.Formats.Gif Unsafe.Add(ref rgb24Ref, i) = rgb; } + // Write the palette to the stream stream.Write(colorTable.Array, 0, colorTableLength); } } diff --git a/src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs b/src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs index 1f1875789..f7bc5f4ed 100644 --- a/src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs +++ b/src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs @@ -7,7 +7,7 @@ using SixLabors.ImageSharp.Processing.Quantization; namespace SixLabors.ImageSharp.Formats.Gif { /// - /// The configuration options used for encoding gifs + /// The configuration options used for encoding gifs. /// internal interface IGifEncoderOptions { diff --git a/src/ImageSharp/MetaData/ImageMetaData.cs b/src/ImageSharp/MetaData/ImageMetaData.cs index 01b53a3fd..af3cc5f5f 100644 --- a/src/ImageSharp/MetaData/ImageMetaData.cs +++ b/src/ImageSharp/MetaData/ImageMetaData.cs @@ -120,6 +120,29 @@ namespace SixLabors.ImageSharp.MetaData /// public ushort RepeatCount { get; set; } + /// + /// Looks up a property with the provided name. + /// + /// The name of the property to lookup. + /// The property, if found, with the provided name. + /// Whether the property was found. + internal bool TryGetProperty(string name, out ImageProperty result) + { + foreach (ImageProperty property in this.Properties) + { + if (property.Name == name) + { + result = property; + + return true; + } + } + + result = default; + + return false; + } + /// /// Clones this into a new instance ///