Exemplo n.º 1
0
/**
*  test for "ImageWriter".
*
*  argc and argv are the command line parameters which were passed to
*  the ADD_TEST command in the CMakeLists.txt file. For the automatic
*  tests, argv is either empty for the simple tests or contains the filename
*  of a test image for the image tests (see CMakeLists.txt).
*/
int mitkImageWriterTest(int  argc , char* argv[])
{
  // always start with this!
  MITK_TEST_BEGIN("ImageWriter")

  // write your own tests here and use the macros from mitkTestingMacros.h !!!
  // do not write to std::cout and do not return from this function yourself!

  // load image
  MITK_TEST_CONDITION_REQUIRED(argc > 1, "File to load has been specified");


  mitk::Image::Pointer image = NULL;

  try
  {
    MITK_TEST_OUTPUT(<< "Loading file: " << argv[1]);
    image = mitk::IOUtil::LoadImage( argv[1] );
  }
  catch (itk::ExceptionObject & ex)
  {
    MITK_TEST_FAILED_MSG(<< "Exception during file loading: " << ex.GetDescription());
  }


  MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(),"loaded image not NULL")

  mitk::FileWriterRegistry writerRegistry;

  std::ofstream tmpStream;
  std::string tmpFilePath = mitk::IOUtil::CreateTemporaryFile(tmpStream);
  tmpStream.close();

  // write ITK .mhd image (2D and 3D only)
  if( image->GetDimension() <= 3 )
  {
    try
    {
      mitk::IFileWriter* writer = writerRegistry.GetWriter(mitk::Image::GetStaticNameOfClass(), ".mhd");
      writer->Write(image, tmpFilePath);

      mitk::Image::Pointer compareImage = mitk::IOUtil::LoadImage(tmpFilePath);
      MITK_TEST_CONDITION_REQUIRED( compareImage.IsNotNull(), "Image stored in MHD format was succesfully loaded again! ");

      MITK_TEST_CONDITION(itksys::SystemTools::FileExists((tmpFilePath + ".mhd").c_str()), ".mhd file exists")
      MITK_TEST_CONDITION(itksys::SystemTools::FileExists((tmpFilePath + ".raw").c_str()) ||
                          itksys::SystemTools::FileExists((tmpFilePath + ".zraw").c_str()), ".raw or .zraw exists")

      // delete
      remove((tmpFilePath + ".mhd").c_str());
      remove((tmpFilePath + ".raw").c_str());
      remove((tmpFilePath + ".zraw").c_str());
    }
    catch (...)
    {
      MITK_TEST_FAILED_MSG(<< "Exception during .mhd file writing");
    }
  }

  //testing more component image writing as nrrd files
  try
  {
    mitk::IFileWriter* writer = writerRegistry.GetWriter(mitk::Image::GetStaticNameOfClass(), ".nrrd");
    writer->Write(image, tmpFilePath);

    std::fstream fin;
    mitk::Image::Pointer compareImage = mitk::IOUtil::LoadImage(AppendExtension(tmpFilePath, ".nrrd").c_str());
    MITK_TEST_CONDITION_REQUIRED(compareImage.IsNotNull(), "Image stored in NRRD format was succesfully loaded again");
    fin.close();
    remove(AppendExtension(tmpFilePath, ".nrrd").c_str());
  }
  catch(...)
  {
    MITK_TEST_FAILED_MSG(<< "Exception during .nrrd file writing");
  }

  TestPictureWriting(image, tmpFilePath, ".png");
  TestPictureWriting(image, tmpFilePath, ".jpg");
  TestPictureWriting(image, tmpFilePath, ".tiff");
  TestPictureWriting(image, tmpFilePath, ".bmp");

  // test for exception handling
  std::vector<mitk::IFileWriter*> writers = writerRegistry.GetWriters(mitk::Image::GetStaticNameOfClass());
  for (std::vector<mitk::IFileWriter*>::const_iterator iter = writers.begin(),
       end = writers.end(); iter != end; ++iter)
  {
    try
    {
      (*iter)->Write(image, "/usr/bin");
      MITK_TEST_FAILED_MSG( << "itk::ExceptionObject expected")
    }
    catch (const itk::ExceptionObject&) { /* this is expected */ }
    catch(...)
    {
      //this means that a wrong exception (i.e. no itk:Exception) has been thrown
      MITK_TEST_FAILED_MSG(<< "Wrong exception (i.e. no itk:Exception) caught during write");
    }
  }

  // always end with this!
  MITK_TEST_END();
}
Exemplo n.º 2
0
/**
*  test for "ImageWriter".
*
*  argc and argv are the command line parameters which were passed to
*  the ADD_TEST command in the CMakeLists.txt file. For the automatic
*  tests, argv is either empty for the simple tests or contains the filename
*  of a test image for the image tests (see CMakeLists.txt).
*/
int mitkImageWriterTest(int  argc , char* argv[])
{
  // always start with this!
  MITK_TEST_BEGIN("ImageWriter")

  // let's create an object of our class
  mitk::ImageWriter::Pointer myImageWriter = mitk::ImageWriter::New();

  // first test: did this work?
  // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since
  // it makes no sense to continue without an object.
  MITK_TEST_CONDITION_REQUIRED(myImageWriter.IsNotNull(),"Testing instantiation")

  // write your own tests here and use the macros from mitkTestingMacros.h !!!
  // do not write to std::cout and do not return from this function yourself!

  // load image
  MITK_TEST_CONDITION_REQUIRED(argc > 1, "File to load has been specified");


  mitk::Image::Pointer image = NULL;

  try
  {
    MITK_TEST_OUTPUT(<< "Loading file: " << argv[1]);
    image = mitk::IOUtil::LoadImage( argv[1] );
  }
  catch (itk::ExceptionObject & ex)
  {
    MITK_TEST_FAILED_MSG(<< "Exception during file loading: " << ex.GetDescription());
  }


  MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(),"loaded image not NULL")
  std::stringstream filename_stream;

