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_h
19 #define itkImageDuplicator_h
20 
21 #include "itkObject.h"
22 #include "itkImage.h"
23 
24 namespace itk
25 {
26 /** \class ImageDuplicator
27  * \brief A helper class which creates an image which is perfect copy of the input image.
28  *
29  * This class is NOT a filter. Although it has an API similar to a filter, this class
30  * is not intended to be used in a pipeline. Instead, the typical use will be like
31  * it is illustrated in the following code:
32  *
33    \code
34        medianFilter->Update();
35        ImageType::Pointer image = medianFilter->GetOutput();
36        using DuplicatorType = itk::ImageDuplicator< ImageType >;
37        DuplicatorType::Pointer duplicator = DuplicatorType::New();
38        duplicator->SetInputImage(image);
39        duplicator->Update();
40        ImageType::Pointer clonedImage = duplicator->GetOutput();
41    \endcode
42  *
43  * Note that the Update() method must be called explicitly in the filter
44  * that provides the input to the ImageDuplicator object. This is needed
45  * because the ImageDuplicator is not a pipeline filter.
46  *
47  * \ingroup ITKCommon
48  *
49  * \wiki
50  * \wikiexample{SimpleOperations/ImageDuplicator,Duplicate an image}
51  * \endwiki
52  */
53 template< typename TInputImage >
54 class ITK_TEMPLATE_EXPORT ImageDuplicator:public Object
55 {
56 public:
57   ITK_DISALLOW_COPY_AND_ASSIGN(ImageDuplicator);
58 
59   /** Standard class type aliases. */
60   using Self = ImageDuplicator;
61   using Superclass = Object;
62   using Pointer = SmartPointer< Self >;
63   using ConstPointer = SmartPointer< const Self >;
64 
65   /** Method for creation through the object factory. */
66   itkNewMacro(Self);
67 
68   /** Run-time type information (and related methods). */
69   itkTypeMacro(ImageDuplicator, Object);
70 
71   /** Type definitions for the input image. */
72   using ImageType = TInputImage;
73   using ImagePointer = typename TInputImage::Pointer;
74   using ImageConstPointer = typename TInputImage::ConstPointer;
75   using PixelType = typename TInputImage::PixelType;
76   using IndexType = typename TInputImage::IndexType;
77 
78   static constexpr unsigned int ImageDimension = ImageType::ImageDimension;
79 
80   itkSetConstObjectMacro(InputImage, ImageType);
81 
82   /**
83     * Provide an interface to match that
84     * of other ProcessObjects
85     * for this source generation object
86     * by returning a non-const pointer
87     * for the generated Object.
88     */
89   //NOTE:  The m_DuplicateImage is only
90   //       exposed via the Source generation interface
91   //       by the GetOutput() method that mimics
92   //       a process object.
GetOutput()93   virtual const ImageType * GetOutput () const { return this->m_DuplicateImage.GetPointer(); }
GetOutput()94   virtual ImageType * GetOutput() { return this->m_DuplicateImage.GetPointer(); }
95 
96 #if !defined(ITK_LEGACY_REMOVE)
97   // This interface was exposed in ITKv4 when the itkGetModifiableObjectMacro was used
GetModifiableOutput()98   virtual ImageType * GetModifiableOutput() { return this->m_DuplicateImage.GetPointer(); }
99 #endif
100 
101   /** Compute of the input image. */
102   void Update();
103 
104 protected:
105   ImageDuplicator();
106   ~ImageDuplicator() override = default;
107   void PrintSelf(std::ostream & os, Indent indent) const override;
108 
109 private:
110   ImageConstPointer m_InputImage;
111   ImagePointer      m_DuplicateImage;
112   ModifiedTimeType  m_InternalImageTime;
113 };
114 } // end namespace itk
115 
116 #ifndef ITK_MANUAL_INSTANTIATION
117 #include "itkImageDuplicator.hxx"
118 #endif
119 
120 #endif /* itkImageDuplicator_h */
121