| IOLink C# 1.11.0

Loading...
Searching...
No Matches
GradientFloatToInt.cs

This code demonstrates how to adapt a float ImageView into a 16-bit unsigned integer ImageView. Data are converted on the fly, and the resulting image has values between 0 and 65535. Both images are displayed to show their content.

using IOLink;
using Newtonsoft.Json.Linq;
using System;
using System.Runtime.Intrinsics;
namespace Examples
{
public class GradientFloatToInt
{
private static ImageView GenerateGradientWithGivenRange(Vector2f inputRange)
{
VectorXu64 shape = new VectorXu64(5, 30);
// 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 = ((float)inputRange[1] - (float)inputRange[0]) / 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 = (float)inputRange[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 + j * stepLine;
}
Console.WriteLine("Writing completed");
return image;
}
// Display the content of an ImageView containing values
// of type T.
private static void displayImageContent<T>(ImageView image)
{
VectorXu64 shape = image.Shape;
RegionXu64 fullRegion = RegionXu64.CreateFullRegion(image.Shape);
T[] buffer = new T[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.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine();
}
public static void Main(string[] args)
{
Vector2f inputRange = new Vector2f(0.0f, 5.0f);
ImageView floatImage = GenerateGradientWithGivenRange(inputRange);
// display ImageView content
GradientFloatToInt.displayImageContent<float>(floatImage);
// Adapt the float ImageView into a 16-bit unsigned
// integer ImageView. Data are not duplicated, only
// the datatype is changed. Conversion is done on the
// fly when reading the data.
// Range of data is mapped from [0, 5] to [0, 65535].
ImageView IntImage = ImageViewFactory.AdaptDynamicRange(
floatImage,
DataTypeId.UINT16,
new Vector2d((double)inputRange[0], (double)inputRange[1]),
new Vector2d(0.0, 65535.0));
// display ImageView content
GradientFloatToInt.displayImageContent<UInt16>(IntImage);
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.ImageViewFactory.AdaptDynamicRange
static ImageView AdaptDynamicRange(ImageView view, DataType type, Vector2d inputRange, Vector2d outputRange)
Creates an ImageView from another view with given type by mapping dynamic range.
Definition ImageViewFactory.cs:465
IOLink.ImageView
Interface representing an N dimensional image.
Definition ImageView.cs:30
IOLink.ImageView.Shape
VectorXu64 Shape
The ImageView shape, the size of each of its dimensions.
Definition ImageView.cs:71
IOLink.ImageView.ReadRegion
unsafe void ReadRegion(RegionXu64 region, byte[] dst)
Read a region of the image into a buffer.
Definition ImageView.cs:270
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.Vector2d
An arithmetic vector.
Definition Vector2d.cs:29
IOLink.Vector2f
An arithmetic vector.
Definition Vector2f.cs:29
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