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

This code demonstrates how to create an image view in memory and write data into it to create a checkerboard pattern.

This code demonstrates how to create an image view in memory and write data into it to create a checkerboard pattern.The code creates a 2D image of given size and fills it pixel by pixel.

#include <chrono>
#include <iostream>
#include <vector>
#include <iolink/view/ImageViewFactory.h>
using namespace iolink;
std::shared_ptr<ImageView>
checkerBoard(const size_t sideLength)
{
// a checker board is always square
const VectorXu64 shape{sideLength, sideLength};
// a checkerboard is black and white, so we use UINT8 pixel type
const DataType dt = DataTypeId::UINT8;
// create an image view in memory
auto image = ImageViewFactory::allocate(shape, dt);
// a checkerboard contains 10 tiles in both direction
const size_t tileCount = 10;
uint8_t blackValue = 0; // black pixel value to set
uint8_t whiteValue = 255; // white pixel value to set
size_t tileSizeInPixel = static_cast<size_t>(shape[0] / tileCount);
// draw tiles in the image pixel by pixel
for (size_t i = 0; i < shape[0]; ++i)
{
for (size_t j = 0; j < shape[1]; ++j)
{
// compute the tile index
size_t tileIndexX = i / tileSizeInPixel;
size_t tileIndexY = j / tileSizeInPixel;
// set the pixel value to 0 or 255 depending on the tile index parity
if ((tileIndexX + tileIndexY) % 2 == 0)
image->write({i, j}, &blackValue);
else
image->write({i, j}, &whiteValue);
}
}
return image;
}
int
main(int argc, char** argv)
{
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
auto image = checkerBoard(1000);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Checkerboard generated in "
<< std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << " microseconds"
<< std::endl;
std::cout << "Generated ImageView: " << image->toString() << std::endl;
return EXIT_SUCCESS;
}