Implemented faster upscaling using templates.

Fixes #819.