0

I will appreciate it if someone can bail me out here. I am stuck on the Blur. The logic of my code is using a nested loop for the 2D array while adding or subtracting one from the current array activated by the Loop. The issue I believe is that I do not know the condition to insert in my if statement to ignore indexes that are less than 0 or equal to the height or width. Attached is the snapshot of my code.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
float averageRed, averageGreen, averageBlue;
float totalRed, totalGreen, totalBlue;
int  non = 0;

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        int ki = i - 1, kj = j - 1;
        int li = i + 1, lj = j + 1;
        
        if (ki < 0 || kj < 0 || li >= height || lj >= width)
        {
            non++;
        }
        
        totalRed = (float) image[ki][kj].rgbtRed + (float) image[ki][j].rgbtRed + (float) image[ki][lj].rgbtRed 
                   + (float) image[i][kj].rgbtRed + (float) image[i][j].rgbtRed + (float) image[i][lj].rgbtRed
                   + (float) image[li][kj].rgbtRed + (float) image[li][j].rgbtRed + (float) image[li][lj].rgbtRed;

        totalGreen = (float) image[ki][kj].rgbtGreen + (float) image[ki][j].rgbtGreen + (float) image[ki][lj].rgbtGreen 
                   + (float) image[i][kj].rgbtGreen + (float) image[i][j].rgbtGreen + (float) image[i][lj].rgbtGreen
                   + (float) image[li][kj].rgbtGreen + (float) image[li][j].rgbtGreen + (float) image[li][lj].rgbtGreen;
                   
        totalBlue = (float) image[ki][kj].rgbtBlue + (float) image[ki][j].rgbtBlue + (float) image[ki][lj].rgbtBlue 
                   + (float) image[i][kj].rgbtBlue + (float) image[i][j].rgbtBlue + (float) image[i][lj].rgbtBlue
                   + (float) image[li][kj].rgbtBlue + (float) image[li][j].rgbtBlue + (float) image[li][lj].rgbtBlue;
        
        averageRed = totalRed / (9 - non);
        averageGreen = totalGreen / (9 - non);
        averageBlue = totalBlue / (9 - non);
        
        image[i][j].rgbtRed = round(averageRed);
        image[i][j].rgbtGreen = round(averageGreen);
        image[i][j].rgbtBlue = round(averageBlue);
        
    }
}

return;

}

You must log in to answer this question.

Browse other questions tagged .