Browse Source

Use TransparentColorMode enum

pull/1552/head
Brian Popow 5 years ago
parent
commit
d72fbb5783
  1. 4
      src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs
  2. 18
      src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs
  3. 11
      src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs
  4. 2
      src/ImageSharp/Formats/Webp/WebpEncoder.cs
  5. 6
      src/ImageSharp/Formats/Webp/WebpEncoderCore.cs
  6. 21
      src/ImageSharp/Formats/Webp/WebpTransparentColorMode.cs
  7. 4
      tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs

4
src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs

@ -59,9 +59,9 @@ namespace SixLabors.ImageSharp.Formats.Webp
/// <summary> /// <summary>
/// Gets a value indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible /// Gets a value indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible
/// RGB information for better compression. /// RGB information for better compression.
/// The default value is false. /// The default value is Clear.
/// </summary> /// </summary>
bool Exact { get; } WebpTransparentColorMode TransparentColorMode { get; }
/// <summary> /// <summary>
/// Gets a value indicating whether near lossless mode should be used. /// Gets a value indicating whether near lossless mode should be used.

18
src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs

@ -43,7 +43,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
Span<uint> image, Span<uint> image,
bool nearLossless, bool nearLossless,
int nearLosslessQuality, int nearLosslessQuality,
bool exact, WebpTransparentColorMode transparentColorMode,
bool usedSubtractGreen, bool usedSubtractGreen,
bool lowEffort) bool lowEffort)
{ {
@ -81,7 +81,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
bgraScratch, bgraScratch,
bgra, bgra,
maxQuantization, maxQuantization,
exact, transparentColorMode,
usedSubtractGreen, usedSubtractGreen,
nearLossless, nearLossless,
image); image);
@ -99,7 +99,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
bgraScratch, bgraScratch,
bgra, bgra,
maxQuantization, maxQuantization,
exact, transparentColorMode,
usedSubtractGreen, usedSubtractGreen,
nearLossless, nearLossless,
lowEffort); lowEffort);
@ -189,7 +189,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
Span<uint> argbScratch, Span<uint> argbScratch,
Span<uint> argb, Span<uint> argb,
int maxQuantization, int maxQuantization,
bool exact, WebpTransparentColorMode transparentColorMode,
bool usedSubtractGreen, bool usedSubtractGreen,
bool nearLossless, bool nearLossless,
Span<uint> modes) Span<uint> 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) for (int relativeX = 0; relativeX < maxX; ++relativeX)
{ {
UpdateHisto(histoArgb, residuals[relativeX]); UpdateHisto(histoArgb, residuals[relativeX]);
@ -330,12 +330,12 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
int xEnd, int xEnd,
int y, int y,
int maxQuantization, int maxQuantization,
bool exact, WebpTransparentColorMode transparentColorMode,
bool usedSubtractGreen, bool usedSubtractGreen,
bool nearLossless, bool nearLossless,
Span<uint> output) Span<uint> output)
{ {
if (exact) if (transparentColorMode == WebpTransparentColorMode.Preserve)
{ {
PredictBatch(mode, xStart, y, xEnd - xStart, currentRowSpan, upperRowSpan, output); PredictBatch(mode, xStart, y, xEnd - xStart, currentRowSpan, upperRowSpan, output);
} }
@ -568,7 +568,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
Span<uint> argbScratch, Span<uint> argbScratch,
Span<uint> argb, Span<uint> argb,
int maxQuantization, int maxQuantization,
bool exact, WebpTransparentColorMode transparentColorMode,
bool usedSubtractGreen, bool usedSubtractGreen,
bool nearLossless, bool nearLossless,
bool lowEffort) bool lowEffort)
@ -631,7 +631,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
xEnd, xEnd,
y, y,
maxQuantization, maxQuantization,
exact, transparentColorMode,
usedSubtractGreen, usedSubtractGreen,
nearLossless, nearLossless,
argb.Slice((y * width) + x)); argb.Slice((y * width) + x));

