| IOLink C# 1.11.0

Loading...
Searching...
No Matches
MetadataTree.cs

This code demonstrates how to store, modify and parse metadata in ImageViews. An ImageView is created and metadata are stored into it. Then the metadata are modified and parsed.

using IOLink;
using System;
using System.Runtime.Intrinsics;
namespace Examples
{
public class MetadataTree
{
private static void AddMetadata(ImageView image)
{
// if given image does not have write capability,
// metadata cannot be added
if (image.Support(ImageCapability.WRITE) == false)
{
throw new ApplicationException("Image does not support write capability");
}
// nodes are created one by one
MetadataNode root = MetadataNodeFactory.Create("root", null);
MetadataNode name = MetadataNodeFactory.Create(
"name", VariantDataValueFactory.Create("generated image for example"));
MetadataNode author = MetadataNodeFactory.Create(
"author", VariantDataValueFactory.Create("Arthur Conley"));
MetadataNode creationDate = MetadataNodeFactory.Create(
"creation_date", VariantDataValueFactory.Create("2024-08-23"));
root.AddChild(name);
root.AddChild(author);
root.AddChild(creationDate);
// you can also use the helper class to create the
// the helper class is more convenient to create trees with complex hierarchies
MetadataNodeHelper.CreatePath(
root, "parameters/param1", VariantDataValueFactory.Create(42));
MetadataNodeHelper.CreatePath(
root, "parameters/param2", VariantDataValueFactory.Create(3.14));
MetadataNodeHelper.CreatePath(
root, "parameters/param3", VariantDataValueFactory.Create("Hello world!"));
MetadataNodeHelper.CreatePath(
root, "parameters/version", VariantDataValueFactory.Create(1));
// assign the metadata tree to the image
image.Metadata = root;
}
private static void IncrementVersionMetadata(ImageView image)
{
// if given image does not have write capability,
// metadata cannot be modified
if (!image.Support(ImageCapability.WRITE))
{
throw new ApplicationException("Image does not support write capability");
}
// get the metadata tree from the image
ReadonlyMetadataNode root = image.Metadata;
// get the version node
ReadonlyMetadataNode version = MetadataNodeHelper.GetNode(root, "parameters/version");
// increment the version number
long versionNumber = VariantDataValueConverter.ToInt64(version.Value);
// clone the tree for modification
MetadataNode newRoot = root.Clone();
MetadataNodeHelper.CreatePath(
newRoot, "parameters/version", VariantDataValueFactory.Create(versionNumber + 1));
// assign the new metadata tree to the image
image.Metadata = newRoot;
}
static private void RecursiveDisplayMethod(ReadonlyMetadataNode node, int depth)
{
// display the node
Console.Write(new string(' ', depth * 2));
Console.Write(string.Format("{0} + :", node.Key));
// if there is a value associated with the node,
// display it
if (node.Value != null)
Console.Write(VariantDataValueConverter.ToString(node.Value));
Console.WriteLine("");
// display the children
foreach (MetadataNode child in node)
{
RecursiveDisplayMethod(child, depth + 1);
}
}
public static void Main(string[] args)
{
// create an ImageView in CPU memory
var image = ImageViewFactory.Allocate(new VectorXu64(100, 200), DataTypeId.UINT8);
// add metadata to the image
AddMetadata(image);
// display the metadata tree
RecursiveDisplayMethod(image.Metadata, 0);
// increment the version number in the metadata
IncrementVersionMetadata(image);
// display the metadata tree
RecursiveDisplayMethod(image.Metadata, 0);
Console.WriteLine("SUCCESS");
}
}
}
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.Support
bool Support(ImageCapabilitySet flags)
Checks if the ImageView supports the given capabilities.
Definition ImageView.cs:177
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.MetadataNodeHelper.GetNode
static ReadonlyMetadataNode GetNode(ReadonlyMetadataNode root, string path)
Helper returning child node of given root node using a path. ex: "/MRC/ext/pixelSize".
Definition MetadataNodeHelper.cs:70
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.MetadataNode.AddChild
void AddChild(MetadataNode child)
Add a node as child. If the given child key already exists in the current node, existing child is rep...
Definition MetadataNode.cs:164
IOLink.ReadonlyMetadataNode
Wrapper around MetadataNode exposing only method that does not modify the instance.
Definition ReadonlyMetadataNode.cs:7
IOLink.ReadonlyMetadataNode.Clone
MetadataNode Clone()
Create a mutable copy, proxy for MetadataNode.Clone
Definition ReadonlyMetadataNode.cs:39
IOLink.ReadonlyMetadataNode.Key
string Key
Proxy for MetadataNode.Key
Definition ReadonlyMetadataNode.cs:46
IOLink.ReadonlyMetadataNode.Value
VariantDataValue Value
Proxy for MetadataNode.Value
Definition ReadonlyMetadataNode.cs:52
IOLink.VariantDataValueConverter
Converter contains a set of method allowing to interpret a VariantDataValue as a determined type.
Definition VariantDataValueConverter.cs:14
IOLink.VariantDataValueConverter.ToInt64
static long ToInt64(VariantDataValue value)
Return converted metadata value into 64-bit signed integers.
Definition VariantDataValueConverter.cs:227
IOLink.VariantDataValueConverter.ToString
static string ToString(VariantDataValue value)
Return converted metadata value into string.
Definition VariantDataValueConverter.cs:290
IOLink.VariantDataValueFactory
A factory to create VariantDataValue instances from a great variety of types.
Definition VariantDataValueFactory.cs:14
IOLink.VectorXu64
A dynamically sized arithmetic vector.
Definition VectorXu64.cs:14
Examples
Definition CreateDataFrame.cs:6
IOLink
Definition AccessCapabilities.cs:11
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