Browse Source

BitReader now uses stream extension to read the data from the stream

pull/1552/head
Brian Popow 6 years ago
parent
commit
71b5a1f31d
  1. 18
      src/ImageSharp/Formats/WebP/BitReader/BitReaderBase.cs
  2. 10
      src/ImageSharp/Formats/WebP/Lossless/WebPLosslessDecoder.cs
  3. 13
      tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs

18
src/ImageSharp/Formats/WebP/BitReader/BitReaderBase.cs

@ -29,23 +29,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.WebP.BitReader
{
this.Data = memoryAllocator.Allocate<byte>(bytesToRead);
Span<byte> dataSpan = this.Data.Memory.Span;
using (IManagedByteBuffer buffer = memoryAllocator.AllocateManagedByteBuffer(4096))
{
Span<byte> bufferSpan = buffer.GetSpan();
int read;
while (bytesToRead > 0 && (read = input.Read(buffer.Array, 0, Math.Min(bufferSpan.Length, bytesToRead))) > 0)
{
buffer.Array.AsSpan(0, read).CopyTo(dataSpan);
bytesToRead -= read;
dataSpan = dataSpan.Slice(read);
}
if (bytesToRead > 0)
{
WebPThrowHelper.ThrowImageFormatException("webp image file has insufficient data");
}
}
input.Read(dataSpan.Slice(0, bytesToRead), 0, bytesToRead);
}
/// <inheritdoc/>

10
src/ImageSharp/Formats/WebP/Lossless/WebPLosslessDecoder.cs

@ -945,17 +945,23 @@ namespace SixLabors.ImageSharp.Formats.Experimental.WebP.Lossless
/// <param name="length">The number of pixels to copy.</param>
private static void CopyBlock(Span<uint> pixelData, int decodedPixels, int dist, int length)
{
int start = decodedPixels - dist;
if (start < 0)
{
WebPThrowHelper.ThrowImageFormatException("webp image data seems to be invalid");
}
if (dist >= length)
{
// no overlap.
Span<uint> src = pixelData.Slice(decodedPixels - dist, length);
Span<uint> src = pixelData.Slice(start, length);
Span<uint> dest = pixelData.Slice(decodedPixels);
src.CopyTo(dest);
}
else
{
// There is overlap between the backward reference distance and the pixels to copy.
Span<uint> src = pixelData.Slice(decodedPixels - dist);
Span<uint> src = pixelData.Slice(start);
Span<uint> dest = pixelData.Slice(decodedPixels);
for (int i = 0; i < length; i++)
{

13
tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs

@ -321,8 +321,19 @@ namespace SixLabors.ImageSharp.Tests.Formats.WebP
[Theory]
[WithFile(Lossless.LossLessCorruptImage1, PixelTypes.Rgba32)]
[WithFile(Lossless.LossLessCorruptImage2, PixelTypes.Rgba32)]
[WithFile(Lossless.LossLessCorruptImage3, PixelTypes.Rgba32)]
[WithFile(Lossless.LossLessCorruptImage4, PixelTypes.Rgba32)]
public void WebpDecoder_CanDecodeLosslessWithIssues<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
// Just make sure no exception is thrown. The reference decoder fails to load the image.
using (Image<TPixel> image = provider.GetImage(WebpDecoder))
{
image.DebugSave(provider);
}
}
[Theory]
[WithFile(Lossless.LossLessCorruptImage3, PixelTypes.Rgba32)]
public void WebpDecoder_ThrowImageFormatException_OnInvalidImages<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{

Loading…
Cancel
Save