CS50 Filter (less) Solution

By Suraj Chaudhary

Are you looking for the solution of Harvard’s CS50 PSet 4? Do you want the CS50 Filter (less) Solution of week 4?

If so you’re in the right place.

In this article, I’m going to share with you exactly how to solve the CS50 Filter (less) problem and will also share the CS50 Filter (less) solution with you.

Here’s how to solve the CS50 Filter (less) problem.

The first thing to do is to understand what we are trying to build. You can read the instructions from CS50’s website.

Here’s my entire code for the CS50 Filter (less) Solution in the file helpers.c:

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

#define RED 0
#define GREEN 1
#define BLUE 2

int getBlur(int i, int j, int height, int width, RGBTRIPLE image[height][width], int color_position);

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

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    int sepiaRed, sepiaGreen, sepiaBlue;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            // sepia
            sepiaRed = round(.393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue);
            sepiaGreen = round(.349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue);
            sepiaBlue = round(.272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue);

            // check if capped or not
            // red
            if (sepiaRed > 255)
            {
                image[i][j].rgbtRed = 255;
            }
            else
            {
                image[i][j].rgbtRed = sepiaRed;
            }
            // blue
            if (sepiaBlue > 255)
            {
                image[i][j].rgbtBlue = 255;
            }
            else
            {
                image[i][j].rgbtBlue = sepiaBlue;
            }
            // green
            if (sepiaGreen > 255)
            {
                image[i][j].rgbtGreen = 255;
            }
            else
            {
                image[i][j].rgbtGreen = sepiaGreen;
            }
        }
    }
    return;
}

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp;
    int i, j;

    for (i = 0; i < height; i++)
    {
        for (j = 0; j < width / 2; j++)
        {
            temp = image[i][j];
            image[i][j] = image[i][width - j - 1];
            image[i][width - j - 1] = temp;
        }
    }

    return;
}

// custom blur function
int getBlur(int i, int j, int height, int width, RGBTRIPLE image[height][width], int color_position)
{
    float count = 0;
    int sum = 0;
    for (int row = i - 1; row <= (i + 1); row++)
    {
        for (int column = j - 1; column <= (j + 1); column++)
        {
            if (row < 0 || row >= height || column < 0 || column >= width)
            {
                continue;
            }

            if (color_position == RED)
            {
                sum += image[row][column].rgbtRed;
            }
            else if (color_position == GREEN)
            {
                sum += image[row][column].rgbtGreen;
            }
            else
            {
                sum += image[row][column].rgbtBlue;
            }
            count++;
        }
    }
    return round(sum / count);
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int i, j;
    RGBTRIPLE temp[height][width];

    // copy to temp
    for (i = 0; i < height; i++)
    {
        for (j = 0; j < width; j++)
        {
            temp[i][j] = image[i][j];
        }
    }

    // find average and blur
    for (i = 0; i < height; i++)
    {

        for (j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = getBlur(i, j, height, width, temp, RED);
            image[i][j].rgbtGreen = getBlur(i, j, height, width, temp, GREEN);
            image[i][j].rgbtBlue = getBlur(i, j, height, width, temp, BLUE);
        }
    }
    return;
}

If you have any confusion or problem, feel free to drop a comment down below. In the meantime, you can read these articles.

Also Read

  1. CS50: How To Study At Harvard For FREE (and get a free certificate)
  2. How to use Windows 11 on Mac for Free
  3. How To Get A Domain Name For FREE
  4. How To Use Microsoft Office Apps For FREE Legally (Mac & PC)

Suraj Chaudhary is a writer, developer, founder, and a constant learner. He shares lessons and resources for living a fuller life every week. On this blog, he shares helpful guides and helpful articles that help his 70,000+ monthly readers find answers, solve problems, and meet their curious needs.

Leave a Comment

Slide to prove you're not a bot/spammer *