Skip to content

Commit b004888

Browse files
Merge pull request #1010 from SixLabors/feature/visitor
Expose visitor through advanced namespace.
2 parents a6f4f8e + ac4d71c commit b004888

File tree

7 files changed

+50
-30
lines changed

7 files changed

+50
-30
lines changed

src/ImageSharp.Drawing/Processing/Processors/Drawing/DrawImageProcessor.cs

Lines changed: 1 addition & 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 SixLabors.ImageSharp.Advanced;
45
using SixLabors.ImageSharp.PixelFormats;
56
using SixLabors.Primitives;
67

src/ImageSharp/Advanced/AdvancedImageExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ namespace SixLabors.ImageSharp.Advanced
1515
/// </summary>
1616
public static class AdvancedImageExtensions
1717
{
18+
/// <summary>
19+
/// Accepts a <see cref="IImageVisitor"/> to implement a double-dispatch pattern in order to
20+
/// apply pixel-specific operations on non-generic <see cref="Image"/> instances
21+
/// </summary>
22+
/// <param name="source">The source.</param>
23+
/// <param name="visitor">The visitor.</param>
24+
public static void AcceptVisitor(this Image source, IImageVisitor visitor)
25+
=> source.Accept(visitor);
26+
1827
/// <summary>
1928
/// Gets the configuration for the image.
2029
/// </summary>

src/ImageSharp/IImageVisitor.cs renamed to src/ImageSharp/Advanced/IImageVisitor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
using SixLabors.ImageSharp.PixelFormats;
55

6-
namespace SixLabors.ImageSharp
6+
namespace SixLabors.ImageSharp.Advanced
77
{
88
/// <summary>
9-
/// A visitor to implement double-dispatch pattern in order to apply pixel-specific operations
10-
/// on non-generic <see cref="Image"/> instances. The operation is dispatched by <see cref="Image.AcceptVisitor"/>.
9+
/// A visitor to implement a double-dispatch pattern in order to apply pixel-specific operations
10+
/// on non-generic <see cref="Image"/> instances.
1111
/// </summary>
12-
internal interface IImageVisitor
12+
public interface IImageVisitor
1313
{
1414
/// <summary>
1515
/// Provides a pixel-specific implementation for a given operation.
@@ -19,4 +19,4 @@ internal interface IImageVisitor
1919
void Visit<TPixel>(Image<TPixel> image)
2020
where TPixel : struct, IPixel<TPixel>;
2121
}
22-
}
22+
}

src/ImageSharp/Image.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ public void Save(Stream stream, IImageEncoder encoder)
100100
Guard.NotNull(encoder, nameof(encoder));
101101
this.EnsureNotDisposed();
102102

103-
var visitor = new EncodeVisitor(encoder, stream);
104-
this.AcceptVisitor(visitor);
103+
this.AcceptVisitor(new EncodeVisitor(encoder, stream));
105104
}
106105

107106
/// <summary>
@@ -121,13 +120,6 @@ public Image<TPixel2> CloneAs<TPixel2>()
121120
public abstract Image<TPixel2> CloneAs<TPixel2>(Configuration configuration)
122121
where TPixel2 : struct, IPixel<TPixel2>;
123122

124-
/// <summary>
125-
/// Accept a <see cref="IImageVisitor"/>.
126-
/// Implemented by <see cref="Image{TPixel}"/> invoking <see cref="IImageVisitor.Visit{TPixel}"/>
127-
/// with the pixel type of the image.
128-
/// </summary>
129-
internal abstract void AcceptVisitor(IImageVisitor visitor);
130-
131123
/// <summary>
132124
/// Update the size of the image after mutation.
133125
/// </summary>
@@ -145,6 +137,14 @@ public abstract Image<TPixel2> CloneAs<TPixel2>(Configuration configuration)
145137
/// </summary>
146138
internal abstract void EnsureNotDisposed();
147139

140+
/// <summary>
141+
/// Accepts a <see cref="IImageVisitor"/>.
142+
/// Implemented by <see cref="Image{TPixel}"/> invoking <see cref="IImageVisitor.Visit{TPixel}"/>
143+
/// with the pixel type of the image.
144+
/// </summary>
145+
/// <param name="visitor">The visitor.</param>
146+
internal abstract void Accept(IImageVisitor visitor);
147+
148148
private class EncodeVisitor : IImageVisitor
149149
{
150150
private readonly IImageEncoder encoder;

src/ImageSharp/Image{TPixel}.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,17 @@ internal override void EnsureNotDisposed()
211211
}
212212
}
213213

214+
/// <inheritdoc/>
215+
public override string ToString() => $"Image<{typeof(TPixel).Name}>: {this.Width}x{this.Height}";
216+
214217
/// <inheritdoc />
215-
internal override void AcceptVisitor(IImageVisitor visitor)
218+
internal override void Accept(IImageVisitor visitor)
216219
{
217220
this.EnsureNotDisposed();
218221

219222
visitor.Visit(this);
220223
}
221224

