ANIMA  4.0
animaMCMFileReader.cxx
Go to the documentation of this file.
1 #include <animaMCMFileReader.h>
2 #include <tinyxml2.h>
3 #include <itkObjectFactoryBase.h>
4 
5 namespace anima
6 {
7 
8 itk::ImageIOBase::IOComponentType ANIMAMCM_EXPORT GetMCMComponentType(std::string fileName)
9 {
10  tinyxml2::XMLDocument doc;
11  tinyxml2::XMLError loadOk = doc.LoadFile(fileName.c_str());
12 
13  std::replace(fileName.begin(),fileName.end(),'\\','/');
14  std::string basePath, baseName;
15  std::size_t lastSlashPos = fileName.find_last_of("/");
16 
17  if (lastSlashPos != std::string::npos)
18  basePath.append(fileName.begin(),fileName.begin() + lastSlashPos);
19 
20  if (basePath.length() != 0)
21  basePath = basePath + "/";
22 
23  std::size_t lastDotPos = fileName.find_last_of(".");
24  baseName.append(fileName.begin() + lastSlashPos, fileName.begin() + lastDotPos);
25 
26  if (loadOk != tinyxml2::XML_SUCCESS)
27  return itk::ImageIOBase::UNKNOWNCOMPONENTTYPE;
28 
29  // Read XML file into transform information vector
30  tinyxml2::XMLElement *modelNode = doc.FirstChildElement( "Model" );
31  if (!modelNode)
32  return itk::ImageIOBase::UNKNOWNCOMPONENTTYPE;
33 
34  tinyxml2::XMLElement *weightsNode = modelNode->FirstChildElement( "Weights" );
35 
36  if (!weightsNode)
37  return itk::ImageIOBase::UNKNOWNCOMPONENTTYPE;
38 
39  std::string weightsFileName = basePath + baseName + "/";
40  weightsFileName += weightsNode->GetText();
41 
42  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(weightsFileName.c_str(),
43  itk::ImageIOFactory::ReadMode);
44 
45  if (!imageIO)
46  return itk::ImageIOBase::UNKNOWNCOMPONENTTYPE;
47 
48  imageIO->SetFileName(weightsFileName.c_str());
49 
50  try
51  {
52  imageIO->ReadImageInformation();
53  }
54  catch(itk::ExceptionObject &e)
55  {
56  return itk::ImageIOBase::UNKNOWNCOMPONENTTYPE;
57  }
58 
59  return imageIO->GetComponentType();
60 }
61 
62 } // end namespace anima
itk::ImageIOBase::IOComponentType ANIMAMCM_EXPORT GetMCMComponentType(std::string fileName)