Reputation: 193
I am implementing an image processing filter within OTB (a C++ library).
I got a strange behaviour with some very basic testing. When I read my image as "char", the following code always outputs the pixel value (in the 0-200 range) even if it is larger than 150. However, it works fine when I use "short int". Is there a special behaviour with "char" (like a letter comparison instead of its numerical value) or could there be any other reason ?
Of course, my image pixels are stored in Bytes, so I prefer to handle "char" instead of "int" because the image is quite large (> 10 Gb).
if (pixelvalue > 150)
{
out = 255;
}
else
{
out = pixelvalue;
}
Upvotes: 5
Views: 1554
Reputation: 129464
The type char
is either signed or unsigned (depending on what the compiler "likes" - and sometimes there are options to select signed or unsigned char types), and the guaranteed size is 8 bits minimum (could be 9, 16, 18, 32 or 36 bits, even if such machines are relatively rare).
Your value of 150 is higher than the biggest signed value for an 8-bit value, which implies that the system you are using has a signed char
.
If the purpose of the type is to be an integer value of a particular size, use [u]intN_t
- in this case, since you want an unsigned value, uint8_t
. That indicates much better that the data you are working on is not a "string" [even if behind the scenes, the compiler translates to unsigned char
. This will fail to compile if you ever encounter a machine where a char
isn't 8 bits, which is a safeguard against trying to debug weird problems.
Upvotes: 5
Reputation: 179991
unsigned char
runs to (at least) 255, but char
may be signed and limited to 127.
Upvotes: 12