|
|
@ -64,17 +64,22 @@ namespace SixLabors.ImageSharp.Formats.Gif |
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Decodes and decompresses all pixel indices from the stream.
|
|
|
/// Decodes and decompresses all pixel indices from the stream.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
/// <param name="dataSize">Size of the data.</param>
|
|
|
/// <param name="minCodeSize">Minimum code size of the data.</param>
|
|
|
/// <param name="pixels">The pixel array to decode to.</param>
|
|
|
/// <param name="pixels">The pixel array to decode to.</param>
|
|
|
public void DecodePixels(int dataSize, Buffer2D<byte> pixels) |
|
|
public void DecodePixels(int minCodeSize, Buffer2D<byte> pixels) |
|
|
{ |
|
|
{ |
|
|
// Calculate the clear code. The value of the clear code is 2 ^ dataSize
|
|
|
// Calculate the clear code. The value of the clear code is 2 ^ minCodeSize
|
|
|
int clearCode = 1 << dataSize; |
|
|
int clearCode = 1 << minCodeSize; |
|
|
if (clearCode > MaxStackSize) |
|
|
|
|
|
|
|
|
// It is possible to specify a larger LZW minimum code size than the palette length in bits
|
|
|
|
|
|
// which may leave a gap in the codes where no colors are assigned.
|
|
|
|
|
|
// http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp#lzw_compression
|
|
|
|
|
|
if (minCodeSize < 2 || clearCode > MaxStackSize) |
|
|
{ |
|
|
{ |
|
|
// Don't attempt to decode the frame indices.
|
|
|
// Don't attempt to decode the frame indices.
|
|
|
// The image is most likely corrupted.
|
|
|
// Theoretically we could determine a min code size from the length of the provided
|
|
|
return; |
|
|
// color palette but we won't bother since the image is most likely corrupted.
|
|
|
|
|
|
ThrowBadMinimumCode(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// The resulting index table length.
|
|
|
// The resulting index table length.
|
|
|
@ -82,7 +87,7 @@ namespace SixLabors.ImageSharp.Formats.Gif |
|
|
int height = pixels.Height; |
|
|
int height = pixels.Height; |
|
|
int length = width * height; |
|
|
int length = width * height; |
|
|
|
|
|
|
|
|
int codeSize = dataSize + 1; |
|
|
int codeSize = minCodeSize + 1; |
|
|
|
|
|
|
|
|
// Calculate the end code
|
|
|
// Calculate the end code
|
|
|
int endCode = clearCode + 1; |
|
|
int endCode = clearCode + 1; |
|
|
@ -169,7 +174,7 @@ namespace SixLabors.ImageSharp.Formats.Gif |
|
|
if (code == clearCode) |
|
|
if (code == clearCode) |
|
|
{ |
|
|
{ |
|
|
// Reset the decoder
|
|
|
// Reset the decoder
|
|
|
codeSize = dataSize + 1; |
|
|
codeSize = minCodeSize + 1; |
|
|
codeMask = (1 << codeSize) - 1; |
|
|
codeMask = (1 << codeSize) - 1; |
|
|
availableCode = clearCode + 2; |
|
|
availableCode = clearCode + 2; |
|
|
oldCode = NullCode; |
|
|
oldCode = NullCode; |
|
|
@ -258,5 +263,8 @@ namespace SixLabors.ImageSharp.Formats.Gif |
|
|
this.suffix.Dispose(); |
|
|
this.suffix.Dispose(); |
|
|
this.pixelStack.Dispose(); |
|
|
this.pixelStack.Dispose(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(InliningOptions.ColdPath)] |
|
|
|
|
|
public static void ThrowBadMinimumCode() => throw new InvalidImageContentException("Gif Image does not contain a valid LZW minimum code."); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|