fw4spl
itk.hxx
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/helper/Array.hpp>
10 #include <fwDataTools/helper/ImageGetter.hpp>
11 
12 #include <fwTools/DynamicType.hpp>
13 
14 #include <itkImage.h>
15 #include <itkImageRegion.h>
16 
17 namespace fwItkIO
18 {
19 
20 //------------------------------------------------------------------------------
21 
22 template< class ITKIMAGE>
23 void dataImageFactory( typename ITKIMAGE::Pointer itkImage, ::fwData::Image::sptr _dataImage,
24  bool bufferManagerIsDataImage )
25 {
26  SLM_ASSERT("_dataImage not instanced", _dataImage);
27 
28  // Add by arnaud
29  std::uint8_t dim = ITKIMAGE::ImageDimension;
30  ::fwData::Image::SpacingType _vSpacing(dim, 1);
31  ::fwData::Image::OriginType _vOrigin(dim, 0);
32  ::fwData::Image::SizeType _vSize(dim, 0);
33 
34  for (std::uint8_t d = 0; d < dim; ++d)
35  {
36  // _vOrigin[d] = itkImage->GetBufferedRegion().GetIndex()[d];
37  _vOrigin[d] = itkImage->GetOrigin()[d];
38  _vSize[d] = itkImage->GetBufferedRegion().GetSize()[d];
39  _vSpacing[d] = itkImage->GetSpacing()[d];
40  }
41 
42  _dataImage->setSize( _vSize );
43  _dataImage->setOrigin( _vOrigin );
44  _dataImage->setSpacing( _vSpacing );
45 
46  typedef typename ITKIMAGE::PixelType PixelType;
47  _dataImage->setType( ::fwTools::Type::create<PixelType>() );
48  ::fwData::Array::sptr array = _dataImage->getDataArray();
49  ::fwDataTools::helper::Array arrayHelper(array);
50  if( bufferManagerIsDataImage )
51  {
52  SLM_ASSERT("Sorry, this method requires that itkImage manages its buffer.",
53  itkImage->GetPixelContainer()->GetContainerManageMemory() );
54  arrayHelper.setBuffer(
55  static_cast<void*>(itkImage->GetBufferPointer()), true, _dataImage->getType(), _vSize, 1,
56  ::fwMemory::BufferNewPolicy::New());
58  itkImage->GetPixelContainer()->SetContainerManageMemory( false );
59  }
60  else
61  {
62  arrayHelper.setBuffer( static_cast<void*>(itkImage->GetBufferPointer()), false,
63  _dataImage->getType(), _vSize, 1 );
64  }
65 
66  // Post Condition correct PixelType
67  SLM_ASSERT("Sorry, pixel type is not correct", _dataImage->getType() != fwTools::Type() );
68 }
69 
70 //------------------------------------------------------------------------------
71 
72 template< class ITKIMAGE>
73 ::fwData::Image::sptr dataImageFactory( typename ITKIMAGE::Pointer itkImage, bool bufferManagerIsDataImage )
74 {
75  ::fwData::Image::sptr data = ::fwData::Image::New();
76  ::fwItkIO::dataImageFactory< ITKIMAGE >(itkImage, data, bufferManagerIsDataImage);
77  return data;
78 }
79 
80 //------------------------------------------------------------------------------
81 
82 template< class ITKIMAGE_PTR >
83 void itkImageToFwDataImage( ITKIMAGE_PTR itkImage, ::fwData::Image::sptr _dataImage )
84 {
85  dataImageFactory< typename ITKIMAGE_PTR::ObjectType >(itkImage, _dataImage);
86 }
87 
88 //------------------------------------------------------------------------------
89 
90 template< class ITKIMAGE>
91 typename ITKIMAGE::Pointer fwDataImageToItkImage( ::fwData::Image::csptr imageData, bool bufferManagerIsDataImage )
92 {
93  // Pre Condition
94  SLM_ASSERT("Sorry, itk image dimension not correspond to fwData image",
95  imageData->getNumberOfDimensions() == ITKIMAGE::ImageDimension );
96 
97  ::fwDataTools::helper::ImageGetter imageHelper(imageData);
98 
99  typename ITKIMAGE::Pointer itkImage = ITKIMAGE::New();
100 
101  // update spacing information ; workaround due to GetSpacing const
102  typename ITKIMAGE::SpacingType spacing = itkImage->GetSpacing();
103  for (std::uint8_t d = 0; d < ITKIMAGE::ImageDimension; ++d)
104  {
105  spacing[d] = imageData->getSpacing()[d];
106  }
107  itkImage->SetSpacing(spacing);
108 
109  // update origin information ; workaround due to GetOrigin const
110  std::copy( imageData->getOrigin().begin(),
111  imageData->getOrigin().end(),
112  const_cast< typename ITKIMAGE::PointType* >( &itkImage->GetOrigin())->Begin()
113  );
114 
115  itk::ImageRegion< ITKIMAGE::ImageDimension > itkRegion;
116 
117  unsigned long nbpixels = 1;
118  for (std::uint8_t d = 0; d < ITKIMAGE::ImageDimension; ++d)
119  {
120  // itkRegion.SetIndex( d, static_cast<int>(imageData->getOrigin()[d]) );
121  itkRegion.SetSize( d, static_cast<unsigned long>(imageData->getSize()[d]) );
122  nbpixels *= itkRegion.GetSize()[d];
123  }
124 
125  itkImage->SetRegions(itkRegion);
126 
127  if( bufferManagerIsDataImage )
128  {
129  itkImage->GetPixelContainer()->SetImportPointer(static_cast< typename ITKIMAGE::PixelType*>( imageHelper.
130  getBuffer() ), nbpixels,
131  false );
132  }
133  else
134  {
135  SLM_ASSERT("Sorry, this method requires that imageData manages its buffer.",
136  imageData->getDataArray()->getIsBufferOwner() );
137  itkImage->GetPixelContainer()->SetImportPointer(static_cast< typename ITKIMAGE::PixelType*>( imageHelper.
138  getBuffer() ), nbpixels,
139  true );
140  imageData->getDataArray()->setIsBufferOwner( false );
141  }
142 
143  return itkImage;
144 }
145 
146 //------------------------------------------------------------------------------
147 
148 template< class ITKIMAGE>
149 typename ITKIMAGE::Pointer itkImageFactory( ::fwData::Image::csptr imageData, bool bufferManagerIsDataImage )
150 {
151  return fwDataImageToItkImage<ITKIMAGE>( imageData, bufferManagerIsDataImage );
152 }
153 
154 //------------------------------------------------------------------------------
155 
156 } // end namespace
FWDATATOOLS_API void setBuffer(void *buf, bool takeOwnership, const ::fwTools::Type &type, const ::fwData::Array::SizeType &size, size_t nbOfComponents,::fwMemory::BufferAllocationPolicy::sptr policy=::fwMemory::BufferMallocPolicy::New())
Setter for the array buffer.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
Class describing an elementary C++ type aka unsigned char, signed char, .... int, float...
Definition: Type.hpp:32
The namespace fwItkIO contains reader, writer and helper for itk image.
Defines an helper to modify an fwData::Image by adding few medical fields and create in parallel the ...
Definition: ImageGetter.hpp:23
std::vector< double > SpacingType
Image spacing type.
ITKIMAGE::Pointer itkImageFactory(::fwData::Image::csptr imageData, bool bufferManagerIsDataImage=true)
Create an ITK image from an fwData::Image.
Definition: itk.hxx:149
::fwData::Image::sptr dataImageFactory(typename ITKIMAGE::Pointer itkImage, bool bufferManagerIsDataImage=true)
Allocate a new data Image from an itk one.
Definition: itk.hxx:73
Helper to manage array buffer. Lock the buffer before to modify it.
::fwData::Array::SizeType SizeType
Image size type.
std::vector< double > OriginType
Image origin type.