0

for greyscale I get issues with check50, but the program appears to operate correctly. With sepia and blur, the image outputted is the same as the image inputted. For blur, I would also be curious to hear if there's anyway of reducing the amount of if statements and for loops in the program.

Thank you


#include "helpers.h"

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

            int grey = blue + green + red / 3;

            image[i][j].rgbtBlue = grey;
            image[i][j].rgbtGreen = grey;
            image[i][j].rgbtRed = grey;

        }
    }

    return;
    // we need to keep in mind that RGBTRIPLE is a double array, and thus represents a grid
    // iterate through each line
        //  iterate through each pixel in the line
            // calculate the average value of each color in the pixel
            // set each color to the average value


}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    return;

    //The amount of sepiaRed should be:
        //.393 * originalRed
        // + .769 * originalGreen
        // + .189 * originalBlue

    // the amount of sepiaGreen should be:

        //.349 * originalRed
        //+ .686 * originalGreen
        //+ .168 * originalBlue

    // the amount of sepiaBlue should be

        //.272 * originalRed
        //+ .534 * originalGreen
        //+ .131 * originalBlue

    // be sure to round the value
    // make sure to cap values at 255

    for(int i = 0; i < height; i++)
    {
        for (int j = 0; j < height; j++)
        {
            int blue = image[i][j].rgbtBlue;
            int green = image[i][j].rgbtGreen;
            int red = image[i][j].rgbtRed;

            int sepiaRed = .393*red + .769*green + .189*blue;
            if (sepiaRed > 255)
            {
                sepiaRed = 255;
            }

            int sepiaGreen = .349*red + .686*green + .168*blue;
            if (sepiaGreen > 255)
            {
                sepiaGreen = 255;
            }

            int sepiaBlue = .272*red + .534*green + .131*blue;
            if (sepiaBlue > 255)
            {
                sepiaBlue = 255;
            }

            image[i][j].rgbtBlue = sepiaBlue;
            image[i][j].rgbtGreen = sepiaGreen;
            image[i][j].rgbtRed = sepiaRed;
        }
    }
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    return;
    // I'm going to need to take the average of surrounding pixels
    // the question is how am I going to do this for edge cases
    // I should still iterate over each pixel in the problem
        // if it's an edge case, act differently
        // try to then go back, and if you can't go back or fourth, then do nothing
    int blue = 0;
    int green = 0;
    int red = 0;
    int counter = 0;

    for (int i = 0; i < height; i++)
    {
        int a;
        int c;

        if (i == 0)
        {
            a = 1;
            c = 0;
        }
        else if (i == height -1)
        {
            a = height - 1;
            c = i - 1;
        }
        else
        {
            a = i + 1;
            c = i - 1;
        }

        for (int j = 0; j < width; j++)
        {
            int b;
            int d;

            if (j == 0)
            {
                b = 1;
                d = 0;
            }
            else if (j == width - 1)
            {
                b = width - 1;
                d = j - 1;
            }
            else
            {
                b = j + 1;
                d = j - 1;
            }

            while (c < a)
            {
                while(d < b)
                {
                    blue += image[c][d].rgbtBlue;
                    green += image[c][d].rgbtGreen;
                    red += image[c][d].rgbtRed;
                    counter ++;
                    d++;
                }
                c++;
            }

            int blurblue = blue / counter;
            int blurgreen = green / counter;
            int blurred = red / counter;

            image[i][j].rgbtBlue = blurblue;
            image[i][j].rgbtGreen = blurgreen;
            image[i][j].rgbtRed = blurred;
        }
    }
}

2

When you divide the 3 colors by 3 you'll probably get a decimal point value. If you receive it in a 'int' variable you'll get a trimmed number. (the decimal part will be removed) The way to do it is round() it.

3
  • I tried that (for greyscale) and it still gives me the same answers – michael Apr 16 '20 at 22:07
  • Also, should I have broken this question into multiple questions? – michael Apr 16 '20 at 22:08
  • For greyscale what worked for me was to work with float numbers to recieve the values from the image and then rounded the result of average it ( round((blue + green + red) / 3) ) . That is because round() returns the nearest integer value of the float/double/long argument. I took the same approach with sepia an blue as they also have some calculations with decimal point numbers. I recomend you when you don't get the expected results to use printf as much as posible to control your variables in every step. Also debug50 is a very helpful tool as you can stop your program at any point. – Tritum Apr 17 '20 at 7:40

You must log in to answer this question.

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