| IOLink 1.11.0

Loading...
Searching...
No Matches
metadataTree.cpp

This code demonstrates how to store, modify and parse metadata in ImageViews.

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.

#include <iostream>
#include <numeric>
#include <vector>
#include <iolink/VariantDataValueConverter.h>
#include <iolink/VariantDataValueFactory.h>
#include <iolink/metadata/MetadataNodeFactory.h>
#include <iolink/metadata/MetadataNodeHelper.h>
#include <iolink/view/ImageViewFactory.h>
using namespace iolink;
static void
addMetadata(std::shared_ptr<ImageView> image)
{
// if given image does not have WRITE capability, metadata cannot be added
if (!image->support(ImageCapability::WRITE))
{
throw std::runtime_error("Image does not have WRITE capability");
}
// create a metadata tree as following
// root
// |- "name" : "generated image for example"
// |- "author" : "Arthur Conley"
// |- "creation_date" : "2024-08-23"
// |- "parameters"
// |- "param1" : 42
// |- "param2" : 3.14
// |- "param3" : "hello world!"
// |- "version" : 1
// Nodes are created one by one
std::shared_ptr<MetadataNode> root = MetadataNodeFactory::create("root", nullptr);
std::shared_ptr<MetadataNode> nameNode =
MetadataNodeFactory::create("name", VariantDataValueFactory::create("generated image for example"));
std::shared_ptr<MetadataNode> authorNode =
MetadataNodeFactory::create("author", VariantDataValueFactory::create("Arthur Conley"));
std::shared_ptr<MetadataNode> createDateNode =
MetadataNodeFactory::create("creation_date", VariantDataValueFactory::create("2024-08-23"));
// and organized as a tree
root->addChild(nameNode);
root->addChild(authorNode);
root->addChild(createDateNode);
// you can also use the helper class to create the tree
// 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->setMetadata(root);
}
static void
incrementVersionMetadata(std::shared_ptr<ImageView> image)
{
// if given image does not have WRITE capability, metadata cannot be updated
if (!image->support(ImageCapability::WRITE))
{
throw std::runtime_error("Image does not have WRITE capability");
}
// the node to be updated cannot be modified directly
// you need to create a new node with the same name and replace the old one
std::shared_ptr<const MetadataNode> root = image->metadata();
// retrieve version number
std::shared_ptr<const MetadataNode> versionValue = MetadataNodeHelper::getNode(root, "parameters/version");
// if version node does not exist, raise an error
if (!versionValue)
{
throw std::runtime_error("version node does not exist");
}
int64_t version = VariantDataValueConverter::toInt64(versionValue->value());
// clone the tree for modification
std::shared_ptr<MetadataNode> rootCloned = root->clone();
MetadataNodeHelper::createPath(rootCloned, "parameters/version", VariantDataValueFactory::create(version + 1));
// assign the new tree including modification to the image
image->setMetadata(rootCloned);
}
static void
recursiveDisplayMethod(std::shared_ptr<const MetadataNode> root, size_t level)
{
std::string indent(level, ' ');
std::cout << indent << root->key() << " : ";
// if the node has a value, display it
if (root->value())
std::cout << VariantDataValueConverter::toString(root->value());
std::cout << std::endl;
for (auto child : *root)
{
recursiveDisplayMethod(child, level + 1);
}
}
static void
displayMetadata(std::shared_ptr<ImageView> image)
{
if (!image->metadata())
{
std::cout << "No metadata to display" << std::endl;
return;
}
std::shared_ptr<const MetadataNode> root = image->metadata();
std::cout << "Metadata tree:" << std::endl;
recursiveDisplayMethod(root, 0);
}
int
main(int argc, char** argv)
{
// create an image view in CPU memory
std::shared_ptr<ImageView> image = ImageViewFactory::allocate(VectorXu64{100, 200}, DataTypeId::UINT8);
// add metadata to the image
addMetadata(image);
// display metadata tree
displayMetadata(image);
// do some modifications in metadata
incrementVersionMetadata(image);
// display metadata tree after modification
displayMetadata(image);
return EXIT_SUCCESS;
}
iolink::VectorX
A dynamically sized arithmetic vector.
Definition VectorX.h:18
iolink
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8