9 #include <fwDataTools/helper/Array.hpp> 10 #include <fwDataTools/helper/ImageGetter.hpp> 12 #include <fwTools/DynamicType.hpp> 15 #include <itkImageRegion.h> 22 template<
class ITKIMAGE>
23 void dataImageFactory(
typename ITKIMAGE::Pointer itkImage, ::fwData::Image::sptr _dataImage,
24 bool bufferManagerIsDataImage )
26 SLM_ASSERT(
"_dataImage not instanced", _dataImage);
29 std::uint8_t dim = ITKIMAGE::ImageDimension;
34 for (std::uint8_t d = 0; d < dim; ++d)
37 _vOrigin[d] = itkImage->GetOrigin()[d];
38 _vSize[d] = itkImage->GetBufferedRegion().GetSize()[d];
39 _vSpacing[d] = itkImage->GetSpacing()[d];
42 _dataImage->setSize( _vSize );
43 _dataImage->setOrigin( _vOrigin );
44 _dataImage->setSpacing( _vSpacing );
46 typedef typename ITKIMAGE::PixelType PixelType;
47 _dataImage->setType( ::fwTools::Type::create<PixelType>() );
48 ::fwData::Array::sptr array = _dataImage->getDataArray();
50 if( bufferManagerIsDataImage )
52 SLM_ASSERT(
"Sorry, this method requires that itkImage manages its buffer.",
53 itkImage->GetPixelContainer()->GetContainerManageMemory() );
55 static_cast<void*>(itkImage->GetBufferPointer()),
true, _dataImage->getType(), _vSize, 1,
56 ::fwMemory::BufferNewPolicy::New());
58 itkImage->GetPixelContainer()->SetContainerManageMemory(
false );
62 arrayHelper.
setBuffer( static_cast<void*>(itkImage->GetBufferPointer()),
false,
63 _dataImage->getType(), _vSize, 1 );
72 template<
class ITKIMAGE>
73 ::fwData::Image::sptr
dataImageFactory(
typename ITKIMAGE::Pointer itkImage,
bool bufferManagerIsDataImage )
75 ::fwData::Image::sptr data = ::fwData::Image::New();
76 ::fwItkIO::dataImageFactory< ITKIMAGE >(itkImage, data, bufferManagerIsDataImage);
82 template<
class ITKIMAGE_PTR >
83 void itkImageToFwDataImage( ITKIMAGE_PTR itkImage, ::fwData::Image::sptr _dataImage )
85 dataImageFactory< typename ITKIMAGE_PTR::ObjectType >(itkImage, _dataImage);
90 template<
class ITKIMAGE>
91 typename ITKIMAGE::Pointer fwDataImageToItkImage( ::fwData::Image::csptr imageData,
bool bufferManagerIsDataImage )
94 SLM_ASSERT(
"Sorry, itk image dimension not correspond to fwData image",
95 imageData->getNumberOfDimensions() == ITKIMAGE::ImageDimension );
99 typename ITKIMAGE::Pointer itkImage = ITKIMAGE::New();
102 typename ITKIMAGE::SpacingType spacing = itkImage->GetSpacing();
103 for (std::uint8_t d = 0; d < ITKIMAGE::ImageDimension; ++d)
105 spacing[d] = imageData->getSpacing()[d];
107 itkImage->SetSpacing(spacing);
110 std::copy( imageData->getOrigin().begin(),
111 imageData->getOrigin().end(),
112 const_cast< typename ITKIMAGE::PointType*
>( &itkImage->GetOrigin())->Begin()
115 itk::ImageRegion< ITKIMAGE::ImageDimension > itkRegion;
117 unsigned long nbpixels = 1;
118 for (std::uint8_t d = 0; d < ITKIMAGE::ImageDimension; ++d)
121 itkRegion.SetSize( d, static_cast<unsigned long>(imageData->getSize()[d]) );
122 nbpixels *= itkRegion.GetSize()[d];
125 itkImage->SetRegions(itkRegion);
127 if( bufferManagerIsDataImage )
129 itkImage->GetPixelContainer()->SetImportPointer(static_cast< typename ITKIMAGE::PixelType*>( imageHelper.
130 getBuffer() ), nbpixels,
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,
140 imageData->getDataArray()->setIsBufferOwner(
false );
148 template<
class ITKIMAGE>
149 typename ITKIMAGE::Pointer
itkImageFactory( ::fwData::Image::csptr imageData,
bool bufferManagerIsDataImage )
151 return fwDataImageToItkImage<ITKIMAGE>( imageData, bufferManagerIsDataImage );
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
The namespace fwItkIO contains reader, writer and helper for itk image.
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.
::fwData::Image::sptr dataImageFactory(typename ITKIMAGE::Pointer itkImage, bool bufferManagerIsDataImage=true)
Allocate a new data Image from an itk one.
::fwData::Array::SizeType SizeType
Image size type.
std::vector< double > OriginType
Image origin type.