fw4spl
MedicalImageHelpers.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #pragma once
8 
9 #include "fwDataTools/config.hpp"
10 #include "fwDataTools/helper/Image.hpp"
11 #include "fwDataTools/helper/ImageGetter.hpp"
12 
13 #include <fwData/Image.hpp>
14 #include <fwData/Integer.hpp>
15 #include <fwData/Point.hpp>
16 
17 #include <fwTools/Dispatcher.hpp>
18 #include <fwTools/DynamicTypeKeyTypeMapping.hpp>
19 #include <fwTools/IntrinsicTypes.hpp>
20 #include <fwTools/NumericRoundCast.hxx>
21 
22 #include <numeric>
23 #include <utility> // std::pair
24 
25 namespace fwDataTools
26 {
27 namespace fieldHelper
28 {
29 
30 template <class T> struct bitwise_or : std::binary_function <T, T, T>
31 {
32  // ------------------------------------------------------------------------------
33 
34  T operator() (const T& x, const T& y) const
35  {
36  return x|y;
37  }
38 };
39 
43 class FWDATATOOLS_CLASS_API MedicalImageHelpers
44 {
45 public:
46 
55  FWDATATOOLS_API static bool checkLandmarks( ::fwData::Image::sptr _pImg );
56 
65  FWDATATOOLS_API static bool checkImageValidity( ::fwData::Image::csptr _pImg );
66 
75  FWDATATOOLS_API static bool checkImageSliceIndex( ::fwData::Image::sptr _pImg );
76 
83  FWDATATOOLS_API static ::fwData::Point::sptr getImageSliceIndices( ::fwData::Image::sptr _pImg );
84 
93  FWDATATOOLS_API static bool checkComment( ::fwData::Image::sptr _pImg );
94 
104  FWDATATOOLS_API static ::fwData::Image::sptr initialize( ::fwData::Image::sptr imgSrc,
105  ::fwData::Image::sptr imgToInitialize =
106  ::fwData::Image::sptr());
107 
113  template < typename INT_INDEX>
114  static bool isPixelNull(::fwData::Image::sptr image, INT_INDEX& point);
115 
116  FWDATATOOLS_API static bool isBufNull(const ::fwData::Image::BufferType* buf, const unsigned int len);
117 
124  template < typename T, typename INT_INDEX>
125  static void setPixel(::fwData::Image::sptr image, INT_INDEX& point, T& value);
126 
133  template < typename T >
134  static void setPixel(::fwData::Image::sptr pImage, ::fwData::Point::sptr point, T& value);
135 
141  template < typename T >
142  static SPTR( ::fwData::Image::BufferType ) getPixelBufferInImageSpace(::fwData::Image::sptr image, T &value);
143 
152  template < typename MINMAXTYPE >
153  static void getMinMax(const ::fwData::Image::csptr _img, MINMAXTYPE& _min, MINMAXTYPE& _max);
154 
155 };
156 
157 // ------------------------------------------------------------------------------
158 
159 template < typename VALUE >
161 {
162 public:
163  class Param
164  {
165  public:
166  typedef VALUE ValueType;
167  typedef SPTR ( ::fwData::Image::BufferType ) BufferTypeSptr;
168 
169  Param(ValueType& v) :
170  value(v)
171  {
172  }
173 
174  const ValueType& value;
175  BufferTypeSptr res;
176  };
177 
178  // ------------------------------------------------------------------------------
179 
180  template < typename IMAGE >
181  void operator()( Param& param )
182  {
183  unsigned char imageTypeSize = sizeof(IMAGE);
184 
185  IMAGE val = ::fwTools::numericRoundCast<IMAGE>(param.value);
186 
187  ::fwData::Image::BufferType* buf = reinterpret_cast< ::fwData::Image::BufferType* > (&val);
188 
189  SPTR( ::fwData::Image::BufferType ) res( new ::fwData::Image::BufferType[imageTypeSize] );
190  std::copy(buf, buf+imageTypeSize, res.get());
191  param.res = res;
192  }
193 
194 };
195 
196 // ------------------------------------------------------------------------------
197 
198 template < typename VALUE, typename INT_INDEX >
200 {
201 public:
202  class Param
203  {
204  public:
205  typedef VALUE ValueType;
206  typedef INT_INDEX PointType;
207 
208  Param(PointType& p, ValueType& v) :
209  value(v),
210  point(p)
211  {
212  }
213 
214  ::fwData::Image::sptr image;
215  const ValueType& value;
216  const PointType& point;
217  };
218 
219  // ------------------------------------------------------------------------------
220 
221  template < typename IMAGE >
222  void operator()( Param& param )
223  {
224  ::fwDataTools::helper::Image imagehelper(param.image);
225  IMAGE* buffer = static_cast < IMAGE* > (imagehelper.getBuffer());
226  const INT_INDEX& p = param.point;
227  const ::fwData::Image::SizeType& size = param.image->getSize();
228  const int& sx = size[0];
229  const int& sy = size[1];
230  const int& offset = p[0] + sx*p[1] + p[2]*sx*sy;
231  *(buffer+offset) = ::fwTools::numericRoundCast<IMAGE>(param.value);
232  }
233 
234 };
235 
236 // ------------------------------------------------------------------------------
237 
238 template < typename T >
239 void MedicalImageHelpers::setPixel(::fwData::Image::sptr image, ::fwData::Point::sptr point, T& value)
240 {
241  setPixel(image, point->getCoord(), value);
242 }
243 
244 // ------------------------------------------------------------------------------
245 
246 template < typename T, typename INT_INDEX>
247 void MedicalImageHelpers::setPixel(::fwData::Image::sptr image, INT_INDEX& point, T& value)
248 {
249  typename CastAndSetFunctor<T, INT_INDEX>::Param param(point, value);
250  param.image = image;
251 
252  ::fwTools::DynamicType type = image->getPixelType();
254 }
255 
256 // ------------------------------------------------------------------------------
257 
258 template < typename T >
259 SPTR( ::fwData::Image::BufferType ) MedicalImageHelpers::getPixelBufferInImageSpace(::fwData::Image::sptr image,
260  T &value)
261 {
262  typename PixelCastAndSetFunctor<T>::Param param(value);
263 
264  ::fwTools::DynamicType type = image->getPixelType();
266  return param.res;
267 }
268 
269 // ------------------------------------------------------------------------------
270 
271 template < typename INT_INDEX >
273 {
274 public:
275  class Param
276  {
277  public:
278  typedef INT_INDEX PointType;
279 
280  Param(PointType& p, bool& b) :
281  point(p),
282  isNull(b)
283  {
284  }
285 
286  ::fwData::Image::sptr image;
287  const PointType& point;
288  bool& isNull;
289  };
290 
291  // ------------------------------------------------------------------------------
292 
293  template < typename IMAGE >
294  void operator()( Param& param )
295  {
296  ::fwDataTools::helper::Image imagehelper(param.image);
297  IMAGE* buffer = static_cast < IMAGE* > (imagehelper.getBuffer());
298  const INT_INDEX& p = param.point;
299  const std::vector<std::int32_t>& size = param.image->getSize();
300  const int& sx = size[0];
301  const int& sy = size[1];
302  const int& offset = p[0] + sx*p[1] + p[2]*sx*sy;
303  param.isNull = (*(buffer+offset) == 0);
304  }
305 
306 };
307 
308 // ------------------------------------------------------------------------------
309 
310 template < typename INT_INDEX>
311 bool MedicalImageHelpers::isPixelNull(::fwData::Image::sptr image, INT_INDEX& point)
312 {
313  ::fwDataTools::helper::Image imageLock( image );
314  const unsigned char imageTypeSize = image->getPixelType().sizeOf();
315  ::fwData::Image::BufferType* buf =
316  static_cast< ::fwData::Image::BufferType*> (imageLock.getPixelBuffer(point[0], point[1], point[2]));
317 
318  return isBufNull(buf, imageTypeSize);
319 }
320 
321 // ------------------------------------------------------------------------------
322 
323 template < typename T >
325 {
326 public:
327  class Param
328  {
329  public:
330 
331  Param(::fwData::Image::csptr _img, T& _min, T& _max) :
332  image(_img),
333  min(_min),
334  max(_max)
335  {
336  }
337 
338  ::fwData::Image::csptr image;
339  T& min;
340  T& max;
341  };
342 
343  // ------------------------------------------------------------------------------
344 
345  template < typename IMAGE >
346  void operator()( Param& param )
347  {
348  ::fwDataTools::helper::ImageGetter imageLock( param.image );
349  IMAGE* buffer = static_cast < IMAGE* > (imageLock.getBuffer());
350  const ::fwData::Image::SizeType& size = param.image->getSize();
351  ::fwData::Image::SizeType::value_type len = static_cast< ::fwData::Image::SizeType::value_type >(
352  std::accumulate(size.begin(), size.end(), 1, std::multiplies< ::fwData::Image::SizeType::value_type >()));
353  T& min = param.min;
354  T& max = param.max;
355 
356  typedef std::numeric_limits<IMAGE> ImgLimits;
357  IMAGE imin = ImgLimits::max();
358  IMAGE imax = ImgLimits::lowest();
359 
360  IMAGE* bufEnd = buffer + len;
361  IMAGE currentVoxel;
362 
363  for (IMAGE* voxel = buffer; voxel < bufEnd; ++voxel )
364  {
365  currentVoxel = *voxel;
366 
367  if ( currentVoxel < imin )
368  {
369  imin = currentVoxel;
370  }
371  else if (currentVoxel > imax)
372  {
373  imax = currentVoxel;
374  }
375  }
376 
377  typedef std::numeric_limits<T> TLimits;
378  T minT = TLimits::lowest();
379  T maxT = TLimits::max();
380 
381  min = ( imin < minT ) ? minT : static_cast< T > (imin);
382  max = ( imax > maxT ) ? maxT : static_cast< T > (imax);
383 
384  }
385 
386 };
387 
388 // ------------------------------------------------------------------------------
389 
390 template < typename MINMAXTYPE >
391 void MedicalImageHelpers::getMinMax(const ::fwData::Image::csptr _img, MINMAXTYPE& _min, MINMAXTYPE& _max)
392 {
393  typename MinMaxFunctor<MINMAXTYPE>::Param param(_img, _min, _max);
394 
395  ::fwTools::DynamicType type = _img->getPixelType();
397 }
398 
399 } // fieldHelper
400 } // fwDataTools
#define SPTR(_cls_)
This class contains helpers for medical image fields.
static void setPixel(::fwData::Image::sptr image, INT_INDEX &point, T &value)
Set a pixel value.
static std::shared_ptr< ::fwData::Image::BufferType > getPixelBufferInImageSpace(::fwData::Image::sptr image, T &value)
Return a buffer of image type&#39;s size, containing &#39;value&#39; casted to image data type.
The namespace fwDataTools contains classes which provide helpers to manipulate fwData::Object. *.
FWDATATOOLS_API void * getBuffer() const
Returns image buffer.
Definition: ImageGetter.cpp:41
Class defining an elementary C++ type aka unsigned char, signed char, .... signed long...
Definition: DynamicType.hpp:31
Defines an helper to modify an fwData::Image by adding few medical fields and create in parallel the ...
static bool isPixelNull(::fwData::Image::sptr image, INT_INDEX &point)
Return true if the pixel value is null.
Defines an helper to modify an fwData::Image by adding few medical fields and create in parallel the ...
Definition: ImageGetter.hpp:23
Create an automatic template instancier exple Dispatcher< TYPESEQUENCE , FUNCTOR>::invoke("int");.
Definition: Dispatcher.hpp:85
static void getMinMax(const ::fwData::Image::csptr _img, MINMAXTYPE &_min, MINMAXTYPE &_max)
Return minimum and maximum values contained in image. If image min or max value is out of MINMAXTYPE ...
FWDATATOOLS_API void * getBuffer()
Returns image buffer.
FWDATATOOLS_API void * getPixelBuffer(SizeType::value_type x, SizeType::value_type y, SizeType::value_type z)
Helpers for 3D images.