0

So I finished my edges function and it's failing everything for check50 and I can't understand what I did wrong. Any help would be greatly appreciated. Functions related to edges:

// Sets new RGB values for a pixel
void set_pixel(RGBTRIPLE Gx, RGBTRIPLE Gy, RGBTRIPLE *pixel)
{
    int red, green, blue;
    red = (int) round(sqrt((Gx.rgbtRed * Gx.rgbtRed) + (Gy.rgbtRed * Gy.rgbtRed)));
    green = (int) round(sqrt((Gx.rgbtGreen * Gx.rgbtGreen) + (Gy.rgbtGreen * Gy.rgbtGreen)));
    blue = (int) round(sqrt((Gx.rgbtBlue * Gx.rgbtBlue) + (Gy.rgbtBlue * Gy.rgbtBlue)));
    if (red > 255)
    {
        red = 255;
    }
    if (green > 255)
    {
        green = 255;
    }
    if (blue > 255)
    {
        blue = 255;
    }
    pixel->rgbtRed = red;
    pixel->rgbtGreen = green;
    pixel->rgbtBlue = blue;
}

// Adds the products for each colour together
void sum(RGBTRIPLE *sum, RGBTRIPLE g[3][3])
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            sum->rgbtRed += g[i][j].rgbtRed;
            sum->rgbtGreen += g[i][j].rgbtGreen;
            sum->rgbtBlue += g[i][j].rgbtBlue;
        }
    }
}

// Copies 3 x 3 grid into XX or YY
void duplicate(RGBTRIPLE grid[3][3], RGBTRIPLE c[3][3])
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            c[i][j] = grid[i][j];
        }
    }
}
// Resets 3 x 3 grid all to 0
void reset(RGBTRIPLE grid[3][3])
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            grid[i][j].rgbtRed = grid[i][j].rgbtGreen = grid[i][j].rgbtBlue = 0;
        }
    }
}

// Multiplies the amount of each colour by a given factor
void multiply(RGBTRIPLE *x, int factor)
{
    x->rgbtRed *= factor;
    x->rgbtGreen *= factor;
    x->rgbtBlue *= factor;
}

Edges function itself:

void edges(int height, int width, RGBTRIPLE image[height][width])
{
   // Copy of image so that we calculate based on the original values of RGB
   RGBTRIPLE copy[height][width];
   for (int i = 0; i < height; ++i)
   {
       for (int j = 0; j < width; ++j)
       {
           copy[i][j] = image[i][j];
       }
   }
   // Stores RGB values of each pixel in grid
   RGBTRIPLE grid[3][3];
   // Iterate over every pixel and alter its RGB values
   for (int i = 0; i < height; ++i)
   {
       for (int j = 0; j < width; ++j)
       {
           reset(grid);
           // Includes current pixel in center of 3 x 3 grid
           grid[1][1] = copy[i][j];
           // Checks if left edge exists and if so, assigns to left edge in 3 x 3 grid
           if (j != 0)
           {
               grid[1][0] = copy[i][j - 1];
           }
           // Checks if right edge exists
           if (j + 1 != width)
           {
               grid[1][2] = copy[i][j + 1];
           }
           // Checks if top edge exists
           if (i != 0)
           {
               grid[0][1] = copy[i - 1][j];
           }
           // Checks if bottom edge exists
           if (i + 1 != height)
           {
               grid[2][1] = copy[i + 1][j];
           }
           // Checks if top left corner exists
           if (i != 0 && j != 0)
           {
               grid[0][0] = copy[i - 1][j - 1];
           }
           // Checks if top right corner exists
           if (i != 0 && j + 1 != width)
           {
               grid[0][2] = copy[i - 1][j + 1];
           }
           // Checks if bottom left corner exists
           if (i + 1 != height && j != 0)
           {
               grid[2][0] = copy[i + 1][j - 1];
           }
           // Checks if bottom right corner exists
           if (i + 1 != height && j + 1 != width)
           {
               grid[2][2] = copy[i + 1][j + 1];
           }
           // Store products of x version and y version
           RGBTRIPLE XX[3][3], YY[3][3];
           duplicate(grid, XX);
           duplicate(grid, YY);
           // Multiplies each pixel in grid by corresponding values for x version
           multiply(&XX[0][0], -1);
           multiply(&XX[2][0], -1);
           multiply(&XX[1][0], -2);
           multiply(&XX[1][2], 2);
           multiply(&XX[0][1], 0);
           multiply(&XX[1][1], 0);
           multiply(&XX[2][1], 0);
           // Multiplies each pixel in grid by corresponding values for y version
           multiply(&YY[0][0], -1);
           multiply(&YY[0][2], -1);
           multiply(&YY[0][1], -2);
           multiply(&YY[2][1], 2);
           multiply(&YY[1][0], 0);
           multiply(&YY[1][1], 0);
           multiply(&YY[1][2], 0);
           // Stores sum of the products for x and y version
           RGBTRIPLE Gx, Gy;
           Gx.rgbtRed = Gx.rgbtGreen = Gx.rgbtBlue = Gy.rgbtRed = Gy.rgbtGreen = Gy.rgbtBlue = 0;
           sum(&Gx, XX);
           sum(&Gy, YY);
           set_pixel(Gx, Gy, &image[i][j]);
       }
   }
}
0

Might you want to include the following pixels when using your multiply function?

XX[0][2]
XX[2][2]
XY[2][0]
XY[2][2]
0

So, turns out I just ended up overflowing unsigned char in my sum and multiply functions. Unsigned char goes to a maximum of 255 so adding all the RGB values together would end up being greater than 255. To prevent overflowing, I declared a seperate struct that used ints instead of unsigned chars so I could go over 255.

You must log in to answer this question.

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