Browse Source

global property store on configuration on processing context

pull/1574/head
Scott Williams 6 years ago
parent
commit
34e3ca8263
  1. 9
      src/ImageSharp/Configuration.cs
  2. 67
      src/ImageSharp/GraphicOptionsDefaultsExtensions.cs
  3. 5
      src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs
  4. 8
      src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs
  5. 8
      src/ImageSharp/Processing/Extensions/Filters/LomographExtensions.cs
  6. 6
      src/ImageSharp/Processing/Extensions/Filters/PolaroidExtensions.cs
  7. 4
      src/ImageSharp/Processing/Extensions/Overlays/BackgroundColorExtensions.cs
  8. 10
      src/ImageSharp/Processing/Extensions/Overlays/GlowExtensions.cs
  9. 10
      src/ImageSharp/Processing/Extensions/Overlays/VignetteExtensions.cs
  10. 7
      src/ImageSharp/Processing/IImageProcessingContext.cs
  11. 9
      src/ImageSharp/Processing/Processors/Filters/LomographProcessor.cs
  12. 4
      src/ImageSharp/Processing/Processors/Filters/LomographProcessor{TPixel}.cs
  13. 9
      src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor.cs
  14. 6
      src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor{TPixel}.cs
  15. 19
      src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs
  16. 9
      src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs
  17. 2
      tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs

9
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<object, object> properties = new Dictionary<object, object>();
/// <summary>
/// Initializes a new instance of the <see cref="Configuration" /> class.
/// </summary>
@ -73,6 +76,12 @@ namespace SixLabors.ImageSharp
}
}
/// <summary>
/// Gets a set of properties for the Congiguration.
/// </summary>
/// <remarks>This can be used for storing global settings and defaults to be accessable to processors.</remarks>
public IDictionary<object, object> Properties => this.properties;
/// <summary>
/// Gets the currently registered <see cref="IImageFormat"/>s.
/// </summary>

67
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
{
/// <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="options">The default options to use.</param>
public static void SetDefaultOptions(this IImageProcessingContext context, GraphicsOptions options)
{
context.Properties[typeof(GraphicsOptions)] = options;
}
/// <summary>
/// Sets the default options against the configuration.
/// </summary>
/// <param name="context">The image processing context to store default against.</param>
/// <param name="options">The default options to use.</param>
public static void SetDefaultOptions(this Configuration context, GraphicsOptions options)
{
context.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 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;
}
/// <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 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;
}
}
}

5
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<TPixel> source;
private readonly Dictionary<object, object> properties = new Dictionary<object, object>();
private Image<TPixel> destination;
/// <summary>
@ -39,6 +41,9 @@ namespace SixLabors.ImageSharp.Processing
/// <inheritdoc/>
public Configuration Configuration { get; }
/// <inheritdoc/>
public IDictionary<object, object> Properties => this.properties;
/// <inheritdoc/>
public Image<TPixel> GetResultImage()
{

8
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));
/// <summary>
@ -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));
/// <summary>

8
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
/// <param name="source">The image this method extends.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Lomograph(this IImageProcessingContext source)
=> source.ApplyProcessor(new LomographProcessor());
=> source.ApplyProcessor(new LomographProcessor(source.GetDefaultGraphicsOptions()));
/// <summary>
/// Alters the colors of the image recreating an old Lomograph camera effect.
@ -28,6 +28,6 @@ namespace SixLabors.ImageSharp.Processing
/// </param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Lomograph(this IImageProcessingContext source, Rectangle rectangle)
=> source.ApplyProcessor(new LomographProcessor(), rectangle);
=> source.ApplyProcessor(new LomographProcessor(source.GetDefaultGraphicsOptions()), rectangle);
}
}
}

