This code demonstrates how it is possible to copy a whole ImageView content into another one.
This code demonstrates how it is possible to copy a whole ImageView content into another one.
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <iolink/VariantDataValueFactory.h>
#include <iolink/metadata/MetadataNodeFactory.h>
#include <iolink/metadata/MetadataNodeHelper.h>
#include <iolink/view/ImageViewFactory.h>
std::shared_ptr<ImageView>
createImageView()
{
const DataType dt = DataTypeId::VEC3_UINT8;
std::shared_ptr<ImageView> image = ImageViewFactory::allocate(shape, dt);
image->setAxesInterpretation(AxesInterpretationId::IMAGE_SEQUENCE);
image->setImageInterpretation(ImageInterpretation::RGB);
image->setAlpha(false);
image->setSpatialOrigin(
VectorXd{1.0, 2.0, 3.0});
image->setSpatialSpacing(
VectorXd{0.1, 0.2, 0.3});
image->setSpatialUnit("mm");
std::shared_ptr<MetadataNode> rootNode = MetadataNodeFactory::create("root", nullptr);
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->setMetadata(rootNode);
RegionXu64 fullRegion = RegionXu64::createFullRegion(shape);
std::vector<uint8_t> buffer(image->dataType().byteCount() * fullRegion.
elementCount());
std::iota(buffer.begin(), buffer.end(), 0);
image->writeRegion(fullRegion, buffer.data());
return image;
}
void
copyImageView(std::shared_ptr<ImageView> imageSrc, std::shared_ptr<ImageView> imageDst)
{
if (!imageSrc->support(ImageCapability::READ))
{
throw std::runtime_error("Source image does not have READ capability");
}
if (!imageDst->support(ImageCapability::WRITE | ImageCapability::RESHAPE))
{
throw std::runtime_error("Destination image does not have WRITE and RESHAPE capability");
}
imageDst->reshape(imageSrc->shape(), imageSrc->dataType());
imageDst->setProperties(imageSrc->properties());
if (imageSrc->metadata())
imageDst->setMetadata(imageSrc->metadata()->clone());
if (imageSrc->support(ImageCapability::MEMORY_ACCESS) && imageDst->support(ImageCapability::MEMORY_ACCESS))
{
size_t bufferSize = imageSrc->bufferSize();
const void* srcPtr = imageSrc->bufferReadOnly();
void* dstPtr = imageDst->buffer();
std::copy_n(reinterpret_cast<const uint8_t*>(srcPtr), bufferSize, reinterpret_cast<uint8_t*>(dstPtr));
}
else
{
RegionXu64 fullRegion = RegionXu64::createFullRegion(imageSrc->shape());
std::vector<uint8_t> buffer(imageSrc->dataType().byteCount() * fullRegion.
elementCount());
imageSrc->readRegion(fullRegion, buffer.data());
imageDst->writeRegion(fullRegion, buffer.data());
}
}
int
main(int argc, char** argv)
{
std::shared_ptr<ImageView> imageSource = createImageView();
std::shared_ptr<ImageView> imageDestination = ImageViewFactory::allocate(
VectorXu64{1, 1}, DataTypeId::FLOAT);
copyImageView(imageSource, imageDestination);
std::cout << imageDestination->toString() << std::endl;
std::cout << imageDestination->properties()->toString() << std::endl;
std::cout << imageDestination->metadata()->toString() << std::endl;
return EXIT_SUCCESS;
}
Stores information about a data type.
Definition DataType.h:162
A Region using dynamic vectors.
Definition RegionX.h:14
size_t elementCount() const
Returns the number of cells in this region.
A dynamically sized arithmetic vector.
Definition VectorX.h:18
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8