0

I got some weird issue with my code! It compiles without any problems. All other filters work perfectly, and check50 shows no problems (no problem in edge according to check50). But, when i try edge filter on any of the sample images I get the following result.

~/pset4/filter/ $ ./filter -e images/tower.bmp out.bmp

UndefinedBehaviorSanitizer:DEADLYSIGNAL
==12056==ERROR: UndefinedBehaviorSanitizer: stack-overflow on address 0x7fff30f8be50 (pc 0x00000044afea bp 0x7fff317d4a10 sp 0x7fff30f8be50 T12056)
    #0 0x44afe9  (/home/ubuntu/pset4/filter/filter+0x44afe9)
    #1 0x42330e  (/home/ubuntu/pset4/filter/filter+0x42330e)
    #2 0x7f6d3cca7b96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #3 0x402e19  (/home/ubuntu/pset4/filter/filter+0x402e19)

==12056==ABORTING

and with that i get an unreadable output file.

following is my code for helper.c file.

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

//float duplicate of RGBTRIPLE
typedef struct
{
    float Red;
    float Green;
    float Blue;
}
Image;

/*
    Extra code cropped
*/

// Detect edges
/*
    This is where the problem lies.
    Code checks out with success on check50 but shows weird error message when i run it.
*/
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    //initialize 3 different arrays for later use
    Image edge[height][width];
    Image gx[height][width];
    Image gy[height][width];

    //loop to get values of gx and gy
    for (int a = 0; a < height; a++)
    {
        for (int b = 0; b < width; b++)
        {
            //for top row
            if (a == 0)
            {
                //top left corner
                if (b == 0)
                {
                    gx[a][b].Red = (((image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (2)))
                                 + ((image[a + 1][b].rgbtRed * (0)) + (image[a + 1][b + 1].rgbtRed * (1))));

                    gx[a][b].Green = (((image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (2)))
                                   + ((image[a + 1][b].rgbtGreen * (0)) + (image[a + 1][b + 1].rgbtGreen * (1))));

                    gx[a][b].Blue = (((image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (2)))
                                  + ((image[a + 1][b].rgbtBlue * (0)) + (image[a + 1][b + 1].rgbtBlue * (1))));

                    gy[a][b].Red = (((image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (0)))
                                 + ((image[a + 1][b].rgbtRed * (2)) + (image[a + 1][b + 1].rgbtRed * (1))));

                    gy[a][b].Green = (((image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (0)))
                                   + ((image[a + 1][b].rgbtGreen * (2)) + (image[a + 1][b + 1].rgbtGreen * (1))));

                    gy[a][b].Blue = (((image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (0)))
                                  + ((image[a + 1][b].rgbtBlue * (2)) + (image[a + 1][b + 1].rgbtBlue * (1))));
                }

                //top right corner
                else if (b == (width - 1))
                {
                    gx[a][b].Red = (((image[a][b - 1].rgbtRed * (-2)) + (image[a][b].rgbtRed * (0)))
                                 + ((image[a + 1][b - 1].rgbtRed * (-1)) + (image[a + 1][b].rgbtRed * (0))));

                    gx[a][b].Green = (((image[a][b - 1].rgbtGreen * (-2)) + (image[a][b].rgbtGreen * (0)))
                                   + ((image[a + 1][b - 1].rgbtGreen * (-1)) + (image[a + 1][b].rgbtGreen * (0))));

                    gx[a][b].Blue = (((image[a][b - 1].rgbtBlue * (-2)) + (image[a][b].rgbtBlue * (0)))
                                  + ((image[a + 1][b - 1].rgbtBlue * (-1)) + (image[a + 1][b].rgbtBlue * (0))));

                    gy[a][b].Red = (((image[a][b - 1].rgbtRed * (0)) + (image[a][b].rgbtRed * (0)))
                                 + ((image[a + 1][b - 1].rgbtRed * (1)) + (image[a + 1][b].rgbtRed * (2))));

                    gy[a][b].Green = (((image[a][b - 1].rgbtGreen * (0)) + (image[a][b].rgbtGreen * (0)))
                                   + ((image[a + 1][b - 1].rgbtGreen * (1)) + (image[a + 1][b].rgbtGreen * (2))));

                    gy[a][b].Blue = (((image[a][b - 1].rgbtBlue * (0)) + (image[a][b].rgbtBlue * (0)))
                                  + ((image[a + 1][b - 1].rgbtBlue * (1)) + (image[a + 1][b].rgbtBlue * (2))));
                }

                //rest of the top row
                else
                {
                    gx[a][b].Red = (((image[a][b - 1].rgbtRed * (-2)) + (image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (2)))
                                 + ((image[a + 1][b - 1].rgbtRed * (-1)) + (image[a + 1][b].rgbtRed * (0)) + (image[a + 1][b + 1].rgbtRed * (1))));

                    gx[a][b].Green = (((image[a][b - 1].rgbtGreen * (-2)) + (image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (2)))
                                   + ((image[a + 1][b - 1].rgbtGreen * (-1)) + (image[a + 1][b].rgbtGreen * (0)) + (image[a + 1][b + 1].rgbtGreen * (1))));

                    gx[a][b].Blue = (((image[a][b - 1].rgbtBlue * (-2)) + (image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (2)))
                                  + ((image[a + 1][b - 1].rgbtBlue * (-1)) + (image[a + 1][b].rgbtBlue * (0)) + (image[a + 1][b + 1].rgbtBlue * (1))));

                    gy[a][b].Red = (((image[a][b - 1].rgbtRed * (0)) + (image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (0)))
                                 + ((image[a + 1][b - 1].rgbtRed * (1)) + (image[a + 1][b].rgbtRed * (2)) + (image[a + 1][b + 1].rgbtRed * (1))));

                    gy[a][b].Green = (((image[a][b - 1].rgbtGreen * (0)) + (image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (0)))
                                   + ((image[a + 1][b - 1].rgbtGreen * (1)) + (image[a + 1][b].rgbtGreen * (2)) + (image[a + 1][b + 1].rgbtGreen * (1))));

                    gy[a][b].Blue = (((image[a][b - 1].rgbtBlue * (0)) + (image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (0)))
                                  + ((image[a + 1][b - 1].rgbtBlue * (1)) + (image[a + 1][b].rgbtBlue * (2)) + (image[a + 1][b + 1].rgbtBlue * (1))));
                }
            }

            //bottom layer
            else if (a == (height - 1))
            {
                //bottom left corner
                if (b == 0)
                {
                    gx[a][b].Red = (((image[a - 1][b].rgbtRed * (0)) + (image[a - 1][b + 1].rgbtRed * (1)))
                                 + ((image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (2))));

                    gx[a][b].Green = (((image[a - 1][b].rgbtGreen * (0)) + (image[a - 1][b + 1].rgbtGreen * (1)))
                                   + ((image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (2))));

                    gx[a][b].Blue = (((image[a - 1][b].rgbtBlue * (0)) + (image[a - 1][b + 1].rgbtBlue * (1)))
                                  + ((image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (2))));

                    gy[a][b].Red = (((image[a - 1][b].rgbtRed * (-2)) + (image[a - 1][b + 1].rgbtRed * (-1)))
                                 + ((image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (0))));

                    gy[a][b].Green = (((image[a - 1][b].rgbtGreen * (-2)) + (image[a - 1][b + 1].rgbtGreen * (-1)))
                                   + ((image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (0))));

                    gy[a][b].Blue = (((image[a - 1][b].rgbtBlue * (-2)) + (image[a - 1][b + 1].rgbtBlue * (-1)))
                                  + ((image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (0))));
                }

                //bottom right corner
                else if (b == (width - 1))
                {
                    gx[a][b].Red = (((image[a - 1][b - 1].rgbtRed * (-1)) + (image[a - 1][b].rgbtRed * (0)))
                                 + ((image[a][b - 1].rgbtRed * (-2)) + (image[a][b].rgbtRed * (0))));

                    gx[a][b].Green = (((image[a - 1][b - 1].rgbtGreen * (-1)) + (image[a - 1][b].rgbtGreen * (0)))
                                   + ((image[a][b - 1].rgbtGreen * (-2)) + (image[a][b].rgbtGreen * (0))));

                    gx[a][b].Blue = (((image[a - 1][b - 1].rgbtBlue * (-1)) + (image[a - 1][b].rgbtBlue * (0)))
                                  + ((image[a][b - 1].rgbtBlue * (-2)) + (image[a][b].rgbtBlue * (0))));

                    gy[a][b].Red = (((image[a - 1][b - 1].rgbtRed * (-1)) + (image[a - 1][b].rgbtRed * (-2)))
                                 + ((image[a][b - 1].rgbtRed * (0)) + (image[a][b].rgbtRed * (0))));

                    gy[a][b].Green = (((image[a - 1][b - 1].rgbtGreen * (-1)) + (image[a - 1][b].rgbtGreen * (-2)))
                                   + ((image[a][b - 1].rgbtGreen * (0)) + (image[a][b].rgbtGreen * (0))));

                    gy[a][b].Blue = (((image[a - 1][b - 1].rgbtBlue * (-1)) + (image[a - 1][b].rgbtBlue * (-2)))
                                  + ((image[a][b - 1].rgbtBlue * (0)) + (image[a][b].rgbtBlue * (0))));
                }

                //rest of the bottom row
                else
                {
                    gx[a][b].Red = (((image[a - 1][b - 1].rgbtRed * (-1)) + (image[a - 1][b].rgbtRed * (0)) + (image[a - 1][b + 1].rgbtRed * (1)))
                                 + ((image[a][b - 1].rgbtRed * (-2)) + (image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (2))));

                    gx[a][b].Green = (((image[a - 1][b - 1].rgbtGreen * (-1)) + (image[a - 1][b].rgbtGreen * (0)) + (image[a - 1][b + 1].rgbtGreen * (1)))
                                   + ((image[a][b - 1].rgbtGreen * (-2)) + (image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (2))));

                    gx[a][b].Blue = (((image[a - 1][b - 1].rgbtBlue * (-1)) + (image[a - 1][b].rgbtBlue * (0)) + (image[a - 1][b + 1].rgbtBlue * (1)))
                                  + ((image[a][b - 1].rgbtBlue * (-2)) + (image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (2))));

                    gy[a][b].Red = (((image[a - 1][b - 1].rgbtRed * (-1)) + (image[a - 1][b].rgbtRed * (-2)) + (image[a - 1][b + 1].rgbtRed * (-1)))
                                 + ((image[a][b - 1].rgbtRed * (0)) + (image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (0))));

                    gy[a][b].Green = (((image[a - 1][b - 1].rgbtGreen * (-1)) + (image[a - 1][b].rgbtGreen * (-2)) + (image[a - 1][b + 1].rgbtGreen * (-1)))
                                   + ((image[a][b - 1].rgbtGreen * (0)) + (image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (0))));

                    gy[a][b].Blue = (((image[a - 1][b - 1].rgbtBlue * (-1)) + (image[a - 1][b].rgbtBlue * (-2)) + (image[a - 1][b + 1].rgbtBlue * (-1)))
                                  + ((image[a][b - 1].rgbtBlue * (0)) + (image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (0))));
                }
            }

            //middle rows
            else
            {
                //left column (except the corners)
                if (b == 0)
                {
                    gx[a][b].Red = (((image[a - 1][b].rgbtRed * (0)) + (image[a - 1][b + 1].rgbtRed * (1)))
                                 + ((image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (2)))
                                 + ((image[a + 1][b].rgbtRed * (0)) + (image[a + 1][b + 1].rgbtRed * (1))));

                    gx[a][b].Green = (((image[a - 1][b].rgbtGreen * (0)) + (image[a - 1][b + 1].rgbtGreen * (1)))
                                   + ((image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (2)))
                                   + ((image[a + 1][b].rgbtGreen * (0)) + (image[a + 1][b + 1].rgbtGreen * (1))));

                    gx[a][b].Blue = (((image[a - 1][b].rgbtBlue * (0)) + (image[a - 1][b + 1].rgbtBlue * (1)))
                                  + ((image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (2)))
                                  + ((image[a + 1][b].rgbtBlue * (0)) + (image[a + 1][b + 1].rgbtBlue * (1))));

                    gy[a][b].Red = (((image[a - 1][b].rgbtRed * (-2)) + (image[a - 1][b + 1].rgbtRed * (-1)))
                                 + ((image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (0)))
                                 + ((image[a + 1][b].rgbtRed * (2)) + (image[a + 1][b + 1].rgbtRed * (1))));

                    gy[a][b].Green = (((image[a - 1][b].rgbtGreen * (-2)) + (image[a - 1][b + 1].rgbtGreen * (-1)))
                                   + ((image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (0)))
                                   + ((image[a + 1][b].rgbtGreen * (2)) + (image[a + 1][b + 1].rgbtGreen * (1))));

                    gy[a][b].Blue = (((image[a - 1][b].rgbtBlue * (-2)) + (image[a - 1][b + 1].rgbtBlue * (-1)))
                                  + ((image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (0)))
                                  + ((image[a + 1][b].rgbtBlue * (2)) + (image[a + 1][b + 1].rgbtBlue * (1))));
                }

                //rightmost column (except corners)
                else if (b == (width - 1))
                {
                    gx[a][b].Red = (((image[a - 1][b - 1].rgbtRed * (-1)) + (image[a - 1][b].rgbtRed * (0)))
                                 + ((image[a][b - 1].rgbtRed * (-2)) + (image[a][b].rgbtRed * (0)))
                                 + ((image[a + 1][b - 1].rgbtRed * (-1)) + (image[a + 1][b].rgbtRed * (0))));

                    gx[a][b].Green = (((image[a - 1][b - 1].rgbtGreen * (-1)) + (image[a - 1][b].rgbtGreen * (0)))
                                   + ((image[a][b - 1].rgbtGreen * (-2)) + (image[a][b].rgbtGreen * (0)))
                                   + ((image[a + 1][b - 1].rgbtGreen * (-1)) + (image[a + 1][b].rgbtGreen * (0))));

                    gx[a][b].Blue = (((image[a - 1][b - 1].rgbtBlue * (-1)) + (image[a - 1][b].rgbtBlue * (0)))
                                  + ((image[a][b - 1].rgbtBlue * (-2)) + (image[a][b].rgbtBlue * (0)))
                                  + ((image[a + 1][b - 1].rgbtBlue * (-1)) + (image[a + 1][b].rgbtBlue * (0))));

                    gy[a][b].Red = (((image[a - 1][b - 1].rgbtRed * (-1)) + (image[a - 1][b].rgbtRed * (-2)))
                                 + ((image[a][b - 1].rgbtRed * (0)) + (image[a][b].rgbtRed * (0)))
                                 + ((image[a + 1][b - 1].rgbtRed * (1)) + (image[a + 1][b].rgbtRed * (2))));

                    gy[a][b].Green = (((image[a - 1][b - 1].rgbtGreen * (-1)) + (image[a - 1][b].rgbtGreen * (-2)))
                                   + ((image[a][b - 1].rgbtGreen * (0)) + (image[a][b].rgbtGreen * (0)))
                                   + ((image[a + 1][b - 1].rgbtGreen * (1)) + (image[a + 1][b].rgbtGreen * (2))));

                    gy[a][b].Blue = (((image[a - 1][b - 1].rgbtBlue * (-1)) + (image[a - 1][b].rgbtBlue * (-2)))
                                  + ((image[a][b - 1].rgbtBlue * (0)) + (image[a][b].rgbtBlue * (0)))
                                  + ((image[a + 1][b - 1].rgbtBlue * (1)) + (image[a + 1][b].rgbtBlue * (2))));
                }

                //everything in middle
                else
                {
                    gx[a][b].Red = (((image[a - 1][b - 1].rgbtRed * (-1)) + (image[a - 1][b].rgbtRed * (0)) + (image[a - 1][b + 1].rgbtRed * (1)))
                                 + ((image[a][b - 1].rgbtRed * (-2)) + (image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (2)))
                                 + ((image[a + 1][b - 1].rgbtRed * (-1)) + (image[a + 1][b].rgbtRed * (0)) + (image[a + 1][b + 1].rgbtRed * (1))));

                    gx[a][b].Green = (((image[a - 1][b - 1].rgbtGreen * (-1)) + (image[a - 1][b].rgbtGreen * (0)) + (image[a - 1][b + 1].rgbtGreen * (1)))
                                   + ((image[a][b - 1].rgbtGreen * (-2)) + (image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (2)))
                                   + ((image[a + 1][b - 1].rgbtGreen * (-1)) + (image[a + 1][b].rgbtGreen * (0)) + (image[a + 1][b + 1].rgbtGreen * (1))));

                    gx[a][b].Blue = (((image[a - 1][b - 1].rgbtBlue * (-1)) + (image[a - 1][b].rgbtBlue * (0)) + (image[a - 1][b + 1].rgbtBlue * (1)))
                                  + ((image[a][b - 1].rgbtBlue * (-2)) + (image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (2)))
                                  + ((image[a + 1][b - 1].rgbtBlue * (-1)) + (image[a + 1][b].rgbtBlue * (0)) + (image[a + 1][b + 1].rgbtBlue * (1))));

                    gy[a][b].Red = (((image[a - 1][b - 1].rgbtRed * (-1)) + (image[a - 1][b].rgbtRed * (-2)) + (image[a - 1][b + 1].rgbtRed * (-1)))
                                 + ((image[a][b - 1].rgbtRed * (0)) + (image[a][b].rgbtRed * (0)) + (image[a][b + 1].rgbtRed * (0)))
                                 + ((image[a + 1][b - 1].rgbtRed * (1)) + (image[a + 1][b].rgbtRed * (2)) + (image[a + 1][b + 1].rgbtRed * (1))));

                    gy[a][b].Green = (((image[a - 1][b - 1].rgbtGreen * (-1)) + (image[a - 1][b].rgbtGreen * (-2)) + (image[a - 1][b + 1].rgbtGreen * (-1)))
                                   + ((image[a][b - 1].rgbtGreen * (0)) + (image[a][b].rgbtGreen * (0)) + (image[a][b + 1].rgbtGreen * (0)))
                                   + ((image[a + 1][b - 1].rgbtGreen * (1)) + (image[a + 1][b].rgbtGreen * (2)) + (image[a + 1][b + 1].rgbtGreen * (1))));

                    gy[a][b].Blue = (((image[a - 1][b - 1].rgbtBlue * (-1)) + (image[a - 1][b].rgbtBlue * (-2)) + (image[a - 1][b + 1].rgbtBlue * (-1)))
                                  + ((image[a][b - 1].rgbtBlue * (0)) + (image[a][b].rgbtBlue * (0)) + (image[a][b + 1].rgbtBlue * (0)))
                                  + ((image[a + 1][b - 1].rgbtBlue * (1)) + (image[a + 1][b].rgbtBlue * (2)) + (image[a + 1][b + 1].rgbtBlue * (1))));
                }
            }
        }
    }

    //loop to get values of edge
    for (int c = 0; c < height; c++)
    {
        for (int d = 0; d < width; d++)
        {
            edge[c][d].Red = round(sqrtf ((gx[c][d].Red * gx[c][d].Red) + (gy[c][d].Red * gy[c][d].Red)));
            edge[c][d].Green = round(sqrtf ((gx[c][d].Green * gx[c][d].Green) + (gy[c][d].Green * gy[c][d].Green)));
            edge[c][d].Blue = round(sqrtf ((gx[c][d].Blue * gx[c][d].Blue) + (gy[c][d].Blue * gy[c][d].Blue)));

            //capping values at 255
            if (edge[c][d].Red > 255)
                edge[c][d].Red = 255;

            if (edge[c][d].Green > 255)
                edge[c][d].Green = 255;

            if (edge[c][d].Blue > 255)
                edge[c][d].Blue = 255;
        }
    }

    //loop to copy new values
    for (int e = 0; e < height; e++)
    {
        for (int f = 0; f < width; f++)
        {
            image[e][f].rgbtRed = edge[e][f].Red;
            image[e][f].rgbtGreen = edge[e][f].Green;
            image[e][f].rgbtBlue = edge[e][f].Blue;
        }
    }
    return;
}```
it might be a bit overcomplicated.
  • Could be the same problem as here?: cs50.stackexchange.com/a/37680/27433 – Tritum Jun 22 at 6:07
  • Problem solved! I deleted all of my old (messy and overcomplicated code!) Wrote completely new code (keeping in mind not to use too much memory!) It works perfectly now! – Aftab Ansari Jun 23 at 18:48
-1

It was just a memory problem i guess. (i re-wrote all of it) it looks like this now;

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

//float duplicate of RGBTRIPLE
typedef struct
{
    float Red;
    float Green;
    float Blue;
}
Image;

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    //to make sure average is a float and then rounded as an integer
    float b = 3.0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //average of rgb for black and white values
            int a = round((image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / b);

            //assigning black and white to RGB
            image[i][j].rgbtRed = a;
            image[i][j].rgbtGreen = a;
            image[i][j].rgbtBlue = a;
        }
    }
    return;
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //applying sepia formula
            float red = round((0.393 * image[i][j].rgbtRed) + (0.769 * image[i][j].rgbtGreen) + (0.189 * image[i][j].rgbtBlue));
            float green = round((0.349 * image[i][j].rgbtRed) + (0.686 * image[i][j].rgbtGreen) + (0.168 * image[i][j].rgbtBlue));
            float blue = round((0.272 * image[i][j].rgbtRed) + (0.534 * image[i][j].rgbtGreen) + (0.131 * image[i][j].rgbtBlue));
            //maxing out values at 255
            if (red > 255)
            {
                red = 255;
            }
            if (green > 255)
            {
                green = 255;
            }
            if (blue > 255)
            {
                blue = 255;
            }
            //assigning the new values to image pixel
            image[i][j].rgbtRed = red;
            image[i][j].rgbtGreen = green;
            image[i][j].rgbtBlue = blue;
        }
    }
    return;
}

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    //initialise a temporary empty copy of image
    RGBTRIPLE inverse[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //copying image
            inverse[i][j].rgbtRed = image[i][j].rgbtRed;
            inverse[i][j].rgbtGreen = image[i][j].rgbtGreen;
            inverse[i][j].rgbtBlue = image[i][j].rgbtBlue;
        }
    }
    for (int a = 0; a < height; a++)
    {
        for (int b = 0; b < width; b++)
        {
            //assigning reflected pixels in image
            image[a][b].rgbtRed = inverse[a][((width - 1) - b)].rgbtRed;
            image[a][b].rgbtGreen = inverse[a][((width - 1) - b)].rgbtGreen;
            image[a][b].rgbtBlue = inverse[a][((width - 1) - b)].rgbtBlue;
        }
    }
    return;
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //initializing a temporary image array
    Image temp[height][width];

    //getting all blur rgb values on temp array
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //initialize floats for calculation (float to make sure round works properly)
            float red = 0;
            float green = 0;
            float blue = 0;
            
            //initialize an int (to divide the float)
            int a = 0;
            //adding rgb values of surrounding pixels
            for (int k = -1; k <= 1; k++)
            {
                for (int l = -1; l <= 1; l++)
                {
                    //making sure pixels aren't beyond image boundaries
                    if ((i + k) >= 0 && (i + k) < height)
                    {
                        if ((j + l) >= 0 && (j + l) < width)
                        {
                            red += image[i + k][j + l].rgbtRed;
                            green += image[i + k][j + l].rgbtGreen;
                            blue += image[i + k][j + l].rgbtBlue;
                            a++;
                        }
                    }
                }
            }
            //taking average and saving the average values in temp array
            temp[i][j].Red = round(red / a);
            temp[i][j].Green = round(green / a);
            temp[i][j].Blue = round(blue / a);
            
            //capping rgb values at 255
            if (temp[i][j].Red > 255)
            {
                temp[i][j].Red = 255;
            }
            if (temp[i][j].Green > 255)
            {
                temp[i][j].Green = 255;
            }
            if (temp[i][j].Blue > 255)
            {
                temp[i][j].Blue = 255;
            }
        }
    }

    //copying values from temp array to original image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = temp[i][j].Red;
            image[i][j].rgbtGreen = temp[i][j].Green;
            image[i][j].rgbtBlue = temp[i][j].Blue;
        }
    }
    return;
}

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    //values to multiply
    int gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
    //initialize new image to hold all values before copying
    Image edge[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //initializing floats for further calculation (to make sure round works)
            float Redx = 0;
            float Greenx = 0;
            float Bluex = 0;
            float Redy = 0;
            float Greeny = 0;
            float Bluey = 0;

            //using an int to tell which value from gx to use
            int a = 0;
            //loop to get all surrounding pixel values
            for (int k = (-1); k <= 1; k++)
            {
                //another int to tell 2nd dimension of gx array
                int b = 0;
                for (int l = (-1); l <= 1; l++)
                {
                    //making sure no values are beyond image boundaries
                    if ((i + k) >= 0 && (j + l) >= 0 && (i + k) < height && (j + l) < width)
                    {
                        //getting values in floats
                        Redx += image[i + k][j + l].rgbtRed * gx[a][b];
                        Greenx += image[i + k][j + l].rgbtGreen * gx[a][b];
                        Bluex += image[i + k][j + l].rgbtBlue * gx[a][b];

                        Redy += image[i + k][j + l].rgbtRed * gx[b][a];
                        Greeny += image[i + k][j + l].rgbtGreen * gx[b][a];
                        Bluey += image[i + k][j + l].rgbtBlue * gx[b][a];
                    }
                    b++;
                }
                a++;
            }
            //saving all values in temporary image array
            edge[i][j].Red = round(sqrt((Redx * Redx) + (Redy * Redy)));
            edge[i][j].Green = round(sqrt((Greenx * Greenx) + (Greeny * Greeny)));
            edge[i][j].Blue = round(sqrt((Bluex * Bluex) + (Bluey * Bluey)));
            //capping all values at 255
            if (edge[i][j].Red > 255)
            {
                edge[i][j].Red = 255;
            }
            if (edge[i][j].Green > 255)
            {
                edge[i][j].Green = 255;
            }
            if (edge[i][j].Blue > 255)
            {
                edge[i][j].Blue = 255;
            }
        }
    }

    //copying values to actual image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = edge[i][j].Red;
            image[i][j].rgbtGreen = edge[i][j].Green;
            image[i][j].rgbtBlue = edge[i][j].Blue;
        }
    }
    return;
}
| improve this answer | |

You must log in to answer this question.

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