0

Here is my code:

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

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    //Iterate through each pixel
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //Calculate the average
            int average = round((image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3.0);
            image[i][j].rgbtRed = average;
            image[i][j].rgbtGreen = average;
            image[i][j].rgbtBlue = average;
        }

    }
}

//-------------------------------------------------------------------

// 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++)
        {
            int red = image[i][j].rgbtRed;
            int green = image[i][j].rgbtGreen;
            int blue = image[i][j].rgbtBlue;

            //Calculate the red colour
            int red_s = round(red * 0.393 + green * 0.769 + blue * 0.189);

            if (red_s > 255)
            {
                image[i][j].rgbtRed = 255;
            }

            else
            {
                image[i][j].rgbtRed = red_s;
            }

            //Calculate the green colour
            int green_s = round(red * 0.349 + green * 0.686 + blue * 0.168);

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

            else
            {
                image[i][j].rgbtGreen = green_s;
            }

            //Calculate the blue colour
            int blue_s = round(red * 0.272 + green * 0.534 + blue * 0.131);

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

            else
            {
                image[i][j].rgbtBlue = blue_s;
            }
        }
    }
    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 = image[i][j];
            image[i][j] = image[i][width - (j + 1)];
            image[i][width - (j + 1)] = temp;
        }
    }
    return;
}

//-------------------------------------------------------------------------

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{

    RGBTRIPLE tempimage[height][width];
    int start_height = 0;
    int end_height = 0;
    int start_width = 0;
    int end_width = 0;
    int count = 0;
    int red = 0;
    int green = 0;
    int blue = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {

            start_height = 0;
            end_height = 0;
            start_width = 0;
            end_width = 0;

            if (i == 0 && j == 0)
            {
                start_height = 0;
                end_height = 1;
                start_width = 0;
                end_width = 1;

            }
            else if (i == 0 && j == width - 1)
            {
                start_height = 0;
                end_height = 1;
                start_width = width - 2;
                end_width = width - 1;

            }
            else if (i == height - 1 && j == 0)
            {
                start_height = height - 2;
                end_height = height - 1;
                start_width = 0;
                end_width = 1;

            }
            else if (i == height - 1 && j == width - 1)
            {
                start_height = height - 2;
                end_height = height - 1;
                start_width = width - 2;
                end_width = width - 1;

            }
            else if ((i == 0) && (j != 0))
            {
                start_height = 0;
                end_height =  1;
                start_width = j - 1;
                end_width = j + 1;

            }

            else if (i != 0 && j == 0)
            {
                start_height = i - 1;
                end_height = i + 1;
                start_width = 0;
                end_width =  1;


            }

            else if (i == height - 1 && j != width - 1)
            {
                start_height = height - 2;
                end_height = height - 1;
                start_width = j - 1;
                end_width = j + 1;

            }

            else if (i != height - 1 && j == width - 1)
            {
                start_height = i - 1;
                end_height = i + 1;
                start_width = width - 2;
                end_width = width - 1;
            }

            //If none of the conditions work then:
            else
            {
                start_height = i - 1;
                end_height = i + 1;
                start_width = j - 1;
                end_width = j + 1;
            }

            count = 0;
            red = 0;
            green = 0;
            blue = 0;

            for (int tempheight = start_height; tempheight <= end_height; tempheight++)
            {
                for (int tempwidth = start_width; tempwidth <= end_width; tempwidth++)
                {
                    red = red + image[tempheight][tempwidth].rgbtRed;
                    green = green + image[tempheight][tempwidth].rgbtGreen;
                    blue = blue + image[tempheight][tempwidth].rgbtBlue;
                    count = count + 1;
                }
            }
            //Average of colours
            tempimage[i][j].rgbtRed = round(red / (count * 1.0));
            tempimage[i][j].rgbtGreen = round(green / (count * 1.0));
            tempimage[i][j].rgbtBlue = round(blue / (count * 1.0));

        }

        for( i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                image[i][j].rgbtRed = tempimage[i][j].rgbtRed;
                image[i][j].rgbtGreen = tempimage[i][j].rgbtGreen;
                image[i][j].rgbtBlue = tempimage[i][j].rgbtBlue;
            }
        }
        return;
    }
}

Only the blur function is not working else all is fine.

This is the check50 test:

:) helpers.c exists
:) filter compiles
:) grayscale correctly filters single pixel with whole number average
:) grayscale correctly filters single pixel without whole number average
:) grayscale leaves alone pixels that are already gray
:) grayscale correctly filters simple 3x3 image
:) grayscale correctly filters more complex 3x3 image
:) grayscale correctly filters 4x4 image
:) sepia correctly filters single pixel
:) sepia correctly filters simple 3x3 image
:) sepia correctly filters more complex 3x3 image
:) sepia correctly filters 4x4 image
:) reflect correctly filters 1x2 image
:) reflect correctly filters 1x3 image
:) reflect correctly filters image that is its own mirror image
:) reflect correctly filters 3x3 image
:) reflect correctly filters 4x4 image
:( blur correctly filters middle pixel
    expected "127 140 149\n", not "0 127 252\n"
:) blur correctly filters pixel on edge
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."

This is the link of results in check50.

https://submit.cs50.io/check50/03f83f518f48bb302514dc84e553a662fbe9cf75

Please look into it and help we figure out where I have gone wrong.

2
  • can you open link of the results and post all expected and actual output values ? Aug 8 at 9:48
  • Sure, I have updated the question. You can check that out. Aug 13 at 12:40

You must log in to answer this question.

Browse other questions tagged .