OTB  9.0.0
Orfeo Toolbox
otbResetMarginFilter.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 otbResetMarginFilter_hxx
22 #define otbResetMarginFilter_hxx
23 
24 #include "otbResetMarginFilter.h"
25 #include "otbMacro.h"
26 #include "otbLogHelpers.h"
27 #include "itkProgressReporter.h"
28 #include <algorithm>
29 #include <cassert>
30 #include <ostream>
31 
32 namespace otb
33 {
34 
35 template<typename TImage>
36 void
39 {
40  Superclass::GenerateOutputInformation();
41  OutputImageType* output = this->GetOutput();
42  const InputImageType* input = this->GetInput();
43  output->SetNumberOfComponentsPerPixel(input->GetNumberOfComponentsPerPixel());
44 }
45 
46 template<typename TImage>
47 void
50  OutputImageRegionType const& outputRegionForThread,
51  itk::ThreadIdType threadId)
52 {
53  auto const* input = this->GetInput();
54  auto * output = this->GetOutput();
55  assert(input);
56  assert(output);
57 
58  auto curRoi = OutputRegionToInputRegion(outputRegionForThread);
59 
60  auto const& size = outputRegionForThread.GetSize();
61  auto const& index = outputRegionForThread.GetIndex();
62  auto const sizeX = size[0];
63  auto const sizeY = size[1];
64  auto const startX = index[0];
65  auto const startY = index[1];
66  itk::IndexValueType const endX = startX + sizeX;
67  itk::IndexValueType const endY = startY + sizeY;
68 
69  auto const nBand = output->GetNumberOfComponentsPerPixel();
70 
71  itk::IndexValueType thrX1 = curRoi.GetIndex(0);
72  itk::IndexValueType thrX2 = curRoi.GetIndex(0) + curRoi.GetSize(0);
73  itk::IndexValueType thrY1 = curRoi.GetIndex(1);
74  itk::IndexValueType thrY2 = curRoi.GetIndex(1) + curRoi.GetSize(1);
75 
76  assert(startX <= thrX1 && "Iterations shall stay within requested region");
77  assert(thrX1 <= thrX2 && "Iterations shall stay within requested region");
78  assert(thrX2 <= endX && "Iterations shall stay within requested region");
79 
80  assert(startY <= thrY1 && "Iterations shall stay within requested region");
81  assert(thrY1 <= thrY2 && "Iterations shall stay within requested region");
82  assert(thrY2 <= endY && "Iterations shall stay within requested region");
83 
84  auto const full_line = sizeX * nBand;
85  auto const nb_zero_left = (unsigned long)(thrX1 - startX) * nBand;
86  auto const nb_copy_middle = (unsigned long)(thrX2 - thrX1) * nBand;
87  auto const nb_zero_right = (unsigned long)(endX - thrX2) * nBand;
88  assert(nb_zero_left + nb_copy_middle + nb_zero_right == full_line);
89 
90  const InternalPixelType *inData = input->GetBufferPointer();
91  InternalPixelType *outData = output->GetBufferPointer();
92  const unsigned long inLineOff = nBand * input->GetBufferedRegion().GetSize(0);
93  const unsigned long outLineOff = nBand * output->GetBufferedRegion().GetSize(0);
94  bool hasContiguousLines( sizeX == output->GetBufferedRegion().GetSize(0) );
95  // Go to begin
96  outData += nBand * output->ComputeOffset(index);
97  if (curRoi.GetNumberOfPixels())
98  {
99  inData += nBand * input->ComputeOffset(curRoi.GetIndex());
100  }
101 
102  itk::ProgressReporter progress( this, threadId, outputRegionForThread.GetNumberOfPixels() / sizeY );
103 
104  auto y = startY;
105  if (hasContiguousLines)
106  {
107  // if output lines are contiguous, we can do one call to std::fill_n
108  std::fill_n(outData, full_line*(thrY1-y), m_Pad);
109  for (
110  ; y < thrY1
111  ; ++y, outData+=outLineOff )
112  {
113  progress.CompletedPixel(); // Completed...Line()
114  }
115  }
116  else
117  {
118  for (
119  ; y < thrY1
120  ; ++y, outData+=outLineOff )
121  {
122  // If there is any trimming of first lines, the inData will
123  // directly point to the right region. we shall not increment it!
124  // otbMsgDevMacro("o(" << y << ") <-- 0");
125  std::fill_n(outData, full_line, m_Pad);
126  progress.CompletedPixel(); // Completed...Line()
127  }
128  }
129  assert(y == thrY1);
130  otbMsgDevMacro("Y in ["<<thrY1<<".."<<thrY2<<"[ <<--- Input");
131  for (
132  ; y < thrY2
133  ; ++y, inData+=inLineOff, outData+=outLineOff)
134  {
135  auto const t1 = std::fill_n(outData, nb_zero_left, m_Pad);
136  // If there is any trimming of first columns, the inData iterator
137  // will directly point to the right region. we shall not apply an offset!
138  auto const t2 = std::copy_n(inData, nb_copy_middle, t1);
139  std::fill_n(t2, nb_zero_right, m_Pad);
140  progress.CompletedPixel(); // Completed...Line()
141  }
142  assert(y == thrY2);
143  otbMsgDevMacro("Y in ["<<thrY2<<".."<<endY<<"[ <<--- 0");
144  if (hasContiguousLines)
145  {
146  // if output lines are contiguous, we can do one call to std::fill_n
147  std::fill_n(outData, full_line*(endY-y), m_Pad);
148  for (
149  ; y < endY
150  ; ++y, outData+=outLineOff )
151  {
152  progress.CompletedPixel(); // Completed...Line()
153  }
154  }
155  else
156  {
157  for (
158  ; y < endY
159  ; ++y, outData+=outLineOff)
160  {
161  // If there is any trimming of last lines, the inputIterator iterator will
162  // directly point to the right region. we shall not increment it!
163  // otbMsgDevMacro("o(" << y << ") <-- 0");
164  std::fill_n(outData, full_line, m_Pad);
165  progress.CompletedPixel(); // Completed...Line()
166  }
167  }
168  assert(y == endY);
169  otbMsgDevMacro("ThreadedGenerateData end");
170 }
171 
172 template<typename TImage>
176 {
177  auto curROI = m_ROI;
178  if (!curROI.Crop(srcRegion))
179  {
180  curROI.SetIndex(srcRegion.GetIndex() + srcRegion.GetSize());
181  curROI.SetSize({0,0});
182  }
183  return curROI;
184 }
185 
186 } // otb namespace
187 
188 #endif // otbResetMarginFilter_hxx
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbMacro.h
otb::ResetMarginFilter::OutputRegionToInputRegion
InputImageRegionType OutputRegionToInputRegion(OutputImageRegionType const &srcRegion)
Definition: otbResetMarginFilter.hxx:175
otbLogHelpers.h
otb::ResetMarginFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbResetMarginFilter.hxx:38
otb::ResetMarginFilter::OutputImageType
TImage OutputImageType
Definition: otbResetMarginFilter.h:51
otb::ResetMarginFilter::InputImageType
TImage InputImageType
Definition: otbResetMarginFilter.h:50
otb::ResetMarginFilter::ThreadedGenerateData
void ThreadedGenerateData(OutputImageRegionType const &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbResetMarginFilter.hxx:49
otbMsgDevMacro
#define otbMsgDevMacro(x)
Definition: otbMacro.h:64
otb::ResetMarginFilter::OutputImageRegionType
typename OutputImageType::RegionType OutputImageRegionType
Hidden constructor.
Definition: otbResetMarginFilter.h:81
otbResetMarginFilter.h
otb::ResetMarginFilter::InternalPixelType
typename OutputImageType::InternalPixelType InternalPixelType
Hidden constructor.
Definition: otbResetMarginFilter.h:78
otb::ResetMarginFilter::InputImageRegionType
typename InputImageType::RegionType InputImageRegionType
Hidden constructor.
Definition: otbResetMarginFilter.h:80