0

This is my code for the blur filter in the pset 4. First I used the average for the corner pixels, then I used a loop for the average of the first and last row and columns and then a nested loop for the rest of the cells. It compiles ok, but when I use it, it causes a segmentation fault, could anyone help me please?

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width]; //array for storing a temporary copy

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            temp[i][j] = image[i][j]; //copying the data in the temporary filter
        }
    }

    //Left top corner
    image[0][0].rgbtBlue = (int) round((temp[0][0].rgbtBlue + temp[0][1].rgbtBlue + temp[1][0].rgbtBlue + temp[1][1].rgbtBlue) / 4);
    image[0][0].rgbtGreen = (int) round((temp[0][0].rgbtGreen + temp[0][1].rgbtGreen + temp[1][0].rgbtGreen + temp[1][1].rgbtGreen) / 4);
    image[0][0].rgbtRed = (int) round((temp[0][0].rgbtRed + temp[0][1].rgbtRed + temp[1][0].rgbtRed + temp[1][1].rgbtRed) / 4);

    //Right top corner
    image[0][width].rgbtBlue = (int) round((temp[0][width].rgbtBlue + temp[0][width - 1].rgbtBlue + temp[1][width].rgbtBlue + temp[1][width - 1].rgbtBlue) / 4);
    image[0][width].rgbtGreen = (int) round((temp[0][width].rgbtGreen + temp[0][width - 1].rgbtGreen + temp[1][width].rgbtGreen + temp[1][width - 1].rgbtGreen) / 4);
    image[0][width].rgbtRed = (int) round((temp[0][width].rgbtRed + temp[0][width - 1].rgbtRed + temp[1][width].rgbtRed + temp[1][width - 1].rgbtRed) / 4);

    //Left bottom corner
    image[height][0].rgbtBlue = (int) round((temp[height][0].rgbtBlue + temp[height][1].rgbtBlue + temp[height - 1][0].rgbtBlue + temp[height - 1][1].rgbtBlue) / 4);
    image[height][0].rgbtGreen = (int) round((temp[height][0].rgbtGreen + temp[height][1].rgbtGreen + temp[height - 1][0].rgbtGreen + temp[height - 1][1].rgbtGreen) / 4);
    image[height][0].rgbtRed = (int) round((temp[height][0].rgbtRed + temp[height][1].rgbtRed + temp[height - 1][0].rgbtRed + temp[height - 1][1].rgbtRed) / 4);

    //Right bottom corner
    image[height][width].rgbtBlue = (int) round((temp[height][width].rgbtBlue + temp[height][width - 1].rgbtBlue + temp[height - 1][width].rgbtBlue + temp[height - 1][width - 1].rgbtBlue) / 4);
    image[height][width].rgbtGreen = (int) round((temp[height][width].rgbtGreen + temp[height][width - 1].rgbtGreen + temp[height - 1][width].rgbtGreen + temp[height - 1][width - 1].rgbtGreen) / 4);
    image[height][width].rgbtRed = (int) round((temp[height][width].rgbtRed + temp[height][width - 1].rgbtRed + temp[height - 1][width].rgbtRed + temp[height - 1][width - 1].rgbtRed) / 4);

    //First row
    for (int i = 1; i < width - 1; i++)
    {
        image[0][i].rgbtBlue = (int) round((temp[0][i - 1].rgbtBlue + temp[0][i].rgbtBlue + temp[0][i + 1].rgbtBlue + temp[1][i - 1].rgbtBlue + temp[1][i].rgbtBlue + temp[1][i + 1].rgbtBlue) / 6);
        image[0][i].rgbtGreen = (int) round((temp[0][i - 1].rgbtGreen + temp[0][i].rgbtGreen + temp[0][i + 1].rgbtGreen + temp[1][i - 1].rgbtGreen + temp[1][i].rgbtGreen + temp[1][i + 1].rgbtGreen) / 6);
        image[0][i].rgbtRed = (int) round((temp[0][i - 1].rgbtRed + temp[0][i].rgbtRed + temp[0][i + 1].rgbtRed + temp[1][i - 1].rgbtRed + temp[1][i].rgbtRed + temp[1][i + 1].rgbtRed) / 6);
    }

    //Last row
    for (int i = 1; i < width - 1; i++)
    {
        image[width][i].rgbtBlue = (int) round((temp[width][i - 1].rgbtBlue + temp[width][i].rgbtBlue + temp[width][i + 1].rgbtBlue + temp[width - 1][i - 1].rgbtBlue + temp[width - 1][i].rgbtBlue + temp[width - 1][i + 1].rgbtBlue) / 6);
        image[width][i].rgbtGreen = (int) round((temp[width][i - 1].rgbtGreen + temp[width][i].rgbtGreen + temp[width][i + 1].rgbtGreen + temp[width - 1][i - 1].rgbtGreen + temp[width -1][i].rgbtGreen + temp[width - 1][i + 1].rgbtGreen) / 6);
        image[width][i].rgbtRed = (int) round((temp[width][i - 1].rgbtRed + temp[width][i].rgbtRed + temp[width][i + 1].rgbtRed + temp[width - 1][i - 1].rgbtRed + temp[width - 1][i].rgbtRed + temp[width - 1][i + 1].rgbtRed) / 6);
    }

    //First column
    for (int i = 1; i < height - 1; i ++)
    {
        image[i][0].rgbtBlue = (int) round((temp[i - 1][0].rgbtBlue + temp[i][0].rgbtBlue + temp[i + 1][0].rgbtBlue + temp[i - 1][1].rgbtBlue + temp[i][1].rgbtBlue + temp[i + 1][1].rgbtBlue) / 6);
        image[i][0].rgbtGreen = (int) round((temp[i - 1][0].rgbtGreen + temp[i][0].rgbtGreen + temp[i + 1][0].rgbtGreen + temp[i - 1][1].rgbtGreen + temp[i][1].rgbtGreen + temp[i + 1][1].rgbtGreen) / 6);
        image[i][0].rgbtRed = (int) round((temp[i - 1][0].rgbtRed + temp[i][0].rgbtRed + temp[i + 1][0].rgbtRed + temp[i - 1][1].rgbtRed + temp[i][1].rgbtRed + temp[i + 1][1].rgbtRed) / 6);
    }

    //Last column
    for (int i = 1; i < height - 1; i ++)
    {
        image[i][width].rgbtBlue = (int) round((temp[i - 1][width].rgbtBlue + temp[i][width].rgbtBlue + temp[i + 1][width].rgbtBlue + temp[i - 1][width - 1].rgbtBlue + temp[i][width - 1].rgbtBlue + temp[i + 1][width - 1].rgbtBlue) / 6);
        image[i][width].rgbtGreen = (int) round((temp[i - 1][width].rgbtGreen + temp[i][width].rgbtGreen + temp[i + 1][width].rgbtGreen + temp[i - 1][width - 1].rgbtGreen + temp[i][width - 1].rgbtGreen + temp[i + 1][width - 1].rgbtGreen) / 6);
        image[i][width].rgbtRed = (int) round((temp[i - 1][width].rgbtRed + temp[i][width].rgbtRed + temp[i + 1][width].rgbtRed + temp[i - 1][width - 1].rgbtRed + temp[i][width - 1].rgbtRed + temp[i + 1][width - 1].rgbtRed) / 6);
    }

    //Rest of cells
    for (int i = 1; i < height - 1; i++)
    {
        for (int j = 1; j < width - 1; j++)
        {
            image[i][j].rgbtBlue = (int)(round)((temp[i - 1][j - 1].rgbtBlue + temp[i - 1][j].rgbtBlue + temp[i - 1][j + 1].rgbtBlue + temp[i][j - 1].rgbtBlue + temp[i][j].rgbtBlue + temp[i][j + 1].rgbtBlue + temp[i + 1][j - 1].rgbtBlue + temp[i + 1][j].rgbtBlue + temp[i + 1][j + 1].rgbtBlue) / 9);
            image[i][j].rgbtGreen = (int)(round)((temp[i - 1][j - 1].rgbtGreen + temp[i - 1][j].rgbtGreen + temp[i - 1][j + 1].rgbtGreen + temp[i][j - 1].rgbtGreen + temp[i][j].rgbtGreen + temp[i][j + 1].rgbtGreen + temp[i + 1][j - 1].rgbtGreen + temp[i + 1][j].rgbtGreen + temp[i + 1][j + 1].rgbtGreen) / 9);
            image[i][j].rgbtRed = (int)(round)((temp[i - 1][j - 1].rgbtRed + temp[i - 1][j].rgbtRed + temp[i - 1][j + 1].rgbtRed + temp[i][j - 1].rgbtRed + temp[i][j].rgbtRed + temp[i][j + 1].rgbtRed + temp[i + 1][j - 1].rgbtRed + temp[i + 1][j].rgbtRed + temp[i + 1][j + 1].rgbtRed) / 9);
        }
    }



    return;
}

1 Answer 1

0

I got it, for the last row I made a loop over the width instead of the height.

You must log in to answer this question.

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