From ef35d52da2be8ad3f25ee964c0bbdc373d0ebbb4 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sat, 18 Jan 2020 18:42:26 +0100 Subject: [PATCH] Introduce VP8 Profile which contains the reconstruction filter and the loop filter --- src/ImageSharp/Formats/WebP/LoopFilter.cs | 12 ++++++++ .../Formats/WebP/ReconstructionFilter.cs | 12 ++++++++ src/ImageSharp/Formats/WebP/Vp8Profile.cs | 21 ++++++++++++++ .../Formats/WebP/WebPDecoderCore.cs | 1 + src/ImageSharp/Formats/WebP/WebPImageInfo.cs | 4 +-- .../Formats/WebP/WebPLossyDecoder.cs | 29 ++++++------------- 6 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 src/ImageSharp/Formats/WebP/LoopFilter.cs create mode 100644 src/ImageSharp/Formats/WebP/ReconstructionFilter.cs create mode 100644 src/ImageSharp/Formats/WebP/Vp8Profile.cs diff --git a/src/ImageSharp/Formats/WebP/LoopFilter.cs b/src/ImageSharp/Formats/WebP/LoopFilter.cs new file mode 100644 index 0000000000..80a6b95edc --- /dev/null +++ b/src/ImageSharp/Formats/WebP/LoopFilter.cs @@ -0,0 +1,12 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.Formats.WebP +{ + internal enum LoopFilter + { + Normal, + Simple, + None + } +} diff --git a/src/ImageSharp/Formats/WebP/ReconstructionFilter.cs b/src/ImageSharp/Formats/WebP/ReconstructionFilter.cs new file mode 100644 index 0000000000..ff59e6b668 --- /dev/null +++ b/src/ImageSharp/Formats/WebP/ReconstructionFilter.cs @@ -0,0 +1,12 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.Formats.WebP +{ + internal enum ReconstructionFilter + { + None, + Bicubic, + Bilinear + } +} diff --git a/src/ImageSharp/Formats/WebP/Vp8Profile.cs b/src/ImageSharp/Formats/WebP/Vp8Profile.cs new file mode 100644 index 0000000000..b1d757cb53 --- /dev/null +++ b/src/ImageSharp/Formats/WebP/Vp8Profile.cs @@ -0,0 +1,21 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.Formats.WebP +{ + /// + /// The version number setting enables or disables certain features in the bitstream. + /// + internal class Vp8Profile + { + /// + /// Gets or sets the reconstruction filter. + /// + public ReconstructionFilter ReconstructionFilter { get; set; } + + /// + /// Gets or sets the loop filter. + /// + public LoopFilter LoopFilter { get; set; } + } +} diff --git a/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs b/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs index 239755c1f7..fafb5e752a 100644 --- a/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs +++ b/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs @@ -343,6 +343,7 @@ namespace SixLabors.ImageSharp.Formats.WebP IsLossLess = false, ImageDataSize = dataSize, Features = features, + Vp8Profile = (sbyte)version }; } diff --git a/src/ImageSharp/Formats/WebP/WebPImageInfo.cs b/src/ImageSharp/Formats/WebP/WebPImageInfo.cs index c9fd9bd513..5b602cce8c 100644 --- a/src/ImageSharp/Formats/WebP/WebPImageInfo.cs +++ b/src/ImageSharp/Formats/WebP/WebPImageInfo.cs @@ -36,9 +36,9 @@ namespace SixLabors.ImageSharp.Formats.WebP public uint ImageDataSize { get; set; } /// - /// Gets or sets the VP8 profile / version. Valid values are between 0 and 3. + /// Gets or sets the VP8 profile / version. Valid values are between 0 and 3. Default value will be the invalid value -1. /// - public byte Vp8Profile { get; set; } + public int Vp8Profile { get; set; } = -1; /// /// Gets or sets Vp8L bitreader. Will be null if its not lossless image. diff --git a/src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs b/src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs index 896c3e256c..f01d5d3625 100644 --- a/src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs +++ b/src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; using System.IO; using SixLabors.ImageSharp.Memory; @@ -16,7 +15,7 @@ namespace SixLabors.ImageSharp.Formats.WebP private readonly Stream currentStream; - private MemoryAllocator memoryAllocator; + private readonly MemoryAllocator memoryAllocator; public WebPLossyDecoder(Configuration configuration, Stream currentStream) { @@ -25,7 +24,7 @@ namespace SixLabors.ImageSharp.Formats.WebP this.memoryAllocator = configuration.MemoryAllocator; } - public void Decode(Buffer2D pixels, int width, int height, uint imageDataSize, byte vp8Version) + public void Decode(Buffer2D pixels, int width, int height, uint imageDataSize, int vp8Version) where TPixel : struct, IPixel { // we need buffers for Y U and V in size of the image @@ -37,40 +36,30 @@ namespace SixLabors.ImageSharp.Formats.WebP // TODO residue signal from DCT: 4x4 blocks of DCT transforms, 16Y, 4U, 4V var bitReader = new Vp8BitReader(this.currentStream, imageDataSize, this.memoryAllocator); - (ReconstructionFilter rec, LoopFilter loop) = this.DecodeVersion(vp8Version); + Vp8Profile vp8Profile = this.DecodeProfile(vp8Version); } - private (ReconstructionFilter, LoopFilter) DecodeVersion(byte version) + private Vp8Profile DecodeProfile(int version) { - var rec = ReconstructionFilter.None; - var loop = LoopFilter.None; - switch (version) { case 0: - return (ReconstructionFilter.Bicubic, LoopFilter.Normal); + return new Vp8Profile { ReconstructionFilter = ReconstructionFilter.Bicubic, LoopFilter = LoopFilter.Normal }; case 1: - return (ReconstructionFilter.Bilinear, LoopFilter.Simple); + return new Vp8Profile { ReconstructionFilter = ReconstructionFilter.Bilinear, LoopFilter = LoopFilter.Simple }; case 2: - return (ReconstructionFilter.Bilinear, LoopFilter.None); + return new Vp8Profile { ReconstructionFilter = ReconstructionFilter.Bilinear, LoopFilter = LoopFilter.None }; case 3: - return (ReconstructionFilter.None, LoopFilter.None); + return new Vp8Profile { ReconstructionFilter = ReconstructionFilter.None, LoopFilter = LoopFilter.None }; default: // Reserved for future use in Spec. // https://tools.ietf.org/html/rfc6386#page-30 WebPThrowHelper.ThrowNotSupportedException($"unsupported VP8 version {version} found"); - return (rec, loop); + return new Vp8Profile(); } } } - enum ReconstructionFilter - { - None, - Bicubic, - Bilinear - } - enum LoopFilter { Normal,