6
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
/// <param name="source">The image this method extends.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Polaroid(this IImageProcessingContext source)
=> source.ApplyProcessor(new PolaroidProcessor());
=> source.ApplyProcessor(new PolaroidProcessor(source.GetDefaultGraphicsOptions()));
/// <summary>
/// Alters the colors of the image recreating an old Polaroid camera effect.
@ -28,6 +28,6 @@ namespace SixLabors.ImageSharp.Processing
/// </param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Polaroid(this IImageProcessingContext source, Rectangle rectangle)
=> source.ApplyProcessor(new PolaroidProcessor(), rectangle);
=> source.ApplyProcessor(new PolaroidProcessor(source.GetDefaultGraphicsOptions()), rectangle);
}
}

4
src/ImageSharp/Processing/Extensions/Overlays/BackgroundColorExtensions.cs

@ -18,7 +18,7 @@ namespace SixLabors.ImageSharp.Processing
/// <param name="color">The color to set as the background.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext BackgroundColor(this IImageProcessingContext source, Color color) =>
BackgroundColor(source, new GraphicsOptions(), color);
BackgroundColor(source, source.GetDefaultGraphicsOptions(), color);
/// <summary>
/// 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);
/// <summary>
/// Replaces the background color of image with the given one.

10
src/ImageSharp/Processing/Extensions/Overlays/GlowExtensions.cs

@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing
/// <param name="source">The image this method extends.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Glow(this IImageProcessingContext source) =>
Glow(source, new GraphicsOptions());
Glow(source, source.GetDefaultGraphicsOptions());
/// <summary>
/// Applies a radial glow effect to an image.
@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Processing
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Glow(this IImageProcessingContext source, Color color)
{
return Glow(source, new GraphicsOptions(), color);
return Glow(source, source.GetDefaultGraphicsOptions(), color);
}
/// <summary>
@ -37,7 +37,7 @@ namespace SixLabors.ImageSharp.Processing
/// <param name="radius">The the radius.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Glow(this IImageProcessingContext source, float radius) =>
Glow(source, new GraphicsOptions(), radius);
Glow(source, source.GetDefaultGraphicsOptions(), radius);
/// <summary>
/// Applies a radial glow effect to an image.
@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Processing
/// </param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Glow(this IImageProcessingContext source, Rectangle rectangle) =>
source.Glow(new GraphicsOptions(), rectangle);
source.Glow(source.GetDefaultGraphicsOptions(), rectangle);
/// <summary>
/// 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);
/// <summary>
/// Applies a radial glow effect to an image.

10
src/ImageSharp/Processing/Extensions/Overlays/VignetteExtensions.cs

@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing
/// <param name="source">The image this method extends.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Vignette(this IImageProcessingContext source) =>
Vignette(source, new GraphicsOptions());
Vignette(source, source.GetDefaultGraphicsOptions());
/// <summary>
/// Applies a radial vignette effect to an image.
@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.Processing
/// <param name="color">The color to set as the vignette.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Vignette(this IImageProcessingContext source, Color color) =>
Vignette(source, new GraphicsOptions(), color);
Vignette(source, source.GetDefaultGraphicsOptions(), color);
/// <summary>
/// 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);
/// <summary>
/// Applies a radial vignette effect to an image.
@ -50,7 +50,7 @@ namespace SixLabors.ImageSharp.Processing
/// </param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Vignette(this IImageProcessingContext source, Rectangle rectangle) =>
Vignette(source, new GraphicsOptions(), rectangle);
Vignette(source, source.GetDefaultGraphicsOptions(), rectangle);
/// <summary>
/// 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);
/// <summary>
/// Applies a radial vignette effect to an image.

7
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
/// </summary>
Configuration Configuration { get; }
/// <summary>
/// Gets a set of properties for the Image Processing Context.
/// </summary>
/// <remarks>This can be used for storing global settings and defaults to be accessable to processors.</remarks>
IDictionary<object, object> Properties { get; }
/// <summary>
/// Gets the image dimensions at the current point in the processing pipeline.
/// </summary>

9
src/ImageSharp/Processing/Processors/Filters/LomographProcessor.cs

