I think you still need to write something to the `img` array to prevent garbage values being returned. Perhaps consider masking these values or setting them to NaN (I'm not sure which approach is best, I'm not too familiar with this section of the code).