Browse Source

Reuse the same buffer.

pull/30/head
dirk 10 years ago
committed by Dirk Lemstra
parent
commit
9e6ab2f1ff
  1. 37
      src/ImageSharp/Formats/Gif/LzwDecoder.cs

37
src/ImageSharp/Formats/Gif/LzwDecoder.cs

@ -90,7 +90,7 @@ namespace ImageSharp.Formats
suffix[code] = (byte)code; suffix[code] = (byte)code;
} }
byte[] buffer = null; byte[] buffer = new byte[255];
while (xyz < pixels.Length) while (xyz < pixels.Length)
{ {
if (top == 0) if (top == 0)
@ -101,8 +101,7 @@ namespace ImageSharp.Formats
if (count == 0) if (count == 0)
{ {
// Read a new data block. // Read a new data block.
buffer = this.ReadBlock(); count = this.ReadBlock(buffer);
count = buffer.Length;
if (count == 0) if (count == 0)
{ {
break; break;
@ -111,10 +110,7 @@ namespace ImageSharp.Formats
bi = 0; bi = 0;
} }
if (buffer != null) data += buffer[bi] << bits;
{
data += buffer[bi] << bits;
}
bits += 8; bits += 8;
bi++; bi++;
@ -200,29 +196,20 @@ namespace ImageSharp.Formats
/// Reads the next data block from the stream. A data block begins with a byte, /// Reads the next data block from the stream. A data block begins with a byte,
/// which defines the size of the block, followed by the block itself. /// which defines the size of the block, followed by the block itself.
/// </summary> /// </summary>
/// <param name="buffer">The buffer to store the block in.</param>
/// <returns> /// <returns>
/// The <see cref="T:byte[]"/>. /// The <see cref="T:byte[]"/>.
/// </returns> /// </returns>
private byte[] ReadBlock() private int ReadBlock(byte[] buffer)
{ {
int blockSize = this.stream.ReadByte(); int bufferSize = this.stream.ReadByte();
return this.ReadBytes(blockSize); if (bufferSize < 1)
} {
return 0;
}
/// <summary> int count = this.stream.Read(buffer, 0, bufferSize);
/// Reads the specified number of bytes from the data stream. return count != bufferSize ? 0 : bufferSize;
/// </summary>
/// <param name="length">
/// The number of bytes to read.
/// </param>
/// <returns>
/// The <see cref="T:byte[]"/>.
/// </returns>
private byte[] ReadBytes(int length)
{
byte[] buffer = new byte[length];
this.stream.Read(buffer, 0, length);
return buffer;
} }
} }
} }

Loading…
Cancel
Save