// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using SixLabors.ImageSharp.Drawing.Processing.Backends; using SixLabors.ImageSharp.Processing; namespace SixLabors.ImageSharp.Drawing.Processing { /// /// Adds extensions that allow configuring the drawing backend implementation. /// public static class RasterizerDefaultsExtensions { /// /// Sets the drawing backend against the source image processing context. /// /// The image processing context to store the backend against. /// The backend to use. /// The passed in to allow chaining. internal static IImageProcessingContext SetDrawingBackend(this IImageProcessingContext context, IDrawingBackend backend) { Guard.NotNull(backend, nameof(backend)); context.Properties[typeof(IDrawingBackend)] = backend; return context; } /// /// Sets the default drawing backend against the configuration. /// /// The configuration to store the backend against. /// The backend to use. public static void SetDrawingBackend(this Configuration configuration, IDrawingBackend backend) { Guard.NotNull(backend, nameof(backend)); configuration.Properties[typeof(IDrawingBackend)] = backend; } /// /// Gets the drawing backend from the source image processing context. /// /// The image processing context to retrieve the backend from. /// The configured backend. internal static IDrawingBackend GetDrawingBackend(this IImageProcessingContext context) { if (context.Properties.TryGetValue(typeof(IDrawingBackend), out object? backend) && backend is IDrawingBackend configured) { return configured; } return context.Configuration.GetDrawingBackend(); } /// /// Gets the default drawing backend from the configuration. /// /// The configuration to retrieve the backend from. /// The configured backend. internal static IDrawingBackend GetDrawingBackend(this Configuration configuration) { if (configuration.Properties.TryGetValue(typeof(IDrawingBackend), out object? backend) && backend is IDrawingBackend configured) { return configured; } IDrawingBackend defaultBackend = DefaultDrawingBackend.Instance; configuration.Properties[typeof(IDrawingBackend)] = defaultBackend; return defaultBackend; } } }