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 itkMinimumMaximumImageCalculator_hxx
19 #define itkMinimumMaximumImageCalculator_hxx
20 
21 #include "itkMinimumMaximumImageCalculator.h"
22 #include "itkImageRegionConstIteratorWithIndex.h"
23 #include "itkNumericTraits.h"
24 
25 namespace itk
26 {
27 
28 template< typename TInputImage >
29 MinimumMaximumImageCalculator< TInputImage >
MinimumMaximumImageCalculator()30 ::MinimumMaximumImageCalculator()
31 {
32   m_Image = TInputImage::New();
33   m_Maximum = NumericTraits< PixelType >::NonpositiveMin();
34   m_Minimum = NumericTraits< PixelType >::max();
35   m_IndexOfMinimum.Fill(0);
36   m_IndexOfMaximum.Fill(0);
37   m_RegionSetByUser = false;
38 }
39 
40 template< typename TInputImage >
41 void
42 MinimumMaximumImageCalculator< TInputImage >
Compute()43 ::Compute()
44 {
45   if ( !m_RegionSetByUser )
46     {
47     m_Region = m_Image->GetRequestedRegion();
48     }
49 
50   ImageRegionConstIteratorWithIndex< TInputImage > it(m_Image, m_Region);
51   m_Maximum = NumericTraits< PixelType >::NonpositiveMin();
52   m_Minimum = NumericTraits< PixelType >::max();
53 
54   while ( !it.IsAtEnd() )
55     {
56     const PixelType value = it.Get();
57     if ( value > m_Maximum )
58       {
59       m_Maximum = value;
60       m_IndexOfMaximum = it.GetIndex();
61       }
62     if ( value < m_Minimum )
63       {
64       m_Minimum = value;
65       m_IndexOfMinimum = it.GetIndex();
66       }
67     ++it;
68     }
69 }
70 
71 template< typename TInputImage >
72 void
73 MinimumMaximumImageCalculator< TInputImage >
ComputeMinimum()74 ::ComputeMinimum()
75 {
76   if ( !m_RegionSetByUser )
77     {
78     m_Region = m_Image->GetRequestedRegion();
79     }
80   ImageRegionConstIteratorWithIndex< TInputImage > it(m_Image, m_Region);
81   m_Minimum = NumericTraits< PixelType >::max();
82 
83   while ( !it.IsAtEnd() )
84     {
85     const PixelType value = it.Get();
86     if ( value < m_Minimum )
87       {
88       m_Minimum = value;
89       m_IndexOfMinimum = it.GetIndex();
90       }
91     ++it;
92     }
93 }
94 
95 template< typename TInputImage >
96 void
97 MinimumMaximumImageCalculator< TInputImage >
ComputeMaximum()98 ::ComputeMaximum()
99 {
100   if ( !m_RegionSetByUser )
101     {
102     m_Region = m_Image->GetRequestedRegion();
103     }
104   ImageRegionConstIteratorWithIndex< TInputImage > it(m_Image, m_Region);
105   m_Maximum = NumericTraits< PixelType >::NonpositiveMin();
106 
107   while ( !it.IsAtEnd() )
108     {
109     const PixelType value = it.Get();
110     if ( value > m_Maximum )
111       {
112       m_Maximum = value;
113       m_IndexOfMaximum = it.GetIndex();
114       }
115     ++it;
116     }
117 }
118 
119 template< typename TInputImage >
120 void
121 MinimumMaximumImageCalculator< TInputImage >
SetRegion(const RegionType & region)122 ::SetRegion(const RegionType & region)
123 {
124   m_Region = region;
125   m_RegionSetByUser = true;
126 }
127 
128 template< typename TInputImage >
129 void
130 MinimumMaximumImageCalculator< TInputImage >
PrintSelf(std::ostream & os,Indent indent) const131 ::PrintSelf(std::ostream & os, Indent indent) const
132 {
133   Superclass::PrintSelf(os, indent);
134 
135   os << indent << "Minimum: "
136      << static_cast< typename NumericTraits< PixelType >::PrintType >( m_Minimum )
137      << std::endl;
138   os << indent << "Maximum: "
139      << static_cast< typename NumericTraits< PixelType >::PrintType >( m_Maximum )
140      << std::endl;
141   os << indent << "Index of Minimum: " << m_IndexOfMinimum << std::endl;
142   os << indent << "Index of Maximum: " << m_IndexOfMaximum << std::endl;
143   itkPrintSelfObjectMacro( Image );
144   os << indent << "Region: " << std::endl;
145   m_Region.Print( os, indent.GetNextIndent() );
146   os << indent << "Region set by User: " << m_RegionSetByUser << std::endl;
147 }
148 } // end namespace itk
149 
150 #endif
151