I think it's because your code assumes the image either has 3 channels (RGB) or 4 (RGBA), but there's also a possibility that it has just 1 channel (grayscale). When attempting to decode grayscale images, things go wrong because you're trying to access non-existent channels.
For grayscale images, you can just set R=G=B=[channel 0 value].
I think it's because your code assumes the image either has 3 channels (RGB) or 4 (RGBA), but there's also a possibility that it has just 1 channel (grayscale). When attempting to decode grayscale images, things go wrong because you're trying to access non-existent channels.
For grayscale images, you can just set R=G=B=[channel 0 value].