Browse Source

Remove unnecessary throw and optimize write.

pull/2014/head
James Jackson-South 4 years ago
parent
commit
c64078cc17
  1. 23
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  2. 6
      src/ImageSharp/Formats/Gif/Sections/GifGraphicControlExtension.cs
  3. 2
      src/ImageSharp/Formats/Gif/Sections/GifNetscapeLoopingApplicationExtension.cs
  4. 13
      src/ImageSharp/Formats/Gif/Sections/GifXmpApplicationExtension.cs

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

@ -428,26 +428,31 @@ namespace SixLabors.ImageSharp.Formats.Gif
where TGifExtension : struct, IGifExtension
{
IMemoryOwner<byte> owner = null;
Span<byte> buffer;
Span<byte> extensionBuffer;
int extensionSize = extension.ContentLength;
if (extensionSize > this.buffer.Length - 3)
if (extensionSize == 0)
{
return;
}
else if (extensionSize > this.buffer.Length - 3)
{
owner = this.memoryAllocator.Allocate<byte>(extensionSize + 3);
buffer = owner.GetSpan();
extensionBuffer = owner.GetSpan();
}
else
{
buffer = this.buffer;
extensionBuffer = this.buffer;
}
buffer[0] = GifConstants.ExtensionIntroducer;
buffer[1] = extension.Label;
extensionBuffer[0] = GifConstants.ExtensionIntroducer;
extensionBuffer[1] = extension.Label;
extension.WriteTo(buffer.Slice(2));
extension.WriteTo(extensionBuffer.Slice(2));
buffer[extensionSize + 2] = GifConstants.Terminator;
extensionBuffer[extensionSize + 2] = GifConstants.Terminator;
stream.Write(buffer, 0, extensionSize + 3);
stream.Write(extensionBuffer, 0, extensionSize + 3);
owner?.Dispose();
}

6
src/ImageSharp/Formats/Gif/Sections/GifGraphicControlExtension.cs

@ -71,13 +71,11 @@ namespace SixLabors.ImageSharp.Formats.Gif
dest = this;
return 5;
return ((IGifExtension)this).ContentLength;
}
public static GifGraphicControlExtension Parse(ReadOnlySpan<byte> buffer)
{
return MemoryMarshal.Cast<byte, GifGraphicControlExtension>(buffer)[0];
}
=> MemoryMarshal.Cast<byte, GifGraphicControlExtension>(buffer)[0];
public static byte GetPackedValue(GifDisposalMethod disposalMethod, bool userInputFlag = false, bool transparencyFlag = false)
{

2
src/ImageSharp/Formats/Gif/Sections/GifNetscapeLoopingApplicationExtension.cs

@ -40,7 +40,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
// 0 means loop indefinitely. Count is set as play n + 1 times.
BinaryPrimitives.WriteUInt16LittleEndian(buffer.Slice(14, 2), this.RepeatCount);
return 16; // Length - Introducer + Label + Terminator.
return this.ContentLength; // Length - Introducer + Label + Terminator.
}
}
}

13
src/ImageSharp/Formats/Gif/Sections/GifXmpApplicationExtension.cs

@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
// size : 1
// identifier : 11
// magic trailer : 257
public int ContentLength => this.Data.Length + 269;
public int ContentLength => (this.Data.Length > 0) ? this.Data.Length + 269 : 0;
/// <summary>
/// Gets the raw Data.
@ -50,12 +50,6 @@ namespace SixLabors.ImageSharp.Formats.Gif
public int WriteTo(Span<byte> buffer)
{
int totalSize = this.ContentLength;
if (buffer.Length < totalSize)
{
ThrowInsufficientMemory();
}
int bytesWritten = 0;
buffer[bytesWritten++] = GifConstants.ApplicationBlockSize;
@ -77,7 +71,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
buffer[bytesWritten++] = 0x00;
return totalSize;
return this.ContentLength;
}
private static byte[] ReadXmpData(Stream stream, MemoryAllocator allocator)
@ -100,8 +94,5 @@ namespace SixLabors.ImageSharp.Formats.Gif
bytes.WriteByte((byte)b);
}
}
private static void ThrowInsufficientMemory()
=> throw new InsufficientMemoryException("Unable to write XMP metadata to GIF image");
}
}

Loading…
Cancel
Save