KWStyle - itkImageBase.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkImageBase.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:36 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12   Portions of this code are covered under the VTK copyright.
13   See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
14
15      This software is distributed WITHOUT ANY WARRANTY; without even 
16      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
17 IND *****PURPOSE.  See the above copyright notices for more information.
18
19 =========================================================================*/
20 DEF #ifndef _itkImageBase_txx
21 DEF #define _itkImageBase_txx
22 #include "itkImageBase.h"
23 #include "itkFastMutexLock.h"
24 #include "itkSpatialOrientation.h"
25
26 namespace itk
27 {
28
29 /**
30  *
31  */
32 template<unsigned int VImageDimension>
33 ImageBase<VImageDimension>
34 ::ImageBase()
35 {
36   memset( m_OffsetTable, 0, (VImageDimension+1)*sizeof(unsigned long) );
37   m_Spacing.Fill(1.0);
38   m_Origin.Fill(0.0);
39   m_Direction.SetIdentity();
40 }
41
42
43 /**
44  *
45  */
46 template<unsigned int VImageDimension>
47 void
48 ImageBase<VImageDimension>
49 ::Initialize()
50 {
51   //
52   // We don't modify ourselves because the "ReleaseData" methods depend upon
53   // no modification when initialized.
54   //
55
56   // Call the superclass which should initialize the BufferedRegion ivar.
57   Superclass::Initialize();
58
59   // Clear the offset table
60   memset( m_OffsetTable, 0, (VImageDimension+1)*sizeof(unsigned long) );
61
62   // Clear the BufferedRegion ivar
63   m_BufferedRegion = RegionType();
64 }
65
66
67 /**
68  *
69  */
70 template<unsigned int VImageDimension>
71 ImageBase<VImageDimension>
72 ::~ImageBase()
73 {
74 }
75
76
77 EML
78 //----------------------------------------------------------------------------
79 template<unsigned int VImageDimension>
80 void 
81 ImageBase<VImageDimension>
82 ::SetSpacing(const double spacing[VImageDimension] )
83 {
84   SpacingType s(spacing);
85   this->SetSpacing(s);
86 }
87
88
89 //----------------------------------------------------------------------------
90 template<unsigned int VImageDimension>
91 void 
92 ImageBase<VImageDimension>
93 ::SetSpacing(const float spacing[VImageDimension] )
94 {
95   Vector<float, VImageDimension> sf(spacing);
96   SpacingType s;
97   s.CastFrom( sf );
98   this->SetSpacing(s);
99 }
100
101 //----------------------------------------------------------------------------
102 template<unsigned int VImageDimension>
103 void 
104 ImageBase<VImageDimension>
105 ::SetOrigin(const double origin[VImageDimension] )
106 {
107   PointType p(origin);
108   this->SetOrigin( p );
109 }
110
111
112 //----------------------------------------------------------------------------
113 template<unsigned int VImageDimension>
114 void 
115 ImageBase<VImageDimension>
116 ::SetOrigin(const float origin[VImageDimension] )
117 {
118   Point<float, VImageDimension> of(origin);
119   PointType p;
120   p.CastFrom( of );
121   this->SetOrigin( p );
122 }
123
124 //----------------------------------------------------------------------------
125 template<unsigned int VImageDimension>
126 void 
127 ImageBase<VImageDimension>
128 ::SetDirection(const DirectionType direction )
129 {
130   bool modified = false;
131   for (unsigned int r = 0; r < VImageDimension; r++)
132     {
133     for (unsigned int c = 0; c < VImageDimension; c++)
134       {
135       if (m_Direction[r][c] != direction[r][c])
136         {
137         m_Direction[r][c] = direction[r][c];
138         modified = true;
139         }
140       }
141     }
142   if (modified)
143     {
144     this->Modified();
145     }
146 }
147
148 //----------------------------------------------------------------------------
149 template<unsigned int VImageDimension>
150 void
151 ImageBase<VImageDimension>
152 ::ComputeOffsetTable()
153 {
154   OffsetValueType num=1;
155   const SizeType& bufferSize = this->GetBufferedRegion().GetSize();
156   
157   m_OffsetTable[0] = num;
158   for (unsigned int i=0; i < VImageDimension; i++)
159     {
160     num *= bufferSize[i];
161     m_OffsetTable[i+1] = num;
162     }  
163 }
164
165
166 //----------------------------------------------------------------------------
167 template<unsigned int VImageDimension>
168 void 
169 ImageBase<VImageDimension>
170 ::UpdateOutputInformation()
171 {
172   if (this->GetSource())
173     {
174     this->GetSource()->UpdateOutputInformation();
175     }
176   else
177     {
178     // If we don't have a source, we should set our Image to span our
179     // buffer (by setting our LargestPossibleRegion to equal our
180     // BufferedRegion). However, if the buffer is empty, we leave the
181     // LargestPossibleRegion at its prior value.  This allows InPlace
182     // filters to overwrite their inputs safely (taking ownership of
183     // the pixel buffers), yet respond to subsequent requests for
184     // information.
185     if (m_BufferedRegion.GetNumberOfPixels() > 0)
186       {
187       m_LargestPossibleRegion = m_BufferedRegion;
188       }
189     }
190   
191   // Now we should know what our largest possible region is. If our 
192   // requested region was not set yet, (or has been set to something 
193   // invalid - with no data in it ) then set it to the largest possible
194   // region.
195   if ( m_RequestedRegion.GetNumberOfPixels() == 0)
196     {
197     this->SetRequestedRegionToLargestPossibleRegion();
198     }
199 }
200
201
202 //----------------------------------------------------------------------------
203 template<unsigned int VImageDimension>
204 void 
205 ImageBase<VImageDimension>
206 ::SetRequestedRegionToLargestPossibleRegion()
207 {
208   m_RequestedRegion = m_LargestPossibleRegion;
209 }
210
211 //----------------------------------------------------------------------------
212 template<unsigned int VImageDimension>
213 void 
214 ImageBase<VImageDimension>
215 ::CopyInformation(const DataObject *data)
216 {
217   // Standard call to the superclass' method
218   Superclass::CopyInformation(data);
219
220   if (data)
221     {
222     // Attempt to cast data to an ImageBase
223     const ImageBase<VImageDimension> *imgData;
224   
225 IND *****try
226 IND *******{
227 IND *******imgData = dynamic_cast<const ImageBase<VImageDimension>*>(data);
228 IND *******}
229 IND *****catch( ... )
230 IND *******{
231 IND *******return;
232 IND *******}
233
234     if( imgData )
235       {
236       // Copy the meta data for this data type
237       m_LargestPossibleRegion = imgData->GetLargestPossibleRegion();
238       m_Spacing = imgData->m_Spacing;
239       m_Origin = imgData->m_Origin;
240       this->SetDirection(imgData->m_Direction);
241       this->SetNumberOfComponentsPerPixel( 
242           imgData->GetNumberOfComponentsPerPixel() );
243       }
244     else
245       {
246       // pointer could not be cast back down
247       itkExceptionMacro( << "itk::ImageBase::CopyInformation() cannot cast "
248                          << typeid(data).name() << " to "
249                          << typeid(const ImageBase*).name() );
250       }
251     }
252 }
253
254
255 EML
256 //----------------------------------------------------------------------------
257 template<unsigned int VImageDimension>
258 void 
259 ImageBase<VImageDimension>
260 ::Graft(const DataObject *data)
261 {
262   typedef ImageBase<VImageDimension>  ImageBaseType;
263
264   const ImageBaseType * image;
265   
266   try
267     {
268     image = dynamic_cast< const ImageBaseType * >( data );
269     }
270   catch( ... )
271     {
272     return;
273     }
274
275   if( !image )
276     {
277     return;
278     }
279
280   // Copy the meta-information
281   this->CopyInformation( image );
282
283   // Copy the remaining region information. Subclasses are
284   // responsible for copying the pixel container.
285   this->SetBufferedRegion( image->GetBufferedRegion() );
286   this->SetRequestedRegion( image->GetRequestedRegion() );
287
288 }
289
290
291 EML
292 //----------------------------------------------------------------------------
293 template<unsigned int VImageDimension>
294 bool 
295 ImageBase<VImageDimension>
296 ::RequestedRegionIsOutsideOfTheBufferedRegion()
297 {
298   unsigned int i;
299   const IndexType &requestedRegionIndex = m_RequestedRegion.GetIndex();
300   const IndexType &bufferedRegionIndex = m_BufferedRegion.GetIndex();
301
302   const SizeType& requestedRegionSize = m_RequestedRegion.GetSize();
303   const SizeType& bufferedRegionSize = m_BufferedRegion.GetSize();
304   
305   for (i=0; i< VImageDimension; i++)
306     {
307     if ( (requestedRegionIndex[i] < bufferedRegionIndex[i]) ||
308 LEN          ((requestedRegionIndex[i] + static_cast<OffsetValueType>(requestedRegionSize[i]))
309 LEN,IND **********> (bufferedRegionIndex[i] + static_cast<OffsetValueType>(bufferedRegionSize[i]))) )
310       {
311       return true;
312       }
313     }
314
315   return false;
316 }
317
318
319 //----------------------------------------------------------------------------
320 template<unsigned int VImageDimension>
321 bool 
322 ImageBase<VImageDimension>
323 ::VerifyRequestedRegion()
324 {
325   bool retval = true;
326   unsigned int i;
327
328   // Is the requested region within the LargestPossibleRegion?
329   // Note that the test is indeed against the largest possible region
330   // rather than the buffered region; see DataObject::VerifyRequestedRegion.
331   const IndexType &requestedRegionIndex = m_RequestedRegion.GetIndex();
332   const IndexType &largestPossibleRegionIndex
333 IND ****= m_LargestPossibleRegion.GetIndex();
334
335   const SizeType& requestedRegionSize = m_RequestedRegion.GetSize();
336   const SizeType& largestPossibleRegionSize
337 IND ****= m_LargestPossibleRegion.GetSize();
338   
339   for (i=0; i< VImageDimension; i++)
340     {
341     if ( (requestedRegionIndex[i] < largestPossibleRegionIndex[i]) ||
342          ((requestedRegionIndex[i] + static_cast<long>(requestedRegionSize[i]))
343 LEN,IND **********> (largestPossibleRegionIndex[i]+static_cast<long>(largestPossibleRegionSize[i]))))
344       {
345       retval = false;
346       }
347     }
348
349   return retval;
350 }
351
352 //----------------------------------------------------------------------------
353 template<unsigned int VImageDimension>
354 void
355 ImageBase<VImageDimension>
356 ::SetBufferedRegion(const RegionType ®ion)
357 {
358   if (m_BufferedRegion != region)
359     {
360     m_BufferedRegion = region;
361     this->ComputeOffsetTable();
362     this->Modified();
363     }
364 }
365
366
367 //----------------------------------------------------------------------------
368 template<unsigned int VImageDimension>
369 void
370 ImageBase<VImageDimension>
371 ::SetRequestedRegion(const RegionType ®ion)
372 {
373   if (m_RequestedRegion != region)
374     {
375     m_RequestedRegion = region;
376     }
377 }
378
379 //----------------------------------------------------------------------------
380 template<unsigned int VImageDimension>
381 void 
382 ImageBase<VImageDimension>
383 ::SetRequestedRegion(DataObject *data)
384 {
385   ImageBase *imgData;
386   
387   imgData = dynamic_cast<ImageBase*>(data);
388
389   if (imgData)
390     {
391     // only copy the RequestedRegion if the parameter object is an image
392     m_RequestedRegion = imgData->GetRequestedRegion();
393     }
394 }
395
396 //----------------------------------------------------------------------------
397 template<unsigned int VImageDimension>
398 void
399 ImageBase<VImageDimension>
400 ::SetLargestPossibleRegion(const RegionType ®ion)
401 {
402   if (m_LargestPossibleRegion != region)
403     {
404     m_LargestPossibleRegion = region;
405     this->Modified();
406     }
407 }
408
409 //----------------------------------------------------------------------------
410 template<unsigned int VImageDimension>
411 unsigned int 
412 ImageBase<VImageDimension>
413 ::GetNumberOfComponentsPerPixel() const
414
415   // Returns the number of components in the image. Note that for most images
416   // this is 1. Even for Image< RGBPixel< T >, 3 >.
417   // This is > 1 only for time-series images such as itk::VectorImage. 
418   return 1;
419 }
420
421 //----------------------------------------------------------------------------
422 template<unsigned int VImageDimension>
423 void
424 ImageBase<VImageDimension>
425 ::SetNumberOfComponentsPerPixel( unsigned int )
426 // does nothing (always 1 )
427 }
428
429 /**
430  *
431  */
432 template<unsigned int VImageDimension>
433 void 
434 ImageBase<VImageDimension>
435 ::PrintSelf(std::ostream& os, Indent indent) const
436 {
437   Superclass::PrintSelf(os,indent);
438
439   os << indent << "LargestPossibleRegion: " << std::endl;
440   m_LargestPossibleRegion.PrintSelf(os, indent.GetNextIndent());
441
442   os << indent << "BufferedRegion: " << std::endl;
443   m_BufferedRegion.PrintSelf(os, indent.GetNextIndent());
444
445   os << indent << "RequestedRegion: " << std::endl;
446   m_RequestedRegion.PrintSelf(os, indent.GetNextIndent());
447
448   os << indent << "Spacing: " << m_Spacing << std::endl;
449
450   os << indent << "Origin: " << m_Origin << std::endl;\
451
452   os << indent << "Direction: " << std::endl << m_Direction << std::endl;
453 }
454
455 // end namespace itk
456
457 #endif
458

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:13PM
© Kitware Inc.