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