Skip to content

Commit afcb3ca

Browse files
authored
Merge pull request #1176 from SixLabors/sw/property-bag
Add Property bag to Configuration and Image Processing Context
2 parents 6e08eb3 + d38764d commit afcb3ca

26 files changed

+445
-80
lines changed

src/ImageSharp/Configuration.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Net.Http;
67
using SixLabors.ImageSharp.Formats;
78
using SixLabors.ImageSharp.Formats.Bmp;
89
using SixLabors.ImageSharp.Formats.Gif;
@@ -73,6 +74,12 @@ public int MaxDegreeOfParallelism
7374
}
7475
}
7576

77+
/// <summary>
78+
/// Gets a set of properties for the Congiguration.
79+
/// </summary>
80+
/// <remarks>This can be used for storing global settings and defaults to be accessable to processors.</remarks>
81+
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
82+
7683
/// <summary>
7784
/// Gets the currently registered <see cref="IImageFormat"/>s.
7885
/// </summary>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
using SixLabors.ImageSharp.Processing;
6+
7+
namespace SixLabors.ImageSharp
8+
{
9+
/// <summary>
10+
/// Adds extensions that allow the processing of images to the <see cref="Image{TPixel}"/> type.
11+
/// </summary>
12+
public static class GraphicOptionsDefaultsExtensions
13+
{
14+
/// <summary>
15+
/// Sets the default options against the image processing context.
16+
/// </summary>
17+
/// <param name="context">The image processing context to store default against.</param>
18+
/// <param name="optionsBuilder">The action to update instance of the default options used.</param>
19+
/// <returns>The passed in <paramref name="context"/> to allow chaining.</returns>
20+
public static IImageProcessingContext SetGraphicsOptions(this IImageProcessingContext context, Action<GraphicsOptions> optionsBuilder)
21+
{
22+
var cloned = context.GetGraphicsOptions().DeepClone();
23+
optionsBuilder(cloned);
24+
context.Properties[typeof(GraphicsOptions)] = cloned;
25+
return context;
26+
}
27+
28+
/// <summary>
29+
/// Sets the default options against the configuration.
30+
/// </summary>
31+
/// <param name="configuration">The configuration to store default against.</param>
32+
/// <param name="optionsBuilder">The default options to use.</param>
33+
public static void SetGraphicsOptions(this Configuration configuration, Action<GraphicsOptions> optionsBuilder)
34+
{
35+
var cloned = configuration.GetGraphicsOptions().DeepClone();
36+
optionsBuilder(cloned);
37+
configuration.Properties[typeof(GraphicsOptions)] = cloned;
38+
}
39+
40+
/// <summary>
41+
/// Sets the default options against the image processing context.
42+
/// </summary>
43+
/// <param name="context">The image processing context to store default against.</param>
44+
/// <param name="options">The default options to use.</param>
45+
/// <returns>The passed in <paramref name="context"/> to allow chaining.</returns>
46+
public static IImageProcessingContext SetGraphicsOptions(this IImageProcessingContext context, GraphicsOptions options)
47+
{
48+
context.Properties[typeof(GraphicsOptions)] = options;
49+
return context;
50+
}
51+
52+
/// <summary>
53+
/// Sets the default options against the configuration.
54+
/// </summary>
55+
/// <param name="configuration">The configuration to store default against.</param>
56+
/// <param name="options">The default options to use.</param>
57+
public static void SetGraphicsOptions(this Configuration configuration, GraphicsOptions options)
58+
{
59+
configuration.Properties[typeof(GraphicsOptions)] = options;
60+
}
61+
62+
/// <summary>
63+
/// Gets the default options against the image processing context.
64+
/// </summary>
65+
/// <param name="context">The image processing context to retrieve defaults from.</param>
66+
/// <returns>The globaly configued default options.</returns>
67+
public static GraphicsOptions GetGraphicsOptions(this IImageProcessingContext context)
68+
{
69+
if (context.Properties.TryGetValue(typeof(GraphicsOptions), out var options) && options is GraphicsOptions go)
70+
{
71+
return go;
72+
}
73+
74+
var configOptions = context.Configuration.GetGraphicsOptions();
75+
76+
// do not cache the fall back to config into the the processing context
77+
// in case someone want to change the value on the config and expects it re trflow thru
78+
return configOptions;
79+
}
80+
81+
/// <summary>
82+
/// Gets the default options against the image processing context.
83+
/// </summary>
84+
/// <param name="configuration">The configuration to retrieve defaults from.</param>
85+
/// <returns>The globaly configued default options.</returns>
86+
public static GraphicsOptions GetGraphicsOptions(this Configuration configuration)
87+
{
88+
if (configuration.Properties.TryGetValue(typeof(GraphicsOptions), out var options) && options is GraphicsOptions go)
89+
{
90+
return go;
91+
}
92+
93+
var configOptions = new GraphicsOptions();
94+
95+
// capture the fallback so the same instance will always be returned in case its mutated
96+
configuration.Properties[typeof(GraphicsOptions)] = configOptions;
97+
return configOptions;
98+
}
99+
}
100+
}

