OTB  9.0.0
Orfeo Toolbox
otbMultiImageFileWriter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017-2019 CS Systemes d'Information (CS SI)
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 otbMultiImageFileWriter_hxx
22 #define otbMultiImageFileWriter_hxx
23 
25 #include "otbImageIOFactory.h"
26 #include "otbMacro.h"
27 
28 namespace otb
29 {
30 
31 template <class TImage>
32 MultiImageFileWriter::Sink<TImage>::Sink(typename TImage::ConstPointer inputImage, const std::string& fileName)
33  : SinkBase(dynamic_cast<const ImageBaseType*>(inputImage.GetPointer())), m_Writer(otb::ImageFileWriter<TImage>::New()), m_ImageIO(NULL)
34 {
35  m_Writer->SetFileName(fileName);
36  m_Writer->SetInput(inputImage);
37 }
38 
39 template <class TImage>
41  : SinkBase(dynamic_cast<const ImageBaseType*>(writer->GetInput())), m_Writer(writer), m_ImageIO(NULL)
42 {
43 }
44 
45 template <class TImage>
47 {
48  if (m_ImageIO.IsNull())
49  return false;
50  return m_ImageIO->CanStreamWrite();
51 }
52 
53 template <class TImage>
55 {
56  m_Writer->UpdateOutputInformation();
57  m_ImageIO = m_Writer->GetImageIO();
58 }
59 
60 template <class TImage>
62 {
63  // Write the image stream
64  itk::ImageIORegion ioRegion(TImage::ImageDimension);
65  for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
66  {
67  ioRegion.SetSize(i, streamRegion.GetSize(i));
68  ioRegion.SetIndex(i, streamRegion.GetIndex(i));
69  }
70  m_ImageIO->SetIORegion(ioRegion);
71  m_Writer->UpdateOutputData(nullptr);
72 }
73 
74 template <class TImage>
75 itk::ImageRegion<2>
77 {
78  auto fnameHelper = m_Writer->GetFilenameHelper();
79  if (fnameHelper->BoxIsSet())
80  {
81  std::vector<unsigned int> boxVector;
82  Utils::ConvertStringToVector(fnameHelper->GetBox(), boxVector, "ExtendedFileName:box", ":");
83 
84  if (boxVector.size() != 4)
85  {
86  itk::ImageFileWriterException e(__FILE__, __LINE__);
87  std::ostringstream msg;
88  msg << "Invalid box option " << fnameHelper->GetBox() << ". The box should contains four elements: startx:starty:sizex:sizey";
89  e.SetDescription(msg.str());
90  e.SetLocation(ITK_LOCATION);
91  throw e;
92  }
93 
94  // indices are signed, sizes are unsigned
95  typename itk::ImageRegion<2>::IndexType start {{static_cast<int>(boxVector[0]), static_cast<int>(boxVector[1])}};
96  typename itk::ImageRegion<2>::SizeType size {{boxVector[2], boxVector[3]}};
97  itk::ImageRegion<2> regionToWrite {start , size};
98  regionToWrite.Crop(m_InputImage->GetLargestPossibleRegion());
99  return regionToWrite;
100  }
101  else
102  {
103  return m_InputImage->GetLargestPossibleRegion();
104  }
105 }
106 
107 template <class TWriter>
108 void MultiImageFileWriter::AddInputWriter(typename TWriter::Pointer writer)
109 {
111  m_SinkList.push_back(SinkBase::Pointer(sink));
112  unsigned int size = m_SinkList.size();
113  this->SetNthInput(size - 1, const_cast<itk::DataObject*>(dynamic_cast<const itk::DataObject*>(writer->GetInput())));
116  auto filenameHelper = writer->GetFilenameHelper();
117  if (filenameHelper->StreamingTypeIsSet())
118  {
119  otbLogMacro(
120  Warning,
121  << "Streaming configuration through extended filename is used. Any previous streaming configuration (ram value, streaming mode ...) will be ignored.");
122 
123  std::string type = filenameHelper->GetStreamingType();
124 
125  std::string sizemode = "auto";
126 
127  if (filenameHelper->StreamingSizeModeIsSet())
128  {
129  sizemode = filenameHelper->GetStreamingSizeMode();
130  }
131 
132  unsigned int sizevalue = 0;
133  // Save the DefaultRAM value for later
134  unsigned int oldDefaultRAM = m_StreamingManager->GetDefaultRAM();
135  if (sizemode == "auto")
136  {
137  sizevalue = oldDefaultRAM;
138  }
139 
140  if (filenameHelper->StreamingSizeValueIsSet())
141  {
142  sizevalue = static_cast<unsigned int>(filenameHelper->GetStreamingSizeValue());
143  }
144 
145  if (type == "auto")
146  {
147  if (sizemode != "auto")
148  {
149  otbLogMacro(Warning, << "In auto streaming type, the sizemode option will be ignored.");
150  }
151  if (sizevalue == 0)
152  {
153  otbLogMacro(Warning, << "sizemode is auto but sizevalue is 0. Value will be fetched from the OTB_MAX_RAM_HINT environment variable if set, or else use "
154  "the default value");
155  }
156  this->SetAutomaticAdaptativeStreaming(sizevalue);
157  }
158  else if (type == "tiled")
159  {
160  if (sizemode == "auto")
161  {
162  if (sizevalue == 0)
163  {
164  otbLogMacro(Warning, << "sizemode is auto but sizevalue is 0. Value will be fetched from the OTB_MAX_RAM_HINT environment variable if set, or else "
165  "use the default value");
166  }
167  this->SetAutomaticTiledStreaming(sizevalue);
168  }
169  else if (sizemode == "nbsplits")
170  {
171  if (sizevalue == 0)
172  {
173  otbLogMacro(Warning, << "Streaming sizemode is set to nbsplits but sizevalue is 0. This will result in undefined behaviour. Please consider setting "
174  "the sizevalue by using &streaming:sizevalue=x.");
175  }
176  this->SetNumberOfDivisionsTiledStreaming(sizevalue);
177  }
178  else if (sizemode == "height")
179  {
180  if (sizevalue == 0)
181  {
182  otbLogMacro(Warning, << "Streaming sizemode is set to height but sizevalue is 0. This will result in undefined behaviour. Please consider setting "
183  "the sizevalue by using &streaming:sizevalue=x.");
184  }
185 
186  this->SetTileDimensionTiledStreaming(sizevalue);
187  }
188  }
189  else if (type == "stripped")
190  {
191  if (sizemode == "auto")
192  {
193  if (sizevalue == 0)
194  {
195  otbLogMacro(
196  Warning, << "sizemode is auto but sizevalue is 0. Value will be fetched from configuration file if any, or from cmake configuration otherwise.");
197  }
198 
199  this->SetAutomaticStrippedStreaming(sizevalue);
200  }
201  else if (sizemode == "nbsplits")
202  {
203  if (sizevalue == 0)
204  {
205  otbLogMacro(Warning, << "Streaming sizemode is set to nbsplits but sizevalue is 0. This will result in undefined behaviour. Please consider setting "
206  "the sizevalue by using &streaming:sizevalue=x.");
207  }
208  this->SetNumberOfDivisionsStrippedStreaming(sizevalue);
209  }
210  else if (sizemode == "height")
211  {
212  if (sizevalue == 0)
213  {
214  otbLogMacro(Warning, << "Streaming sizemode is set to height but sizevalue is 0. This will result in undefined behaviour. Please consider setting "
215  "the sizevalue by using &streaming:sizevalue=x.");
216  }
217  this->SetNumberOfLinesStrippedStreaming(sizevalue);
218  }
219  }
220  else if (type == "none")
221  {
222  if (sizemode != "" || sizevalue != 0)
223  {
224  otbLogMacro(Warning, << "Streaming is explicitly disabled, sizemode and sizevalue will be ignored.");
225  }
227  }
228  m_StreamingManager->SetDefaultRAM(oldDefaultRAM);
229  }
230  else
231  {
232  if (filenameHelper->StreamingSizeValueIsSet() || filenameHelper->StreamingSizeModeIsSet())
233  {
234  otbLogMacro(Warning, << "No streaming type is set, streaming sizemode and sizevalue will be ignored.");
235  }
236  }
237 }
238 
239 template <class TImage>
240 void MultiImageFileWriter::AddInputImage(const TImage* inputPtr, const std::string& fileName)
241 {
242  Sink<TImage>* sink = new Sink<TImage>(inputPtr, fileName);
243  m_SinkList.push_back(SinkBase::Pointer(sink));
244  unsigned int size = m_SinkList.size();
245  this->SetNthInput(size - 1, const_cast<itk::DataObject*>(dynamic_cast<const itk::DataObject*>(inputPtr)));
246 }
247 
248 } // end of namespace otb
249 
250 #endif // otbMultiImageFileWriter_hxx
otbImageIOFactory.h
otb::MultiImageFileWriter::SetAutomaticAdaptativeStreaming
void SetAutomaticAdaptativeStreaming(unsigned int availableRAM=0, double bias=1.0)
otbLogMacro
#define otbLogMacro(level, msg)
Definition: otbMacro.h:52
otb::MultiImageFileWriter::Sink::GetRegionToWrite
itk::ImageRegion< 2 > GetRegionToWrite() const override
Definition: otbMultiImageFileWriter.hxx:76
otb::MultiImageFileWriter::Sink::WriteImageInformation
void WriteImageInformation() override
Definition: otbMultiImageFileWriter.hxx:54
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbMacro.h
otb::MultiImageFileWriter::SetNumberOfLinesStrippedStreaming
void SetNumberOfLinesStrippedStreaming(unsigned int nbLinesPerStrip)
otb::MultiImageFileWriter::SetAutomaticStrippedStreaming
void SetAutomaticStrippedStreaming(unsigned int availableRAM=0, double bias=1.0)
otb::MultiImageFileWriter::SetTileDimensionTiledStreaming
void SetTileDimensionTiledStreaming(unsigned int tileDimension)
otb::MultiImageFileWriter::Sink::Sink
Sink()
Definition: otbMultiImageFileWriter.h:256
otb::MultiImageFileWriter::Sink::Write
void Write(const RegionType &streamRegion) override
Definition: otbMultiImageFileWriter.hxx:61
otb::ImageFileWriter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbImageFileWriter.h:72
otb::MultiImageFileWriter::SetNumberOfDivisionsTiledStreaming
void SetNumberOfDivisionsTiledStreaming(unsigned int nbDivisions)
otb::MultiImageFileWriter::m_SinkList
SinkListType m_SinkList
Definition: otbMultiImageFileWriter.h:286
otb::MultiImageFileWriter::RegionType
ImageBaseType::RegionType RegionType
Definition: otbMultiImageFileWriter.h:62
otb::MultiImageFileWriter::ImageBaseType
itk::ImageBase< 2 > ImageBaseType
Definition: otbMultiImageFileWriter.h:58
otb::MultiImageFileWriter::AddInputWriter
void AddInputWriter(typename TWriter::Pointer writer)
Definition: otbMultiImageFileWriter.hxx:108
otb::MultiImageFileWriter::SetNumberOfDivisionsStrippedStreaming
void SetNumberOfDivisionsStrippedStreaming(unsigned int nbDivisions)
otb::ImageFileWriter::SetInput
virtual void SetInput(const InputImageType *input)
Definition: otbImageFileWriter.hxx:223
otb::MultiImageFileWriter::AddInputImage
void AddInputImage(const TImage *inputPtr, const std::string &fileName)
Definition: otbMultiImageFileWriter.hxx:240
otb::MultiImageFileWriter::SinkBase::Pointer
std::shared_ptr< SinkBase > Pointer
Definition: otbMultiImageFileWriter.h:237
otb::MultiImageFileWriter::Sink
Definition: otbMultiImageFileWriter.h:253
otb::MultiImageFileWriter::Sink::m_Writer
otb::ImageFileWriter< TImage >::Pointer m_Writer
Definition: otbMultiImageFileWriter.h:278
otb::Utils::ConvertStringToVector
void ConvertStringToVector(std::string const &str, T &ret, std::string const &errmsg, const char *delims=" ")
Definition: otbStringUtils.h:83
otb::MultiImageFileWriter::SetAutomaticTiledStreaming
void SetAutomaticTiledStreaming(unsigned int availableRAM=0, double bias=1.0)
otb::ImageFileWriter
Writes image data to a single file with streaming process.
Definition: otbImageFileWriter.h:66
otbMultiImageFileWriter.h
otb::MultiImageFileWriter::Sink::CanStreamWrite
bool CanStreamWrite() const override
Definition: otbMultiImageFileWriter.hxx:46
otb::MultiImageFileWriter::m_StreamingManager
StreamingManagerType::Pointer m_StreamingManager
Definition: otbMultiImageFileWriter.h:199
otb::ImageFileWriter::SetFileName
virtual void SetFileName(const std::string &extendedFileName)
Definition: otbImageFileWriter.hxx:803
otb::MultiImageFileWriter::SinkBase
Definition: otbMultiImageFileWriter.h:214