OTB  9.0.0
Orfeo Toolbox
otbMaskedIteratorDecorator.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbMaskedIteratorDecorator_hxx
22 #define otbMaskedIteratorDecorator_hxx
23 
25 #include "itkMacro.h"
26 
27 namespace otb
28 {
29 
30 template <typename TIteratorType, typename TMaskIteratorType>
32 {
33  m_ItImage = TIteratorType(image, region);
34  if (mask == nullptr)
35  {
36  m_UseMask = false;
37  }
38  else
39  {
40  if (image->GetLargestPossibleRegion() != mask->GetLargestPossibleRegion())
41  {
42  itkGenericExceptionMacro("Input image and mask have different largest regions : mask is discarded!");
43  }
44  else
45  {
46  m_UseMask = true;
47  m_ItMask = TMaskIteratorType(mask, region);
48  m_StartMask = TMaskIteratorType(mask, region);
49  m_StartImage = TIteratorType(image, region);
50  }
51  }
52 }
53 
54 template <typename TIteratorType, typename TMaskIteratorType>
56 {
57  return m_ItImage.GetIndex();
58 }
59 
60 template <typename TIteratorType, typename TMaskIteratorType>
62 {
63  if (m_UseMask)
64  {
65  this->ComputeMaskedBegin();
66  }
67  else
68  {
69  m_ItImage.GoToBegin();
70  }
71 }
72 
73 template <typename TIteratorType, typename TMaskIteratorType>
75 {
76  if (m_UseMask)
77  {
78  // make sure masked begin is computed as next calls might be operator--()
79  // and IsAtBegin() (for a reverse iteration)
80  this->ComputeMaskedBegin();
81  m_ItMask.GoToEnd();
82  }
83  m_ItImage.GoToEnd();
84 }
85 
86 template <typename TIteratorType, typename TMaskIteratorType>
88 {
89  if (m_UseMask)
90  {
91  return m_ItMask == m_StartMask || m_ItImage == m_StartImage;
92  }
93  return m_ItImage.IsAtBegin();
94 }
95 
96 template <typename TIteratorType, typename TMaskIteratorType>
98 {
99  if (m_UseMask)
100  {
101  return m_ItMask.IsAtEnd() || m_ItImage.IsAtEnd();
102  }
103  return m_ItImage.IsAtEnd();
104 }
105 
106 // Wrap the underlying iterators to skip masked pixels
107 template <typename TIteratorType, typename TMaskIteratorType>
109 {
110  if (m_UseMask)
111  {
112  do
113  {
114  ++m_ItMask;
115  ++m_ItImage;
116  } while (m_ItMask.Value() == 0 && !this->IsAtEnd());
117  }
118  else
119  {
120  ++m_ItImage;
121  }
122  return *this;
123 }
124 
125 // Wrap the underlying iterators to skip masked pixels
126 template <typename TIteratorType, typename TMaskIteratorType>
128 {
129  if (m_UseMask)
130  {
131  do
132  {
133  --m_ItMask;
134  --m_ItImage;
135  } while (m_ItMask.Value() == 0 && !this->IsAtBegin());
136  }
137  else
138  {
139  --m_ItImage;
140  }
141  return *this;
142 }
143 
144 /*
145 template <typename TIteratorType, typename TMaskIteratorType>
146 void MaskedIteratorDecorator<TIteratorType,TMaskIteratorType>::Set(const PixelType& value) const
147 {
148  m_ItImage.Set(value);
149 }
150 */
151 
152 template <typename TIteratorType, typename TMaskIteratorType>
155 {
156  return m_ItImage.Value();
157 }
158 
159 template <typename TIteratorType, typename TMaskIteratorType>
161 {
162  return m_ItMask;
163 }
164 
165 template <typename TIteratorType, typename TMaskIteratorType>
167 {
168  return m_ItMask;
169 }
170 
171 template <typename TIteratorType, typename TMaskIteratorType>
173 {
174  return m_ItImage;
175 }
176 
177 template <typename TIteratorType, typename TMaskIteratorType>
179 {
180  return m_ItImage;
181 }
182 
183 template <typename TIteratorType, typename TMaskIteratorType>
185 {
186  return m_UseMask;
187 }
188 
189 // Compute begin iterator position taking into account the mask
190 template <typename TIteratorType, typename TMaskIteratorType>
192 {
193  // We must search for the first index where the image is not masked
194  // Start searching at the beginning
195  m_ItMask.GoToBegin();
196  m_ItImage.GoToBegin();
197 
198  // Advance to the first non-masked position, or the end
199  while (m_ItMask.Value() == 0 && !m_ItMask.IsAtEnd() && !m_ItImage.IsAtEnd())
200  {
201  ++m_ItMask;
202  ++m_ItImage;
203  }
204  m_StartMask.SetIndex(m_ItMask.GetIndex());
205  m_StartImage.SetIndex(m_ItImage.GetIndex());
206 }
207 
208 } // namespace otb
209 
210 #endif
otb::MaskedIteratorDecorator::RegionType
ImageType::RegionType RegionType
Definition: otbMaskedIteratorDecorator.h:47
otb::MaskedIteratorDecorator::GetMaskIterator
TMaskIteratorType & GetMaskIterator()
Definition: otbMaskedIteratorDecorator.hxx:160
otb::MaskedIteratorDecorator::operator--
Self & operator--()
Definition: otbMaskedIteratorDecorator.hxx:127
otbMaskedIteratorDecorator.h
otb::MaskedIteratorDecorator::GetImageIterator
TIteratorType & GetImageIterator()
Definition: otbMaskedIteratorDecorator.hxx:172
otb::MaskedIteratorDecorator::MaskType
TMaskIteratorType::ImageType MaskType
Definition: otbMaskedIteratorDecorator.h:44
otb::MaskedIteratorDecorator::ComputeMaskedBegin
void ComputeMaskedBegin()
Definition: otbMaskedIteratorDecorator.hxx:191
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MaskedIteratorDecorator::PixelType
ImageType::PixelType PixelType
Definition: otbMaskedIteratorDecorator.h:48
otb::MaskedIteratorDecorator::MaskedIteratorDecorator
MaskedIteratorDecorator(MaskType *mask, ImageType *image, const RegionType &region)
Definition: otbMaskedIteratorDecorator.hxx:31
otb::MaskedIteratorDecorator::GoToBegin
void GoToBegin()
Definition: otbMaskedIteratorDecorator.hxx:61
otb::MaskedIteratorDecorator
Decorate an iterator to ignore masked pixels.
Definition: otbMaskedIteratorDecorator.h:40
otb::MaskedIteratorDecorator::IsAtBegin
bool IsAtBegin() const
Definition: otbMaskedIteratorDecorator.hxx:87
otb::MaskedIteratorDecorator::HasMask
const bool & HasMask() const
Definition: otbMaskedIteratorDecorator.hxx:184
otb::MaskedIteratorDecorator::IsAtEnd
bool IsAtEnd() const
Definition: otbMaskedIteratorDecorator.hxx:97
otb::MaskedIteratorDecorator::GoToEnd
void GoToEnd()
Definition: otbMaskedIteratorDecorator.hxx:74
otb::MaskedIteratorDecorator::operator++
Self & operator++()
Definition: otbMaskedIteratorDecorator.hxx:108
otb::MaskedIteratorDecorator::Value
const PixelType & Value(void) const
Definition: otbMaskedIteratorDecorator.hxx:154
otb::MaskedIteratorDecorator::IndexType
ImageType::IndexType IndexType
Definition: otbMaskedIteratorDecorator.h:46
otb::MaskedIteratorDecorator::GetIndex
IndexType GetIndex() const
Definition: otbMaskedIteratorDecorator.hxx:55
otb::MaskedIteratorDecorator::ImageType
TIteratorType::ImageType ImageType
Definition: otbMaskedIteratorDecorator.h:45