0

hello so I've been coding blur for a few days and have been running check50 for a while, and I keep getting results that show that the pixel values are just off by a max range of 10. It is really pestering me as I am unsure which part of my code resulted in this inaccuracy, such as the incorrect assignment of a variable type to variables, etc. . Help would be appreciated. The check50 results will be shown in the image below and the code below. Bear with me because it is pretty WET. I added 2 otherfunctions to the blur function to find the height and width of the pixel. enter image description here

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE cloneimage[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            cloneimage[i][j] = image[i][j];
        }
    }
    int topleft = (- width - 1);
    int top = (- width);
    int topright = (- width + 1);
    int right = 1;
    int bottomright = (width + 1);
    int bottom = (width);
    int bottomleft = (width - 1);
    int left = (-1);
    int start_position = 1;
    int end_position = (height * width);
    for (int k = 0; k < height; k++)
    {
        for (int l = 0; l < width; l++)
        {
            float totalBlue;
            float totalRed;
            float totalGreen;
            float selected = 1.0;
            int position = ( (k * width) + (l + 1) );
            totalBlue = cloneimage[k][l].rgbtBlue;
            totalGreen = cloneimage[k][l].rgbtGreen;
            totalRed = cloneimage[k][l].rgbtRed;
            //Top left
            if ((position + topleft >= start_position) && (position + topleft <= end_position))
            {
                totalBlue += cloneimage[findheight((position + topleft), width)][findwidth((position + topleft), width)].rgbtBlue;
                totalGreen += cloneimage[findheight((position + topleft), width)][findwidth((position + topleft), width)].rgbtGreen;
                totalRed += cloneimage[findheight((position + topleft), width)][findwidth((position + topleft), width)].rgbtRed;
                selected++;
            }
            //Top
            if ((position + top >= start_position) && (position + top <= end_position))
            {
                totalBlue += cloneimage[findheight((position + top), width)][findwidth((position + top), width)].rgbtBlue;
                totalGreen += cloneimage[findheight((position + top), width)][findwidth((position + top), width)].rgbtGreen;
                totalRed += cloneimage[findheight((position + top), width)][findwidth((position + top), width)].rgbtRed;
                selected++;
            }
            //Top right
            if ((position + topright >= start_position) && (position + topright <= end_position))
            {
                totalBlue += cloneimage[findheight((position + topright), width)][findwidth((position + topright), width)].rgbtBlue;
                totalGreen += cloneimage[findheight((position + topright), width)][findwidth((position + topright), width)].rgbtGreen;
                totalRed += cloneimage[findheight((position + topright), width)][findwidth((position + topright), width)].rgbtRed;
                selected++;
            }
            //Right
            if ((position + right >= start_position) && (position + right <= end_position))
            {
                totalBlue += cloneimage[findheight((position + right), width)][findwidth((position + right), width)].rgbtBlue;
                totalGreen += cloneimage[findheight((position + right), width)][findwidth((position + right), width)].rgbtGreen;
                totalRed += cloneimage[findheight((position + right), width)][findwidth((position + right), width)].rgbtRed;
                selected++;
            }
            //Bottom Right
            if ((position + bottomright >= start_position) && (position + bottomright <= end_position))
            {
                totalBlue += cloneimage[findheight((position + bottomright), width)][findwidth((position + bottomright), width)].rgbtBlue;
                totalGreen += cloneimage[findheight((position + bottomright), width)][findwidth((position + bottomright), width)].rgbtGreen;
                totalRed += cloneimage[findheight((position + bottomright), width)][findwidth((position + bottomright), width)].rgbtRed;
                selected++;
            }
            //Bottom
            if ((position + bottom >= start_position) && (position + bottom <= end_position))
            {
                totalBlue += cloneimage[findheight((position + bottom), width)][findwidth((position + bottom), width)].rgbtBlue;
                totalGreen += cloneimage[findheight((position + bottom), width)][findwidth((position + bottom), width)].rgbtGreen;
                totalRed += cloneimage[findheight((position + bottom), width)][findwidth((position + bottom), width)].rgbtRed;
                selected++;
            }
            //Bottom Left
            if ((position + bottomleft >= start_position) && (position + bottomleft <= end_position))
            {
                totalBlue += cloneimage[findheight((position + bottomleft), width)][findwidth((position + bottomleft), width)].rgbtBlue;
                totalGreen += cloneimage[findheight((position + bottomleft), width)][findwidth((position + bottomleft), width)].rgbtGreen;
                totalRed += cloneimage[findheight((position + bottomleft), width)][findwidth((position + bottomleft), width)].rgbtRed;
                selected++;
            }
            //Left
            if ((position + left >= start_position) && (position + left <= end_position))
            {
                totalBlue += cloneimage[findheight((position + left), width)][findwidth((position + left), width)].rgbtBlue;
                totalGreen += cloneimage[findheight((position + left), width)][findwidth((position + left), width)].rgbtGreen;
                totalRed += cloneimage[findheight((position + left), width)][findwidth((position + left), width)].rgbtRed;
                selected++;
            }
            //Set new averaged colour component values for the subject pixel only; other pixels will get averaged once it's their turn
            int blurBlue = round(totalBlue / selected);
            int blurGreen = round(totalGreen / selected);
            int blurRed = round(totalRed / selected);
            if (blurBlue > 255)
            {
                blurBlue = 255;
            }
            if (blurGreen > 255)
            {
                blurGreen = 255;
            }
            if (blurRed > 255)
            {
                blurRed = 255;
            }
            image[k][l].rgbtBlue = blurBlue;
            image[k][l].rgbtGreen = blurGreen;
            image[k][l].rgbtRed = blurRed;
        }
    }
    return;
}

int findheight(int position, int width)
{
    int h;
    if (position % width == 0)
    {
        h = (position / width) - 1;
        return h;
    }
    else
    {
        h = position / width;
        return h;
    }
}

int findwidth(int position, int width)
{
    int w;
    if (position % width == 0)
    {
        w = (width - 1);
        return w;
    }
    else
    {
        w = (position % width) - 1;
        return w;
    }
}

1 Answer 1

1

I'll be honest. I tried to go through the code briefly, but I couldn't follow it. If I sat down with pen and paper and spent some serious time, maybe I could. Simply put, I don't know if the code is on track or way off, but it seems far too complicated to me.

The blur function simply looks at the pixels surrounding the current one and blurs based on their values. Instead of several convoluted calculations, the code simply needs to look at the current position, plus the pixels at the current position +/-1 for height, width or both. It also needs to make sure that the adjacent pixels aren't off the edge, meaning < 0 or > height or width minus 1.

Overly complex code should always be a red flag that maybe there's a simpler way to do something. It invariably leads to code bugs and is difficult to troubleshoot or modify later. You might consider a new approach based on the comments above.

Having said that, be aware of the impact of integer division vs. regular division. Integer division can often (but not always) be the source of one-off errors or results that are "almost" right.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .