From 96c1c725f94dcb8d5882c22af68c3f46a9d5582e Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 30 Jan 2022 18:41:56 +0100 Subject: [PATCH 1/8] Write ALPH chunk (only uncompressed for now) --- src/ImageSharp/Formats/Webp/AlphaEncoder.cs | 42 ++++++++++++++++++ .../Formats/Webp/BitWriter/BitWriterBase.cs | 43 ++++++++++++++++++- .../Formats/Webp/BitWriter/Vp8BitWriter.cs | 35 ++++++++++++--- .../Formats/Webp/Lossy/Vp8Encoder.cs | 13 ++++-- .../Formats/Webp/Lossy/YuvConversion.cs | 11 ++++- 5 files changed, 133 insertions(+), 11 deletions(-) create mode 100644 src/ImageSharp/Formats/Webp/AlphaEncoder.cs diff --git a/src/ImageSharp/Formats/Webp/AlphaEncoder.cs b/src/ImageSharp/Formats/Webp/AlphaEncoder.cs new file mode 100644 index 0000000000..06c114c71f --- /dev/null +++ b/src/ImageSharp/Formats/Webp/AlphaEncoder.cs @@ -0,0 +1,42 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Buffers; +using SixLabors.ImageSharp.Memory; +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Formats.Webp +{ + /// + /// Encodes the alpha channel data. + /// Data is either compressed as lossless webp image or uncompressed. + /// + internal static class AlphaEncoder + { + public static byte[] EncodeAlpha(Image image, Configuration configuration, MemoryAllocator memoryAllocator) + where TPixel : unmanaged, IPixel + { + Buffer2D imageBuffer = image.Frames.RootFrame.PixelBuffer; + int height = image.Height; + int width = image.Width; + byte[] alphaData = new byte[width * height]; + + using IMemoryOwner rowBuffer = memoryAllocator.Allocate(width); + Span rgbaRow = rowBuffer.GetSpan(); + + for (int y = 0; y < height; y++) + { + Span rowSpan = imageBuffer.DangerousGetRowSpan(y); + PixelOperations.Instance.ToRgba32(configuration, rowSpan, rgbaRow); + int offset = y * width; + for (int x = 0; x < width; x++) + { + alphaData[offset + x] = rgbaRow[x].A; + } + } + + return alphaData; + } + } +} diff --git a/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs b/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs index ac039be797..b33f7987c1 100644 --- a/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs +++ b/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs @@ -94,7 +94,7 @@ protected void WriteRiffHeader(Stream stream, uint riffSize) /// Calculates the chunk size of EXIF or XMP metadata. /// /// The metadata profile bytes. - /// The exif chunk size in bytes. + /// The metadata chunk size in bytes. protected uint MetadataChunkSize(byte[] metadataBytes) { uint metaSize = (uint)metadataBytes.Length; @@ -103,6 +103,19 @@ protected uint MetadataChunkSize(byte[] metadataBytes) return metaChunkSize; } + /// + /// Calculates the chunk size of a alpha chunk. + /// + /// The alpha chunk bytes. + /// The alpha data chunk size in bytes. + protected uint AlphaChunkSize(byte[] alphaBytes) + { + uint alphaSize = (uint)alphaBytes.Length + 1; + uint alphaChunkSize = WebpConstants.ChunkHeaderSize + alphaSize + (alphaSize & 1); + + return alphaChunkSize; + } + /// /// Writes a metadata profile (EXIF or XMP) to the stream. /// @@ -128,6 +141,34 @@ protected void WriteMetadataProfile(Stream stream, byte[] metadataBytes, WebpChu } } + /// + /// Writes the alpha chunk to the stream. + /// + /// The stream to write to. + /// The alpha channel data bytes. + protected void WriteAlphaChunk(Stream stream, byte[] dataBytes) + { + DebugGuard.NotNull(dataBytes, nameof(dataBytes)); + + uint size = (uint)dataBytes.Length + 1; + Span buf = this.scratchBuffer.AsSpan(0, 4); + BinaryPrimitives.WriteUInt32BigEndian(buf, (uint)WebpChunkType.Alpha); + stream.Write(buf); + BinaryPrimitives.WriteUInt32LittleEndian(buf, size); + stream.Write(buf); + + // Write flags, all zero for now. + stream.WriteByte(0); + + stream.Write(dataBytes); + + // Add padding byte if needed. + if ((size & 1) == 1) + { + stream.WriteByte(0); + } + } + /// /// Writes a VP8X header to the stream. /// diff --git a/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs b/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs index 4e91bedb0b..3f16fc89bc 100644 --- a/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs +++ b/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs @@ -409,7 +409,8 @@ private void Flush() /// The width of the image. /// The height of the image. /// Flag indicating, if a alpha channel is present. - public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, XmpProfile xmpProfile, uint width, uint height, bool hasAlpha) + /// The alpha channel data. + public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, XmpProfile xmpProfile, uint width, uint height, bool hasAlpha, byte[] alphaData) { bool isVp8X = false; byte[] exifBytes = null; @@ -418,7 +419,6 @@ public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, Xm if (exifProfile != null) { isVp8X = true; - riffSize += ExtendedFileChunkSize; exifBytes = exifProfile.ToByteArray(); riffSize += this.MetadataChunkSize(exifBytes); } @@ -426,11 +426,21 @@ public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, Xm if (xmpProfile != null) { isVp8X = true; - riffSize += ExtendedFileChunkSize; xmpBytes = xmpProfile.Data; riffSize += this.MetadataChunkSize(xmpBytes); } + if (hasAlpha) + { + isVp8X = true; + riffSize += this.AlphaChunkSize(alphaData); + } + + if (isVp8X) + { + riffSize += ExtendedFileChunkSize; + } + this.Finish(); uint numBytes = (uint)this.NumBytes(); int mbSize = this.enc.Mbw * this.enc.Mbh; @@ -451,7 +461,7 @@ public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, Xm riffSize += WebpConstants.TagSize + WebpConstants.ChunkHeaderSize + vp8Size; // Emit headers and partition #0 - this.WriteWebpHeaders(stream, size0, vp8Size, riffSize, isVp8X, width, height, exifProfile, xmpProfile, hasAlpha); + this.WriteWebpHeaders(stream, size0, vp8Size, riffSize, isVp8X, width, height, exifProfile, xmpProfile, hasAlpha, alphaData); bitWriterPartZero.WriteToStream(stream); // Write the encoded image to the stream. @@ -639,7 +649,18 @@ private void CodeIntraModes(Vp8BitWriter bitWriter) while (it.Next()); } - private void WriteWebpHeaders(Stream stream, uint size0, uint vp8Size, uint riffSize, bool isVp8X, uint width, uint height, ExifProfile exifProfile, XmpProfile xmpProfile, bool hasAlpha) + private void WriteWebpHeaders( + Stream stream, + uint size0, + uint vp8Size, + uint riffSize, + bool isVp8X, + uint width, + uint height, + ExifProfile exifProfile, + XmpProfile xmpProfile, + bool hasAlpha, + byte[] alphaData) { this.WriteRiffHeader(stream, riffSize); @@ -647,6 +668,10 @@ private void WriteWebpHeaders(Stream stream, uint size0, uint vp8Size, uint riff if (isVp8X) { this.WriteVp8XHeader(stream, exifProfile, xmpProfile, width, height, hasAlpha); + if (hasAlpha) + { + this.WriteAlphaChunk(stream, alphaData); + } } this.WriteVp8Header(stream, vp8Size); diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs index 0222320502..48af53960c 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs @@ -300,7 +300,7 @@ public void Encode(Image image, Stream stream) Span y = this.Y.GetSpan(); Span u = this.U.GetSpan(); Span v = this.V.GetSpan(); - YuvConversion.ConvertRgbToYuv(image, this.configuration, this.memoryAllocator, y, u, v); + bool hasAlpha = YuvConversion.ConvertRgbToYuv(image, this.configuration, this.memoryAllocator, y, u, v); int yStride = width; int uvStride = (yStride + 1) >> 1; @@ -322,8 +322,13 @@ public void Encode(Image image, Stream stream) int expectedSize = this.Mbw * this.Mbh * averageBytesPerMacroBlock; this.bitWriter = new Vp8BitWriter(expectedSize, this); - // TODO: EncodeAlpha(); - bool hasAlpha = false; + // Extract and encode alpha data, if present. + byte[] alphaData = null; + if (hasAlpha) + { + // TODO: This can potentially run in an separate task. + alphaData = AlphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator); + } // Stats-collection loop. this.StatLoop(width, height, yStride, uvStride); @@ -358,7 +363,7 @@ public void Encode(Image image, Stream stream) // Write bytes from the bitwriter buffer to the stream. ImageMetadata metadata = image.Metadata; metadata.SyncProfiles(); - this.bitWriter.WriteEncodedImageToStream(stream, metadata.ExifProfile, metadata.XmpProfile, (uint)width, (uint)height, hasAlpha); + this.bitWriter.WriteEncodedImageToStream(stream, metadata.ExifProfile, metadata.XmpProfile, (uint)width, (uint)height, hasAlpha, alphaData); } /// diff --git a/src/ImageSharp/Formats/Webp/Lossy/YuvConversion.cs b/src/ImageSharp/Formats/Webp/Lossy/YuvConversion.cs index 7a731f4284..878bebd105 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/YuvConversion.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/YuvConversion.cs @@ -318,7 +318,8 @@ private static void PackAndStore(Vector128 a, Vector128 b, Vector128 /// Span to store the luma component of the image. /// Span to store the u component of the image. /// Span to store the v component of the image. - public static void ConvertRgbToYuv(Image image, Configuration configuration, MemoryAllocator memoryAllocator, Span y, Span u, Span v) + /// true, if the image contains alpha data. + public static bool ConvertRgbToYuv(Image image, Configuration configuration, MemoryAllocator memoryAllocator, Span y, Span u, Span v) where TPixel : unmanaged, IPixel { Buffer2D imageBuffer = image.Frames.RootFrame.PixelBuffer; @@ -335,6 +336,7 @@ public static void ConvertRgbToYuv(Image image, Configuration co Span bgraRow1 = bgraRow1Buffer.GetSpan(); int uvRowIndex = 0; int rowIndex; + bool hasAlpha = false; for (rowIndex = 0; rowIndex < height - 1; rowIndex += 2) { Span rowSpan = imageBuffer.DangerousGetRowSpan(rowIndex); @@ -343,6 +345,10 @@ public static void ConvertRgbToYuv(Image image, Configuration co PixelOperations.Instance.ToBgra32(configuration, nextRowSpan, bgraRow1); bool rowsHaveAlpha = WebpCommonUtils.CheckNonOpaque(bgraRow0) && WebpCommonUtils.CheckNonOpaque(bgraRow1); + if (rowsHaveAlpha) + { + hasAlpha = true; + } // Downsample U/V planes, two rows at a time. if (!rowsHaveAlpha) @@ -375,10 +381,13 @@ public static void ConvertRgbToYuv(Image image, Configuration co else { AccumulateRgba(bgraRow0, bgraRow0, tmpRgbSpan, width); + hasAlpha = true; } ConvertRgbaToUv(tmpRgbSpan, u.Slice(uvRowIndex * uvWidth), v.Slice(uvRowIndex * uvWidth), uvWidth); } + + return hasAlpha; } /// From d1929412289d3c8bb5bc3974cc47e0b811f0e75f Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 1 Feb 2022 10:23:08 +0100 Subject: [PATCH 2/8] Add lossless alpha compression --- src/ImageSharp/Formats/Webp/AlphaEncoder.cs | 86 ++++++++++++++++++- .../Formats/Webp/BitWriter/BitWriterBase.cs | 11 ++- .../Formats/Webp/BitWriter/Vp8BitWriter.cs | 10 ++- .../Formats/Webp/Lossless/Vp8LEncoder.cs | 31 ++++++- .../Formats/Webp/Lossy/Vp8Encoder.cs | 14 +-- .../Formats/Webp/WebpEncoderCore.cs | 6 +- 6 files changed, 134 insertions(+), 24 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/AlphaEncoder.cs b/src/ImageSharp/Formats/Webp/AlphaEncoder.cs index 06c114c71f..571da5bb24 100644 --- a/src/ImageSharp/Formats/Webp/AlphaEncoder.cs +++ b/src/ImageSharp/Formats/Webp/AlphaEncoder.cs @@ -3,18 +3,98 @@ using System; using System.Buffers; +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Formats.Webp.Lossless; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Webp { /// - /// Encodes the alpha channel data. - /// Data is either compressed as lossless webp image or uncompressed. + /// Methods for encoding the alpha data of a VP8 image. /// internal static class AlphaEncoder { - public static byte[] EncodeAlpha(Image image, Configuration configuration, MemoryAllocator memoryAllocator) + /// + /// Encodes the alpha channel data. + /// Data is either compressed as lossless webp image or uncompressed. + /// + /// The pixel format. + /// The to encode from. + /// The global configuration. + /// The memory manager. + /// Indicates, if the data should be compressed with the lossless webp compression. + /// The alpha data. + public static byte[] EncodeAlpha(Image image, Configuration configuration, MemoryAllocator memoryAllocator, bool compress) + where TPixel : unmanaged, IPixel + { + byte[] alphaData = ExtractAlphaChannel(image, configuration, memoryAllocator); + int width = image.Width; + int height = image.Height; + if (compress) + { + WebpEncodingMethod effort = WebpEncodingMethod.Default; + int quality = 8 * (int)effort; + using var lossLessEncoder = new Vp8LEncoder( + memoryAllocator, + configuration, + width, + height, + quality, + effort, + WebpTransparentColorMode.Preserve, + false, + 0); + + // The transparency information will be stored in the green channel of the ARGB quadruplet. + // The green channel is allowed extra transformation steps in the specification -- unlike the other channels, + // that can improve compression. + using Image alphaAsImage = DispatchAlphaToGreen(image, alphaData); + + return lossLessEncoder.EncodeAlphaImageData(alphaAsImage); + } + + return alphaData; + } + + /// + /// Store the transparency in the green channel. + /// + /// The pixel format. + /// The to encode from. + /// A byte sequence of length width * height, containing all the 8-bit transparency values in scan order. + /// The transparency image. + private static Image DispatchAlphaToGreen(Image image, byte[] alphaData) + where TPixel : unmanaged, IPixel + { + int width = image.Width; + int height = image.Height; + var alphaAsImage = new Image(width, height); + + for (int y = 0; y < height; y++) + { + Memory rowBuffer = alphaAsImage.DangerousGetPixelRowMemory(y); + Span pixelRow = rowBuffer.Span; + Span alphaRow = alphaData.AsSpan(y * width, width); + for (int x = 0; x < width; x++) + { + // Leave A/R/B channels zero'd. + pixelRow[x] = new Rgba32(0, alphaRow[x], 0, 0); + } + } + + return alphaAsImage; + } + + /// + /// Extract the alpha data of the image. + /// + /// The pixel format. + /// The to encode from. + /// The global configuration. + /// The memory manager. + /// A byte sequence of length width * height, containing all the 8-bit transparency values in scan order. + private static byte[] ExtractAlphaChannel(Image image, Configuration configuration, MemoryAllocator memoryAllocator) where TPixel : unmanaged, IPixel { Buffer2D imageBuffer = image.Frames.RootFrame.PixelBuffer; diff --git a/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs b/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs index b33f7987c1..84c9d3f133 100644 --- a/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs +++ b/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs @@ -146,7 +146,8 @@ protected void WriteMetadataProfile(Stream stream, byte[] metadataBytes, WebpChu /// /// The stream to write to. /// The alpha channel data bytes. - protected void WriteAlphaChunk(Stream stream, byte[] dataBytes) + /// Indicates, if the alpha channel data is compressed. + protected void WriteAlphaChunk(Stream stream, byte[] dataBytes, bool alphaDataIsCompressed) { DebugGuard.NotNull(dataBytes, nameof(dataBytes)); @@ -157,9 +158,13 @@ protected void WriteAlphaChunk(Stream stream, byte[] dataBytes) BinaryPrimitives.WriteUInt32LittleEndian(buf, size); stream.Write(buf); - // Write flags, all zero for now. - stream.WriteByte(0); + byte flags = 0; + if (alphaDataIsCompressed) + { + flags |= 1; + } + stream.WriteByte(flags); stream.Write(dataBytes); // Add padding byte if needed. diff --git a/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs b/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs index 3f16fc89bc..577a87e6a1 100644 --- a/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs +++ b/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs @@ -410,7 +410,8 @@ private void Flush() /// The height of the image. /// Flag indicating, if a alpha channel is present. /// The alpha channel data. - public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, XmpProfile xmpProfile, uint width, uint height, bool hasAlpha, byte[] alphaData) + /// Indicates, if the alpha data is compressed. + public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, XmpProfile xmpProfile, uint width, uint height, bool hasAlpha, byte[] alphaData, bool alphaDataIsCompressed) { bool isVp8X = false; byte[] exifBytes = null; @@ -461,7 +462,7 @@ public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, Xm riffSize += WebpConstants.TagSize + WebpConstants.ChunkHeaderSize + vp8Size; // Emit headers and partition #0 - this.WriteWebpHeaders(stream, size0, vp8Size, riffSize, isVp8X, width, height, exifProfile, xmpProfile, hasAlpha, alphaData); + this.WriteWebpHeaders(stream, size0, vp8Size, riffSize, isVp8X, width, height, exifProfile, xmpProfile, hasAlpha, alphaData, alphaDataIsCompressed); bitWriterPartZero.WriteToStream(stream); // Write the encoded image to the stream. @@ -660,7 +661,8 @@ private void WriteWebpHeaders( ExifProfile exifProfile, XmpProfile xmpProfile, bool hasAlpha, - byte[] alphaData) + byte[] alphaData, + bool alphaDataIsCompressed) { this.WriteRiffHeader(stream, riffSize); @@ -670,7 +672,7 @@ private void WriteWebpHeaders( this.WriteVp8XHeader(stream, exifProfile, xmpProfile, width, height, hasAlpha); if (hasAlpha) { - this.WriteAlphaChunk(stream, alphaData); + this.WriteAlphaChunk(stream, alphaData, alphaDataIsCompressed); } } diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs index e9dce913a3..797d0794f9 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs @@ -228,7 +228,7 @@ public Vp8LEncoder( public Vp8LHashChain HashChain { get; } /// - /// Encodes the image to the specified stream from the . + /// Encodes the image as lossless webp to the specified stream. /// /// The pixel format. /// The to encode from. @@ -236,10 +236,12 @@ public Vp8LEncoder( public void Encode(Image image, Stream stream) where TPixel : unmanaged, IPixel { - image.Metadata.SyncProfiles(); int width = image.Width; int height = image.Height; + ImageMetadata metadata = image.Metadata; + metadata.SyncProfiles(); + // Convert image pixels to bgra array. bool hasAlpha = this.ConvertPixelsToBgra(image, width, height); @@ -253,11 +255,32 @@ public void Encode(Image image, Stream stream) this.EncodeStream(image); // Write bytes from the bitwriter buffer to the stream. - ImageMetadata metadata = image.Metadata; - metadata.SyncProfiles(); this.bitWriter.WriteEncodedImageToStream(stream, metadata.ExifProfile, metadata.XmpProfile, (uint)width, (uint)height, hasAlpha); } + /// + /// Encodes the alpha image data using the webp lossless compression. + /// + /// The type of the pixel. + /// The to encode from. + /// The encoded alpha stream. + public byte[] EncodeAlphaImageData(Image image) + where TPixel : unmanaged, IPixel + { + int width = image.Width; + int height = image.Height; + + // Convert image pixels to bgra array. + this.ConvertPixelsToBgra(image, width, height); + + // The image-stream does NOT contain any headers describing the image dimension, the dimension is already known. + this.EncodeStream(image); + this.bitWriter.Finish(); + using var ms = new MemoryStream(); + this.bitWriter.WriteToStream(ms); + return ms.ToArray(); + } + /// /// Writes the image size to the bitwriter buffer. /// diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs index 48af53960c..d8bd8f759c 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs @@ -71,10 +71,7 @@ internal class Vp8Encoder : IDisposable /// private int uvAlpha; - /// - /// Scratch buffer to reduce allocations. - /// - private readonly int[] scratch = new int[16]; + private readonly bool alphaCompression; private readonly byte[] averageBytesPerMb = { 50, 24, 16, 9, 7, 5, 3, 2 }; @@ -105,6 +102,7 @@ internal class Vp8Encoder : IDisposable /// Number of entropy-analysis passes (in [1..10]). /// The filter the strength of the deblocking filter, between 0 (no filtering) and 100 (maximum filtering). /// The spatial noise shaping. 0=off, 100=maximum. + /// If true, the alpha channel will be compressed with the lossless compression. public Vp8Encoder( MemoryAllocator memoryAllocator, Configuration configuration, @@ -114,7 +112,8 @@ public Vp8Encoder( WebpEncodingMethod method, int entropyPasses, int filterStrength, - int spatialNoiseShaping) + int spatialNoiseShaping, + bool alphaCompression) { this.memoryAllocator = memoryAllocator; this.configuration = configuration; @@ -125,6 +124,7 @@ public Vp8Encoder( this.entropyPasses = Numerics.Clamp(entropyPasses, 1, 10); this.filterStrength = Numerics.Clamp(filterStrength, 0, 100); this.spatialNoiseShaping = Numerics.Clamp(spatialNoiseShaping, 0, 100); + this.alphaCompression = alphaCompression; this.rdOptLevel = method is WebpEncodingMethod.BestQuality ? Vp8RdLevel.RdOptTrellisAll : method >= WebpEncodingMethod.Level5 ? Vp8RdLevel.RdOptTrellis : method >= WebpEncodingMethod.Level3 ? Vp8RdLevel.RdOptBasic @@ -327,7 +327,7 @@ public void Encode(Image image, Stream stream) if (hasAlpha) { // TODO: This can potentially run in an separate task. - alphaData = AlphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator); + alphaData = AlphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.alphaCompression); } // Stats-collection loop. @@ -363,7 +363,7 @@ public void Encode(Image image, Stream stream) // Write bytes from the bitwriter buffer to the stream. ImageMetadata metadata = image.Metadata; metadata.SyncProfiles(); - this.bitWriter.WriteEncodedImageToStream(stream, metadata.ExifProfile, metadata.XmpProfile, (uint)width, (uint)height, hasAlpha, alphaData); + this.bitWriter.WriteEncodedImageToStream(stream, metadata.ExifProfile, metadata.XmpProfile, (uint)width, (uint)height, hasAlpha, alphaData, this.alphaCompression); } /// diff --git a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs index 195fa62bdc..deed08b729 100644 --- a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs @@ -22,7 +22,6 @@ internal sealed class WebpEncoderCore : IImageEncoderInternals private readonly MemoryAllocator memoryAllocator; /// - /// TODO: not used at the moment. /// Indicating whether the alpha plane should be compressed with Webp lossless format. /// private readonly bool alphaCompression; @@ -100,7 +99,7 @@ public WebpEncoderCore(IWebpEncoderOptions options, MemoryAllocator memoryAlloca } /// - /// Encodes the image to the specified stream from the . + /// Encodes the image as webp to the specified stream. /// /// The pixel format. /// The to encode from. @@ -149,7 +148,8 @@ public void Encode(Image image, Stream stream, CancellationToken this.method, this.entropyPasses, this.filterStrength, - this.spatialNoiseShaping); + this.spatialNoiseShaping, + this.alphaCompression); enc.Encode(image, stream); } } From 8b8993dadc37a8a7970d4bcf26b1f043b860df60 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 1 Feb 2022 11:19:40 +0100 Subject: [PATCH 3/8] Add encode lossy webp with alpha tests --- .../Formats/WebP/WebpEncoderTests.cs | 40 +++++++++++++------ tests/ImageSharp.Tests/TestImages.cs | 1 + tests/Images/Input/Png/transparency.png | 3 ++ 3 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 tests/Images/Input/Png/transparency.png diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs index 7043549b22..7c74429edc 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs @@ -167,18 +167,6 @@ public void Encode_Lossless_WithPreserveTransparentColor_Works(TestImage image.VerifyEncoder(provider, "webp", testOutputDetails, encoder); } - [Theory] - [WithFile(TestPatternOpaque, PixelTypes.Rgba32)] - [WithFile(TestPatternOpaqueSmall, PixelTypes.Rgba32)] - public void Encode_Lossless_WorksWithTestPattern(TestImageProvider provider) - where TPixel : unmanaged, IPixel - { - using Image image = provider.GetImage(); - - var encoder = new WebpEncoder() { FileFormat = WebpFileFormatType.Lossless }; - image.VerifyEncoder(provider, "webp", string.Empty, encoder); - } - [Fact] public void Encode_Lossless_OneByOnePixel_Works() { @@ -279,6 +267,34 @@ public void Encode_Lossy_WithDifferentMethodsAndQuality_Works(TestImageP image.VerifyEncoder(provider, "webp", testOutputDetails, encoder, customComparer: GetComparer(quality)); } + [Theory] + [WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, false)] + [WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, true)] + public void Encode_Lossy_WithAlpha_Works(TestImageProvider provider, bool compressed) + where TPixel : unmanaged, IPixel + { + var encoder = new WebpEncoder() + { + FileFormat = WebpFileFormatType.Lossy, + UseAlphaCompression = compressed + }; + + using Image image = provider.GetImage(); + image.VerifyEncoder(provider, "webp", $"with_alpha_compressed_{compressed}", encoder, ImageComparer.Tolerant(0.04f)); + } + + [Theory] + [WithFile(TestPatternOpaque, PixelTypes.Rgba32)] + [WithFile(TestPatternOpaqueSmall, PixelTypes.Rgba32)] + public void Encode_Lossless_WorksWithTestPattern(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + + var encoder = new WebpEncoder() { FileFormat = WebpFileFormatType.Lossless }; + image.VerifyEncoder(provider, "webp", string.Empty, encoder); + } + [Theory] [WithFile(TestPatternOpaque, PixelTypes.Rgba32)] [WithFile(TestPatternOpaqueSmall, PixelTypes.Rgba32)] diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index bce22799da..a73d262433 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -15,6 +15,7 @@ public static class TestImages { public static class Png { + public const string Transparency = "Png/transparency.png"; public const string P1 = "Png/pl.png"; public const string Pd = "Png/pd.png"; public const string Blur = "Png/blur.png"; diff --git a/tests/Images/Input/Png/transparency.png b/tests/Images/Input/Png/transparency.png new file mode 100644 index 0000000000..26de0f2d1a --- /dev/null +++ b/tests/Images/Input/Png/transparency.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:843bea4db378f52935e2f19f60d289df8ebe20ddde3977c63225f1d58a10bd62 +size 48119 From d398ae744555d87cf71da22a02ed8fb50127ff00 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 1 Feb 2022 11:25:03 +0100 Subject: [PATCH 4/8] Default alpha compression to true --- src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs | 1 + src/ImageSharp/Formats/Webp/WebpEncoder.cs | 2 +- src/ImageSharp/Formats/Webp/WebpEncoderCore.cs | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs b/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs index d119d3031f..57ec32753d 100644 --- a/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs +++ b/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs @@ -31,6 +31,7 @@ internal interface IWebpEncoderOptions /// /// Gets a value indicating whether the alpha plane should be compressed with Webp lossless format. + /// Defaults to true. /// bool UseAlphaCompression { get; } diff --git a/src/ImageSharp/Formats/Webp/WebpEncoder.cs b/src/ImageSharp/Formats/Webp/WebpEncoder.cs index bdcbb194b1..d0b60d18cd 100644 --- a/src/ImageSharp/Formats/Webp/WebpEncoder.cs +++ b/src/ImageSharp/Formats/Webp/WebpEncoder.cs @@ -24,7 +24,7 @@ public sealed class WebpEncoder : IImageEncoder, IWebpEncoderOptions public WebpEncodingMethod Method { get; set; } = WebpEncodingMethod.Default; /// - public bool UseAlphaCompression { get; set; } + public bool UseAlphaCompression { get; set; } = true; /// public int EntropyPasses { get; set; } = 1; diff --git a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs index deed08b729..0fbff81fe4 100644 --- a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs @@ -23,6 +23,7 @@ internal sealed class WebpEncoderCore : IImageEncoderInternals /// /// Indicating whether the alpha plane should be compressed with Webp lossless format. + /// Defaults to true. /// private readonly bool alphaCompression; From cf672b99aeaa80d734edfa0a7ca0e2e36b590526 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 1 Feb 2022 12:10:19 +0100 Subject: [PATCH 5/8] Use memory allocator for alpha data --- src/ImageSharp/Formats/Webp/AlphaEncoder.cs | 26 ++++++++++++------- .../Formats/Webp/BitWriter/BitWriterBase.cs | 12 ++++++--- .../Formats/Webp/BitWriter/Vp8BitWriter.cs | 12 +++++++-- .../Formats/Webp/Lossless/Vp8LEncoder.cs | 13 +++++----- .../Formats/Webp/Lossy/Vp8Encoder.cs | 22 +++++++++++++--- 5 files changed, 59 insertions(+), 26 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/AlphaEncoder.cs b/src/ImageSharp/Formats/Webp/AlphaEncoder.cs index 571da5bb24..38497281ff 100644 --- a/src/ImageSharp/Formats/Webp/AlphaEncoder.cs +++ b/src/ImageSharp/Formats/Webp/AlphaEncoder.cs @@ -24,13 +24,15 @@ internal static class AlphaEncoder /// The global configuration. /// The memory manager. /// Indicates, if the data should be compressed with the lossless webp compression. - /// The alpha data. - public static byte[] EncodeAlpha(Image image, Configuration configuration, MemoryAllocator memoryAllocator, bool compress) + /// The size in bytes of the alpha data. + /// The encoded alpha data. + public static IMemoryOwner EncodeAlpha(Image image, Configuration configuration, MemoryAllocator memoryAllocator, bool compress, out int size) where TPixel : unmanaged, IPixel { - byte[] alphaData = ExtractAlphaChannel(image, configuration, memoryAllocator); int width = image.Width; int height = image.Height; + IMemoryOwner alphaData = ExtractAlphaChannel(image, configuration, memoryAllocator); + if (compress) { WebpEncodingMethod effort = WebpEncodingMethod.Default; @@ -49,11 +51,14 @@ public static byte[] EncodeAlpha(Image image, Configuration conf // The transparency information will be stored in the green channel of the ARGB quadruplet. // The green channel is allowed extra transformation steps in the specification -- unlike the other channels, // that can improve compression. - using Image alphaAsImage = DispatchAlphaToGreen(image, alphaData); + using Image alphaAsImage = DispatchAlphaToGreen(image, alphaData.GetSpan()); + + size = lossLessEncoder.EncodeAlphaImageData(alphaAsImage, alphaData); - return lossLessEncoder.EncodeAlphaImageData(alphaAsImage); + return alphaData; } + size = width * height; return alphaData; } @@ -64,7 +69,7 @@ public static byte[] EncodeAlpha(Image image, Configuration conf /// The to encode from. /// A byte sequence of length width * height, containing all the 8-bit transparency values in scan order. /// The transparency image. - private static Image DispatchAlphaToGreen(Image image, byte[] alphaData) + private static Image DispatchAlphaToGreen(Image image, Span alphaData) where TPixel : unmanaged, IPixel { int width = image.Width; @@ -75,7 +80,7 @@ private static Image DispatchAlphaToGreen(Image image, b { Memory rowBuffer = alphaAsImage.DangerousGetPixelRowMemory(y); Span pixelRow = rowBuffer.Span; - Span alphaRow = alphaData.AsSpan(y * width, width); + Span alphaRow = alphaData.Slice(y * width, width); for (int x = 0; x < width; x++) { // Leave A/R/B channels zero'd. @@ -94,13 +99,14 @@ private static Image DispatchAlphaToGreen(Image image, b /// The global configuration. /// The memory manager. /// A byte sequence of length width * height, containing all the 8-bit transparency values in scan order. - private static byte[] ExtractAlphaChannel(Image image, Configuration configuration, MemoryAllocator memoryAllocator) + private static IMemoryOwner ExtractAlphaChannel(Image image, Configuration configuration, MemoryAllocator memoryAllocator) where TPixel : unmanaged, IPixel { Buffer2D imageBuffer = image.Frames.RootFrame.PixelBuffer; int height = image.Height; int width = image.Width; - byte[] alphaData = new byte[width * height]; + IMemoryOwner alphaDataBuffer = memoryAllocator.Allocate(width * height); + Span alphaData = alphaDataBuffer.GetSpan(); using IMemoryOwner rowBuffer = memoryAllocator.Allocate(width); Span rgbaRow = rowBuffer.GetSpan(); @@ -116,7 +122,7 @@ private static byte[] ExtractAlphaChannel(Image image, Configura } } - return alphaData; + return alphaDataBuffer; } } } diff --git a/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs b/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs index 84c9d3f133..fc1accfdee 100644 --- a/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs +++ b/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs @@ -47,6 +47,12 @@ internal abstract class BitWriterBase /// The stream to write to. public void WriteToStream(Stream stream) => stream.Write(this.Buffer.AsSpan(0, this.NumBytes())); + /// + /// Writes the encoded bytes of the image to the given buffer. Call Finish() before this. + /// + /// The destination buffer. + public void WriteToBuffer(Span dest) => this.Buffer.AsSpan(0, this.NumBytes()).CopyTo(dest); + /// /// Resizes the buffer to write to. /// @@ -108,7 +114,7 @@ protected uint MetadataChunkSize(byte[] metadataBytes) /// /// The alpha chunk bytes. /// The alpha data chunk size in bytes. - protected uint AlphaChunkSize(byte[] alphaBytes) + protected uint AlphaChunkSize(Span alphaBytes) { uint alphaSize = (uint)alphaBytes.Length + 1; uint alphaChunkSize = WebpConstants.ChunkHeaderSize + alphaSize + (alphaSize & 1); @@ -147,10 +153,8 @@ protected void WriteMetadataProfile(Stream stream, byte[] metadataBytes, WebpChu /// The stream to write to. /// The alpha channel data bytes. /// Indicates, if the alpha channel data is compressed. - protected void WriteAlphaChunk(Stream stream, byte[] dataBytes, bool alphaDataIsCompressed) + protected void WriteAlphaChunk(Stream stream, Span dataBytes, bool alphaDataIsCompressed) { - DebugGuard.NotNull(dataBytes, nameof(dataBytes)); - uint size = (uint)dataBytes.Length + 1; Span buf = this.scratchBuffer.AsSpan(0, 4); BinaryPrimitives.WriteUInt32BigEndian(buf, (uint)WebpChunkType.Alpha); diff --git a/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs b/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs index 577a87e6a1..fa6e09d875 100644 --- a/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs +++ b/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs @@ -411,7 +411,15 @@ private void Flush() /// Flag indicating, if a alpha channel is present. /// The alpha channel data. /// Indicates, if the alpha data is compressed. - public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, XmpProfile xmpProfile, uint width, uint height, bool hasAlpha, byte[] alphaData, bool alphaDataIsCompressed) + public void WriteEncodedImageToStream( + Stream stream, + ExifProfile exifProfile, + XmpProfile xmpProfile, + uint width, + uint height, + bool hasAlpha, + Span alphaData, + bool alphaDataIsCompressed) { bool isVp8X = false; byte[] exifBytes = null; @@ -661,7 +669,7 @@ private void WriteWebpHeaders( ExifProfile exifProfile, XmpProfile xmpProfile, bool hasAlpha, - byte[] alphaData, + Span alphaData, bool alphaDataIsCompressed) { this.WriteRiffHeader(stream, riffSize); diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs index 797d0794f9..ece9aefd0f 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs @@ -263,8 +263,9 @@ public void Encode(Image image, Stream stream) /// /// The type of the pixel. /// The to encode from. - /// The encoded alpha stream. - public byte[] EncodeAlphaImageData(Image image) + /// The destination buffer to write the encoded alpha data to. + /// The size of the data in bytes. + public int EncodeAlphaImageData(Image image, IMemoryOwner alphaData) where TPixel : unmanaged, IPixel { int width = image.Width; @@ -273,12 +274,12 @@ public byte[] EncodeAlphaImageData(Image image) // Convert image pixels to bgra array. this.ConvertPixelsToBgra(image, width, height); - // The image-stream does NOT contain any headers describing the image dimension, the dimension is already known. + // The image-stream will NOT contain any headers describing the image dimension, the dimension is already known. this.EncodeStream(image); this.bitWriter.Finish(); - using var ms = new MemoryStream(); - this.bitWriter.WriteToStream(ms); - return ms.ToArray(); + int size = this.bitWriter.NumBytes(); + this.bitWriter.WriteToBuffer(alphaData.GetSpan()); + return size; } /// diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs index d8bd8f759c..4b7f3f5c88 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs @@ -241,6 +241,11 @@ public Vp8Encoder( public int DqUvDc { get; private set; } + /// + /// Gets or sets the alpha data. + /// + private IMemoryOwner AlphaData { get; set; } + /// /// Gets the luma component. /// @@ -322,12 +327,12 @@ public void Encode(Image image, Stream stream) int expectedSize = this.Mbw * this.Mbh * averageBytesPerMacroBlock; this.bitWriter = new Vp8BitWriter(expectedSize, this); - // Extract and encode alpha data, if present. - byte[] alphaData = null; + // Extract and encode alpha channel data, if present. + int alphaDataSize = 0; if (hasAlpha) { // TODO: This can potentially run in an separate task. - alphaData = AlphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.alphaCompression); + this.AlphaData = AlphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.alphaCompression, out alphaDataSize); } // Stats-collection loop. @@ -363,7 +368,15 @@ public void Encode(Image image, Stream stream) // Write bytes from the bitwriter buffer to the stream. ImageMetadata metadata = image.Metadata; metadata.SyncProfiles(); - this.bitWriter.WriteEncodedImageToStream(stream, metadata.ExifProfile, metadata.XmpProfile, (uint)width, (uint)height, hasAlpha, alphaData, this.alphaCompression); + this.bitWriter.WriteEncodedImageToStream( + stream, + metadata.ExifProfile, + metadata.XmpProfile, + (uint)width, + (uint)height, + hasAlpha, + hasAlpha ? this.AlphaData.GetSpan().Slice(0, alphaDataSize) : Span.Empty, + this.alphaCompression); } /// @@ -372,6 +385,7 @@ public void Dispose() this.Y.Dispose(); this.U.Dispose(); this.V.Dispose(); + this.AlphaData?.Dispose(); } /// From b12ad7596e3e1b5c91d87332f167afe340f65226 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 1 Feb 2022 12:50:28 +0100 Subject: [PATCH 6/8] Leave alpha data uncompressed, if compression does not yield in smaller data --- src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs | 11 ++++++++++- src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs | 9 ++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs index ece9aefd0f..30d65562ae 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs @@ -264,12 +264,15 @@ public void Encode(Image image, Stream stream) /// The type of the pixel. /// The to encode from. /// The destination buffer to write the encoded alpha data to. - /// The size of the data in bytes. + /// The size of the compressed data in bytes. + /// If the size of the data is the same as the pixel count, the compression would not yield in smaller data and is left uncompressed. + /// public int EncodeAlphaImageData(Image image, IMemoryOwner alphaData) where TPixel : unmanaged, IPixel { int width = image.Width; int height = image.Height; + int pixelCount = width * height; // Convert image pixels to bgra array. this.ConvertPixelsToBgra(image, width, height); @@ -278,6 +281,12 @@ public int EncodeAlphaImageData(Image image, IMemoryOwner this.EncodeStream(image); this.bitWriter.Finish(); int size = this.bitWriter.NumBytes(); + if (size >= pixelCount) + { + // Compressing would not yield in smaller data -> leave the data uncompressed. + return pixelCount; + } + this.bitWriter.WriteToBuffer(alphaData.GetSpan()); return size; } diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs index 4b7f3f5c88..60bdee362c 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs @@ -302,6 +302,7 @@ public void Encode(Image image, Stream stream) { int width = image.Width; int height = image.Height; + int pixelCount = width * height; Span y = this.Y.GetSpan(); Span u = this.U.GetSpan(); Span v = this.V.GetSpan(); @@ -329,10 +330,16 @@ public void Encode(Image image, Stream stream) // Extract and encode alpha channel data, if present. int alphaDataSize = 0; + bool alphaCompressionSucceeded = false; if (hasAlpha) { // TODO: This can potentially run in an separate task. this.AlphaData = AlphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.alphaCompression, out alphaDataSize); + if (alphaDataSize < pixelCount) + { + // Only use compressed data, if the compressed data is actually smaller then the uncompressed data. + alphaCompressionSucceeded = true; + } } // Stats-collection loop. @@ -376,7 +383,7 @@ public void Encode(Image image, Stream stream) (uint)height, hasAlpha, hasAlpha ? this.AlphaData.GetSpan().Slice(0, alphaDataSize) : Span.Empty, - this.alphaCompression); + this.alphaCompression && alphaCompressionSucceeded); } /// From 192cfb03f9a35282eb62f99aa42c3bae91869181 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 1 Feb 2022 13:33:04 +0100 Subject: [PATCH 7/8] Move disposing the alpha data to the AlphaEncoder --- src/ImageSharp/Formats/Webp/AlphaEncoder.cs | 19 ++++++++++++------- .../Formats/Webp/Lossy/Vp8Encoder.cs | 13 +++++-------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/AlphaEncoder.cs b/src/ImageSharp/Formats/Webp/AlphaEncoder.cs index 38497281ff..1019073d87 100644 --- a/src/ImageSharp/Formats/Webp/AlphaEncoder.cs +++ b/src/ImageSharp/Formats/Webp/AlphaEncoder.cs @@ -13,8 +13,10 @@ namespace SixLabors.ImageSharp.Formats.Webp /// /// Methods for encoding the alpha data of a VP8 image. /// - internal static class AlphaEncoder + internal class AlphaEncoder : IDisposable { + private IMemoryOwner alphaData; + /// /// Encodes the alpha channel data. /// Data is either compressed as lossless webp image or uncompressed. @@ -26,12 +28,12 @@ internal static class AlphaEncoder /// Indicates, if the data should be compressed with the lossless webp compression. /// The size in bytes of the alpha data. /// The encoded alpha data. - public static IMemoryOwner EncodeAlpha(Image image, Configuration configuration, MemoryAllocator memoryAllocator, bool compress, out int size) + public IMemoryOwner EncodeAlpha(Image image, Configuration configuration, MemoryAllocator memoryAllocator, bool compress, out int size) where TPixel : unmanaged, IPixel { int width = image.Width; int height = image.Height; - IMemoryOwner alphaData = ExtractAlphaChannel(image, configuration, memoryAllocator); + this.alphaData = ExtractAlphaChannel(image, configuration, memoryAllocator); if (compress) { @@ -51,15 +53,15 @@ public static IMemoryOwner EncodeAlpha(Image image, Config // The transparency information will be stored in the green channel of the ARGB quadruplet. // The green channel is allowed extra transformation steps in the specification -- unlike the other channels, // that can improve compression. - using Image alphaAsImage = DispatchAlphaToGreen(image, alphaData.GetSpan()); + using Image alphaAsImage = DispatchAlphaToGreen(image, this.alphaData.GetSpan()); - size = lossLessEncoder.EncodeAlphaImageData(alphaAsImage, alphaData); + size = lossLessEncoder.EncodeAlphaImageData(alphaAsImage, this.alphaData); - return alphaData; + return this.alphaData; } size = width * height; - return alphaData; + return this.alphaData; } /// @@ -124,5 +126,8 @@ private static IMemoryOwner ExtractAlphaChannel(Image imag return alphaDataBuffer; } + + /// + public void Dispose() => this.alphaData?.Dispose(); } } diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs index 60bdee362c..927b04c0cf 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs @@ -241,11 +241,6 @@ public Vp8Encoder( public int DqUvDc { get; private set; } - /// - /// Gets or sets the alpha data. - /// - private IMemoryOwner AlphaData { get; set; } - /// /// Gets the luma component. /// @@ -331,10 +326,13 @@ public void Encode(Image image, Stream stream) // Extract and encode alpha channel data, if present. int alphaDataSize = 0; bool alphaCompressionSucceeded = false; + using var alphaEncoder = new AlphaEncoder(); + Span alphaData = Span.Empty; if (hasAlpha) { // TODO: This can potentially run in an separate task. - this.AlphaData = AlphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.alphaCompression, out alphaDataSize); + IMemoryOwner encodedAlphaData = alphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.alphaCompression, out alphaDataSize); + alphaData = encodedAlphaData.GetSpan(); if (alphaDataSize < pixelCount) { // Only use compressed data, if the compressed data is actually smaller then the uncompressed data. @@ -382,7 +380,7 @@ public void Encode(Image image, Stream stream) (uint)width, (uint)height, hasAlpha, - hasAlpha ? this.AlphaData.GetSpan().Slice(0, alphaDataSize) : Span.Empty, + alphaData, this.alphaCompression && alphaCompressionSucceeded); } @@ -392,7 +390,6 @@ public void Dispose() this.Y.Dispose(); this.U.Dispose(); this.V.Dispose(); - this.AlphaData?.Dispose(); } /// From 2491b6ab626f4329a40b9363e4058d5857d8567d Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 1 Feb 2022 16:16:35 +0100 Subject: [PATCH 8/8] Change AverageBytesPerMb to ReadOnlySpan --- src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs index 927b04c0cf..695359e5ea 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs @@ -73,8 +73,6 @@ internal class Vp8Encoder : IDisposable private readonly bool alphaCompression; - private readonly byte[] averageBytesPerMb = { 50, 24, 16, 9, 7, 5, 3, 2 }; - private const int NumMbSegments = 4; private const int MaxItersKMeans = 6; @@ -174,6 +172,9 @@ public Vp8Encoder( this.ResetBoundaryPredictions(); } + // This uses C#'s optimization to refer to the static data segment of the assembly, no allocation occurs. + private static ReadOnlySpan AverageBytesPerMb => new byte[] { 50, 24, 16, 9, 7, 5, 3, 2 }; + public int BaseQuant { get; set; } /// @@ -319,7 +320,7 @@ public void Encode(Image image, Stream stream) this.SetLoopParams(this.quality); // Initialize the bitwriter. - int averageBytesPerMacroBlock = this.averageBytesPerMb[this.BaseQuant >> 4]; + int averageBytesPerMacroBlock = AverageBytesPerMb[this.BaseQuant >> 4]; int expectedSize = this.Mbw * this.Mbh * averageBytesPerMacroBlock; this.bitWriter = new Vp8BitWriter(expectedSize, this);