Copied over the raster generation code from rasterfun.