ANIMA  4.0
animaMergeBlockImages.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 #include <iostream>
3 #include <string>
4 
6 #include <itkImageRegionIterator.h>
7 
8 using namespace std;
9 
10 int main(int argc, char **argv)
11 {
12  typedef itk::Image <double,3> DoubleImageType;
13  typedef itk::VectorImage <double,3> VectorImageType;
14 
15  TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION);
16 
17  TCLAP::ValueArg<std::string> inArg("i","inputlist","Input list of chunks of data",true,"","input list",cmd);
18  TCLAP::ValueArg<std::string> outArg("o","outputfile","output image",true,"","output image",cmd);
19 
20  TCLAP::ValueArg<std::string> geomArg("g","geometryimage","geometry image",true,"","geometry image",cmd);
21  TCLAP::SwitchArg vecArg("V","isvec","Input images are vector images",cmd,false);
22 
23  try
24  {
25  cmd.parse(argc,argv);
26  }
27  catch (TCLAP::ArgException& e)
28  {
29  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
30  return EXIT_FAILURE;
31  }
32 
33  DoubleImageType::Pointer geomImage = anima::readImage <DoubleImageType> (geomArg.getValue());
34 
35  DoubleImageType::SpacingType spacing = geomImage->GetSpacing();
36  DoubleImageType::PointType origin = geomImage->GetOrigin();
37  DoubleImageType::DirectionType direction = geomImage->GetDirection();
38  DoubleImageType::RegionType region = geomImage->GetLargestPossibleRegion();
39 
40  std::vector <std::string> fileNames;
41  std::vector <DoubleImageType::IndexType> indexes;
42 
43  ifstream fileList(inArg.getValue());
44  char tmpStr[2048];
45 
46  while (!fileList.eof())
47  {
48  fileList.getline(tmpStr,2048);
49  while ((strncmp(tmpStr,"<BLOCK>",7) != 0)&&(!fileList.eof()))
50  {
51  fileList.getline(tmpStr,2048);
52  }
53 
54  if (fileList.eof())
55  break;
56 
57  fileList.getline(tmpStr,2048);
58  char fileName[2048];
59  DoubleImageType::IndexType tmpInd;
60  while (strncmp(tmpStr,"</BLOCK>",8) != 0)
61  {
62  if (strncmp(tmpStr,"BLOCK_FILE=",11) == 0)
63  {
64  sscanf(tmpStr,"BLOCK_FILE=%s",fileName);
65  }
66  else if (strncmp(tmpStr,"STARTING_INDEX=",15) == 0)
67  {
68  sscanf(tmpStr,"STARTING_INDEX=%ld %ld %ld",&tmpInd[0],&tmpInd[1],&tmpInd[2]);
69  }
70 
71  fileList.getline(tmpStr,2048);
72  }
73 
74  fileNames.push_back(fileName);
75  indexes.push_back(tmpInd);
76  }
77 
78  fileList.close();
79 
80  bool isVect = vecArg.isSet();
81  if (!isVect)
82  {
83  DoubleImageType::Pointer mainData = DoubleImageType::New();
84  mainData->Initialize();
85  mainData->SetOrigin(origin);
86  mainData->SetSpacing(spacing);
87  mainData->SetDirection(direction);
88  mainData->SetRegions(region);
89 
90  mainData->Allocate();
91 
92  for (unsigned int i = 0;i < fileNames.size();++i)
93  {
94  DoubleImageType::Pointer tmpImage = anima::readImage <DoubleImageType> (fileNames[i]);
95 
96  DoubleImageType::RegionType origRegion = tmpImage->GetLargestPossibleRegion();
97  DoubleImageType::RegionType destRegion = origRegion;
98  for (unsigned int j = 0;j < DoubleImageType::GetImageDimension();++j)
99  destRegion.SetIndex(j,indexes[i][j]);
100 
101  itk::ImageRegionIterator <DoubleImageType> origItr(tmpImage,origRegion);
102  itk::ImageRegionIterator <DoubleImageType> destItr(mainData,destRegion);
103 
104  while (!origItr.IsAtEnd())
105  {
106  destItr.Set(origItr.Get());
107 
108  ++destItr;
109  ++origItr;
110  }
111  }
112 
113  anima::writeImage <DoubleImageType> (outArg.getValue(),mainData);
114  }
115  else
116  {
117  VectorImageType::Pointer mainData = VectorImageType::New();
118  mainData->Initialize();
119 
120  mainData->SetOrigin(origin);
121  mainData->SetSpacing(spacing);
122  mainData->SetDirection(direction);
123  mainData->SetRegions(region);
124 
125  //mainData->Allocate();
126 
127  for (unsigned int i = 0;i < fileNames.size();++i)
128  {
129  std::cout << fileNames[i] << "..." << std::flush;
130  VectorImageType::Pointer tmpImage = anima::readImage <VectorImageType> (fileNames[i]);
131 
132  if (i == 0)
133  {
134  mainData->SetNumberOfComponentsPerPixel(tmpImage->GetNumberOfComponentsPerPixel());
135  mainData->Allocate();
136 
137  itk::VariableLengthVector <double> tmpVecFill(tmpImage->GetNumberOfComponentsPerPixel());
138  for (unsigned int j = 0;j < tmpImage->GetNumberOfComponentsPerPixel();++j)
139  tmpVecFill[j] = 0;
140 
141  itk::ImageRegionIterator <VectorImageType> tmpDestItr(mainData,mainData->GetLargestPossibleRegion());
142  while (!tmpDestItr.IsAtEnd())
143  {
144  tmpDestItr.Set(tmpVecFill);
145  ++tmpDestItr;
146  }
147  }
148 
149  VectorImageType::RegionType origRegion = tmpImage->GetLargestPossibleRegion();
150  VectorImageType::RegionType destRegion = origRegion;
151  for (unsigned int j = 0;j < VectorImageType::GetImageDimension();++j)
152  destRegion.SetIndex(j,indexes[i][j]);
153 
154  itk::ImageRegionIterator <VectorImageType> origItr(tmpImage,origRegion);
155  itk::ImageRegionIterator <VectorImageType> destItr(mainData,destRegion);
156 
157  while (!origItr.IsAtEnd())
158  {
159  itk::VariableLengthVector <double> tmpVec = origItr.Get();
160  destItr.Set(tmpVec);
161 
162  ++destItr;
163  ++origItr;
164  }
165 
166  std::cout << " Done..." <<std::endl;
167  }
168 
169  anima::writeImage <VectorImageType> (outArg.getValue(),mainData);
170  }
171 
172  return EXIT_SUCCESS;
173 }
STL namespace.
int main(int argc, char **argv)