psemek/libs/pcg/source/blur.cpp

44 lines
1.1 KiB
C++

#include <psemek/pcg/blur.hpp>
#include <psemek/gfx/color.hpp>
#include <cmath>
#include <vector>
namespace psemek::pcg
{
namespace detail
{
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::pixmap_rgb blur(gfx::pixmap_rgb const & p, int size, float sigma, seamless_tag)
{
auto converted = util::map([](gfx::color_rgb const & c){ return gfx::to_colorf(c); }, p);
converted = blur(converted, size, sigma, seamless);
return util::map([](geom::vector<float, 3> const & c){ return gfx::to_coloru8(c); }, converted);
}
gfx::pixmap_rgba blur(gfx::pixmap_rgba const & p, int size, float sigma, seamless_tag)
{
auto converted = util::map([](gfx::color_rgba const & c){ return gfx::to_colorf(c); }, p);
converted = blur(converted, size, sigma, seamless);
return util::map([](geom::vector<float, 4> const & c){ return gfx::to_coloru8(c); }, converted);
}
}