// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Drawing.Processing {
///
/// Extension methods for creating drawing canvas instances over ImageSharp image frames.
///
public static class DrawingCanvasFactoryExtensions
{
///
/// Creates a drawing canvas over an existing typed image frame.
///
///
/// The caller owns the returned canvas and must dispose it to replay recorded work into the frame.
///
/// The pixel format.
/// The frame backing the canvas.
/// The configuration to use for this canvas instance.
/// Initial drawing options for this canvas instance.
/// Initial clip paths for this canvas instance.
/// A drawing canvas targeting .
public static DrawingCanvas CreateCanvas(
this ImageFrame frame,
Configuration configuration,
DrawingOptions options,
params IPath[] clipPaths)
where TPixel : unmanaged, IPixel
{
Guard.NotNull(frame, nameof(frame));
Guard.NotNull(options, nameof(options));
Guard.NotNull(clipPaths, nameof(clipPaths));
return new DrawingCanvas(
configuration,
options,
frame.PixelBuffer.GetRegion(),
clipPaths);
}
///
/// Creates a drawing canvas over an existing image frame.
///
///
/// The caller owns the returned canvas and must dispose it to replay recorded work into the frame.
///
/// The frame backing the canvas.
/// The configuration to use for this canvas instance.
/// Initial drawing options for this canvas instance.
/// Initial clip paths for this canvas instance.
/// A drawing canvas targeting .
public static DrawingCanvas CreateCanvas(
this ImageFrame frame,
Configuration configuration,
DrawingOptions options,
params IPath[] clipPaths)
{
Guard.NotNull(frame, nameof(frame));
Guard.NotNull(options, nameof(options));
Guard.NotNull(clipPaths, nameof(clipPaths));
CanvasFactoryVisitor visitor = new(configuration, options, clipPaths);
frame.AcceptVisitor(visitor);
return visitor.Value!;
}
private struct CanvasFactoryVisitor : IImageFrameVisitor
{
private readonly Configuration configuration;
private readonly DrawingOptions options;
private readonly IPath[] clipPaths;
public CanvasFactoryVisitor(Configuration configuration, DrawingOptions options, IPath[] clipPaths)
{
this.configuration = configuration;
this.options = options;
this.clipPaths = clipPaths;
}
public DrawingCanvas? Value { get; private set; }
void IImageFrameVisitor.Visit(ImageFrame frame)
=> this.Value = frame.CreateCanvas(this.configuration, this.options, this.clipPaths);
}
}
}