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

This code demonstrates how it is possible to copy a whole ImageView content into another one.

using IOLink;
using System;
namespace Examples
{
public class CopyImageView
{
// Create an ImageView with some content
private static ImageView CreateImageView()
{
VectorXu64 shape = new VectorXu64(10, 20, 30);
DataType dt = DataTypeId.VEC3_UINT8;
// create an image view in CPU memory
var image = ImageViewFactory.Allocate(shape, dt);
// set some properties (not exhaustive)
image.AxesInterpretation = ImageTypeId.IMAGE_SEQUENCE;
image.ImageInterpretation = ImageInterpretation.RGB;
image.HasAlpha = false;
image.SpatialOrigin = new Vector3d(1.0, 2.0, 3.0);
image.SpatialSpacing = new Vector3d(0.1, 0.2, 0.3);
image.SpatialUnit = "mm";
// create some metadata
MetadataNode rootNode = MetadataNodeFactory.Create("root", null);
rootNode, "level1/data1", VariantDataValueFactory.Create("value1"));
rootNode, "level1/data2", VariantDataValueFactory.Create("value2"));
rootNode, "level2/data3", VariantDataValueFactory.Create("value3"));
image.Metadata = rootNode;
// write some data (anything)
RegionXu64 fullRegion = RegionXu64.CreateFullRegion(shape);
Byte[] buffer = new byte[image.Type.ByteCount() * fullRegion.ElementCount];
Array.Fill<Byte>(buffer, 0x42);
image.WriteRegion(fullRegion, buffer);
return image;
}
private static void CopyImageViewContent(ImageView imageSrc, ImageView imageDst)
{
// when source image origin is not controlled, we
// should check if we have READ capability this test
// is not mandatory, but it is a good practice to
// check if we can read the source image
if (!imageSrc.Support(ImageCapability.READ))
{
throw new ApplicationException("Source image does not have READ capability");
}
// check if destination image has WRITE and RESHAPE
// capability
if (!imageDst.Support(ImageCapability.WRITE | ImageCapability.RESHAPE))
{
throw new ApplicationException(
"Destination image does not have WRITE and RESHAPE capability");
}
// destination image is reshaped to the same shape
// and data type as the source image
imageDst.Reshape(imageSrc.Shape, imageSrc.Type);
// properties are copied from source to destination
imageDst.Properties = imageSrc.Properties;
// metadata are copied from source to destination
if (imageSrc.Metadata != null)
imageDst.Metadata = imageSrc.Metadata.Clone();
// copy pixels/voxels one shot
// this is the fastest way to copy the whole image
// but it can require a lot of memory a copy chunk by
// chunk could be more memory efficient
RegionXu64 fullRegion = RegionXu64.CreateFullRegion(imageSrc.Shape);
// dimension a buffer to store the whole image
// temporarily
Byte[] buffer = new byte[imageSrc.Type.ByteCount() * fullRegion.ElementCount];
// read/write pixels/voxels
imageSrc.ReadRegion(fullRegion, buffer);
imageDst.WriteRegion(fullRegion, buffer);
}
public static void Main(string[] args)
{
// create an ImageView with some content
var imageSrc = CreateImageView();
// create an ImageView with any shape and data type
var imageDst = ImageViewFactory.Allocate(new VectorXu64(1, 1), DataTypeId.INT8);
// copy the content of the source image into the
// destination image
CopyImageViewContent(imageSrc, imageDst);
Console.WriteLine(imageSrc.ToString());
Console.WriteLine(imageSrc.Properties.Clone().ToString());
Console.WriteLine(imageSrc.Metadata.Clone().ToString());
}
}
}
Definition CreateDataFrame.cs:6