diff --git a/src/ImageSharp/Formats/WebP/Vp8LBitReader.cs b/src/ImageSharp/Formats/WebP/Vp8LBitReader.cs index 036f441d09..b801a4c335 100644 --- a/src/ImageSharp/Formats/WebP/Vp8LBitReader.cs +++ b/src/ImageSharp/Formats/WebP/Vp8LBitReader.cs @@ -18,10 +18,7 @@ namespace SixLabors.ImageSharp.Formats.WebP /// The input stream to read from. public Vp8LBitReader(Stream inputStream) { - this.stream = new MemoryStream(); - long inputStreamPos = inputStream.Position; - inputStream.CopyTo(this.stream); - inputStream.Position = inputStreamPos; + this.stream = inputStream; this.Offset = 0; this.Bit = 0; } diff --git a/src/ImageSharp/Formats/WebP/WebPChunkType.cs b/src/ImageSharp/Formats/WebP/WebPChunkType.cs index 85ee888bf7..9af98a3943 100644 --- a/src/ImageSharp/Formats/WebP/WebPChunkType.cs +++ b/src/ImageSharp/Formats/WebP/WebPChunkType.cs @@ -6,7 +6,7 @@ namespace SixLabors.ImageSharp.Formats.WebP /// /// Contains a list of different webp chunk types. /// - internal enum WebPChunkType : uint + public enum WebPChunkType : uint { /// /// Header signaling the use of VP8 video format. diff --git a/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs b/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs index c04a3f2426..022ded8aa7 100644 --- a/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs +++ b/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs @@ -67,8 +67,6 @@ namespace SixLabors.ImageSharp.Formats.WebP public Image Decode(Stream stream) where TPixel : struct, IPixel { - var metadata = new ImageMetadata(); - WebPMetadata webpMetadata = metadata.GetFormatMetadata(WebPFormat.Instance); this.currentStream = stream; uint fileSize = this.ReadImageHeader(); @@ -298,11 +296,8 @@ namespace SixLabors.ImageSharp.Formats.WebP // The next 3 bytes are the version. The version_number is a 3 bit code that must be set to 0. // Any other value should be treated as an error. + // TODO: should we throw here when version number is != 0? uint version = bitReader.Read(3); - if (version != 0) - { - WebPThrowHelper.ThrowImageFormatException($"Unexpected webp version number: {version}"); - } // Next bit indicates, if a transformation is present. bool transformPresent = bitReader.ReadBit(); @@ -365,15 +360,15 @@ namespace SixLabors.ImageSharp.Formats.WebP private void ReadSimpleLossy(Buffer2D pixels, int width, int height, int imageDataSize) where TPixel : struct, IPixel { - // TODO: implement decoding, for simulating the decoding, skipping the chunk size bytes. - this.currentStream.Skip(imageDataSize - 10); // 10 bytes because of VP8X header. + // TODO: implement decoding. For simulating the decoding: skipping the chunk size bytes. + this.currentStream.Skip(imageDataSize); // TODO: this does not seem to work in all cases } private void ReadSimpleLossless(Buffer2D pixels, int width, int height, int imageDataSize) where TPixel : struct, IPixel { - // TODO: implement decoding, for simulating the decoding, skipping the chunk size bytes. - this.currentStream.Skip(imageDataSize - 10); // 10 bytes because of VP8X header. + // TODO: implement decoding. For simulating the decoding: skipping the chunk size bytes. + this.currentStream.Skip(imageDataSize); // TODO: this does not seem to work in all cases } private void ReadExtended(Buffer2D pixels, int width, int height) @@ -403,9 +398,14 @@ namespace SixLabors.ImageSharp.Formats.WebP /// private WebPChunkType ReadChunkType() { - return this.currentStream.Read(this.buffer, 0, 4) == 4 - ? (WebPChunkType)BinaryPrimitives.ReadUInt32BigEndian(this.buffer) - : throw new ImageFormatException("Invalid WebP data."); + if (this.currentStream.Read(this.buffer, 0, 4) == 4) + { + var chunkType = (WebPChunkType)BinaryPrimitives.ReadUInt32BigEndian(this.buffer); + this.webpMetadata.ChunkTypes.Enqueue(chunkType); + return chunkType; + } + + throw new ImageFormatException("Invalid WebP data."); } /// diff --git a/src/ImageSharp/Formats/WebP/WebPMetadata.cs b/src/ImageSharp/Formats/WebP/WebPMetadata.cs index 88c687827a..69e9a43f48 100644 --- a/src/ImageSharp/Formats/WebP/WebPMetadata.cs +++ b/src/ImageSharp/Formats/WebP/WebPMetadata.cs @@ -1,6 +1,9 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Collections; +using System.Collections.Generic; + namespace SixLabors.ImageSharp.Formats.WebP { /// @@ -33,6 +36,11 @@ namespace SixLabors.ImageSharp.Formats.WebP /// public WebPFormatType Format { get; set; } + /// + /// All found chunk types ordered by appearance. + /// + public Queue ChunkTypes { get; set; } = new Queue(); + /// /// Indicates, if the webp file contains a animation. /// diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs index f12ba32315..c0636cb191 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.WebP [InlineData(Lossless.Lossless2, 1000, 307, 24)] [InlineData(Lossy.Alpha.LossyAlpha1, 1000, 307, 24)] [InlineData(Lossy.Alpha.LossyAlpha2, 1000, 307, 24)] - //[InlineData(Animated.Animated1, 400, 400, 24)] + [InlineData(Animated.Animated1, 400, 400, 24)] public void Identify_DetectsCorrectDimensions(string imagePath, int expectedWidth, int expectedHeight, int expectedBitsPerPixel) { var testFile = TestFile.Create(imagePath);