1

I have managed to get the sepia filter to work - almost. At first I was getting bright pink/magenta in the areas that were supposed to be white. I managed to fix that, but now I am left with some light pinky colors in the transition area (between the blue of the sky and white). I'm thinking it has to do with the r/g/b values going over 255, but I seem to have dealt with it in other situations...

Also, my code for the sepia section seems unnecessarily long.

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

    //sepiaRed = 0.393(originalRed) + 0.769(originalG) + 0.189(originalB)
    //sepiaGreen = 0.349(ogR) + 0.686(ogG) + 0.168(ogB)
    //sepiaBlue = 0.272(ogR) + 0.534(ogG) + 0.131(ogB)

    //if values > 255, just set to 255. must be between 0 and 255


    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            // Calculations based on the info in the video
            float sepiaRed = (0.393 * (image[h][w].rgbtRed)) + (0.769 * (image[h][w].rgbtGreen)) + (0.189 * (image[h][w].rgbtBlue));
            float sepiaGreen = (0.349 * (image[h][w].rgbtRed)) + (0.686 * (image[h][w].rgbtGreen)) + (0.168 * (image[h][w].rgbtBlue));
            float sepiaBlue = (0.272 * (image[h][w].rgbtRed)) + (0.534 * (image[h][w].rgbtGreen)) + (0.131 * (image[h][w].rgbtBlue));

            // Rounding to nearest integer
            int sepR = round(sepiaRed);
            int sepG = round(sepiaGreen);
            int sepB = round(sepiaBlue);

            // Checking for cases of r/g/b values > 255. Is there a more concise way to do this?
            if (sepR > 255)
            {
                sepR = 255;
                image[h][w].rgbtRed = sepR;
            }
            else if (sepG > 255)
            {
                sepG = 255;
                image[h][w].rgbtGreen = sepG;
            }
            else if (sepB > 255)
            {
                sepB = 255;
                image[h][w].rgbtBlue = sepB;
            }
            else if (sepR > 255 && sepB > 255)
            {
                sepR = sepB = 255;
                image[h][w].rgbtRed = image[h][w].rgbtBlue = sepR;
            }
            else if (sepR > 255 && sepG > 255)
            {
                sepR = sepG = 255;
                image[h][w].rgbtRed = image[h][w].rgbtGreen = sepR;
            }
            else if (sepB > 255 && sepG > 255)
            {
                sepB = sepG = 255;
                image[h][w].rgbtBlue = image[h][w].rgbtGreen = sepB;
            }
            else if (sepR > 255 && sepB > 255 && sepG > 255)
            {
                sepR = sepB = sepG = 255;
                image[h][w].rgbtRed = image[h][w].rgbtBlue = image[h][w].rgbtGreen = sepR;
            }
            else // For some reason, when I put this at the bottom, it works better than at the top (before the if/else if section)
            {
                image[h][w].rgbtRed = sepR;
                image[h][w].rgbtGreen = sepG;
                image[h][w].rgbtBlue = sepB;
            }
        }
    }
    return;
}

Any ideas/tips?

0

That's lots of if/else ifs. None of the ones after the first 3 will ever run (because if the first was true, none of the others will be tested), so you're missing the situations where more than one of the values is > 255.

Why not just have 3 if statements?

if sepR > 255, set sepR = 255
if sepG > 255, set sepG = 255 etc.

And then, just set the image values.

1
  • Awesome, that worked. Thanks! – azb1297 Jan 18 '20 at 1:37

You must log in to answer this question.

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