22 #include "itksys/SystemTools.hxx" 23 #include <itkImageRegionIterator.h> 36 CPPUNIT_TEST_SUITE(mitkItkImageIOTestSuite);
45 MITK_TEST(TestWrite3DplusT_ProportionalTG);
46 CPPUNIT_TEST_SUITE_END();
49 void setUp()
override {}
50 void tearDown()
override {}
51 void TestImageWriterJpg() { TestImageWriter(
"NrrdWritingTestImage.jpg"); }
52 void TestImageWriterPng1() { TestImageWriter(
"Png2D-bw.png"); }
53 void TestImageWriterPng2() { TestImageWriter(
"RenderingTestData/rgbImage.png"); }
54 void TestImageWriterPng3() { TestImageWriter(
"RenderingTestData/rgbaImage.png"); }
55 void TestWrite3DplusT_ArbitraryTG()
57 TestImageWriter(
"3D+t-ITKIO-TestData/LinearModel_4D_arbitrary_time_geometry.nrrd");
60 void TestWrite3DplusT_ProportionalTG()
62 TestImageWriter(
"3D+t-ITKIO-TestData/LinearModel_4D_prop_time_geometry.nrrd");
65 void TestImageWriterSimple()
70 std::string AppendExtension(
const std::string &filename,
const char *extension)
72 std::string new_filename = filename;
74 new_filename += extension;
82 if (image->GetDimension() != reference->GetDimension())
84 MITK_ERROR <<
"The image dimension differs: IN (" << image->GetDimension() <<
") REF(" 85 << reference->GetDimension() <<
")";
91 (image->GetPixelType() != reference->GetPixelType() &&
92 image->GetPixelType().GetBitsPerComponent() != reference->GetPixelType().GetBitsPerComponent()))
94 MITK_ERROR <<
"Pixeltype differs ( image=" << image->GetPixelType().GetPixelTypeAsString() <<
"[" 95 << image->GetPixelType().GetBitsPerComponent() <<
"]" 96 <<
" reference=" << reference->GetPixelType().GetPixelTypeAsString() <<
"[" 97 << reference->GetPixelType().GetBitsPerComponent() <<
"]" 110 void TestPictureWriting(
mitk::Image *image,
const std::string &filename,
const std::string &extension)
112 const std::string fullFileName = AppendExtension(filename, extension.c_str());
118 extractFilter->SetInput(image);
121 extractFilter->Update();
122 singleSliceImage = extractFilter->GetOutput();
128 unsigned int foundImagesCount = 0;
134 std::stringstream series_filenames;
135 series_filenames << filename << extension;
137 if (compareImage.IsNotNull())
141 CompareImageMetaData(singleSliceImage, compareImage,
false),
142 "Image meta data unchanged after writing and loading again. ");
144 remove(series_filenames.str().c_str());
148 for (
unsigned int i = 0; i < image->
GetDimension(2); i++)
150 std::stringstream series_filenames;
151 series_filenames << filename <<
"." << i + 1 << extension;
153 if (compareImage.IsNotNull())
157 CompareImageMetaData(singleSliceImage, compareImage,
false),
158 "Image meta data unchanged after writing and loading again. ");
160 remove(series_filenames.str().c_str());
164 "All 2D-Slices of a 3D image were stored correctly.");
168 singleSliceImage =
image;
172 if (singleSliceImage.IsNotNull())
182 CompareImageMetaData(singleSliceImage, compareImage,
false),
183 "Image meta data unchanged after writing and loading again. ");
184 remove(fullFileName.c_str());
186 catch (itk::ExceptionObject &e)
188 MITK_TEST_FAILED_MSG(<<
"Exception during file writing for ." << extension <<
": " << e.what());
198 CPPUNIT_ASSERT_MESSAGE(
"Internal error. Passed reference image is null.", image);
200 std::ofstream tmpStream;
209 CPPUNIT_ASSERT_MESSAGE(
"Image stored in NRRD format was succesfully loaded again", compareImage.IsNotNull());
217 CPPUNIT_ASSERT_MESSAGE(
218 "TimeGeometries are equal.",
221 remove(tmpFilePath.c_str());
225 std::remove(tmpFilePath.c_str());
226 CPPUNIT_FAIL(
"Exception during NRRD file writing");
235 CPPUNIT_ASSERT_MESSAGE(
"Internal error. Passed reference image is null.", image);
237 std::ofstream tmpStream;
241 std::string tmpFilePathWithoutExt = tmpFilePath.substr(0, tmpFilePath.size() - 4);
248 CPPUNIT_ASSERT_MESSAGE(
"Image stored in MHD format was succesfully loaded again! ", compareImage.IsNotNull());
250 CPPUNIT_ASSERT_MESSAGE(
".mhd file exists",
251 itksys::SystemTools::FileExists((tmpFilePathWithoutExt +
".mhd").c_str()));
252 CPPUNIT_ASSERT_MESSAGE(
".raw or .zraw exists",
253 itksys::SystemTools::FileExists((tmpFilePathWithoutExt +
".raw").c_str()) ||
254 itksys::SystemTools::FileExists((tmpFilePathWithoutExt +
".zraw").c_str()));
262 CPPUNIT_ASSERT_MESSAGE(
"TimeGeometries are equal.",
266 remove(tmpFilePath.c_str());
267 remove((tmpFilePathWithoutExt +
".raw").c_str());
268 remove((tmpFilePathWithoutExt +
".zraw").c_str());
272 CPPUNIT_FAIL(
"Exception during.mhd file writing");
284 void TestImageWriter(std::string sourcefile)
289 CPPUNIT_ASSERT_MESSAGE(
"Checking whether source image exists", itksys::SystemTools::FileExists(sourcefile.c_str()));
295 image = mitk::IOUtil::Load<mitk::Image>(sourcefile);
299 CPPUNIT_FAIL(
"Exception during file loading:");
302 CPPUNIT_ASSERT_MESSAGE(
"loaded image not nullptr", image.IsNotNull());
305 if (image->GetDimension() <= 3)
307 TestMHDWriting(image);
311 TestNRRDWriting(image);
313 std::ofstream tmpStream;
317 TestPictureWriting(image, tmpFilePath,
".png");
318 TestPictureWriting(image, tmpFilePath,
".jpg");
319 TestPictureWriting(image, tmpFilePath,
".tiff");
320 TestPictureWriting(image, tmpFilePath,
".bmp");
327 void TestWrite3DImageWithOnePlane()
329 typedef itk::Image<unsigned char, 3>
ImageType;
331 ImageType::Pointer itkImage = ImageType::New();
333 ImageType::IndexType start;
336 ImageType::SizeType size;
341 ImageType::RegionType region;
342 region.SetSize(size);
343 region.SetIndex(start);
344 itkImage->SetRegions(region);
345 itkImage->Allocate();
346 itkImage->FillBuffer(0);
348 itk::ImageRegionIterator<ImageType> imageIterator(itkImage, itkImage->GetLargestPossibleRegion());
351 while (!imageIterator.IsAtEnd())
353 if ((imageIterator.GetIndex()[0] > 5 && imageIterator.GetIndex()[0] < 20) &&
354 (imageIterator.GetIndex()[1] > 5 && imageIterator.GetIndex()[1] < 20))
356 imageIterator.Set(255);
359 if ((imageIterator.GetIndex()[0] > 50 && imageIterator.GetIndex()[0] < 70) &&
360 (imageIterator.GetIndex()[1] > 50 && imageIterator.GetIndex()[1] < 70))
362 imageIterator.Set(60);
376 void TestWrite3DImageWithTwoPlanes()
378 typedef itk::Image<unsigned char, 3>
ImageType;
380 ImageType::Pointer itkImage = ImageType::New();
382 ImageType::IndexType start;
385 ImageType::SizeType size;
390 ImageType::RegionType region;
391 region.SetSize(size);
392 region.SetIndex(start);
393 itkImage->SetRegions(region);
394 itkImage->Allocate();
395 itkImage->FillBuffer(0);
397 itk::ImageRegionIterator<ImageType> imageIterator(itkImage, itkImage->GetLargestPossibleRegion());
400 while (!imageIterator.IsAtEnd())
402 if ((imageIterator.GetIndex()[0] > 5 && imageIterator.GetIndex()[0] < 20) &&
403 (imageIterator.GetIndex()[1] > 5 && imageIterator.GetIndex()[1] < 20))
405 imageIterator.Set(255);
407 if ((imageIterator.GetIndex()[0] > 50 && imageIterator.GetIndex()[0] < 70) &&
408 (imageIterator.GetIndex()[1] > 50 && imageIterator.GetIndex()[1] < 70))
410 imageIterator.Set(60);
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
itk::Image< unsigned char, 3 > ImageType
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static std::string GetTestDataFilePath(const std::string &testData)
Get the absolute path for test data.
Image::Pointer ImportItkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, const BaseGeometry *geometry=nullptr, bool update=true)
Imports an itk::Image (with a specific type) as an mitk::Image.Instantiates instance of ITKImageImpor...
virtual mitk::PlaneGeometry * GetPlaneGeometry(int s) const
Returns the PlaneGeometry of the slice (s).
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
unsigned int GetDimension() const
Get dimension of the image.
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
#define MITK_TEST_CONDITION(COND, MSG)
#define MITK_TEST_FAILED_MSG(MSG)
Fail and finish test with message MSG.
Image class for storing images.
Test fixture for parameterized tests.
SlicedGeometry3D * GetSlicedGeometry(unsigned int t=0) const
Convenience access method for the geometry, which is of type SlicedGeometry3D (or a sub-class of it)...
mitk::Image::Pointer image
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
static std::string CreateTemporaryFile(std::ofstream &tmpStream, const std::string &templateName="XXXXXX", std::string path=std::string())
MITKCORE_EXPORT const ScalarType eps