mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
26 changed files with 445 additions and 80 deletions
@ -0,0 +1,100 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Processing; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Adds extensions that allow the processing of images to the <see cref="Image{TPixel}"/> type.
|
||||
|
/// </summary>
|
||||
|
public static class GraphicOptionsDefaultsExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Sets the default options against the image processing context.
|
||||
|
/// </summary>
|
||||
|
/// <param name="context">The image processing context to store default against.</param>
|
||||
|
/// <param name="optionsBuilder">The action to update instance of the default options used.</param>
|
||||
|
/// <returns>The passed in <paramref name="context"/> to allow chaining.</returns>
|
||||
|
public static IImageProcessingContext SetGraphicsOptions(this IImageProcessingContext context, Action<GraphicsOptions> optionsBuilder) |
||||
|
{ |
||||
|
var cloned = context.GetGraphicsOptions().DeepClone(); |
||||
|
optionsBuilder(cloned); |
||||
|
context.Properties[typeof(GraphicsOptions)] = cloned; |
||||
|
return context; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Sets the default options against the configuration.
|
||||
|
/// </summary>
|
||||
|
/// <param name="configuration">The configuration to store default against.</param>
|
||||
|
/// <param name="optionsBuilder">The default options to use.</param>
|
||||
|
public static void SetGraphicsOptions(this Configuration configuration, Action<GraphicsOptions> optionsBuilder) |
||||
|
{ |
||||
|
var cloned = configuration.GetGraphicsOptions().DeepClone(); |
||||
|
optionsBuilder(cloned); |
||||
|
configuration.Properties[typeof(GraphicsOptions)] = cloned; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Sets the default options against the image processing context.
|
||||
|
/// </summary>
|
||||
|
/// <param name="context">The image processing context to store default against.</param>
|
||||
|
/// <param name="options">The default options to use.</param>
|
||||
|
/// <returns>The passed in <paramref name="context"/> to allow chaining.</returns>
|
||||
|
public static IImageProcessingContext SetGraphicsOptions(this IImageProcessingContext context, GraphicsOptions options) |
||||
|
{ |
||||
|
context.Properties[typeof(GraphicsOptions)] = options; |
||||
|
return context; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Sets the default options against the configuration.
|
||||
|
/// </summary>
|
||||
|
/// <param name="configuration">The configuration to store default against.</param>
|
||||
|
/// <param name="options">The default options to use.</param>
|
||||
|
public static void SetGraphicsOptions(this Configuration configuration, GraphicsOptions options) |
||||
|
{ |
||||
|
configuration.Properties[typeof(GraphicsOptions)] = options; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the default options against the image processing context.
|
||||
|
/// </summary>
|
||||
|
/// <param name="context">The image processing context to retrieve defaults from.</param>
|
||||
|
/// <returns>The globaly configued default options.</returns>
|
||||
|
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.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
|
||||
|
return configOptions; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the default options against the image processing context.
|
||||
|
/// </summary>
|
||||
|
/// <param name="configuration">The configuration to retrieve defaults from.</param>
|
||||
|
/// <returns>The globaly configued default options.</returns>
|
||||
|
public static GraphicsOptions GetGraphicsOptions(this Configuration configuration) |
||||
|
{ |
||||
|
if (configuration.Properties.TryGetValue(typeof(GraphicsOptions), out var options) && options is GraphicsOptions go) |
||||
|
{ |
||||
|
return go; |
||||
|
} |
||||
|
|
||||
|
var configOptions = new GraphicsOptions(); |
||||
|
|
||||
|
// capture the fallback so the same instance will always be returned in case its mutated
|
||||
|
configuration.Properties[typeof(GraphicsOptions)] = configOptions; |
||||
|
return configOptions; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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<DrawImageProcessor>(); |
||||
|
|
||||
|
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<DrawImageProcessor>(); |
||||
|
|
||||
|
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<DrawImageProcessor>(); |
||||
|
|
||||
|
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<DrawImageProcessor>(); |
||||
|
|
||||
|
Assert.Equal(0.5, dip.Opacity); |
||||
|
Assert.Equal(this.options.AlphaCompositionMode, dip.AlphaCompositionMode); |
||||
|
Assert.Equal(PixelColorBlendingMode.Multiply, dip.ColorBlendingMode); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,172 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using SixLabors.ImageSharp.Tests.Processing; |
||||
|
using SixLabors.ImageSharp.Tests.TestUtilities; |
||||
|
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<Rgba32>(config, null, true); |
||||
|
|
||||
|
context.SetGraphicsOptions(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 UpdateDefaultOptionsOnProcessingContext_AlwaysNewInstance() |
||||
|
{ |
||||
|
var option = new GraphicsOptions() |
||||
|
{ |
||||
|
BlendPercentage = 0.9f |
||||
|
}; |
||||
|
var config = new Configuration(); |
||||
|
var context = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(config, null, true); |
||||
|
context.SetGraphicsOptions(option); |
||||
|
|
||||
|
context.SetGraphicsOptions(o => |
||||
|
{ |
||||
|
Assert.Equal(0.9f, o.BlendPercentage); // has origional values
|
||||
|
o.BlendPercentage = 0.4f; |
||||
|
}); |
||||
|
|
||||
|
var returnedOption = context.GetGraphicsOptions(); |
||||
|
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.SetGraphicsOptions(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.SetGraphicsOptions(option); |
||||
|
|
||||
|
config.SetGraphicsOptions(o => |
||||
|
{ |
||||
|
Assert.Equal(0.9f, o.BlendPercentage); // has origional values
|
||||
|
o.BlendPercentage = 0.4f; |
||||
|
}); |
||||
|
|
||||
|
var returnedOption = config.GetGraphicsOptions(); |
||||
|
Assert.Equal(0.4f, returnedOption.BlendPercentage); |
||||
|
Assert.Equal(0.9f, option.BlendPercentage); // hasn't been mutated
|
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void GetDefaultOptionsFromConfiguration_SettingNullThenReturnsNewInstance() |
||||
|
{ |
||||
|
var config = new Configuration(); |
||||
|
|
||||
|
var options = config.GetGraphicsOptions(); |
||||
|
Assert.NotNull(options); |
||||
|
config.SetGraphicsOptions((GraphicsOptions)null); |
||||
|
|
||||
|
var options2 = config.GetGraphicsOptions(); |
||||
|
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.GetGraphicsOptions(); |
||||
|
Assert.NotNull(options); |
||||
|
Assert.IsType<GraphicsOptions>(options); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void GetDefaultOptionsFromConfiguration_AlwaysReturnsInstance() |
||||
|
{ |
||||
|
var config = new Configuration(); |
||||
|
|
||||
|
Assert.DoesNotContain(typeof(GraphicsOptions), config.Properties.Keys); |
||||
|
var options = config.GetGraphicsOptions(); |
||||
|
Assert.NotNull(options); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void GetDefaultOptionsFromConfiguration_AlwaysReturnsSameValue() |
||||
|
{ |
||||
|
var config = new Configuration(); |
||||
|
|
||||
|
var options = config.GetGraphicsOptions(); |
||||
|
var options2 = config.GetGraphicsOptions(); |
||||
|
Assert.Equal(options, options2); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void GetDefaultOptionsFromProcessingContext_AlwaysReturnsInstance() |
||||
|
{ |
||||
|
var config = new Configuration(); |
||||
|
var context = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(config, null, true); |
||||
|
|
||||
|
var ctxOptions = context.GetGraphicsOptions(); |
||||
|
Assert.NotNull(ctxOptions); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void GetDefaultOptionsFromProcessingContext_AlwaysReturnsInstanceEvenIfSetToNull() |
||||
|
{ |
||||
|
var config = new Configuration(); |
||||
|
var context = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(config, null, true); |
||||
|
|
||||
|
context.SetGraphicsOptions((GraphicsOptions)null); |
||||
|
var ctxOptions = context.GetGraphicsOptions(); |
||||
|
Assert.NotNull(ctxOptions); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void GetDefaultOptionsFromProcessingContext_FallbackToConfigsInstance() |
||||
|
{ |
||||
|
var option = new GraphicsOptions(); |
||||
|
var config = new Configuration(); |
||||
|
config.SetGraphicsOptions(option); |
||||
|
var context = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(config, null, true); |
||||
|
|
||||
|
var ctxOptions = context.GetGraphicsOptions(); |
||||
|
Assert.Equal(option, ctxOptions); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void GetDefaultOptionsFromProcessingContext_IgnoreIncorectlyTypesDictionEntry() |
||||
|
{ |
||||
|
var config = new Configuration(); |
||||
|
var context = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(config, null, true); |
||||
|
context.Properties[typeof(GraphicsOptions)] = "wronge type"; |
||||
|
var options = context.GetGraphicsOptions(); |
||||
|
Assert.NotNull(options); |
||||
|
Assert.IsType<GraphicsOptions>(options); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue