DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
ImageInfo.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Image Info
4 
5  Copyright (c) Pew-Thian Yap. All rights reserved.
6  See http://www.unc.edu/~ptyap/ for details.
7 
8  This software is distributed WITHOUT ANY WARRANTY; without even
9  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  PURPOSE. See the above copyright notices for more information.
11 
12  =========================================================================*/
13 
14 #if defined(_MSC_VER)
15 #pragma warning ( disable : 4786 )
16 #endif
17 
18 #ifdef __BORLANDC__
19 #define ITK_LEAN_AND_MEAN
20 #endif
21 
27 #include <iostream>
28 #include <stdio.h>
29 #include <vector>
30 #include "itkImage.h"
31 #include "itkImageFileReader.h"
32 #include "itkMetaDataDictionary.h"
33 #include "itkMetaDataObject.h"
34 #include <ImageInfoCLP.h>
35 
36 #include "utlITK.h"
37 
38 enum
39 {
55 };
56 
57 void
58 SetOperationWithChecking(int &operation, int value)
59 {
60  if (operation == OP_NULL)
61  {
62  operation = value;
63  }
64  else
65  {
66  std::cerr << "Only one operation allowed!" << std::endl;
67  exit(EXIT_FAILURE);
68  }
69 }
70 
71 void
72 IntRangeCheck(int x, int LowerLimit, int UpperLimit)
73 {
74  if (x < LowerLimit || x > UpperLimit)
75  {
76  std::cerr << "Parameter out of range!" << std::endl;
77  exit(EXIT_FAILURE);
78  }
79 }
80 
81 int
82 show_file(const std::string& file)
83 {
84  std::string line;
85  std::ifstream myfile (file);
86  if (myfile.is_open())
87  {
88  while ( getline (myfile,line) )
89  {
90  std::cout << line << '\n';
91  }
92  myfile.close();
93  }
94  return 0;
95 }
96 
97 int
98 main(int argc, char *argv[])
99 {
100  PARSE_ARGS;
101 
102  if (itk::GetImageType(_InputFile)==IMAGE_SPARSE)
103  return show_file(_InputFile);
104  else if (itk::GetImageType(_InputFile)==IMAGE_VARIABLELENGTH)
105  return show_file(_InputFile);
106 
107  int operation = OP_NULL;
108 
109  if (_ImageDimensionArg.isSet())
110  {
112  IntRangeCheck(_ImageDimension, 0, 3);
113  }
114 
115  if (_ImageSpacingArg.isSet())
116  {
118  IntRangeCheck(_ImageSpacing, 0, 3);
119  }
120  if (_ComponentTypeArg.isSet())
122  if (_PixelTypeArg.isSet())
124  if (_NumberOfComponentsPerVoxelArg.isSet())
126  if (_ImageSizeInBytesArg.isSet())
128  if (_ImageSizeInComponentsArg.isSet())
130  if (_ImageSizeInPixelsArg.isSet())
132  if (_OriginArg.isSet())
134  if (_MetaDataArg.isSet())
136  if (_GradientsArg.isSet())
138  if (_DiffusionWeightingArg.isSet())
140  if (_DiffusionWeightingsArg.isSet())
142 
143  // Define Variables
144  typedef float PixelType;
145  static const unsigned int ImageDimension = 4;
146  typedef itk::VectorImage<PixelType, ImageDimension> MultiVolumeVectorImageType;
147  typedef itk::ImageFileReader<MultiVolumeVectorImageType> ReaderType;
148 
149  MultiVolumeVectorImageType::Pointer inputImage;
150  ReaderType::Pointer reader = ReaderType::New();
151 
152  // Read Input File Info
153  reader->SetFileName(_InputFile);
154  reader->UpdateOutputInformation();
155  inputImage = reader->GetOutput();
156  itk::ImageIOBase::Pointer inputImageIOBase = reader->GetImageIO();
157  inputImageIOBase->ReadImageInformation();
158 
159  itk::ImageIOBase::IOPixelType inputPixelType = inputImageIOBase->GetPixelType();
160  itk::ImageIOBase::IOComponentType inputComponentType =
161  inputImageIOBase->GetComponentType();
162  itk::ImageIOBase::SizeType inputImageSizeInBytes =
163  inputImageIOBase->GetImageSizeInBytes();
164  itk::ImageIOBase::SizeType inputImageSizeInComponents =
165  inputImageIOBase->GetImageSizeInComponents();
166  itk::ImageIOBase::SizeType inputImageSizeInPixels =
167  inputImageIOBase->GetImageSizeInPixels();
168 
169  itk::ImageIOBase::ByteOrder inputByteOrder = inputImageIOBase->GetByteOrder();
170  // bool useCompression = inputImageIOBase->GetUseCompression();
171 
172  std::string inputPixelTypeAsString(
173  inputImageIOBase->GetPixelTypeAsString(inputPixelType));
174  std::string inputComponentTypeAsString(
175  inputImageIOBase->GetComponentTypeAsString(inputComponentType));
176  std::string inputByteOrderAsString(inputImageIOBase->GetByteOrderAsString(inputByteOrder));
177 
178  unsigned int inputNumberOfComponentsPerPixel =
179  inputImage->GetNumberOfComponentsPerPixel();
180 
181  MultiVolumeVectorImageType::RegionType inputRegion =
182  inputImage->GetLargestPossibleRegion();
183  MultiVolumeVectorImageType::SizeType inputSize = inputRegion.GetSize();
184  MultiVolumeVectorImageType::SpacingType inputSpacing = inputImage->GetSpacing();
185  MultiVolumeVectorImageType::PointType inputOrigin = inputImage->GetOrigin();
186  MultiVolumeVectorImageType::DirectionType inputDirection = inputImage->GetDirection();
187 
188  // Meta Data
189  itk::MetaDataDictionary MetaDictionary = inputImage->GetMetaDataDictionary();
190  std::vector < std::string > MetaKeys = MetaDictionary.GetKeys();
191  std::vector<std::string>::const_iterator itKey;
192  std::string metaString;
193 
194  double x, y, z;
195  double b0 = 0;
196 
197  std::cout.precision(10);
198  switch (operation)
199  {
200  case OP_NULL:
201  std::cout << "Dimension =" << std::flush;
202  for (unsigned int k=0; k<ImageDimension; k++)
203  {
204  std::cout << " " << inputSize[k] << std::flush;
205  }
206  std::cout << std::endl;
207 
208  std::cout << "Spacing =" << std::flush;
209  for (unsigned int k=0; k<ImageDimension; k++)
210  {
211  std::cout << " " << inputSpacing[k] << std::flush;
212  }
213  std::cout << std::endl;
214 
215  std::cout << "Origin =" << std::flush;
216  for (unsigned int k=0; k<ImageDimension; k++)
217  {
218  std::cout << " " << inputOrigin[k] << std::flush;
219  }
220  std::cout << std::endl;
221 
222  std::cout << "ComponentType = " << inputComponentTypeAsString
223  << std::endl;
224  std::cout << "PixelType = " << inputPixelTypeAsString << std::endl;
225  std::cout << "NumberOfComponents = " << inputNumberOfComponentsPerPixel
226  << std::endl;
227  std::cout << "ImageSizeInBytes = " << inputImageSizeInBytes << std::endl;
228  std::cout << "ImageSizeInComponents = " << inputImageSizeInComponents
229  << std::endl;
230  std::cout << "ImageSizeInPixels = " << inputImageSizeInPixels << std::endl;
231 // std::cout << "UseCompression = " << useCompression << std::endl;
232 // std::cout << "ByteOrder = " << inputByteOrderAsString << std::endl;
233  std::cout << "DirectionType = [" << std::flush;
234  for (unsigned int r=0; r<ImageDimension; r++)
235  {
236  for (unsigned int c=0; c<ImageDimension; c++)
237  {
238  std::cout << inputDirection(r,c) << std::flush;
239  if (c < ImageDimension - 1)
240  {
241  std::cout << " " << std::flush;
242  }
243  }
244  if (r < ImageDimension - 1)
245  {
246  std::cout << "; " << std::flush;
247  }
248  }
249  std::cout << "]" << std::endl;
250 
251  break;
252  case OP_DIMENSION:
253  std::cout << inputSize[_ImageDimension] << std::flush;
254  break;
255 
256  case OP_SPACING:
257 // std::cout << vcl_abs(inputSpacing[_ImageSpacing]);
258  std::cout << inputSpacing[_ImageSpacing] << std::flush;
259  break;
260 
261  case OP_COMPONENT_TYPE:
262  std::cout << inputComponentTypeAsString;
263  break;
264 
265  case OP_PIXEL_TYPE:
266  std::cout << inputPixelTypeAsString;
267  break;
268 
270  std::cout << inputNumberOfComponentsPerPixel;
271  break;
272 
273  case OP_SIZE_IN_BYTES:
274  std::cout << inputImageSizeInBytes;
275  break;
276 
278  std::cout << inputImageSizeInComponents;
279  break;
280 
281  case OP_SIZE_IN_PIXELS:
282  std::cout << inputImageSizeInPixels;
283  break;
284 
285  case OP_ORIGIN:
286  std::cout << inputOrigin;
287  break;
288 
289  case OP_META_DATA:
290  for (itKey = MetaKeys.begin(); itKey != MetaKeys.end(); ++itKey)
291  {
292  itk::ExposeMetaData < std::string
293  > (MetaDictionary, *itKey, metaString);
294  std::cout << *itKey << " ---> " << metaString << std::endl;
295  }
296  break;
297 
298  case OP_GRADIENTS:
299  for (itKey = MetaKeys.begin(); itKey != MetaKeys.end(); ++itKey)
300  {
301  itk::ExposeMetaData < std::string> (MetaDictionary, *itKey, metaString);
302  if (itKey->find("DWMRI_gradient") != std::string::npos)
303  {
304  sscanf(metaString.c_str(), "%lf %lf %lf\n", &x, &y, &z);
305  // std::cout << x << " " << y << " " << " " << z << std::endl;
306  printf("%lf %lf %lf\n",x,y,z); // To ensure proper display formatting
307  }
308  }
309  break;
310 
311  case OP_BVALUE:
312  for (itKey = MetaKeys.begin(); itKey != MetaKeys.end(); ++itKey)
313  {
314  itk::ExposeMetaData < std::string
315  > (MetaDictionary, *itKey, metaString);
316  if (itKey->find("DWMRI_b-value") != std::string::npos)
317  {
318  b0 = atof(metaString.c_str());
319  // std::cout << b0 << std::endl;
320  printf("%lf\n",b0); // To ensure proper display formatting
321  }
322  }
323  break;
324 
325  case OP_BVALUES:
326  for (itKey = MetaKeys.begin(); itKey != MetaKeys.end(); ++itKey)
327  {
328  itk::ExposeMetaData < std::string
329  > (MetaDictionary, *itKey, metaString);
330  if (itKey->find("DWMRI_b-value") != std::string::npos)
331  {
332  b0 = atof(metaString.c_str());
333  // printf("%lf\n",b0); // To ensure proper display formatting
334  }
335  }
336  for (itKey = MetaKeys.begin(); itKey != MetaKeys.end(); ++itKey)
337  {
338  itk::ExposeMetaData < std::string
339  > (MetaDictionary, *itKey, metaString);
340  if (itKey->find("DWMRI_gradient") != std::string::npos)
341  {
342  sscanf(metaString.c_str(), "%lf %lf %lf\n", &x, &y, &z);
343  // printf("%lf %lf %lf\n",x,y,z); // To ensure proper display formatting
344  printf("%lf\n", (x*x+y*y+z*z)*b0); // To ensure proper display formatting
345  }
346  }
347  break;
348 
349  default:
350  break;
351  }
352 
353  return EXIT_SUCCESS;
354 }
int GetImageType(const std::string &filename)
Definition: utlITK.h:203
void IntRangeCheck(int x, int LowerLimit, int UpperLimit)
Definition: ImageInfo.cxx:72
void SetOperationWithChecking(int &operation, int value)
Definition: ImageInfo.cxx:58
int show_file(const std::string &file)
Definition: ImageInfo.cxx:82
int main(int argc, char *argv[])
Definition: ImageInfo.cxx:98