|
| 1 | +/** |
| 2 | + * @file gaussian.cpp |
| 3 | + * @author mononerv ([email protected]) |
| 4 | + * @brief Box blur |
| 5 | + * https://en.wikipedia.org/wiki/Box_blur |
| 6 | + * @date 2022-10-14 |
| 7 | + * |
| 8 | + * @copyright Copyright (c) 2022 mononerv |
| 9 | + */ |
| 10 | +#include <random> |
| 11 | +#include <filesystem> |
| 12 | +#include <cmath> |
| 13 | +#include <iostream> |
| 14 | + |
| 15 | +#include "image.hpp" |
| 16 | + |
| 17 | +auto box_blur(nrv::image const& img) -> nrv::image { |
| 18 | + nrv::image output{img.width(), img.height(), img.channels()}; |
| 19 | + nrv::render_img(img, [&](glm::ivec2 const& pos, glm::vec4 const&) { |
| 20 | + auto sum_at = [&](glm::ivec2 const& offset) { |
| 21 | + return img.get_pixel_rgba(pos.x + offset.x, pos.y + offset.y); |
| 22 | + }; |
| 23 | + |
| 24 | + std::int32_t const blur_width = 1; |
| 25 | + std::int32_t const blur_height = 1; |
| 26 | + auto sum = glm::vec4{0.0f}; |
| 27 | + auto denom = 0.0f; |
| 28 | + for (std::int32_t i = -blur_height; i <= blur_height; ++i) { |
| 29 | + for (std::int32_t j = -blur_width; j <= blur_width; ++j) { |
| 30 | + sum += sum_at({j, i}); |
| 31 | + denom += 1.0f; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + output.set_pixel(pos.x, pos.y, sum / denom); |
| 36 | + }); |
| 37 | + return output; |
| 38 | +} |
| 39 | + |
| 40 | +auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int { |
| 41 | + if (argc < 2) { |
| 42 | + std::cout << "No file given\n"; |
| 43 | + std::cout << "usage: " << argv[0] << " {filename}\n"; |
| 44 | + return 1; |
| 45 | + } |
| 46 | + |
| 47 | + std::filesystem::path filename = argv[1]; |
| 48 | + if (!std::filesystem::exists(filename)) { |
| 49 | + std::cout << "Not a valid file\n"; |
| 50 | + return 1; |
| 51 | + } |
| 52 | + |
| 53 | + nrv::image image{filename}; |
| 54 | + auto out = box_blur(image); |
| 55 | + nrv::write_png("box_blur_out.png", out); |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
0 commit comments