Skip to content

Commit a9816fd

Browse files
committed
use overloading in ImageFrameCollection + more tests for API-s affected by the PR
1 parent 24f6ff9 commit a9816fd

File tree

4 files changed

+98
-10
lines changed

4 files changed

+98
-10
lines changed

src/ImageSharp/ImageFrameCollection.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,31 @@ public Image<TPixel> CloneFrame(int index)
198198
return new Image<TPixel>(this.parent.GetConfiguration(), this.parent.MetaData.Clone(), new[] { clonedFrame });
199199
}
200200

201+
/// <summary>
202+
/// Creates a new <seealso cref="ImageFrame{TPixel}" /> and appends it to the end of the collection.
203+
/// </summary>
204+
/// <returns>
205+
/// The new <see cref="ImageFrame{TPixel}" />.
206+
/// </returns>
207+
public ImageFrame<TPixel> CreateFrame()
208+
{
209+
return this.CreateFrame(default);
210+
}
211+
201212
/// <summary>
202213
/// Creates a new <seealso cref="ImageFrame{TPixel}" /> and appends it to the end of the collection.
203214
/// </summary>
204215
/// <param name="backgroundColor">The background color to initialize the pixels with.</param>
205216
/// <returns>
206217
/// The new <see cref="ImageFrame{TPixel}" />.
207218
/// </returns>
208-
public ImageFrame<TPixel> CreateFrame(TPixel backgroundColor = default) {
209-
var frame = new ImageFrame<TPixel>(this.parent.GetConfiguration(), this.RootFrame.Width, this.RootFrame.Height, backgroundColor);
219+
public ImageFrame<TPixel> CreateFrame(TPixel backgroundColor)
220+
{
221+
var frame = new ImageFrame<TPixel>(
222+
this.parent.GetConfiguration(),
223+
this.RootFrame.Width,
224+
this.RootFrame.Height,
225+
backgroundColor);
210226
this.frames.Add(frame);
211227
return frame;
212228
}

tests/ImageSharp.Tests/Image/ImageFramesCollectionTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,21 @@ public void ExtractFrame<TPixel>(TestImageProvider<TPixel> provider)
225225
}
226226

227227
[Fact]
228-
public void CreateFrame()
228+
public void CreateFrame_Default()
229229
{
230230
this.image.Frames.CreateFrame();
231+
232+
Assert.Equal(2, this.image.Frames.Count);
233+
this.image.Frames[1].ComparePixelBufferTo(default(Rgba32));
234+
}
235+
236+
[Fact]
237+
public void CreateFrame_CustomFillColor()
238+
{
239+
this.image.Frames.CreateFrame(Rgba32.HotPink);
240+
231241
Assert.Equal(2, this.image.Frames.Count);
242+
this.image.Frames[1].ComparePixelBufferTo(Rgba32.HotPink);
232243
}
233244

234245
[Fact]

tests/ImageSharp.Tests/Image/ImageTests.cs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,60 @@ namespace SixLabors.ImageSharp.Tests
1313
/// <summary>
1414
/// Tests the <see cref="Image"/> class.
1515
/// </summary>
16-
public class ImageTests : FileTestBase
16+
public class ImageTests
1717
{
18+
public class Constructor
19+
{
20+
[Fact]
21+
public void Width_Height()
22+
{
23+
using (var image = new Image<Rgba32>(11, 23))
24+
{
25+
Assert.Equal(11, image.Width);
26+
Assert.Equal(23, image.Height);
27+
Assert.Equal(11*23, image.GetPixelSpan().Length);
28+
image.ComparePixelBufferTo(default(Rgba32));
29+
30+
Assert.Equal(Configuration.Default, image.GetConfiguration());
31+
}
32+
}
33+
34+
[Fact]
35+
public void Configuration_Width_Height()
36+
{
37+
Configuration configuration = Configuration.Default.ShallowCopy();
38+
39+
using (var image = new Image<Rgba32>(configuration, 11, 23))
40+
{
41+
Assert.Equal(11, image.Width);
42+
Assert.Equal(23, image.Height);
43+
Assert.Equal(11 * 23, image.GetPixelSpan().Length);
44+
image.ComparePixelBufferTo(default(Rgba32));
45+
46+
Assert.Equal(configuration, image.GetConfiguration());
47+
}
48+
}
49+
50+
[Fact]
51+
public void Configuration_Width_Height_BackroundColor()
52+
{
53+
Configuration configuration = Configuration.Default.ShallowCopy();
54+
Rgba32 color = Rgba32.Aquamarine;
55+
56+
using (var image = new Image<Rgba32>(configuration, 11, 23, color))
57+
{
58+
Assert.Equal(11, image.Width);
59+
Assert.Equal(23, image.Height);
60+
Assert.Equal(11 * 23, image.GetPixelSpan().Length);
61+
image.ComparePixelBufferTo(color);
62+
63+
Assert.Equal(configuration, image.GetConfiguration());
64+
}
65+
}
66+
}
67+
1868
[Fact]
19-
public void ConstructorByteArray()
69+
public void Load_ByteArray()
2070
{
2171
Assert.Throws<ArgumentNullException>(() =>
2272
{
@@ -32,7 +82,7 @@ public void ConstructorByteArray()
3282
}
3383

3484
[Fact]
35-
public void ConstructorFileSystem()
85+
public void Load_FileSystemPath()
3686
{
3787
TestFile file = TestFile.Create(TestImages.Bmp.Car);
3888
using (Image<Rgba32> image = Image.Load<Rgba32>(file.FullPath))
@@ -43,7 +93,7 @@ public void ConstructorFileSystem()
4393
}
4494

4595
[Fact]
46-
public void ConstructorFileSystem_FileNotFound()
96+
public void Load_FileSystemPath_FileNotFound()
4797
{
4898
System.IO.FileNotFoundException ex = Assert.Throws<System.IO.FileNotFoundException>(
4999
() =>
@@ -53,7 +103,7 @@ public void ConstructorFileSystem_FileNotFound()
53103
}
54104

55105
[Fact]
56-
public void ConstructorFileSystem_NullPath()
106+
public void Load_FileSystemPath_NullPath()
57107
{
58108
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(
59109
() =>

tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,25 @@ public static Image<TPixel> ComparePixelBufferTo<TPixel>(
361361
public static Image<TPixel> ComparePixelBufferTo<TPixel>(this Image<TPixel> image, TPixel expectedPixel)
362362
where TPixel : struct, IPixel<TPixel>
363363
{
364-
Span<TPixel> actualPixels = image.GetPixelSpan();
364+
foreach (ImageFrame<TPixel> imageFrame in image.Frames)
365+
{
366+
imageFrame.ComparePixelBufferTo(expectedPixel);
367+
}
368+
369+
return image;
370+
}
371+
372+
public static ImageFrame<TPixel> ComparePixelBufferTo<TPixel>(this ImageFrame<TPixel> imageFrame, TPixel expectedPixel)
373+
where TPixel : struct, IPixel<TPixel>
374+
{
375+
Span<TPixel> actualPixels = imageFrame.GetPixelSpan();
365376

366377
for (int i = 0; i < actualPixels.Length; i++)
367378
{
368379
Assert.True(expectedPixel.Equals(actualPixels[i]), $"Pixels are different on position {i}!");
369380
}
370381

371-
return image;
382+
return imageFrame;
372383
}
373384

374385
public static ImageFrame<TPixel> ComparePixelBufferTo<TPixel>(

0 commit comments

Comments
 (0)