Browse Source

Better transparency destection

af/merge-core
James Jackson-South 10 years ago
parent
commit
4dbe394c9d
  1. 25
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs

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

@ -9,10 +9,9 @@ namespace ImageSharp.Formats
using System.Buffers; using System.Buffers;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Numerics;
using IO; using IO;
using Quantizers; using Quantizers;
/// <summary> /// <summary>
@ -129,10 +128,15 @@ namespace ImageSharp.Formats
float alpha = 1; float alpha = 1;
for (int i = 0; i < quantized.Palette.Length; i++) for (int i = 0; i < quantized.Palette.Length; i++)
{ {
float a = quantized.Palette[i].ToVector4().W; Vector4 vector = quantized.Palette[i].ToVector4();
if (a < alpha) if (vector == Vector4.Zero)
{
return i;
}
if (vector.W < alpha)
{ {
alpha = a; alpha = vector.W;
index = i; index = i;
} }
} }
@ -157,10 +161,9 @@ namespace ImageSharp.Formats
/// <param name="image">The image to encode.</param> /// <param name="image">The image to encode.</param>
/// <param name="writer">The writer to write to the stream with.</param> /// <param name="writer">The writer to write to the stream with.</param>
/// <param name="tranparencyIndex">The transparency index to set the default background index to.</param> /// <param name="tranparencyIndex">The transparency index to set the default background index to.</param>
private void WriteLogicalScreenDescriptor<TColor, TPacked>( private void WriteLogicalScreenDescriptor<TColor, TPacked>(Image<TColor, TPacked> image, EndianBinaryWriter writer, int tranparencyIndex)
Image<TColor, TPacked> image, where TColor : struct, IPackedPixel<TPacked>
EndianBinaryWriter writer, where TPacked : struct
int tranparencyIndex) where TColor : struct, IPackedPixel<TPacked> where TPacked : struct
{ {
GifLogicalScreenDescriptor descriptor = new GifLogicalScreenDescriptor GifLogicalScreenDescriptor descriptor = new GifLogicalScreenDescriptor
{ {
@ -238,8 +241,8 @@ namespace ImageSharp.Formats
// TODO: Check transparency logic. // TODO: Check transparency logic.
bool hasTransparent = transparencyIndex > -1; bool hasTransparent = transparencyIndex > -1;
DisposalMethod disposalMethod = hasTransparent DisposalMethod disposalMethod = hasTransparent
? DisposalMethod.RestoreToBackground ? DisposalMethod.RestoreToBackground
: DisposalMethod.Unspecified; : DisposalMethod.Unspecified;
GifGraphicsControlExtension extension = new GifGraphicsControlExtension() GifGraphicsControlExtension extension = new GifGraphicsControlExtension()
{ {

Loading…
Cancel
Save