I'm working on pset4 filter(less) and have a question regarding limiting the RGB value to 8 bits. I'd like to know what happens when a value of more than 8 bits is stored in BYTE (uint8_t). Initially, I thought that this would limit the value to 255, but clearly, I'm wrong.

BYTE sepiaRed = round(.393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue);
BYTE sepiaGreen = round(.349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue);
BYTE sepiaBlue = round(.272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue);

The error messages I'm getting are:

helpers.c:34:29: runtime error: 274 is outside the range of representable values of type 'unsigned char'
helpers.c:35:31: runtime error: 277 is outside the range of representable values of type 'unsigned char'

On a side note, if this causes 8-bit unsigned integer overflow, according to wraparound, shouldn't I get 19 (255 + 19) and 22 (255 + 22) instead of 274 and 277?


The results are displayed in your question. If the code tries to store a number greater than 8 bits (greater than 255) in a one-byte data type, it generates a runtime overflow error. Nothing will be stored and the program will terminate with an error condition.

Perhaps you should add some code that will check for x > 255 and if true, store 255. OR, use an int instead and then limit the value to 255.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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