0

So here is my code. Not the best one, but I wrote it without any help and it work. At least i think so when i look at the output images. But check50 says it is wrong. My only thought is that i decided not to give value 0 to part which are behind the borders but just skip them, cause i found it easier. Am I right? P.s. all remarks about the code are welcome. I really want to improve

RGBTRIPLE copy[height][width]; 
typedef struct
{
    int rgbtred;
    int rgbtgreen;
    int rgbtblue;
}
rgbtriple;

rgbtriple Gx[height][width];
rgbtriple Gy[height][width];
for (int i = 0; i<height; i++)
{
    for (int j =0; j<width; j++)
    {
        copy[i][j]=image[i][j];
    }
}
for (int i = 0; i< height; i++)
{
    for (int j =0; j<width; j++)
    {
                        int k=-2;

        for (int i2=i-1; i2<=i+1; i2++)
        {
             int n=-1;
            if (i2<0 || i2>=height)
            {
                k=k+2;
                continue;
            }
            for (int j2=j-1; j2<=j+1; j2++)
            {
                if (j2<0 || j2>=width)
                {
                    continue;
                }
                if (i2==i) 
                {
                    
                        Gx[i][j].rgbtred=Gx[i][j].rgbtred+copy[i2][j2].rgbtRed*k;
                        Gx[i][j].rgbtgreen=Gx[i][j].rgbtgreen+copy[i2][j2].rgbtGreen*k;
                        Gx[i][j].rgbtblue=Gx[i][j].rgbtblue+copy[i2][j2].rgbtBlue*k;
                    
                        Gy[i][j].rgbtred=Gy[i][j].rgbtred+copy[i2][j2].rgbtRed*0;
                        Gy[i][j].rgbtgreen=Gy[i][j].rgbtgreen+copy[i2][j2].rgbtGreen*0;
                        Gy[i][j].rgbtblue=Gy[i][j].rgbtblue+copy[i2][j2].rgbtBlue*0;
                        k=k+2;
                }
                if (i2==i-1)
                {
                    if (j2==j)
                    {
                        Gy[i][j].rgbtred=Gy[i][j].rgbtred+copy[i2][j2].rgbtRed*-2;
                        Gy[i][j].rgbtgreen=Gy[i][j].rgbtgreen+copy[i2][j2].rgbtGreen*-2;
                        Gy[i][j].rgbtblue=Gy[i][j].rgbtblue+copy[i2][j2].rgbtBlue*-2;
                    }
                    Gy[i][j].rgbtred=Gy[i][j].rgbtred+copy[i2][j2].rgbtRed*-1;
                    Gy[i][j].rgbtgreen=Gy[i][j].rgbtgreen+copy[i2][j2].rgbtGreen*-1;
                    Gy[i][j].rgbtblue=Gy[i][j].rgbtblue+copy[i2][j2].rgbtBlue*-1;
                }
                if (i2==i+1)
                {
                    if (j2==j)
                    {
                        Gy[i][j].rgbtred=Gy[i][j].rgbtred+copy[i2][j2].rgbtRed*2;
                        Gy[i][j].rgbtgreen=Gy[i][j].rgbtgreen+copy[i2][j2].rgbtGreen*2;
                        Gy[i][j].rgbtblue=Gy[i][j].rgbtblue+copy[i2][j2].rgbtBlue*2;
                    }
                    Gy[i][j].rgbtred=Gy[i][j].rgbtred+copy[i2][j2].rgbtRed*1;
                    Gy[i][j].rgbtgreen=Gy[i][j].rgbtgreen+copy[i2][j2].rgbtGreen*1;
                    Gy[i][j].rgbtblue=Gy[i][j].rgbtblue+copy[i2][j2].rgbtBlue*1;
                }
               if (i2!=i)
               {
                    Gx[i][j].rgbtred=Gx[i][j].rgbtred+copy[i2][j2].rgbtRed*n;
                    Gx[i][j].rgbtgreen=Gx[i][j].rgbtgreen+copy[i2][j2].rgbtGreen*n;
                    Gx[i][j].rgbtblue=Gx[i][j].rgbtblue+copy[i2][j2].rgbtBlue*n;
                
                    n++;
               }
            }
        }
        int r= round(sqrt(pow(Gx[i][j].rgbtred,2)+(pow(Gy[i][j].rgbtred,2))));
        int g= round(sqrt(pow(Gx[i][j].rgbtgreen, 2)+(pow(Gy[i][j].rgbtgreen,2))));
        int b = round(sqrt(pow(Gx[i][j].rgbtblue,2)+(pow(Gy[i][j].rgbtblue,2))));
        if (r>255)
        {
            image[i][j].rgbtRed=255;
        }
        else 
        {
            image[i][j].rgbtRed= r;

        }
        if (g>255)
        {
          image[i][j].rgbtGreen=255;  
        }
        else
        {
            image[i][j].rgbtGreen= g;

        }
        if (b>255)
        {
            image[i][j].rgbtBlue=255;
        }
        else
        {
        image[i][j].rgbtBlue= b;
        }
    }
}
return;

}

0

You must log in to answer this question.

Browse other questions tagged .