0

the values are given by images, so

typedef struct IMAGE{
        uint8_t  rgbtBlue;
        uint8_t  rgbtGreen;
        uint8_t  rgbtRed;
}IMAGE;
IMAGE image[height][width];

in the function, I create another 2 dimensional array.

typedef struct BUFFER{
        uint8_t  rgbtBlue;
        uint8_t  rgbtGreen;
        uint8_t  rgbtRed;
    }BUFFER;
BUFFER buffer[height][width];

After calculation, I will give the values back, updating the images' values.

But I encounter a problem. For example:

int GxB, GyB;
buffer[i][j].rgbtBlue = round((double)sqrt(GxB * GxB + GyB * GyB));
if (buffer[i][j].rgbtBlue >= 255)
      buffer[i][j].rgbtBlue = 255;
printf("%d\n", buffer[i][j].rgbtBlue);

If statement is keeping being ignored, I know there must be something wrong with uint8_t because after I change rgbtBlue to integer, everything is fine, the if statement is working. I've searched online but cannot find the answer. Any tips will be appreciated

akuan10
  • 111
  • 6

1 Answers1

2

The maximum value of uint8_t is 255. Therefore,

if (buffer[i][j].rgbtBlue >= 255)
      buffer[i][j].rgbtBlue = 255;

means

if (buffer[i][j].rgbtBlue == 255)
      buffer[i][j].rgbtBlue = 255;

and it does virtually nothing.

You may want to store the result of round() to a bigger type not to cause truncation and then check the value, do proper process and assign to buffer[i][j].rgbtBlue like this:

double round_res = round((double)sqrt(GxB * GxB + GyB * GyB));
if (round_res >= 255)
      round_res = 255;
buffer[i][j].rgbtBlue = round_res;
MikeCAT
  • 73,922
  • 11
  • 45
  • 70