// Return tensor from bitmap private static Tensor <float> ToTensor(Bitmap image) { var rect = new Rectangle(Point.Empty, image.Size); var tensor = new DenseTensor <float>(rect.ToNHWC()); var data = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var row = new byte[data.Stride]; for (var i = 0; i < rect.Height; ++i) { _ = data.GetRowArgb(row, 0, i); tensor.SetRowArgb(row, 0, i); } image.UnlockBits(data); return(tensor); }
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); } }