18 #ifndef __otbImageFileReader_txx
19 #define __otbImageFileReader_txx
25 #include "itksys/SystemTools.hxx"
52 template <
class TOutputImage>
58 m_FilenameHelper = FNameHelperType::New();
60 m_AdditionalNumber = 0;
63 template <
class TOutputImage>
69 template <
class TOutputImage>
73 Superclass::PrintSelf(os, indent);
77 os << indent <<
"ImageIO: \n";
78 this->m_ImageIO->Print(os, indent.GetNextIndent());
82 os << indent <<
"ImageIO: (null)" <<
"\n";
85 os << indent <<
"UserSpecifiedImageIO flag: " << this->m_UserSpecifiedImageIO <<
"\n";
86 os << indent <<
"m_FileName: " << this->m_FileName <<
"\n";
89 template <
class TOutputImage>
95 typename TOutputImage::Pointer output = this->GetOutput();
98 output->SetBufferedRegion(output->GetRequestedRegion());
103 this->TestFileExistanceAndReadability();
107 output->GetPixelContainer()->GetBufferPointer();
108 this->m_ImageIO->SetFileName(this->m_FileName.c_str());
117 for (
unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
119 if (i < this->m_ImageIO->GetNumberOfDimensions())
121 if (!this->m_ImageIO->CanStreamRead()) dimSize[i] = this->m_ImageIO->GetDimensions(i);
122 else dimSize[i] = output->GetRequestedRegion().GetSize()[i];
133 for (
unsigned int i = 0; i < dimSize.GetSizeDimension(); ++i)
135 ioSize[i] = dimSize[i];
138 typedef typename TOutputImage::IndexType
IndexType;
140 if (!this->m_ImageIO->CanStreamRead()) start.Fill(0);
141 else start = output->GetRequestedRegion().GetIndex();
142 for (
unsigned int i = 0; i < start.GetIndexDimension(); ++i)
144 ioStart[i] = start[i];
150 this->m_ImageIO->SetIORegion(ioRegion);
155 if (this->m_ImageIO->GetComponentTypeInfo()
156 ==
typeid(
typename ConvertPixelTraits::ComponentType)
157 && (this->m_ImageIO->GetNumberOfComponents()
158 == ConvertIOPixelTraits::GetNumberOfComponents()))
161 this->m_ImageIO->Read(buffer);
171 std::streamoff nbBytes = (this->m_ImageIO->GetComponentSize() * this->m_ImageIO->GetNumberOfComponents())
172 * static_cast<std::streamoff>(region.GetNumberOfPixels());
174 char * loadBuffer =
new char[nbBytes];
176 otbMsgDevMacro(<<
"size of Buffer to GDALImageIO::read = " << nbBytes <<
" = \n"
177 <<
"ComponentSize ("<< this->m_ImageIO->GetComponentSize() <<
") x " \
178 <<
"Nb of Component (" << this->m_ImageIO->GetNumberOfComponents() <<
") x " \
179 <<
"Nb of Pixel to read (" << region.GetNumberOfPixels() <<
")" );
181 this->m_ImageIO->Read(loadBuffer);
183 this->DoConvertBuffer(loadBuffer, region.GetNumberOfPixels());
189 template <
class TOutputImage>
194 typename TOutputImage::Pointer out =
dynamic_cast<TOutputImage*
>(output);
198 if (!this->m_ImageIO->CanStreamRead())
202 out->SetRequestedRegion(out->GetLargestPossibleRegion());
207 "Invalid output object type");
212 template <
class TOutputImage>
218 typename TOutputImage::Pointer output = this->GetOutput();
220 itkDebugMacro(<<
"Reading file for GenerateOutputInformation()" << this->m_FileName);
224 if (this->m_FileName ==
"")
231 std::string lFileName;
232 bool found = GetGdalReadImageFileName(this->m_FileName, lFileName);
235 otbMsgDebugMacro(<<
"Filename was NOT unknown. May be recognized by a Image factory ! ");
238 this->m_FileName = lFileName;
240 std::string lFileNameOssimKeywordlist = this->m_FileName;
249 m_ExceptionMessage =
"";
250 this->TestFileExistanceAndReadability();
257 if (this->m_UserSpecifiedImageIO ==
false)
262 if (this->m_ImageIO.IsNull())
264 this->Print(std::cerr);
266 std::ostringstream msg;
267 msg <<
" Could not create IO object for file "
268 << this->m_FileName.c_str() << std::endl;
269 msg <<
" Tried to create one of the following:" << std::endl;
270 std::list<itk::LightObject::Pointer> allobjects =
272 for (std::list<itk::LightObject::Pointer>::iterator i = allobjects.begin();
273 i != allobjects.end(); ++i)
278 msg <<
" You probably failed to set a file suffix, or" << std::endl;
279 msg <<
" set the suffix to an unsupported type." << std::endl;
290 if (strcmp(this->m_ImageIO->GetNameOfClass(),
"GDALImageIO") == 0)
300 if (strcmp(output->GetNameOfClass(),
"VectorImage") == 0)
301 imageIO->SetIsVectorImage(
true);
303 imageIO->SetIsVectorImage(
false);
306 if (m_FilenameHelper->SubDatasetIndexIsSet())
308 imageIO->SetDatasetNumber(m_FilenameHelper->GetSubDatasetIndex());
312 imageIO->SetDatasetNumber(m_AdditionalNumber);
317 if( strcmp(this->m_ImageIO->GetNameOfClass(),
"JPEG2000ImageIO") == 0 )
319 if (m_FilenameHelper->ResolutionFactorIsSet())
321 itk::EncapsulateMetaData<unsigned int>(dict,
326 itk::EncapsulateMetaData<unsigned int>(dict,
329 itk::EncapsulateMetaData<unsigned int>(dict,
336 this->m_ImageIO->SetFileName(this->m_FileName.c_str());
337 this->m_ImageIO->ReadImageInformation();
343 double spacing[TOutputImage::ImageDimension];
344 double origin[TOutputImage::ImageDimension];
345 typename TOutputImage::DirectionType direction;
346 std::vector<double> axis;
348 for (
unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
350 if (i < this->m_ImageIO->GetNumberOfDimensions())
352 dimSize[i] = this->m_ImageIO->GetDimensions(i);
353 spacing[i] = this->m_ImageIO->GetSpacing(i);
354 origin[i] = this->m_ImageIO->GetOrigin(i);
357 axis = this->m_ImageIO->GetDirection(i);
358 for (
unsigned j = 0; j < TOutputImage::ImageDimension; ++j)
360 if (j < this->m_ImageIO->GetNumberOfDimensions())
362 direction[j][i] = axis[j];
366 direction[j][i] = 0.0;
378 for (
unsigned j = 0; j < TOutputImage::ImageDimension; ++j)
382 direction[j][i] = 1.0;
386 direction[j][i] = 0.0;
392 if (m_FilenameHelper->GetSkipCarto())
394 for (
unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
397 if ( m_FilenameHelper->GetResolutionFactor() != 0 )
399 spacing[i] = 1.0*vcl_pow((
double)2, (
double)m_FilenameHelper->GetResolutionFactor());
408 output->SetSpacing(spacing);
409 output->SetOrigin(origin);
410 output->SetDirection(direction);
414 if (!m_FilenameHelper->ExtGEOMFileNameIsSet())
426 if (strcmp(this->m_ImageIO->GetNameOfClass(),
"TileMapImageIO") == 0)
429 std::ostringstream depth;
430 depth << imageIO->GetDepth();
431 otb_kwl.
AddKey(
"depth", depth.str());
439 itk::EncapsulateMetaData<ImageKeywordlist>(dict,
452 const double Epsilon = 1.0E-12;
454 && vcl_abs(origin[0]) > Epsilon
455 && vcl_abs(origin[1]) > Epsilon
456 && vcl_abs(spacing[0] - 1) > Epsilon
457 && vcl_abs(spacing[1] - 1) > Epsilon)
459 std::string wgs84ProjRef =
460 "GEOGCS[\"GCS_WGS_1984\", DATUM[\"D_WGS_1984\", SPHEROID[\"WGS_1984\", 6378137, 298.257223563]],"
461 "PRIMEM[\"Greenwich\", 0], UNIT[\"Degree\", 0.017453292519943295]]";
468 if (m_FilenameHelper->GetSkipCarto())
474 if (!m_FilenameHelper->GetSkipGeom())
476 output->SetMetaDataDictionary(this->m_ImageIO->GetMetaDataDictionary());
477 this->SetMetaDataDictionary(this->m_ImageIO->GetMetaDataDictionary());
485 output->SetMetaDataDictionary(dictLight);
486 this->SetMetaDataDictionary(dictLight);
489 typedef typename TOutputImage::IndexType
IndexType;
495 region.SetSize(dimSize);
496 region.SetIndex(start);
501 if (strcmp(output->GetNameOfClass(),
"VectorImage") == 0)
503 typedef typename TOutputImage::AccessorFunctorType AccessorFunctorType;
504 AccessorFunctorType::SetVectorLength(output, this->m_ImageIO->GetNumberOfComponents());
507 output->SetLargestPossibleRegion(region);
511 template <
class TOutputImage>
517 if (this->m_FileName[0] ==
'h'
518 && this->m_FileName[1] ==
't'
519 && this->m_FileName[2] ==
't'
520 && this->m_FileName[3] ==
'p')
523 if (!m_Curl->TestUrlAvailability(this->m_FileName))
526 std::ostringstream msg;
527 msg <<
"File name is an http address, but curl fails to connect to it "
528 << std::endl <<
"Filename = " << this->m_FileName
537 if (!itksys::SystemTools::FileExists(this->m_FileName.c_str()))
540 std::ostringstream msg;
541 msg <<
"The file doesn't exist. "
542 << std::endl <<
"Filename = " << this->m_FileName
553 std::ifstream readTester;
554 readTester.open(this->m_FileName.c_str());
555 if (readTester.fail())
558 std::ostringstream msg;
559 msg <<
"The file couldn't be opened for reading. "
560 << std::endl <<
"Filename: " << this->m_FileName
571 template <
class TOutputImage>
576 std::vector<std::string> listFileSearch;
577 listFileSearch.push_back(
"DAT_01.001");
578 listFileSearch.push_back(
"dat_01.001");
579 listFileSearch.push_back(
"IMAGERY.TIF");
580 listFileSearch.push_back(
"imagery.tif");
583 listFileSearch.push_back(
"IMAG_01.DAT");
584 listFileSearch.push_back(
"imag_01.dat");
586 std::string str_FileName;
587 bool fic_trouve(
false);
590 std::vector<std::string> listFileFind;
592 if (listFileFind.empty() ==
false)
595 while ((cpt < listFileFind.size()) && (fic_trouve ==
false))
597 str_FileName = std::string(listFileFind[cpt]);
598 for (
unsigned int i = 0; i < listFileSearch.size(); ++i)
600 if (str_FileName.compare(listFileSearch[i]) == 0)
602 GdalFileName = std::string(filename) + str_FileName;
611 std::string strFileName(filename);
614 if ((extension ==
"HDR") || (extension ==
"hdr"))
623 GdalFileName = std::string(filename);
632 template <
class TOutputImage>
637 this->SetFileName(extendedFileName.c_str());
640 template <
class TOutputImage>
645 this->m_FilenameHelper->SetExtendedFileName(extendedFileName);
646 this->m_FileName = this->m_FilenameHelper->GetSimpleFileName();
650 template <
class TOutputImage>
655 return this->m_FilenameHelper->GetSimpleFileName();