0
#include "helpers.h"
#include <math.h>
 
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    int total = 0;
    float avg = 0;
 
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            total = image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue;
            avg = total / 3.00;
            image[i][j].rgbtRed = round(avg);
            image[i][j].rgbtGreen = round(avg);
            image[i][j].rgbtBlue = round(avg);
        }
    }
    return;
}
 
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    float sepia_red = 0;
    float sepia_green = 0;
    float sepia_blue = 0;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            sepia_red = (.393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue);
            sepia_green = (.349*image[i][j].rgbtRed) + (.686*image[i][j].rgbtGreen) + (.168*image[i][j].rgbtBlue);
            sepia_blue = (.272*image[i][j].rgbtRed) + (.534*image[i][j].rgbtGreen) + (.131*image[i][j].rgbtBlue);
 
            if (sepia_red > 255)
            {
                sepia_red = 255;
            }
            if (sepia_green > 255)
            {
                sepia_green = 255;
            }
            if (sepia_blue > 255)
            {
                sepia_blue = 255;
            }
            image[i][j].rgbtRed = round(sepia_red);
            image[i][j].rgbtGreen = round(sepia_green);
            image[i][j].rgbtBlue = round(sepia_blue);
        }
    }
    return;
}
 
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int temp[3];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width/2; j ++)
        {
            temp[0] = image[i][j].rgbtRed;
            temp[1] = image[i][j].rgbtGreen;
            temp[2] = image[i][j].rgbtBlue;
 
            image[i][j].rgbtRed = image[i][width - j - 1].rgbtRed;
            image[i][j].rgbtGreen = image[i][width - j - 1].rgbtGreen;
            image[i][j].rgbtBlue = image[i][width - j - 1].rgbtBlue;
 
            image[i][width - j - 1].rgbtRed = temp[0];
            image[i][width - j - 1].rgbtGreen = temp[1];
            image[i][width - j - 1].rgbtBlue = temp[2];
        }
    }
    return;
}
 
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int total = 0;
    float count = 0;
    int elements = 0;
 
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            for (int x = i - 1; x <= i + 1; x++)
            {
                if (x >= 0 && x <= height - 1)
                {
                    elements++;
                }
            }
            for (int y = j - 1; y <= j + 1; y++)
            {
                if (y >= 0 && y < width - 1)
                {
                    elements++;
                }
            }
 
            total = image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue;
 
            count = total / elements;
 
            image[i][j].rgbtRed = round(count);
            image[i][j].rgbtGreen = round(count);
            image[i][j].rgbtBlue = round(count);
        }
    }
    return;
}

After a few reddit posts, I have been told I need to

  • create a temporary variable and then once that has been created go about changing the original pixels to the value fo the temporary variable
  • create an incrementing variable such as (int pixels_used) to divide by

I do not know how to go about this, dividing by the incrementing variable troubles me because I would think I have to create red_count, blue_count, green_count and pixels_used. I tried writing this form of code out here https://pastebin.com/UX1miGtd as it is similar to the example Brian gave us in this picture https://i.stack.imgur.com/K7rVL.png. We add up the total amount of red, green and blue – and then assign the selected pixel to those numbers. Obviously based off the amount of variables I had to use it does not look good and I assume is wrong, but I don't have any other ideas.

If anyone can help I would greatly appreciate!

Enis Arik
  • 665
  • 10
  • 24
  • Just warning you, blur actually does need a ton of variables. My working implementation had like 10 variables. – M-Chen-3 Aug 04 '20 at 23:20
  • There are similar questions on SO. Check those two, https://stackoverflow.com/questions/62209966/cs50-blur-function-does-not-pass-check50-even-though-image-is-being-blurred/62210080#62210080, https://stackoverflow.com/questions/62335810/cs50-pset4-blur-filter-i-am-getting-black-image-as-an-output/62339054#62339054. – Enis Arik Aug 05 '20 at 08:46
  • 1
    @M-Chen-3 thank you! that is super helpful, genuinely because of the amount of variables compared to the other filters I thought I was doing something wrong. – esnzger Aug 05 '20 at 13:23
  • @esnzger No problem! I hope you have a great time taking CS50! :) – M-Chen-3 Aug 05 '20 at 17:22

0 Answers0