I wanted to save a 2D mitkImage and/or a 3D with just one slice mitkImage as .png, which does not work correctly.
I used the following code to save the image:
mitk::ImageWriter::Pointer imageWriter = mitk::ImageWriter::New();
imageWriter->SetInput(image);
imageWriter->SetFileName("/path/name");
imageWriter->SetExtension(".png");
imageWriter->Update();
imageWriter->Write();
I figured out that the line 42 (int numberOfSlices = itkImage->GetLargestPossibleRegion().GetSize()[2];) in mitkItkPictureWrite.cpp does not result in the correct number of slices, when you input a 2D image or a 3D image with just one slice.
Furthermore, if you don't set the extension in the SetFileName() method (like i did), the code behind line 62 in the mitkImageWriter will never be reached. I didn't have a detailed look at the logic there, but either the "if" seems wrong or the code behind is dead. In general, the logic saving the different formats (.nrrd, .pic, or whatever) seems complicated and could need some refactoring. In my opition the code to save images as .png is inside the "if condition" in line 62 and so I think the "if" is just wrong. (It checks: fileName.find(".png") != std::string::npos).
Additionally, the mitkImageWriterTest does not support the .png format (or different dimensions) at all.