From d72fbb5783271ca094997ec30b1d73d25ed598e6 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Wed, 20 Oct 2021 18:51:58 +0200 Subject: [PATCH] Use TransparentColorMode enum --- .../Formats/Webp/IWebpEncoderOptions.cs | 4 ++-- .../Formats/Webp/Lossless/PredictorEncoder.cs | 18 ++++++++-------- .../Formats/Webp/Lossless/Vp8LEncoder.cs | 11 +++++----- src/ImageSharp/Formats/Webp/WebpEncoder.cs | 2 +- .../Formats/Webp/WebpEncoderCore.cs | 6 +++--- .../Formats/Webp/WebpTransparentColorMode.cs | 21 +++++++++++++++++++ .../Formats/WebP/WebpEncoderTests.cs | 4 ++-- 7 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 src/ImageSharp/Formats/Webp/WebpTransparentColorMode.cs diff --git a/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs b/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs index 6abf34483..63d76a598 100644 --- a/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs +++ b/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs @@ -59,9 +59,9 @@ namespace SixLabors.ImageSharp.Formats.Webp /// /// Gets a value indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible /// RGB information for better compression. - /// The default value is false. + /// The default value is Clear. /// - bool Exact { get; } + WebpTransparentColorMode TransparentColorMode { get; } /// /// Gets a value indicating whether near lossless mode should be used. diff --git a/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs index 0858a3d3f..671e9a043 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs @@ -43,7 +43,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless Span image, bool nearLossless, int nearLosslessQuality, - bool exact, + WebpTransparentColorMode transparentColorMode, bool usedSubtractGreen, bool lowEffort) { @@ -81,7 +81,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless bgraScratch, bgra, maxQuantization, - exact, + transparentColorMode, usedSubtractGreen, nearLossless, image); @@ -99,7 +99,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless bgraScratch, bgra, maxQuantization, - exact, + transparentColorMode, usedSubtractGreen, nearLossless, lowEffort); @@ -189,7 +189,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless Span argbScratch, Span argb, int maxQuantization, - bool exact, + WebpTransparentColorMode transparentColorMode, bool usedSubtractGreen, bool nearLossless, Span modes) @@ -272,7 +272,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless } } - GetResidual(width, height, upperRow, currentRow, maxDiffs, mode, startX, startX + maxX, y, maxQuantization, exact, usedSubtractGreen, nearLossless, residuals); + GetResidual(width, height, upperRow, currentRow, maxDiffs, mode, startX, startX + maxX, y, maxQuantization, transparentColorMode, usedSubtractGreen, nearLossless, residuals); for (int relativeX = 0; relativeX < maxX; ++relativeX) { UpdateHisto(histoArgb, residuals[relativeX]); @@ -330,12 +330,12 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless int xEnd, int y, int maxQuantization, - bool exact, + WebpTransparentColorMode transparentColorMode, bool usedSubtractGreen, bool nearLossless, Span output) { - if (exact) + if (transparentColorMode == WebpTransparentColorMode.Preserve) { PredictBatch(mode, xStart, y, xEnd - xStart, currentRowSpan, upperRowSpan, output); } @@ -568,7 +568,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless Span argbScratch, Span argb, int maxQuantization, - bool exact, + WebpTransparentColorMode transparentColorMode, bool usedSubtractGreen, bool nearLossless, bool lowEffort) @@ -631,7 +631,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless xEnd, y, maxQuantization, - exact, + transparentColorMode, usedSubtractGreen, nearLossless, argb.Slice((y * width) + x)); diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs index 2ebd3a270..693585637 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs @@ -58,7 +58,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless /// Flag indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible /// RGB information for better compression. /// - private readonly bool exact; + private readonly WebpTransparentColorMode transparentColorMode; /// /// Indicating whether near lossless mode should be used. @@ -85,7 +85,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless /// The height of the input image. /// The encoding quality. /// Quality/speed trade-off (0=fast, 6=slower-better). - /// Flag indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible RGB information for better compression. + /// Flag indicating whether to preserve the exact RGB values under transparent area. + /// Otherwise, discard this invisible RGB information for better compression. /// Indicating whether near lossless mode should be used. /// The near lossless quality. The range is 0 (maximum preprocessing) to 100 (no preprocessing, the default). public Vp8LEncoder( @@ -95,7 +96,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless int height, int quality, WebpEncodingMethod method, - bool exact, + WebpTransparentColorMode transparentColorMode, bool nearLossless, int nearLosslessQuality) { @@ -106,7 +107,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless this.configuration = configuration; this.quality = Numerics.Clamp(quality, 0, 100); this.method = method; - this.exact = exact; + this.transparentColorMode = transparentColorMode; this.nearLossless = nearLossless; this.nearLosslessQuality = Numerics.Clamp(nearLosslessQuality, 0, 100); this.bitWriter = new Vp8LBitWriter(initialSize); @@ -676,7 +677,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless this.TransformData.GetSpan(), this.nearLossless, nearLosslessStrength, - this.exact, + this.transparentColorMode, this.UseSubtractGreenTransform, lowEffort); diff --git a/src/ImageSharp/Formats/Webp/WebpEncoder.cs b/src/ImageSharp/Formats/Webp/WebpEncoder.cs index 1eb7b3846..1b1dab784 100644 --- a/src/ImageSharp/Formats/Webp/WebpEncoder.cs +++ b/src/ImageSharp/Formats/Webp/WebpEncoder.cs @@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.Formats.Webp public int FilterStrength { get; set; } = 60; /// - public bool Exact { get; set; } + public WebpTransparentColorMode TransparentColorMode { get; set; } = WebpTransparentColorMode.Clear; /// public bool NearLossless { get; set; } diff --git a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs index 57c696c96..ff0246cdd 100644 --- a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs @@ -61,7 +61,7 @@ namespace SixLabors.ImageSharp.Formats.Webp /// Flag indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible /// RGB information for better compression. /// - private readonly bool exact; + private readonly WebpTransparentColorMode transparentColorMode; /// /// Indicating whether near lossless mode should be used. @@ -93,7 +93,7 @@ namespace SixLabors.ImageSharp.Formats.Webp this.entropyPasses = options.EntropyPasses; this.spatialNoiseShaping = options.SpatialNoiseShaping; this.filterStrength = options.FilterStrength; - this.exact = options.Exact; + this.transparentColorMode = options.TransparentColorMode; this.nearLossless = options.NearLossless; this.nearLosslessQuality = options.NearLosslessQuality; } @@ -136,7 +136,7 @@ namespace SixLabors.ImageSharp.Formats.Webp image.Height, this.quality, this.method, - this.exact, + this.transparentColorMode, this.nearLossless, this.nearLosslessQuality); enc.Encode(image, stream); diff --git a/src/ImageSharp/Formats/Webp/WebpTransparentColorMode.cs b/src/ImageSharp/Formats/Webp/WebpTransparentColorMode.cs new file mode 100644 index 000000000..993033b80 --- /dev/null +++ b/src/ImageSharp/Formats/Webp/WebpTransparentColorMode.cs @@ -0,0 +1,21 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.Formats.Webp +{ + /// + /// Enum indicating how the transparency should be handled on encoding. + /// + public enum WebpTransparentColorMode + { + /// + /// Discard the transparency information for better compression. + /// + Clear = 0, + + /// + /// The transparency will be kept as is. + /// + Preserve = 1, + } +} diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs index 6a0e599d0..70bf3e66c 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs @@ -111,14 +111,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp [WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 5)] [WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 6)] [WithFile(Lossy.Alpha1, PixelTypes.Rgba32, 4)] - public void Encode_Lossless_WithExactFlag_Works(TestImageProvider provider, WebpEncodingMethod method) + public void Encode_Lossless_WithPreserveTransparentColor_Works(TestImageProvider provider, WebpEncodingMethod method) where TPixel : unmanaged, IPixel { var encoder = new WebpEncoder() { Lossy = false, Method = method, - Exact = true + TransparentColorMode = WebpTransparentColorMode.Preserve }; using Image image = provider.GetImage();