@ -11,11 +11,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters
/// <summary>
/// Initializes a new instance of the <see cref="LomographProcessor" /> class.
/// </summary>
public LomographProcessor()
/// <param name="graphicsOptions">Graphics options to use within the processor.</param>
public LomographProcessor(GraphicsOptions graphicsOptions)
: base(KnownFilterMatrices.LomographFilter)
{
this.GraphicsOptions = graphicsOptions;
}
/// <summary>
/// Gets the options effecting blending and composition
/// </summary>
public GraphicsOptions GraphicsOptions { get; }
/// <inheritdoc />
public override IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle) =>
new LomographProcessor<TPixel>(configuration, this, source, sourceRectangle);

4
src/ImageSharp/Processing/Processors/Filters/LomographProcessor{TPixel}.cs

@ -13,6 +13,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters
where TPixel : unmanaged, IPixel<TPixel>
{
private static readonly Color VeryDarkGreen = Color.FromRgba(0, 10, 0, 255);
private readonly LomographProcessor definition;
/// <summary>
/// Initializes a new instance of the <see cref="LomographProcessor{TPixel}"/> class.
@ -24,12 +25,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters
public LomographProcessor(Configuration configuration, LomographProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
: base(configuration, definition, source, sourceRectangle)
{
this.definition = definition;
}
/// <inheritdoc/>
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();
}
}

9
src/ImageSharp/Processing/Processors/Filters/PolaroidProcessor.cs

@ -11,11 +11,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters
/// <summary>
/// Initializes a new instance of the <see cref="PolaroidProcessor" /> class.
/// </summary>
public PolaroidProcessor()
/// <param name="graphicsOptions">Graphics options to use within the processor.</param>
public PolaroidProcessor(GraphicsOptions graphicsOptions)
: base(KnownFilterMatrices.PolaroidFilter)
{
this.GraphicsOptions = graphicsOptions;
}
/// <summary>
/// Gets the options effecting blending and composition
/// </summary>
public GraphicsOptions GraphicsOptions { get; }
/// <inheritdoc />
public override IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle) =>
new PolaroidProcessor<TPixel>(configuration, this, source, sourceRectangle);

6
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;
/// <summary>
/// Initializes a new instance of the <see cref="PolaroidProcessor{TPixel}"/> class.
@ -25,13 +26,14 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters
public PolaroidProcessor(Configuration configuration, PolaroidProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
: base(configuration, definition, source, sourceRectangle)
{
this.definition = definition;
}
/// <inheritdoc/>
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();
}
}

19
src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs

@ -10,15 +10,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
/// </summary>
public sealed class GlowProcessor : IImageProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="GlowProcessor" /> class.
/// </summary>
/// <param name="color">The color or the glow.</param>
public GlowProcessor(Color color)
: this(color, 0)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="GlowProcessor" /> class.
/// </summary>
@ -29,16 +20,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
{
}
/// <summary>
/// Initializes a new instance of the <see cref="GlowProcessor" /> class.
/// </summary>
/// <param name="color">The color or the glow.</param>
/// <param name="radius">The radius of the glow.</param>
internal GlowProcessor(Color color, ValueSize radius)
: this(new GraphicsOptions(), color, radius)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="GlowProcessor" /> class.
/// </summary>

9
src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs

@ -10,15 +10,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
/// </summary>
public sealed class VignetteProcessor : IImageProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="VignetteProcessor" /> class.
/// </summary>
/// <param name="color">The color of the vignette.</param>
public VignetteProcessor(Color color)
: this(new GraphicsOptions(), color)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="VignetteProcessor" /> class.
/// </summary>

2
tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs

@ -56,6 +56,8 @@ namespace SixLabors.ImageSharp.Tests.Processing
public Configuration Configuration { get; }
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
public Image<TPixel> GetResultImage()
{
return this.Source;

Loading…
Cancel
Save