| IOLink C# 1.11.0

Loading...
Searching...
No Matches
CheckerBoardFast.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 region by region, and is faster than pixel by pixel.

using IOLink;
using System.Diagnostics;
using System;
namespace Examples
{
public class CheckerBoardFast
{
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;
VectorXu64 tileSize = shape / tileCount;
RegionXu64 tileRegion = RegionXu64.CreateFullRegion(tileSize);
byte[] blackTileBuffer = new byte[tileRegion.ElementCount]; // buffer of black pixels
byte[] whiteTileBuffer = new byte[tileRegion.ElementCount]; // buffer of white pixels
Array.Fill(blackTileBuffer, (byte)0);
Array.Fill(whiteTileBuffer, (byte)255);
// draw tiles in the image pixel by pixel
for (ulong i = 0; i < tileCount; ++i)
{
for (ulong j = 0; j < tileCount; ++j)
{
// compute the origin position of the region
// to write
VectorXu64 origin = new VectorXu64(i, j) * tileSize;
RegionXu64 currentTileRegion = new RegionXu64(origin, tileSize);
// write the black or white tile depending on
// the tile index parity
if ((i + j) % 2 == 0)
image.WriteRegion(currentTileRegion, blackTileBuffer);
else
image.WriteRegion(currentTileRegion, whiteTileBuffer);
}
}
return image;
}
public static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
var imageCheckerBoard = Build(1000);
sw.Stop();
Console.WriteLine("Checkerboard (faster method) image generated in "
+ sw.ElapsedMilliseconds + " ms");
// display some information about the image
Console.WriteLine(imageCheckerBoard.ToString());
Console.WriteLine("SUCCESS");
}
}
}
IOLink.DataType
Stores information about a data type.
Definition DataType.cs:24
IOLink.ImageViewFactory
This factory is aimed at creating dataset views.
Definition ImageViewFactory.cs:21
IOLink.ImageViewFactory.Allocate
static ImageView Allocate(VectorXu64 shape, DataType type, ImageProperties properties, MetadataNode metadata)
Creates a memory image with the given shape and type.
Definition ImageViewFactory.cs:86
IOLink.ImageView
Interface representing an N dimensional image.
Definition ImageView.cs:30
IOLink.ImageView.WriteRegion
unsafe void WriteRegion(RegionXu64 region, byte[] src)
Write a input buffer into a given region of the image.
Definition ImageView.cs:387
IOLink.RegionXu64
A Region using dynamic vectors.
Definition RegionXu64.cs:14
IOLink.RegionXu64.ElementCount
uint ElementCount
The number of elements in this region.
Definition RegionXu64.cs:139
IOLink.RegionXu64.CreateFullRegion
static RegionXu64 CreateFullRegion(VectorXu64 regionSize)
Utility factory that create the region of origin [0, 0, 0] and given size.
Definition RegionXu64.cs:173
IOLink.VectorXu64
A dynamically sized arithmetic vector.
Definition VectorXu64.cs:14
Examples
Definition CreateDataFrame.cs:6
IOLink
Definition AccessCapabilities.cs:11
IOLink.DataTypeId
DataTypeId
A collection of built-in data types.
Definition DataTypeId.cs:15