fw4spl
core/fwDataTools/src/fwDataTools/Image.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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 #include "fwDataTools/Image.hpp"
8 
9 #include <fwTools/Combinatory.hpp>
10 #include <fwTools/Dispatcher.hpp>
11 #include <fwTools/DynamicTypeKeyTypeMapping.hpp>
12 #include <fwTools/IntrinsicTypes.hpp>
13 
14 namespace fwDataTools
15 {
16 
17 //------------------------------------------------------------------------------
18 
20 {
21  ::fwData::Image::sptr img;
22  ::fwData::Image::sptr roi;
23 };
24 
25 //------------------------------------------------------------------------------
26 
27 template<typename IMAGE_TYPE>
28 struct RoiApplyer
29 {
30  //------------------------------------------------------------------------------
31 
32  template<typename ROI_TYPE>
33  void operator()( RoiApplyerParam& p )
34  {
35  typedef IMAGE_TYPE ImgType;
36  typedef ROI_TYPE RoiType;
37 
38  SLM_ASSERT( "Null image pointer", p.img && p.roi);
39 
40  ::fwData::Array::sptr imgData;
41  ::fwData::Array::sptr roiData;
42  imgData = p.img->getDataArray();
43  roiData = p.roi->getDataArray();
44 
45  ::fwDataTools::helper::Array imgHelper(imgData);
46  ::fwDataTools::helper::Array roiHelper(roiData);
47  SLM_ASSERT( "Null data array pointer", imgData && roiData);
48  SLM_ASSERT( "Null data buffers", imgHelper.getBuffer() && roiHelper.getBuffer());
49 
50  ImgType* imIt = imgHelper.begin<ImgType>();
51  RoiType* roiIt = roiHelper.begin<RoiType>();
52 
53  const ImgType* imEnd = imIt + imgData->getNumberOfElements();
54 
55  for (; imIt != imEnd; ++imIt, ++roiIt)
56  {
57  if (*roiIt == 0)
58  {
59  * imIt = 0;
60  }
61  }
62  }
63 };
64 
65 //------------------------------------------------------------------------------
66 
68 {
69  //------------------------------------------------------------------------------
70 
71  template<typename IMAGE_TYPE>
72  void operator()( RoiApplyerParam& p )
73  {
75  }
76 };
77 
78 //------------------------------------------------------------------------------
79 
80 void Image::applyRoi( ::fwData::Image::sptr image, ::fwData::Image::sptr roi )
81 {
82  SLM_ASSERT( "Null image pointers", image && roi);
83  SLM_ASSERT( "Images have different size", image->getSize() == roi->getSize());
84 
85  using namespace boost;
86 
87  RoiApplyerParam param;
88  param.img = image;
89  param.roi = roi;
90 
91  // Due to link failure, we use two dispatcher calls instead of one with a cross-product type list
93 }
94 
95 //------------------------------------------------------------------------------
96 
98 {
99  ::fwData::Image::sptr img;
100  ::fwData::Image::sptr imgRoiApplyed;
101  ::fwData::Image::sptr roi;
102  bool result;
103 };
104 
105 //------------------------------------------------------------------------------
106 
107 template<typename IMAGE_TYPE>
108 struct RoiTester
109 {
110 
111  //------------------------------------------------------------------------------
112 
113  template<typename ROI_TYPE>
114  void operator()( RoiTesterParam& p )
115  {
116  bool& result = p.result;
117  result = true;
118 
119  typedef IMAGE_TYPE ImgType;
120  typedef ROI_TYPE RoiType;
121 
122  ::fwData::Array::sptr imgData;
123  ::fwData::Array::sptr imgRoiApplyedData;
124  ::fwData::Array::sptr roiData;
125 
126  imgData = p.img->getDataArray();
127  imgRoiApplyedData = p.imgRoiApplyed->getDataArray();
128  roiData = p.roi->getDataArray();
129 
130  ::fwDataTools::helper::Array imgHelper(imgData);
131  ::fwDataTools::helper::Array roiHelper(roiData);
132  ::fwDataTools::helper::Array imgRoiApplyedHelper(imgRoiApplyedData);
133 
134  SLM_ASSERT( "Null data array pointer", imgData && roiData && imgRoiApplyedData);
135  SLM_ASSERT( "Null data buffers",
136  imgHelper.getBuffer() && roiHelper.getBuffer() && imgRoiApplyedHelper.getBuffer() );
137 
138  ImgType* imIt = imgHelper.begin<ImgType>();
139  ImgType* imRoiIt = imgRoiApplyedHelper.begin<ImgType>();
140  RoiType* roiIt = roiHelper.begin<RoiType>();
141 
142  const ImgType* imEnd = imIt + imgData->getNumberOfElements();
143 
144  for (; result && imIt != imEnd; ++imIt, ++roiIt, ++imRoiIt)
145  {
146  result = result && ( (*roiIt == 0) ? (*imRoiIt == 0) : (*imIt == *imRoiIt) );
147  }
148  }
149 };
150 
151 //------------------------------------------------------------------------------
152 
154 {
155  //------------------------------------------------------------------------------
156 
157  template<typename IMAGE_TYPE>
158  void operator()( RoiTesterParam& p )
159  {
161  }
162 };
163 
164 //------------------------------------------------------------------------------
165 
166 bool Image::isRoiApplyed( ::fwData::Image::sptr image, ::fwData::Image::sptr roi, ::fwData::Image::sptr imgRoiApplyed )
167 {
168  SLM_ASSERT( "Null image pointers", image && imgRoiApplyed && roi);
169  SLM_ASSERT( "Images have different size",
170  image->getSize() == imgRoiApplyed->getSize() && image->getSize() == roi->getSize());
171 
172  RoiTesterParam param;
173  param.img = image;
174  param.imgRoiApplyed = imgRoiApplyed;
175  param.roi = roi;
176 
177  // Due to link failure, we use two dispatcher calls instead of one with a cross-product type list
179 
180  return param.result;
181 }
182 
183 //------------------------------------------------------------------------------
184 
185 } // namespace fwDataTools
The namespace fwDataTools contains classes which provide helpers to manipulate fwData::Object. *.
virtual FWDATATOOLS_API void * getBuffer()
Getter for the array buffer.
virtual FWDATATOOLS_API char * begin()
Returns the begining/end of the buffer interpreted as a char buffer.
static FWDATATOOLS_API void applyRoi(::fwData::Image::sptr image,::fwData::Image::sptr roi)
Apply roi to image.
#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
static void invoke()
Instanciate and invoke all functors.
Definition: Dispatcher.hpp:99
Create an automatic template instancier exple Dispatcher< TYPESEQUENCE , FUNCTOR>::invoke("int");.
Definition: Dispatcher.hpp:85
Helper to manage array buffer. Lock the buffer before to modify it.
static FWDATATOOLS_API bool isRoiApplyed(::fwData::Image::sptr image,::fwData::Image::sptr imgRoiApplyed,::fwData::Image::sptr roi)
Check if &#39;imgRoiApplyed&#39; is the result of &#39;roi&#39; applyed to &#39;image&#39;.