From 4dbe394c9da05f9a4acaebbd7be45a8419d1ddcd Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 16 Dec 2016 01:36:14 +1100 Subject: [PATCH] Better transparency destection --- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 25 +++++++++++--------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 4eb5629e5..15f76f5be 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -9,10 +9,9 @@ namespace ImageSharp.Formats using System.Buffers; using System.IO; using System.Linq; - using System.Threading.Tasks; + using System.Numerics; using IO; - using Quantizers; /// @@ -129,10 +128,15 @@ namespace ImageSharp.Formats float alpha = 1; for (int i = 0; i < quantized.Palette.Length; i++) { - float a = quantized.Palette[i].ToVector4().W; - if (a < alpha) + Vector4 vector = quantized.Palette[i].ToVector4(); + if (vector == Vector4.Zero) + { + return i; + } + + if (vector.W < alpha) { - alpha = a; + alpha = vector.W; index = i; } } @@ -157,10 +161,9 @@ namespace ImageSharp.Formats /// The image to encode. /// The writer to write to the stream with. /// The transparency index to set the default background index to. - private void WriteLogicalScreenDescriptor( - Image image, - EndianBinaryWriter writer, - int tranparencyIndex) where TColor : struct, IPackedPixel where TPacked : struct + private void WriteLogicalScreenDescriptor(Image image, EndianBinaryWriter writer, int tranparencyIndex) + where TColor : struct, IPackedPixel + where TPacked : struct { GifLogicalScreenDescriptor descriptor = new GifLogicalScreenDescriptor { @@ -238,8 +241,8 @@ namespace ImageSharp.Formats // TODO: Check transparency logic. bool hasTransparent = transparencyIndex > -1; DisposalMethod disposalMethod = hasTransparent - ? DisposalMethod.RestoreToBackground - : DisposalMethod.Unspecified; + ? DisposalMethod.RestoreToBackground + : DisposalMethod.Unspecified; GifGraphicsControlExtension extension = new GifGraphicsControlExtension() {