55 lines
1.2 KiB
C++
55 lines
1.2 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 (std::size_t y = 0; y < p.height(); ++y)
|
|
{
|
|
for (std::size_t 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 (static_cast<std::size_t>(ix) >= p.width()) ix = ix % p.width();
|
|
|
|
int iy = (y + j);
|
|
while (iy < 0) iy += p.height();
|
|
if (static_cast<std::size_t>(iy) >= p.height()) iy = iy % p.height();
|
|
|
|
res(x, y) += p(ix, iy) * c[i + size] * c[j + size];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
}
|