| IOLink 1.11.0

Loading...
Searching...
No Matches
gradientFloatToInt.cpp

This code demonstrates how to adapt a float ImageView into a 16-bit unsigned integer ImageView.

This code demonstrates how to adapt a float ImageView into a 16-bit unsigned integer ImageView.Data are converted on the fly, and the resulting image has values between 0 and 65535. Both images are displayed to show their content.

#include <iomanip>
#include <iostream>
#include <vector>
#include <iolink/view/ImageViewFactory.h>
using namespace iolink;
// Create a a vertical gradient into a float ImageView
// using the given input range [min, max].
static std::shared_ptr<ImageView>
gradientFloatImage(const Vector2f& inputRange)
{
const VectorXu64 shape = VectorXu64{5, 30};
const DataType dt = DataTypeId::FLOAT;
std::shared_ptr<ImageView> image = ImageViewFactory::allocate(shape, dt);
std::vector<float> lineBuffer(shape[0]);
const VectorXu64 lineSize{shape[0], 1};
const float stepLine = (inputRange[1] - inputRange[0]) / shape[1];
float valueInit = inputRange[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;
}
return image;
}
// Display the content of an ImageView containing values of type T.
template <typename T>
static void
displayImageContent(std::shared_ptr<ImageView> image)
{
const VectorXu64& shape = image->shape();
RegionXu64 fullRegion = RegionXu64::createFullRegion(image->shape());
std::vector<T> 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(6) << buffer[i + j * shape[0]] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int
main(int argc, char** argv)
{
Vector2f range{0.0f, 5.0f};
// create a float image containing a vertical gradient
// with values between 0 and 5.0
std::shared_ptr<ImageView> floatImage = gradientFloatImage(range);
displayImageContent<float>(floatImage);
// convert the float image to a 16-bit integer image
// an adapter is added to the float image to convert the values on the fly
// data are not duplicated
std::shared_ptr<ImageView> imageInt =
ImageViewFactory::adaptDynamicRange(floatImage,
DataTypeId::UINT16,
Vector2d(range),
Vector2d{0, static_cast<double>(std::numeric_limits<uint16_t>().max())});
displayImageContent<uint16_t>(imageInt);
return EXIT_SUCCESS;
}
iolink::DataType
Stores information about a data type.
Definition DataType.h:162
iolink::RegionX
A Region using dynamic vectors.
Definition RegionX.h:14
iolink::RegionX::elementCount
size_t elementCount() const
Returns the number of cells in this region.
iolink::Vector
An arithmetic vector.
Definition Vector.h:37
iolink::VectorX
A dynamically sized arithmetic vector.
Definition VectorX.h:18
iolink
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8