Browse Source

Use TransparentColorMode enum

pull/1552/head
Brian Popow 4 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>
/// 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.
/// </summary>
bool Exact { get; }
WebpTransparentColorMode TransparentColorMode { get; }
/// <summary>
/// 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,
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<uint> argbScratch,
Span<uint> argb,
int maxQuantization,
bool exact,
WebpTransparentColorMode transparentColorMode,
bool usedSubtractGreen,
bool nearLossless,
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)
{
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<uint> 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<uint> argbScratch,
Span<uint> 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));

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
/// RGB information for better compression.
/// </summary>
private readonly bool exact;
private readonly WebpTransparentColorMode transparentColorMode;
/// <summary>
/// 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="quality">The encoding quality.</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="nearLosslessQuality">The near lossless quality. The range is 0 (maximum preprocessing) to 100 (no preprocessing, the default).</param>
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);

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

@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
public int FilterStrength { get; set; } = 60;
/// <inheritdoc/>
public bool Exact { get; set; }
public WebpTransparentColorMode TransparentColorMode { get; set; } = WebpTransparentColorMode.Clear;
/// <inheritdoc/>
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
/// RGB information for better compression.
/// </summary>
private readonly bool exact;
private readonly WebpTransparentColorMode transparentColorMode;
/// <summary>
/// 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);

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, 6)]
[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>
{
var encoder = new WebpEncoder()
{
Lossy = false,
Method = method,
Exact = true
TransparentColorMode = WebpTransparentColorMode.Preserve
};
using Image<TPixel> image = provider.GetImage();

Loading…
Cancel
Save