# What's wrong with my sepia code? pls help

My sepia code are as follows:

``````// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
// Comb through each row
for (int i = 0; i < height; i++)
{
// Comb through each column
for (int j = 0; j < width; j++)
{
// Get the original value of each pixel
int originalRed = image[i][j].rgbtRed;
int originalGreen = image[i][j].rgbtGreen;
int originalBlue = image[i][j].rgbtBlue;

// Change the value of each pixel by sepia fomula
image[i][j].rgbtRed = round(.393 * originalRed + .769 * originalGreen + .189 * originalBlue);
image[i][j].rgbtGreen = round(.349 * originalRed + .686 * originalGreen + .168 * originalBlue);
image[i][j].rgbtBlue = round(.272 * originalRed + .534 * originalGreen + .131 * originalBlue);

// Make sur the value is less than 256
if (image[i][j].rgbtRed > 255)
{
image[i][j].rgbtRed = 255;
}
if (image[i][j].rgbtGreen > 255)
{
image[i][j].rgbtGreen = 255;
}
if (image[i][j].rgbtBlue > 255)
{
image[i][j].rgbtBlue = 255;
}
}
}
return;
}
``````

As I tried running it, the original image:

After filtering:

I just don't know what I did wrong.Could u pls help me? Thanks!

While the code looks like it should work, there's an underlying problem that is breaking it. The problem is centered around variable and struct types.

Look in bmp.h at the type def for a pixel:

``````/**
* RGBTRIPLE
*
* This structure describes a color consisting of relative intensities of
* red, green, and blue.
*
*/
typedef struct
{
BYTE  rgbtBlue;
BYTE  rgbtGreen;
BYTE  rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
``````

The three vars in the struct that hold the values for Red, Green and Blue are all single byte vars. If you read higher up in bmp.h, they're really 8 bit unsigned integers.

This has a tremendous impact on this code. If any of your calculations try to set a value greater than 255, then it will cause an overflow condition and the value stored will not be the calculated value. The code needs to calculate the new sepia values using larger vars, such as a regular integer or int type. Once it's checked for being > 255 and replaced if necessary, then the result can be stored back in the struct's values for rbg.

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

• Thank you so much! So does it mean that I should make sure the calculated value is less than 256 before I assign it to the image's pixel? Commented Aug 26, 2022 at 1:17

Try declaring the pixels in the new image like "image[i][j].rgbtRed" as integer values. Use those integers in your calculations and then assign them to the pixels in the array at the very end.

• How would you do that? Sounds like a redefinition of the RGBTRIPLE type - not a great idea. Instead, just create 3 temp vars, red, green and blue, as int types, and use them to calculate the new values. Then, transfer the values back to the pixel array. Commented Aug 19, 2022 at 4:38
• ... more specifically, Then, after verifying that the new values are between 0 and 255 inclusive, transfer the values back to the pixel array. Commented Aug 26, 2022 at 8:36