Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Window blur #55

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libraries/libgraphics/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ typedef union Color {
return *this = *this * other;
}

[[nodiscard]] constexpr Color operator+(Color other) const {
return {
(uint8_t)(r + other.r),
(uint8_t)(g + other.g),
(uint8_t)(b + other.b),
(uint8_t)(a + other.a)
};
}

[[nodiscard]] constexpr Color lightened(float amount = 0.25) const {
return darkened(1 + amount);
}
Expand Down
74 changes: 74 additions & 0 deletions libraries/libgraphics/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,80 @@ void Framebuffer::copy_blitting_flipped(const Framebuffer& other, Rect other_are
}
}

void Framebuffer::blur(Gfx::Rect area, int radius) const {
int window_size = radius * 2 + 1;
area = area.overlapping_area(Rect {0, 0, width, height});

auto do_pass = [&]() {
// First, apply blur horizontally.
for(int y = area.y; y < area.y + area.height; y++) {
int window[3] = {0, 0, 0};
Color window_preblur[window_size];
int preblur_index = 0;

// Populate window
for(int i = -radius; i <= radius; i++) {
auto color = data[(std::min(std::max(area.x + i, 0), width - 1)) + y * width];
window_preblur[i + radius] = color;
window[0] += color.r;
window[1] += color.g;
window[2] += color.b;
}

for(int x = area.x; x < area.x + area.width; x++) {
data[x + y * width] = {
(uint8_t) (window[0] / window_size),
(uint8_t) (window[1] / window_size),
(uint8_t) (window[2] / window_size),
};
auto window_add = data[(std::min(x + radius + 1, width - 1)) + y * width];
auto window_sub = window_preblur[preblur_index];
window_preblur[preblur_index] = window_add;
preblur_index++;
preblur_index %= window_size;
window[0] += (int) window_add.r - (int) window_sub.r;
window[1] += (int) window_add.g - (int) window_sub.g;
window[2] += (int) window_add.b - (int) window_sub.b;
}
}

// Then, apply blur vertically.
for(int x = area.x; x < area.x + area.width; x++) {
int window[3] = {0, 0, 0};
Color window_preblur[window_size];
int preblur_index = 0;

// Populate window
for(int i = -radius; i <= radius; i++) {
auto color = data[x + (std::min(std::max(area.y + i, 0), height - 1)) * width];
window_preblur[i + radius] = color;
window[0] += color.r;
window[1] += color.g;
window[2] += color.b;
}

for(int y = area.y; y < area.y + area.height; y++) {
data[x + y * width] = {
(uint8_t) (window[0] / window_size),
(uint8_t) (window[1] / window_size),
(uint8_t) (window[2] / window_size),
};
auto window_add = data[x + (std::min(y + radius + 1, height - 1)) * width];
auto window_sub = window_preblur[preblur_index];
window_preblur[preblur_index] = window_add;
preblur_index++;
preblur_index %= window_size;
window[0] += (int) window_add.r - (int) window_sub.r;
window[1] += (int) window_add.g - (int) window_sub.g;
window[2] += (int) window_add.b - (int) window_sub.b;
}
}
};

for(int i = 0; i < 3; i++)
do_pass();
}

void Framebuffer::copy_tiled(const Framebuffer& other, Rect other_area, const Point& pos) const {
//Make sure self_area is in bounds of the framebuffer
Rect self_area = {pos.x, pos.y, other_area.width, other_area.height};
Expand Down
Loading