0

I'm working on Sepia (2020 problem set 4- filter). The command window printed out the following error message:

helpers.c:33:29: runtime error: 260 is outside the range of representable values of type 'unsigned char'

helpers.c:34:31: runtime error: 263 is outside the range of representable values of type 'unsigned char'

But I did assign 255 to all values above 255. The command window also did not print out the printf("color\n") I used to debug. Please help. Thank you.

void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    // loop thorough rows
    for (int i = 0; i < height; i++)
    {
        // loop through columns
        for (int j = 0; j < width; j++)
        {
            // store original values
            BYTE originalRed = image[i][j].rgbtRed;
            BYTE originalGreen = image[i][j].rgbtGreen;
            BYTE originalBlue = image[i][j].rgbtBlue;
            // calculate sepia effect
            BYTE sepiaRed = round(.393 * originalRed + .769 * originalGreen + .189 * originalBlue);
            BYTE sepiaGreen = round(.349 * originalRed + .686 * originalGreen + .168 * originalBlue);
            BYTE sepiaBlue = round(.272 * originalRed + .534 * originalGreen + .131 * originalBlue);

            // cap valus over 255
            if (sepiaRed > 255)
            {
                printf("red\n");
                sepiaRed = 255;
            }

            if (sepiaGreen > 255)
            {
                printf("green\n");
                sepiaGreen = 255;
            }

            if (sepiaBlue > 255)
            {
                printf("blue\n");
                sepiaBlue = 255;
            }

            // assign the new sepia values
            image[i][j].rgbtBlue = sepiaBlue;
            image[i][j].rgbtGreen = sepiaGreen;
            image[i][j].rgbtRed = sepiaRed;
        }
    }
    return;
}
2

instead of using BYTE before sepiaRed, sepiaBlue, sepiaGreen, originalRed, originalBlue and originalGreen, use float

Also, don't forget to round the values of sepia Red, Blue and Green using the round() function defined in math.h

1
  • Changing BYTE to float solved the problem. I guess instead of intrinsic typecasting, it freaked out instead because I used unsigned char to store float. Thank you! – CTK2019 Mar 19 '20 at 7:36

You must log in to answer this question.

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