I have a png file created with python PIL that contains an height map (positive values).
The format is : single channel (grey level) at 16bit, thus 16 bit per pixel.
I read the file on android with BitmapFactory.decodeStream(<...data input stream...>);
I correctly get the size of the image through getWidth()
and getHeight()
.
However when I loop though the pixels calling the getPixel(i,j)
I obtain negative values, something like: -16776192 -16250872 -16250872 -16250872 -16250872 -16249848 ....
Instead I expect a positive value between 0 and 65535.
I discover that the value -16250872 in binary is 1111111111111111111111111111111111111111000010000000100000001000
This shows that the information relies on the first 16 least significant bits.
I tryed with getPixel(i,j)&0xffff
and I obtain a plausible values, however I'm not sure about the endianess: should I to flip the 2 extracted bytes?
Is there a way to do it in more elegant and portable way this conversion ?
NOTE: the file is not a color (RGBA) PNG but a grey level PNG image with a single 16 bit value for each pixel.