img: Support writing 3d file to an existing FILE*
This complements the support for reading from an existing FILE*.