fix a off by one error in calculating k (thanks to tip from Tobias Kalbitz)
use uint for size, width and height