Copy improvements of p01 from https://github.com/notmasteryet/jpgjs/pull/25