OTB  10.0.0
Orfeo Toolbox
otbStreamingImageVirtualWriter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 otbStreamingImageVirtualWriter_hxx
22 #define otbStreamingImageVirtualWriter_hxx
24 
25 #include "otbMacro.h"
26 #include "itkCommand.h"
27 
35 #include "otbUtils.h"
36 
37 namespace otb
38 {
39 
40 template <class TInputImage>
42  : m_NumberOfDivisions(0), m_CurrentDivision(0), m_DivisionProgress(0.0), m_IsObserving(true), m_ObserverID(0)
43 {
44  // By default, we use tiled streaming, with automatic tile size
45  // We don't set any parameter, so the memory size is retrieved from the OTB configuration options
47 }
48 
49 template <class TInputImage>
51 {
52 }
53 
54 template <class TInputImage>
55 void StreamingImageVirtualWriter<TInputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
56 {
57  Superclass::PrintSelf(os, indent);
58 }
59 
60 template <class TInputImage>
62 {
63  typedef NumberOfDivisionsStrippedStreamingManager<TInputImage> NumberOfDivisionsStrippedStreamingManagerType;
64  typename NumberOfDivisionsStrippedStreamingManagerType::Pointer streamingManager = NumberOfDivisionsStrippedStreamingManagerType::New();
65  streamingManager->SetNumberOfDivisions(nbDivisions);
66 
67  m_StreamingManager = streamingManager;
68 }
69 
70 template <class TInputImage>
72 {
73  typedef NumberOfDivisionsTiledStreamingManager<TInputImage> NumberOfDivisionsTiledStreamingManagerType;
74  typename NumberOfDivisionsTiledStreamingManagerType::Pointer streamingManager = NumberOfDivisionsTiledStreamingManagerType::New();
75  streamingManager->SetNumberOfDivisions(nbDivisions);
76 
77  m_StreamingManager = streamingManager;
78 }
79 
80 template <class TInputImage>
82 {
83  typedef NumberOfLinesStrippedStreamingManager<TInputImage> NumberOfLinesStrippedStreamingManagerType;
84  typename NumberOfLinesStrippedStreamingManagerType::Pointer streamingManager = NumberOfLinesStrippedStreamingManagerType::New();
85  streamingManager->SetNumberOfLinesPerStrip(nbLinesPerStrip);
86 
87  m_StreamingManager = streamingManager;
88 }
89 
90 template <class TInputImage>
92 {
93  typedef RAMDrivenStrippedStreamingManager<TInputImage> RAMDrivenStrippedStreamingManagerType;
94  typename RAMDrivenStrippedStreamingManagerType::Pointer streamingManager = RAMDrivenStrippedStreamingManagerType::New();
95  streamingManager->SetAvailableRAMInMB(availableRAM);
96  streamingManager->SetBias(bias);
97  m_StreamingManager = streamingManager;
98 }
99 
100 template <class TInputImage>
102 {
103  typedef TileDimensionTiledStreamingManager<TInputImage> TileDimensionTiledStreamingManagerType;
104  typename TileDimensionTiledStreamingManagerType::Pointer streamingManager = TileDimensionTiledStreamingManagerType::New();
105  streamingManager->SetTileDimension(tileDimension);
106 
107  m_StreamingManager = streamingManager;
108 }
109 
110 template <class TInputImage>
112 {
113  typedef RAMDrivenTiledStreamingManager<TInputImage> RAMDrivenTiledStreamingManagerType;
114  typename RAMDrivenTiledStreamingManagerType::Pointer streamingManager = RAMDrivenTiledStreamingManagerType::New();
115  streamingManager->SetAvailableRAMInMB(availableRAM);
116  streamingManager->SetBias(bias);
117  m_StreamingManager = streamingManager;
118 }
119 
120 template <class TInputImage>
122 {
123  typedef RAMDrivenAdaptativeStreamingManager<TInputImage> RAMDrivenAdaptativeStreamingManagerType;
124  typename RAMDrivenAdaptativeStreamingManagerType::Pointer streamingManager = RAMDrivenAdaptativeStreamingManagerType::New();
125  streamingManager->SetAvailableRAMInMB(availableRAM);
126  streamingManager->SetBias(bias);
127  m_StreamingManager = streamingManager;
128 }
129 
130 template <class TInputImage>
132 {
133  InputImagePointer inputPtr = const_cast<InputImageType*>(this->GetInput(0));
134  inputPtr->UpdateOutputInformation();
135 
136  this->GenerateData();
137 }
138 
139 template <class TInputImage>
141 {
142  InputImagePointer inputPtr = const_cast<InputImageType*>(this->GetInput(0));
143 
144  InputImageRegionType region;
145  typename InputImageRegionType::SizeType size;
146  typename InputImageRegionType::IndexType index;
147 
148  index.Fill(0);
149  size.Fill(0);
150  region.SetSize(size);
151  region.SetIndex(index);
152  inputPtr->SetRequestedRegion(region);
153 }
154 
155 template <class TInputImage>
157 {
158  otb::Logger::Instance()->LogSetupInformation();
159 
163  this->PrepareOutputs();
164  this->SetAbortGenerateData(0);
165  this->SetProgress(0.0);
166  this->m_Updating = true;
167 
171  this->InvokeEvent(itk::StartEvent());
172 
176  InputImagePointer inputPtr = const_cast<InputImageType*>(this->GetInput(0));
177  InputImageRegionType outputRegion = inputPtr->GetLargestPossibleRegion();
178 
184  m_StreamingManager->PrepareStreaming(inputPtr, outputRegion);
185  m_NumberOfDivisions = m_StreamingManager->GetNumberOfSplits();
187 
191  // Get the source process object
192  itk::ProcessObject* source = inputPtr->GetSource();
193  m_IsObserving = false;
194  m_ObserverID = 0;
195 
196  // Check if source exists
197  if (source)
198  {
199  typedef itk::MemberCommand<Self> CommandType;
200  typedef typename CommandType::Pointer CommandPointerType;
201 
202  CommandPointerType command = CommandType::New();
203  command->SetCallbackFunction(this, &Self::ObserveSourceFilterProgress);
204 
205  m_ObserverID = source->AddObserver(itk::ProgressEvent(), command);
206  m_IsObserving = true;
207  }
208 
209  const auto firstSplitSize = m_StreamingManager->GetSplit(0).GetSize();
210  otbLogMacro(Info, << "Estimation will be performed in " << m_NumberOfDivisions << " blocks of " << firstSplitSize[0] << "x" << firstSplitSize[1]
211  << " pixels");
212 
213 
218  InputImageRegionType streamRegion;
219  for (m_CurrentDivision = 0; m_CurrentDivision < m_NumberOfDivisions && !this->GetAbortGenerateData();
220  m_CurrentDivision++, m_DivisionProgress = 0, this->UpdateFilterProgress())
221  {
222  streamRegion = m_StreamingManager->GetSplit(m_CurrentDivision);
223  // inputPtr->ReleaseData();
224  // inputPtr->SetRequestedRegion(streamRegion);
225  // inputPtr->Update();
226  inputPtr->SetRequestedRegion(streamRegion);
227  inputPtr->PropagateRequestedRegion();
228  inputPtr->UpdateOutputData();
229  }
231 
236  if (!this->GetAbortGenerateData())
237  {
238  this->UpdateProgress(1.0);
239  }
240  else
241  {
242  itk::ProcessAborted e(__FILE__, __LINE__);
243  e.SetLocation(ITK_LOCATION);
244  e.SetDescription("Image streaming has been aborted");
245  throw e;
246  }
248 
249  // Notify end event observers
250  this->InvokeEvent(itk::EndEvent());
251 
252  if (m_IsObserving)
253  {
254  m_IsObserving = false;
255  source->RemoveObserver(m_ObserverID);
256  }
257 
261  for (unsigned int idx = 0; idx < this->GetNumberOfOutputs(); ++idx)
262  {
263  if (this->GetOutput(idx))
264  {
265  this->GetOutput(idx)->DataHasBeenGenerated();
266  }
267  }
269 
273  this->ReleaseInputs();
274 }
275 
276 template <class TInputImage>
278 {
279  std::lock_guard<std::mutex> mutexHolder(m_Lock);
280  bool ret = Superclass::GetAbortGenerateData();
281  m_Lock.unlock();
282  if (ret)
285 }
286 
287 template <class TInputImage>
289 {
290  std::lock_guard<std::mutex> mutexHolder(m_Lock);
291  Superclass::SetAbortGenerateData(val);
292 }
293 
294 } // end namespace otb
295 
296 #endif
static Logger * Instance()
This class computes the divisions needed to stream an image by strips, driven by a user-defined numbe...
This class computes the divisions needed to stream an image by strips, driven by a user-defined numbe...
This class computes the divisions needed to stream an image by strips, driven by a user-defined desir...
This class computes the divisions needed to stream an image according to the input image tiling schem...
This class computes the divisions needed to stream an image by strips, according to a user-defined av...
This class computes the divisions needed to stream an image in square tiles, according to a user-defi...
void SetAutomaticStrippedStreaming(unsigned int availableRAM=0, double bias=1.0)
void SetAbortGenerateData(const bool val) override
const bool & GetAbortGenerateData() const override
void SetAutomaticTiledStreaming(unsigned int availableRAM=0, double bias=1.0)
void SetTileDimensionTiledStreaming(unsigned int tileDimension)
void SetNumberOfLinesStrippedStreaming(unsigned int nbLinesPerStrip)
void SetNumberOfDivisionsStrippedStreaming(unsigned int nbDivisions)
void SetNumberOfDivisionsTiledStreaming(unsigned int nbDivisions)
void SetAutomaticAdaptativeStreaming(unsigned int availableRAM=0, double bias=1.0)
void PrintSelf(std::ostream &os, itk::Indent indent) const override
This class computes the divisions needed to stream an image in square tiles, driven by a user-defined...
OTBCommon_EXPORT bool const FalseConstant
OTBCommon_EXPORT bool const TrueConstant
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
#define otbLogMacro(level, msg)
Definition: otbMacro.h:104