src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System.Collections.Generic;
45
using SixLabors.ImageSharp.PixelFormats;
56
using SixLabors.ImageSharp.Processing.Processors;
67

@@ -39,6 +40,9 @@ public DefaultImageProcessorContext(Configuration configuration, Image<TPixel> s
3940
/// <inheritdoc/>
4041
public Configuration Configuration { get; }
4142

43+
/// <inheritdoc/>
44+
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
45+
4246
/// <inheritdoc/>
4347
public Image<TPixel> GetResultImage()
4448
{

src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static IImageProcessingContext DrawImage(
2323
Image image,
2424
float opacity)
2525
{
26-
var options = new GraphicsOptions();
26+
var options = source.GetGraphicsOptions();
2727
return source.ApplyProcessor(
2828
new DrawImageProcessor(
2929
image,
@@ -51,7 +51,7 @@ public static IImageProcessingContext DrawImage(
5151
image,
5252
Point.Empty,
5353
colorBlending,
54-
new GraphicsOptions().AlphaCompositionMode,
54+
source.GetGraphicsOptions().AlphaCompositionMode,
5555
opacity));
5656

5757
/// <summary>
@@ -104,7 +104,7 @@ public static IImageProcessingContext DrawImage(
104104
Point location,
105105
float opacity)
106106
{
107-
var options = new GraphicsOptions();
107+
var options = source.GetGraphicsOptions();
108108
return source.ApplyProcessor(
109109
new DrawImageProcessor(
110110
image,
@@ -134,7 +134,7 @@ public static IImageProcessingContext DrawImage(
134134
image,
135135
location,
136136
colorBlending,
137-
new GraphicsOptions().AlphaCompositionMode,
137+
source.GetGraphicsOptions().AlphaCompositionMode,
138138
opacity));
139139

140140
/// <summary>

src/ImageSharp/Processing/Extensions/Filters/LomographExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors and contributors.
1+
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using SixLabors.ImageSharp.Processing.Processors.Filters;
@@ -17,7 +17,7 @@ public static class LomographExtensions
1717
/// <param name="source">The image this method extends.</param>
1818
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
1919
public static IImageProcessingContext Lomograph(this IImageProcessingContext source)
20-
=> source.ApplyProcessor(new LomographProcessor());
20+
=> source.ApplyProcessor(new LomographProcessor(source.GetGraphicsOptions()));
2121

2222
/// <summary>
2323
/// Alters the colors of the image recreating an old Lomograph camera effect.
@@ -28,6 +28,6 @@ public static IImageProcessingContext Lomograph(this IImageProcessingContext sou
2828
/// </param>
2929
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
3030
public static IImageProcessingContext Lomograph(this IImageProcessingContext source, Rectangle rectangle)
31-
=> source.ApplyProcessor(new LomographProcessor(), rectangle);
31+
=> source.ApplyProcessor(new LomographProcessor(source.GetGraphicsOptions()), rectangle);
3232
}
33-
}
33+
}

src/ImageSharp/Processing/Extensions/Filters/PolaroidExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors and contributors.
1+
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using SixLabors.ImageSharp.Processing.Processors.Filters;
@@ -17,7 +17,7 @@ public static class PolaroidExtensions
1717
/// <param name="source">The image this method extends.</param>
1818
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
1919
public static IImageProcessingContext Polaroid(this IImageProcessingContext source)
20-
=> source.ApplyProcessor(new PolaroidProcessor());
20+
=> source.ApplyProcessor(new PolaroidProcessor(source.GetGraphicsOptions()));
2121

2222
/// <summary>
2323
/// Alters the colors of the image recreating an old Polaroid camera effect.
@@ -28,6 +28,6 @@ public static IImageProcessingContext Polaroid(this IImageProcessingContext sour
2828
/// </param>
2929
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
3030
public static IImageProcessingContext Polaroid(this IImageProcessingContext source, Rectangle rectangle)
31-
=> source.ApplyProcessor(new PolaroidProcessor(), rectangle);
31+
=> source.ApplyProcessor(new PolaroidProcessor(source.GetGraphicsOptions()), rectangle);
3232
}
3333
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class BackgroundColorExtensions
1818
/// <param name="color">The color to set as the background.</param>
1919
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2020
public static IImageProcessingContext BackgroundColor(this IImageProcessingContext source, Color color) =>
21-
BackgroundColor(source, new GraphicsOptions(), color);
21+
BackgroundColor(source, source.GetGraphicsOptions(), color);
2222

2323
/// <summary>
2424
/// Replaces the background color of image with the given one.
@@ -33,7 +33,7 @@ public static IImageProcessingContext BackgroundColor(
3333
this IImageProcessingContext source,
3434
Color color,
3535
Rectangle rectangle) =>
36-
BackgroundColor(source, new GraphicsOptions(), color, rectangle);
36+
BackgroundColor(source, source.GetGraphicsOptions(), color, rectangle);
3737

3838
/// <summary>
3939
/// Replaces the background color of image with the given one.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static class GlowExtensions
1717
/// <param name="source">The image this method extends.</param>
1818
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
1919
public static IImageProcessingContext Glow(this IImageProcessingContext source) =>
20-
Glow(source, new GraphicsOptions());
20+
Glow(source, source.GetGraphicsOptions());
2121

2222
/// <summary>
2323
/// Applies a radial glow effect to an image.
@@ -27,7 +27,7 @@ public static IImageProcessingContext Glow(this IImageProcessingContext source)
2727
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2828
public static IImageProcessingContext Glow(this IImageProcessingContext source, Color color)
2929
{
30-
return Glow(source, new GraphicsOptions(), color);
30+
return Glow(source, source.GetGraphicsOptions(), color);
3131
}
3232

3333
/// <summary>
@@ -37,7 +37,7 @@ public static IImageProcessingContext Glow(this IImageProcessingContext source,
3737
/// <param name="radius">The the radius.</param>
3838
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
3939
public static IImageProcessingContext Glow(this IImageProcessingContext source, float radius) =>
40-
Glow(source, new GraphicsOptions(), radius);
40+
Glow(source, source.GetGraphicsOptions(), radius);
4141

4242
/// <summary>
4343
/// Applies a radial glow effect to an image.
@@ -48,7 +48,7 @@ public static IImageProcessingContext Glow(this IImageProcessingContext source,
4848
/// </param>
4949
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
5050
public static IImageProcessingContext Glow(this IImageProcessingContext source, Rectangle rectangle) =>
51-
source.Glow(new GraphicsOptions(), rectangle);
51+
source.Glow(source.GetGraphicsOptions(), rectangle);
5252

5353
/// <summary>
5454
/// Applies a radial glow effect to an image.
@@ -65,7 +65,7 @@ public static IImageProcessingContext Glow(
6565
Color color,
6666
float radius,
6767
Rectangle rectangle) =>
68-
source.Glow(new GraphicsOptions(), color, ValueSize.Absolute(radius), rectangle);
68+
source.Glow(source.GetGraphicsOptions(), color, ValueSize.Absolute(radius), rectangle);
6969

7070
/// <summary>
7171
/// Applies a radial glow effect to an image.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static class VignetteExtensions
1717
/// <param name="source">The image this method extends.</param>
1818
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
1919
public static IImageProcessingContext Vignette(this IImageProcessingContext source) =>
20-
Vignette(source, new GraphicsOptions());
20+
Vignette(source, source.GetGraphicsOptions());
2121

2222
/// <summary>
2323
/// Applies a radial vignette effect to an image.
@@ -26,7 +26,7 @@ public static IImageProcessingContext Vignette(this IImageProcessingContext sour
2626
/// <param name="color">The color to set as the vignette.</param>
2727
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2828
public static IImageProcessingContext Vignette(this IImageProcessingContext source, Color color) =>
29-
Vignette(source, new GraphicsOptions(), color);
29+
Vignette(source, source.GetGraphicsOptions(), color);
3030

3131
/// <summary>
3232
/// Applies a radial vignette effect to an image.
@@ -39,7 +39,7 @@ public static IImageProcessingContext Vignette(
3939
this IImageProcessingContext source,
4040
float radiusX,
4141
float radiusY) =>
42-
Vignette(source, new GraphicsOptions(), radiusX, radiusY);
42+
Vignette(source, source.GetGraphicsOptions(), radiusX, radiusY);
4343

4444
/// <summary>
4545
/// Applies a radial vignette effect to an image.
@@ -50,7 +50,7 @@ public static IImageProcessingContext Vignette(
5050
/// </param>
5151
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
5252
public static IImageProcessingContext Vignette(this IImageProcessingContext source, Rectangle rectangle) =>
53-
Vignette(source, new GraphicsOptions(), rectangle);
53+
Vignette(source, source.GetGraphicsOptions(), rectangle);
5454

5555
/// <summary>
5656
/// Applies a radial vignette effect to an image.
@@ -69,7 +69,7 @@ public static IImageProcessingContext Vignette(
6969
float radiusX,
7070
float radiusY,
7171
Rectangle rectangle) =>
72-
source.Vignette(new GraphicsOptions(), color, radiusX, radiusY, rectangle);
72+
source.Vignette(source.GetGraphicsOptions(), color, radiusX, radiusY, rectangle);
7373

7474
/// <summary>
7575
/// Applies a radial vignette effect to an image.

src/ImageSharp/Processing/IImageProcessingContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System.Collections.Generic;
45
using SixLabors.ImageSharp.Processing.Processors;
56

67
namespace SixLabors.ImageSharp.Processing
@@ -15,6 +16,12 @@ public interface IImageProcessingContext
1516
/// </summary>
1617
Configuration Configuration { get; }
1718

19+
/// <summary>
20+
/// Gets a set of properties for the Image Processing Context.
21+
/// </summary>
22+
/// <remarks>This can be used for storing global settings and defaults to be accessable to processors.</remarks>
23+
IDictionary<object, object> Properties { get; }
24+
1825
/// <summary>
1926
/// Gets the image dimensions at the current point in the processing pipeline.
2027
/// </summary>

0 commit comments

Comments
 (0)