MAINT: ndimage: malloc casts, NULL checks before free

See 8708ad91fffd972313f4bf97a93869e57b058767 for rationale.
MAINT const for strings in TNC

This is just cleanup, not a semantic change.