IOLink C# 1.11.0
Loading...
Searching...
No Matches
CheckerBoard.cs

This code demonstrates how to create an image view in memory and write data into it to create a checkerboard pattern. Tiles are drawn pixel by pixel.

using IOLink;
using System.Diagnostics;
using System;
namespace Examples
{
public class CheckerBoard
{
static ImageView Build(ulong sideLength)
{
// a checker board is always square
VectorXu64 shape = new VectorXu64(sideLength, sideLength);
// a checkerboard is black and white, so we use UINT8
// pixel type
DataType dtype = DataTypeId.UINT8;
// create the image view in memory
ImageView image = ImageViewFactory.Allocate(shape, dtype);
// a checkerboard contains 10 tiles in both direction
const ulong tileCount = 10;
byte[] blackValue = new byte[]{ 0 }; // black pixel value to set
byte[] whiteValue = new byte[]{ 255 }; // white pixel value to set
ulong tilSizeInPixel = (ulong)(shape[0] / tileCount);
// draw tiles in the image pixel by pixel
for (ulong i = 0; i < shape[0]; ++i)
{
for (ulong j = 0; j < shape[1]; ++j)
{
// compute the tile index
ulong tileIndexX = (ulong)(i / tilSizeInPixel);
ulong tileIndexY = (ulong)(j / tilSizeInPixel);
// set the pixel value to 0 or 255 depending
// on the tile index parity
if ((tileIndexX + tileIndexY) % 2 == 0)
image.Write(new VectorXu64(i, j), blackValue);
else
image.Write(new VectorXu64(i, j), whiteValue);
}
}
return image;
}
public static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
var imageCheckerBoard = Build(1000);
sw.Stop();
Console.WriteLine("Checkerboard image generated in " + sw.ElapsedMilliseconds + " ms");
// display some information about the image
Console.WriteLine(imageCheckerBoard.ToString());
Console.WriteLine("SUCCESS");
}
}
}
Definition CreateDataFrame.cs:6