MATLAB: Does IMREAD return more significant bits than are included in the PNG-file

bit depthbitdepthMATLAB

I have a PNG-file that has 16-bit color depth. The original image was 10-bit, and the file has been saved with the sBIT tag to indicate this.
However, when I use IMREAD to load this into MATLAB, the data has more then 16-bits. It appears the trailing bits, which I expected to be zero, repeat some of the significant bits.

Best Answer

In cases where the image has fewer significant bits than the bit depth of the PNG-file, the image encoder that produces the PNG-file must store the original significant bits as the most significant bits of the pixel data, but has the option of filling in the (undefined) least significant bits as it wishes, according to the PNG standard.
One common method of doing this is "left-bit replication", which you have observed. This has the effect of scaling the original data (approximately linearly) to fill the dynamic range allowed by the bit depth of the PNG-file. This accounts for the data repetition observed.
The image decoder has the option of ignoring the number of significant bits, but the original data can always be obtained by bit-shifting out the least significant bits.
You may use the BITSHIFT function in MATLAB to recover the original data. For instance, with the 10-bit image with 16-bit color depth:
data = imread('foo.png');
data = bitshift(data, 10 - 16);