#ifdef WIN32
  filename_stream << "test" << _getpid();
#else
  filename_stream << "test" << getpid();
#endif


  std::string filename = filename_stream.str();
  std::cout << filename << std::endl;

  // test set/get methods
  myImageWriter->SetInput(image);
  MITK_TEST_CONDITION_REQUIRED(myImageWriter->GetInput()==image,"test Set/GetInput()");
  myImageWriter->SetFileName(filename);
  MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFileName(),filename.c_str()),"test Set/GetFileName()");
  myImageWriter->SetFilePrefix("pref");
  MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFilePrefix(),"pref"),"test Set/GetFilePrefix()");
  myImageWriter->SetFilePattern("pattern");
  MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFilePattern(),"pattern"),"test Set/GetFilePattern()");

  // write ITK .mhd image (2D and 3D only)
  if( image->GetDimension() <= 3 )
  {
    try
    {
      myImageWriter->SetExtension(".mhd");
      myImageWriter->Update();

      mitk::Image::Pointer compareImage = mitk::IOUtil::LoadImage( AppendExtension(filename, ".mhd").c_str() );
      MITK_TEST_CONDITION_REQUIRED( compareImage.IsNotNull(), "Image stored in MHD format was succesfully loaded again! ");

      std::string rawExtension = ".raw";
      std::fstream rawPartIn;
      rawPartIn.open(AppendExtension(filename, ".raw").c_str());
      if( !rawPartIn.is_open() )
      {
        rawExtension = ".zraw";
        rawPartIn.open(AppendExtension(filename, ".zraw").c_str());
      }

      MITK_TEST_CONDITION_REQUIRED(rawPartIn.is_open(),"Write .raw file");
      rawPartIn.close();

      // delete
      remove(AppendExtension(filename, ".mhd").c_str());
      remove(AppendExtension(filename, rawExtension.c_str()).c_str());
    }
    catch (...)
    {
      MITK_TEST_FAILED_MSG(<< "Exception during .mhd file writing");
    }
  }

  //testing more component image writing as nrrd files
  try
  {
    myImageWriter->SetExtension(".nrrd");
    myImageWriter->Update();
    std::fstream fin;
    mitk::Image::Pointer compareImage = mitk::IOUtil::LoadImage(AppendExtension(filename, ".nrrd").c_str());
    MITK_TEST_CONDITION_REQUIRED(compareImage.IsNotNull(), "Image stored in NRRD format was succesfully loaded again");
    fin.close();
    remove(AppendExtension(filename, ".nrrd").c_str());
  }
  catch(...)
  {
    MITK_TEST_FAILED_MSG(<< "Exception during .nrrd file writing");
  }

  TestPictureWriting(image, filename, ".png");
  TestPictureWriting(image, filename, ".jpg");
  TestPictureWriting(image, filename, ".tiff");
  TestPictureWriting(image, filename, ".bmp");

  // test for exception handling
  try
  {
    MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
    myImageWriter->SetInput(image);
    myImageWriter->SetFileName("/usr/bin");
    myImageWriter->Update();
    MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
  }
  catch(...) {
    //this means that a wrong exception (i.e. no itk:Exception) has been thrown
    MITK_TEST_FAILED_MSG(<< "Wrong exception (i.e. no itk:Exception) caught during write");
  }

  // always end with this!
  MITK_TEST_END();
}