26 #include "itksys/SystemTools.hxx"
27 #include <itkImageRegionIterator.h>
40 CPPUNIT_TEST_SUITE(mitkItkImageIOTestSuite);
49 MITK_TEST(TestWrite3DplusT_ProportionalTG);
50 CPPUNIT_TEST_SUITE_END();
53 void setUp()
override {}
54 void tearDown()
override {}
55 void TestImageWriterJpg() { TestImageWriter(
"NrrdWritingTestImage.jpg"); }
56 void TestImageWriterPng1() { TestImageWriter(
"Png2D-bw.png"); }
57 void TestImageWriterPng2() { TestImageWriter(
"RenderingTestData/rgbImage.png"); }
58 void TestImageWriterPng3() { TestImageWriter(
"RenderingTestData/rgbaImage.png"); }
59 void TestWrite3DplusT_ArbitraryTG()
61 TestImageWriter(
"3D+t-ITKIO-TestData/LinearModel_4D_arbitrary_time_geometry.nrrd");
64 void TestWrite3DplusT_ProportionalTG()
66 TestImageWriter(
"3D+t-ITKIO-TestData/LinearModel_4D_prop_time_geometry.nrrd");
69 void TestImageWriterSimple()
74 std::string AppendExtension(
const std::string &
filename,
const char *extension)
78 new_filename += extension;
86 if (image->GetDimension() != reference->GetDimension())
88 MITK_ERROR <<
"The image dimension differs: IN (" << image->GetDimension() <<
") REF("
89 << reference->GetDimension() <<
")";
95 (image->GetPixelType() != reference->GetPixelType() &&
96 image->GetPixelType().GetBitsPerComponent() != reference->GetPixelType().GetBitsPerComponent()))
98 MITK_ERROR <<
"Pixeltype differs ( image=" << image->GetPixelType().GetPixelTypeAsString() <<
"["
99 << image->GetPixelType().GetBitsPerComponent() <<
"]"
100 <<
" reference=" << reference->GetPixelType().GetPixelTypeAsString() <<
"["
101 << reference->GetPixelType().GetBitsPerComponent() <<
"]"
114 void TestPictureWriting(
mitk::Image *image,
const std::string &
filename,
const std::string &extension)
116 const std::string fullFileName = AppendExtension(filename, extension.c_str());
122 extractFilter->SetInput(image);
125 extractFilter->Update();
126 singleSliceImage = extractFilter->GetOutput();
132 unsigned int foundImagesCount = 0;
138 std::stringstream series_filenames;
139 series_filenames << filename << extension;
141 if (compareImage.IsNotNull())
145 CompareImageMetaData(singleSliceImage, compareImage,
false),
146 "Image meta data unchanged after writing and loading again. ");
148 remove(series_filenames.str().c_str());
152 for (
unsigned int i = 0; i < image->
GetDimension(2); i++)
154 std::stringstream series_filenames;
155 series_filenames << filename <<
"." << i + 1 << extension;
157 if (compareImage.IsNotNull())
161 CompareImageMetaData(singleSliceImage, compareImage,
false),
162 "Image meta data unchanged after writing and loading again. ");
164 remove(series_filenames.str().c_str());
168 "All 2D-Slices of a 3D image were stored correctly.");
172 singleSliceImage = image;
176 if (singleSliceImage.IsNotNull())
186 CompareImageMetaData(singleSliceImage, compareImage,
false),
187 "Image meta data unchanged after writing and loading again. ");
188 remove(fullFileName.c_str());
190 catch (itk::ExceptionObject &e)
192 MITK_TEST_FAILED_MSG(<<
"Exception during file writing for ." << extension <<
": " << e.what());
202 CPPUNIT_ASSERT_MESSAGE(
"Internal error. Passed reference image is null.", image);
204 std::ofstream tmpStream;
213 CPPUNIT_ASSERT_MESSAGE(
"Image stored in NRRD format was succesfully loaded again", compareImage.IsNotNull());
221 CPPUNIT_ASSERT_MESSAGE(
222 "TimeGeometries are equal.",
225 remove(tmpFilePath.c_str());
229 std::remove(tmpFilePath.c_str());
230 CPPUNIT_FAIL(
"Exception during NRRD file writing");
239 CPPUNIT_ASSERT_MESSAGE(
"Internal error. Passed reference image is null.", image);
241 std::ofstream tmpStream;
245 std::string tmpFilePathWithoutExt = tmpFilePath.substr(0, tmpFilePath.size() - 4);
252 CPPUNIT_ASSERT_MESSAGE(
"Image stored in MHD format was succesfully loaded again! ", compareImage.IsNotNull());
254 CPPUNIT_ASSERT_MESSAGE(
".mhd file exists",
255 itksys::SystemTools::FileExists((tmpFilePathWithoutExt +
".mhd").c_str()));
256 CPPUNIT_ASSERT_MESSAGE(
".raw or .zraw exists",
257 itksys::SystemTools::FileExists((tmpFilePathWithoutExt +
".raw").c_str()) ||
258 itksys::SystemTools::FileExists((tmpFilePathWithoutExt +
".zraw").c_str()));
266 CPPUNIT_ASSERT_MESSAGE(
"TimeGeometries are equal.",
270 remove(tmpFilePath.c_str());
271 remove((tmpFilePathWithoutExt +
".raw").c_str());
272 remove((tmpFilePathWithoutExt +
".zraw").c_str());
276 CPPUNIT_FAIL(
"Exception during.mhd file writing");
288 void TestImageWriter(std::string sourcefile)
293 CPPUNIT_ASSERT_MESSAGE(
"Checking whether source image exists", itksys::SystemTools::FileExists(sourcefile.c_str()));
303 CPPUNIT_FAIL(
"Exception during file loading:");
306 CPPUNIT_ASSERT_MESSAGE(
"loaded image not NULL", image.IsNotNull());
309 if (image->GetDimension() <= 3)
311 TestMHDWriting(image);
315 TestNRRDWriting(image);
317 std::ofstream tmpStream;
321 TestPictureWriting(image, tmpFilePath,
".png");
322 TestPictureWriting(image, tmpFilePath,
".jpg");
323 TestPictureWriting(image, tmpFilePath,
".tiff");
324 TestPictureWriting(image, tmpFilePath,
".bmp");
331 void TestWrite3DImageWithOnePlane()
333 typedef itk::Image<unsigned char, 3>
ImageType;
337 ImageType::IndexType start;
340 ImageType::SizeType size;
345 ImageType::RegionType region;
346 region.SetSize(size);
347 region.SetIndex(start);
348 itkImage->SetRegions(region);
349 itkImage->Allocate();
350 itkImage->FillBuffer(0);
352 itk::ImageRegionIterator<ImageType> imageIterator(itkImage, itkImage->GetLargestPossibleRegion());
355 while (!imageIterator.IsAtEnd())
357 if ((imageIterator.GetIndex()[0] > 5 && imageIterator.GetIndex()[0] < 20) &&
358 (imageIterator.GetIndex()[1] > 5 && imageIterator.GetIndex()[1] < 20))
360 imageIterator.Set(255);
363 if ((imageIterator.GetIndex()[0] > 50 && imageIterator.GetIndex()[0] < 70) &&
364 (imageIterator.GetIndex()[1] > 50 && imageIterator.GetIndex()[1] < 70))
366 imageIterator.Set(60);
380 void TestWrite3DImageWithTwoPlanes()
382 typedef itk::Image<unsigned char, 3>
ImageType;
386 ImageType::IndexType start;
389 ImageType::SizeType size;
394 ImageType::RegionType region;
395 region.SetSize(size);
396 region.SetIndex(start);
397 itkImage->SetRegions(region);
398 itkImage->Allocate();
399 itkImage->FillBuffer(0);
401 itk::ImageRegionIterator<ImageType> imageIterator(itkImage, itkImage->GetLargestPossibleRegion());
404 while (!imageIterator.IsAtEnd())
406 if ((imageIterator.GetIndex()[0] > 5 && imageIterator.GetIndex()[0] < 20) &&
407 (imageIterator.GetIndex()[1] > 5 && imageIterator.GetIndex()[1] < 20))
409 imageIterator.Set(255);
411 if ((imageIterator.GetIndex()[0] > 50 && imageIterator.GetIndex()[0] < 70) &&
412 (imageIterator.GetIndex()[1] > 50 && imageIterator.GetIndex()[1] < 70))
414 imageIterator.Set(60);
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
itk::SmartPointer< Self > Pointer
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
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...
static bool SaveImage(mitk::Image::Pointer image, const std::string &path)
SaveImage Convenience method to save an arbitrary mitkImage.
map::core::discrete::Elements< 3 >::InternalImageType ImageType
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
#define MITK_TEST_CONDITION(COND, MSG)
static const std::string filename
#define MITK_TEST_FAILED_MSG(MSG)
Fail and finish test with message MSG.
Image class for storing images.
virtual mitk::PlaneGeometry * GetPlaneGeometry(int s) const
Returns the PlaneGeometry of the slice (s).
Test fixture for parameterized tests.
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 std::string CreateTemporaryFile(std::ofstream &tmpStream, const std::string &templateName="XXXXXX", std::string path=std::string())
MITKCORE_EXPORT const ScalarType eps
SlicedGeometry3D * GetSlicedGeometry(unsigned int t=0) const
Convenience access method for the geometry, which is of type SlicedGeometry3D (or a sub-class of it)...
unsigned int GetDimension() const
Get dimension of the image.
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.