11
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 /// Flag indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible
/// RGB information for better compression. /// RGB information for better compression.
/// </summary> /// </summary>
private readonly bool exact; private readonly WebpTransparentColorMode transparentColorMode;
/// <summary> /// <summary>
/// Indicating whether near lossless mode should be used. /// Indicating whether near lossless mode should be used.
@ -85,7 +85,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
/// <param name="height">The height of the input image.</param> /// <param name="height">The height of the input image.</param>
/// <param name="quality">The encoding quality.</param> /// <param name="quality">The encoding quality.</param>
/// <param name="method">Quality/speed trade-off (0=fast, 6=slower-better).</param> /// <param name="method">Quality/speed trade-off (0=fast, 6=slower-better).</param>
/// <param name="exact">Flag indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible RGB information for better compression.</param> /// <param name="transparentColorMode">Flag indicating whether to preserve the exact RGB values under transparent area.
/// Otherwise, discard this invisible RGB information for better compression.</param>
/// <param name="nearLossless">Indicating whether near lossless mode should be used.</param> /// <param name="nearLossless">Indicating whether near lossless mode should be used.</param>
/// <param name="nearLosslessQuality">The near lossless quality. The range is 0 (maximum preprocessing) to 100 (no preprocessing, the default).</param> /// <param name="nearLosslessQuality">The near lossless quality. The range is 0 (maximum preprocessing) to 100 (no preprocessing, the default).</param>
public Vp8LEncoder( public Vp8LEncoder(
@ -95,7 +96,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
int height, int height,
int quality, int quality,
WebpEncodingMethod method, WebpEncodingMethod method,
bool exact, WebpTransparentColorMode transparentColorMode,
bool nearLossless, bool nearLossless,
int nearLosslessQuality) int nearLosslessQuality)
{ {
@ -106,7 +107,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
this.configuration = configuration; this.configuration = configuration;
this.quality = Numerics.Clamp(quality, 0, 100); this.quality = Numerics.Clamp(quality, 0, 100);
this.method = method; this.method = method;
this.exact = exact; this.transparentColorMode = transparentColorMode;
this.nearLossless = nearLossless; this.nearLossless = nearLossless;
this.nearLosslessQuality = Numerics.Clamp(nearLosslessQuality, 0, 100); this.nearLosslessQuality = Numerics.Clamp(nearLosslessQuality, 0, 100);
this.bitWriter = new Vp8LBitWriter(initialSize); this.bitWriter = new Vp8LBitWriter(initialSize);
@ -676,7 +677,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
this.TransformData.GetSpan(), this.TransformData.GetSpan(),
this.nearLossless, this.nearLossless,
nearLosslessStrength, nearLosslessStrength,
this.exact, this.transparentColorMode,
this.UseSubtractGreenTransform, this.UseSubtractGreenTransform,
lowEffort); lowEffort);

2
src/ImageSharp/Formats/Webp/WebpEncoder.cs

@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
public int FilterStrength { get; set; } = 60; public int FilterStrength { get; set; } = 60;
/// <inheritdoc/> /// <inheritdoc/>
public bool Exact { get; set; } public WebpTransparentColorMode TransparentColorMode { get; set; } = WebpTransparentColorMode.Clear;
/// <inheritdoc/> /// <inheritdoc/>
public bool NearLossless { get; set; } public bool NearLossless { get; set; }

6
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 /// Flag indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible
/// RGB information for better compression. /// RGB information for better compression.
/// </summary> /// </summary>
private readonly bool exact; private readonly WebpTransparentColorMode transparentColorMode;
/// <summary> /// <summary>
/// Indicating whether near lossless mode should be used. /// Indicating whether near lossless mode should be used.
@ -93,7 +93,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
this.entropyPasses = options.EntropyPasses; this.entropyPasses = options.EntropyPasses;
this.spatialNoiseShaping = options.SpatialNoiseShaping; this.spatialNoiseShaping = options.SpatialNoiseShaping;
this.filterStrength = options.FilterStrength; this.filterStrength = options.FilterStrength;
this.exact = options.Exact; this.transparentColorMode = options.TransparentColorMode;
this.nearLossless = options.NearLossless; this.nearLossless = options.NearLossless;
this.nearLosslessQuality = options.NearLosslessQuality; this.nearLosslessQuality = options.NearLosslessQuality;
} }
@ -136,7 +136,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
image.Height, image.Height,
this.quality, this.quality,
this.method, this.method,
this.exact, this.transparentColorMode,
this.nearLossless, this.nearLossless,
this.nearLosslessQuality); this.nearLosslessQuality);
enc.Encode(image, stream); enc.Encode(image, stream);

21
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
{
/// <summary>
/// Enum indicating how the transparency should be handled on encoding.
/// </summary>
public enum WebpTransparentColorMode
{
/// <summary>
/// Discard the transparency information for better compression.
/// </summary>
Clear = 0,
/// <summary>
/// The transparency will be kept as is.
/// </summary>
Preserve = 1,
}
}

4
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, 5)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 6)] [WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 6)]
[WithFile(Lossy.Alpha1, PixelTypes.Rgba32, 4)] [WithFile(Lossy.Alpha1, PixelTypes.Rgba32, 4)]
public void Encode_Lossless_WithExactFlag_Works<TPixel>(TestImageProvider<TPixel> provider, WebpEncodingMethod method) public void Encode_Lossless_WithPreserveTransparentColor_Works<TPixel>(TestImageProvider<TPixel> provider, WebpEncodingMethod method)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
{ {
Lossy = false, Lossy = false,
Method = method, Method = method,
Exact = true TransparentColorMode = WebpTransparentColorMode.Preserve
}; };
using Image<TPixel> image = provider.GetImage(); using Image<TPixel> image = provider.GetImage();

Loading…
Cancel
Save