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

Commit 921327b

Browse files
committed
libgraphics: Fix blur algorithm
The blur algorithm had an error in the indexing for the vertical component of the blur and an error in the sliding window logic.
1 parent 0452a67 commit 921327b

File tree

3 files changed

+7
-98
lines changed

3 files changed

+7
-98
lines changed

libraries/libgraphics/Framebuffer.cpp

Lines changed: 7 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -126,80 +126,6 @@ void Framebuffer::copy_blitting(const Framebuffer& other, Rect other_area, const
126126
}
127127
}
128128

129-
void Framebuffer::blur(Gfx::Rect area, int radius) const {
130-
int window_size = radius * 2 + 1;
131-
area = area.overlapping_area(Rect{0, 0, width, height});
132-
133-
auto do_pass = [&]() {
134-
// First, apply blur horizontally.
135-
for(int y = area.y; y < area.y + area.height; y++) {
136-
int window[3] = {0, 0, 0};
137-
Color window_preblur[window_size];
138-
int preblur_index = 0;
139-
140-
// Populate window
141-
for(int i = -radius; i <= radius; i++) {
142-
auto color = data[(std::min(std::max(area.x + i, 0), width - 1)) + y * width];
143-
window_preblur[i + radius] = color;
144-
window[0] += color.r;
145-
window[1] += color.g;
146-
window[2] += color.b;
147-
}
148-
149-
for(int x = area.x; x < area.x + area.width; x++) {
150-
data[x + y * width] = {
151-
(uint8_t) (window[0] / window_size),
152-
(uint8_t) (window[1] / window_size),
153-
(uint8_t) (window[2] / window_size),
154-
};
155-
auto window_add = data[(std::min(x + radius + 1, width - 1)) + y * width];
156-
auto window_sub = window_preblur[preblur_index];
157-
window_preblur[preblur_index] = window_add;
158-
preblur_index++;
159-
preblur_index %= window_size;
160-
window[0] += (int) window_add.r - (int) window_sub.r;
161-
window[1] += (int) window_add.g - (int) window_sub.g;
162-
window[2] += (int) window_add.b - (int) window_sub.b;
163-
}
164-
}
165-
166-
// Then, apply blur vertically.
167-
for(int x = area.x; x < area.x + area.width; x++) {
168-
int window[3] = {0, 0, 0};
169-
Color window_preblur[window_size];
170-
int preblur_index = 0;
171-
172-
// Populate window
173-
for(int i = -radius; i <= radius; i++) {
174-
auto color = data[x + (std::min(std::max(area.y + i, 0), height - 1)) * width];
175-
window_preblur[i + radius] = color;
176-
window[0] += color.r;
177-
window[1] += color.g;
178-
window[2] += color.b;
179-
}
180-
181-
for(int y = area.y; y < area.y + area.height; y++) {
182-
data[x + y * width] = {
183-
(uint8_t) (window[0] / window_size),
184-
(uint8_t) (window[1] / window_size),
185-
(uint8_t) (window[2] / window_size),
186-
};
187-
auto window_add = data[x + (std::min(y + radius + 1, height - 1)) * width];
188-
auto window_sub = window_preblur[preblur_index];
189-
window_preblur[preblur_index] = window_add;
190-
preblur_index++;
191-
preblur_index %= window_size;
192-
window[0] += (int) window_add.r - (int) window_sub.r;
193-
window[1] += (int) window_add.g - (int) window_sub.g;
194-
window[2] += (int) window_add.b - (int) window_sub.b;
195-
}
196-
}
197-
};
198-
199-
for(int i = 0; i < 3; i++)
200-
do_pass();
201-
}
202-
203129
void Framebuffer::copy_blitting_flipped(const Framebuffer& other, Rect other_area, const Point& pos, bool flip_h, bool flip_v) const {
204130
//Make sure self_area is in bounds of the framebuffer
205131
Rect self_area = {pos.x, pos.y, other_area.width, other_area.height};
@@ -227,8 +153,9 @@ void Framebuffer::copy_blitting_flipped(const Framebuffer& other, Rect other_are
227153

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

231-
auto do_pass = [&]() { ;
158+
auto do_pass = [&]() {
232159
// First, apply blur horizontally.
233160
for(int y = area.y; y < area.y + area.height; y++) {
234161
int window[3] = {0, 0, 0};
@@ -250,11 +177,11 @@ void Framebuffer::blur(Gfx::Rect area, int radius) const {
250177
(uint8_t) (window[1] / window_size),
251178
(uint8_t) (window[2] / window_size),
252179
};
253-
auto window_add = data[(std::min(x + 5, width - 1)) + y * width];
180+
auto window_add = data[(std::min(x + radius + 1, width - 1)) + y * width];
254181
auto window_sub = window_preblur[preblur_index];
182+
window_preblur[preblur_index] = window_add;
255183
preblur_index++;
256184
preblur_index %= window_size;
257-
window_preblur[((preblur_index - 1) % window_size + window_size) % window_size] = window_add;
258185
window[0] += (int) window_add.r - (int) window_sub.r;
259186
window[1] += (int) window_add.g - (int) window_sub.g;
260187
window[2] += (int) window_add.b - (int) window_sub.b;
@@ -269,7 +196,7 @@ void Framebuffer::blur(Gfx::Rect area, int radius) const {
269196

270197
// Populate window
271198
for(int i = -radius; i <= radius; i++) {
272-
auto color = data[(x + std::min(std::max(area.y + i, 0), height - 1)) * width];
199+
auto color = data[x + (std::min(std::max(area.y + i, 0), height - 1)) * width];
273200
window_preblur[i + radius] = color;
274201
window[0] += color.r;
275202
window[1] += color.g;
@@ -282,11 +209,11 @@ void Framebuffer::blur(Gfx::Rect area, int radius) const {
282209
(uint8_t) (window[1] / window_size),
283210
(uint8_t) (window[2] / window_size),
284211
};
285-
auto window_add = data[x + (std::min(y + 5, height - 1)) * width];
212+
auto window_add = data[x + (std::min(y + radius + 1, height - 1)) * width];
286213
auto window_sub = window_preblur[preblur_index];
214+
window_preblur[preblur_index] = window_add;
287215
preblur_index++;
288216
preblur_index %= window_size;
289-
window_preblur[((preblur_index - 1) % window_size + window_size) % window_size] = window_add;
290217
window[0] += (int) window_add.r - (int) window_sub.r;
291218
window[1] += (int) window_add.g - (int) window_sub.g;
292219
window[2] += (int) window_add.b - (int) window_sub.b;

libraries/libgraphics/Framebuffer.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ namespace Gfx {
7676
*/
7777
void copy_blitting(const Framebuffer& other, Rect other_area, const Point& pos) const;
7878

79-
/**
80-
* Blurs an area of this framebuffer with an approximated gaussian blur.
81-
* @param area The area to blur.
82-
* @param radius The blur radius.
83-
*/
84-
void blur(Rect area, int radius = 4) const;
85-
8679
/**
8780
* Copies a part of another Image to this one, with alpha blending, flipped horizontally.
8881
* @param other The other Image to copy from.

services/pond/Window.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,6 @@ class Window {
269269
*/
270270
bool blurs_behind() const;
271271

272-
/**
273-
* Gets the shadow framebuffers for drawing shadows.
274-
* Sets whether the window should blur contents behind it.
275-
*/
276-
void set_blur_behind(bool blur);
277-
278-
/**
279-
* Returns whether the window blurs contents behind it.
280-
*/
281-
bool blurs_behind() const;
282-
283272
/**
284273
* Gets the shadow framebuffer for drawing shadows.
285274
*/

0 commit comments

Comments
 (0)