Add pixmap bluring

This commit is contained in:
Nikita Lisitsa 2020-09-13 11:09:12 +03:00
parent 519adcae3a
commit 807daf3237
2 changed files with 66 additions and 0 deletions

View file

@ -0,0 +1,11 @@
#pragma once
#include <psemek/gfx/pixmap.hpp>
#include <psemek/pcg/seamless.hpp>
namespace psemek::pcg
{
gfx::basic_pixmap<float> blur(gfx::basic_pixmap<float> const & p, int size, float sigma, seamless_tag);
}

55
libs/pcg/source/blur.cpp Normal file
View file

@ -0,0 +1,55 @@
#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;
}
}