23 #ifndef otbImageSeriesFileReaderBase_hxx
24 #define otbImageSeriesFileReaderBase_hxx
30 template <
class TImage,
class TInternalImage>
37 m_ListOfFileNames.clear();
38 m_ListOfBandSelection.clear();
39 m_ListOfRegionSelection.clear();
42 template <
class TImage,
class TInternalImage>
45 if (file == m_FileName)
48 this->m_FileName = file;
50 AllocateListOfComponents();
55 template <
class TImage,
class TInternalImage>
58 if (this->GetNumberOfOutputs() < 1)
64 template <
class TImage,
class TInternalImage>
68 if (this->GetNumberOfOutputs() < 1)
71 if (idx >= m_OutputList->Size())
74 return static_cast<OutputImageType*
>(this->m_OutputList->GetNthElement(idx));
77 template <
class TImage,
class TInternalImage>
80 m_ListOfFileNames.clear();
81 m_ListOfBandSelection.clear();
82 m_ListOfRegionSelection.clear();
84 this->TestFileExistenceAndReadability(m_FileName, kFileName);
87 std::ifstream inputFile(m_FileName, std::ios_base::in);
93 std::ostringstream msg;
94 msg <<
"The file " << m_FileName <<
" is not a \"ENVI META FILE\" format\n";
104 std::ostringstream msg;
105 msg <<
"The file " << m_FileName <<
" is not a \"ENVI META FILE\" format\n";
115 std::ostringstream msg;
116 msg <<
"The file " << m_FileName <<
" is not a \"ENVI META FILE\" format\n";
131 }
while (aLine !=
"File" && inputFile.good());
133 if (inputFile.good())
139 }
while (aLine !=
":" && inputFile.good());
141 if (!inputFile.good())
144 std::ostringstream msg;
145 msg <<
"Unable to read image files in the \"ENVI META FILE\" file \n";
146 msg <<
"FileName: " << m_FileName <<
"\n";
157 std::cerr <<
"-> " << aLine <<
"\n";
162 TestFileExistenceAndReadability(aLine, kImageFileName);
163 m_ListOfFileNames.push_back(aLine);
164 std::string imageFileName = aLine;
173 }
while (aLine !=
"Bands:" && inputFile.good());
175 if (!inputFile.good())
178 std::ostringstream msg;
179 msg <<
"Unable to read the number of bands in the images in the \"ENVI META FILE\" file \n";
180 msg <<
"FileName: " << m_FileName <<
"\n";
186 std::vector<unsigned int>
bands;
187 int aBand, oldBand = -1;
194 for (
int i = oldBand; i <= aBand; ++i)
199 bands.push_back(aBand);
201 int posRef = inputFile.tellg();
209 inputFile.seekg(posRef, std::ios_base::beg);
217 TestBandSelection(
bands);
221 std::ostringstream msg;
222 msg << e.GetDescription();
223 msg <<
"Image FileName : " << imageFileName <<
"\n";
224 e.SetDescription(msg.str());
227 m_ListOfBandSelection.push_back(
bands);
236 }
while (aLine !=
"Dims" && inputFile.good());
238 if (inputFile.good())
244 }
while (aLine !=
":" && inputFile.good());
246 if (!inputFile.good())
249 std::ostringstream msg;
250 msg <<
"Unable to read image region in the \"ENVI META FILE\" file \n";
251 msg <<
"FileName : " << m_FileName <<
"\n";
252 msg <<
"ImageName: " << imageFileName <<
"\n";
259 int beg_line, end_line, beg_col, end_col;
260 char sep1, sep2, sep3;
261 inputFile >> beg_col >> sep1 >> end_col >> sep2 >> beg_line >> sep3 >> end_line;
263 if (!inputFile.good())
266 std::ostringstream msg;
267 msg <<
"Unable to read image region selection in the \"ENVI META FILE\" file \n";
268 msg <<
"FileName : " << m_FileName <<
"\n";
269 msg <<
"ImageName: " << imageFileName <<
"\n";
276 index[0] = beg_col - 1;
277 index[1] = beg_line - 1;
280 size[0] = end_col - beg_col + 1;
281 size[1] = end_line - beg_line + 1;
284 region.SetSize(size);
285 region.SetIndex(index);
287 m_ListOfRegionSelection.push_back(region);
291 template <
class TImage,
class TInternalImage>
294 for (
unsigned int i = 0; i < GetNumberOfOutputs(); ++i)
296 m_ImageFileReaderList->PushBack(ReaderType::New());
297 m_OutputList->PushBack(OutputImageType::New());
304 template <
class TImage,
class TInternalImage>
307 std::ostringstream msg;
308 msg <<
"Something wrong... Check the template definition of this class in the program...\n";
309 msg <<
"\"ENVI META FILE\" FileName: " << m_FileName <<
"\n";
315 template <
class TImage,
class TInternalImage>
318 for (
unsigned int i = 0; i < GetNumberOfOutputs(); ++i)
325 template <
class TImage,
class TInternalImage>
329 return this->GetOutput();
333 template <
class TImage,
class TInternalImage>
337 this->GenerateData(idx);
338 return this->GetOutput(idx);
341 template <
class TImage,
class TInternalImage>
344 Superclass::PrintSelf(os, indent);
345 os << indent <<
"File to be read : " << m_FileName <<
"\n";
347 if (m_ListOfFileNames.size() > 0)
349 std::vector<std::vector<unsigned int>>::const_iterator bandSelection = m_ListOfBandSelection.begin();
350 os << indent <<
"Image File(s) to be read\n";
351 for (
unsigned int i = 0; i < GetNumberOfOutputs(); ++i)
354 <<
"Filename : " << m_ListOfFileNames[i] <<
"\n";
356 <<
"RegionSelection: Index( " << m_ListOfRegionSelection[i].GetIndex()[0] <<
", " << m_ListOfRegionSelection[i].GetIndex()[1] <<
") Size( "
357 << m_ListOfRegionSelection[i].GetSize()[0] <<
", " << m_ListOfRegionSelection[i].GetSize()[1] <<
")\n";
359 <<
"BandSelection : ";
361 for (std::vector<unsigned int>::const_iterator bd = (*bandSelection).begin(); bd != (*bandSelection).end(); ++bd)
372 template <
class TImage,
class TInternalImage>
376 if (!itksys::SystemTools::FileExists(file))
378 if (fileType != kImageFileName)
381 std::ostringstream msg;
382 msg <<
"The file doesn't exist. \n";
383 if (fileType == kFileName)
384 msg <<
"Filename = " << file <<
"\n";
386 msg <<
"File = " << file <<
"\n";
387 e.SetDescription(msg.str());
393 std::vector<std::string> fullPath;
394 fullPath.push_back(itksys::SystemTools::GetFilenamePath(m_FileName));
395 fullPath.push_back(
"/");
396 fullPath.push_back(file);
398 std::string fullFileName = itksys::SystemTools::JoinPath(fullPath);
400 if (!itksys::SystemTools::FileExists(fullFileName))
403 std::ostringstream msg;
404 msg <<
"The image file doesn't exist. \n";
405 msg <<
"ImageFileName = " << file <<
"\n";
406 msg <<
"tested path = " << itksys::SystemTools::GetFilenamePath(m_FileName) <<
"\n";
407 msg <<
"Other Tested File = " << fullFileName <<
"\n";
408 e.SetDescription(msg.str());
421 std::ifstream readTester;
422 readTester.open(file);
423 if (readTester.fail())
426 std::ostringstream msg;
427 msg <<
"The file couldn't be opened for reading. " << std::endl <<
"Filename: " << file << std::endl;