-
-
Notifications
You must be signed in to change notification settings - Fork 893
Expand file tree
/
Copy pathImageTests.cs
More file actions
365 lines (308 loc) · 13.7 KB
/
Copy pathImageTests.cs
File metadata and controls
365 lines (308 loc) · 13.7 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.IO;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.Memory;
using Xunit;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests
{
/// <summary>
/// Tests the <see cref="Image"/> class.
/// </summary>
public partial class ImageTests
{
public class Constructor
{
[Fact]
public void Width_Height()
{
using (var image = new Image<Rgba32>(11, 23))
{
Assert.Equal(11, image.Width);
Assert.Equal(23, image.Height);
Assert.True(image.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> imageMem));
Assert.Equal(11 * 23, imageMem.Length);
image.ComparePixelBufferTo(default(Rgba32));
Assert.Equal(Configuration.Default, image.GetConfiguration());
}
}
[Fact]
public void Width_Height_SizeNotRepresentable_ThrowsInvalidImageOperationException()
=> Assert.Throws<InvalidMemoryOperationException>(() => new Image<Rgba32>(int.MaxValue, int.MaxValue));
[Fact]
public void Configuration_Width_Height()
{
Configuration configuration = Configuration.Default.Clone();
using (var image = new Image<Rgba32>(configuration, 11, 23))
{
Assert.Equal(11, image.Width);
Assert.Equal(23, image.Height);
Assert.True(image.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> imageMem));
Assert.Equal(11 * 23, imageMem.Length);
image.ComparePixelBufferTo(default(Rgba32));
Assert.Equal(configuration, image.GetConfiguration());
}
}
[Fact]
public void Configuration_Width_Height_BackgroundColor()
{
Configuration configuration = Configuration.Default.Clone();
Rgba32 color = Color.Aquamarine;
using (var image = new Image<Rgba32>(configuration, 11, 23, color))
{
Assert.Equal(11, image.Width);
Assert.Equal(23, image.Height);
Assert.True(image.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> imageMem));
Assert.Equal(11 * 23, imageMem.Length);
image.ComparePixelBufferTo(color);
Assert.Equal(configuration, image.GetConfiguration());
}
}
[Fact]
public void CreateUninitialized()
{
Configuration configuration = Configuration.Default.Clone();
byte dirtyValue = 123;
configuration.MemoryAllocator = new TestMemoryAllocator(dirtyValue);
var metadata = new ImageMetadata();
using (var image = Image.CreateUninitialized<L8>(configuration, 21, 22, metadata))
{
Assert.Equal(21, image.Width);
Assert.Equal(22, image.Height);
Assert.Same(configuration, image.GetConfiguration());
Assert.Same(metadata, image.Metadata);
Assert.Equal(dirtyValue, image[5, 5].PackedValue);
}
}
}
public class Indexer
{
private readonly Configuration configuration = Configuration.CreateDefaultInstance();
private void LimitBufferCapacity(int bufferCapacityInBytes) =>
this.configuration.MemoryAllocator = new TestMemoryAllocator { BufferCapacityInBytes = bufferCapacityInBytes };
[Theory]
[InlineData(false)]
[InlineData(true)]
public void GetSet(bool enforceDisco)
{
if (enforceDisco)
{
this.LimitBufferCapacity(100);
}
using var image = new Image<Rgba32>(this.configuration, 10, 10);
Rgba32 val = image[3, 4];
Assert.Equal(default(Rgba32), val);
image[3, 4] = Color.Red;
val = image[3, 4];
Assert.Equal(Color.Red.ToRgba32(), val);
}
public static TheoryData<bool, int> OutOfRangeData = new TheoryData<bool, int>()
{
{ false, -1 },
{ false, 10 },
{ true, -1 },
{ true, 10 },
};
[Theory]
[MemberData(nameof(OutOfRangeData))]
public void Get_OutOfRangeX(bool enforceDisco, int x)
{
if (enforceDisco)
{
this.LimitBufferCapacity(100);
}
using var image = new Image<Rgba32>(this.configuration, 10, 10);
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => _ = image[x, 3]);
Assert.Equal("x", ex.ParamName);
}
[Theory]
[MemberData(nameof(OutOfRangeData))]
public void Set_OutOfRangeX(bool enforceDisco, int x)
{
if (enforceDisco)
{
this.LimitBufferCapacity(100);
}
using var image = new Image<Rgba32>(this.configuration, 10, 10);
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => image[x, 3] = default);
Assert.Equal("x", ex.ParamName);
}
[Theory]
[MemberData(nameof(OutOfRangeData))]
public void Set_OutOfRangeY(bool enforceDisco, int y)
{
if (enforceDisco)
{
this.LimitBufferCapacity(100);
}
using var image = new Image<Rgba32>(this.configuration, 10, 10);
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => image[3, y] = default);
Assert.Equal("y", ex.ParamName);
}
[Theory]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void CopyPixelDataTo_Success(bool disco, bool byteSpan)
{
if (disco)
{
this.LimitBufferCapacity(20);
}
using var image = new Image<La16>(this.configuration, 10, 10);
if (disco)
{
Assert.True(image.GetPixelMemoryGroup().Count > 1);
}
byte[] expected = TestUtils.FillImageWithRandomBytes(image);
byte[] actual = new byte[expected.Length];
if (byteSpan)
{
image.CopyPixelDataTo(actual);
}
else
{
Span<La16> destination = MemoryMarshal.Cast<byte, La16>(actual);
image.CopyPixelDataTo(destination);
}
Assert.True(expected.AsSpan().SequenceEqual(actual));
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void CopyPixelDataTo_DestinationTooShort_Throws(bool byteSpan)
{
using var image = new Image<La16>(this.configuration, 10, 10);
Assert.ThrowsAny<ArgumentOutOfRangeException>(() =>
{
if (byteSpan)
{
image.CopyPixelDataTo(new byte[199]);
}
else
{
image.CopyPixelDataTo(new La16[99]);
}
});
}
}
public class ProcessPixelRows : ProcessPixelRowsTestBase
{
protected override void ProcessPixelRowsImpl<TPixel>(
Image<TPixel> image,
PixelAccessorAction<TPixel> processPixels) =>
image.ProcessPixelRows(processPixels);
protected override void ProcessPixelRowsImpl<TPixel>(
Image<TPixel> image1,
Image<TPixel> image2,
PixelAccessorAction<TPixel, TPixel> processPixels) =>
image1.ProcessPixelRows(image2, processPixels);
protected override void ProcessPixelRowsImpl<TPixel>(
Image<TPixel> image1,
Image<TPixel> image2,
Image<TPixel> image3,
PixelAccessorAction<TPixel, TPixel, TPixel> processPixels) =>
image1.ProcessPixelRows(image2, image3, processPixels);
[Fact]
public void NullReference_Throws()
{
using var img = new Image<Rgb24>(1, 1);
Assert.Throws<ArgumentNullException>(() => img.ProcessPixelRows(null));
Assert.Throws<ArgumentNullException>(() => img.ProcessPixelRows((Image<Rgb24>)null, (_, _) => { }));
Assert.Throws<ArgumentNullException>(() => img.ProcessPixelRows(img, img, null));
Assert.Throws<ArgumentNullException>(() => img.ProcessPixelRows((Image<Rgb24>)null, img, (_, _, _) => { }));
Assert.Throws<ArgumentNullException>(() => img.ProcessPixelRows(img, (Image<Rgb24>)null, (_, _, _) => { }));
Assert.Throws<ArgumentNullException>(() => img.ProcessPixelRows(img, img, null));
}
}
public class Dispose
{
private readonly Configuration configuration = Configuration.CreateDefaultInstance();
public void MultipleDisposeCalls()
{
var image = new Image<Rgba32>(this.configuration, 10, 10);
image.Dispose();
image.Dispose();
}
[Fact]
public void NonPrivateProperties_ObjectDisposedException()
{
var image = new Image<Rgba32>(this.configuration, 10, 10);
var genericImage = (Image)image;
image.Dispose();
// Image<TPixel>
Assert.Throws<ObjectDisposedException>(() => { var prop = image.Frames; });
// Image
Assert.Throws<ObjectDisposedException>(() => { var prop = genericImage.Frames; });
}
[Fact]
public void Save_ObjectDisposedException()
{
using var stream = new MemoryStream();
var image = new Image<Rgba32>(this.configuration, 10, 10);
var encoder = new JpegEncoder();
image.Dispose();
// Image<TPixel>
Assert.Throws<ObjectDisposedException>(() => image.Save(stream, encoder));
}
[Fact]
public void AcceptVisitor_ObjectDisposedException()
{
// This test technically should exist but it's impossible to write proper test case without reflection:
// All visitor types are private and can't be created without context of some save/processing operation
// Save_ObjectDisposedException test checks this method with AcceptVisitor(EncodeVisitor) anyway
return;
}
[Fact]
public void NonPrivateMethods_ObjectDisposedException()
{
var image = new Image<Rgba32>(this.configuration, 10, 10);
var genericImage = (Image)image;
image.Dispose();
// Image<TPixel>
Assert.Throws<ObjectDisposedException>(() => { var res = image.Clone(this.configuration); });
Assert.Throws<ObjectDisposedException>(() => { var res = image.CloneAs<Rgba32>(this.configuration); });
Assert.Throws<ObjectDisposedException>(() => { var res = image.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> _); });
// Image
Assert.Throws<ObjectDisposedException>(() => { var res = genericImage.CloneAs<Rgba32>(this.configuration); });
}
}
public class DetectEncoder
{
[Fact]
public void KnownExtension_ReturnsEncoder()
{
using var image = new Image<L8>(1, 1);
IImageEncoder encoder = image.DetectEncoder("dummy.png");
Assert.NotNull(encoder);
Assert.IsType<PngEncoder>(encoder);
}
[Fact]
public void UnknownExtension_ThrowsNotSupportedException()
{
using var image = new Image<L8>(1, 1);
Assert.Throws<NotSupportedException>(() => image.DetectEncoder("dummy.yolo"));
}
[Fact]
public void NoDetectorRegisteredForKnownExtension_ThrowsNotSupportedException()
{
var configuration = new Configuration();
var format = new TestFormat();
configuration.ImageFormatsManager.AddImageFormat(format);
configuration.ImageFormatsManager.AddImageFormatDetector(new MockImageFormatDetector(format));
using var image = new Image<L8>(configuration, 1, 1);
Assert.Throws<NotSupportedException>(() => image.DetectEncoder($"dummy.{format.Extension}"));
}
}
}
}