From c7a64675b3dc5cefae3a7468e7b226db3613a46f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 5 Nov 2019 22:07:49 +1100 Subject: [PATCH] Fix GraphicsOptions --- .../Extensions/GraphicsOptionsExtensions.cs | 53 ++++++++++++++ src/ImageSharp/GraphicsOptions.cs | 72 ++++--------------- 2 files changed, 65 insertions(+), 60 deletions(-) create mode 100644 src/ImageSharp.Drawing/Common/Extensions/GraphicsOptionsExtensions.cs diff --git a/src/ImageSharp.Drawing/Common/Extensions/GraphicsOptionsExtensions.cs b/src/ImageSharp.Drawing/Common/Extensions/GraphicsOptionsExtensions.cs new file mode 100644 index 000000000..c32d0a46e --- /dev/null +++ b/src/ImageSharp.Drawing/Common/Extensions/GraphicsOptionsExtensions.cs @@ -0,0 +1,53 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Numerics; +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp +{ + /// + /// Extensions methods fpor the class. + /// + internal static class GraphicsOptionsExtensions + { + /// + /// Evaluates if a given SOURCE color can completely replace a BACKDROP color given the current blending and composition settings. + /// + /// The graphics options. + /// The source color. + /// true if the color can be considered opaque + /// + /// 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. + /// + public static bool IsOpaqueColorWithoutBlending(this GraphicsOptions options, Color color) + { + if (options.ColorBlendingMode != PixelColorBlendingMode.Normal) + { + return false; + } + + if (options.AlphaCompositionMode != PixelAlphaCompositionMode.SrcOver + && options.AlphaCompositionMode != PixelAlphaCompositionMode.Src) + { + return false; + } + + const float Opaque = 1F; + + if (options.BlendPercentage != Opaque) + { + return false; + } + + if (((Vector4)color).W != Opaque) + { + return false; + } + + return true; + } + } +} diff --git a/src/ImageSharp/GraphicsOptions.cs b/src/ImageSharp/GraphicsOptions.cs index 214b10810..644bde1cf 100644 --- a/src/ImageSharp/GraphicsOptions.cs +++ b/src/ImageSharp/GraphicsOptions.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using SixLabors.ImageSharp.PixelFormats; @@ -21,18 +21,14 @@ namespace SixLabors.ImageSharp private bool? antialias; - private PixelColorBlendingMode colorBlendingMode; - - private PixelAlphaCompositionMode alphaCompositionMode; - /// /// Initializes a new instance of the struct. /// /// If set to true [enable antialiasing]. public GraphicsOptions(bool enableAntialiasing) { - this.colorBlendingMode = PixelColorBlendingMode.Normal; - this.alphaCompositionMode = PixelAlphaCompositionMode.SrcOver; + this.ColorBlendingMode = PixelColorBlendingMode.Normal; + this.AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver; this.blendPercentage = 1; this.antialiasSubpixelDepth = 16; this.antialias = enableAntialiasing; @@ -47,8 +43,8 @@ namespace SixLabors.ImageSharp { Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity)); - this.colorBlendingMode = PixelColorBlendingMode.Normal; - this.alphaCompositionMode = PixelAlphaCompositionMode.SrcOver; + this.ColorBlendingMode = PixelColorBlendingMode.Normal; + this.AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver; this.blendPercentage = opacity; this.antialiasSubpixelDepth = 16; this.antialias = enableAntialiasing; @@ -64,8 +60,8 @@ namespace SixLabors.ImageSharp { Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity)); - this.colorBlendingMode = blending; - this.alphaCompositionMode = PixelAlphaCompositionMode.SrcOver; + this.ColorBlendingMode = blending; + this.AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver; this.blendPercentage = opacity; this.antialiasSubpixelDepth = 16; this.antialias = enableAntialiasing; @@ -82,8 +78,8 @@ namespace SixLabors.ImageSharp { Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity)); - this.colorBlendingMode = blending; - this.alphaCompositionMode = composition; + this.ColorBlendingMode = blending; + this.AlphaCompositionMode = composition; this.blendPercentage = opacity; this.antialiasSubpixelDepth = 16; this.antialias = enableAntialiasing; @@ -123,55 +119,11 @@ namespace SixLabors.ImageSharp /// /// Gets or sets a value indicating the color blending mode to apply to the drawing operation /// - public PixelColorBlendingMode ColorBlendingMode - { - get => this.colorBlendingMode; - set => this.colorBlendingMode = value; - } + public PixelColorBlendingMode ColorBlendingMode { get; set; } /// /// Gets or sets a value indicating the alpha composition mode to apply to the drawing operation /// - public PixelAlphaCompositionMode AlphaCompositionMode - { - get => this.alphaCompositionMode; - set => this.alphaCompositionMode = value; - } - - /// - /// Evaluates if a given SOURCE color can completely replace a BACKDROP color given the current blending and composition settings. - /// - /// the color - /// true if the color can be considered opaque - /// - /// 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. - /// - internal bool IsOpaqueColorWithoutBlending(Color color) - { - 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; - } + public PixelAlphaCompositionMode AlphaCompositionMode { get; set; } } -} \ No newline at end of file +}