int testImageToItkAndBack(mitk::Image* imgMem)
{
  int result;

  int *p = (int*)imgMem->GetData();
  if(p==NULL)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing for dimension " << dim << ": " << std::flush;
  std::cout << "Testing mitk::ImageToItk: " << std::flush;
  typedef itk::Image<int,dim> ImageType;

  typename mitk::ImageToItk<ImageType>::Pointer toItkFilter = mitk::ImageToItk<ImageType>::New();
  toItkFilter->SetInput(imgMem);
  toItkFilter->Update();
  typename ImageType::Pointer itkImage = toItkFilter->GetOutput();

  result = testBackCasting<ImageType>(imgMem, itkImage, false);
  if(result != EXIT_SUCCESS)
    return result;

  std::cout << "Testing mitk::ImageToItk (with subsequent DisconnectPipeline, see below): " << std::flush;
  result = testBackCasting<ImageType>(imgMem, itkImage, true);
  if(result != EXIT_SUCCESS)
    return result;

  return EXIT_SUCCESS;
}
itk::Object::Pointer QmitkVersorRigid3DTransformView::GetTransform2(itk::Image<TPixelType, VImageDimension>* itkImage1)
{
  if (VImageDimension == 3)
  {
    typedef typename itk::Image< TPixelType, 3 >  FixedImage3DType;
    typedef typename itk::Image< TPixelType, 3 >  MovingImage3DType;

    // the fixedImage is the input parameter (fix for Bug #14626)
    typename FixedImage3DType::Pointer fixedImage = itkImage1;

    // the movingImage type is known, use the ImageToItk filter (fix for Bug #14626)
    typename mitk::ImageToItk<MovingImage3DType>::Pointer movingImageToItk = mitk::ImageToItk<MovingImage3DType>::New();
    movingImageToItk->SetInput(m_MovingImage);
    movingImageToItk->Update();
    typename MovingImage3DType::Pointer movingImage = movingImageToItk->GetOutput();

    typename itk::VersorRigid3DTransform< double >::Pointer transformPointer = itk::VersorRigid3DTransform< double >::New();
    transformPointer->SetIdentity();
    typedef typename itk::VersorRigid3DTransform< double >    VersorRigid3DTransformType;
    if (m_Controls.m_CenterForInitializerVersorRigid3D->isChecked())
    {
      typedef typename itk::CenteredTransformInitializer<VersorRigid3DTransformType, FixedImage3DType, MovingImage3DType> TransformInitializerType;
      typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
      transformInitializer->SetFixedImage( fixedImage );
      transformInitializer->SetMovingImage( movingImage );
      transformInitializer->SetTransform( transformPointer );
      if (m_Controls.m_MomentsVersorRigid3D->isChecked())
      {
        transformInitializer->MomentsOn();
      }
      else
      {
        transformInitializer->GeometryOn();
      }
      transformInitializer->InitializeTransform();
    }
    typedef VersorRigid3DTransformType::VersorType  VersorType;
    typedef VersorType::VectorType     VectorType;

    VersorType     rotation;
    VectorType     axis;

    axis[0] = 0.0;
    axis[1] = 0.0;
    axis[2] = 1.0;

    const double angle = 0;

    rotation.Set(  axis, angle  );

    transformPointer->SetRotation( rotation );
    m_CenterX = transformPointer->GetCenter()[0];
    m_CenterY = transformPointer->GetCenter()[1];
    m_CenterZ = transformPointer->GetCenter()[2];
    m_TransformObject = transformPointer.GetPointer();
    return transformPointer.GetPointer();
  }
  return NULL;
}
itk::Object::Pointer QmitkCenteredRigid2DTransformView::GetTransform2(
  itk::Image<TPixelType, VImageDimension> *itkImage1)
{
  if (VImageDimension == 2)
  {
    typedef typename itk::Image<TPixelType, 2> FixedImage2DType;
    typedef typename itk::Image<TPixelType, 2> MovingImage2DType;

    // the fixedImage is the input parameter (fix for Bug #14626)
    typename FixedImage2DType::Pointer fixedImage2D = itkImage1;

    // the movingImage type is known, use the ImageToItk filter (fix for Bug #14626)
    typename mitk::ImageToItk<MovingImage2DType>::Pointer movingImageToItk = mitk::ImageToItk<MovingImage2DType>::New();
    movingImageToItk->SetInput(m_MovingImage);
    movingImageToItk->Update();
    typename MovingImage2DType::Pointer movingImage2D = movingImageToItk->GetOutput();

    typename itk::CenteredRigid2DTransform<double>::Pointer transformPointer =
      itk::CenteredRigid2DTransform<double>::New();
    transformPointer->SetIdentity();
    if (m_Controls.m_CenterForInitializerCenteredRigid2D->isChecked())
    {
      typedef typename itk::CenteredRigid2DTransform<double> CenteredRigid2DTransformType;
      typedef
        typename itk::CenteredTransformInitializer<CenteredRigid2DTransformType, FixedImage2DType, MovingImage2DType>
          TransformInitializerType;
      typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
      transformInitializer->SetFixedImage(fixedImage2D);
      transformInitializer->SetMovingImage(movingImage2D);
      transformInitializer->SetTransform(transformPointer);
      if (m_Controls.m_MomentsCenteredRigid2D->isChecked())
      {
        transformInitializer->MomentsOn();
      }
      else
      {
        transformInitializer->GeometryOn();
      }
      transformInitializer->InitializeTransform();
    }
    transformPointer->SetAngle(m_Controls.m_AngleCenteredRigid2D->text().toFloat());
    m_CenterX = transformPointer->GetCenter()[0];
    m_CenterY = transformPointer->GetCenter()[1];
    m_TransformObject = transformPointer.GetPointer();
    return transformPointer.GetPointer();
  }
  return nullptr;
}
itk::Object::Pointer QmitkAffineTransformView::GetTransform2(itk::Image<TPixelType, VImageDimension>* itkImage1)
{
  typedef typename itk::Image< TPixelType, VImageDimension >  FixedImageType;
  typedef typename itk::Image< TPixelType, VImageDimension >  MovingImageType;

  // the fixedImage is the input parameter (fix for Bug #14626)
  typename FixedImageType::Pointer fixedImage = itkImage1;

  // the movingImage type is known, use the ImageToItk filter (fix for Bug #14626)
  typename mitk::ImageToItk<MovingImageType>::Pointer movingImageToItk = mitk::ImageToItk<MovingImageType>::New();
  movingImageToItk->SetInput(m_MovingImage);
  movingImageToItk->Update();
  typename MovingImageType::Pointer movingImage = movingImageToItk->GetOutput();

  typename itk::AffineTransform< double, VImageDimension>::Pointer transformPointer = itk::AffineTransform< double, VImageDimension>::New();
  transformPointer->SetIdentity();
  if (m_Controls.m_CenterForInitializerAffine->isChecked())
  {
    typedef typename itk::AffineTransform< double, VImageDimension >    AffineTransformType;
    typedef typename itk::CenteredTransformInitializer<AffineTransformType, FixedImageType, MovingImageType> TransformInitializerType;
    typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
    transformInitializer->SetFixedImage( fixedImage );
    transformInitializer->SetMovingImage( movingImage );
    transformInitializer->SetTransform( transformPointer );
    if (m_Controls.m_MomentsAffine->isChecked())
    {
      transformInitializer->MomentsOn();
    }
    else
    {
      transformInitializer->GeometryOn();
    }
    transformInitializer->InitializeTransform();
  }
  m_CenterX = transformPointer->GetCenter()[0];
  m_CenterY = transformPointer->GetCenter()[1];
  m_CenterZ = transformPointer->GetCenter()[2];
  m_TransformObject = transformPointer.GetPointer();
  return transformPointer.GetPointer();
}
void mitk::MaskImageFilter::InternalComputeMask(itk::Image<TPixel, VImageDimension>* inputItkImage)
{
  typedef itk::Image<TPixel, VImageDimension> ItkInputImageType;
  typedef itk::Image<unsigned char, VImageDimension> ItkMaskImageType;
  typedef itk::Image<TPixel, VImageDimension> ItkOutputImageType;
  
  typedef itk::ImageRegionConstIterator< ItkInputImageType > ItkInputImageIteratorType;
  typedef itk::ImageRegionConstIterator< ItkMaskImageType > ItkMaskImageIteratorType;
  typedef itk::ImageRegionIteratorWithIndex< ItkOutputImageType > ItkOutputImageIteratorType;

  typename mitk::ImageToItk<ItkMaskImageType>::Pointer maskimagetoitk = mitk::ImageToItk<ItkMaskImageType>::New();
  maskimagetoitk->SetInput(m_MaskTimeSelector->GetOutput());
  maskimagetoitk->Update();
  typename ItkMaskImageType::Pointer maskItkImage = maskimagetoitk->GetOutput();

  typename mitk::ImageToItk<ItkOutputImageType>::Pointer outputimagetoitk = mitk::ImageToItk<ItkOutputImageType>::New();
  outputimagetoitk->SetInput(m_OutputTimeSelector->GetOutput());
  outputimagetoitk->Update();
  typename ItkOutputImageType::Pointer outputItkImage = outputimagetoitk->GetOutput();

  // create the iterators
  typename ItkInputImageType::RegionType inputRegionOfInterest = inputItkImage->GetLargestPossibleRegion();
  ItkInputImageIteratorType  inputIt( inputItkImage, inputRegionOfInterest );
  ItkMaskImageIteratorType  maskIt ( maskItkImage, inputRegionOfInterest );
  ItkOutputImageIteratorType outputIt( outputItkImage, inputRegionOfInterest );

  //typename ItkOutputImageType::PixelType outsideValue = itk::NumericTraits<typename ItkOutputImageType::PixelType>::min();
  if ( !m_OverrideOutsideValue )
    m_OutsideValue = itk::NumericTraits<typename ItkOutputImageType::PixelType>::min();

   m_MinValue = (float)(itk::NumericTraits<typename ItkOutputImageType::PixelType>::max());
   m_MaxValue = (float)(itk::NumericTraits<typename ItkOutputImageType::PixelType>::min());


  for ( inputIt.GoToBegin(), maskIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd() && !maskIt.IsAtEnd(); ++inputIt, ++maskIt, ++outputIt)
  {
    if ( maskIt.Get() > itk::NumericTraits<typename ItkMaskImageType::PixelType>::Zero )
    {
      outputIt.Set(inputIt.Get());
      m_MinValue = vnl_math_min((float)inputIt.Get(), (float)m_MinValue);
      m_MaxValue = vnl_math_max((float)inputIt.Get(), (float)m_MaxValue);
    }
    else
    {
      outputIt.Set(m_OutsideValue);
    }
  }
}
void mitk::_InternalComputeClippedImage(itk::Image<TPixel, VImageDimension>* inputItkImage, mitk::GeometryClipImageFilter* geometryClipper, const mitk::Geometry2D* clippingGeometry2D)
{
  typedef itk::Image<TPixel, VImageDimension> ItkInputImageType;
  typedef itk::Image<TPixel, VImageDimension> ItkOutputImageType;

  typedef itk::ImageRegionConstIteratorWithIndex< ItkInputImageType > ItkInputImageIteratorType;
  typedef itk::ImageRegionIteratorWithIndex< ItkOutputImageType > ItkOutputImageIteratorType;

  typename mitk::ImageToItk<ItkOutputImageType>::Pointer outputimagetoitk = mitk::ImageToItk<ItkOutputImageType>::New();
  outputimagetoitk->SetInput(geometryClipper->m_OutputTimeSelector->GetOutput());
  outputimagetoitk->Update();
  typename ItkOutputImageType::Pointer outputItkImage = outputimagetoitk->GetOutput();

  // create the iterators
  typename ItkInputImageType::RegionType inputRegionOfInterest = inputItkImage->GetLargestPossibleRegion();
  ItkInputImageIteratorType  inputIt( inputItkImage, inputRegionOfInterest );
  ItkOutputImageIteratorType outputIt( outputItkImage, inputRegionOfInterest );

  typename ItkOutputImageType::PixelType outsideValue;
  if(geometryClipper->m_AutoOutsideValue)
    outsideValue = itk::NumericTraits<typename ItkOutputImageType::PixelType>::min();
  else
    outsideValue = (typename ItkOutputImageType::PixelType) geometryClipper->m_OutsideValue;

  mitk::Geometry3D* inputGeometry = geometryClipper->m_InputTimeSelector->GetOutput()->GetGeometry();
  typedef itk::Index<VImageDimension> IndexType;
  Point3D indexPt; indexPt.Fill(0);
  int i, dim=IndexType::GetIndexDimension();
  Point3D pointInMM;
  bool above = geometryClipper->m_ClipPartAboveGeometry;
  bool labelBothSides = geometryClipper->GetLabelBothSides();

  if (geometryClipper->GetAutoOrientLabels())
  {
    Point3D leftMostPoint;
    leftMostPoint.Fill( std::numeric_limits<float>::min() / 2.0 );
    if(clippingGeometry2D->IsAbove(pointInMM) != above)
      {
      // invert meaning of above --> left is always the "above" side
      above = !above;
    MITK_INFO << leftMostPoint << " is BELOW geometry. Inverting meaning of above" << std::endl;
    }
    else
    MITK_INFO << leftMostPoint << " is above geometry" << std::endl;
  }

  typename ItkOutputImageType::PixelType aboveLabel = (typename ItkOutputImageType::PixelType)geometryClipper->GetAboveGeometryLabel();
  typename ItkOutputImageType::PixelType belowLabel = (typename ItkOutputImageType::PixelType)geometryClipper->GetBelowGeometryLabel();

  for ( inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt, ++outputIt)
  {
    if((typename ItkOutputImageType::PixelType)inputIt.Get() == outsideValue)
    {
      outputIt.Set(outsideValue);
    }
    else
    {
      for(i=0;i<dim;++i)
        indexPt[i]=(mitk::ScalarType)inputIt.GetIndex()[i];
      inputGeometry->IndexToWorld(indexPt, pointInMM);
      if(clippingGeometry2D->IsAbove(pointInMM) == above)
      {
        if ( labelBothSides )
          outputIt.Set( aboveLabel );
        else
          outputIt.Set( outsideValue );
      }
      else
      {
        if ( labelBothSides)
          outputIt.Set( belowLabel );
        else
          outputIt.Set( inputIt.Get() );
      }
    }
  }
}