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