Browse Source

Optimize LzwEncoder

af/merge-core
Jason Nelson 8 years ago
parent
commit
370bb68096
  1. 33
      src/ImageSharp/Formats/Gif/LzwEncoder.cs

33
src/ImageSharp/Formats/Gif/LzwEncoder.cs

@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
}; };
/// <summary> /// <summary>
/// The working pixel array /// The working pixel array.
/// </summary> /// </summary>
private readonly byte[] pixelArray; private readonly byte[] pixelArray;
@ -99,7 +99,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
/// <summary> /// <summary>
/// The current pixel /// The current pixel
/// </summary> /// </summary>
private int currentPixel; private int position;
/// <summary> /// <summary>
/// Number of bits/code /// Number of bits/code
@ -212,7 +212,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
// Write "initial code size" byte // Write "initial code size" byte
stream.WriteByte((byte)this.initialCodeSize); stream.WriteByte((byte)this.initialCodeSize);
this.currentPixel = 0; this.position = 0;
// Compress and write the pixel data // Compress and write the pixel data
this.Compress(this.initialCodeSize + 1, stream); this.Compress(this.initialCodeSize + 1, stream);
@ -221,13 +221,6 @@ namespace SixLabors.ImageSharp.Formats.Gif
stream.WriteByte(GifConstants.Terminator); stream.WriteByte(GifConstants.Terminator);
} }
/// <inheritdoc />
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
this.Dispose(true);
}
/// <summary> /// <summary>
/// Gets the maximum code value /// Gets the maximum code value
/// </summary> /// </summary>
@ -326,8 +319,10 @@ namespace SixLabors.ImageSharp.Formats.Gif
ref int hashTableRef = ref MemoryMarshal.GetReference(this.hashTable.Span); ref int hashTableRef = ref MemoryMarshal.GetReference(this.hashTable.Span);
ref int codeTableRef = ref MemoryMarshal.GetReference(this.codeTable.Span); ref int codeTableRef = ref MemoryMarshal.GetReference(this.codeTable.Span);
while ((c = this.NextPixel()) != Eof) while (this.position < this.pixelArray.Length)
{ {
c = this.NextPixel();
fcode = (c << this.maxbits) + ent; fcode = (c << this.maxbits) + ent;
int i = (c << hshift) ^ ent /* = 0 */; int i = (c << hshift) ^ ent /* = 0 */;
@ -404,15 +399,10 @@ namespace SixLabors.ImageSharp.Formats.Gif
/// <returns> /// <returns>
/// The <see cref="int"/> /// The <see cref="int"/>
/// </returns> /// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int NextPixel() private int NextPixel()
{ {
if (this.currentPixel == this.pixelArray.Length) return this.pixelArray[this.position++] & 0xff;
{
return Eof;
}
this.currentPixel++;
return this.pixelArray[this.currentPixel - 1] & 0xff;
} }
/// <summary> /// <summary>
@ -478,6 +468,13 @@ namespace SixLabors.ImageSharp.Formats.Gif
} }
} }
/// <inheritdoc />
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
this.Dispose(true);
}
/// <summary> /// <summary>
/// Disposes the object and frees resources for the Garbage Collector. /// Disposes the object and frees resources for the Garbage Collector.
/// </summary> /// </summary>

Loading…
Cancel
Save