psemek/libs/pcg/source/blur.cpp
2020-09-13 11:09:12 +03:00

55 lines
1.1 KiB
C++

#include <psemek/pcg/blur.hpp>
#include <cmath>
#include <vector>
namespace psemek::pcg
{
static std::vector<float> gauss_coeffs(int size, float sigma)
{
std::vector<float> res(2 * size + 1);
float sum = 0.f;
for (int i = -size; i <= size; ++i)
{
float x = (i / sigma);
res[i + size] = std::exp(- x * x);
sum += res[i + size];
}
for (auto & c : res)
c /= sum;
return res;
}
gfx::basic_pixmap<float> blur(gfx::basic_pixmap<float> const & p, int size, float sigma, seamless_tag)
{
gfx::basic_pixmap<float> res({p.width(), p.height()}, 0.f);
auto c = gauss_coeffs(size, sigma);
for (int y = 0; y < p.height(); ++y)
{
for (int x = 0; x < p.width(); ++x)
{
for (int j = - size; j <= size; ++j)
{
for (int i = - size; i <= size; ++i)
{
int ix = (x + i);
while (ix < 0) ix += p.width();
if (ix >= p.width()) ix = ix % p.width();
int iy = (y + j);
while (iy < 0) iy += p.height();
if (iy >= p.height()) iy = iy % p.height();
res(x, y) += p(ix, iy) * c[i + size] * c[j + size];
}
}
}
}
return res;
}
}