This code demonstrates how to create a custom implementation of an ImageView to generate a checkerboard.
This code demonstrates how to create a custom implementation of an ImageView to generate a checkerboard. The image itself won't be allocated in memory, but rather generated on-the-fly when requested.
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>
#include <iolink/Error.h>
#include <iolink/UidGenerator.h>
#include <iolink/view/ImageView.h>
#include <iolink/view/ImageViewFactory.h>
{
public:
: m_shape{width, height}
, m_tileSize{width / tileCountByRow, height / tileCountByColumn}
{
m_properties = ImageProperties::fromDataType(DataTypeId::UINT8);
m_properties->imageInfo().setBitDepth(1);
m_properties->imageInfo().setAlpha(false);
m_properties->imageInfo().setInterpretation(ImageInterpretation::BINARY);
m_properties->calibration().setUnit("cm");
m_properties->calibration().setSpacing(
Vector3d{0.1, 0.1, 1});
}
{
return ImageCapability::READ;
}
inline std::shared_ptr<const ImageProperties>
properties()
override {
return m_properties; }
inline std::shared_ptr<const MetadataNode>
metadata()
const override {
return nullptr; }
{
const uint8_t valueBlack = 0;
const uint8_t valueWhite = 1;
uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
for (size_t y = 0; y < sizeReg[1]; ++y)
{
for (size_t x = 0; x < sizeReg[0]; ++x)
{
if ((tileId[0] + tileId[1]) % 2 == 0)
ptr[y * sizeReg[0] + x] = valueBlack;
else
ptr[y * sizeReg[0] + x] = valueWhite;
}
}
}
{
public:
ViewOriginImpl() = default;
uint64_t
uid()
const override
{
return UidGenerator::generate();
}
{
return 0;
}
std::shared_ptr<ViewOriginExtension>
parent(
size_t idx)
const override {
throw Error(
"No parent available"); }
{
return ViewDataOrigin::GENERATED;
}
{
return "Checkerboard";
}
};
std::shared_ptr<Extension>
extension(
size_t id)
override
{
if (id == ViewOriginExtension::EXTENSION_ID)
return std::make_shared<ViewOriginImpl>();
else
throw Error(
"Extension not available");
}
private:
std::shared_ptr<ImageProperties> m_properties;
};
static void
displayImageContent(std::shared_ptr<ImageView> image)
{
RegionXu64 fullRegion = RegionXu64::createFullRegion(image->shape());
image->readRegion(fullRegion, buffer.data());
std::cout << "Image content: " << std::endl;
for (size_t j = 0; j < shape[1]; ++j)
{
for (size_t i = 0; i < shape[0]; ++i)
{
std::cout << std::setw(1) << static_cast<size_t>(buffer[i + j * shape[0]]) << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int
main(int argc, char** argv)
{
std::shared_ptr<ImageView> checkerboard = std::make_shared<CheckerboardImageView>(20, 20, 4, 4);
std::cout << "Checkerboard image:" << std::endl;
std::cout << checkerboard->toString() << std::endl;
std::cout << checkerboard->properties()->toString() << std::endl;
std::cout << checkerboard->viewOrigin()->toString() << std::endl;
displayImageContent(checkerboard);
std::shared_ptr<ImageView> subCheckerboard = ImageViewFactory::extractRegion(checkerboard, subRegion);
displayImageContent(subCheckerboard);
return EXIT_SUCCESS;
}
std::string displayName() const override
Returns the name of the current view to display.
Definition customImageView.cpp:122
size_t parentCount() const override
Returns the count of parents used to create the current view.
Definition customImageView.cpp:110
ViewDataOrigin dataOrigin() const override
Returns the data origin of current view.
Definition customImageView.cpp:117
uint64_t uid() const override
Returns an unique identifier for current view.
Definition customImageView.cpp:104
std::shared_ptr< ViewOriginExtension > parent(size_t idx) const override
Returns, for the given index, the viewOrigin extension of the parent used to create the current view.
Definition customImageView.cpp:115
Definition customImageView.cpp:21
std::shared_ptr< const ImageProperties > properties() override
Return the view's properties.
Definition customImageView.cpp:52
std::shared_ptr< const MetadataNode > metadata() const override
Return the root node of the metadata attached to this image.
Definition customImageView.cpp:55
void readRegion(const RegionXu64 ®ion, void *dst) override
Read a region of the image into a buffer.
Definition customImageView.cpp:58
DataType dataType() override
Return the data type of the samples of this image.
Definition customImageView.cpp:42
ImageCapabilitySet capabilities() const override
Return the capabilities of this image.
Definition customImageView.cpp:44
VectorXu64 & shape() override
Return the shape of the image, each component containing the size in this dimension.
Definition customImageView.cpp:39
Stores information about a data type.
Definition DataType.h:162
Error base class.
Definition Error.h:13
Template class to handle a flags system from an enum.
Definition FlagSet.h:43
static const ImageType IMAGE
Represents a 2D image (COLUMN, ROW)
Definition ImageType.h:219
Interface representing an N dimensional image.
Definition ImageView.h:70
'Invalid argument' error raised when an argument is not the one expected in a public method.
Definition Error.h:36
A Region using dynamic vectors.
Definition RegionX.h:14
static RegionX createFullRegion(const SizeType ®ionSize)
Utility factory that create the region of origin [0, 0, 0] and given size.
size_t dimensionCount() const
The dimension of this region.
size_t elementCount() const
Returns the number of cells in this region.
const VectorType & origin() const
Returns the position of the region's origin.
const SizeType & size() const
Returns the region's size.
bool contains(const RegionX &other) const
Checks if this region conatins another.
A dynamically sized arithmetic vector.
Definition VectorX.h:18
std::shared_ptr< T > extension()
Get an extension of the view, corresponding to the given type.
Definition View.h:37
Extension used to access view's origins if it exists.
Definition ViewOriginExtension.h:62
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8
ViewDataOrigin
Enum which represents the different locations where view data may come from.
Definition ViewOriginExtension.h:16