I need to resize some images and the library I'm using is stb, the free header-only C library, and it has functions for uint8, uint16, uint32, and float. I have some images that are signed int16, with values ranging from negative to positive. What would happen to those values if I pass those signed ints cast as unsigned int, either explicitly or implicitly by the compiler? I'm guessing it would completely break. Do I have to therefore create a new buffer, convert them to float, and call the float function, then convert those float values back to int16? Is there any better way to do it?
What happens when you pass a signed integer to a function that expects an unsigned integer for the purpose of filtering/mixing?
159 Views Asked by Zebrafish At
2
There are 2 best solutions below
0
On
Two things shall be taken into considerations:
is there a communication between different devices? such as driver etc... then endianness shall be checked or even two unsigned bytes values will differ from architecture to another,
When using a signed integer, first bit is always considered as integer sign, therefore you will be following a 2s complement representation, and the max of 2 Bytes will be 2^15 - 1 instead of 2^16 - 1 with a signing capability
therefore:
- in an uint16_t the range is from 0 ... 65535
- while in a int16_t the range is from -32768 ... 32767
If you pass an array of
int16_tto a function that expects an array ofuint16_t, using a(uint16_t *)cast to silence the compiler warning, whether the code will execute or break depends on how it handles values in the range 32768 to 65535. Any negative entries in the array will be read as large positive values offset by 65536, so with a strong discontinuity at0.Depending of the actual algorithm implemented in the filter, the effect will range from normal to unexpected and including surprising, weird, psychedelic and the like.