Inpaint gradient function refactor

Creating an array on each iteration was slow. Also, that implementation had quite a bit of duplicated logic.