IOLink 1.11.0
Loading...
Searching...
No Matches
gradientFloatImage.cpp

This code demonstrates how to create a vertical gradient image with values between 0.0 and 5.0.

This code demonstrates how to create a vertical gradient image with values between 0.0 and 5.0.Lines are written one by one from the bottom to the top.

#include <iomanip>
#include <iostream>
#include <vector>
#include <iolink/view/ImageViewFactory.h>
using namespace iolink;
std::shared_ptr<ImageView>
gradientFloatImage(const VectorXu64& shape)
{
// To create a float ImageView, the datatype of the image can be FLOAT (32bits) or DOUBLE (64 bits)
const DataType dt = DataTypeId::FLOAT;
// create an image view in CPU memory
std::shared_ptr<ImageView> image = ImageViewFactory::allocate(shape, dt);
// create a buffer to write a line of data
// this buffer is float-typed to match the image datatype
std::vector<float> lineBuffer(shape[0]);
// define the size of the line to write
const VectorXu64 lineSize{shape[0], 1};
std::cout << "Writing data line by line" << std::endl;
// define the step between two values in the gradient
const float stepLine = 5.0f / shape[1];
// write data line by line with a growing value
float valueInit = 0;
float valueToSet = valueInit;
for (size_t j = 0; j < shape[1]; ++j)
{
const RegionXu64 lineRegion(VectorXu64{0, j}, lineSize);
std::fill(lineBuffer.begin(), lineBuffer.end(), valueToSet);
image->writeRegion(lineRegion, lineBuffer.data());
valueToSet = valueInit + j * stepLine;
}
std::cout << "Writing completed" << std::endl;
return image;
}
// Display the content of a float ImageView.
static void
displayImageContent(std::shared_ptr<ImageView> image)
{
const VectorXu64& shape = image->shape();
RegionXu64 fullRegion = RegionXu64::createFullRegion(image->shape());
std::vector<float> buffer(fullRegion.elementCount());
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(8) << buffer[i + j * shape[0]] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int
main(int argc, char** argv)
{
std::shared_ptr<ImageView> image = gradientFloatImage({5, 30});
std::cout << "Shape of created image: " << image->shape() << std::endl;
displayImageContent(image);
return EXIT_SUCCESS;
}