|
| 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 | +} |
0 commit comments