Carefully go through the logic of your loops. If you loop through the entire width/height, adding an offset other than 0 will at some point take you out of bounds. Either loop from offset forward, or...