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

This code demonstrates how to create a vertical gradient image with values between 0.0 and 5.0. Lines are written one by one from the bottom to the top.

using IOLink;
using System;
using System.Runtime.Intrinsics;
namespace Examples
{
public class GradientFloatImage
{
static ImageView Generate(VectorXu64 shape)
{
// To create a float ImageView, the datatype of the
// image can be FLOAT (32bits) or DOUBLE (64 bits)
DataType dt = DataTypeId.FLOAT;
// create an image view in CPU memory
ImageView image = ImageViewFactory.Allocate(shape, dt);
// create a buffer to write a line of data
// this buffer is float-typed to match the image
// datatype
float[] lineBuffer = new float[shape[0]];
VectorXu64 lineSize = new VectorXu64(shape[0], 1);
// define the step between two values in the gradient
float stepLine = 5.0f / shape[1];
Console.WriteLine("Writing data slice by slice, line by line");
// write data slice by slice, line by line with a
// growing value
float valueInit = 0;
float valueToSet = valueInit;
for (ulong j = 0; j < shape[1]; ++j)
{
RegionXu64 lineRegion = new RegionXu64(new VectorXu64(0, j), lineSize);
// fill buffer with the same value
Array.Fill(lineBuffer, valueToSet);
image.WriteRegion(lineRegion, lineBuffer);
valueToSet = valueInit + stepLine * j;
}
Console.WriteLine("Writing completed");
return image;
}
// Display the content of an ImageView containing values
// of type float
private static void displayImageContent(ImageView image)
{
VectorXu64 shape = image.Shape;
float[] buffer = new float[fullRegion.ElementCount];
image.ReadRegion(fullRegion, buffer);
Console.WriteLine("Image content: ");
for (ulong j = 0; j < shape[1]; ++j)
{
for (ulong i = 0; i < shape[0]; ++i)
{
Console.Write(buffer[i + j * shape[0]] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
public static void Main(string[] args)
{
ImageView image = Generate(new VectorXu64(5, 30));
Console.WriteLine("Shape of generated image: " + image.Shape.ToString());
displayImageContent(image);
Console.WriteLine("SUCCESS");
}
}
}
Definition CreateDataFrame.cs:6