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);
"name", VariantDataValueFactory.Create("generated image for example"));
"author", VariantDataValueFactory.Create("Arthur Conley"));
"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
root, "parameters/param1", VariantDataValueFactory.Create(42));
root, "parameters/param2", VariantDataValueFactory.Create(3.14));
root, "parameters/param3", VariantDataValueFactory.Create("Hello world!"));
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
// 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();
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.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");
}
}
}
Definition CreateDataFrame.cs:6