Use a template to share the algorithm of TileLayer::region

This way, TileLayer::tilesetReferences can trivially use the same
optimized loop and it also becomes available for searching for other
conditions.
Allow creating empty tilesets and adding images later

Sponsored-by: Jamie Rocks