Fixed RowIndexer on sub-images and added tests for that case

This commit is contained in:
Darth Affe 2024-07-21 17:28:54 +02:00
parent 44ff0bf5b0
commit 536a462540
3 changed files with 84 additions and 2 deletions

View File

@ -117,6 +117,25 @@ public class ImageTest
} }
} }
[TestMethod]
public void ImageRowIndexerSubImage()
{
(int width, int height) = SIZES[0];
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
image = image[163, 280, 720, 13];
Assert.AreEqual(image.Height, image.Rows.Count);
for (int y = 0; y < image.Height; y++)
{
IImageRow row = image.Rows[y];
Assert.AreEqual(image.Width, row.Length);
for (int x = 0; x < row.Length; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(163 + x, 280 + y), row[x]);
}
}
[TestMethod] [TestMethod]
public void ImageRowEnumerator() public void ImageRowEnumerator()
{ {
@ -154,6 +173,25 @@ public class ImageTest
} }
} }
[TestMethod]
public void ImageColumnIndexerSubImage()
{
(int width, int height) = SIZES[0];
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(width, height);
image = image[163, 280, 720, 13];
Assert.AreEqual(image.Width, image.Columns.Count);
for (int x = 0; x < image.Width; x++)
{
IImageColumn column = image.Columns[x];
Assert.AreEqual(image.Height, column.Length);
for (int y = 0; y < column.Length; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(163 + x, 280 + y), column[y]);
}
}
[TestMethod] [TestMethod]
public void ImageColumnEnumerator() public void ImageColumnEnumerator()
{ {

View File

@ -118,6 +118,25 @@ public class RefImageTest
} }
} }
[TestMethod]
public void ImageRowIndexerSubImage()
{
(int width, int height) = SIZES[0];
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
image = image[163, 280, 720, 13];
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>(163 + x, 280 + y), row[x]);
}
}
[TestMethod] [TestMethod]
public void ImageRowEnumerator() public void ImageRowEnumerator()
{ {
@ -155,6 +174,25 @@ public class RefImageTest
} }
} }
[TestMethod]
public void ImageColumnIndexerSubImage()
{
(int width, int height) = SIZES[0];
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(width, height).AsRefImage();
image = image[163, 280, 720, 13];
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>(163 + x, 280 + y), column[y]);
}
}
[TestMethod] [TestMethod]
public void ImageColumnEnumerator() public void ImageColumnEnumerator()
{ {

View File

@ -15,6 +15,7 @@ public readonly ref struct ImageRows<T>
private readonly int _width; private readonly int _width;
private readonly int _height; private readonly int _height;
private readonly int _stride; private readonly int _stride;
private readonly int _bpp;
public int Count => _height; public int Count => _height;
@ -29,7 +30,7 @@ public readonly ref struct ImageRows<T>
{ {
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); return new ImageRow<T>(_data, ((row + _y) * _stride) + (_x * _bpp), _width);
} }
} }
@ -46,6 +47,8 @@ public readonly ref struct ImageRows<T>
this._width = width; this._width = width;
this._height = height; this._height = height;
this._stride = stride; this._stride = stride;
_bpp = T.ColorFormat.BytesPerPixel;
} }
#endregion #endregion
@ -110,6 +113,7 @@ internal class IColorImageRows<T> : IImageRows
private readonly int _width; private readonly int _width;
private readonly int _height; private readonly int _height;
private readonly int _stride; private readonly int _stride;
private readonly int _bpp;
/// <inheritdoc /> /// <inheritdoc />
public int Count => _height; public int Count => _height;
@ -126,7 +130,7 @@ internal class IColorImageRows<T> : IImageRows
{ {
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); return new IColorImageRow<T>(_data, ((row + _y) * _stride) + (_x * _bpp), _width);
} }
} }
@ -143,6 +147,8 @@ internal class IColorImageRows<T> : IImageRows
this._width = width; this._width = width;
this._height = height; this._height = height;
this._stride = stride; this._stride = stride;
_bpp = T.ColorFormat.BytesPerPixel;
} }
#endregion #endregion