forked from SixLabors/ImageSharp.Drawing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawingCanvasTests.DrawImage.cs
More file actions
292 lines (251 loc) · 12.5 KB
/
Copy pathDrawingCanvasTests.DrawImage.cs
File metadata and controls
292 lines (251 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Drawing.Tests.TestUtilities.ImageComparison;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
namespace SixLabors.ImageSharp.Drawing.Tests.Processing;
public partial class DrawingCanvasTests
{
[Theory]
[WithBasicTestPatternImages(384, 256, PixelTypes.Rgba32)]
public void DrawImage_WithRotationTransform_MatchesReference<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> foreground = provider.GetImage();
using Image<TPixel> target = new(384, 256);
DrawingOptions options = new()
{
Transform = new Matrix4x4(Matrix3x2.CreateRotation(MathF.PI / 4F, new Vector2(192F, 128F)))
};
using (DrawingCanvas<TPixel> canvas = CreateCanvas(provider, target, options))
{
canvas.Clear(Brushes.Solid(Color.White));
canvas.DrawImage(
foreground,
foreground.Bounds,
new RectangleF(72, 48, 240, 160),
KnownResamplers.NearestNeighbor);
}
target.DebugSave(provider, appendSourceFileOrDescription: false);
// Ubunut with .NET10 has some minor difference due to nearest neightbor resampling, so we need to use a tolerant comparer here.
target.CompareToReferenceOutput(ImageComparer.TolerantPercentage(0.0080F), provider, appendSourceFileOrDescription: false);
}
[Theory]
[WithBasicTestPatternImages(320, 220, PixelTypes.Rgba32)]
public void DrawImage_WithSourceClippingAndScaling_MatchesReference<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> foreground = provider.GetImage();
using Image<TPixel> target = new(320, 220);
using (DrawingCanvas<TPixel> canvas = CreateCanvas(provider, target, new DrawingOptions()))
{
canvas.Clear(Brushes.Solid(Color.White));
canvas.DrawImage(
foreground,
new Rectangle(-48, 18, 196, 148),
new RectangleF(18, 20, 170, 120),
KnownResamplers.Bicubic);
canvas.DrawImage(
foreground,
new Rectangle(220, 100, 160, 140),
new RectangleF(170, 72, 130, 110),
KnownResamplers.NearestNeighbor);
canvas.Draw(Pens.Solid(Color.Black, 3), new Rectangle(8, 8, 304, 204));
}
target.DebugSave(provider, appendSourceFileOrDescription: false);
target.CompareToReferenceOutput(provider, appendSourceFileOrDescription: false);
}
[Theory]
[WithBasicTestPatternImages(360, 240, PixelTypes.Rgba32)]
public void DrawImage_WithClipPathAndTransform_MatchesReference<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> foreground = provider.GetImage();
using Image<TPixel> target = new(360, 240);
DrawingOptions transformedOptions = new()
{
Transform = new Matrix4x4(Matrix3x2.CreateRotation(0.32F, new Vector2(180, 120)))
};
IPath clipPath = new EllipsePolygon(new PointF(180, 120), new SizeF(208, 126));
using (DrawingCanvas<TPixel> canvas = CreateCanvas(provider, target, new DrawingOptions()))
{
canvas.Clear(Brushes.Solid(Color.White));
canvas.Fill(Brushes.Solid(Color.LightGray.WithAlpha(0.45F)), new Rectangle(18, 16, 324, 208));
_ = canvas.Save(transformedOptions, clipPath);
canvas.DrawImage(
foreground,
new Rectangle(10, 8, 234, 180),
new RectangleF(64, 36, 232, 164),
KnownResamplers.Bicubic);
canvas.DrawImage(
foreground,
new Rectangle(102, 32, 196, 166),
new RectangleF(92, 58, 210, 148),
KnownResamplers.NearestNeighbor);
canvas.Restore();
canvas.Draw(Pens.DashDot(Color.DarkSlateGray, 3F), clipPath);
canvas.Draw(Pens.Solid(Color.Black, 2F), new Rectangle(8, 8, 344, 224));
}
target.DebugSave(provider, appendSourceFileOrDescription: false);
target.CompareToReferenceOutput(provider, appendSourceFileOrDescription: false);
}
[Theory]
[WithBasicTestPatternImages(320, 240, PixelTypes.Rgba32)]
public void DrawImage_WithForeignPixelFormat_MatchesFullConversion<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
=> AssertForeignPixelFormatMatchesFullConversion(
provider,
new Rectangle(64, 48, 180, 150),
new RectangleF(40, 30, 200, 170),
new Matrix4x4(Matrix3x2.CreateRotation(0.28F, new Vector2(160, 120))));
[Theory]
[WithBasicTestPatternImages(320, 240, PixelTypes.Rgba32)]
public void DrawImage_WithForeignPixelFormat_PartialRegionNoTransform_MatchesFullConversion<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
=> AssertForeignPixelFormatMatchesFullConversion(
provider,
new Rectangle(64, 48, 180, 150),
new RectangleF(40, 30, 200, 170),
Matrix4x4.Identity);
[Theory]
[WithBasicTestPatternImages(320, 240, PixelTypes.Rgba32)]
public void DrawImage_WithForeignPixelFormat_SourceOutsideTopLeft_MatchesFullConversion<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
=> AssertForeignPixelFormatMatchesFullConversion(
provider,
new Rectangle(-48, -32, 220, 190),
new RectangleF(30, 24, 210, 180),
new Matrix4x4(Matrix3x2.CreateRotation(0.21F, new Vector2(160, 120))));
[Theory]
[WithBasicTestPatternImages(320, 240, PixelTypes.Rgba32)]
public void DrawImage_WithForeignPixelFormat_SourceOutsideBottomRight_MatchesFullConversion<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
=> AssertForeignPixelFormatMatchesFullConversion(
provider,
new Rectangle(200, 150, 260, 220),
new RectangleF(48, 40, 200, 168),
Matrix4x4.Identity);
[Theory]
[WithBasicTestPatternImages(320, 240, PixelTypes.Rgba32)]
public void DrawImage_WithForeignPixelFormat_ProjectiveTransform_MatchesFullConversion<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
// A quad/projective transform (non-affine Matrix4x4 with perspective terms) combined
// with a rotation, exercising the transform path over the clipped region.
Matrix4x4 projective = new Matrix4x4(Matrix3x2.CreateRotation(0.18F, new Vector2(160, 120)))
{
M14 = 0.0006F,
M24 = 0.0004F
};
AssertForeignPixelFormatMatchesFullConversion(
provider,
new Rectangle(56, 40, 190, 160),
new RectangleF(44, 34, 200, 168),
projective);
}
[Theory]
[WithBasicTestPatternImages(320, 240, PixelTypes.Rgba32)]
public void DrawImage_WithForeignPixelFormat_WholeImage_MatchesFullConversion<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
=> AssertForeignPixelFormatMatchesFullConversion(
provider,
new Rectangle(0, 0, 320, 240),
new RectangleF(24, 20, 260, 200),
new Matrix4x4(Matrix3x2.CreateRotation(0.15F, new Vector2(160, 120))));
[Theory]
[WithBasicTestPatternImages(320, 240, PixelTypes.Rgba32)]
public void DrawImage_WithEmptySourceRect_IsNoOp<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
=> AssertDrawImageIsNoOp(
provider,
new Rectangle(40, 30, 0, 120),
new RectangleF(20, 20, 200, 160));
[Theory]
[WithBasicTestPatternImages(320, 240, PixelTypes.Rgba32)]
public void DrawImage_WithEmptyDestinationRect_IsNoOp<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
=> AssertDrawImageIsNoOp(
provider,
new Rectangle(40, 30, 180, 150),
new RectangleF(20, 20, 200, 0));
[Theory]
[WithBasicTestPatternImages(320, 240, PixelTypes.Rgba32)]
public void DrawImage_WithSourceRectFullyOutsideImage_IsNoOp<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
=> AssertDrawImageIsNoOp(
provider,
new Rectangle(400, 300, 120, 100),
new RectangleF(20, 20, 200, 160));
/// <summary>
/// A draw whose clipped source/destination region is empty must be a no-op for both the
/// typed <see cref="Image{TPixel}"/> overload and the foreign-pixel-format <see cref="Image"/>
/// overload, leaving the cleared background untouched.
/// </summary>
private static void AssertDrawImageIsNoOp<TPixel>(
TestImageProvider<TPixel> provider,
Rectangle sourceRect,
RectangleF destinationRect)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> source = provider.GetImage();
// A source image whose pixel format differs from the canvas, forcing the foreign-format path.
using Image<Rgb24> foreignSource = source.CloneAs<Rgb24>();
// The reference is the cleared background: a degenerate draw must not change any pixel.
using Image<TPixel> expected = new(source.Width, source.Height);
using (DrawingCanvas<TPixel> reference = CreateCanvas(provider, expected, new DrawingOptions()))
{
reference.Clear(Brushes.Solid(Color.White));
}
void AssertNoOp(Action<DrawingCanvas<TPixel>> draw)
{
using Image<TPixel> actual = new(source.Width, source.Height);
using (DrawingCanvas<TPixel> canvas = CreateCanvas(provider, actual, new DrawingOptions()))
{
canvas.Clear(Brushes.Solid(Color.White));
draw(canvas);
}
ImageComparer.Exact.VerifySimilarity(expected, actual);
}
// Typed overload -> DrawImageCore empty-region early-return.
AssertNoOp(canvas => canvas.DrawImage(source, sourceRect, destinationRect, KnownResamplers.Bicubic));
// Foreign-format overload -> DrawImage empty-region early-return before any conversion.
AssertNoOp(canvas => canvas.DrawImage((Image)foreignSource, sourceRect, destinationRect, KnownResamplers.Bicubic));
}
/// <summary>
/// Drawing a foreign-pixel-format image (which converts only the clipped source region) must
/// produce pixels identical to first converting the whole image to the canvas format and drawing that.
/// </summary>
private static void AssertForeignPixelFormatMatchesFullConversion<TPixel>(
TestImageProvider<TPixel> provider,
Rectangle sourceRect,
RectangleF destinationRect,
Matrix4x4 transform)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> source = provider.GetImage();
// A source image whose pixel format differs from the canvas, forcing a per-pixel conversion.
using Image<Rgb24> foreignSource = source.CloneAs<Rgb24>();
// Reference source: the whole foreign image converted up-front to the canvas format.
using Image<TPixel> convertedSource = foreignSource.CloneAs<TPixel>();
DrawingOptions options = new()
{
Transform = transform
};
using Image<TPixel> actual = new(source.Width, source.Height);
using Image<TPixel> expected = new(source.Width, source.Height);
using (DrawingCanvas<TPixel> canvas = CreateCanvas(provider, actual, options))
{
canvas.Clear(Brushes.Solid(Color.White));
canvas.DrawImage((Image)foreignSource, sourceRect, destinationRect, KnownResamplers.Bicubic);
}
using (DrawingCanvas<TPixel> canvas = CreateCanvas(provider, expected, options))
{
canvas.Clear(Brushes.Solid(Color.White));
canvas.DrawImage(convertedSource, sourceRect, destinationRect, KnownResamplers.Bicubic);
}
// Converting only the clipped region must produce pixels identical to converting the whole image.
ImageComparer.Exact.VerifySimilarity(expected, actual);
}
}