0

Check50 link & code below All compiles but just can't get check50 right, I feel like there must be a common mistake between all of the functions as they all seem to get the first 3 lines of pixels right but then it goes wrong? Thanks in advance !!

https://submit.cs50.io/check50/d4c52618748f5455d0ec83f1288cb72ca8b657de

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

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float grey = 0;
            grey = round((image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue)/3.000);
            image[i][j].rgbtRed = grey;
            image[i][j].rgbtGreen = grey;
            image[i][j].rgbtBlue = grey;
      }
    return;
   }
}


// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width/2; j++)
        {
            RGBTRIPLE temp[width][height];
            temp[i][j] = image[i][j];
            image[i][j] = image[1 - i][j];
            image[1 - i][j] = temp[i][j];
        }
    }
    return;
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int i, j;
    RGBTRIPLE blurred[height][width];
    int counter;
    for (i = 0; i < width; i++)
    {
        for (j = 0; j < height; j++)
        {
            int m;
            int n;
            counter = 0;
            for (m = -1; m < 2; m++)
            {
                for (n = -1; n < 2; n++)
                {
                    if ((i + 1) >= 0 && (i + m) < width && (j + 1) >= 0 && (j + n) < height)
                    {
                        counter++;
                    }
                }
            }
            blurred[i][j].rgbtRed = round((image[i+m][j+n].rgbtRed)/counter);
            blurred[i][j].rgbtGreen = round((image[i +m][j+n].rgbtGreen)/counter);
            blurred[i][j].rgbtBlue = round((image[i+m][j+n].rgbtBlue)/counter);
        }
    }
    image[i][j] = blurred[i][j];
    return;
}

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE edges[height][width];
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            edges[i][j] = image[i][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 i;
    int j;
    for (i = 0; i < width; i++)
    {
        for (j = 0; j < height; j++)
        {
            float redx = 0, redy =0, greenx = 0, greeny = 0, bluex = 0, bluey = 0;
            for (int m = 0; m < 3; m++)
            {
                for (int n = 0; n < 3; n++)
                {
                    if (i - 1 + m < 0 || i - 1 + m > height - 1 || j - 1 + n < 0 || j - 1 + n > width - 1 )
                    {
                        continue;
                    }
                    redx += image[i - 1 + m][j - 1 + n].rgbtRed * Gx[m][n];
                    redy += image[i - 1 + m][j - 1 + n].rgbtRed * Gy[m][n];
                    greenx += image[i - 1 + m][j - 1 + n].rgbtGreen * Gx[m][n];
                    greeny += image[i - 1 + m][j - 1 + n].rgbtGreen * Gy[m][n];
                    bluex += image[i - 1 + m][j - 1 + n].rgbtBlue * Gx[m][n];
                    bluey += image[i - 1 + m][j - 1 + n].rgbtBlue * Gy[m][n];
                }
                edges[i][j].rgbtRed = round(sqrt((redx * redx) + (redy * redy)));
                edges[i][j].rgbtGreen = round(sqrt((greenx * greenx) + (greeny * greeny)));
                edges[i][j].rgbtBlue = round(sqrt((bluex * bluex) + (bluey * bluey)));
            }
        }
    }
    image[i][j].rgbtRed = edges[i][j].rgbtRed;
    image[i][j].rgbtGreen = edges[i][j].rgbtGreen;
    image[i][j].rgbtBlue = edges[i][j].rgbtBlue;
    return;
}

You must log in to answer this question.

Browse other questions tagged .