More tests and small fixes

This commit is contained in:
Darth Affe 2024-07-20 22:09:34 +02:00
parent 2a26c4404f
commit 9f3fbc7473
12 changed files with 1345 additions and 396 deletions

View File

@ -15,11 +15,12 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HPPH.Reference\HPPH.Reference.csproj" />
<ProjectReference Include="..\HPPH.SkiaSharp\HPPH.SkiaSharp.csproj" />
<ProjectReference Include="..\HPPH.System.Drawing\HPPH.System.Drawing.csproj" />
<ProjectReference Include="..\HPPH\HPPH.csproj" />
</ItemGroup>

View File

@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Runtime.CompilerServices;
namespace HPPH.Test.Image;
@ -7,8 +9,7 @@ public class ImageTest
{
#region Constants
private const int TEST_WIDTH = 1920;
private const int TEST_HEIGHT = 1080;
private readonly List<(int width, int height)> SIZES = [(1920, 1080), (1920, 1), (1, 1080), (200, 500), (1, 1)];
#endregion
@ -17,31 +18,39 @@ public class ImageTest
[TestMethod]
public void ImageCreation()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
Assert.AreEqual(TEST_WIDTH, image.Width);
Assert.AreEqual(TEST_HEIGHT, image.Height);
Assert.AreEqual(width, image.Width);
Assert.AreEqual(height, image.Height);
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), image[x, y]);
}
}
[TestMethod]
public void ImageInnerFull()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
image = image[0, 0, image.Width, image.Height];
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), image[x, y]);
}
}
[TestMethod]
public void ImageEnumerator()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int counter = 0;
foreach (IColor color in image)
@ -54,11 +63,14 @@ public class ImageTest
counter++;
}
}
}
[TestMethod]
public void ImageInnerPartial()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
(int width, int height) = SIZES[0];
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
image = image[163, 280, 720, 13];
Assert.AreEqual(720, image.Width);
@ -72,7 +84,9 @@ public class ImageTest
[TestMethod]
public void ImageInnerInnerPartial()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
(int width, int height) = SIZES[0];
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
image = image[163, 280, 720, 13];
image = image[15, 2, 47, 8];
@ -87,7 +101,9 @@ public class ImageTest
[TestMethod]
public void ImageRowIndexer()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
Assert.AreEqual(image.Height, image.Rows.Count);
@ -99,11 +115,14 @@ public class ImageTest
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), row[x]);
}
}
}
[TestMethod]
public void ImageRowEnumerator()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int y = 0;
foreach (IImageRow row in image.Rows)
@ -114,11 +133,14 @@ public class ImageTest
y++;
}
}
}
[TestMethod]
public void ImageColumnIndexer()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
Assert.AreEqual(image.Width, image.Columns.Count);
@ -130,11 +152,14 @@ public class ImageTest
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), column[y]);
}
}
}
[TestMethod]
public void ImageColumnEnumerator()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int x = 0;
foreach (IImageColumn column in image.Columns)
@ -145,13 +170,14 @@ public class ImageTest
x++;
}
}
}
[TestMethod]
public void AsRefImage()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
image = image[163, 280, 720, 13];
image = image[15, 2, 47, 8];
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
RefImage<ColorARGB> refImage = image.AsRefImage<ColorARGB>();
@ -159,11 +185,14 @@ public class ImageTest
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(image[x, y], refImage[x, y]);
}
}
[TestMethod]
public void ConvertToInPlace()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
IColor[] referenceData = image.ToArray();
@ -182,11 +211,14 @@ public class ImageTest
Assert.AreEqual(reference.A, test.A, $"A differs at index {i}");
}
}
}
[TestMethod]
public void ConvertToCopy()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
IColor[] referenceData = image.ToArray();
@ -205,34 +237,43 @@ public class ImageTest
Assert.AreEqual(reference.A, test.A, $"A differs at index {i}");
}
}
}
[TestMethod]
public void ToArray()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
ColorARGB[] testData = image.ToArray();
for (int y = 0; y < TEST_HEIGHT; y++)
for (int x = 0; x < TEST_WIDTH; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), testData[(y * TEST_WIDTH) + x]);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), testData[(y * width) + x]);
}
}
[TestMethod]
public void CopyTo()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
ColorARGB[] testData = new ColorARGB[TEST_WIDTH * TEST_HEIGHT];
foreach ((int width, int height) in SIZES)
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
ColorARGB[] testData = new ColorARGB[width * height];
image.CopyTo(testData);
for (int y = 0; y < TEST_HEIGHT; y++)
for (int x = 0; x < TEST_WIDTH; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), testData[(y * TEST_WIDTH) + x]);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), testData[(y * width) + x]);
}
}
[TestMethod]
public void SubImage()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
(int width, int height) = SIZES[0];
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
RefImage<ColorARGB> subImage = image[10, 20, 100, 200];
for (int y = 0; y < 200; y++)
@ -243,7 +284,9 @@ public class ImageTest
[TestMethod]
public void GenericRowsIterate()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int y = 0;
foreach (ImageRow<ColorARGB> row in image.Rows)
@ -255,18 +298,20 @@ public class ImageTest
y++;
}
}
}
[TestMethod]
public void GenericRowsCopyTo()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int y = 0;
foreach (ImageRow<ColorARGB> row in image.Rows)
{
ColorARGB[] colors = new ColorARGB[TEST_WIDTH];
byte[] bytes = new byte[TEST_WIDTH * 4];
ColorARGB[] colors = new ColorARGB[width];
byte[] bytes = new byte[width * 4];
row.CopyTo(colors);
row.CopyTo(bytes);
@ -274,7 +319,7 @@ public class ImageTest
foreach (ColorARGB color in colors)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x++, y), color);
for (x = 0; x < TEST_WIDTH; x++)
for (x = 0; x < width; x++)
{
ColorARGB reference = TestDataHelper.GetColorFromLocation<ColorARGB>(x, y);
@ -287,28 +332,34 @@ public class ImageTest
y++;
}
}
}
[TestMethod]
public void GenericRowsToArray()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int y = 0;
foreach (ImageRow<ColorARGB> row in image.Rows)
{
ColorARGB[] data = row.ToArray();
for (int x = 0; x < TEST_WIDTH; x++)
for (int x = 0; x < width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), data[x]);
y++;
}
}
}
[TestMethod]
public void GenericColumnsIterate()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int x = 0;
foreach (ImageColumn<ColorARGB> column in image.Columns)
@ -320,17 +371,20 @@ public class ImageTest
x++;
}
}
}
[TestMethod]
public void GenericColumnsCopyTo()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int x = 0;
foreach (ImageColumn<ColorARGB> column in image.Columns)
{
ColorARGB[] colors = new ColorARGB[TEST_HEIGHT];
byte[] bytes = new byte[TEST_HEIGHT * 4];
ColorARGB[] colors = new ColorARGB[height];
byte[] bytes = new byte[height * 4];
column.CopyTo(colors);
column.CopyTo(bytes);
@ -338,7 +392,7 @@ public class ImageTest
foreach (ColorARGB color in colors)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y++), color);
for (y = 0; y < TEST_HEIGHT; y++)
for (y = 0; y < height; y++)
{
ColorARGB reference = TestDataHelper.GetColorFromLocation<ColorARGB>(x, y);
@ -351,29 +405,35 @@ public class ImageTest
x++;
}
}
}
[TestMethod]
public void GenericColumnsToArray()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int x = 0;
foreach (ImageColumn<ColorARGB> column in image.Columns)
{
ColorARGB[] data = column.ToArray();
for (int y = 0; y < TEST_HEIGHT; y++)
for (int y = 0; y < height; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), data[y]);
x++;
}
}
}
[TestMethod]
public void GenericEnumerate()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
ColorARGB[] reference = TestDataHelper.GetPixelData<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
ColorARGB[] reference = TestDataHelper.GetPixelData<ColorARGB>(width, height);
int i = 0;
foreach (ColorARGB color in image)
@ -384,12 +444,15 @@ public class ImageTest
foreach (ColorARGB color in enumerable)
Assert.AreEqual(reference[i++], color);
}
}
[TestMethod]
public unsafe void Pin()
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
ColorARGB[] reference = TestDataHelper.GetPixelData<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
Image<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
ColorARGB[] reference = TestDataHelper.GetPixelData<ColorARGB>(width, height);
fixed (byte* ptr = image)
{
@ -402,11 +465,14 @@ public class ImageTest
}
}
}
}
[TestMethod]
public void InterfaceRowsIterate()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int y = 0;
foreach (IImageRow row in image.Rows)
@ -428,11 +494,14 @@ public class ImageTest
y++;
}
}
}
[TestMethod]
public void InterfaceColumnsIterate()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int x = 0;
foreach (IImageColumn column in image.Columns)
@ -454,12 +523,15 @@ public class ImageTest
x++;
}
}
}
[TestMethod]
public void InterfaceEnumerate()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
ColorARGB[] reference = TestDataHelper.GetPixelData<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
ColorARGB[] reference = TestDataHelper.GetPixelData<ColorARGB>(width, height);
int i = 0;
foreach (IColor color in image)
@ -470,17 +542,20 @@ public class ImageTest
foreach (IColor color in enumerable)
Assert.AreEqual(reference[i++], color);
}
}
[TestMethod]
public void InterfaceColumnsCopyTo()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int x = 0;
foreach (IImageColumn column in image.Columns)
{
IColor[] colors = new IColor[TEST_HEIGHT];
byte[] bytes = new byte[TEST_HEIGHT * 4];
IColor[] colors = new IColor[height];
byte[] bytes = new byte[height * 4];
column.CopyTo(colors);
column.CopyTo(bytes);
@ -488,7 +563,7 @@ public class ImageTest
foreach (IColor color in colors)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y++), color);
for (y = 0; y < TEST_HEIGHT; y++)
for (y = 0; y < height; y++)
{
IColor reference = TestDataHelper.GetColorFromLocation<ColorARGB>(x, y);
@ -501,34 +576,40 @@ public class ImageTest
x++;
}
}
}
[TestMethod]
public void InterfaceColumnsToArray()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int x = 0;
foreach (IImageColumn column in image.Columns)
{
IColor[] data = column.ToArray();
for (int y = 0; y < TEST_HEIGHT; y++)
for (int y = 0; y < height; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), data[y]);
x++;
}
}
}
[TestMethod]
public void InterfaceRowsCopyTo()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int y = 0;
foreach (IImageRow row in image.Rows)
{
IColor[] colors = new IColor[TEST_WIDTH];
byte[] bytes = new byte[TEST_WIDTH * 4];
IColor[] colors = new IColor[width];
byte[] bytes = new byte[width * 4];
row.CopyTo(colors);
row.CopyTo(bytes);
@ -536,7 +617,7 @@ public class ImageTest
foreach (IColor color in colors)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x++, y), color);
for (x = 0; x < TEST_WIDTH; x++)
for (x = 0; x < width; x++)
{
IColor reference = TestDataHelper.GetColorFromLocation<ColorARGB>(x, y);
@ -549,23 +630,454 @@ public class ImageTest
y++;
}
}
}
[TestMethod]
public void InterfaceRowsToArray()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
int y = 0;
foreach (IImageRow row in image.Rows)
{
IColor[] data = row.ToArray();
for (int x = 0; x < TEST_WIDTH; x++)
for (int x = 0; x < width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), data[x]);
y++;
}
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void CreateWrongStride()
{
Image<ColorRGB>.Create([], 10, 20, 9);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void CreateWrongBufferSize()
{
Image<ColorRGB>.Create(new byte[299], 10, 10, 30);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void CopyToByteNull()
{
TestDataHelper.CreateTestImage<ColorRGB>(10, 10).CopyTo((Span<byte>)null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void CopyToColorNull()
{
TestDataHelper.CreateTestImage<ColorRGB>(10, 10).CopyTo((Span<ColorRGB>)null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void CopyToByteWrongSize()
{
TestDataHelper.CreateTestImage<ColorRGB>(10, 10).CopyTo(new byte[(10 * 10 * 3) - 1]);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void CopyToColorWrongSize()
{
TestDataHelper.CreateTestImage<ColorRGB>(10, 10).CopyTo(new ColorRGB[(10 * 10) - 1]);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void AsRefImageWrongType()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 10);
RefImage<ColorBGR> refImage = image.AsRefImage<ColorBGR>();
}
[TestMethod]
public void PinEmpty()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(0, 0);
Assert.IsTrue(Unsafe.IsNullRef(in image.GetPinnableReference()));
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void IndexerWrongXBig()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
ColorRGB test = image[10, 19];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void IndexerWrongYBig()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
ColorRGB test = image[9, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void IndexerWrongXSmall()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
ColorRGB test = image[-1, 19];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void IndexerWrongYSmall()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
ColorRGB test = image[9, -1];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageWrongX()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
RefImage<ColorRGB> test = image[-1, 0, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageWrongY()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
RefImage<ColorRGB> test = image[0, -1, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageWrongWidth()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
RefImage<ColorRGB> test = image[0, 0, 0, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageWrongHeight()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
RefImage<ColorRGB> test = image[0, 0, 10, 0];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageInvalidSizeWidth()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
RefImage<ColorRGB> test = image[1, 0, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageInvalidSizeHeight()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
RefImage<ColorRGB> test = image[0, 1, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageInterfaceWrongX()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage test = image[-1, 0, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageInterfaceWrongY()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage test = image[0, -1, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageInterfaceWrongWidth()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage test = image[0, 0, 0, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageInterfaceWrongHeight()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage test = image[0, 0, 10, 0];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageInterfaceInvalidSizeWidth()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage test = image[1, 0, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageInterfaceInvalidSizeHeight()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage test = image[0, 1, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void ColumnsIndexerToBig()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImageColumn test = image.Columns[20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void ColumnsIndexerToSmall()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImageColumn test = image.Columns[-1];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void RowsIndexerToBig()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImageRow test = image.Rows[20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void RowsIndexerToSmall()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImageRow test = image.Rows[-1];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void ColumnIndexerToBig()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IColor test = image.Columns[1][20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void ColumnIndexerToSmall()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IColor test = image.Columns[1][-1];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void RowIndexerToBig()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IColor test = image.Rows[1][10];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void RowIndexerToSmall()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IColor test = image.Rows[1][-1];
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ColumnCopyToByteNull()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
image.Columns[1].CopyTo((Span<byte>)null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ColumnCopyToColorNull()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
image.Columns[1].CopyTo((Span<IColor>)null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ColumnCopyToByteToSmall()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
image.Columns[1].CopyTo(new byte[(20 * 3) - 1]);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ColumnCopyToColorToSmall()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
image.Columns[1].CopyTo(new IColor[19]);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void RowCopyToByteNull()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
image.Rows[1].CopyTo((Span<byte>)null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void RowCopyToColorNull()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
image.Rows[1].CopyTo((Span<IColor>)null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void RowCopyToByteToSmall()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
image.Rows[1].CopyTo(new byte[(10 * 3) - 1]);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void RowCopyToColorToSmall()
{
IImage image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
image.Rows[1].CopyTo(new IColor[9]);
}
[TestMethod]
public void EqualsIImage()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage image2 = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
Assert.AreEqual(image, image2);
Assert.IsFalse(image.Equals((IImage?)null));
}
[TestMethod]
public void EqualsGenericIImage()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage<ColorRGB> image2 = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
Assert.AreEqual(image, image2);
Assert.IsFalse(image.Equals((IImage<ColorRGB>?)null));
}
[TestMethod]
public void EqualsImage()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
Image<ColorRGB> image2 = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
Assert.AreEqual(image, image2);
Assert.IsFalse(image.Equals((Image<ColorRGB>?)null));
}
[TestMethod]
public void EqualsIImageWrongSize()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage image2 = TestDataHelper.CreateTestImage<ColorRGB>(10, 10);
Assert.AreNotEqual(image, image2);
IImage image3 = TestDataHelper.CreateTestImage<ColorRGB>(20, 20);
Assert.AreNotEqual(image, image3);
}
[TestMethod]
public void EqualsGenericIImageWrongSize()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage<ColorRGB> image2 = TestDataHelper.CreateTestImage<ColorRGB>(10, 10);
Assert.AreNotEqual(image, image2);
IImage<ColorRGB> image3 = TestDataHelper.CreateTestImage<ColorRGB>(20, 20);
Assert.AreNotEqual(image, image3);
}
[TestMethod]
public void EqualsImageWrongSize()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
Image<ColorRGB> image2 = TestDataHelper.CreateTestImage<ColorRGB>(10, 10);
Assert.AreNotEqual(image, image2);
Image<ColorRGB> image3 = TestDataHelper.CreateTestImage<ColorRGB>(20, 20);
Assert.AreNotEqual(image, image3);
}
[TestMethod]
public void EqualsIImageWrongPixel()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
ColorRGB[] array = image.ToArray();
array[1] = new ColorRGB(255, 255, 255);
IImage image2 = Image<ColorRGB>.Create(array, 10, 20);
Assert.AreNotEqual(image, image2);
}
[TestMethod]
public void EqualsGenericIImageWrongPixel()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
ColorRGB[] array = image.ToArray();
array[1] = new ColorRGB(255, 255, 255);
IImage<ColorRGB> image2 = Image<ColorRGB>.Create(array, 10, 20);
Assert.AreNotEqual(image, image2);
}
[TestMethod]
public void EqualsImageWrongPixel()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
ColorRGB[] array = image.ToArray();
array[1] = new ColorRGB(255, 255, 255);
Image<ColorRGB> image2 = Image<ColorRGB>.Create(array, 10, 20);
Assert.AreNotEqual(image, image2);
}
[TestMethod]
public void EqualsIImageWrongFormat()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20);
IImage image2 = TestDataHelper.CreateTestImage<ColorBGR>(10, 20);
Assert.AreNotEqual(image, image2);
}
#endregion
}

View File

@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace HPPH.Test.Image;
@ -7,8 +8,7 @@ public class RefImageTest
{
#region Constants
private const int TEST_WIDTH = 1920;
private const int TEST_HEIGHT = 1080;
private readonly List<(int width, int height)> SIZES = [(1920, 1080), (1920, 1), (1, 1080), (200, 500), (1, 1)];
#endregion
@ -17,31 +17,39 @@ public class RefImageTest
[TestMethod]
public void ImageCreation()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
foreach ((int width, int height) in SIZES)
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
Assert.AreEqual(TEST_WIDTH, image.Width);
Assert.AreEqual(TEST_HEIGHT, image.Height);
Assert.AreEqual(width, image.Width);
Assert.AreEqual(height, image.Height);
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), image[x, y]);
}
}
[TestMethod]
public void ImageInnerFull()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
foreach ((int width, int height) in SIZES)
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
image = image[0, 0, image.Width, image.Height];
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), image[x, y]);
}
}
[TestMethod]
public void ImageEnumerator()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
foreach ((int width, int height) in SIZES)
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
int counter = 0;
foreach (ColorARGB color in image)
@ -56,11 +64,14 @@ public class RefImageTest
counter++;
}
}
}
[TestMethod]
public void ImageInnerPartial()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
(int width, int height) = SIZES[0];
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
image = image[163, 280, 720, 13];
Assert.AreEqual(720, image.Width);
@ -74,7 +85,9 @@ public class RefImageTest
[TestMethod]
public void ImageInnerInnerPartial()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
(int width, int height) = SIZES[0];
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
image = image[163, 280, 720, 13];
image = image[15, 2, 47, 8];
@ -89,7 +102,9 @@ public class RefImageTest
[TestMethod]
public void ImageRowIndexer()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
foreach ((int width, int height) in SIZES)
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
Assert.AreEqual(image.Height, image.Rows.Count);
@ -101,11 +116,14 @@ public class RefImageTest
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), row[x]);
}
}
}
[TestMethod]
public void ImageRowEnumerator()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
foreach ((int width, int height) in SIZES)
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
int y = 0;
foreach (ImageRow<ColorARGB> row in image.Rows)
@ -116,11 +134,14 @@ public class RefImageTest
y++;
}
}
}
[TestMethod]
public void ImageColumnIndexer()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
foreach ((int width, int height) in SIZES)
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
Assert.AreEqual(image.Width, image.Columns.Count);
@ -132,11 +153,14 @@ public class RefImageTest
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), column[y]);
}
}
}
[TestMethod]
public void ImageColumnEnumerator()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
foreach ((int width, int height) in SIZES)
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
int x = 0;
foreach (ImageColumn<ColorARGB> column in image.Columns)
@ -147,34 +171,43 @@ public class RefImageTest
x++;
}
}
}
[TestMethod]
public void ToArray()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
foreach ((int width, int height) in SIZES)
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
ColorARGB[] testData = image.ToArray();
for (int y = 0; y < TEST_HEIGHT; y++)
for (int x = 0; x < TEST_WIDTH; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), testData[(y * TEST_WIDTH) + x]);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), testData[(y * width) + x]);
}
}
[TestMethod]
public void CopyTo()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
ColorARGB[] testData = new ColorARGB[TEST_WIDTH * TEST_HEIGHT];
foreach ((int width, int height) in SIZES)
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
ColorARGB[] testData = new ColorARGB[width * height];
image.CopyTo(testData);
for (int y = 0; y < TEST_HEIGHT; y++)
for (int x = 0; x < TEST_WIDTH; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), testData[(y * TEST_WIDTH) + x]);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), testData[(y * width) + x]);
}
}
[TestMethod]
public void SubImage()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
(int width, int height) = SIZES[0];
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
RefImage<ColorARGB> subImage = image[10, 20, 100, 200];
for (int y = 0; y < 200; y++)
@ -185,8 +218,10 @@ public class RefImageTest
[TestMethod]
public unsafe void Pin()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
ColorARGB[] reference = TestDataHelper.GetPixelData<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
foreach ((int width, int height) in SIZES)
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
ColorARGB[] reference = TestDataHelper.GetPixelData<ColorARGB>(width, height);
fixed (byte* ptr = image)
{
@ -199,6 +234,236 @@ public class RefImageTest
}
}
}
}
[TestMethod]
public void PinEmpty()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(0, 0).AsRefImage();
Assert.IsTrue(Unsafe.IsNullRef(in image.GetPinnableReference()));
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void CopyToColorNull()
{
TestDataHelper.CreateTestImage<ColorRGB>(10, 10).AsRefImage().CopyTo(null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void CopyToColorWrongSize()
{
TestDataHelper.CreateTestImage<ColorRGB>(10, 10).AsRefImage().CopyTo(new ColorRGB[(10 * 10) - 1]);
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void IndexerWrongXBig()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
ColorRGB test = image[10, 19];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void IndexerWrongYBig()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
ColorRGB test = image[9, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void IndexerWrongXSmall()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
ColorRGB test = image[-1, 19];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void IndexerWrongYSmall()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
ColorRGB test = image[9, -1];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageWrongX()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
RefImage<ColorRGB> test = image[-1, 0, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageWrongY()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
RefImage<ColorRGB> test = image[0, -1, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageWrongWidth()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
RefImage<ColorRGB> test = image[0, 0, 0, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageWrongHeight()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
RefImage<ColorRGB> test = image[0, 0, 10, 0];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageInvalidSizeWidth()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
RefImage<ColorRGB> test = image[1, 0, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void SubImageInvalidSizeHeight()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
RefImage<ColorRGB> test = image[0, 1, 10, 20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void ColumnsIndexerToBig()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
ImageColumn<ColorRGB> test = image.Columns[20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void ColumnsIndexerToSmall()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
ImageColumn<ColorRGB> test = image.Columns[-1];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void RowsIndexerToBig()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
ImageRow<ColorRGB> test = image.Rows[20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void RowsIndexerToSmall()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
ImageRow<ColorRGB> test = image.Rows[-1];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void ColumnIndexerToBig()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
IColor test = image.Columns[1][20];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void ColumnIndexerToSmall()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
IColor test = image.Columns[1][-1];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void RowIndexerToBig()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
IColor test = image.Rows[1][10];
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void RowIndexerToSmall()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
IColor test = image.Rows[1][-1];
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ColumnCopyToByteNull()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
image.Columns[1].CopyTo((Span<byte>)null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ColumnCopyToColorNull()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
image.Rows[1].CopyTo((Span<ColorRGB>)null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ColumnCopyToByteToSmall()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
image.Columns[1].CopyTo(new byte[(20 * 3) - 1]);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ColumnCopyToColorToSmall()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
image.Columns[1].CopyTo(new ColorRGB[19]);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void RowCopyToByteNull()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
image.Rows[1].CopyTo((Span<byte>)null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void RowCopyToColorNull()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
image.Rows[1].CopyTo((Span<ColorRGB>)null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void RowCopyToByteToSmall()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
image.Rows[1].CopyTo(new byte[(10 * 3) - 1]);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void RowCopyToColorToSmall()
{
RefImage<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(10, 20).AsRefImage();
image.Rows[1].CopyTo(new ColorRGB[9]);
}
#endregion
}

View File

@ -274,4 +274,34 @@ public class ConvertTests
}
}
}
[TestMethod]
public void Convert3ByteSameBppRGBToBGRReadOnlySpan()
{
foreach (string image in GetTestImages())
{
for (int skip = 0; skip < 4; skip++)
{
ColorRGB[] data = ImageHelper.GetColorsFromImage<ColorRGB>(image).SkipLast(skip).ToArray();
ReadOnlySpan<ColorRGB> referenceData = data;
Span<ColorRGB> sourceData = new ColorRGB[referenceData.Length];
referenceData.CopyTo(sourceData);
Span<ColorBGR> result = ((ReadOnlySpan<ColorRGB>)sourceData).Convert<ColorRGB, ColorBGR>();
Assert.AreEqual(referenceData.Length, result.Length);
for (int i = 0; i < referenceData.Length; i++)
{
ColorRGB reference = referenceData[i];
ColorBGR test = result[i];
Assert.AreEqual(reference.R, test.R, $"R differs at index {i}. Image: {image}, skip: {skip}");
Assert.AreEqual(reference.G, test.G, $"G differs at index {i}. Image: {image}, skip: {skip}");
Assert.AreEqual(reference.B, test.B, $"B differs at index {i}. Image: {image}, skip: {skip}");
Assert.AreEqual(reference.A, test.A, $"A differs at index {i}. Image: {image}, skip: {skip}");
}
}
}
}
}

View File

@ -0,0 +1,52 @@
using HPPH.SkiaSharp;
using SkiaSharp;
namespace HPPH.Test.Skia;
[TestClass]
public class SkiaTests
{
private static IEnumerable<string> GetTestImages() => Directory.EnumerateFiles(@"..\..\..\..\sample_data", "*.png", SearchOption.AllDirectories);
[TestMethod]
public void ImageConversion24Bit()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(1920, 1080);
using SKImage bitmap = image.ToSKImage();
IImage image2 = bitmap.ToImage();
Assert.AreEqual(IColorFormat.BGRA, image2.ColorFormat);
image2 = image2.ConvertTo<ColorRGB>();
Assert.AreEqual(image, image2);
}
[TestMethod]
public void ImageConversion32Bit()
{
Image<ColorRGBA> image = TestDataHelper.CreateTestImage<ColorRGBA>(1920, 1080);
using SKImage bitmap = image.ToSKImage();
IImage image2 = bitmap.ToImage();
Assert.AreEqual(IColorFormat.BGRA, image2.ColorFormat);
image2 = image2.ConvertTo<ColorRGBA>();
Assert.AreEqual(image, image2);
}
[TestMethod]
public void LoadFileToPngLoadStream()
{
foreach (string image in GetTestImages())
{
IImage img = SkiaSharp.ImageHelper.LoadImage(image);
byte[] png = img.ToPng();
using MemoryStream ms = new(png);
IImage img2 = SkiaSharp.ImageHelper.LoadImage(ms);
Assert.AreEqual(img, img2);
}
}
}

View File

@ -0,0 +1,52 @@
using System.Drawing;
using HPPH.System.Drawing;
namespace HPPH.Test.SystemDrawing;
[TestClass]
public class SystemDrawingTests
{
private static IEnumerable<string> GetTestImages() => Directory.EnumerateFiles(@"..\..\..\..\sample_data", "*.png", SearchOption.AllDirectories);
[TestMethod]
public void ImageConversion24Bit()
{
Image<ColorRGB> image = TestDataHelper.CreateTestImage<ColorRGB>(1920, 1080);
using Bitmap bitmap = image.ToBitmap();
IImage image2 = bitmap.ToImage();
Assert.AreEqual(IColorFormat.BGR, image2.ColorFormat);
image2 = image2.ConvertTo<ColorRGB>();
Assert.AreEqual(image, image2);
}
[TestMethod]
public void ImageConversion32Bit()
{
Image<ColorRGBA> image = TestDataHelper.CreateTestImage<ColorRGBA>(1920, 1080);
using Bitmap bitmap = image.ToBitmap();
IImage image2 = bitmap.ToImage();
Assert.AreEqual(IColorFormat.BGRA, image2.ColorFormat);
image2 = image2.ConvertTo<ColorRGBA>();
Assert.AreEqual(image, image2);
}
[TestMethod]
public void LoadFileToPngLoadStream()
{
foreach (string image in GetTestImages())
{
IImage img = System.Drawing.ImageHelper.LoadImage(image);
byte[] png = img.ToPng();
using MemoryStream ms = new(png);
IImage img2 = System.Drawing.ImageHelper.LoadImage(ms);
Assert.AreEqual(img, img2);
}
}
}

View File

@ -6,7 +6,7 @@ namespace HPPH;
/// <inheritdoc />
[SkipLocalsInit]
public sealed class Image<T> : IImage<T>
public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
where T : struct, IColor
{
#region Properties & Fields
@ -224,5 +224,49 @@ public sealed class Image<T> : IImage<T>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
//TODO DarthAffe 20.07.2024: All of those equals can be optimized
public bool Equals(IImage? other)
{
if (other == null) return false;
if (other.ColorFormat != ColorFormat) return false;
if (other.Width != Width) return false;
if (other.Height != Height) return false;
for (int y = 0; y < Height; y++)
for (int x = 0; x < Width; x++)
if (!this[x, y].Equals(other[x, y]))
return false;
return true;
}
public bool Equals(IImage<T>? other)
{
if (other == null) return false;
if (other.Width != Width) return false;
if (other.Height != Height) return false;
for (int y = 0; y < Height; y++)
for (int x = 0; x < Width; x++)
if (!this[x, y].Equals(other[x, y]))
return false;
return true;
}
public bool Equals(Image<T>? other)
{
if (other == null) return false;
if (other.Width != Width) return false;
if (other.Height != Height) return false;
for (int y = 0; y < Height; y++)
for (int x = 0; x < Width; x++)
if (!this[x, y].Equals(other[x, y]))
return false;
return true;
}
#endregion
}

View File

@ -56,16 +56,11 @@ public readonly ref struct ImageColumn<T>
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
if (_step == 1)
_buffer.Slice(_start, SizeInBytes).CopyTo(destination);
else
{
ref byte dataRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer), _start);
Span<T> target = MemoryMarshal.Cast<byte, T>(destination);
for (int i = 0; i < Length; i++)
target[i] = Unsafe.As<byte, T>(ref Unsafe.Add(ref dataRef, i * _step));
}
}
public T[] ToArray()
{
@ -177,16 +172,11 @@ internal class IColorImageColumn<T> : IImageColumn
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
if (_step == 1)
_buffer.AsSpan().Slice(_start, SizeInBytes).CopyTo(destination);
else
{
ref byte dataRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), _start);
Span<T> target = MemoryMarshal.Cast<byte, T>(destination);
for (int i = 0; i < Length; i++)
target[i] = Unsafe.As<byte, T>(ref Unsafe.Add(ref dataRef, i * _step));
}
}
public IColor[] ToArray()
{

View File

@ -28,7 +28,7 @@ public readonly ref struct ImageColumns<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
return new ImageColumn<T>(_data, (_y * _stride) + ((column + _x) * _bpp), _height, _stride);
}
@ -124,7 +124,7 @@ internal class IColorImageColumns<T> : IImageColumns
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
return new IColorImageColumn<T>(_data, (_y * _stride) + ((column + _x) * _bpp), _height, _stride);
}

View File

@ -152,6 +152,9 @@ internal class IColorImageRow<T> : IImageRow
public void CopyTo(Span<IColor> destination)
{
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < _length) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
for (int i = 0; i < _length; i++)
destination[i] = this[i];
}

View File

@ -27,7 +27,7 @@ public readonly ref struct ImageRows<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
return new ImageRow<T>(_data, ((row + _y) * _stride) + _x, _width);
}
@ -122,7 +122,7 @@ internal class IColorImageRows<T> : IImageRows
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
return new IColorImageRow<T>(_data, ((row + _y) * _stride) + _x, _width);
}

View File

@ -3,7 +3,7 @@
/// <summary>
/// Represents an image.
/// </summary>
public interface IImage : IEnumerable<IColor>
public interface IImage : IEnumerable<IColor>, IEquatable<IImage>
{
/// <summary>
/// Gets the color format used in this image.
@ -83,7 +83,7 @@ public interface IImage : IEnumerable<IColor>
/// <summary>
/// Represents an image.
/// </summary>
public interface IImage<T> : IImage
public interface IImage<T> : IImage, IEquatable<IImage<T>>
where T : struct, IColor
{
/// <summary>