diff --git a/HPPH.Test/Image/ImageTest.cs b/HPPH.Test/Image/ImageTest.cs index 34fa2ca..ff74615 100644 --- a/HPPH.Test/Image/ImageTest.cs +++ b/HPPH.Test/Image/ImageTest.cs @@ -117,6 +117,25 @@ public class ImageTest } } + [TestMethod] + public void ImageRowIndexerSubImage() + { + (int width, int height) = SIZES[0]; + + IImage image = TestDataHelper.CreateTestImage(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(163 + x, 280 + y), row[x]); + } + } + [TestMethod] public void ImageRowEnumerator() { @@ -154,6 +173,25 @@ public class ImageTest } } + [TestMethod] + public void ImageColumnIndexerSubImage() + { + (int width, int height) = SIZES[0]; + + IImage image = TestDataHelper.CreateTestImage(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(163 + x, 280 + y), column[y]); + } + } + [TestMethod] public void ImageColumnEnumerator() { diff --git a/HPPH.Test/Image/RefImageTest.cs b/HPPH.Test/Image/RefImageTest.cs index 3ce3d1b..e1197ec 100644 --- a/HPPH.Test/Image/RefImageTest.cs +++ b/HPPH.Test/Image/RefImageTest.cs @@ -118,6 +118,25 @@ public class RefImageTest } } + [TestMethod] + public void ImageRowIndexerSubImage() + { + (int width, int height) = SIZES[0]; + + RefImage image = TestDataHelper.CreateTestImage(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 row = image.Rows[y]; + Assert.AreEqual(image.Width, row.Length); + for (int x = 0; x < row.Length; x++) + Assert.AreEqual(TestDataHelper.GetColorFromLocation(163 + x, 280 + y), row[x]); + } + } + [TestMethod] public void ImageRowEnumerator() { @@ -155,6 +174,25 @@ public class RefImageTest } } + [TestMethod] + public void ImageColumnIndexerSubImage() + { + (int width, int height) = SIZES[0]; + + RefImage image = TestDataHelper.CreateTestImage(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 column = image.Columns[x]; + Assert.AreEqual(image.Height, column.Length); + for (int y = 0; y < column.Length; y++) + Assert.AreEqual(TestDataHelper.GetColorFromLocation(163 + x, 280 + y), column[y]); + } + } + [TestMethod] public void ImageColumnEnumerator() { diff --git a/HPPH/Images/ImageRows.cs b/HPPH/Images/ImageRows.cs index dfc1436..d50569d 100644 --- a/HPPH/Images/ImageRows.cs +++ b/HPPH/Images/ImageRows.cs @@ -15,6 +15,7 @@ public readonly ref struct ImageRows private readonly int _width; private readonly int _height; private readonly int _stride; + private readonly int _bpp; public int Count => _height; @@ -29,7 +30,7 @@ public readonly ref struct ImageRows { if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException(); - return new ImageRow(_data, ((row + _y) * _stride) + _x, _width); + return new ImageRow(_data, ((row + _y) * _stride) + (_x * _bpp), _width); } } @@ -46,6 +47,8 @@ public readonly ref struct ImageRows this._width = width; this._height = height; this._stride = stride; + + _bpp = T.ColorFormat.BytesPerPixel; } #endregion @@ -110,6 +113,7 @@ internal class IColorImageRows : IImageRows private readonly int _width; private readonly int _height; private readonly int _stride; + private readonly int _bpp; /// public int Count => _height; @@ -126,7 +130,7 @@ internal class IColorImageRows : IImageRows { if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException(); - return new IColorImageRow(_data, ((row + _y) * _stride) + _x, _width); + return new IColorImageRow(_data, ((row + _y) * _stride) + (_x * _bpp), _width); } } @@ -143,6 +147,8 @@ internal class IColorImageRows : IImageRows this._width = width; this._height = height; this._stride = stride; + + _bpp = T.ColorFormat.BytesPerPixel; } #endregion