Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkPixelTypeList.h
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 
24 #ifndef mitkPixelTypeList_h
25 #define mitkPixelTypeList_h
26 
27 #include <stdexcept>
28 
29 namespace mitk
30 {
34  struct EmptyType
35  {
36  };
37 
46  template <typename T0 = EmptyType,
47  typename T1 = EmptyType,
48  typename T2 = EmptyType,
49  typename T3 = EmptyType,
50  typename T4 = EmptyType,
51  typename T5 = EmptyType,
52  typename T6 = EmptyType,
53  typename T7 = EmptyType,
54  typename T8 = EmptyType,
55  typename T9 = EmptyType>
56  struct PixelTypeList;
57 
58  template <typename T0,
59  typename T1,
60  typename T2,
61  typename T3,
62  typename T4,
63  typename T5,
64  typename T6,
65  typename T7,
66  typename T8,
67  typename T9>
69  {
70  typedef T0 head;
72  enum
73  {
74  length = tail::length + 1
75  };
76  };
77 
78  template <>
80  EmptyType,
81  EmptyType,
82  EmptyType,
83  EmptyType,
84  EmptyType,
85  EmptyType,
86  EmptyType,
87  EmptyType,
88  EmptyType>
89  {
90  enum
91  {
92  length = 0
93  };
94  };
95 
101  template <typename TypeList>
103  {
104  enum
105  {
106  value = TypeList::length
107  };
108  };
109 
122  template <typename TypeList,
123  int Index, // requested element index
124  int Step = 0, // current recursion step
125  bool Stop = (Index == Step), // stop recursion flag
126  bool OutOfRange = PixelTypeLength<TypeList>::value == 0 // out of range flag
127  >
129  {
131  };
132 
133  //"out of range" specialization
134  template <typename TypeList, int Index, int Step, bool Stop>
136  {
137  // if OutOfRange is 'true' the 'type' is undefined
138  // so we'll get a compile-time error
139  };
140 
141  //"element found" specialization
142  template <typename TypeList, int Index, int Step, bool OutOfRange>
144  {
145  // the index is equal to the recursion step
146  // so the result type is the head of the Typlist and stop!
147  typedef typename TypeList::head type;
148  };
149 
151  // run-time type switch
152 
165  template <typename TypeList, int Index = 0, bool Stop = (Index == PixelTypeLength<TypeList>::value)>
166  struct PixelTypeSwitch;
167 
168  template <typename TypeList, int Index, bool Stop>
170  {
171  template <typename F>
172  bool operator()(int i, F &f)
173  {
174  if (i == Index)
175  {
176  return f.operator()<typename GetPixelType<TypeList, Index>::type>();
177  }
178  else
179  {
181  return next(i, f);
182  }
183  }
184  };
185 
186  template <typename TypeList, int Index>
187  struct PixelTypeSwitch<TypeList, Index, true>
188  {
189  template <typename F>
190  bool operator()(int, F &)
191  {
192  throw std::out_of_range("Index out of range");
193  }
194  };
195 
209  template <typename X, int VDimension, typename T1 = EmptyType, typename T2 = EmptyType, typename T3 = EmptyType>
211  {
212  typedef void (*CallBack)(T1, T2, T3);
213 
215  X *cl, CallBack callBack, const mitk::Image *mitkImage, T1 t1 = T1(), T2 t2 = T2(), T3 t3 = T3())
216  : cl(cl), callBack(callBack), mitkImage(mitkImage), pixelType(mitkImage->GetPixelType()), t1(t1), t2(t2), t3(t3)
217  {
218  }
219 
220  template <typename PixelType>
221  bool operator()()
222  {
223  if (pixelType != typeid(PixelType))
224  return false;
225  if (mitkImage->GetDimension() != VDimension)
226  return false;
227 
228  const_cast<mitk::Image *>(mitkImage)->Update();
229 
230  typedef itk::Image<PixelType, VDimension> ImageType;
231  typedef mitk::ImageToItk<ImageType> ImageToItkType;
232  itk::SmartPointer<ImageToItkType> imagetoitk = ImageToItkType::New();
233  imagetoitk->SetInput(mitkImage);
234  imagetoitk->Update();
235  cl->*callBack(imagetoitk->GetOutput(), t1, t2, t3);
236  return true;
237  }
238 
239  private:
240  X *cl;
241  CallBack callBack;
242 
243  const mitk::Image *mitkImage;
244  const mitk::PixelType &pixelType;
245  T1 t1;
246  T2 t2;
247  T3 t3;
248  };
249 
250 }
251 
252 #endif
Image class for storing multi-dimensional medical image data.
Definition: mitkImage.h:76
unsigned int GetDimension() const
Get the number of dimensions of the image.
Describes the data type of image pixels.
Definition: mitkPixelType.h:75
Find image slices visible on a given plane.
Functor that converts a mitk::Image to an itk::Image and invokes a callback.
void(* CallBack)(T1, T2, T3)
AccessItkImageFunctor(X *cl, CallBack callBack, const mitk::Image *mitkImage, T1 t1=T1(), T2 t2=T2(), T3 t3=T3())
Sentinel type used to mark unused slots in a PixelTypeList.
Compile-time accessor that retrieves the type at position Index in a PixelTypeList.
GetPixelType< typename TypeList::tail, Index, Step+1 >::type type
Compile-time query for the number of types in a PixelTypeList.
Compile-time list of pixel types (up to 10 types).
PixelTypeList< T1, T2, T3, T4, T5, T6, T7, T8, T9 > tail
Run-time type switch over a PixelTypeList.
bool operator()(int i, F &f)