| 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);
MetadataNodeHelper.CreatePath(
rootNode, "level1/data1", VariantDataValueFactory.Create("value1"));
MetadataNodeHelper.CreatePath(
rootNode, "level1/data2", VariantDataValueFactory.Create("value2"));
MetadataNodeHelper.CreatePath(
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());
}
}
}
IOLink.DataType
Stores information about a data type.
Definition DataType.cs:24
IOLink.DataType.ByteCount
uint ByteCount()
Get the number of bits required to store the type.
Definition DataType.cs:282
IOLink.ImageTypeId
Enum for the image type (see ImageType)
Definition ImageTypeId.cs:20
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.Metadata
ReadonlyMetadataNode Metadata
The MetadataNode associated with this ImageView instance.
Definition ImageView.cs:92
IOLink.ImageView.Properties
ReadonlyImageProperties Properties
The ImageProperties associated with this ImageView instance.
Definition ImageView.cs:84
IOLink.ImageView.Shape
VectorXu64 Shape
The ImageView shape, the size of each of its dimensions.
Definition ImageView.cs:71
IOLink.ImageView.Type
DataType Type
The type of the image's elements.
Definition ImageView.cs:77
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.Support
bool Support(ImageCapabilitySet flags)
Checks if the ImageView supports the given capabilities.
Definition ImageView.cs:177
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.ImageView.Reshape
virtual void Reshape(VectorXu64 shape, DataType dataType)
Change the shape and sample data type of the image.
Definition ImageView.cs:465
IOLink.MetadataNodeFactory
A factory to build MetadataNode instances.
Definition MetadataNodeFactory.cs:14
IOLink.MetadataNodeFactory.Create
static MetadataNode Create(string key, VariantDataValue value)
To create a metadata node with a key and a value.
Definition MetadataNodeFactory.cs:65
IOLink.MetadataNodeHelper
A collection of functions to handle paths on MetadataNode instances.
Definition MetadataNodeHelper.cs:14
IOLink.MetadataNodeHelper.CreatePath
static ReadonlyMetadataNode CreatePath(MetadataNode origin, string path, VariantDataValue value)
Helper allowing to create the given path in given tree origin. ex: /MRC/ext/pixelSize" or "MRC/ext/pi...
Definition MetadataNodeHelper.cs:103
IOLink.MetadataNode
Class which represents one node from a metadata tree. Each node contains a key and also an associated...
Definition MetadataNode.cs:18
IOLink.ReadonlyMetadataNode.Clone
MetadataNode Clone()
Create a mutable copy, proxy for MetadataNode.Clone
Definition ReadonlyMetadataNode.cs:39
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.VariantDataValueFactory
A factory to create VariantDataValue instances from a great variety of types.
Definition VariantDataValueFactory.cs:14
IOLink.Vector3d
An arithmetic vector.
Definition Vector3d.cs:29
IOLink.VectorXu64
A dynamically sized arithmetic vector.
Definition VectorXu64.cs:14
Examples
Definition CreateDataFrame.cs:6
IOLink
Definition AccessCapabilities.cs:11
IOLink.ImageInterpretation
ImageInterpretation
Interpretation of an Image.
Definition ImageInterpretation.cs:15
IOLink.ImageCapability
ImageCapability
Define capabilities of an ImageView.
Definition ImageCapability.cs:15
IOLink.DataTypeId
DataTypeId
A collection of built-in data types.
Definition DataTypeId.cs:15