Improve image writing, especially for color 3d images

Fix #84