0

so I modified the code from last time, and it seems that instead of making copies of the image and calculating on them, takes the first if statement and makes it the new image so all the other values are wrong. Please how do I fix this?

#include "helpers.h"
#include <math.h>

void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy_image[height][width], avg_image[height][width];

for (int i = 0; i < height; i ++)
{
    for (int j = 0; j < width; j ++)
    {
        copy_image[i][j] = image[i][j];
        avg_image[i][j] = image[i][j];
    }
}

for (int i = 0, count = 0; i < height; i ++)
{
    for (int j = 0; j < width; j ++)
    {
        float sum_red = 0, sum_green = 0, sum_blue = 0;

        //middle pixel
        if (i >= 0 && j >= 0)
        {
            for (int a = i - 1; a < i + 2; a ++)
            {
                for (int b = j - 1; b < j + 2; b ++)
                {
                    sum_red += copy_image[a][b].rgbtRed;
                    sum_green += copy_image[a][b].rgbtGreen;
                    sum_blue += copy_image[a][b].rgbtBlue;
                }
            }
            
            //calc avg
            avg_image[i][j].rgbtRed = round(sum_red / 9);
            avg_image[i][j].rgbtGreen = round(sum_green / 9);
            avg_image[i][j].rgbtBlue = round(sum_blue / 9);
            
            image[i][j].rgbtRed =  avg_image[i][j].rgbtRed;
            image[i][j].rgbtGreen = avg_image[i][j].rgbtGreen;
            image[i][j].rgbtBlue = avg_image[i][j].rgbtBlue;
        }



        //CORNER PIXELS

        //left bottom corner pixel
        else if (i == height - 1 && j == 0)
        {
            //loop through surrounding 4 pixels
            for (int a = i; a < 2; a ++)
            {
                for (int b = j; b < 2; b ++)
                {
                    sum_red += copy_image[a][b].rgbtRed;
                    sum_green += copy_image[a][b].rgbtGreen;
                    sum_blue += copy_image[a][b].rgbtBlue;
                }
            }

            //calc avgs
            avg_image[i][j].rgbtRed = round(sum_red / 4);
            avg_image[i][j].rgbtGreen = round(sum_green / 4);
            avg_image[i][j].rgbtBlue = round(sum_blue / 4);
            
            image[i][j].rgbtRed =  avg_image[i][j].rgbtRed;
            image[i][j].rgbtGreen = avg_image[i][j].rgbtGreen;
            image[i][j].rgbtBlue = avg_image[i][j].rgbtBlue;
        }

        //left top corner pixel
        else if (i == 0 && j == 0)
        {
            //loop through surrounding 4 pixels
            for (int a = i - 1; a < 2; a ++)
            {
                for (int b = j; b < 2; b ++)
                {
                    sum_red += copy_image[a][b].rgbtRed;
                    sum_green += copy_image[a][b].rgbtGreen;
                    sum_blue += copy_image[a][b].rgbtBlue;
                }
            }
            //get averages
            avg_image[i][j].rgbtRed = round(sum_red / 4);
            avg_image[i][j].rgbtGreen = round(sum_green / 4);
            avg_image[i][j].rgbtBlue = round(sum_blue / 4);
            
            image[i][j].rgbtRed =  avg_image[i][j].rgbtRed;
            image[i][j].rgbtGreen = avg_image[i][j].rgbtGreen;
            image[i][j].rgbtBlue = avg_image[i][j].rgbtBlue;
        }

        //right top corner pixel
        else if (i == 0 && j == width - 1)
        {
            //loop through the four pixels
            for (int a = i - 1; a < 2; a ++)
            {
                for (int b = j - 1; b < 2; b ++)
                {
                    sum_red += copy_image[a][b].rgbtRed;
                    sum_green += copy_image[a][b].rgbtGreen;
                    sum_blue += copy_image[a][b].rgbtBlue;
                }
            }
            //get averages
            avg_image[i][j].rgbtRed = round(sum_red / 4);
            avg_image[i][j].rgbtGreen = round(sum_green / 4);
            avg_image[i][j].rgbtBlue = round(sum_blue / 4);
            
            image[i][j].rgbtRed =  avg_image[i][j].rgbtRed;
            image[i][j].rgbtGreen = avg_image[i][j].rgbtGreen;
            image[i][j].rgbtBlue = avg_image[i][j].rgbtBlue;
        }

        //right bottom corner pixel
        else if (i == height - 1 && j == width - 1)
        {
            //loop through the four pixels
            for (int a = i; a < 2; a ++)
            {
                for (int b = j - 1; b < 2; b ++)
                {
                    sum_red += copy_image[a][b].rgbtRed;
                    sum_green += copy_image[a][b].rgbtGreen;
                    sum_blue += copy_image[a][b].rgbtBlue;
                }
            }
            //get averages
            avg_image[i][j].rgbtRed = round(sum_red / 4);
            avg_image[i][j].rgbtGreen= round(sum_green / 4);
            avg_image[i][j].rgbtBlue = round(sum_blue / 4);
            
            image[i][j].rgbtRed =  avg_image[i][j].rgbtRed;
            image[i][j].rgbtGreen = avg_image[i][j].rgbtGreen;
            image[i][j].rgbtBlue = avg_image[i][j].rgbtBlue;
        }

        //EDGES

        //top edge
        else if ((i == 0) && (j >= 1 && j <= width - 2))
        {
            for (int a = i; a < 2; a ++)
            {
                for (int b = j - 1; b < j + 2; b ++)
                {
                    sum_red += copy_image[a][b].rgbtRed;
                    sum_green += copy_image[a][b].rgbtGreen;
                    sum_blue += copy_image[a][b].rgbtBlue;
                }
            }

            //avgs
            avg_image[i][j].rgbtRed = round(sum_red / 6);
            avg_image[i][j].rgbtGreen = round(sum_green / 6);
            avg_image[i][j].rgbtBlue = round(sum_blue / 6);
            
            image[i][j].rgbtRed =  avg_image[i][j].rgbtRed;
            image[i][j].rgbtGreen = avg_image[i][j].rgbtGreen;
            image[i][j].rgbtBlue = avg_image[i][j].rgbtBlue;
        }

        //left edge
        else if ((i >= 1 && i <= height - 2) && (j == 0))
        {
            for (int a = i - 1; a < i + 2; a ++)
            {
                for (int b = j; b < 2; b ++)
                {
                    sum_red += copy_image[a][b].rgbtRed;
                    sum_green += copy_image[a][b].rgbtGreen;
                    sum_blue += copy_image[a][b].rgbtBlue;
                }
            }

            //avgs
            avg_image[i][j].rgbtRed = round(sum_red / 6);
            avg_image[i][j].rgbtGreen = round(sum_green / 6);
            avg_image[i][j].rgbtBlue = round(sum_blue / 6);
            
            image[i][j].rgbtRed =  avg_image[i][j].rgbtRed;
            image[i][j].rgbtGreen = avg_image[i][j].rgbtGreen;
            image[i][j].rgbtBlue = avg_image[i][j].rgbtBlue;
        }

        //bottom edge
        else if ((i == height - 1) && (j <= width - 2 && j >= 1))
        {
            for (int a = i - 1; a < 2; a ++)
            {
                for (int b = j - 1; b < j + 2; b ++)
                {
                    sum_red += copy_image[a][b].rgbtRed;
                    sum_green += copy_image[a][b].rgbtGreen;
                    sum_blue += copy_image[a][b].rgbtBlue;
                }
            }

            //avgs
            avg_image[i][j].rgbtRed = round(sum_red / 6);
            avg_image[i][j].rgbtGreen = round(sum_red / 6);
            avg_image[i][j].rgbtBlue = round(sum_red / 6);
            
            image[i][j].rgbtRed =  avg_image[i][j].rgbtRed;
            image[i][j].rgbtGreen = avg_image[i][j].rgbtGreen;
            image[i][j].rgbtBlue = avg_image[i][j].rgbtBlue;
        }
        
        //right edge
        else if ((i >= 1 && i <= height - 2) && (j == width - 1))
        {
            for (int a = i - 1; a < i + 2; a ++)
            {
                for (int b = j - 1; b < 2; b ++)
                {
                    sum_red +=  copy_image[a][b].rgbtRed;
                    sum_green +=  copy_image[a][b].rgbtGreen;
                    sum_blue +=  copy_image[a][b].rgbtBlue;
                }
            }

            //avgs
            avg_image[i][j].rgbtRed = round(sum_red / 6);
            avg_image[i][j].rgbtGreen = round(sum_green / 6);
            avg_image[i][j].rgbtBlue = round(sum_blue / 6);
            
            image[i][j].rgbtRed =  avg_image[i][j].rgbtRed;
            image[i][j].rgbtGreen = avg_image[i][j].rgbtGreen;
            image[i][j].rgbtBlue = avg_image[i][j].rgbtBlue;
        }
        
        image[i][j].rgbtRed =  avg_image[i][j].rgbtRed;
        image[i][j].rgbtGreen = avg_image[i][j].rgbtGreen;
        image[i][j].rgbtBlue = avg_image[i][j].rgbtBlue;
    }
}
    //}
//}
return;

}

You must log in to answer this question.

Browse other questions tagged .