1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkImageDuplicator_hxx
19 #define itkImageDuplicator_hxx
20 
21 #include "itkImageDuplicator.h"
22 #include "itkImageAlgorithm.h"
23 
24 namespace itk
25 {
26 /** Constructor */
27 template< typename TInputImage >
28 ImageDuplicator< TInputImage >
ImageDuplicator()29 ::ImageDuplicator()
30 {
31   m_InputImage = nullptr;
32   m_DuplicateImage = nullptr;
33   m_InternalImageTime = 0;
34 }
35 
36 /** */
37 template< typename TInputImage >
38 void
39 ImageDuplicator< TInputImage >
Update()40 ::Update()
41 {
42   if ( !m_InputImage )
43     {
44     itkExceptionMacro(<< "Input image has not been connected");
45     return;
46     }
47 
48   // Update only if the input image has been modified
49   const ModifiedTimeType t1 = m_InputImage->GetPipelineMTime();
50   const ModifiedTimeType t2 = m_InputImage->GetMTime();
51   const ModifiedTimeType t = ( t1 > t2 ? t1 : t2 );
52 
53   if ( t == m_InternalImageTime )
54     {
55     return; // No need to update
56     }
57 
58   // Cache the timestamp
59   m_InternalImageTime = t;
60 
61   // Allocate the image
62   m_DuplicateImage = ImageType::New();
63   m_DuplicateImage->CopyInformation( m_InputImage );
64   m_DuplicateImage->SetRequestedRegion( m_InputImage->GetRequestedRegion() );
65   m_DuplicateImage->SetBufferedRegion( m_InputImage->GetBufferedRegion() );
66   m_DuplicateImage->Allocate();
67   typename ImageType::RegionType region = m_InputImage->GetBufferedRegion();
68   ImageAlgorithm::Copy(m_InputImage.GetPointer(),m_DuplicateImage.GetPointer(),region,region);
69 }
70 
71 template< typename TInputImage >
72 void
73 ImageDuplicator< TInputImage >
PrintSelf(std::ostream & os,Indent indent) const74 ::PrintSelf(std::ostream & os, Indent indent) const
75 {
76   Superclass::PrintSelf(os, indent);
77   os << indent << "Input Image: " << m_InputImage << std::endl;
78   os << indent << "Output Image: " << m_DuplicateImage << std::endl;
79   os << indent << "Internal Image Time: " << m_InternalImageTime << std::endl;
80 }
81 } // end namespace itk
82 
83 #endif
84