Fixed handling of per-tile external image references

Previously Tiled would load maps with per-tile external image references,
but on saving the map all images would become embedded.
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.