0

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. enter image description here (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).

loki
  • 314
  • 4
  • 17
  • 1
    Exactly the same as with shaders - by getting each pixel, calculating blured colour (e.g. gaussian blur) and writing it into output surface. – keltar Apr 09 '15 at 04:09
  • But I have a problem with a transparency. The resulting text has dark outline. – loki Apr 09 '15 at 04:31
  • 1
    What's your current code you have problem with? From the way you've asked the question I assume you had it working with shader - what was a shader's code? Edit question to add all necessary details, preferably even screenshots of your problem and result you want to get. – keltar Apr 09 '15 at 05:48

1 Answers1

0

I solved this problem. I create two SDL_Surface: the first text and the second with the background color as the text. And then I make a blurry only alpha channel:

    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);
                    blur_sum_factor++;
                    ad += a;
                }
            }
            ad /= blur_sum_factor;
            p = get_pixel32(sfc_blur_target, x, y);
            SDL_GetRGB(p, sfc_blur_target->format, &r, &g, &b);
            a = (Uint8)ad;
            p = SDL_MapRGBA(sfc_blur_target->format, r, g, b, a);
            put_pixel32(sfc_blur_target, x, y, p);
        }
    }

Next I want to add the Gaussian Blur and parallelize the process.

loki
  • 314
  • 4
  • 17