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>

File diff suppressed because it is too large Load Diff

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,50 +17,61 @@ 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]);
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();
image = image[0, 0, image.Width, image.Height];
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]);
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();
int counter = 0;
foreach (ColorARGB color in image)
foreach ((int width, int height) in SIZES)
{
int x = counter % image.Width;
int y = counter / image.Width;
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
if (y == 1) Debugger.Break();
int counter = 0;
foreach (ColorARGB color in image)
{
int x = counter % image.Width;
int y = counter / image.Width;
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), color);
if (y == 1) Debugger.Break();
counter++;
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), color);
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,92 +102,112 @@ public class RefImageTest
[TestMethod]
public void ImageRowIndexer()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
Assert.AreEqual(image.Height, image.Rows.Count);
for (int y = 0; y < image.Height; y++)
foreach ((int width, int height) in SIZES)
{
ImageRow<ColorARGB> row = image.Rows[y];
Assert.AreEqual(image.Width, row.Length);
for (int x = 0; x < row.Length; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), row[x]);
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
Assert.AreEqual(image.Height, image.Rows.Count);
for (int y = 0; y < image.Height; y++)
{
ImageRow<ColorARGB> row = image.Rows[y];
Assert.AreEqual(image.Width, row.Length);
for (int x = 0; x < row.Length; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), row[x]);
}
}
}
[TestMethod]
public void ImageRowEnumerator()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
int y = 0;
foreach (ImageRow<ColorARGB> row in image.Rows)
foreach ((int width, int height) in SIZES)
{
for (int x = 0; x < row.Length; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), row[x]);
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
y++;
int y = 0;
foreach (ImageRow<ColorARGB> row in image.Rows)
{
for (int x = 0; x < row.Length; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), row[x]);
y++;
}
}
}
[TestMethod]
public void ImageColumnIndexer()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
Assert.AreEqual(image.Width, image.Columns.Count);
for (int x = 0; x < image.Width; x++)
foreach ((int width, int height) in SIZES)
{
ImageColumn<ColorARGB> column = image.Columns[x];
Assert.AreEqual(image.Height, column.Length);
for (int y = 0; y < column.Length; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), column[y]);
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
Assert.AreEqual(image.Width, image.Columns.Count);
for (int x = 0; x < image.Width; x++)
{
ImageColumn<ColorARGB> column = image.Columns[x];
Assert.AreEqual(image.Height, column.Length);
for (int y = 0; y < column.Length; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), column[y]);
}
}
}
[TestMethod]
public void ImageColumnEnumerator()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
int x = 0;
foreach (ImageColumn<ColorARGB> column in image.Columns)
foreach ((int width, int height) in SIZES)
{
for (int y = 0; y < column.Length; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), column[y]);
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
x++;
int x = 0;
foreach (ImageColumn<ColorARGB> column in image.Columns)
{
for (int y = 0; y < column.Length; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), column[y]);
x++;
}
}
}
[TestMethod]
public void ToArray()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
ColorARGB[] testData = image.ToArray();
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];
image.CopyTo(testData);
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,20 +218,252 @@ 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);
fixed (byte* ptr = image)
foreach ((int width, int height) in SIZES)
{
for (int i = 0; i < reference.Length; i++)
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
ColorARGB[] reference = TestDataHelper.GetPixelData<ColorARGB>(width, height);
fixed (byte* ptr = image)
{
Assert.AreEqual(reference[i].A, ptr[(i * 4) + 0]);
Assert.AreEqual(reference[i].R, ptr[(i * 4) + 1]);
Assert.AreEqual(reference[i].G, ptr[(i * 4) + 2]);
Assert.AreEqual(reference[i].B, ptr[(i * 4) + 3]);
for (int i = 0; i < reference.Length; i++)
{
Assert.AreEqual(reference[i].A, ptr[(i * 4) + 0]);
Assert.AreEqual(reference[i].R, ptr[(i * 4) + 1]);
Assert.AreEqual(reference[i].G, ptr[(i * 4) + 2]);
Assert.AreEqual(reference[i].B, ptr[(i * 4) + 3]);
}
}
}
}
[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,15 +56,10 @@ 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));
}
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,15 +172,10 @@ 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));
}
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>