| IOLink 1.11.0

Loading...
Searching...
No Matches
CheckerBoardFast.cpp

Faster method to create a checkerboard pattern in a memory image view The image is written tile by tile, using a buffer to write the tile values at once.

Faster method to create a checkerboard pattern in a memory image view The image is written tile by tile, using a buffer to write the tile values at once.

#include <chrono>
#include <iostream>
#include <vector>
#include <iolink/view/ImageViewFactory.h>
using namespace iolink;
std::shared_ptr<ImageView>
checkerBoard_Fast(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;
const VectorXu64 tileSize = shape / tileCount;
RegionXu64 tileRegion = RegionXu64::createFullRegion(tileSize);
// allocate a buffer to write a tile (black or white) at once
std::vector<uint8_t> blackTileBuffer(tileRegion.elementCount(), 0);
std::vector<uint8_t> whiteTileBuffer(tileRegion.elementCount(), 255);
// draw tiles in the image tile by tile
for (size_t i = 0; i < tileCount; ++i)
{
for (size_t j = 0; j < tileCount; ++j)
{
// compute the origin position of the region to write
VectorXu64 origin = VectorXu64{i, j} * tileSize;
RegionXu64 currentTileRegion(origin, tileSize);
// write the black or white tile depending on the tile index parity
if ((i + j) % 2 == 0)
image->writeRegion(currentTileRegion, blackTileBuffer.data());
else
image->writeRegion(currentTileRegion, whiteTileBuffer.data());
}
}
return image;
}
int
main(int argc, char** argv)
{
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
auto image = checkerBoard_Fast(1000);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Checkerboard (fast method) 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;
}
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::VectorX
A dynamically sized arithmetic vector.
Definition VectorX.h:18
iolink
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8