Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkImageCastTest.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkTestFixture.h"
18 #include "mitkTestingMacros.h"
19 
20 #include "mitkImageCast.h"
21 #include "mitkImageToItk.h"
22 
23 class mitkImageCastTestSuite : public mitk::TestFixture
24 {
25  CPPUNIT_TEST_SUITE(mitkImageCastTestSuite);
26  MITK_TEST(Cast_ToMultipleConstItkImages_Succeeds);
27  MITK_TEST(Cast_ToMultipleNonConstItkImages_Fails);
28  CPPUNIT_TEST_SUITE_END();
29 
30 private:
37  mitk::Image::Pointer CreateTestImageFixedValue(const mitk::PixelType &pixelType, unsigned int size)
38  {
40  unsigned int dimensions[] = {size, size, size};
41  mitkImage->Initialize(pixelType, 3, dimensions);
42  return mitkImage;
43  }
44 
45 public:
46  void Cast_ToMultipleConstItkImages_Succeeds()
47  {
48  mitk::PixelType pixelType = mitk::MakeScalarPixelType<short>();
49  mitk::Image::ConstPointer mitkConstImage(CreateTestImageFixedValue(pixelType, 3).GetPointer());
50  CPPUNIT_ASSERT(mitkConstImage.IsNotNull());
51 
52  typedef itk::Image<short, 3> ItkImageType;
53  // This locks the MITK image for reading
55  mitk::ImageToItkImage<ItkImageType::PixelType, ItkImageType::ImageDimension>(mitkConstImage);
56 
57  // Trying to get another const itk::Image should work
58  CPPUNIT_ASSERT((mitk::ImageToItkImage<ItkImageType::PixelType, ItkImageType::ImageDimension>(mitkConstImage)));
59  }
60 
61  void Cast_ToMultipleNonConstItkImages_Fails()
62  {
63  mitk::PixelType pixelType = mitk::MakeScalarPixelType<short>();
64  mitk::Image::Pointer mitkImage = CreateTestImageFixedValue(pixelType, 3);
65  mitk::Image::ConstPointer mitkConstImage(mitkImage.GetPointer());
66  CPPUNIT_ASSERT(mitkImage.IsNotNull());
67 
68  typedef itk::Image<short, 3> ItkImageType;
69  // This locks the MITK image for reading (we use the const image)
71  mitk::ImageToItkImage<ItkImageType::PixelType, ItkImageType::ImageDimension>(mitkConstImage);
72 
73  // Using the non-const MITK image to convert to ITK should fail
74  CPPUNIT_ASSERT_THROW((mitk::ImageToItkImage<ItkImageType::PixelType, ItkImageType::ImageDimension>(mitkImage)),
76  }
77 };
78 
79 MITK_TEST_SUITE_REGISTRATION(mitkImageCast)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
itk::SmartPointer< const Self > ConstPointer
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
itk::Image< TPixel, VDimension >::Pointer CreateTestImageFixedValue(vcl_size_t size, TPixel value)
Test fixture for parameterized tests.
static Pointer New()
Class for defining the data type of pixels.
Definition: mitkPixelType.h:55