Fix image flipping code
Dynamically calculate padding, fix row size calculation