I want to add blur effect to drawing text. I use SDL2
and SDL_TTF
.
SDL_Surface
has a transparency.
How to do it without shaders?
Below is the algorithm that I use. This is a simple averaging.
Uint32 p;
Uint8 r, g, b, a;
int x, y, px, py;
int blur_sum_factor;
for (y = 0; y < sfc_blur->h; y++)
{
for (x = 0; x < sfc_blur->w; x++)
{
double rd = 0.0, gd = 0.0, bd = 0.0, ad = 0.0;
blur_sum_factor = 0;
for (py = -blur_size; py <= blur_size; py++)
{
for (px = -blur_size; px <= blur_size; px++)
{
int x1 = x + px;
int y1 = y + py;
if (x1 < 0 || x1 >= sfc_blur->w || y1 < 0 || y1 >= sfc_blur->h)
continue;
p = get_pixel32(sfc_blur, x1, y1);
SDL_GetRGBA(p, sfc_blur->format, &r, &g, &b, &a);
rd += r;
gd += g;
bd += b;
blur_sum_factor++;
ad += a;
}
}
rd /= blur_sum_factor;
gd /= blur_sum_factor;
bd /= blur_sum_factor;
ad /= blur_sum_factor;
r = (Uint8)rd;
g = (Uint8)gd;
b = (Uint8)bd;
a = (Uint8)ad;
p = SDL_MapRGBA(sfc_blur_target->format, r, g, b, a);
put_pixel32(sfc_blur_target, x, y, p);
}
}
In result I have a dark area around the text.
(red background for better demonstration - the blur is applied only to text with transparency.)
p.s. I had never used shaders. And in this task I can't use shaders (even if I wanted it).