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
mitkCompositePixelValueToStringTest.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 "mitkTestingMacros.h"
18 #include <mitkTestFixture.h>
19 #include <mitkTestingConfig.h>
20 
21 #include <itkImage.h>
23 #include <mitkImage.h>
24 #include <mitkImageAccessByItk.h>
25 #include <sstream>
26 
27 class mitkCompositePixelValueToStringTestSuite : public mitk::TestFixture
28 {
29  CPPUNIT_TEST_SUITE(mitkCompositePixelValueToStringTestSuite);
30  MITK_TEST(TestConvertRGBUnsignedChar2D_CorrectResult);
31  MITK_TEST(TestConvertRGBUnsignedChar3D_CorrectResult);
32  MITK_TEST(TestConvertRGBAUnsignedChar2D_CorrectResult);
33  MITK_TEST(TestConvertRGBAUnsignedChar3D_CorrectResult);
34  MITK_TEST(TestConvertRGBLong_Exception);
35  MITK_TEST(TestConvertInt_Exception);
36  MITK_TEST(TestConvertRGBIndexOutside_NoResult);
37  MITK_TEST(TestConvertNullImage_Exception);
38 
39  CPPUNIT_TEST_SUITE_END();
40 
41 private:
42 public:
43  void setUp() override {}
44  void TestConvertRGBUnsignedChar2D_CorrectResult()
45  {
46  typedef itk::RGBPixel<unsigned char> PixelType;
47  typedef itk::Image<PixelType, 2> ImageType;
48 
49  ImageType::RegionType region;
50  ImageType::IndexType start;
51  start.Fill(0);
52 
53  ImageType::SizeType size;
54  size.Fill(1);
55 
56  region.SetSize(size);
57  region.SetIndex(start);
58 
60  image->SetRegions(region);
61  image->Allocate();
62 
63  ImageType::IndexType pixelIndex;
64  pixelIndex.Fill(0);
65 
66  PixelType rgbPixel;
67  rgbPixel.Set(0, 125, 250);
68 
69  image->SetPixel(pixelIndex, rgbPixel);
70 
72  mitkImage->InitializeByItk(image.GetPointer());
73  mitkImage->SetVolume(image->GetBufferPointer());
74 
75  std::string pixelValue;
76  itk::Index<3> index3D;
77  index3D.Fill(0);
78 
79  pixelValue = ConvertCompositePixelValueToString(mitkImage, index3D);
80 
81  CPPUNIT_ASSERT(pixelValue.compare("0 125 250") == 0);
82  }
83 
84  void TestConvertRGBUnsignedChar3D_CorrectResult()
85  {
86  typedef itk::RGBPixel<unsigned char> PixelType;
87  typedef itk::Image<PixelType, 3> ImageType;
88 
89  ImageType::RegionType region;
90  ImageType::IndexType start;
91  start.Fill(0);
92 
93  ImageType::SizeType size;
94  size.Fill(1);
95 
96  region.SetSize(size);
97  region.SetIndex(start);
98 
100  image->SetRegions(region);
101  image->Allocate();
102 
103  ImageType::IndexType pixelIndex;
104  pixelIndex.Fill(0);
105 
106  PixelType rgbPixel;
107  rgbPixel.Set(0, 125, 250);
108 
109  image->SetPixel(pixelIndex, rgbPixel);
110 
112  mitkImage->InitializeByItk(image.GetPointer());
113  mitkImage->SetVolume(image->GetBufferPointer());
114 
115  std::string pixelValue;
116  itk::Index<3> index3D;
117  index3D.Fill(0);
118 
119  pixelValue = ConvertCompositePixelValueToString(mitkImage, index3D);
120 
121  CPPUNIT_ASSERT(pixelValue.compare("0 125 250") == 0);
122  }
123 
124  void TestConvertRGBAUnsignedChar2D_CorrectResult()
125  {
126  typedef itk::RGBAPixel<unsigned char> PixelType;
127  typedef itk::Image<PixelType, 2> ImageType;
128 
129  ImageType::RegionType region;
130  ImageType::IndexType start;
131  start.Fill(0);
132 
133  ImageType::SizeType size;
134  size.Fill(1);
135 
136  region.SetSize(size);
137  region.SetIndex(start);
138 
140  image->SetRegions(region);
141  image->Allocate();
142 
143  ImageType::IndexType pixelIndex;
144  pixelIndex.Fill(0);
145 
146  PixelType rgbPixel;
147  rgbPixel.Set(0, 125, 250, 42);
148 
149  image->SetPixel(pixelIndex, rgbPixel);
150 
152  mitkImage->InitializeByItk(image.GetPointer());
153  mitkImage->SetVolume(image->GetBufferPointer());
154 
155  std::string pixelValue;
156  itk::Index<3> index3D;
157  index3D.Fill(0);
158 
159  pixelValue = ConvertCompositePixelValueToString(mitkImage, index3D);
160 
161  CPPUNIT_ASSERT(pixelValue.compare("0 125 250 42") == 0);
162  }
163 
164  void TestConvertRGBAUnsignedChar3D_CorrectResult()
165  {
166  typedef itk::RGBAPixel<unsigned char> PixelType;
167  typedef itk::Image<PixelType, 3> ImageType;
168 
169  ImageType::RegionType region;
170  ImageType::IndexType start;
171  start.Fill(0);
172 
173  ImageType::SizeType size;
174  size.Fill(1);
175 
176  region.SetSize(size);
177  region.SetIndex(start);
178 
180  image->SetRegions(region);
181  image->Allocate();
182 
183  ImageType::IndexType pixelIndex;
184  pixelIndex.Fill(0);
185 
186  PixelType rgbPixel;
187  rgbPixel.Set(0, 125, 250, 42);
188 
189  image->SetPixel(pixelIndex, rgbPixel);
190 
192  mitkImage->InitializeByItk(image.GetPointer());
193  mitkImage->SetVolume(image->GetBufferPointer());
194 
195  std::string pixelValue;
196  itk::Index<3> index3D;
197  index3D.Fill(0);
198 
199  pixelValue = ConvertCompositePixelValueToString(mitkImage, index3D);
200 
201  CPPUNIT_ASSERT(pixelValue.compare("0 125 250 42") == 0);
202  }
203 
204  void TestConvertRGBLong_Exception()
205  {
206  typedef itk::RGBAPixel<long> PixelType;
207  typedef itk::Image<PixelType, 3> ImageType;
208 
209  ImageType::RegionType region;
210  ImageType::IndexType start;
211  start.Fill(0);
212 
213  ImageType::SizeType size;
214  size.Fill(1);
215 
216  region.SetSize(size);
217  region.SetIndex(start);
218 
220  image->SetRegions(region);
221  image->Allocate();
222 
223  ImageType::IndexType pixelIndex;
224  pixelIndex.Fill(0);
225 
226  PixelType rgbPixel;
227  rgbPixel.Set(0, 125, 250, 42);
228 
229  image->SetPixel(pixelIndex, rgbPixel);
230 
232  mitkImage->InitializeByItk(image.GetPointer());
233  mitkImage->SetVolume(image->GetBufferPointer());
234 
235  std::string pixelValue;
236  itk::Index<3> index3D;
237  index3D.Fill(0);
238 
239  CPPUNIT_ASSERT_THROW(ConvertCompositePixelValueToString(mitkImage, index3D), mitk::AccessByItkException);
240  }
241 
242  void TestConvertInt_Exception()
243  {
244  typedef itk::Image<int, 3> ImageType;
245 
246  ImageType::RegionType region;
247  ImageType::IndexType start;
248  start.Fill(0);
249 
250  ImageType::SizeType size;
251  size.Fill(1);
252 
253  region.SetSize(size);
254  region.SetIndex(start);
255 
257  image->SetRegions(region);
258  image->Allocate();
259 
260  ImageType::IndexType pixelIndex;
261  pixelIndex.Fill(0);
262 
263  int pixel = 21;
264 
265  image->SetPixel(pixelIndex, pixel);
266 
268  mitkImage->InitializeByItk(image.GetPointer());
269  mitkImage->SetVolume(image->GetBufferPointer());
270 
271  std::string pixelValue;
272  itk::Index<3> index3D;
273  index3D.Fill(0);
274 
275  CPPUNIT_ASSERT_THROW(ConvertCompositePixelValueToString(mitkImage, index3D), mitk::AccessByItkException);
276  }
277 
278  void TestConvertRGBIndexOutside_NoResult()
279  {
280  typedef itk::RGBPixel<unsigned char> PixelType;
281  typedef itk::Image<PixelType, 2> ImageType;
282 
283  ImageType::RegionType region;
284  ImageType::IndexType start;
285  start.Fill(0);
286 
287  ImageType::SizeType size;
288  size.Fill(1);
289 
290  region.SetSize(size);
291  region.SetIndex(start);
292 
294  image->SetRegions(region);
295  image->Allocate();
296 
297  ImageType::IndexType pixelIndex;
298  pixelIndex.Fill(0);
299 
300  PixelType rgbPixel;
301  rgbPixel.Set(0, 125, 250);
302 
303  image->SetPixel(pixelIndex, rgbPixel);
304 
306  mitkImage->InitializeByItk(image.GetPointer());
307  mitkImage->SetVolume(image->GetBufferPointer());
308 
309  std::string pixelValue;
310  itk::Index<3> index3D;
311  index3D.Fill(5);
312 
313  pixelValue = ConvertCompositePixelValueToString(mitkImage, index3D);
314 
315  CPPUNIT_ASSERT(pixelValue.compare("Out of bounds") == 0);
316  }
317 
318  void TestConvertNullImage_Exception()
319  {
320  mitk::Image::Pointer mitkImage;
321 
322  std::string pixelValue;
323  itk::Index<3> index3D;
324  index3D.Fill(0);
325 
326  CPPUNIT_ASSERT_THROW(ConvertCompositePixelValueToString(mitkImage, index3D), mitk::Exception);
327  }
328 };
329 
330 MITK_TEST_SUITE_REGISTRATION(mitkCompositePixelValueToString)
itk::SmartPointer< Self > Pointer
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
std::string MITKCORE_EXPORT ConvertCompositePixelValueToString(Image::Pointer image, itk::Index< 3 > index)
Converts composite pixel values to a displayable string.
map::core::discrete::Elements< 3 >::InternalImageType ImageType
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
Test fixture for parameterized tests.
static Pointer New()
Exception class thrown in AccessByItk macros.
unsigned short PixelType
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.