Skip to content

Commit bcda7de

Browse files
committed
Box blue algorithm
1 parent 779aedf commit bcda7de

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,40 @@ else()
7575
endif()
7676
source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${TARGET_SOURCE_FILES})
7777

78+
# Box blur
79+
project(boxblur)
80+
set(TARGET_SOURCE_FILES
81+
"boxblur.cpp"
82+
)
83+
add_executable(${PROJECT_NAME} ${TARGET_SOURCE_FILES})
84+
target_include_directories(${PROJECT_NAME} PRIVATE
85+
"${PROJECT_SOURCE_DIR}"
86+
)
87+
target_link_libraries(${PROJECT_NAME}
88+
stb
89+
glm::glm
90+
asio::asio
91+
Threads::Threads
92+
image
93+
)
94+
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
95+
if (NOT MSVC)
96+
target_compile_options(${PROJECT_NAME} PRIVATE
97+
"-Wall"
98+
"-Wextra"
99+
"-Wconversion"
100+
"-Wpedantic"
101+
"-Wshadow"
102+
"-Werror"
103+
)
104+
else()
105+
target_compile_options(${PROJECT_NAME} PRIVATE
106+
"/W4"
107+
"/WX"
108+
)
109+
endif()
110+
source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${TARGET_SOURCE_FILES})
111+
78112
# Gaussian blur
79113
project(gaussian)
80114
set(TARGET_SOURCE_FILES

boxblur.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)