An edited version of Kim Asendorf's pixelsorting algorithm.