public void TestGetRowArgbArraySegment() { // 3x2 argb image var bitmap = new byte[] { 20, 40, 60, 255, 30, 50, 70, 255, 40, 60, 80, 255, 120, 140, 160, 255, 130, 150, 170, 255, 140, 160, 180, 255, }; var tensorData = bitmap .Where((_, i) => (i + 1) % 4 != 0) .Select(b => b / 255f) .ToArray(); for (int i = 0; i < tensorData.Length / 3; ++i) { (tensorData[i * 3 + 0], tensorData[i * 3 + 2]) = (tensorData[i * 3 + 2], tensorData[i * 3 + 0]); } var tensor = new DenseTensor <float>(tensorData, new int[] { 1, 2, 3, 3 }); var data = new byte[bitmap.Length]; var row = new ArraySegment <byte>(data, 12, 8); var len = tensor.GetRowArgb(row, 0, 0); CollectionAssert.AreEqual(bitmap.Take(row.Count).ToArray(), row.Take(len).ToArray()); len = tensor.GetRowArgb(row, 2, 0); CollectionAssert.AreEqual(bitmap.Skip(8).Take(4).ToArray(), row.Take(len).ToArray()); len = tensor.GetRowArgb(row, 0, 1); CollectionAssert.AreEqual(bitmap.Skip(12).Take(row.Count).ToArray(), row.Take(len).ToArray()); }
public void TestSetRowArgbArray() { using (var bitmap = SurfaceExtensionsTest.LoadResource("style.png")) { var line = new Rectangle(13, 27, 64, 1); var data = bitmap.LockBits(line, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var expected = new byte[line.Width * 4]; _ = data.GetRowArgb(expected, 0, 0); bitmap.UnlockBits(data); var tensor = new DenseTensor <float>(new Rectangle(Point.Empty, bitmap.Size).ToNHWC()); tensor.SetRowArgb(expected, line.X, line.Y); var actual = new byte[expected.Length]; _ = tensor.GetRowArgb(actual, line.X, line.Y); CollectionAssert.AreEqual(expected, actual); } }
public void TestSetRowArgbArraySegment() { using (var bitmap = SurfaceExtensionsTest.LoadResource("style.png")) { var line = new Rectangle(13, 27, 64, 1); var data = bitmap.LockBits(line, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var expected = new byte[data.Stride + 32]; var memory = new ArraySegment <byte>(expected, 8, line.Width * 4); _ = data.GetRowArgb(memory, 0, 0); bitmap.UnlockBits(data); var tensor = new DenseTensor <float>(new Rectangle(Point.Empty, bitmap.Size).ToNHWC()); tensor.SetRowArgb(memory, line.X, line.Y); var actual = new byte[memory.Count]; _ = tensor.GetRowArgb(actual, line.X, line.Y); CollectionAssert.AreEqual(expected.Skip(memory.Offset).Take(memory.Count).ToArray(), actual); } }
public void TestCopyToTensor() { using (var bitmap = LoadResource("style.png")) using (var surface = LoadSurface(bitmap)) { var portion = new Rectangle(8, 4, 16, 32); var tensor = new DenseTensor <float>(portion.ToNHWC()); _ = surface.CopyToTensor(tensor, portion); var data = bitmap.LockBits(portion, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var expected = new byte[data.Stride]; var actual = new byte[expected.Length]; for (int i = 0; i < data.Height; ++i) { _ = data.GetRowArgb(expected, 0, i); _ = tensor.GetRowArgb(actual, 0, i); CollectionAssert.AreEqual(expected, actual); } bitmap.UnlockBits(data); } }