Browse Source

Add TryGetProperty to ImageMetadata and remove Linq call from GifEncoder

af/merge-core
Jason Nelson 8 years ago
parent
commit
49d41fff88
  1. 18
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  2. 2
      src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs
  3. 23
      src/ImageSharp/MetaData/ImageMetaData.cs

18
src/ImageSharp/Formats/Gif/GifEncoderCore.cs

@ -4,7 +4,6 @@
using System; using System;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
@ -23,7 +22,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
private readonly MemoryManager memoryManager; private readonly MemoryManager memoryManager;
/// <summary> /// <summary>
/// The temp buffer used to reduce allocations. /// A reusable buffer used to reduce allocations.
/// </summary> /// </summary>
private readonly byte[] buffer = new byte[20]; 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 // Transparent pixels are much more likely to be found at the end of a palette
int index = -1; int index = -1;
var trans = default(Rgba32); Rgba32 trans = default;
ref TPixel paletteRef = ref MemoryMarshal.GetReference(quantized.Palette.AsSpan()); ref TPixel paletteRef = ref MemoryMarshal.GetReference(quantized.Palette.AsSpan());
for (int i = quantized.Palette.Length - 1; i >= 0; i--) for (int i = quantized.Palette.Length - 1; i >= 0; i--)
{ {
@ -221,8 +221,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
return; return;
} }
ImageProperty property = metadata.Properties.FirstOrDefault(p => p.Name == GifConstants.Comments); if (!metadata.TryGetProperty(GifConstants.Comments, out ImageProperty property) || string.IsNullOrEmpty(property.Value))
if (property == null || string.IsNullOrEmpty(property.Value))
{ {
return; return;
} }
@ -313,17 +312,15 @@ namespace SixLabors.ImageSharp.Formats.Gif
private void WriteColorTable<TPixel>(QuantizedFrame<TPixel> image, Stream stream) private void WriteColorTable<TPixel>(QuantizedFrame<TPixel> image, Stream stream)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
// Grab the palette and write it to the stream.
int pixelCount = image.Palette.Length; int pixelCount = image.Palette.Length;
// Get max colors for bit depth. int colorTableLength = (int)Math.Pow(2, this.bitDepth) * 3; // The maximium number of colors for the bit depth
int colorTableLength = (int)Math.Pow(2, this.bitDepth) * 3; Rgb24 rgb = default;
var rgb = default(Rgb24);
using (IManagedByteBuffer colorTable = this.memoryManager.AllocateManagedByteBuffer(colorTableLength)) using (IManagedByteBuffer colorTable = this.memoryManager.AllocateManagedByteBuffer(colorTableLength))
{ {
ref TPixel paletteRef = ref MemoryMarshal.GetReference(image.Palette.AsSpan()); ref TPixel paletteRef = ref MemoryMarshal.GetReference(image.Palette.AsSpan());
ref Rgb24 rgb24Ref = ref Unsafe.As<byte, Rgb24>(ref MemoryMarshal.GetReference(colorTable.Span)); ref Rgb24 rgb24Ref = ref Unsafe.As<byte, Rgb24>(ref MemoryMarshal.GetReference(colorTable.Span));
for (int i = 0; i < pixelCount; i++) for (int i = 0; i < pixelCount; i++)
{ {
ref TPixel entry = ref Unsafe.Add(ref paletteRef, 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; Unsafe.Add(ref rgb24Ref, i) = rgb;
} }
// Write the palette to the stream
stream.Write(colorTable.Array, 0, colorTableLength); stream.Write(colorTable.Array, 0, colorTableLength);
} }
} }

2
src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs

@ -7,7 +7,7 @@ using SixLabors.ImageSharp.Processing.Quantization;
namespace SixLabors.ImageSharp.Formats.Gif namespace SixLabors.ImageSharp.Formats.Gif
{ {
/// <summary> /// <summary>
/// The configuration options used for encoding gifs /// The configuration options used for encoding gifs.
/// </summary> /// </summary>
internal interface IGifEncoderOptions internal interface IGifEncoderOptions
{ {

23
src/ImageSharp/MetaData/ImageMetaData.cs

@ -120,6 +120,29 @@ namespace SixLabors.ImageSharp.MetaData
/// </summary> /// </summary>
public ushort RepeatCount { get; set; } public ushort RepeatCount { get; set; }
/// <summary>
/// Looks up a property with the provided name.
/// </summary>
/// <param name="name">The name of the property to lookup.</param>
/// <param name="result">The property, if found, with the provided name.</param>
/// <returns>Whether the property was found.</returns>
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;
}
/// <summary> /// <summary>
/// Clones this into a new instance /// Clones this into a new instance
/// </summary> /// </summary>

Loading…
Cancel
Save