Browse Source

Refactored IsSolidBrushWithoutBlending into GraphicsOptions so it can be called from more places, and also allows for specific tests.

af/merge-core
Vicente Penades 8 years ago
parent
commit
3eb0a73081
  1. 23
      src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor.cs
  2. 89
      src/ImageSharp/GraphicsOptions.cs
  3. 11
      tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs

23
src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor.cs

@ -109,28 +109,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Drawing
return false;
}
if (this.options.ColorBlendingMode != PixelColorBlendingMode.Normal)
{
return false;
}
if (this.options.AlphaCompositionMode != PixelAlphaCompositionMode.SrcOver &&
this.options.AlphaCompositionMode != PixelAlphaCompositionMode.Src)
{
return false;
}
if (this.options.BlendPercentage != 1f)
{
return false;
}
if (solidBrush.Color.ToVector4().W != 1f)
{
return false;
}
return true;
return this.options.IsOpaqueColorWithoutBlending(solidBrush.Color);
}
}
}

89
src/ImageSharp/GraphicsOptions.cs

@ -36,6 +36,57 @@ namespace SixLabors.ImageSharp
this.blendPercentage = 1;
this.antialiasSubpixelDepth = 16;
this.antialias = enableAntialiasing;
}
/// <summary>
/// Initializes a new instance of the <see cref="GraphicsOptions"/> struct.
/// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
/// <param name="opacity">blending percentage to apply to the drawing operation</param>
public GraphicsOptions(bool enableAntialiasing, float opacity)
{
Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity));
this.colorBlendingMode = PixelColorBlendingMode.Normal;
this.alphaCompositionMode = PixelAlphaCompositionMode.SrcOver;
this.blendPercentage = opacity;
this.antialiasSubpixelDepth = 16;
this.antialias = enableAntialiasing;
}
/// <summary>
/// Initializes a new instance of the <see cref="GraphicsOptions"/> struct.
/// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
/// <param name="opacity">blending percentage to apply to the drawing operation</param>
/// <param name="blending">color blending mode to apply to the drawing operation</param>
public GraphicsOptions(bool enableAntialiasing, PixelColorBlendingMode blending, float opacity)
{
Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity));
this.colorBlendingMode = blending;
this.alphaCompositionMode = PixelAlphaCompositionMode.SrcOver;
this.blendPercentage = opacity;
this.antialiasSubpixelDepth = 16;
this.antialias = enableAntialiasing;
}
/// <summary>
/// Initializes a new instance of the <see cref="GraphicsOptions"/> struct.
/// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
/// <param name="opacity">blending percentage to apply to the drawing operation</param>
/// <param name="blending">color blending mode to apply to the drawing operation</param>
/// <param name="composition">alpha composition mode to apply to the drawing operation</param>
public GraphicsOptions(bool enableAntialiasing, PixelColorBlendingMode blending, PixelAlphaCompositionMode composition, float opacity)
{
Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity));
this.colorBlendingMode = blending;
this.alphaCompositionMode = composition;
this.blendPercentage = opacity;
this.antialiasSubpixelDepth = 16;
this.antialias = enableAntialiasing;
}
/// <summary>
@ -85,6 +136,44 @@ namespace SixLabors.ImageSharp
{
get => this.alphaCompositionMode;
set => this.alphaCompositionMode = value;
}
/// <summary>
/// Evaluates if a given SOURCE color can completely replace a BACKDROP color given the current blending and composition settings.
/// </summary>
/// <typeparam name="TPixel">The pixel format</typeparam>
/// <param name="color">the color</param>
/// <returns>true if the color can be considered opaque</returns>
/// <remarks>
/// Blending and composition is an expensive operation, in some cases, like
/// filling with a solid color, the blending can be avoided by a plain color replacement.
/// This method can be useful for such processors to select the fast path.
/// </remarks>
internal bool IsOpaqueColorWithoutBlending<TPixel>(TPixel color)
where TPixel : struct, IPixel<TPixel>
{
if (this.ColorBlendingMode != PixelColorBlendingMode.Normal)
{
return false;
}
if (this.AlphaCompositionMode != PixelAlphaCompositionMode.SrcOver &&
this.AlphaCompositionMode != PixelAlphaCompositionMode.Src)
{
return false;
}
if (this.BlendPercentage != 1f)
{
return false;
}
if (color.ToVector4().W != 1f)
{
return false;
}
return true;
}
}
}

11
tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs

@ -88,6 +88,17 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats
where TPixel : struct, IPixel<TPixel>
{
Assert.NotNull(PixelOperations<TPixel>.Instance);
}
[Fact]
public void IsOpaqueColor()
{
Assert.True(new GraphicsOptions(true).IsOpaqueColorWithoutBlending(ImageSharp.PixelFormats.Rgba32.Red));
Assert.False(new GraphicsOptions(true, 0.5f).IsOpaqueColorWithoutBlending(ImageSharp.PixelFormats.Rgba32.Red));
Assert.False(new GraphicsOptions(true).IsOpaqueColorWithoutBlending(ImageSharp.PixelFormats.Rgba32.Transparent));
Assert.False(new GraphicsOptions(true, PixelColorBlendingMode.Lighten, 1).IsOpaqueColorWithoutBlending(ImageSharp.PixelFormats.Rgba32.Red));
Assert.False(new GraphicsOptions(true, PixelColorBlendingMode.Normal,PixelAlphaCompositionMode.DestOver, 1).IsOpaqueColorWithoutBlending(ImageSharp.PixelFormats.Rgba32.Red));
}
}

Loading…
Cancel
Save