Medical Imaging Interaction Toolkit  2018.4.99-c4b6bb11
Medical Imaging Interaction Toolkit
mitkCompressedImageContainerTest.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 
14 #include "mitkCoreObjectFactory.h"
15 #include "mitkIOUtil.h"
16 #include "mitkImageDataItem.h"
17 #include "mitkImageReadAccessor.h"
18 
19 class mitkCompressedImageContainerTestClass
20 {
21 public:
22  static void Test(mitk::CompressedImageContainer *container, mitk::Image *image, unsigned int &numberFailed)
23  {
24  container->SetImage(image); // compress
25  mitk::Image::Pointer uncompressedImage = container->GetImage(); // uncompress
26 
27  // check dimensions
28  if (image->GetDimension() != uncompressedImage->GetDimension())
29  {
30  ++numberFailed;
31  std::cerr << " (EE) Number of image dimensions wrong after uncompression (was: " << image->GetDimension()
32  << ", now: " << uncompressedImage->GetDimension() << ")" << std::endl;
33  }
34 
35  for (unsigned int dim = 0; dim < image->GetDimension(); ++dim)
36  {
37  if (image->GetDimension(dim) != uncompressedImage->GetDimension(dim))
38  {
39  ++numberFailed;
40  std::cerr << " (EE) Image dimension " << dim
41  << " differs after uncompression (was: " << image->GetDimension(dim)
42  << ", now: " << uncompressedImage->GetDimension(dim) << ")" << std::endl;
43  }
44  }
45 
46  // check pixel type
47  if (image->GetPixelType() != uncompressedImage->GetPixelType())
48  {
49  ++numberFailed;
50  std::cerr << " (EE) Pixel type wrong after uncompression:" << std::endl;
51 
52  mitk::PixelType m_PixelType = image->GetPixelType();
53  std::cout << "Original pixel type:" << std::endl;
54  std::cout << " PixelType: " << m_PixelType.GetTypeAsString() << std::endl;
55  std::cout << " BitsPerElement: " << m_PixelType.GetBpe() << std::endl;
56  std::cout << " NumberOfComponents: " << m_PixelType.GetNumberOfComponents() << std::endl;
57  std::cout << " BitsPerComponent: " << m_PixelType.GetBitsPerComponent() << std::endl;
58 
59  // m_PixelType = uncompressedImage->GetPixelType();
60  std::cout << "Uncompressed pixel type:" << std::endl;
61  std::cout << " PixelType: " << uncompressedImage->GetPixelType().GetTypeAsString() << std::endl;
62  std::cout << " BitsPerElement: " << uncompressedImage->GetPixelType().GetBpe() << std::endl;
63  std::cout << " NumberOfComponents: " << uncompressedImage->GetPixelType().GetNumberOfComponents() << std::endl;
64  std::cout << " BitsPerComponent: " << uncompressedImage->GetPixelType().GetBitsPerComponent() << std::endl;
65  }
66 
67  // check data
68  mitk::PixelType m_PixelType = image->GetPixelType();
69  unsigned long oneTimeStepSizeInBytes = m_PixelType.GetBpe() >> 3; // bits per element divided by 8
70  for (unsigned int dim = 0; dim < image->GetDimension(); ++dim)
71  {
72  if (dim < 3)
73  {
74  oneTimeStepSizeInBytes *= image->GetDimension(dim);
75  }
76  }
77 
78  unsigned int numberOfTimeSteps(1);
79  if (image->GetDimension() > 3)
80  {
81  numberOfTimeSteps = image->GetDimension(3);
82  }
83 
84  for (unsigned int timeStep = 0; timeStep < numberOfTimeSteps; ++timeStep)
85  {
86  mitk::ImageReadAccessor origImgAcc(image, image->GetVolumeData(timeStep));
87  mitk::ImageReadAccessor unCompImgAcc(uncompressedImage, uncompressedImage->GetVolumeData(timeStep));
88 
89  auto *originalData((unsigned char *)origImgAcc.GetData());
90  auto *uncompressedData((unsigned char *)unCompImgAcc.GetData());
91 
92  unsigned long difference(0);
93  for (unsigned long byte = 0; byte < oneTimeStepSizeInBytes; ++byte)
94  {
95  if (originalData[byte] != uncompressedData[byte])
96  {
97  ++difference;
98  }
99  }
100 
101  if (difference > 0)
102  {
103  ++numberFailed;
104  std::cerr << " (EE) Pixel data in timestep " << timeStep << " not identical after uncompression. "
105  << difference << " pixels different." << std::endl;
106  break; // break "for timeStep"
107  }
108  }
109  }
110 };
111 
113 int mitkCompressedImageContainerTest(int argc, char *argv[])
114 {
115  // one big variable to tell if anything went wrong
116  unsigned int numberFailed(0);
117 
118  // need one parameter (image filename)
119  if (argc == 0)
120  {
121  std::cerr << "No file specified [FAILED]" << std::endl;
122  return EXIT_FAILURE;
123  }
124 
125  // load the image
126 
127  mitk::Image::Pointer image = nullptr;
128  try
129  {
130  std::cout << "Testing with parameter '" << argv[1] << "'" << std::endl;
131  image = mitk::IOUtil::Load<mitk::Image>(argv[1]);
132  }
133  catch (const mitk::Exception &)
134  {
135  std::cout << "File not an image - test will not be applied [PASSED]" << std::endl;
136  std::cout << "[TEST DONE]" << std::endl;
137  return EXIT_SUCCESS;
138  }
139  catch (itk::ExceptionObject &ex)
140  {
141  ++numberFailed;
142  std::cerr << "Exception: " << ex << "[FAILED]" << std::endl;
143  return EXIT_FAILURE;
144  }
145 
146  std::cout << " (II) Could load image." << std::endl;
147  std::cout << "Testing instantiation" << std::endl;
148 
149  // instantiation
150  mitk::CompressedImageContainer::Pointer container = mitk::CompressedImageContainer::New();
151  if (container.IsNotNull())
152  {
153  std::cout << " (II) Instantiation works." << std::endl;
154  }
155  else
156  {
157  ++numberFailed;
158  std::cout << "Test failed, and it's the ugliest one!" << std::endl;
159  return EXIT_FAILURE;
160  }
161 
162  // some real work
163  mitkCompressedImageContainerTestClass::Test(container, image, numberFailed);
164 
165  std::cout << "Testing destruction" << std::endl;
166 
167  // freeing
168  container = nullptr;
169 
170  std::cout << " (II) Freeing works." << std::endl;
171 
172  if (numberFailed > 0)
173  {
174  std::cerr << numberFailed << " tests failed." << std::endl;
175  return EXIT_FAILURE;
176  }
177  else
178  {
179  std::cout << "PASSED all tests." << std::endl;
180  return EXIT_SUCCESS;
181  }
182 }
vcl_size_t GetBitsPerComponent() const
Get the number of bits per components.
std::string GetTypeAsString() const
Returns a string representing the pixel type and pixel components.
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Definition: mitkImage.cpp:101
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
void SetImage(Image *)
Creates a compressed version of the image.
Holds one (compressed) mitk::Image.
vcl_size_t GetBpe() const
Get the number of bits per element (of an element)
Image::Pointer GetImage()
Creates a full mitk::Image from its compressed version.
unsigned int GetDimension() const
Get dimension of the image.
Definition: mitkImage.cpp:106
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
virtual ImageDataItemPointer GetVolumeData(int t=0, int n=0, void *data=nullptr, ImportMemoryManagementType importMemoryManagement=CopyMemory) const
Definition: mitkImage.cpp:326
Image class for storing images.
Definition: mitkImage.h:72
int mitkCompressedImageContainerTest(int argc, char *argv[])
ctest entry point
mitk::Image::Pointer image
ImageReadAccessor class to get locked read access for a particular image part.
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51