From 1de13422ed39f6d1538e220635c1fe2fb585af11 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Thu, 24 Oct 2019 20:00:45 +0200 Subject: [PATCH] Throwing not supported exception for animated images --- .../Formats/WebP/WebPDecoderCore.cs | 23 ++++++++----------- src/ImageSharp/Formats/WebP/WebPImageInfo.cs | 5 ++++ .../Formats/WebP/WebPThrowHelper.cs | 11 +++++++++ 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs b/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs index 5d03db5584..c04a3f2426 100644 --- a/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs +++ b/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs @@ -73,6 +73,10 @@ namespace SixLabors.ImageSharp.Formats.WebP uint fileSize = this.ReadImageHeader(); WebPImageInfo imageInfo = this.ReadVp8Info(); + if (imageInfo.IsAnimation) + { + WebPThrowHelper.ThrowNotSupportedException("Animations are not supported"); + } var image = new Image(this.configuration, imageInfo.Width, imageInfo.Height, this.metadata); Buffer2D pixels = image.GetRootFramePixelBuffer(); @@ -195,19 +199,12 @@ namespace SixLabors.ImageSharp.Formats.WebP { this.webpMetadata.Animated = true; - // ANIM chunk will be followed by n ANMF chunks - chunkType = this.ReadChunkType(); - uint animationParameterChunkSize = this.ReadChunkSize(); - this.currentStream.Skip((int)animationParameterChunkSize); - chunkType = this.ReadChunkType(); - - // TODO: not sure yet how to determine how many animation chunks there will be. - while (chunkType == WebPChunkType.Animation) - { - uint animationChunkSize = this.ReadChunkSize(); - this.currentStream.Skip((int)animationChunkSize); - chunkType = this.ReadChunkType(); - } + return new WebPImageInfo() + { + Width = width, + Height = height, + IsAnimation = true + }; } if (isAlphaPresent) diff --git a/src/ImageSharp/Formats/WebP/WebPImageInfo.cs b/src/ImageSharp/Formats/WebP/WebPImageInfo.cs index 10b6aa1822..b03244dc72 100644 --- a/src/ImageSharp/Formats/WebP/WebPImageInfo.cs +++ b/src/ImageSharp/Formats/WebP/WebPImageInfo.cs @@ -20,6 +20,11 @@ namespace SixLabors.ImageSharp.Formats.WebP /// public bool IsLossLess { get; set; } + /// + /// Gets or sets whether this image is a animation. + /// + public bool IsAnimation { get; set; } + /// /// The bytes of the image payload. /// diff --git a/src/ImageSharp/Formats/WebP/WebPThrowHelper.cs b/src/ImageSharp/Formats/WebP/WebPThrowHelper.cs index fabbc9bc3a..7cc4df2466 100644 --- a/src/ImageSharp/Formats/WebP/WebPThrowHelper.cs +++ b/src/ImageSharp/Formats/WebP/WebPThrowHelper.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; using System.Runtime.CompilerServices; namespace SixLabors.ImageSharp.Formats.WebP @@ -16,5 +17,15 @@ namespace SixLabors.ImageSharp.Formats.WebP { throw new ImageFormatException(errorMessage); } + + /// + /// Cold path optimization for throwing -s + /// + /// The error message for the exception. + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowNotSupportedException(string errorMessage) + { + throw new NotSupportedException(errorMessage); + } } }