222-
/// <inheritdoc/>
223-
public override string ToString() => $"Image<{typeof(TPixel).Name}>: {this.Width}x{this.Height}";
224-
225225
/// <summary>
226226
/// Switches the buffers used by the image and the pixelSource meaning that the Image will "own" the buffer from the pixelSource and the pixelSource will now own the Images buffer.
227227
/// </summary>

src/ImageSharp/Processing/Extensions/ProcessingExtensions.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public static void Mutate(this Image source, Action<IImageProcessingContext> ope
2525
Guard.NotNull(operation, nameof(operation));
2626
source.EnsureNotDisposed();
2727

28-
var visitor = new ProcessingVisitor(operation, true);
29-
source.AcceptVisitor(visitor);
28+
source.AcceptVisitor(new ProcessingVisitor(operation, true));
3029
}
3130

3231
/// <summary>
@@ -42,8 +41,10 @@ public static void Mutate<TPixel>(this Image<TPixel> source, Action<IImageProces
4241
Guard.NotNull(operation, nameof(operation));
4342
source.EnsureNotDisposed();
4443

45-
IInternalImageProcessingContext<TPixel> operationsRunner = source.GetConfiguration().ImageOperationsProvider
46-
.CreateImageProcessingContext(source, true);
44+
IInternalImageProcessingContext<TPixel> operationsRunner
45+
= source.GetConfiguration()
46+
.ImageOperationsProvider.CreateImageProcessingContext(source, true);
47+
4748
operation(operationsRunner);
4849
}
4950

@@ -60,8 +61,10 @@ public static void Mutate<TPixel>(this Image<TPixel> source, params IImageProces
6061
Guard.NotNull(operations, nameof(operations));
6162
source.EnsureNotDisposed();
6263

63-
IInternalImageProcessingContext<TPixel> operationsRunner = source.GetConfiguration().ImageOperationsProvider
64-
.CreateImageProcessingContext(source, true);
64+
IInternalImageProcessingContext<TPixel> operationsRunner
65+
= source.GetConfiguration()
66+
.ImageOperationsProvider.CreateImageProcessingContext(source, true);
67+
6568
operationsRunner.ApplyProcessors(operations);
6669
}
6770

@@ -96,8 +99,10 @@ public static Image<TPixel> Clone<TPixel>(this Image<TPixel> source, Action<IIma
9699
Guard.NotNull(operation, nameof(operation));
97100
source.EnsureNotDisposed();
98101

99-
IInternalImageProcessingContext<TPixel> operationsRunner = source.GetConfiguration().ImageOperationsProvider
100-
.CreateImageProcessingContext(source, false);
102+
IInternalImageProcessingContext<TPixel> operationsRunner
103+
= source.GetConfiguration()
104+
.ImageOperationsProvider.CreateImageProcessingContext(source, false);
105+
101106
operation(operationsRunner);
102107
return operationsRunner.GetResultImage();
103108
}
@@ -116,8 +121,10 @@ public static Image<TPixel> Clone<TPixel>(this Image<TPixel> source, params IIma
116121
Guard.NotNull(operations, nameof(operations));
117122
source.EnsureNotDisposed();
118123

119-
IInternalImageProcessingContext<TPixel> operationsRunner = source.GetConfiguration().ImageOperationsProvider
120-
.CreateImageProcessingContext(source, false);
124+
IInternalImageProcessingContext<TPixel> operationsRunner
125+
= source.GetConfiguration()
126+
.ImageOperationsProvider.CreateImageProcessingContext(source, false);
127+
121128
operationsRunner.ApplyProcessors(operations);
122129
return operationsRunner.GetResultImage();
123130
}
@@ -157,8 +164,10 @@ public ProcessingVisitor(Action<IImageProcessingContext> operation, bool mutate)
157164
public void Visit<TPixel>(Image<TPixel> image)
158165
where TPixel : struct, IPixel<TPixel>
159166
{
160-
IInternalImageProcessingContext<TPixel> operationsRunner = image.GetConfiguration()
161-
.ImageOperationsProvider.CreateImageProcessingContext(image, this.mutate);
167+
IInternalImageProcessingContext<TPixel> operationsRunner =
168+
image.GetConfiguration()
169+
.ImageOperationsProvider.CreateImageProcessingContext(image, this.mutate);
170+
162171
this.operation(operationsRunner);
163172
this.ResultImage = operationsRunner.GetResultImage();
164173
}

src/ImageSharp/Processing/Processors/ImageProcessorExtensions.cs

Lines changed: 1 addition & 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 SixLabors.ImageSharp.Advanced;
45
using SixLabors.ImageSharp.PixelFormats;
56
using SixLabors.Primitives;
67

0 commit comments

Comments
 (0)