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

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
   // looping through the image row by row
   for(int i=0; i < height; i++)
   {
    // looping through the row pixel by pixel
    for(int n=0; n < width; n++)
    {
        RGBTRIPLE pixel = image[i][n];

        //grayscale "algorithm"
        int average = round((pixel.rgbtRed + pixel.rgbtBlue + pixel.rgbtGreen) / 3.0);

        //each pixel is now equal to that grayscale value
        image[i][n].rgbtRed = average;
        image[i][n].rgbtBlue = average;
        image[i][n].rgbtGreen = average;
    }
   }
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
   // looping through the image row by row
   for(int i=0; i < height; i++)
   {
    // looping through the row pixel by pixel
    for(int n=0; n < width; n++)
    {
        RGBTRIPLE pixel = image[i][n];

        int OrgRed = pixel.rgbtRed;
        int OrgBlue = pixel.rgbtBlue;
        int OrgGreen = pixel.rgbtGreen;

        //each pixel is now equal to that sepia value
        int SepiaRed = round(.393 * pixel.rgbtRed + .769 * pixel.rgbtGreen + .189 * pixel.rgbtBlue);

        if(SepiaRed > 255)
        {
            pixel.rgbtRed = 255;
        }

        int SepiaGreen = round(.349 * pixel.rgbtRed + .686 * pixel.rgbtGreen + .168 * pixel.rgbtBlue);

        if(SepiaGreen > 255)
        {
            pixel.rgbtGreen = 255;
        }

        int SepiaBlue = round(.272 * pixel.rgbtRed + .534 * pixel.rgbtGreen + .131 * pixel.rgbtBlue);

        if(SepiaBlue > 255)
        {
            pixel.rgbtBlue = 255;
        }

    }
   }
}

//swap function from week 4
void swap(RGBTRIPLE *pixel1, RGBTRIPLE *pixel2)
{
    RGBTRIPLE temp = *pixel1;
    *pixel1 = *pixel2;
    *pixel2 = temp;
}

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int n = 0; n < width; n ++)
        {
            swap(&image[i][n], &image[i][n] - 1);
        }
    }
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];
    //store sum of all colors
    int red = 0;
    int blue = 0;
    int green = 0;

    int total = 0;

    //looping through the image
    for (int i = 0; i < width ; i++)
    {
        for (int j = 0 ; j < height; j ++)
        {

            for (int k = -1; k < 2; k++)
            {
                if (i+k < 0 || i + k > width - 1)
                {
                    continue;
                }

                for (int h = -1; h < 2; h++)
                {
                    if (j+h < 0 || j + h > height -1)
                    {
                        continue;
                    }

                    blue += image[i+k][i+h].rgbtBlue;
                    green += image[i+k][i+h].rgbtGreen;
                    red += image[i+k][i+h].rgbtRed;
                    total ++;
                }
            }

            temp[i][j].rgbtBlue = round(blue/total);
            temp[i][j].rgbtGreen = round(green/total);
            temp[i][j].rgbtRed = round(red/total);

        }
    }

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

For example on sepia I get the following output:

Expected Output:
100 89 69

100 89 69

100 89 69

196 175 136

196 175 136

196 175 136

48 43 33

48 43 33

48 43 33

Actual Output:

255 0 0

255 0 0

255 0 0

0 255 0

0 255 0

0 255 0

0 0 255

0 0 255

0 0 255
2
  • You need to add some more information to your answer. Which values are off and for what function? You have provided all of your code for the problem, but no clue as to where you are having an issue for us to help you. Feb 26 at 16:26
  • Added output. Apologies.
    – Nomad
    Feb 26 at 16:29
1

In your sepia function, you made variables called OrgRed, OrgBlue, and OrgGreen. I am assuming these were to hold the original values so that you would not need to worry about using the modified values in your equations. However, you consistently use the values in pixel, which are the same values that you are updating, leading to unexpected results. I would ensure that your equations are working with constant values.

Also, it is common practice to start your variables with lowercase letters, so orgRed; names that use uppercase letters at the beginning and for every new word are generally used for class names. :)

Hope that helps. If not, let me know and I will try to provide some more support.

1
  • Thank you very much ! I played around with those variables and eventually got it right. Thank you also for pointing out the thing about naming variables :) I was unaware of this somehow.
    – Nomad
    Mar 1 at 14:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .