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>
static std::shared_ptr<ImageView>
gradientFloatImage(
const Vector2f& inputRange)
{
std::shared_ptr<ImageView> image = ImageViewFactory::allocate(shape, dt);
std::vector<float> lineBuffer(shape[0]);
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)
{
std::fill(lineBuffer.begin(), lineBuffer.end(), valueToSet);
image->writeRegion(lineRegion, lineBuffer.data());
valueToSet = valueInit + j * stepLine;
}
return image;
}
template <typename T>
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(6) << buffer[i + j * shape[0]] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int
main(int argc, char** argv)
{
std::shared_ptr<ImageView> floatImage = gradientFloatImage(range);
displayImageContent<float>(floatImage);
std::shared_ptr<ImageView> imageInt =
ImageViewFactory::adaptDynamicRange(floatImage,
DataTypeId::UINT16,
Vector2d{0,
static_cast<double>(std::numeric_limits<uint16_t>().max())});
displayImageContent<uint16_t>(imageInt);
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.
An arithmetic vector.
Definition Vector.h:37
A dynamically sized arithmetic vector.
Definition VectorX.h:18
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8