11// Copyright (c) Six Labors.
22// Licensed under the Six Labors Split License.
33
4- using System . Diagnostics . CodeAnalysis ;
5- using SixLabors . ImageSharp . Advanced ;
64using SixLabors . ImageSharp . Memory ;
75
86namespace SixLabors . ImageSharp . Drawing . Processing ;
97
108/// <summary>
119/// Convenience extension methods for creating drawing canvas instances from ImageSharp image types.
1210/// </summary>
13- public static class DrawingCanvasFactoryExtensions
11+ internal static class DrawingCanvasFactoryExtensions
1412{
15- /// <summary>
16- /// Creates a drawing canvas over a specific frame of an image whose pixel type is known only at runtime.
17- /// </summary>
18- /// <param name="image">The image containing the frame.</param>
19- /// <param name="options">Initial drawing options for this canvas instance.</param>
20- /// <param name="frameIndex">The zero-based frame index to target.</param>
21- /// <param name="clipPaths">Initial clip paths for this canvas instance.</param>
22- /// <returns>A drawing canvas targeting the selected frame.</returns>
23- public static DrawingCanvas CreateCanvas (
24- this Image image ,
25- DrawingOptions options ,
26- int frameIndex ,
27- params IPath [ ] clipPaths )
28- => CreateCanvas ( image , image . Configuration , options , frameIndex , clipPaths ) ;
29-
30- /// <summary>
31- /// Creates a drawing canvas over a specific frame of an image whose pixel type is known only at runtime.
32- /// </summary>
33- /// <param name="image">The image containing the frame.</param>
34- /// <param name="configuration">The configuration to use for this canvas instance.</param>
35- /// <param name="options">Initial drawing options for this canvas instance.</param>
36- /// <param name="frameIndex">The zero-based frame index to target.</param>
37- /// <param name="clipPaths">Initial clip paths for this canvas instance.</param>
38- /// <returns>A drawing canvas targeting the selected frame.</returns>
39- public static DrawingCanvas CreateCanvas (
40- this Image image ,
41- Configuration configuration ,
42- DrawingOptions options ,
43- int frameIndex ,
44- params IPath [ ] clipPaths )
45- {
46- Guard . NotNull ( image , nameof ( image ) ) ;
47- Guard . NotNull ( options , nameof ( options ) ) ;
48- Guard . NotNull ( clipPaths , nameof ( clipPaths ) ) ;
49- Guard . MustBeBetweenOrEqualTo ( frameIndex , 0 , image . Frames . Count - 1 , nameof ( frameIndex ) ) ;
50-
51- CreateCanvasVisitor visitor = new ( configuration , options , frameIndex , clipPaths ) ;
52- image . AcceptVisitor ( visitor ) ;
53- return visitor . Canvas ! ;
54- }
55-
56- /// <summary>
57- /// Creates a drawing canvas over the root frame of an image whose pixel type is known only at runtime.
58- /// </summary>
59- /// <param name="image">The image whose root frame should be targeted.</param>
60- /// <param name="options">Initial drawing options for this canvas instance.</param>
61- /// <param name="clipPaths">Initial clip paths for this canvas instance.</param>
62- /// <returns>A drawing canvas targeting the root frame.</returns>
63- public static DrawingCanvas CreateCanvas (
64- this Image image ,
65- DrawingOptions options ,
66- params IPath [ ] clipPaths )
67- => CreateCanvas ( image , image . Configuration , options , clipPaths ) ;
68-
69- /// <summary>
70- /// Creates a drawing canvas over the root frame of an image whose pixel type is known only at runtime.
71- /// </summary>
72- /// <param name="image">The image whose root frame should be targeted.</param>
73- /// <param name="configuration">The configuration to use for this canvas instance.</param>
74- /// <param name="options">Initial drawing options for this canvas instance.</param>
75- /// <param name="clipPaths">Initial clip paths for this canvas instance.</param>
76- /// <returns>A drawing canvas targeting the root frame.</returns>
77- public static DrawingCanvas CreateCanvas (
78- this Image image ,
79- Configuration configuration ,
80- DrawingOptions options ,
81- params IPath [ ] clipPaths )
82- {
83- Guard . NotNull ( image , nameof ( image ) ) ;
84- Guard . NotNull ( options , nameof ( options ) ) ;
85- Guard . NotNull ( clipPaths , nameof ( clipPaths ) ) ;
86-
87- CreateCanvasVisitor visitor = new ( configuration , options , frameIndex : 0 , clipPaths ) ;
88- image . AcceptVisitor ( visitor ) ;
89- return visitor . Canvas ! ;
90- }
91-
92- /// <summary>
93- /// Creates a drawing canvas over an existing frame.
94- /// </summary>
95- /// <typeparam name="TPixel">The pixel format.</typeparam>
96- /// <param name="frame">The frame backing the canvas.</param>
97- /// <param name="options">Initial drawing options for this canvas instance.</param>
98- /// <param name="clipPaths">Initial clip paths for this canvas instance.</param>
99- /// <returns>A drawing canvas targeting <paramref name="frame"/>.</returns>
100- public static DrawingCanvas CreateCanvas < TPixel > (
101- this ImageFrame < TPixel > frame ,
102- DrawingOptions options ,
103- params IPath [ ] clipPaths )
104- where TPixel : unmanaged, IPixel < TPixel >
105- => CreateCanvas ( frame , frame . Configuration , options , clipPaths ) ;
106-
10713 /// <summary>
10814 /// Creates a drawing canvas over an existing frame.
10915 /// </summary>
@@ -113,7 +19,7 @@ public static DrawingCanvas CreateCanvas<TPixel>(
11319 /// <param name="options">Initial drawing options for this canvas instance.</param>
11420 /// <param name="clipPaths">Initial clip paths for this canvas instance.</param>
11521 /// <returns>A drawing canvas targeting <paramref name="frame"/>.</returns>
116- public static DrawingCanvas CreateCanvas < TPixel > (
22+ internal static DrawingCanvas CreateCanvas < TPixel > (
11723 this ImageFrame < TPixel > frame ,
11824 Configuration configuration ,
11925 DrawingOptions options ,
@@ -130,116 +36,4 @@ public static DrawingCanvas CreateCanvas<TPixel>(
13036 new Buffer2DRegion < TPixel > ( frame . PixelBuffer ) ,
13137 clipPaths ) ;
13238 }
133-
134- /// <summary>
135- /// Creates a drawing canvas over a specific frame of an image.
136- /// </summary>
137- /// <typeparam name="TPixel">The pixel format.</typeparam>
138- /// <param name="image">The image containing the frame.</param>
139- /// <param name="options">Initial drawing options for this canvas instance.</param>
140- /// <param name="frameIndex">The zero-based frame index to target.</param>
141- /// <param name="clipPaths">Initial clip paths for this canvas instance.</param>
142- /// <returns>A drawing canvas targeting the selected frame.</returns>
143- public static DrawingCanvas CreateCanvas < TPixel > (
144- this Image < TPixel > image ,
145- DrawingOptions options ,
146- int frameIndex ,
147- params IPath [ ] clipPaths )
148- where TPixel : unmanaged, IPixel < TPixel >
149- => CreateCanvas ( image , image . Configuration , options , frameIndex , clipPaths ) ;
150-
151- /// <summary>
152- /// Creates a drawing canvas over a specific frame of an image.
153- /// </summary>
154- /// <typeparam name="TPixel">The pixel format.</typeparam>
155- /// <param name="image">The image containing the frame.</param>
156- /// <param name="configuration">The configuration to use for this canvas instance.</param>
157- /// <param name="options">Initial drawing options for this canvas instance.</param>
158- /// <param name="frameIndex">The zero-based frame index to target.</param>
159- /// <param name="clipPaths">Initial clip paths for this canvas instance.</param>
160- /// <returns>A drawing canvas targeting the selected frame.</returns>
161- public static DrawingCanvas CreateCanvas < TPixel > (
162- this Image < TPixel > image ,
163- Configuration configuration ,
164- DrawingOptions options ,
165- int frameIndex ,
166- params IPath [ ] clipPaths )
167- where TPixel : unmanaged, IPixel < TPixel >
168- {
169- Guard . NotNull ( image , nameof ( image ) ) ;
170- Guard . NotNull ( options , nameof ( options ) ) ;
171- Guard . NotNull ( clipPaths , nameof ( clipPaths ) ) ;
172- Guard . MustBeBetweenOrEqualTo ( frameIndex , 0 , image . Frames . Count - 1 , nameof ( frameIndex ) ) ;
173-
174- return image . Frames [ frameIndex ] . CreateCanvas ( configuration , options , clipPaths ) ;
175- }
176-
177- /// <summary>
178- /// Creates a drawing canvas over the root frame of an image.
179- /// </summary>
180- /// <typeparam name="TPixel">The pixel format.</typeparam>
181- /// <param name="image">The image whose root frame should be targeted.</param>
182- /// <param name="options">Initial drawing options for this canvas instance.</param>
183- /// <param name="clipPaths">Initial clip paths for this canvas instance.</param>
184- /// <returns>A drawing canvas targeting the root frame.</returns>
185- public static DrawingCanvas CreateCanvas < TPixel > (
186- this Image < TPixel > image ,
187- DrawingOptions options ,
188- params IPath [ ] clipPaths )
189- where TPixel : unmanaged, IPixel < TPixel >
190- => CreateCanvas ( image , image . Configuration , options , clipPaths ) ;
191-
192- /// <summary>
193- /// Creates a drawing canvas over the root frame of an image.
194- /// </summary>
195- /// <typeparam name="TPixel">The pixel format.</typeparam>
196- /// <param name="image">The image whose root frame should be targeted.</param>
197- /// <param name="configuration">The configuration to use for this canvas instance.</param>
198- /// <param name="options">Initial drawing options for this canvas instance.</param>
199- /// <param name="clipPaths">Initial clip paths for this canvas instance.</param>
200- /// <returns>A drawing canvas targeting the root frame.</returns>
201- public static DrawingCanvas CreateCanvas < TPixel > (
202- this Image < TPixel > image ,
203- Configuration configuration ,
204- DrawingOptions options ,
205- params IPath [ ] clipPaths )
206- where TPixel : unmanaged, IPixel < TPixel >
207- {
208- Guard . NotNull ( image , nameof ( image ) ) ;
209- Guard . NotNull ( options , nameof ( options ) ) ;
210- Guard . NotNull ( clipPaths , nameof ( clipPaths ) ) ;
211-
212- return image . Frames . RootFrame . CreateCanvas ( configuration , options , clipPaths ) ;
213- }
214-
215- private sealed class CreateCanvasVisitor : IImageVisitor
216- {
217- private readonly Configuration configuration ;
218- private readonly DrawingOptions options ;
219- private readonly int frameIndex ;
220- private readonly IPath [ ] clipPaths ;
221-
222- public CreateCanvasVisitor (
223- Configuration configuration ,
224- DrawingOptions options ,
225- int frameIndex ,
226- IPath [ ] clipPaths )
227- {
228- this . configuration = configuration ;
229- this . options = options ;
230- this . frameIndex = frameIndex ;
231- this . clipPaths = clipPaths ;
232- }
233-
234- public DrawingCanvas ? Canvas { get ; private set ; }
235-
236- [ MemberNotNull ( nameof ( Canvas ) ) ]
237- public void Visit < TPixel > ( Image < TPixel > image )
238- where TPixel : unmanaged, IPixel < TPixel >
239- => this . Canvas = image . CreateCanvas (
240- this . configuration ,
241- this . options ,
242- this . frameIndex ,
243- this . clipPaths ) ;
244- }
24539}
0 commit comments