Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - D4RKS0UL23

Pages: [1]
1
Graphics / sf::Image get every pixel around a pixel without crashing
« on: October 28, 2017, 03:26:42 pm »
I am currently trying to create a program that blurrs a given image. To do that I get the average rgb-values of all the pixels around one pixel, and then I set the pixel to this value.
The problem is, that if I am at a pixel that is at the border/edge of an image, the program will crash because it tries to get pixel information of pixels that are outside the image (eg. getPixel(-1, 0);).

I could technically do dozens of if-statements, but I think it is no really elegant, fast or easy to maintain. Do you have any other suggestions? Currently I am only blurring every pixel except for the ones at the borders. But this looks a bit off, since it creates a non-fitting outline around the image.

If required, here is my code that blurrs an image:
void Framework::Blurr(unsigned level)
{
        unsigned counter = 0;

        while (counter < level)
        {
                for (int y = 1; y < orig.getSize().y - 1; y++)
                {
                        for (int x = 1; x < orig.getSize().x - 1; x++)
                        {
                                double r, g, b;
                               
                                r = (orig.getPixel(x-1, y-1).r + orig.getPixel(x, y-1).r + orig.getPixel(x+1, y-1).r +
                                        orig.getPixel(x, y-1).r + orig.getPixel(x+1, y).r +
                                        orig.getPixel(x-1, y+1).r + orig.getPixel(x, y+1).r + orig.getPixel(x+1, y+1).r) / 8;

                                g = (orig.getPixel(x-1, y-1).g + orig.getPixel(x, y-1).g + orig.getPixel(x+1, y-1).g +
                                        orig.getPixel(x, y-1).g + orig.getPixel(x+1, y).g +
                                        orig.getPixel(x-1, y+1).g + orig.getPixel(x, y+1).g + orig.getPixel(x+1, y+1).g) / 8;

                                b = (orig.getPixel(x-1, y-1).b + orig.getPixel(x, y-1).b + orig.getPixel(x+1, y-1).b +
                                        orig.getPixel(x, y-1).b + orig.getPixel(x+1, y).b +
                                        orig.getPixel(x-1, y+1).b + orig.getPixel(x, y+1).b + orig.getPixel(x+1, y+1).b) / 8;

                                post.setPixel(x, y, sf::Color(r, g, b));
                        }
                }
                orig = post;
                counter++;
        }


        text.loadFromImage(post);
        sprite.setTexture(text);
}

Pages: [1]