From 9df82f57ce937f69138b3d57ec5948c39862b3e8 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 18 Apr 2020 11:37:44 +0100 Subject: [PATCH 1/6] global property store on configuration on processing context --- src/ImageSharp/Configuration.cs | 9 +++ .../GraphicOptionsDefaultsExtensions.cs | 67 +++++++++++++++++++ .../DefaultImageProcessorContext{TPixel}.cs | 5 ++ .../Extensions/Drawing/DrawImageExtensions.cs | 8 +-- .../Extensions/Filters/LomographExtensions.cs | 8 +-- .../Extensions/Filters/PolaroidExtensions.cs | 6 +- .../Overlays/BackgroundColorExtensions.cs | 4 +- .../Extensions/Overlays/GlowExtensions.cs | 10 +-- .../Extensions/Overlays/VignetteExtensions.cs | 10 +-- .../Processing/IImageProcessingContext.cs | 7 ++ .../Processors/Filters/LomographProcessor.cs | 9 ++- .../Filters/LomographProcessor{TPixel}.cs | 4 +- .../Processors/Filters/PolaroidProcessor.cs | 9 ++- .../Filters/PolaroidProcessor{TPixel}.cs | 6 +- .../Processors/Overlays/GlowProcessor.cs | 19 ------ .../Processors/Overlays/VignetteProcessor.cs | 9 --- .../Processing/FakeImageOperationsProvider.cs | 2 + 17 files changed, 136 insertions(+), 56 deletions(-) create mode 100644 src/ImageSharp/GraphicOptionsDefaultsExtensions.cs diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 47c7c54ea..b2d819f1c 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Net.Http; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.Formats.Gif; @@ -27,6 +28,8 @@ namespace SixLabors.ImageSharp private int maxDegreeOfParallelism = Environment.ProcessorCount; + private Dictionary properties = new Dictionary(); + /// /// Initializes a new instance of the class. /// @@ -73,6 +76,12 @@ namespace SixLabors.ImageSharp } } + /// + /// Gets a set of properties for the Congiguration. + /// + /// This can be used for storing global settings and defaults to be accessable to processors. + public IDictionary Properties => this.properties; + /// /// Gets the currently registered s. /// diff --git a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs new file mode 100644 index 000000000..3d86a5b0c --- /dev/null +++ b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs @@ -0,0 +1,67 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.Processing; + +namespace SixLabors.ImageSharp +{ + /// + /// Adds extensions that allow the processing of images to the type. + /// + public static class GraphicOptionsDefaultsExtensions + { + /// + /// Sets the default options against the image processing context. + /// + /// The image processing context to store default against. + /// The default options to use. + public static void SetDefaultOptions(this IImageProcessingContext context, GraphicsOptions options) + { + context.Properties[typeof(GraphicsOptions)] = options; + } + + /// + /// Sets the default options against the configuration. + /// + /// The image processing context to store default against. + /// The default options to use. + public static void SetDefaultOptions(this Configuration context, GraphicsOptions options) + { + context.Properties[typeof(GraphicsOptions)] = options; + } + + /// + /// Gets the default options against the image processing context. + /// + /// The image processing context to retrieve defaults from. + /// The globaly configued default options. + public static GraphicsOptions GetDefaultGraphicsOptions(this IImageProcessingContext context) + { + if (context.Properties.TryGetValue(typeof(GraphicsOptions), out var options) && options is GraphicsOptions go) + { + return go; + } + + var configOptions = context.Configuration.GetDefaultGraphicsOptions(); + context.Properties[typeof(GraphicsOptions)] = configOptions; + return configOptions; + } + + /// + /// Gets the default options against the image processing context. + /// + /// The image processing context to retrieve defaults from. + /// The globaly configued default options. + public static GraphicsOptions GetDefaultGraphicsOptions(this Configuration context) + { + if (context.Properties.TryGetValue(typeof(GraphicsOptions), out var options) && options is GraphicsOptions go) + { + return go; + } + + var configOptions = new GraphicsOptions(); + context.Properties[typeof(GraphicsOptions)] = configOptions; + return configOptions; + } + } +} diff --git a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs index 2e5919d1e..5ee3f6483 100644 --- a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs +++ b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Collections.Generic; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; @@ -15,6 +16,7 @@ namespace SixLabors.ImageSharp.Processing { private readonly bool mutate; private readonly Image source; + private readonly Dictionary properties = new Dictionary(); private Image destination; /// @@ -39,6 +41,9 @@ namespace SixLabors.ImageSharp.Processing /// public Configuration Configuration { get; } + /// + public IDictionary Properties => this.properties; + /// public Image GetResultImage() { diff --git a/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs b/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs index 4717c09ea..197dcd3ef 100644 --- a/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs @@ -23,7 +23,7 @@ namespace SixLabors.ImageSharp.Processing Image image, float opacity) { - var options = new GraphicsOptions(); + var options = source.GetDefaultGraphicsOptions(); return source.ApplyProcessor( new DrawImageProcessor( image, @@ -51,7 +51,7 @@ namespace SixLabors.ImageSharp.Processing image, Point.Empty, colorBlending, - new GraphicsOptions().AlphaCompositionMode, + source.GetDefaultGraphicsOptions().AlphaCompositionMode, opacity)); /// @@ -104,7 +104,7 @@ namespace SixLabors.ImageSharp.Processing Point location, float opacity) { - var options = new GraphicsOptions(); + var options = source.GetDefaultGraphicsOptions(); return source.ApplyProcessor( new DrawImageProcessor( image, @@ -134,7 +134,7 @@ namespace SixLabors.ImageSharp.Processing image, location, colorBlending, - new GraphicsOptions().AlphaCompositionMode, + source.GetDefaultGraphicsOptions().AlphaCompositionMode, opacity)); /// diff --git a/src/ImageSharp/Processing/Extensions/Filters/LomographExtensions.cs b/src/ImageSharp/Processing/Extensions/Filters/LomographExtensions.cs index 84b11c5e7..b46f53cf6 100644 --- a/src/ImageSharp/Processing/Extensions/Filters/LomographExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Filters/LomographExtensions.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.Processing.Processors.Filters; @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing /// The image this method extends. /// The to allow chaining of operations. public static IImageProcessingContext Lomograph(this IImageProcessingContext source) - => source.ApplyProcessor(new LomographProcessor()); + => source.ApplyProcessor(new LomographProcessor(source.GetDefaultGraphicsOptions())); /// /// Alters the colors of the image recreating an old Lomograph camera effect. @@ -28,6 +28,6 @@ namespace SixLabors.ImageSharp.Processing /// /// The to allow chaining of operations. public static IImageProcessingContext Lomograph(this IImageProcessingContext source, Rectangle rectangle) - => source.ApplyProcessor(new LomographProcessor(), rectangle); + => source.ApplyProcessor(new LomographProcessor(source.GetDefaultGraphicsOptions()), rectangle); } -} \ No newline at end of file +} diff --git a/src/ImageSharp/Processing/Extensions/Filters/PolaroidExtensions.cs b/src/ImageSharp/Processing/Extensions/Filters/PolaroidExtensions.cs index 94ced7108..4e216b4f7 100644 --- a/src/ImageSharp/Processing/Extensions/Filters/PolaroidExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Filters/PolaroidExtensions.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.Processing.Processors.Filters; @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing /// The image this method extends. /// The to allow chaining of operations. public static IImageProcessingContext Polaroid(this IImageProcessingContext source) - => source.ApplyProcessor(new PolaroidProcessor()); + => source.ApplyProcessor(new PolaroidProcessor(source.GetDefaultGraphicsOptions())); /// /// Alters the colors of the image recreating an old Polaroid camera effect. @@ -28,6 +28,6 @@ namespace SixLabors.ImageSharp.Processing /// /// The to allow chaining of operations. public static IImageProcessingContext Polaroid(this IImageProcessingContext source, Rectangle rectangle) - => source.ApplyProcessor(new PolaroidProcessor(), rectangle); + => source.ApplyProcessor(new PolaroidProcessor(source.GetDefaultGraphicsOptions()), rectangle); } } diff --git a/src/ImageSharp/Processing/Extensions/Overlays/BackgroundColorExtensions.cs b/src/ImageSharp/Processing/Extensions/Overlays/BackgroundColorExtensions.cs index d068ba10b..ced37091e 100644 --- a/src/ImageSharp/Processing/Extensions/Overlays/BackgroundColorExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Overlays/BackgroundColorExtensions.cs @@ -18,7 +18,7 @@ namespace SixLabors.ImageSharp.Processing /// The color to set as the background. /// The to allow chaining of operations. public static IImageProcessingContext BackgroundColor(this IImageProcessingContext source, Color color) => - BackgroundColor(source, new GraphicsOptions(), color); + BackgroundColor(source, source.GetDefaultGraphicsOptions(), color); /// /// Replaces the background color of image with the given one. @@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp.Processing this IImageProcessingContext source, Color color, Rectangle rectangle) => - BackgroundColor(source, new GraphicsOptions(), color, rectangle); + BackgroundColor(source, source.GetDefaultGraphicsOptions(), color, rectangle); /// /// Replaces the background color of image with the given one. diff --git a/src/ImageSharp/Processing/Extensions/Overlays/GlowExtensions.cs b/src/ImageSharp/Processing/Extensions/Overlays/GlowExtensions.cs index d5114e30a..63f065119 100644 --- a/src/ImageSharp/Processing/Extensions/Overlays/GlowExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Overlays/GlowExtensions.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing /// The image this method extends. /// The to allow chaining of operations. public static IImageProcessingContext Glow(this IImageProcessingContext source) => - Glow(source, new GraphicsOptions()); + Glow(source, source.GetDefaultGraphicsOptions()); /// /// Applies a radial glow effect to an image. @@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Processing /// The to allow chaining of operations. public static IImageProcessingContext Glow(this IImageProcessingContext source, Color color) { - return Glow(source, new GraphicsOptions(), color); + return Glow(source, source.GetDefaultGraphicsOptions(), color); } /// @@ -37,7 +37,7 @@ namespace SixLabors.ImageSharp.Processing /// The the radius. /// The to allow chaining of operations. public static IImageProcessingContext Glow(this IImageProcessingContext source, float radius) => - Glow(source, new GraphicsOptions(), radius); + Glow(source, source.GetDefaultGraphicsOptions(), radius); /// /// Applies a radial glow effect to an image. @@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Processing /// /// The to allow chaining of operations. public static IImageProcessingContext Glow(this IImageProcessingContext source, Rectangle rectangle) => - source.Glow(new GraphicsOptions(), rectangle); + source.Glow(source.GetDefaultGraphicsOptions(), rectangle); /// /// Applies a radial glow effect to an image. @@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp.Processing Color color, float radius, Rectangle rectangle) => - source.Glow(new GraphicsOptions(), color, ValueSize.Absolute(radius), rectangle); + source.Glow(source.GetDefaultGraphicsOptions(), color, ValueSize.Absolute(radius), rectangle); /// /// Applies a radial glow effect to an image. diff --git a/src/ImageSharp/Processing/Extensions/Overlays/VignetteExtensions.cs b/src/ImageSharp/Processing/Extensions/Overlays/VignetteExtensions.cs index 799b30e01..a3063832a 100644 --- a/src/ImageSharp/Processing/Extensions/Overlays/VignetteExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Overlays/VignetteExtensions.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing /// The image this method extends. /// The to allow chaining of operations. public static IImageProcessingContext Vignette(this IImageProcessingContext source) => - Vignette(source, new GraphicsOptions()); + Vignette(source, source.GetDefaultGraphicsOptions()); /// /// Applies a radial vignette effect to an image. @@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.Processing /// The color to set as the vignette. /// The to allow chaining of operations. public static IImageProcessingContext Vignette(this IImageProcessingContext source, Color color) => - Vignette(source, new GraphicsOptions(), color); + Vignette(source, source.GetDefaultGraphicsOptions(), color); /// /// Applies a radial vignette effect to an image. @@ -39,7 +39,7 @@ namespace SixLabors.ImageSharp.Processing this IImageProcessingContext source, float radiusX, float radiusY) => - Vignette(source, new GraphicsOptions(), radiusX, radiusY); + Vignette(source, source.GetDefaultGraphicsOptions(), radiusX, radiusY); /// /// Applies a radial vignette effect to an image. @@ -50,7 +50,7 @@ namespace SixLabors.ImageSharp.Processing /// /// The to allow chaining of operations. public static IImageProcessingContext Vignette(this IImageProcessingContext source, Rectangle rectangle) => - Vignette(source, new GraphicsOptions(), rectangle); + Vignette(source, source.GetDefaultGraphicsOptions(), rectangle); /// /// Applies a radial vignette effect to an image. @@ -69,7 +69,7 @@ namespace SixLabors.ImageSharp.Processing float radiusX, float radiusY, Rectangle rectangle) => - source.Vignette(new GraphicsOptions(), color, radiusX, radiusY, rectangle); + source.Vignette(source.GetDefaultGraphicsOptions(), color, radiusX, radiusY, rectangle); /// /// Applies a radial vignette effect to an image. diff --git a/src/ImageSharp/Processing/IImageProcessingContext.cs b/src/ImageSharp/Processing/IImageProcessingContext.cs index 8b57a289d..cb39766a9 100644 --- a/src/ImageSharp/Processing/IImageProcessingContext.cs +++ b/src/ImageSharp/Processing/IImageProcessingContext.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Collections.Generic; using SixLabors.ImageSharp.Processing.Processors; namespace SixLabors.ImageSharp.Processing @@ -15,6 +16,12 @@ namespace SixLabors.ImageSharp.Processing /// Configuration Configuration { get; } + /// + /// Gets a set of properties for the Image Processing Context. + /// + /// This can be used for storing global settings and defaults to be accessable to processors. + IDictionary Properties { get; } + /// /// Gets the image dimensions at the current point in the processing pipeline. /// diff --git a/src/ImageSharp/Processing/Processors/Filters/LomographProcessor.cs b/src/ImageSharp/Processing/Processors/Filters/LomographProcessor.cs index 3c150d7eb..bb6ea51c1 100644 --- a/src/ImageSharp/Processing/Processors/Filters/LomographProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Filters/LomographProcessor.cs @@ -11,11 +11,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters /// /// Initializes a new instance of the class. /// - public LomographProcessor() + /// Graphics options to use within the processor. + public LomographProcessor(GraphicsOptions graphicsOptions) : base(KnownFilterMatrices.LomographFilter) { + this.GraphicsOptions = graphicsOptions; } + /// + /// Gets the options effecting blending and composition + /// + public GraphicsOptions GraphicsOptions { get; } + /// public override IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) => new LomographProcessor(configuration, this, source, sourceRectangle); diff --git a/src/ImageSharp/Processing/Processors/Filters/LomographProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Filters/LomographProcessor{TPixel}.cs index 5a19b7851..0706e9fc8 100644 --- a/src/ImageSharp/Processing/Processors/Filters/LomographProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Filters/LomographProcessor{TPixel}.cs @@ -13,6 +13,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters where TPixel : unmanaged, IPixel { private static readonly Color VeryDarkGreen = Color.FromRgba(0, 10, 0, 255); + private readonly LomographProcessor definition; /// /// Initializes a new instance of the class. @@ -24,12 +25,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters public LomographProcessor(Configuration configuration, LomographProcessor definition, Image source, Rectangle sourceRectangle) : base(configuration, definition, source, sourceRectangle) { + this.definition = definition; } /// protected override void AfterImageApply() { - new VignetteProcessor(VeryDarkGreen).Execute(this.Configuration, this.Source, this.SourceRectangle); + new VignetteProcessor(this.definition.GraphicsOptions, VeryDarkGreen).Execute(this.Configuration, this.Source, this.SourceRectangle); base.AfterImageApply(); } } diff --git a/src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor.cs b/src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor.cs index a5cf26862..965a35be1 100644 --- a/src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor.cs @@ -11,11 +11,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters /// /// Initializes a new instance of the class. /// - public PolaroidProcessor() + /// Graphics options to use within the processor. + public PolaroidProcessor(GraphicsOptions graphicsOptions) : base(KnownFilterMatrices.PolaroidFilter) { + this.GraphicsOptions = graphicsOptions; } + /// + /// Gets the options effecting blending and composition + /// + public GraphicsOptions GraphicsOptions { get; } + /// public override IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) => new PolaroidProcessor(configuration, this, source, sourceRectangle); diff --git a/src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor{TPixel}.cs index 9f547be1c..470d553c2 100644 --- a/src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor{TPixel}.cs @@ -14,6 +14,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters { private static readonly Color LightOrange = Color.FromRgba(255, 153, 102, 128); private static readonly Color VeryDarkOrange = Color.FromRgb(102, 34, 0); + private readonly PolaroidProcessor definition; /// /// Initializes a new instance of the class. @@ -25,13 +26,14 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters public PolaroidProcessor(Configuration configuration, PolaroidProcessor definition, Image source, Rectangle sourceRectangle) : base(configuration, definition, source, sourceRectangle) { + this.definition = definition; } /// protected override void AfterImageApply() { - new VignetteProcessor(VeryDarkOrange).Execute(this.Configuration, this.Source, this.SourceRectangle); - new GlowProcessor(LightOrange, this.Source.Width / 4F).Execute(this.Configuration, this.Source, this.SourceRectangle); + new VignetteProcessor(this.definition.GraphicsOptions, VeryDarkOrange).Execute(this.Configuration, this.Source, this.SourceRectangle); + new GlowProcessor(this.definition.GraphicsOptions, LightOrange, this.Source.Width / 4F).Execute(this.Configuration, this.Source, this.SourceRectangle); base.AfterImageApply(); } } diff --git a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs index 87e93ca21..5e0d1cbf7 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs @@ -10,15 +10,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays /// public sealed class GlowProcessor : IImageProcessor { - /// - /// Initializes a new instance of the class. - /// - /// The color or the glow. - public GlowProcessor(Color color) - : this(color, 0) - { - } - /// /// Initializes a new instance of the class. /// @@ -29,16 +20,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays { } - /// - /// Initializes a new instance of the class. - /// - /// The color or the glow. - /// The radius of the glow. - internal GlowProcessor(Color color, ValueSize radius) - : this(new GraphicsOptions(), color, radius) - { - } - /// /// Initializes a new instance of the class. /// diff --git a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs index 5654eccfa..3b16f8bc8 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs @@ -10,15 +10,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays /// public sealed class VignetteProcessor : IImageProcessor { - /// - /// Initializes a new instance of the class. - /// - /// The color of the vignette. - public VignetteProcessor(Color color) - : this(new GraphicsOptions(), color) - { - } - /// /// Initializes a new instance of the class. /// diff --git a/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs b/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs index 3f11b4631..cd4d78279 100644 --- a/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs +++ b/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs @@ -56,6 +56,8 @@ namespace SixLabors.ImageSharp.Tests.Processing public Configuration Configuration { get; } + public IDictionary Properties { get; } = new Dictionary(); + public Image GetResultImage() { return this.Source; From 0ab64c2f895adce9db275192aecb4328693f4706 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 18 Apr 2020 16:34:06 +0100 Subject: [PATCH 2/6] Add some unit tests to verify usage of default graphics options --- .../GraphicOptionsDefaultsExtensions.cs | 7 +- .../Drawing/DrawImageExtensionsTests.cs | 83 ++++++++++++ .../GraphicOptionsDefaultsExtensionsTests.cs | 128 ++++++++++++++++++ .../BaseImageOperationsExtensionTest.cs | 1 + .../Processing/Effects/BackgroundColorTest.cs | 10 +- .../Processing/Filters/LomographTest.cs | 6 +- .../Processing/Filters/PolaroidTest.cs | 6 +- .../Processing/Overlays/GlowTest.cs | 10 +- .../Processing/Overlays/VignetteTest.cs | 10 +- 9 files changed, 238 insertions(+), 23 deletions(-) create mode 100644 tests/ImageSharp.Tests/Drawing/DrawImageExtensionsTests.cs create mode 100644 tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs diff --git a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs index 3d86a5b0c..38baf91d3 100644 --- a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs +++ b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; using SixLabors.ImageSharp.Processing; namespace SixLabors.ImageSharp @@ -43,7 +44,9 @@ namespace SixLabors.ImageSharp } var configOptions = context.Configuration.GetDefaultGraphicsOptions(); - context.Properties[typeof(GraphicsOptions)] = configOptions; + + // do not cache the fall back to config into the the processing context + // in case someone want to change the value on the config and expects it re trflow thru return configOptions; } @@ -60,6 +63,8 @@ namespace SixLabors.ImageSharp } var configOptions = new GraphicsOptions(); + + // capture the fallback so the same instance will always be returned in case its mutated context.Properties[typeof(GraphicsOptions)] = configOptions; return configOptions; } diff --git a/tests/ImageSharp.Tests/Drawing/DrawImageExtensionsTests.cs b/tests/ImageSharp.Tests/Drawing/DrawImageExtensionsTests.cs new file mode 100644 index 000000000..3f90412ae --- /dev/null +++ b/tests/ImageSharp.Tests/Drawing/DrawImageExtensionsTests.cs @@ -0,0 +1,83 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Linq; +using Moq; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Processors.Drawing; +using SixLabors.ImageSharp.Tests.Processing; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; + +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Drawing +{ + public class DrawImageExtensionsTests : BaseImageOperationsExtensionTest + { + + [Fact] + public void DrawImage_OpacityOnly_VerifyGraphicOptionsTakenFromContext() + { + // non-default values as we cant easly defect usage otherwise + this.options.AlphaCompositionMode = PixelAlphaCompositionMode.Xor; + this.options.ColorBlendingMode = PixelColorBlendingMode.Screen; + + this.operations.DrawImage(null, 0.5f); + var dip = this.Verify(); + + Assert.Equal(0.5, dip.Opacity); + Assert.Equal(this.options.AlphaCompositionMode, dip.AlphaCompositionMode); + Assert.Equal(this.options.ColorBlendingMode, dip.ColorBlendingMode); + } + + [Fact] + public void DrawImage_OpacityAndBlending_VerifyGraphicOptionsTakenFromContext() + { + // non-default values as we cant easly defect usage otherwise + this.options.AlphaCompositionMode = PixelAlphaCompositionMode.Xor; + this.options.ColorBlendingMode = PixelColorBlendingMode.Screen; + + this.operations.DrawImage(null, PixelColorBlendingMode.Multiply, 0.5f); + var dip = this.Verify(); + + Assert.Equal(0.5, dip.Opacity); + Assert.Equal(this.options.AlphaCompositionMode, dip.AlphaCompositionMode); + Assert.Equal(PixelColorBlendingMode.Multiply, dip.ColorBlendingMode); + } + + [Fact] + public void DrawImage_LocationAndOpacity_VerifyGraphicOptionsTakenFromContext() + { + // non-default values as we cant easly defect usage otherwise + this.options.AlphaCompositionMode = PixelAlphaCompositionMode.Xor; + this.options.ColorBlendingMode = PixelColorBlendingMode.Screen; + + this.operations.DrawImage(null, Point.Empty, 0.5f); + var dip = this.Verify(); + + Assert.Equal(0.5, dip.Opacity); + Assert.Equal(this.options.AlphaCompositionMode, dip.AlphaCompositionMode); + Assert.Equal(this.options.ColorBlendingMode, dip.ColorBlendingMode); + } + + [Fact] + public void DrawImage_LocationAndOpacityAndBlending_VerifyGraphicOptionsTakenFromContext() + { + // non-default values as we cant easly defect usage otherwise + this.options.AlphaCompositionMode = PixelAlphaCompositionMode.Xor; + this.options.ColorBlendingMode = PixelColorBlendingMode.Screen; + + this.operations.DrawImage(null, Point.Empty, PixelColorBlendingMode.Multiply, 0.5f); + var dip = this.Verify(); + + Assert.Equal(0.5, dip.Opacity); + Assert.Equal(this.options.AlphaCompositionMode, dip.AlphaCompositionMode); + Assert.Equal(PixelColorBlendingMode.Multiply, dip.ColorBlendingMode); + } + } +} diff --git a/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs new file mode 100644 index 000000000..6707341d2 --- /dev/null +++ b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs @@ -0,0 +1,128 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Tests.Processing; +using Xunit; + +namespace SixLabors.ImageSharp.Tests +{ + public class GraphicOptionsDefaultsExtensionsTests + { + [Fact] + public void SetDefaultOptionsOnProcessingContext() + { + var option = new GraphicsOptions(); + var config = new Configuration(); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + + context.SetDefaultOptions(option); + + // sets the prop on the processing context not on the configuration + Assert.Equal(option, context.Properties[typeof(GraphicsOptions)]); + Assert.DoesNotContain(typeof(GraphicsOptions), config.Properties.Keys); + } + + [Fact] + public void SetDefaultOptionsOnConfiguration() + { + var option = new GraphicsOptions(); + var config = new Configuration(); + + config.SetDefaultOptions(option); + + Assert.Equal(option, config.Properties[typeof(GraphicsOptions)]); + } + + [Fact] + public void GetDefaultOptionsFromConfiguration_SettingNullThenReturnsNewInstance() + { + var config = new Configuration(); + + var options = config.GetDefaultGraphicsOptions(); + Assert.NotNull(options); + config.SetDefaultOptions((GraphicsOptions)null); + + var options2 = config.GetDefaultGraphicsOptions(); + Assert.NotNull(options2); + + // we set it to null should now be a new instance + Assert.NotEqual(options, options2); + } + + [Fact] + public void GetDefaultOptionsFromConfiguration_IgnoreIncorectlyTypesDictionEntry() + { + var config = new Configuration(); + + config.Properties[typeof(GraphicsOptions)] = "wronge type"; + var options = config.GetDefaultGraphicsOptions(); + Assert.NotNull(options); + Assert.IsType(options); + } + + [Fact] + public void GetDefaultOptionsFromConfiguration_AlwaysReturnsInstance() + { + var config = new Configuration(); + + Assert.DoesNotContain(typeof(GraphicsOptions), config.Properties.Keys); + var options = config.GetDefaultGraphicsOptions(); + Assert.NotNull(options); + } + + [Fact] + public void GetDefaultOptionsFromConfiguration_AlwaysReturnsSameValue() + { + var config = new Configuration(); + + var options = config.GetDefaultGraphicsOptions(); + var options2 = config.GetDefaultGraphicsOptions(); + Assert.Equal(options, options2); + } + + [Fact] + public void GetDefaultOptionsFromProcessingContext_AlwaysReturnsInstance() + { + var config = new Configuration(); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + + var ctxOptions = context.GetDefaultGraphicsOptions(); + Assert.NotNull(ctxOptions); + } + + [Fact] + public void GetDefaultOptionsFromProcessingContext_AlwaysReturnsInstanceEvenIfSetToNull() + { + var config = new Configuration(); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + + context.SetDefaultOptions((GraphicsOptions)null); + var ctxOptions = context.GetDefaultGraphicsOptions(); + Assert.NotNull(ctxOptions); + } + + [Fact] + public void GetDefaultOptionsFromProcessingContext_FallbackToConfigsInstance() + { + var option = new GraphicsOptions(); + var config = new Configuration(); + config.SetDefaultOptions(option); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + + var ctxOptions = context.GetDefaultGraphicsOptions(); + Assert.Equal(option, ctxOptions); + } + + [Fact] + public void GetDefaultOptionsFromProcessingContext_IgnoreIncorectlyTypesDictionEntry() + { + var config = new Configuration(); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + context.Properties[typeof(GraphicsOptions)] = "wronge type"; + var options = context.GetDefaultGraphicsOptions(); + Assert.NotNull(options); + Assert.IsType(options); + } + } +} diff --git a/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs b/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs index 82c22245d..da2040131 100644 --- a/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs +++ b/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs @@ -25,6 +25,7 @@ namespace SixLabors.ImageSharp.Tests.Processing this.source = new Image(91 + 324, 123 + 56); this.rect = new Rectangle(91, 123, 324, 56); // make this random? this.internalOperations = new FakeImageOperationsProvider.FakeImageOperations(this.source.GetConfiguration(), this.source, false); + this.internalOperations.SetDefaultOptions(this.options); this.operations = this.internalOperations; } diff --git a/tests/ImageSharp.Tests/Processing/Effects/BackgroundColorTest.cs b/tests/ImageSharp.Tests/Processing/Effects/BackgroundColorTest.cs index 37bd2e87a..34b99461d 100644 --- a/tests/ImageSharp.Tests/Processing/Effects/BackgroundColorTest.cs +++ b/tests/ImageSharp.Tests/Processing/Effects/BackgroundColorTest.cs @@ -10,15 +10,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects { public class BackgroundColorTest : BaseImageOperationsExtensionTest { - private static readonly GraphicsOptionsComparer GraphicsOptionsComparer = new GraphicsOptionsComparer(); - [Fact] public void BackgroundColor_amount_BackgroundColorProcessorDefaultsSet() { this.operations.BackgroundColor(Color.BlanchedAlmond); BackgroundColorProcessor processor = this.Verify(); - Assert.Equal(new GraphicsOptions(), processor.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, processor.GraphicsOptions); Assert.Equal(Color.BlanchedAlmond, processor.Color); } @@ -28,7 +26,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects this.operations.BackgroundColor(Color.BlanchedAlmond, this.rect); BackgroundColorProcessor processor = this.Verify(this.rect); - Assert.Equal(new GraphicsOptions(), processor.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, processor.GraphicsOptions); Assert.Equal(Color.BlanchedAlmond, processor.Color); } @@ -38,7 +36,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects this.operations.BackgroundColor(this.options, Color.BlanchedAlmond); BackgroundColorProcessor processor = this.Verify(); - Assert.Equal(this.options, processor.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, processor.GraphicsOptions); Assert.Equal(Color.BlanchedAlmond, processor.Color); } @@ -48,7 +46,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects this.operations.BackgroundColor(this.options, Color.BlanchedAlmond, this.rect); BackgroundColorProcessor processor = this.Verify(this.rect); - Assert.Equal(this.options, processor.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, processor.GraphicsOptions); Assert.Equal(Color.BlanchedAlmond, processor.Color); } } diff --git a/tests/ImageSharp.Tests/Processing/Filters/LomographTest.cs b/tests/ImageSharp.Tests/Processing/Filters/LomographTest.cs index 65e04fbcc..6cb38e2fe 100644 --- a/tests/ImageSharp.Tests/Processing/Filters/LomographTest.cs +++ b/tests/ImageSharp.Tests/Processing/Filters/LomographTest.cs @@ -16,14 +16,16 @@ namespace SixLabors.ImageSharp.Tests public void Lomograph_amount_LomographProcessorDefaultsSet() { this.operations.Lomograph(); - this.Verify(); + var processor = this.Verify(); + Assert.Equal(processor.GraphicsOptions, this.options); } [Fact] public void Lomograph_amount_rect_LomographProcessorDefaultsSet() { this.operations.Lomograph(this.rect); - this.Verify(this.rect); + var processor = this.Verify(this.rect); + Assert.Equal(processor.GraphicsOptions, this.options); } } } diff --git a/tests/ImageSharp.Tests/Processing/Filters/PolaroidTest.cs b/tests/ImageSharp.Tests/Processing/Filters/PolaroidTest.cs index c3e2c3c50..346df0379 100644 --- a/tests/ImageSharp.Tests/Processing/Filters/PolaroidTest.cs +++ b/tests/ImageSharp.Tests/Processing/Filters/PolaroidTest.cs @@ -14,14 +14,16 @@ namespace SixLabors.ImageSharp.Tests.Processing.Filters public void Polaroid_amount_PolaroidProcessorDefaultsSet() { this.operations.Polaroid(); - this.Verify(); + var processor = this.Verify(); + Assert.Equal(processor.GraphicsOptions, this.options); } [Fact] public void Polaroid_amount_rect_PolaroidProcessorDefaultsSet() { this.operations.Polaroid(this.rect); - this.Verify(this.rect); + var processor = this.Verify(this.rect); + Assert.Equal(processor.GraphicsOptions, this.options); } } } diff --git a/tests/ImageSharp.Tests/Processing/Overlays/GlowTest.cs b/tests/ImageSharp.Tests/Processing/Overlays/GlowTest.cs index ea000ae2a..0336b231b 100644 --- a/tests/ImageSharp.Tests/Processing/Overlays/GlowTest.cs +++ b/tests/ImageSharp.Tests/Processing/Overlays/GlowTest.cs @@ -11,15 +11,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays { public class GlowTest : BaseImageOperationsExtensionTest { - private static readonly GraphicsOptionsComparer GraphicsOptionsComparer = new GraphicsOptionsComparer(); - [Fact] public void Glow_GlowProcessorWithDefaultValues() { this.operations.Glow(); GlowProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.GlowColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.Radius); } @@ -30,7 +28,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Glow(Color.Aquamarine); GlowProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Aquamarine, p.GlowColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.Radius); } @@ -41,7 +39,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Glow(3.5f); GlowProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.GlowColor); Assert.Equal(ValueSize.Absolute(3.5f), p.Radius); } @@ -53,7 +51,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Glow(rect); GlowProcessor p = this.Verify(rect); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.GlowColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.Radius); } diff --git a/tests/ImageSharp.Tests/Processing/Overlays/VignetteTest.cs b/tests/ImageSharp.Tests/Processing/Overlays/VignetteTest.cs index 8e5eb7207..5d41c58ce 100644 --- a/tests/ImageSharp.Tests/Processing/Overlays/VignetteTest.cs +++ b/tests/ImageSharp.Tests/Processing/Overlays/VignetteTest.cs @@ -10,15 +10,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays { public class VignetteTest : BaseImageOperationsExtensionTest { - private static readonly GraphicsOptionsComparer GraphicsOptionsComparer = new GraphicsOptionsComparer(); - [Fact] public void Vignette_VignetteProcessorWithDefaultValues() { this.operations.Vignette(); VignetteProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.VignetteColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.RadiusX); Assert.Equal(ValueSize.PercentageOfHeight(.5f), p.RadiusY); @@ -30,7 +28,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Vignette(Color.Aquamarine); VignetteProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Aquamarine, p.VignetteColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.RadiusX); Assert.Equal(ValueSize.PercentageOfHeight(.5f), p.RadiusY); @@ -42,7 +40,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Vignette(3.5f, 12123f); VignetteProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.VignetteColor); Assert.Equal(ValueSize.Absolute(3.5f), p.RadiusX); Assert.Equal(ValueSize.Absolute(12123f), p.RadiusY); @@ -55,7 +53,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Vignette(rect); VignetteProcessor p = this.Verify(rect); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.VignetteColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.RadiusX); Assert.Equal(ValueSize.PercentageOfHeight(.5f), p.RadiusY); From 5df21ad69a457f00069288c4e2e325dca8440226 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 18 Apr 2020 16:47:09 +0100 Subject: [PATCH 3/6] Add options builder extension --- .../GraphicOptionsDefaultsExtensions.cs | 26 +++++++++- .../GraphicOptionsDefaultsExtensionsTests.cs | 50 +++++++++++++++++-- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs index 38baf91d3..45e444ffe 100644 --- a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs +++ b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs @@ -11,6 +11,30 @@ namespace SixLabors.ImageSharp /// public static class GraphicOptionsDefaultsExtensions { + /// + /// Sets the default options against the image processing context. + /// + /// The image processing context to store default against. + /// The action to update instance of the default options used. + public static void SetDefaultOptions(this IImageProcessingContext context, Action optionsBuilder) + { + var cloned = context.GetDefaultGraphicsOptions().DeepClone(); + optionsBuilder(cloned); + context.Properties[typeof(GraphicsOptions)] = cloned; + } + + /// + /// Sets the default options against the configuration. + /// + /// The image processing context to store default against. + /// The default options to use. + public static void SetDefaultGraphicsOptions(this Configuration context, Action optionsBuilder) + { + var cloned = context.GetDefaultGraphicsOptions().DeepClone(); + optionsBuilder(cloned); + context.Properties[typeof(GraphicsOptions)] = cloned; + } + /// /// Sets the default options against the image processing context. /// @@ -26,7 +50,7 @@ namespace SixLabors.ImageSharp /// /// The image processing context to store default against. /// The default options to use. - public static void SetDefaultOptions(this Configuration context, GraphicsOptions options) + public static void SetDefaultGraphicsOptions(this Configuration context, GraphicsOptions options) { context.Properties[typeof(GraphicsOptions)] = options; } diff --git a/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs index 6707341d2..4b0d7a62d 100644 --- a/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs +++ b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs @@ -3,6 +3,7 @@ using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.Processing; +using SixLabors.ImageSharp.Tests.TestUtilities; using Xunit; namespace SixLabors.ImageSharp.Tests @@ -23,17 +24,60 @@ namespace SixLabors.ImageSharp.Tests Assert.DoesNotContain(typeof(GraphicsOptions), config.Properties.Keys); } + [Fact] + public void UpdateDefaultOptionsOnProcessingContext_AlwaysNewInstance() + { + var option = new GraphicsOptions() + { + BlendPercentage = 0.9f + }; + var config = new Configuration(); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + context.SetDefaultOptions(option); + + context.SetDefaultOptions(o => + { + Assert.Equal(0.9f, o.BlendPercentage); // has origional values + o.BlendPercentage = 0.4f; + }); + + var returnedOption = context.GetDefaultGraphicsOptions(); + Assert.Equal(0.4f, returnedOption.BlendPercentage); + Assert.Equal(0.9f, option.BlendPercentage); // hasn't been mutated + } + [Fact] public void SetDefaultOptionsOnConfiguration() { var option = new GraphicsOptions(); var config = new Configuration(); - config.SetDefaultOptions(option); + config.SetDefaultGraphicsOptions(option); Assert.Equal(option, config.Properties[typeof(GraphicsOptions)]); } + [Fact] + public void UpdateDefaultOptionsOnConfiguration_AlwaysNewInstance() + { + var option = new GraphicsOptions() + { + BlendPercentage = 0.9f + }; + var config = new Configuration(); + config.SetDefaultGraphicsOptions(option); + + config.SetDefaultGraphicsOptions(o => + { + Assert.Equal(0.9f, o.BlendPercentage); // has origional values + o.BlendPercentage = 0.4f; + }); + + var returnedOption = config.GetDefaultGraphicsOptions(); + Assert.Equal(0.4f, returnedOption.BlendPercentage); + Assert.Equal(0.9f, option.BlendPercentage); // hasn't been mutated + } + [Fact] public void GetDefaultOptionsFromConfiguration_SettingNullThenReturnsNewInstance() { @@ -41,7 +85,7 @@ namespace SixLabors.ImageSharp.Tests var options = config.GetDefaultGraphicsOptions(); Assert.NotNull(options); - config.SetDefaultOptions((GraphicsOptions)null); + config.SetDefaultGraphicsOptions((GraphicsOptions)null); var options2 = config.GetDefaultGraphicsOptions(); Assert.NotNull(options2); @@ -107,7 +151,7 @@ namespace SixLabors.ImageSharp.Tests { var option = new GraphicsOptions(); var config = new Configuration(); - config.SetDefaultOptions(option); + config.SetDefaultGraphicsOptions(option); var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); var ctxOptions = context.GetDefaultGraphicsOptions(); From 63453cfa36bdd7e76f9d82ed444f45fd28e874c1 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sun, 19 Apr 2020 17:58:49 +0100 Subject: [PATCH 4/6] Tweek extension names --- .../GraphicOptionsDefaultsExtensions.cs | 22 ++++++---- .../Extensions/Drawing/DrawImageExtensions.cs | 8 ++-- .../Extensions/Filters/LomographExtensions.cs | 4 +- .../Extensions/Filters/PolaroidExtensions.cs | 4 +- .../Overlays/BackgroundColorExtensions.cs | 4 +- .../Extensions/Overlays/GlowExtensions.cs | 10 ++--- .../Extensions/Overlays/VignetteExtensions.cs | 10 ++--- .../GraphicOptionsDefaultsExtensionsTests.cs | 42 +++++++++---------- .../BaseImageOperationsExtensionTest.cs | 3 +- .../Processing/Filters/ContrastTest.cs | 4 +- 10 files changed, 58 insertions(+), 53 deletions(-) diff --git a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs index 45e444ffe..10909c4f3 100644 --- a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs +++ b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs @@ -16,11 +16,13 @@ namespace SixLabors.ImageSharp /// /// The image processing context to store default against. /// The action to update instance of the default options used. - public static void SetDefaultOptions(this IImageProcessingContext context, Action optionsBuilder) + /// The passed in to allow chaining. + public static IImageProcessingContext SetGraphicsOptions(this IImageProcessingContext context, Action optionsBuilder) { - var cloned = context.GetDefaultGraphicsOptions().DeepClone(); + var cloned = context.GetGraphicsOptions().DeepClone(); optionsBuilder(cloned); context.Properties[typeof(GraphicsOptions)] = cloned; + return context; } /// @@ -28,9 +30,9 @@ namespace SixLabors.ImageSharp /// /// The image processing context to store default against. /// The default options to use. - public static void SetDefaultGraphicsOptions(this Configuration context, Action optionsBuilder) + public static void SetGraphicsOptions(this Configuration context, Action optionsBuilder) { - var cloned = context.GetDefaultGraphicsOptions().DeepClone(); + var cloned = context.GetGraphicsOptions().DeepClone(); optionsBuilder(cloned); context.Properties[typeof(GraphicsOptions)] = cloned; } @@ -40,9 +42,11 @@ namespace SixLabors.ImageSharp /// /// The image processing context to store default against. /// The default options to use. - public static void SetDefaultOptions(this IImageProcessingContext context, GraphicsOptions options) + /// The passed in to allow chaining. + public static IImageProcessingContext SetGraphicsOptions(this IImageProcessingContext context, GraphicsOptions options) { context.Properties[typeof(GraphicsOptions)] = options; + return context; } /// @@ -50,7 +54,7 @@ namespace SixLabors.ImageSharp /// /// The image processing context to store default against. /// The default options to use. - public static void SetDefaultGraphicsOptions(this Configuration context, GraphicsOptions options) + public static void SetGraphicsOptions(this Configuration context, GraphicsOptions options) { context.Properties[typeof(GraphicsOptions)] = options; } @@ -60,14 +64,14 @@ namespace SixLabors.ImageSharp /// /// The image processing context to retrieve defaults from. /// The globaly configued default options. - public static GraphicsOptions GetDefaultGraphicsOptions(this IImageProcessingContext context) + public static GraphicsOptions GetGraphicsOptions(this IImageProcessingContext context) { if (context.Properties.TryGetValue(typeof(GraphicsOptions), out var options) && options is GraphicsOptions go) { return go; } - var configOptions = context.Configuration.GetDefaultGraphicsOptions(); + var configOptions = context.Configuration.GetGraphicsOptions(); // do not cache the fall back to config into the the processing context // in case someone want to change the value on the config and expects it re trflow thru @@ -79,7 +83,7 @@ namespace SixLabors.ImageSharp /// /// The image processing context to retrieve defaults from. /// The globaly configued default options. - public static GraphicsOptions GetDefaultGraphicsOptions(this Configuration context) + public static GraphicsOptions GetGraphicsOptions(this Configuration context) { if (context.Properties.TryGetValue(typeof(GraphicsOptions), out var options) && options is GraphicsOptions go) { diff --git a/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs b/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs index 197dcd3ef..3c25bb7c4 100644 --- a/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs @@ -23,7 +23,7 @@ namespace SixLabors.ImageSharp.Processing Image image, float opacity) { - var options = source.GetDefaultGraphicsOptions(); + var options = source.GetGraphicsOptions(); return source.ApplyProcessor( new DrawImageProcessor( image, @@ -51,7 +51,7 @@ namespace SixLabors.ImageSharp.Processing image, Point.Empty, colorBlending, - source.GetDefaultGraphicsOptions().AlphaCompositionMode, + source.GetGraphicsOptions().AlphaCompositionMode, opacity)); /// @@ -104,7 +104,7 @@ namespace SixLabors.ImageSharp.Processing Point location, float opacity) { - var options = source.GetDefaultGraphicsOptions(); + var options = source.GetGraphicsOptions(); return source.ApplyProcessor( new DrawImageProcessor( image, @@ -134,7 +134,7 @@ namespace SixLabors.ImageSharp.Processing image, location, colorBlending, - source.GetDefaultGraphicsOptions().AlphaCompositionMode, + source.GetGraphicsOptions().AlphaCompositionMode, opacity)); /// diff --git a/src/ImageSharp/Processing/Extensions/Filters/LomographExtensions.cs b/src/ImageSharp/Processing/Extensions/Filters/LomographExtensions.cs index b46f53cf6..3f8a67feb 100644 --- a/src/ImageSharp/Processing/Extensions/Filters/LomographExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Filters/LomographExtensions.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing /// The image this method extends. /// The to allow chaining of operations. public static IImageProcessingContext Lomograph(this IImageProcessingContext source) - => source.ApplyProcessor(new LomographProcessor(source.GetDefaultGraphicsOptions())); + => source.ApplyProcessor(new LomographProcessor(source.GetGraphicsOptions())); /// /// Alters the colors of the image recreating an old Lomograph camera effect. @@ -28,6 +28,6 @@ namespace SixLabors.ImageSharp.Processing /// /// The to allow chaining of operations. public static IImageProcessingContext Lomograph(this IImageProcessingContext source, Rectangle rectangle) - => source.ApplyProcessor(new LomographProcessor(source.GetDefaultGraphicsOptions()), rectangle); + => source.ApplyProcessor(new LomographProcessor(source.GetGraphicsOptions()), rectangle); } } diff --git a/src/ImageSharp/Processing/Extensions/Filters/PolaroidExtensions.cs b/src/ImageSharp/Processing/Extensions/Filters/PolaroidExtensions.cs index 4e216b4f7..ab75ea56b 100644 --- a/src/ImageSharp/Processing/Extensions/Filters/PolaroidExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Filters/PolaroidExtensions.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing /// The image this method extends. /// The to allow chaining of operations. public static IImageProcessingContext Polaroid(this IImageProcessingContext source) - => source.ApplyProcessor(new PolaroidProcessor(source.GetDefaultGraphicsOptions())); + => source.ApplyProcessor(new PolaroidProcessor(source.GetGraphicsOptions())); /// /// Alters the colors of the image recreating an old Polaroid camera effect. @@ -28,6 +28,6 @@ namespace SixLabors.ImageSharp.Processing /// /// The to allow chaining of operations. public static IImageProcessingContext Polaroid(this IImageProcessingContext source, Rectangle rectangle) - => source.ApplyProcessor(new PolaroidProcessor(source.GetDefaultGraphicsOptions()), rectangle); + => source.ApplyProcessor(new PolaroidProcessor(source.GetGraphicsOptions()), rectangle); } } diff --git a/src/ImageSharp/Processing/Extensions/Overlays/BackgroundColorExtensions.cs b/src/ImageSharp/Processing/Extensions/Overlays/BackgroundColorExtensions.cs index ced37091e..21e244f0a 100644 --- a/src/ImageSharp/Processing/Extensions/Overlays/BackgroundColorExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Overlays/BackgroundColorExtensions.cs @@ -18,7 +18,7 @@ namespace SixLabors.ImageSharp.Processing /// The color to set as the background. /// The to allow chaining of operations. public static IImageProcessingContext BackgroundColor(this IImageProcessingContext source, Color color) => - BackgroundColor(source, source.GetDefaultGraphicsOptions(), color); + BackgroundColor(source, source.GetGraphicsOptions(), color); /// /// Replaces the background color of image with the given one. @@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp.Processing this IImageProcessingContext source, Color color, Rectangle rectangle) => - BackgroundColor(source, source.GetDefaultGraphicsOptions(), color, rectangle); + BackgroundColor(source, source.GetGraphicsOptions(), color, rectangle); /// /// Replaces the background color of image with the given one. diff --git a/src/ImageSharp/Processing/Extensions/Overlays/GlowExtensions.cs b/src/ImageSharp/Processing/Extensions/Overlays/GlowExtensions.cs index 63f065119..c3ce32e63 100644 --- a/src/ImageSharp/Processing/Extensions/Overlays/GlowExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Overlays/GlowExtensions.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing /// The image this method extends. /// The to allow chaining of operations. public static IImageProcessingContext Glow(this IImageProcessingContext source) => - Glow(source, source.GetDefaultGraphicsOptions()); + Glow(source, source.GetGraphicsOptions()); /// /// Applies a radial glow effect to an image. @@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Processing /// The to allow chaining of operations. public static IImageProcessingContext Glow(this IImageProcessingContext source, Color color) { - return Glow(source, source.GetDefaultGraphicsOptions(), color); + return Glow(source, source.GetGraphicsOptions(), color); } /// @@ -37,7 +37,7 @@ namespace SixLabors.ImageSharp.Processing /// The the radius. /// The to allow chaining of operations. public static IImageProcessingContext Glow(this IImageProcessingContext source, float radius) => - Glow(source, source.GetDefaultGraphicsOptions(), radius); + Glow(source, source.GetGraphicsOptions(), radius); /// /// Applies a radial glow effect to an image. @@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Processing /// /// The to allow chaining of operations. public static IImageProcessingContext Glow(this IImageProcessingContext source, Rectangle rectangle) => - source.Glow(source.GetDefaultGraphicsOptions(), rectangle); + source.Glow(source.GetGraphicsOptions(), rectangle); /// /// Applies a radial glow effect to an image. @@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp.Processing Color color, float radius, Rectangle rectangle) => - source.Glow(source.GetDefaultGraphicsOptions(), color, ValueSize.Absolute(radius), rectangle); + source.Glow(source.GetGraphicsOptions(), color, ValueSize.Absolute(radius), rectangle); /// /// Applies a radial glow effect to an image. diff --git a/src/ImageSharp/Processing/Extensions/Overlays/VignetteExtensions.cs b/src/ImageSharp/Processing/Extensions/Overlays/VignetteExtensions.cs index a3063832a..b53880fc1 100644 --- a/src/ImageSharp/Processing/Extensions/Overlays/VignetteExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Overlays/VignetteExtensions.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing /// The image this method extends. /// The to allow chaining of operations. public static IImageProcessingContext Vignette(this IImageProcessingContext source) => - Vignette(source, source.GetDefaultGraphicsOptions()); + Vignette(source, source.GetGraphicsOptions()); /// /// Applies a radial vignette effect to an image. @@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.Processing /// The color to set as the vignette. /// The to allow chaining of operations. public static IImageProcessingContext Vignette(this IImageProcessingContext source, Color color) => - Vignette(source, source.GetDefaultGraphicsOptions(), color); + Vignette(source, source.GetGraphicsOptions(), color); /// /// Applies a radial vignette effect to an image. @@ -39,7 +39,7 @@ namespace SixLabors.ImageSharp.Processing this IImageProcessingContext source, float radiusX, float radiusY) => - Vignette(source, source.GetDefaultGraphicsOptions(), radiusX, radiusY); + Vignette(source, source.GetGraphicsOptions(), radiusX, radiusY); /// /// Applies a radial vignette effect to an image. @@ -50,7 +50,7 @@ namespace SixLabors.ImageSharp.Processing /// /// The to allow chaining of operations. public static IImageProcessingContext Vignette(this IImageProcessingContext source, Rectangle rectangle) => - Vignette(source, source.GetDefaultGraphicsOptions(), rectangle); + Vignette(source, source.GetGraphicsOptions(), rectangle); /// /// Applies a radial vignette effect to an image. @@ -69,7 +69,7 @@ namespace SixLabors.ImageSharp.Processing float radiusX, float radiusY, Rectangle rectangle) => - source.Vignette(source.GetDefaultGraphicsOptions(), color, radiusX, radiusY, rectangle); + source.Vignette(source.GetGraphicsOptions(), color, radiusX, radiusY, rectangle); /// /// Applies a radial vignette effect to an image. diff --git a/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs index 4b0d7a62d..9c02dd601 100644 --- a/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs +++ b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Tests var config = new Configuration(); var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); - context.SetDefaultOptions(option); + context.SetGraphicsOptions(option); // sets the prop on the processing context not on the configuration Assert.Equal(option, context.Properties[typeof(GraphicsOptions)]); @@ -33,15 +33,15 @@ namespace SixLabors.ImageSharp.Tests }; var config = new Configuration(); var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); - context.SetDefaultOptions(option); + context.SetGraphicsOptions(option); - context.SetDefaultOptions(o => + context.SetGraphicsOptions(o => { Assert.Equal(0.9f, o.BlendPercentage); // has origional values o.BlendPercentage = 0.4f; }); - var returnedOption = context.GetDefaultGraphicsOptions(); + var returnedOption = context.GetGraphicsOptions(); Assert.Equal(0.4f, returnedOption.BlendPercentage); Assert.Equal(0.9f, option.BlendPercentage); // hasn't been mutated } @@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Tests var option = new GraphicsOptions(); var config = new Configuration(); - config.SetDefaultGraphicsOptions(option); + config.SetGraphicsOptions(option); Assert.Equal(option, config.Properties[typeof(GraphicsOptions)]); } @@ -65,15 +65,15 @@ namespace SixLabors.ImageSharp.Tests BlendPercentage = 0.9f }; var config = new Configuration(); - config.SetDefaultGraphicsOptions(option); + config.SetGraphicsOptions(option); - config.SetDefaultGraphicsOptions(o => + config.SetGraphicsOptions(o => { Assert.Equal(0.9f, o.BlendPercentage); // has origional values o.BlendPercentage = 0.4f; }); - var returnedOption = config.GetDefaultGraphicsOptions(); + var returnedOption = config.GetGraphicsOptions(); Assert.Equal(0.4f, returnedOption.BlendPercentage); Assert.Equal(0.9f, option.BlendPercentage); // hasn't been mutated } @@ -83,11 +83,11 @@ namespace SixLabors.ImageSharp.Tests { var config = new Configuration(); - var options = config.GetDefaultGraphicsOptions(); + var options = config.GetGraphicsOptions(); Assert.NotNull(options); - config.SetDefaultGraphicsOptions((GraphicsOptions)null); + config.SetGraphicsOptions((GraphicsOptions)null); - var options2 = config.GetDefaultGraphicsOptions(); + var options2 = config.GetGraphicsOptions(); Assert.NotNull(options2); // we set it to null should now be a new instance @@ -100,7 +100,7 @@ namespace SixLabors.ImageSharp.Tests var config = new Configuration(); config.Properties[typeof(GraphicsOptions)] = "wronge type"; - var options = config.GetDefaultGraphicsOptions(); + var options = config.GetGraphicsOptions(); Assert.NotNull(options); Assert.IsType(options); } @@ -111,7 +111,7 @@ namespace SixLabors.ImageSharp.Tests var config = new Configuration(); Assert.DoesNotContain(typeof(GraphicsOptions), config.Properties.Keys); - var options = config.GetDefaultGraphicsOptions(); + var options = config.GetGraphicsOptions(); Assert.NotNull(options); } @@ -120,8 +120,8 @@ namespace SixLabors.ImageSharp.Tests { var config = new Configuration(); - var options = config.GetDefaultGraphicsOptions(); - var options2 = config.GetDefaultGraphicsOptions(); + var options = config.GetGraphicsOptions(); + var options2 = config.GetGraphicsOptions(); Assert.Equal(options, options2); } @@ -131,7 +131,7 @@ namespace SixLabors.ImageSharp.Tests var config = new Configuration(); var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); - var ctxOptions = context.GetDefaultGraphicsOptions(); + var ctxOptions = context.GetGraphicsOptions(); Assert.NotNull(ctxOptions); } @@ -141,8 +141,8 @@ namespace SixLabors.ImageSharp.Tests var config = new Configuration(); var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); - context.SetDefaultOptions((GraphicsOptions)null); - var ctxOptions = context.GetDefaultGraphicsOptions(); + context.SetGraphicsOptions((GraphicsOptions)null); + var ctxOptions = context.GetGraphicsOptions(); Assert.NotNull(ctxOptions); } @@ -151,10 +151,10 @@ namespace SixLabors.ImageSharp.Tests { var option = new GraphicsOptions(); var config = new Configuration(); - config.SetDefaultGraphicsOptions(option); + config.SetGraphicsOptions(option); var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); - var ctxOptions = context.GetDefaultGraphicsOptions(); + var ctxOptions = context.GetGraphicsOptions(); Assert.Equal(option, ctxOptions); } @@ -164,7 +164,7 @@ namespace SixLabors.ImageSharp.Tests var config = new Configuration(); var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); context.Properties[typeof(GraphicsOptions)] = "wronge type"; - var options = context.GetDefaultGraphicsOptions(); + var options = context.GetGraphicsOptions(); Assert.NotNull(options); Assert.IsType(options); } diff --git a/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs b/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs index da2040131..953563006 100644 --- a/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs +++ b/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.ComponentModel.DataAnnotations; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; @@ -25,7 +26,7 @@ namespace SixLabors.ImageSharp.Tests.Processing this.source = new Image(91 + 324, 123 + 56); this.rect = new Rectangle(91, 123, 324, 56); // make this random? this.internalOperations = new FakeImageOperationsProvider.FakeImageOperations(this.source.GetConfiguration(), this.source, false); - this.internalOperations.SetDefaultOptions(this.options); + this.internalOperations.SetGraphicsOptions(this.options); this.operations = this.internalOperations; } diff --git a/tests/ImageSharp.Tests/Processing/Filters/ContrastTest.cs b/tests/ImageSharp.Tests/Processing/Filters/ContrastTest.cs index e55e983da..bf2d6823a 100644 --- a/tests/ImageSharp.Tests/Processing/Filters/ContrastTest.cs +++ b/tests/ImageSharp.Tests/Processing/Filters/ContrastTest.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 Xunit; @@ -28,4 +28,4 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects Assert.Equal(1.5F, processor.Amount); } } -} \ No newline at end of file +} From 8738afe816869fbced855cc4ae3a629009829979 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Wed, 22 Apr 2020 09:01:19 +0100 Subject: [PATCH 5/6] fix configuration param names --- .../GraphicOptionsDefaultsExtensions.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs index 10909c4f3..c8beea8e8 100644 --- a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs +++ b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs @@ -28,13 +28,13 @@ namespace SixLabors.ImageSharp /// /// Sets the default options against the configuration. /// - /// The image processing context to store default against. + /// The configuration to store default against. /// The default options to use. - public static void SetGraphicsOptions(this Configuration context, Action optionsBuilder) + public static void SetGraphicsOptions(this Configuration configuration, Action optionsBuilder) { - var cloned = context.GetGraphicsOptions().DeepClone(); + var cloned = configuration.GetGraphicsOptions().DeepClone(); optionsBuilder(cloned); - context.Properties[typeof(GraphicsOptions)] = cloned; + configuration.Properties[typeof(GraphicsOptions)] = cloned; } /// @@ -52,11 +52,11 @@ namespace SixLabors.ImageSharp /// /// Sets the default options against the configuration. /// - /// The image processing context to store default against. + /// The configuration to store default against. /// The default options to use. - public static void SetGraphicsOptions(this Configuration context, GraphicsOptions options) + public static void SetGraphicsOptions(this Configuration configuration, GraphicsOptions options) { - context.Properties[typeof(GraphicsOptions)] = options; + configuration.Properties[typeof(GraphicsOptions)] = options; } /// @@ -81,11 +81,11 @@ namespace SixLabors.ImageSharp /// /// Gets the default options against the image processing context. /// - /// The image processing context to retrieve defaults from. + /// The configuration to retrieve defaults from. /// The globaly configued default options. - public static GraphicsOptions GetGraphicsOptions(this Configuration context) + public static GraphicsOptions GetGraphicsOptions(this Configuration configuration) { - if (context.Properties.TryGetValue(typeof(GraphicsOptions), out var options) && options is GraphicsOptions go) + if (configuration.Properties.TryGetValue(typeof(GraphicsOptions), out var options) && options is GraphicsOptions go) { return go; } @@ -93,7 +93,7 @@ namespace SixLabors.ImageSharp var configOptions = new GraphicsOptions(); // capture the fallback so the same instance will always be returned in case its mutated - context.Properties[typeof(GraphicsOptions)] = configOptions; + configuration.Properties[typeof(GraphicsOptions)] = configOptions; return configOptions; } } From d38764dba2cbcf68b25778a304b5ab2b794b3754 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Wed, 22 Apr 2020 09:02:03 +0100 Subject: [PATCH 6/6] remove explicit backing fields for property bags --- src/ImageSharp/Configuration.cs | 4 +--- .../Processing/DefaultImageProcessorContext{TPixel}.cs | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index b2d819f1c..17ac8c3fd 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -28,8 +28,6 @@ namespace SixLabors.ImageSharp private int maxDegreeOfParallelism = Environment.ProcessorCount; - private Dictionary properties = new Dictionary(); - /// /// Initializes a new instance of the class. /// @@ -80,7 +78,7 @@ namespace SixLabors.ImageSharp /// Gets a set of properties for the Congiguration. /// /// This can be used for storing global settings and defaults to be accessable to processors. - public IDictionary Properties => this.properties; + public IDictionary Properties { get; } = new Dictionary(); /// /// Gets the currently registered s. diff --git a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs index 5ee3f6483..714a45f5f 100644 --- a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs +++ b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs @@ -16,7 +16,6 @@ namespace SixLabors.ImageSharp.Processing { private readonly bool mutate; private readonly Image source; - private readonly Dictionary properties = new Dictionary(); private Image destination; /// @@ -42,7 +41,7 @@ namespace SixLabors.ImageSharp.Processing public Configuration Configuration { get; } /// - public IDictionary Properties => this.properties; + public IDictionary Properties { get; } = new Dictionary(); /// public Image GetResultImage()