Skip to content

Instantly share code, notes, and snippets.

@MehdiNS
Created October 31, 2016 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MehdiNS/9374370ec3eeaf7788016cf53e245b08 to your computer and use it in GitHub Desktop.
Save MehdiNS/9374370ec3eeaf7788016cf53e245b08 to your computer and use it in GitHub Desktop.
Generate the bayer matrix of order n (of dimension 2^n x 2^n) iteratively
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
using uint = unsigned int;
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
for (auto& el : vec)
{
os << el << ' ';
}
return os;
}
uint bayer(uint x, uint y, uint order)
{
uint res = 0;
for (uint i = 0; i < order; ++i)
{
uint xOdd_XOR_yOdd = (x & 1) ^ (y & 1);
uint xOdd = x & 1;
res = ((res << 1 | xOdd_XOR_yOdd) << 1) | xOdd;
x >>= 1;
y >>= 1;
}
return res;
}
void computeBayer(uint order)
{
uint dim = 1 << order;
uint nbElt = dim * dim;
std::vector<uint> matrix;
for (uint i = 0; i < nbElt; i++)
{
matrix.emplace_back(bayer(i / dim, i % dim, order));
}
std::cout << matrix << std::endl;
}
int main()
{
uint order = 2;
assert(order >= 1);
std::cout << "Bayer of order : " << order << std::endl;
computeBayer(order);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment