fw4spl
core/fwData/src/fwData/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 "fwData/Image.hpp"
8 
9 #include "fwData/Exception.hpp"
10 #include "fwData/registry/macros.hpp"
11 
12 #include <fwCom/Signal.hxx>
13 
14 #include <fwTools/DynamicType.hpp>
15 #include <fwTools/DynamicTypeKeyTypeMapping.hpp>
16 
17 #include <boost/assign.hpp>
18 
19 #include <numeric>
20 
21 //------------------------------------------------------------------------------
22 
23 fwDataRegisterMacro( ::fwData::Image );
24 
25 //------------------------------------------------------------------------------
26 
27 namespace fwData
28 {
29 
30 const ::fwCom::Signals::SignalKeyType Image::s_BUFFER_MODIFIED_SIG = "bufferModified";
31 const ::fwCom::Signals::SignalKeyType Image::s_LANDMARK_ADDED_SIG = "landmarkAdded";
32 const ::fwCom::Signals::SignalKeyType Image::s_LANDMARK_REMOVED_SIG = "landmarkRemoved";
33 const ::fwCom::Signals::SignalKeyType Image::s_LANDMARK_DISPLAYED_SIG = "landmarkDisplayed";
34 const ::fwCom::Signals::SignalKeyType Image::s_DISTANCE_ADDED_SIG = "distanceAdded";
35 const ::fwCom::Signals::SignalKeyType Image::s_DISTANCE_REMOVED_SIG = "distanceRemoved";
36 const ::fwCom::Signals::SignalKeyType Image::s_DISTANCE_DISPLAYED_SIG = "distanceDisplayed";
37 const ::fwCom::Signals::SignalKeyType Image::s_SLICE_INDEX_MODIFIED_SIG = "sliceIndexModified";
38 const ::fwCom::Signals::SignalKeyType Image::s_SLICE_TYPE_MODIFIED_SIG = "sliceTypeModified";
39 const ::fwCom::Signals::SignalKeyType Image::s_VISIBILITY_MODIFIED_SIG = "visibilityModified";
40 const ::fwCom::Signals::SignalKeyType Image::s_TRANSPARENCY_MODIFIED_SIG = "transparencyModified";
41 
42 //------------------------------------------------------------------------------
43 
45  m_type(),
46  m_windowCenter(0.),
47  m_windowWidth(0.),
48  m_numberOfComponents(1),
49  m_dataArray( ::fwData::Array::New() )
50 {
51  newSignal< BufferModifiedSignalType >(s_BUFFER_MODIFIED_SIG);
52  newSignal< LandmarkAddedSignalType >(s_LANDMARK_ADDED_SIG);
53  newSignal< LandmarkRemovedSignalType >(s_LANDMARK_REMOVED_SIG);
54  newSignal< LandmarkDisplayedSignalType >(s_LANDMARK_DISPLAYED_SIG);
55  newSignal< DistanceDisplayedSignalType >(s_DISTANCE_DISPLAYED_SIG);
56  newSignal< DistanceAddedSignalType >(s_DISTANCE_ADDED_SIG);
57  newSignal< DistanceRemovedSignalType >(s_DISTANCE_REMOVED_SIG);
58  newSignal< SliceIndexModifiedSignalType >(s_SLICE_INDEX_MODIFIED_SIG);
59  newSignal< SliceTypeModifiedSignalType >(s_SLICE_TYPE_MODIFIED_SIG);
60  newSignal< VisibilityModifiedSignalType >(s_VISIBILITY_MODIFIED_SIG);
61  newSignal< TransparencyModifiedSignalType >(s_TRANSPARENCY_MODIFIED_SIG);
62 }
63 
64 //------------------------------------------------------------------------------
65 
66 Image::~Image() noexcept
67 {
69 }
70 
71 //-----------------------------------------------------------------------------
72 
73 void Image::shallowCopy(const Object::csptr& _source )
74 {
75  Image::csptr other = Image::dynamicConstCast(_source);
76  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
77  "Unable to copy" + (_source ? _source->getClassname() : std::string("<NULL>"))
78  + " to " + this->getClassname()), !bool(other) );
79  this->fieldShallowCopy( _source );
80 
81  // Assign
82  copyInformation( other );
83 
84  m_dataArray = other->m_dataArray;
85 }
86 
87 //-----------------------------------------------------------------------------
88 
89 void Image::cachedDeepCopy(const Object::csptr& _source, DeepCopyCacheType& cache)
90 {
91  Image::csptr other = Image::dynamicConstCast(_source);
92  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
93  "Unable to copy" + (_source ? _source->getClassname() : std::string("<NULL>"))
94  + " to " + this->getClassname()), !bool(other) );
95  this->fieldDeepCopy( _source, cache );
96 
97  // Assign
98  copyInformation( other );
99 
100  if( other->m_dataArray )
101  {
102  m_dataArray = ::fwData::Object::copy(other->m_dataArray, cache);
103  }
104 }
105 
106 //------------------------------------------------------------------------------
107 
108 ::fwData::Array::sptr Image::getDataArray() const
109 {
110  return m_dataArray;
111 }
112 
113 //------------------------------------------------------------------------------
114 
115 void Image::setDataArray(::fwData::Array::sptr array, bool copyArrayInfo)
116 {
117  if( !array )
118  {
119  array = ::fwData::Array::New();
120  }
121  m_dataArray = array;
122  if (copyArrayInfo)
123  {
124  m_size = array->getSize();
125  m_type = array->getType();
126  }
127 }
128 
129 //------------------------------------------------------------------------------
130 
132 {
133  if (!m_dataArray)
134  {
135  m_dataArray = ::fwData::Array::New();
136  }
137 
138  SLM_ASSERT( "NumberOfComponents must be > 0", m_numberOfComponents > 0 );
139  return m_dataArray->resize(m_type, m_size, m_numberOfComponents, true);
140 }
141 
142 //------------------------------------------------------------------------------
143 
144 size_t Image::allocate(SizeType::value_type x, SizeType::value_type y, SizeType::value_type z,
145  const ::fwTools::Type& type, size_t numberOfComponents)
146 {
147  m_size = { x, y, z};
148  m_type = type;
149  m_numberOfComponents = numberOfComponents;
150  return allocate();
151 }
152 
153 //------------------------------------------------------------------------------
154 
155 size_t Image::allocate(const SizeType& size, const ::fwTools::Type& type, size_t numberOfComponents)
156 {
157  m_size = size;
158  m_type = type;
159  m_numberOfComponents = numberOfComponents;
160  return allocate();
161 }
162 
163 //------------------------------------------------------------------------------
164 
166 {
167  typedef std::map<std::string, ::fwTools::DynamicType> DynamicTypeMapType;
168 
169  static DynamicTypeMapType dynamicTypeMap = ::boost::assign::map_list_of
170  (::fwTools::Type().string(), ::fwTools::DynamicType() )
171  ("uint8", ::fwTools::makeDynamicType<std::string>("unsigned char") )
172  ("uint16",
173  ::fwTools::makeDynamicType<std::string>("unsigned short") )
174  ("uint32",
175  ::fwTools::makeDynamicType<std::string>("unsigned int") )
176  ("int8", ::fwTools::makeDynamicType<std::string>("signed char") )
177  ("int16",
178  ::fwTools::makeDynamicType<std::string>("signed short") )
179  ("int32",
180  ::fwTools::makeDynamicType<std::string>("signed int") )
181  ("float",
182  ::fwTools::makeDynamicType<std::string>("float") )
183  ("double",
184  ::fwTools::makeDynamicType<std::string>("double") )
185 
186 //special case for dynamic type : 64bits integers was not managed by dynamic type.
187 #if ( INT_MAX < LONG_MAX )
188  ("uint64", ::fwTools::makeDynamicType<std::string>("unsigned long") )
189  ("int64",
190  ::fwTools::makeDynamicType<std::string>("signed long") )
191 #else
192  ("uint32", ::fwTools::makeDynamicType<std::string>("unsigned long") )
193  ("int32",
194  ::fwTools::makeDynamicType<std::string>("signed long") )
195  ("uint64", ::fwTools::DynamicType() )
196  ("int64", ::fwTools::DynamicType() )
197 #endif
198  ;
199 
200  ::fwTools::DynamicType dtype = dynamicTypeMap[getType().string()];
201  return dtype;
202 }
203 
204 //------------------------------------------------------------------------------
205 
207 {
208  return m_type;
209 }
210 
211 //------------------------------------------------------------------------------
212 
214 {
215  m_type = type;
216 }
217 
218 //------------------------------------------------------------------------------
219 
220 void Image::setType(const std::string& type)
221 {
222  m_type = ::fwTools::Type(type);
223 }
224 
225 //------------------------------------------------------------------------------
226 
227 void Image::copyInformation( Image::csptr _source )
228 {
229  m_size = _source->m_size;
230  m_type = _source->m_type;
231  m_spacing = _source->m_spacing;
232  m_origin = _source->m_origin;
233  m_windowCenter = _source->m_windowCenter;
234  m_windowWidth = _source->m_windowWidth;
235  m_numberOfComponents = _source->m_numberOfComponents;
236 }
237 
238 //------------------------------------------------------------------------------
239 
241 {
242  return m_size.size();
243 }
244 
245 //------------------------------------------------------------------------------
246 
248 {
249  return m_spacing;
250 }
251 
252 //------------------------------------------------------------------------------
253 
254 void Image::setSpacing(const SpacingType& spacing)
255 {
256  m_spacing = spacing;
257 }
258 
259 //------------------------------------------------------------------------------
260 
262 {
263  return m_origin;
264 }
265 
266 //------------------------------------------------------------------------------
267 
268 void Image::setOrigin(const OriginType& origin)
269 {
270  m_origin = origin;
271 }
272 
273 //------------------------------------------------------------------------------
274 
276 {
277  return m_size;
278 }
279 
280 //------------------------------------------------------------------------------
281 
282 void Image::setSize(const SizeType& size)
283 {
284  m_size = size;
285 }
286 
287 //------------------------------------------------------------------------------
288 
289 size_t Image::getSizeInBytes() const
290 {
291  SLM_TRACE_FUNC();
292 
293  size_t size = std::accumulate(
294  m_size.begin(), m_size.end(),
295  static_cast<size_t>(m_type.sizeOf()) * m_numberOfComponents,
296  std::multiplies< size_t > () );
297  return size;
298 }
299 
300 //------------------------------------------------------------------------------
301 
303 {
304  SLM_TRACE_FUNC();
305  size_t size = 0;
306  if (m_dataArray)
307  {
308  size = m_dataArray->getSizeInBytes();
309  }
310  return size;
311 }
312 
313 //------------------------------------------------------------------------------
314 
315 } // namespace fwData
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_SLICE_INDEX_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
FWDATA_API void setSize(const SizeType &size)
get/set image size
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_TRANSPARENCY_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
FWDATA_API const SpacingType & getSpacing() const
get/set image spacing
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
SizeType m_size
Size of the image (in terms of points)
FWDATA_API void setDataArray(::fwData::Array::sptr array, bool copyArrayInfo=true)
set data array
FWDATA_API const OriginType & getOrigin() const
get/set image origin
FWDATA_API const SizeType & getSize() const
get/set image size
virtual FWDATA_API ~Image() noexcept
Destructor.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Implements data exception class.
size_t m_numberOfComponents
Number of components.
::fwData::Array::sptr m_dataArray
image buffer
Class defining an elementary C++ type aka unsigned char, signed char, .... signed long...
Definition: DynamicType.hpp:31
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_LANDMARK_DISPLAYED_SIG
Type of signal when image&#39;s buffer is added.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_LANDMARK_ADDED_SIG
Type of signal when image&#39;s buffer is added.
FWDATA_API::fwData::Array::sptr getDataArray() const
get data array
FWDATA_API size_t allocate()
Allocate image.
FWDATA_API size_t getSizeInBytes() const
return image size in bytes
FWDATA_API::fwTools::Type getType() const
get/set image type
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_DISTANCE_ADDED_SIG
Type of signal when image&#39;s buffer is added.
Provides a way to manage a view on a multidimentionnal array.
FWDATA_API Image(::fwData::Object::Key key)
Constructor.
FWDATA_API void shallowCopy(const Object::csptr &_source) override
Defines shallow copy.
OriginType m_origin
Origin of the image in 3D repair.
FWDATA_API void fieldDeepCopy(const ::fwData::Object::csptr &source)
A deep copy of fields (objects in m_children)
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_VISIBILITY_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
FWDATA_API size_t getNumberOfDimensions() const
Number of dimension of the image (3 for 3D image)
FWDATA_API size_t getAllocatedSizeInBytes() const
return allocated image size in bytes
#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
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
FWDATA_API void setOrigin(const OriginType &origin)
get/set image origin
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_BUFFER_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
::fwTools::Type m_type
type of image pixel
std::vector< double > SpacingType
Image spacing type.
Contains the representation of the data objects used in the framework.
FWDATA_API void fieldShallowCopy(const ::fwData::Object::csptr &source)
A shallow copy of fields (objects in m_children)
This class defines an image.
FWDATA_API::fwTools::DynamicType getPixelType() const
get a DynamicType for retrocompatibility
FWDATA_API void copyInformation(Image::csptr _source)
get image information from source. Informations are spacing,origin,size ... expect Fields ...
SpacingType m_spacing
An array on the voxel size of the image.
virtual const std::string & getClassname() const override
return full object&#39;s classname with its namespace, i.e. fwCore::BaseObject
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_SLICE_TYPE_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_DISTANCE_REMOVED_SIG
Type of signal when image&#39;s buffer is added.
FWDATA_API void setSpacing(const SpacingType &spacing)
get/set image spacing
::fwData::Array::SizeType SizeType
Image size type.
FWDATA_API void setType(::fwTools::Type type)
get/set image type
FWDATA_API void cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache) override
Defines deep copy.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_LANDMARK_REMOVED_SIG
Type of signal when image&#39;s buffer is added.
std::vector< double > OriginType
Image origin type.
FWTOOLS_API unsigned char sizeOf() const
Return the sizeof of the type.
Definition: Type.cpp:168
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_DISTANCE_DISPLAYED_SIG
Type of signal when image&#39;s buffer is added.