0
void edges(int height, int width, RGBTRIPLE image[height][width])
{
//Set temp image
RGBTRIPLE *edgeimage = malloc((height + 1) * (width + 1) + 4);

//RGBTRIPLE 
edgeimage[height][width];

//Go through all pixels and copy to edgeimage
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        edgeimage[i][j] = image[i][j];
    }
}

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        int Gx[3][3] = { {-1, 0, 1},
                         {-2, 0, 2},
                         {-1, 0, 1} };
                         
        int Gy[3][3] = { {-1, -2, -1},
                         {0 , 0 , 0 },
                         {1 , 2 , 1 } };
        
        int GxRed = 0, GxGreen = 0, GxBlue = 0, GyRed = 0, GyGreen = 0, GyBlue = 0;
        
        for (int k = -1; k < 2; k++)
        {
            if (i + k < 0 || (i + k > height - 1))
            {
                edgeimage[i + k][j].rgbtRed = 0;
                edgeimage[i + k][j].rgbtGreen = 0;
                edgeimage[i + k][j].rgbtBlue = 0;
            }
            for (int h = -1; h < 2; h++)
            {
                if (j + h < 0 || (j + h > width - 1))
                {
                    //continue;
                    edgeimage[i][j + h].rgbtRed = 0;
                    edgeimage[i][j + h].rgbtGreen = 0;
                    edgeimage[i][j + h].rgbtBlue = 0;
                }
                for (int a = 0; a < 3; a++)
                {
                    for (int b = 0; b < 3; b++)
                    {
                        GxRed += edgeimage[i + k][j + h].rgbtRed * Gx[a][b];
                        GxGreen += edgeimage[i + k][j + h].rgbtGreen * Gx[a][b];
                        GxBlue += edgeimage[i + k][j + h].rgbtBlue * Gx[a][b];
                        
                        GyRed += edgeimage[i + k][j + h].rgbtRed * Gy[a][b];
                        GyGreen += edgeimage[i + k][j + h].rgbtGreen * Gy[a][b];
                        GyBlue += edgeimage[i + k][j + h].rgbtBlue * Gy[a][b];
                    }
                }
            }
        }

        float red = sqrt(pow(GxRed,2) + pow(GyRed,2));
        float green = sqrt(pow(GxGreen,2) + pow(GyGreen,2));
        float blue = sqrt(pow(GxBlue,2) + pow(GyBlue,2));

        if (red > 255)
            red = 255;

        if (green > 255)
            green = 255;

        if (blue > 255)
            blue = 255;

        image[i][j].rgbtRed = round(red);
        image[i][j].rgbtGreen = round(green);
        image[i][j].rgbtBlue = round(blue);
    }
}

return;

}

1

This happens because the code is attempting to access an array element outside the bounds of the array. Remember that when an array is allocated a size of, say 5, the elements will run from 0 to 4. Height and width are array SIZES, so they each exceed the maximum allowable values by 1.

As for the for loop that the code is using to initialize the array, I don't understand why you're using image[height][width]. I would think that you'd initialize the array by copying the original array (using image[i][j]) or by setting each element to 0 values for each RGBTRIPLE element.

There may be other issues, but this answers the question asked.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • yeah i noticed that and changed to image[i][j]. But it didn't help! – Javohir Abdullayev Aug 4 '20 at 8:01
  • Just forgot to edit the question – Javohir Abdullayev Aug 4 '20 at 8:03

You must log in to answer this question.

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