It turned out that I can rotate an image with a much simpler algorithm when I working with a copy of an image:
github.com/artyom-poptsov/guil