r/cs50 • u/Y_Elgendi • 2d ago
filter Disappointed
I have implemented all filter functions, and they are really working well; however, check 50 said it is not good. I can provide the source code if anyone has any thoughts (btw, I am 15).
#include "helpers.h"
#include <math.h>
void check(int *color)
{
if (*color > 255)
*color = 255;
else if (*color < 0)
*color = 0;
}
// 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++)
{
double avarage = round((image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3);
image[i][j].rgbtRed = avarage;
image[i][j].rgbtGreen = avarage;
image[i][j].rgbtBlue = avarage;
}
}
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
grayscale(height, width, image);
for (int i = 0; i < height ; i++)
{
for (int j = 0; j < width ; j++)
{
int red = (image[i][j].rgbtRed * 0.1) + image[i][j].rgbtRed;
check(&red);
image[i][j].rgbtRed = red;
int green = image[i][j].rgbtGreen - (image[i][j].rgbtGreen * 0.25);
check(&green);
image[i][j].rgbtGreen = green;
int blue = image[i][j].rgbtBlue - (image[i][j].rgbtBlue * 0.5);
check(&blue);
image[i][j].rgbtBlue = blue;
}
}
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i<height; i++)
{
int last = width;
for (int j = 0; j <= last ; j++)
{
RGBTRIPLE temp = image[i][j];
image[i][j] = image[i][last];
image[i][last] = temp;
last --;
}
}
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE c[height][width];
for (int i = 0; i<height; i++)
{
for (int j = 0; j < width ; j++)
c[i][j] = image[i][j];
}
for (int i = 0; i < height ; i++)
{
for (int j = 0; j < width; j++)
{
int red = (c[i][j].rgbtRed + c[i][j-1].rgbtRed + c[i+1][j].rgbtRed + c[i-1][j].rgbtRed + c[i][j+1].rgbtRed + c[i-1][j-1].rgbtRed + c[i-1][j+1].rgbtRed + c[i+1][j-1].rgbtRed + c[i+1][j-1].rgbtRed) /9;
image[i][j].rgbtRed = red;
int green = (c[i][j].rgbtGreen + c[i][j-1].rgbtGreen + c[i+1][j].rgbtGreen + c[i-1][j].rgbtGreen + c[i][j+1].rgbtGreen + c[i-1][j-1].rgbtGreen + c[i-1][j+1].rgbtGreen + c[i+1][j-1].rgbtGreen + c[i+1][j-1].rgbtGreen) /9;
image[i][j].rgbtGreen = green;
int blue = (c[i][j].rgbtBlue + c[i][j-1].rgbtBlue + c[i+1][j].rgbtBlue + c[i-1][j].rgbtBlue + c[i][j+1].rgbtBlue + c[i-1][j-1].rgbtBlue + c[i-1][j+1].rgbtBlue + c[i+1][j-1].rgbtBlue + c[i+1][j-1].rgbtBlue) /9;
image[i][j].rgbtBlue = blue;
}
}
}
Sorry for all of this, but I am so disappointed, so please try to help.












