Pixelating an image

Hi guys, I've been struggling with this for a few days now.
My assignment is to pixelate an image based on users input.
I have a structure that stores the image, and in this structure I'm storing the height and width and a dynamic array of RGBA struct.

This is my function. my main issue (that I've found) is offsetting and figuring out a way to handle the "edge" if the dimensions don't add up to the set pixelation size.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// @------------- Pixelation X
bool filter_pixelation(float percentageX, float percentageY, Picture& pic)
{
    // http://www.mathsisfun.com/percentage.html
        /* 50% of 80 = (50/100) * 80 = 40 */
    size_t perX = percentageX * (float)pic.width;
    size_t perY = percentageY * (float)pic.height;

    if((perX <= 0 || perY <= 0) || (perX > pic.width || perY > pic.height))
        return 0;

    RGBA avg {0,0,0,0};

    // @-- Calculate the colours
    // Add up all the pixel values
    for(size_t y = 0; y < pic.height; y += perY)
    {
        for(size_t x = 0; x < pic.width; x += perX)
        {
            int offset = getIndex(x, y, pic.width);
            for(size_t yi = 0; yi < perY; yi++)
            {
                for(size_t xi = 0; xi < perX; xi++)
                {
                    int i = getIndex(xi, yi, pic.width);
                    addColour(pic.pixel[i + offset].rgba, avg);
                }
            }

            // Gets the average colour.
            divideColourBy(avg, perY * perX);

            // Applies the average value to the picture
            for(size_t yi = 0; yi < perY; yi++)
            {
                for(size_t xi = 0; xi < perX; xi++)
                {
                    int i = getIndex(xi, yi, pic.width);
                    setColour(avg, pic.pixel[i + offset].rgba);
                }
            }

            avg = {0,0,0,0};
        }

    }

    return true;
}



Here is the getIndex function:

1
2
3
4
int getIndex(int x, int y, int width)
{
    return y * width + x;
}


any response is much appreciated :D
You have to treat edges specially -- they should be 'infinite' -- when you hit the edge any 'pixels' beyond that are the same color.

You can have special processing (in getIndex) to return the edge pixel if trying to access beyond the edge.

You can make the RGBA array larger than the actual image, centering (or otherwise adjusting) the image to be inside that frame such that attempts to access pixels outside the frame will not fail.

Method 2 uses more memory and is a little more difficult to set-up, but it is faster when actually processing the image. If you are just doing a one-off image, use method 1.

disclosure: I've not read your code
Topic archived. No new replies allowed.