Orfeo Toolbox  3.16
itkBruker2DSEQImageIO.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkBruker2DSEQImageIO.cxx,v $
5  Language: C++
6  Date: $Date: 2009-12-01 18:04:54 $
7  Version: $Revision: 1.9 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #include "itkBruker2DSEQImageIO.h"
19 #include "itkIOCommon.h"
20 #include "itkExceptionObject.h"
21 #include "itkByteSwapper.h"
22 #include "itkMetaDataObject.h"
23 #include <itksys/SystemTools.hxx>
24 #include <fstream>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <vector>
28 #include <algorithm>
29 #include <locale>
30 
40 namespace itk
41 {
42 const char *const RECO_BYTE_ORDER = "##$RECO_byte_order";
43 const char *const RECO_FOV = "##$RECO_fov";
44 const char *const RECO_SIZE = "##$RECO_size";
45 const char *const RECO_WORDTYPE = "##$RECO_wordtype";
46 const char *const RECO_IMAGE_TYPE = "##$RECO_image_type";
47 const char *const RECO_TRANSPOSITION = "##$RECO_transposition";
48 const char *const ACQ_DIM = "##$ACQ_dim";
49 const char *const NI = "##$NI";
50 const char *const NR = "##$NR";
51 const char *const ACQ_SLICE_THICK = "##$ACQ_slice_thick";
52 const char *const NECHOES = "##$NECHOES";
53 const char *const ACQ_SLICE_SEPN = "##$ACQ_slice_sepn";
54 const char *const ACQ_SLICE_SEPN_MODE = "##$ACQ_slice_sepn_mode";
55 const char *const ACQ_ECHO_TIME = "##$ACQ_echo_time";
56 const char *const ACQ_REPETITION_TIME = "##$ACQ_repetition_time";
57 const char *const ACQ_INVERSION_TIME = "##$ACQ_inversion_time";
58 
59 #define FORWARDSLASH_DIRECTORY_SEPARATOR '/'
60 
61 #define RECO_FILE "reco"
62 #define ACQP_FILE "acqp"
63 #define DTHREEPROC_FILE "d3proc"
64 #define RECO_byte_order "##$RECO_byte_order="
65 #define BRUKER_LITTLE_ENDIAN "littleEndian"
66 #define BRUKER_BIG_ENDIAN "bigEndian"
67 #define RECO_fov "##$RECO_fov=("
68 #define RECO_size "##$RECO_size=("
69 #define RECO_wordtype "##$RECO_wordtype="
70 #define RECO_image_type "##$RECO_image_type="
71 #define RECO_transposition "##$RECO_transposition=("
72 #define MAGNITUDE_IMAGE "MAGNITUDE_IMAGE"
73 #define REAL_IMAGE "REAL_IMAGE"
74 #define IMAGINARY_IMAGE "IMAGINARY_IMAGE"
75 #define COMPLEX_IMAGE "COMPLEX_IMAGE"
76 #define PHASE_IMAGE "PHASE_IMAGE"
77 #define IR_IMAGE "IR_IMAGE"
78 #define ACQ_dim "##$ACQ_dim="
79 #define Ni "##$NI="
80 #define Nr "##$NR="
81 #define Nechoes "##$NECHOES="
82 #define ACQ_slice_thick "##$ACQ_slice_thick="
83 #define ACQ_slice_sepn "##$ACQ_slice_sepn=("
84 #define ACQ_slice_sepn_mode "##$ACQ_slice_sepn_mode="
85 #define BRUKER_SIGNED_CHAR "_8BIT_SGN_INT"
86 #define BRUKER_UNSIGNED_CHAR "_8BIT_UNSGN_INT"
87 #define BRUKER_SIGNED_SHORT "_16BIT_SGN_INT"
88 #define BRUKER_SIGNED_INT "_32BIT_SGN_INT"
89 #define BRUKER_FLOAT "_32BIT_FLOAT"
90 #define ACQ_echo_time "##$ACQ_echo_time=("
91 #define ACQ_repetition_time "##$ACQ_repetition_time=("
92 #define ACQ_inversion_time "##$ACQ_inversion_time=("
93 #define ACQ_grad_matrix "##$ACQ_grad_matrix=("
94 #define DATTYPE "##$DATTYPE="
95 #define IM_SIX "##$IM_SIX="
96 #define IM_SIY "##$IM_SIY="
97 #define IM_SIZ "##$IM_SIZ="
98 #define IP_CHAR "ip_char"
99 #define IP_SHORT "ip_short"
100 #define IP_INT "ip_int"
101 
102 void
104  unsigned long numberOfPixels )
105 {
106  if ( m_ByteOrder == LittleEndian )
107  {
108  switch(this->m_ComponentType)
109  {
110  case CHAR:
112  ((char*)buffer, numberOfPixels );
113  break;
114  case UCHAR:
116  ((unsigned char*)buffer, numberOfPixels );
117  break;
118  case SHORT:
120  ((short*)buffer, numberOfPixels );
121  break;
122  case USHORT:
124  ((unsigned short*)buffer, numberOfPixels );
125  break;
126  case INT:
128  ((int*)buffer, numberOfPixels );
129  break;
130  case UINT:
132  ((unsigned int*)buffer, numberOfPixels );
133  break;
134  case LONG:
136  ((long*)buffer, numberOfPixels );
137  break;
138  case ULONG:
140  ((unsigned long*)buffer, numberOfPixels );
141  break;
142  case FLOAT:
144  ((float*)buffer, numberOfPixels );
145  break;
146  case DOUBLE:
148  ((double*)buffer, numberOfPixels );
149  break;
150  default:
151  ExceptionObject exception(__FILE__, __LINE__,
152  "Component Type Unknown",
153  ITK_LOCATION);
154  throw exception;
155  }
156  }
157  else
158  {
159  switch(this->m_ComponentType)
160  {
161  case CHAR:
163  ((char *)buffer, numberOfPixels );
164  break;
165  case UCHAR:
167  ((unsigned char *)buffer, numberOfPixels );
168  break;
169  case SHORT:
171  ((short *)buffer, numberOfPixels );
172  break;
173  case USHORT:
175  ((unsigned short *)buffer, numberOfPixels );
176  break;
177  case INT:
179  ((int *)buffer, numberOfPixels );
180  break;
181  case UINT:
183  ((unsigned int *)buffer, numberOfPixels );
184  break;
185  case LONG:
187  ((long *)buffer, numberOfPixels );
188  break;
189  case ULONG:
191  ((unsigned long *)buffer, numberOfPixels );
192  break;
193  case FLOAT:
195  ((float *)buffer, numberOfPixels );
196  break;
197  case DOUBLE:
199  ((double *)buffer, numberOfPixels );
200  break;
201  default:
202  ExceptionObject exception(__FILE__, __LINE__,
203  "Component Type Unknown",
204  ITK_LOCATION);
205  throw exception;
206  }
207  }
208 }
209 
211 {
212  //by default, only have 3 dimensions
213  this->SetNumberOfDimensions(3);
214  this->m_PixelType = SCALAR;
215  this->m_ComponentType = CHAR;
216  this->SetNumberOfComponents(1);
217 
218  // Set m_MachineByteOrder to the ByteOrder of the machine
219  // Start out with file byte order == system byte order
220  // this will be changed if we're reading a file to whatever
221  // the file actually contains.
223  {
224  this->m_MachineByteOrder = this->m_ByteOrder = BigEndian;
225  }
226  else
227  {
229  }
230 }
231 
233 {
234  // Left blank on purpose.
235 }
236 
237 void Bruker2DSEQImageIO::PrintSelf(std::ostream& os, Indent indent) const
238 {
239  Superclass::PrintSelf(os, indent);
240 }
241 
242 
243 void Bruker2DSEQImageIO::Read(void* buffer)
244 {
245  unsigned int dim;
246  const unsigned int dimensions = this->GetNumberOfDimensions();
247  unsigned int numberOfPixels = 1;
248  char * const p = static_cast<char *>(buffer);
249 
250  for(dim=0; dim< dimensions; dim++ )
251  {
252  numberOfPixels *= this->m_Dimensions[ dim ];
253  }
254 
255  /* Get the 2dseq filename */
256  /* Use the same code as CanReadFile() */
257  std::string file2Dseq =
258  itksys::SystemTools::CollapseFullPath(this->m_FileName.c_str());
259  itksys::SystemTools::ConvertToUnixSlashes(file2Dseq);
260  /* Try to open the file */
261  std::ifstream twodseq_InputStream;
262  twodseq_InputStream.imbue(std::locale::classic());
263  twodseq_InputStream.open( file2Dseq.c_str(), std::ios::in | std::ios::binary );
264 
265  if( twodseq_InputStream.fail() )
266  {
267  OStringStream message;
268  message << "The Brucker2DSEG Data File can not be opened. "
269  << "The following file was attempted:" << std::endl
270  << file2Dseq;
271  ExceptionObject exception(__FILE__, __LINE__,
272  message.str(),
273  ITK_LOCATION);
274  throw exception;
275  }
276 
277  twodseq_InputStream.read(p, Math::CastWithRangeCheck< std::streamsize, SizeType >( this->GetImageSizeInBytes() ) );
278 
279  if( twodseq_InputStream.fail() )
280  {
281  OStringStream message;
282  message << "The Brucker2DSEG Data File can not be read. "
283  << "The following file was attempted:" << std::endl
284  << file2Dseq;
285  ExceptionObject exception(__FILE__, __LINE__,
286  message.str(),
287  ITK_LOCATION);
288  throw exception;
289  }
290  twodseq_InputStream.close();
291  this->SwapBytesIfNecessary( buffer, numberOfPixels );
292 }
293 
294 bool Bruker2DSEQImageIO::CanReadFile( const char* FileNameToRead )
295 {
296  std::string file2Dseq = itksys::SystemTools::CollapseFullPath(FileNameToRead);
297  itksys::SystemTools::ConvertToUnixSlashes(file2Dseq);
298  std::string path = itksys::SystemTools::GetFilenamePath(file2Dseq);
299  std::string filereco = path + FORWARDSLASH_DIRECTORY_SEPARATOR;
300  filereco += RECO_FILE;
301  std::string filed3proc = path + FORWARDSLASH_DIRECTORY_SEPARATOR;
302  filed3proc += DTHREEPROC_FILE;
303  std::vector<std::string> pathComponents;
304  itksys::SystemTools::SplitPath(path.c_str(), pathComponents);
305  if(pathComponents.size() < 3)
306  {
307  return false;
308  }
309  // Go two directories up.
310  pathComponents.pop_back();pathComponents.pop_back();
311  path = itksys::SystemTools::JoinPath(pathComponents);
312  std::string fileacqp = path + FORWARDSLASH_DIRECTORY_SEPARATOR;
313  fileacqp += ACQP_FILE;
314  std::string readFileBufferString = "";
315  char readFileBuffer[512] = "";
316  std::string::size_type index;
317  unsigned long length2DSEQ = 0;
318  unsigned long calcLength = 1;
319 
320  // Does the '2dseq' file exist?
321  if( !itksys::SystemTools::FileExists(file2Dseq.c_str()) )
322  {
323  return false;
324  }
325 
326  // get length of file in bytes:
327  length2DSEQ = itksys::SystemTools::FileLength(file2Dseq.c_str());
328  //std::cout << "length2DSEQ = " << length2DSEQ << std::endl;
329 
330  // Check reco for existance.
331  std::ifstream reco_InputStream;
332  reco_InputStream.open( filereco.c_str(),
333  std::ios::in );
334  if( reco_InputStream.fail() )
335  {
336  return false;
337  }
338  reco_InputStream.imbue(std::locale::classic());
339  while( !reco_InputStream.eof() )
340  {
341  reco_InputStream.getline(readFileBuffer, sizeof(readFileBuffer));
342  readFileBufferString = readFileBuffer;
343 
344  // Get the image data type.
345  index = readFileBufferString.find(RECO_wordtype);
346  if( index != std::string::npos )
347  {
348  std::string tempString = RECO_wordtype;
349  std::string dattypeString =
350  readFileBufferString.substr(index+tempString.length());
351  if( dattypeString.find(BRUKER_SIGNED_CHAR) != std::string::npos )
352  {
353  calcLength *= (unsigned long)sizeof(char);
354  }
355  else if( dattypeString.find(BRUKER_UNSIGNED_CHAR) != std::string::npos )
356  {
357  calcLength *= (unsigned long)sizeof(unsigned char);
358  }
359  else if( dattypeString.find(BRUKER_SIGNED_SHORT) != std::string::npos )
360  {
361  calcLength *= (unsigned long)sizeof(short);
362  }
363  else if( dattypeString.find(BRUKER_SIGNED_INT) != std::string::npos )
364  {
365  calcLength *= (unsigned long)sizeof(int);
366  }
367  else if( dattypeString.find(BRUKER_FLOAT) != std::string::npos )
368  {
369  calcLength *= (unsigned long)sizeof(float);
370  }
371  else
372  {
373  reco_InputStream.close();
374  return false;
375  }
376  //std::cout << "calcLength = " << calcLength << std::endl;
377  }
378  }
379  reco_InputStream.close();
380 
381  // Check acqp for existance.
382  //std::cout << "fileacqp = " << fileacqp << std::endl;
383  if( !itksys::SystemTools::FileExists(fileacqp.c_str()) )
384  {
385  return false;
386  }
387 
388  // Check d3proc for existance.
389  std::ifstream d3proc_InputStream;
390  d3proc_InputStream.open( filed3proc.c_str(),
391  std::ios::in );
392  if( d3proc_InputStream.fail() )
393  {
394  return false;
395  }
396  d3proc_InputStream.imbue(std::locale::classic());
397  while( !d3proc_InputStream.eof() )
398  {
399  d3proc_InputStream.getline(readFileBuffer, sizeof(readFileBuffer));
400  readFileBufferString = readFileBuffer;
401 
402  // Get the image data type.
403  // This method is not a reliable method for determining the data type.
404  // Using RECO_wordtype instead.
405  //index = readFileBufferString.find(DATTYPE);
406  //if( index != std::string::npos )
407  // {
408  // std::string tempString = DATTYPE;
409  // std::string dattypeString =
410  // readFileBufferString.substr(index+tempString.length());
411  // if( dattypeString.find(IP_CHAR) != std::string::npos )
412  // {
413  // calcLength *= 1;
414  // }
415  // else if( (dattypeString.find(IP_SHORT) != std::string::npos) ||
416  // (dattypeString.find("3") != std::string::npos) )
417  // {
418  // calcLength *= 2;
419  // }
420  // else if( (dattypeString.find(IP_INT) != std::string::npos) ||
421  // (dattypeString.find("5") != std::string::npos) )
422  // {
423  // calcLength *= 4;
424  // }
425  // else
426  // {
427  // std::cerr << "FIX ME: Unknown DATTYPE = ";
428  // std::cerr << dattypeString << std::endl;
429  // }
430  // std::cout << "calcLength = " << calcLength << std::endl;
431  //}
432 
433  // Get the x size.
434  index = readFileBufferString.find(IM_SIX);
435  if( index != std::string::npos )
436  {
437  unsigned long xDim = 0;
438  std::string tempString = IM_SIX;
439  std::istringstream im_sixString(readFileBufferString.substr(
440  index+tempString.length()));
441  if (!im_sixString)
442  {
443  d3proc_InputStream.close();
444  return false;
445  }
446  im_sixString >> xDim;
447  //std::cout << "xDim = " << xDim << std::endl;
448  calcLength *= xDim;
449  //std::cout << "calcLength = " << calcLength << std::endl;
450  }
451 
452  // Get the y size.
453  index = readFileBufferString.find(IM_SIY);
454  if( index != std::string::npos )
455  {
456  unsigned long yDim = 0;
457  std::string tempString = IM_SIY;
458  std::istringstream im_siyString(readFileBufferString.substr(
459  index+tempString.length()));
460  if (!im_siyString)
461  {
462  d3proc_InputStream.close();
463  return false;
464  }
465  im_siyString >> yDim;
466  //std::cout << "yDim = " << yDim << std::endl;
467  calcLength *= yDim;
468  //std::cout << "calcLength = " << calcLength << std::endl;
469  }
470 
471  // Get the z size.
472  index = readFileBufferString.find(IM_SIZ);
473  if( index != std::string::npos )
474  {
475  unsigned long zDim = 0;
476  std::string tempString = IM_SIZ;
477  std::istringstream im_sizString(readFileBufferString.substr(
478  index+tempString.length()));
479  if (!im_sizString)
480  {
481  d3proc_InputStream.close();
482  return false;
483  }
484  im_sizString >> zDim;
485  //std::cout << "zDim = " << zDim << std::endl;
486  calcLength *= zDim;
487  //std::cout << "calcLength = " << calcLength << std::endl;
488  }
489 
490  }
491  d3proc_InputStream.close();
492 
493  // Compare the file length to the calculated length.
494  // Are they equal?
495  if( calcLength != length2DSEQ )
496  {
497  return false;
498  }
499 
500  return true;
501 }
502 
504 {
505  unsigned int dim;
506  std::string file2Dseq =
507  itksys::SystemTools::CollapseFullPath(this->m_FileName.c_str());
508  itksys::SystemTools::ConvertToUnixSlashes(file2Dseq);
509  std::string path = itksys::SystemTools::GetFilenamePath(file2Dseq);
510  std::string filereco = path + FORWARDSLASH_DIRECTORY_SEPARATOR;
511  filereco += RECO_FILE;
512  std::string filed3proc = path + FORWARDSLASH_DIRECTORY_SEPARATOR;
513  filed3proc += DTHREEPROC_FILE;
514  std::vector<std::string> pathComponents;
515  itksys::SystemTools::SplitPath(path.c_str(), pathComponents);
516  if(pathComponents.size() < 3)
517  {
518  OStringStream message;
519  message << "Cannot create path for acqp file: "
520  << path << std::endl
521  << "Path Components: ";
522  for (unsigned int i = 0; i < pathComponents.size(); i++)
523  {
524  message << pathComponents[i] << "' ";
525  }
526  ExceptionObject exception(__FILE__, __LINE__,
527  message.str(),
528  ITK_LOCATION);
529  throw exception;
530  }
531  // Go two directories up.
532  pathComponents.pop_back();pathComponents.pop_back();
533  path = itksys::SystemTools::JoinPath(pathComponents);
534  std::string fileacqp = path + FORWARDSLASH_DIRECTORY_SEPARATOR;
535  fileacqp += ACQP_FILE;
536  std::string readFileBufferString = "";
537  char readFileBuffer[512] = "";
538  std::string::size_type index;
539  std::string::size_type tempIndex = 0;
540  std::vector<double> imageFOV(3);
541  std::vector<unsigned int> imageDim(3);
542  bool numDimensions = false;
543  bool byteOrder = false;
544  bool slicesNotInSameOrientation = false;
545  bool echoTime = false;
546  bool repetitionTime = false;
547  bool inversionTime = false;
548  int ni = 0;
549  int nr = 0;
550  unsigned int numEchoImages = 0;
551  bool sliceThickness = false;
552  bool sliceSeperation = false;
553  int numSeperation = 0;
554  int numRecoTranspose = -1;
555  double sliceThick = 0;
556  std::string seperationMode = "";
557  std::vector<double> dirx(3,0),diry(3,0),dirz(3,0);
558  std::vector<int> recoTransposition;
559  int acq_dim = -1;
560  int transpose = 0;
561 
562  // Get the meta dictionary for this object.
563  MetaDataDictionary &thisDic=this->GetMetaDataDictionary();
564  std::string classname(this->GetNameOfClass());
565  EncapsulateMetaData<std::string>(thisDic,ITK_InputFilterName, classname);
566 
567  std::ifstream d3proc_InputStream;
568  d3proc_InputStream.open( filed3proc.c_str(),
569  std::ios::in );
570  if( d3proc_InputStream.fail() )
571  {
572  OStringStream message;
573  message << "d3proc file: " << filed3proc << " cannot be opened.";
574  ExceptionObject exception(__FILE__, __LINE__,
575  message.str(),
576  ITK_LOCATION);
577  throw exception;
578  }
579  d3proc_InputStream.imbue(std::locale::classic());
580  while( !d3proc_InputStream.eof() )
581  {
582  d3proc_InputStream.getline(readFileBuffer, sizeof(readFileBuffer));
583  readFileBufferString = readFileBuffer;
584 
585  // Get the image data type.
586  // This method is not a reliable method for determining the data type.
587  // Using RECO_wordtype instead.
588  //index = readFileBufferString.find(DATTYPE);
589  //if( index != std::string::npos )
590  // {
591  // std::string tempString = DATTYPE;
592  // std::string dattypeString =
593  // readFileBufferString.substr(index+tempString.length());
594  // if( dattypeString.find(IP_CHAR) != std::string::npos )
595  // {
596  // this->m_ComponentType = CHAR;
597  // this->m_PixelType = SCALAR;
598  // }
599  // else if( (dattypeString.find(IP_SHORT) != std::string::npos) ||
600  // (dattypeString.find("3") != std::string::npos) )
601  // {
602  // this->m_ComponentType = SHORT;
603  // this->m_PixelType = SCALAR;
604  // }
605  // else if( (dattypeString.find(IP_INT) != std::string::npos) ||
606  // (dattypeString.find("5") != std::string::npos) )
607  // {
608  // this->m_ComponentType = INT;
609  // this->m_PixelType = SCALAR;
610  // }
611  // else
612  // {
613  // ExceptionObject exception(__FILE__, __LINE__);
614  // exception.SetDescription("Invalid d3proc file: "
615  // "Couldn't locate data type string");
616  // throw exception;
617  // }
618  // }
619 
620  // Get the x size.
621  index = readFileBufferString.find(IM_SIX);
622  if( index != std::string::npos )
623  {
624  std::string tempString = IM_SIX;
625  std::istringstream im_sixString(readFileBufferString.substr(
626  index+tempString.length()));
627  if (!im_sixString)
628  {
629  d3proc_InputStream.close();
630  OStringStream message;
631  message << "Could not create std::istringstream for "
632  << "##$IM_SIX" << std::endl
633  << ". File is "
634  << filed3proc;
635  ExceptionObject exception(__FILE__, __LINE__,
636  message.str(),
637  ITK_LOCATION);
638  throw exception;
639  }
640  im_sixString >> imageDim[0];
641  }
642 
643  // Get the y size.
644  index = readFileBufferString.find(IM_SIY);
645  if( index != std::string::npos )
646  {
647  std::string tempString = IM_SIY;
648  std::istringstream im_siyString(readFileBufferString.substr(
649  index+tempString.length()));
650  if (!im_siyString)
651  {
652  d3proc_InputStream.close();
653  OStringStream message;
654  message << "Could not create std::istringstream for "
655  << "##$IM_SIY" << std::endl
656  << ". File is "
657  << filed3proc;
658  ExceptionObject exception(__FILE__, __LINE__,
659  message.str(),
660  ITK_LOCATION);
661  throw exception;
662  }
663  im_siyString >> imageDim[1];
664  }
665 
666  // Get the z size.
667  index = readFileBufferString.find(IM_SIZ);
668  if( index != std::string::npos )
669  {
670  std::string tempString = IM_SIZ;
671  std::istringstream im_sizString(readFileBufferString.substr(
672  index+tempString.length()));
673  if (!im_sizString)
674  {
675  d3proc_InputStream.close();
676  OStringStream message;
677  message << "Could not create std::istringstream for "
678  << "##$IM_SIZ" << std::endl
679  << ". File is "
680  << filed3proc;
681  ExceptionObject exception(__FILE__, __LINE__,
682  message.str(),
683  ITK_LOCATION);
684  throw exception;
685  }
686  im_sizString >> imageDim[2];
687  }
688 
689  }
690  d3proc_InputStream.close();
691 
692  std::ifstream reco_InputStream;
693  reco_InputStream.open(filereco.c_str(),
694  std::ios::in );
695  if( reco_InputStream.fail())
696  {
697  OStringStream message;
698  message << "reco file: " << filereco << " cannot be opened";
699  ExceptionObject exception(__FILE__, __LINE__,
700  message.str(),
701  ITK_LOCATION);
702  throw exception;
703  }
704  reco_InputStream.imbue(std::locale::classic());
705  while( !reco_InputStream.eof() )
706  {
707  reco_InputStream.getline(readFileBuffer, sizeof(readFileBuffer));
708  readFileBufferString = readFileBuffer;
709 
710  // Set number of dimensions and get fov.
711  index = readFileBufferString.find(RECO_fov);
712  if( index != std::string::npos )
713  {
715  tempIndex = readFileBufferString.find("2");
716  if( tempIndex != std::string::npos )
717  {
718  reco_InputStream >> imageFOV[0] >> imageFOV[1];
719  imageFOV[2] = 0;
720  tempRecoFOV->resize(2);
721  tempRecoFOV->SetElement(0, imageFOV[0]);
722  tempRecoFOV->SetElement(1, imageFOV[1]);
723  numDimensions = true;
724  }
725  else
726  {
727  tempIndex = readFileBufferString.find("3");
728  if( tempIndex != std::string::npos )
729  {
730  reco_InputStream >> imageFOV[0] >> imageFOV[1] >> imageFOV[2];
731  tempRecoFOV->resize(3);
732  tempRecoFOV->SetElement(0, imageFOV[0]);
733  tempRecoFOV->SetElement(1, imageFOV[1]);
734  tempRecoFOV->SetElement(2, imageFOV[2]);
735  numDimensions = true;
736  }
737  else
738  {
739  reco_InputStream.close();
740  OStringStream message;
741  message << "Invalid reco file: Couldn't locate proper "
742  << "fov parameters" << std::endl
743  << "Reco file is "
744  << filereco;
745  ExceptionObject exception(__FILE__, __LINE__,
746  message.str(),
747  ITK_LOCATION);
748  throw exception;
749  }
750  }
751  EncapsulateMetaData<RECOFOVContainerType::Pointer>(
752  thisDic,RECO_FOV,tempRecoFOV);
753  }
754 
755  // Get reco size.
756  index = readFileBufferString.find(RECO_size);
757  if( index != std::string::npos )
758  {
759  unsigned int tempRecoSize = 2;
760  tempIndex = readFileBufferString.find("2");
761  if( tempIndex == std::string::npos )
762  {
763  tempIndex = readFileBufferString.find("3");
764  tempRecoSize = 3;
765  if( tempIndex == std::string::npos )
766  {
767  reco_InputStream.close();
768  OStringStream message;
769  message << "Invalid reco file: Couldn't locate proper "
770  << "dimension parameters" << std::endl
771  << "Reco file is "
772  << filereco;
773  ExceptionObject exception(__FILE__, __LINE__,
774  message.str(),
775  ITK_LOCATION);
776  throw exception;
777  }
778  }
779  EncapsulateMetaData<unsigned int>(thisDic,RECO_SIZE,tempRecoSize);
780  }
781 
782  // Get data type
783  index = readFileBufferString.find(RECO_wordtype);
784  if( index != std::string::npos )
785  {
786  //std::cout << readFileBufferString.c_str() << std::endl;
787  tempIndex = readFileBufferString.find(BRUKER_SIGNED_CHAR);
788  if( tempIndex != std::string::npos )
789  {
790  this->m_ComponentType = CHAR;
791  this->m_PixelType = SCALAR;
792  EncapsulateMetaData<std::string>(
793  thisDic,RECO_WORDTYPE,std::string(BRUKER_SIGNED_CHAR,13));
794  }
795  else
796  {
797  tempIndex = readFileBufferString.find(BRUKER_UNSIGNED_CHAR);
798  if( tempIndex != std::string::npos )
799  {
800  this->m_ComponentType = UCHAR;
801  this->m_PixelType = SCALAR;
802  EncapsulateMetaData<std::string>(
803  thisDic,RECO_WORDTYPE,std::string(BRUKER_UNSIGNED_CHAR,15));
804  }
805  else
806  {
807  tempIndex = readFileBufferString.find(BRUKER_SIGNED_SHORT);
808  if( tempIndex != std::string::npos )
809  {
810  this->m_ComponentType = SHORT;
811  this->m_PixelType = SCALAR;
812  EncapsulateMetaData<std::string>(
813  thisDic,RECO_WORDTYPE,std::string(BRUKER_SIGNED_SHORT,14));
814  }
815  else
816  {
817  tempIndex = readFileBufferString.find(BRUKER_SIGNED_INT);
818  if( tempIndex != std::string::npos )
819  {
820  this->m_ComponentType = INT;
821  this->m_PixelType = SCALAR;
822  EncapsulateMetaData<std::string>(
823  thisDic,RECO_WORDTYPE,std::string(BRUKER_SIGNED_INT,14));
824  }
825  else
826  {
827  tempIndex = readFileBufferString.find(BRUKER_FLOAT);
828  if( tempIndex != std::string::npos )
829  {
830  this->m_ComponentType = FLOAT;
831  this->m_PixelType = SCALAR;
832  EncapsulateMetaData<std::string>(
833  thisDic,RECO_WORDTYPE,std::string(BRUKER_FLOAT,12));
834  }
835  else
836  {
837  reco_InputStream.close();
838  OStringStream message;
839  message << "Invalid reco file: Couldn't locate proper "
840  << "wordtype parameter" << std::endl
841  << "Reco file is "
842  << filereco;
843  ExceptionObject exception(__FILE__, __LINE__,
844  message.str(),
845  ITK_LOCATION);
846  throw exception;
847  }
848  }
849  }
850  }
851  }
852  }
853 
854  // OK, handle RECO_transposition!
855  index = readFileBufferString.find(RECO_transposition);
856  if( index != std::string::npos )
857  {
858  std::string tempString = RECO_transposition;
859  std::istringstream recoTransposeString(readFileBufferString.substr(
860  index+tempString.length()));
861  if (!recoTransposeString)
862  {
863  reco_InputStream.close();
864  OStringStream message;
865  message << "Could not create std::istringstream for "
866  << "##$RECO_transposition" << std::endl
867  << "Reco file is "
868  << filereco;
869  ExceptionObject exception(__FILE__, __LINE__,
870  message.str(),
871  ITK_LOCATION);
872  throw exception;
873  }
874  recoTransposeString >> numRecoTranspose;
875  //std::cout << "numRecoTranspose = " << numRecoTranspose << std::endl;
876  if( numRecoTranspose > 0 )
877  {
878  RECOTranspositionContainerType::Pointer tempRecoTransposition =
880  recoTransposition.resize(numRecoTranspose);
881  tempRecoTransposition->resize(numRecoTranspose);
882  for(unsigned int i=0; i<(unsigned int)numRecoTranspose; i++)
883  {
884  reco_InputStream >> recoTransposition[i];
885  tempRecoTransposition->SetElement(i,recoTransposition[i]);
886  }
887  EncapsulateMetaData<RECOTranspositionContainerType::Pointer>(
888  thisDic,RECO_TRANSPOSITION,tempRecoTransposition);
889  }
890  }
891 
892  // Get RECO_image_type.
893  index = readFileBufferString.find(RECO_image_type);
894  if( index != std::string::npos )
895  {
896  std::string tempString = RECO_image_type;
897  std::string recoType = "";
898  recoType = readFileBufferString.substr(index+tempString.length());
899  if( recoType.find(MAGNITUDE_IMAGE) != std::string::npos )
900  {
901  EncapsulateMetaData<std::string>(
902  thisDic,RECO_IMAGE_TYPE,std::string(MAGNITUDE_IMAGE,15));
903  }
904  else if( recoType.find(REAL_IMAGE) != std::string::npos )
905  {
906  EncapsulateMetaData<std::string>(
907  thisDic,RECO_IMAGE_TYPE,std::string(REAL_IMAGE,10));
908  }
909  else if( recoType.find(IMAGINARY_IMAGE) != std::string::npos )
910  {
911  EncapsulateMetaData<std::string>(
912  thisDic,RECO_IMAGE_TYPE,std::string(IMAGINARY_IMAGE,15));
913  }
914  else if( recoType.find(COMPLEX_IMAGE) != std::string::npos )
915  {
916  EncapsulateMetaData<std::string>(
917  thisDic,RECO_IMAGE_TYPE,std::string(COMPLEX_IMAGE,15));
918  }
919  else if( recoType.find(PHASE_IMAGE) != std::string::npos )
920  {
921  EncapsulateMetaData<std::string>(
922  thisDic,RECO_IMAGE_TYPE,std::string(PHASE_IMAGE,11));
923  }
924  else if( recoType.find(IR_IMAGE) != std::string::npos )
925  {
926  EncapsulateMetaData<std::string>(
927  thisDic,RECO_IMAGE_TYPE,std::string(IR_IMAGE,8));
928  }
929  else
930  {
931  reco_InputStream.close();
932  OStringStream message;
933  message << "Invalid reco file: Couldn't locate proper"
934  << "datatype parameter" << std::endl
935  << "Reco file is "
936  << filereco;
937  ExceptionObject exception(__FILE__, __LINE__,
938  message.str(),
939  ITK_LOCATION);
940  throw exception;
941  }
942  }
943 
944  // Set byte order
945  index = readFileBufferString.find(RECO_byte_order);
946  if( index != std::string::npos )
947  {
948  tempIndex = readFileBufferString.find(BRUKER_LITTLE_ENDIAN);
949  if( tempIndex != std::string::npos )
950  {
951  this->m_ByteOrder = LittleEndian;
952  byteOrder = true;
953  EncapsulateMetaData<std::string>(
954  thisDic,RECO_BYTE_ORDER,std::string(BRUKER_LITTLE_ENDIAN,12));
955  }
956  else
957  {
958  tempIndex = readFileBufferString.find(BRUKER_BIG_ENDIAN);
959  if( tempIndex != std::string::npos )
960  {
961  this->m_ByteOrder = BigEndian;
962  byteOrder = true;
963  EncapsulateMetaData<std::string>(
964  thisDic,RECO_BYTE_ORDER,std::string(BRUKER_BIG_ENDIAN,9));
965  }
966  else
967  {
968  reco_InputStream.close();
969  OStringStream message;
970  message << "Invalid reco file: Couldn't locate proper"
971  << "byte order parameter" << std::endl
972  << "Reco file is "
973  << filereco;
974  ExceptionObject exception(__FILE__, __LINE__,
975  message.str(),
976  ITK_LOCATION);
977  throw exception;
978  }
979  }
980  }
981  }
982  reco_InputStream.close();
983 
984  if( !numDimensions )
985  {
986  OStringStream message;
987  message << "Invalid reco file: Couldn't locate "
988  << "'##$RECO_fov=(' tag" << std::endl
989  << "Reco file is "
990  << filereco;
991  ExceptionObject exception(__FILE__, __LINE__,
992  message.str(),
993  ITK_LOCATION);
994  throw exception;
995  }
996  if( !byteOrder )
997  {
998  OStringStream message;
999  message << "Invalid reco file: Couldn't locate "
1000  << "'##$RECO_byte_order=' tag" << std::endl
1001  << "Reco file is "
1002  << filereco;
1003  ExceptionObject exception(__FILE__, __LINE__,
1004  message.str(),
1005  ITK_LOCATION);
1006  throw exception;
1007  }
1008  if( numRecoTranspose < 0 )
1009  {
1010  OStringStream message;
1011  message << "Invalid reco file: Couldn't locate "
1012  << "'##$RECO_transposition=(' tag" << std::endl
1013  << "Reco file is "
1014  << filereco;
1015  ExceptionObject exception(__FILE__, __LINE__,
1016  message.str(),
1017  ITK_LOCATION);
1018  throw exception;
1019  }
1020 
1021  // Open the acqp file & extract relevant info.
1022  std::ifstream acqp_InputStream;
1023  std::string acqpFileString = "";
1024  acqp_InputStream.open(fileacqp.c_str(),
1025  std::ios::in );
1026  //std::cout << fileacqp.c_str() << std::endl;
1027  if( acqp_InputStream.fail() )
1028  {
1029  OStringStream message;
1030  message << "acqp file cannot be opened. "
1031  << "File is "
1032  << fileacqp;
1033  ExceptionObject exception(__FILE__, __LINE__,
1034  message.str(),
1035  ITK_LOCATION);
1036  throw exception;
1037  }
1038  acqp_InputStream.imbue(std::locale::classic());
1039  while( !acqp_InputStream.eof() )
1040  {
1041  int numEchoes = 0;
1042  int numRepetitions = 0;
1043  int numInversionTimes = 0;
1044  acqp_InputStream.getline(readFileBuffer, sizeof(readFileBuffer));
1045 
1046  acqpFileString = readFileBuffer;
1047 
1048  // Get ACQ_dim.
1049  index = acqpFileString.find(ACQ_dim);
1050  if( index != std::string::npos )
1051  {
1052  std::string tempString = ACQ_dim;
1053  std::istringstream acqDimString(acqpFileString.substr(
1054  index+tempString.length()));
1055  if (!acqDimString)
1056  {
1057  acqp_InputStream.close();
1058  OStringStream message;
1059  message << "Could not create std::istringstream for "
1060  << "##$ACQ_dim. "
1061  << "The file is "
1062  << fileacqp;
1063  ExceptionObject exception(__FILE__, __LINE__,
1064  message.str(),
1065  ITK_LOCATION);
1066  throw exception;
1067  }
1068  acqDimString >> acq_dim;
1069  //std::cout << "acq_dim = " << acq_dim << std::endl;
1070  EncapsulateMetaData<int>(thisDic,ACQ_DIM,acq_dim);
1071  }
1072 
1073  // Get the number of objects produced by a single repetition.
1074  // Number of slices multiplied by the number of echos.
1075  index = acqpFileString.find(Ni);
1076  if( index != std::string::npos )
1077  {
1078  std::string tempString = Ni;
1079  std::istringstream niString(acqpFileString.substr(
1080  index+tempString.length()));
1081  if (!niString)
1082  {
1083  acqp_InputStream.close();
1084  OStringStream message;
1085  message << "Could not create std::istringstream for "
1086  << "##$NI" << std::endl
1087  << "The file is "
1088  << fileacqp;
1089  ExceptionObject exception(__FILE__, __LINE__,
1090  message.str(),
1091  ITK_LOCATION);
1092  throw exception;
1093  }
1094  niString >> ni;
1095  //std::cout << "ni = " << ni << std::endl;
1096  EncapsulateMetaData<int>(thisDic,NI,ni);
1097  }
1098 
1099  // Get the number of repetitions of this experiment.
1100  index = acqpFileString.find(Nr);
1101  if( index != std::string::npos )
1102  {
1103  std::string tempString = Nr;
1104  std::istringstream nrString(acqpFileString.substr(
1105  index+tempString.length()));
1106  if (!nrString)
1107  {
1108  acqp_InputStream.close();
1109  OStringStream message;
1110  message << "Could not create std::istringstream for "
1111  << "##$NR" << std::endl
1112  << "The file is "
1113  << fileacqp;
1114  ExceptionObject exception(__FILE__, __LINE__,
1115  message.str(),
1116  ITK_LOCATION);
1117  throw exception;
1118  }
1119  nrString >> nr;
1120  //std::cout << "nr = " << nr << std::endl;
1121  EncapsulateMetaData<int>(thisDic,NR,nr);
1122  }
1123 
1124  // Get number of echo images.
1125  index = acqpFileString.find(Nechoes);
1126  if( index != std::string::npos )
1127  {
1128  std::string tempString = Nechoes;
1129  std::istringstream dimString(acqpFileString.substr(
1130  index+tempString.length()));
1131  if (!dimString)
1132  {
1133  acqp_InputStream.close();
1134  OStringStream message;
1135  message << "Could not create std::istringstream for "
1136  << "##$NECHOES" << std::endl
1137  << "The file is "
1138  << fileacqp;
1139  ExceptionObject exception(__FILE__, __LINE__,
1140  message.str(),
1141  ITK_LOCATION);
1142  throw exception;
1143  }
1144  dimString >> numEchoImages;
1145  //std::cout << "numEchoImages = " << numEchoImages << std::endl;
1146  EncapsulateMetaData<unsigned int>(thisDic,NECHOES,numEchoImages);
1147  }
1148 
1149  //Get the slice thickness
1150  index = acqpFileString.find(ACQ_slice_thick);
1151  if( index != std::string::npos )
1152  {
1153  std::string tempString = ACQ_slice_thick;
1154  std::istringstream sliceThickString(acqpFileString.substr(
1155  index+tempString.length()));
1156  if (!sliceThickString)
1157  {
1158  acqp_InputStream.close();
1159  OStringStream message;
1160  message << "Could not create std::istringstream for "
1161  << "##$ACQ_slice_thick" << std::endl
1162  << "The file is "
1163  << fileacqp;
1164  ExceptionObject exception(__FILE__, __LINE__,
1165  message.str(),
1166  ITK_LOCATION);
1167  throw exception;
1168  }
1169  sliceThickString >> sliceThick;
1170  //std::cout << "sliceThick = " << sliceThick << std::endl;
1171  sliceThickness = true;
1172  EncapsulateMetaData<double>(thisDic,ACQ_SLICE_THICK,sliceThick);
1173  }
1174 
1175  // Get the slice seperation.
1176  index = acqpFileString.find(ACQ_slice_sepn);
1177  if( index != std::string::npos )
1178  {
1179  std::string tempString = ACQ_slice_sepn;
1180  std::istringstream sliceSepString(acqpFileString.substr(
1181  index+tempString.length()));
1182  if (!sliceSepString)
1183  {
1184  acqp_InputStream.close();
1185  OStringStream message;
1186  message << "Could not create std::istringstream for "
1187  << "##$ACQ_slice_sepn" << std::endl
1188  << "The file is "
1189  << fileacqp;
1190  ExceptionObject exception(__FILE__, __LINE__,
1191  message.str(),
1192  ITK_LOCATION);
1193  throw exception;
1194  }
1195  sliceSepString >> numSeperation;
1196  //std::cout << "numSeperation = " << numSeperation << std::endl;
1197  if( numSeperation > 0 )
1198  {
1199  std::vector<double> imageSliceSeperation(numSeperation);
1202  sliceSepn->resize(numSeperation);
1203  for(unsigned int i=0; i<(unsigned int)numSeperation; i++)
1204  {
1205  acqp_InputStream >> imageSliceSeperation[i];
1206  sliceSepn->SetElement(i,imageSliceSeperation[i]);
1207  }
1208  EncapsulateMetaData<ACQSliceSepnContainerType::Pointer>(
1209  thisDic,ACQ_SLICE_SEPN,sliceSepn);
1210  sliceSeperation = true;
1211  }
1212  }
1213 
1214  // Get the slice seperation mode.
1215  index = acqpFileString.find(ACQ_slice_sepn_mode);
1216  if( index != std::string::npos )
1217  {
1218  std::string tempString = ACQ_slice_sepn_mode;
1219  seperationMode = acqpFileString.substr(index+tempString.length());
1220  //std::cout << "seperationMode = " << seperationMode << std::endl;
1221  EncapsulateMetaData<std::string>(
1222  thisDic,ACQ_SLICE_SEPN_MODE,seperationMode);
1223  }
1224 
1225  // Get echo times.
1226  index = acqpFileString.find(ACQ_echo_time);
1227  if( index != std::string::npos )
1228  {
1229  std::string tempString = ACQ_echo_time;
1230  std::istringstream echoTimeString(acqpFileString.substr(
1231  index+tempString.length()));
1232  if (!echoTimeString)
1233  {
1234  acqp_InputStream.close();
1235  OStringStream message;
1236  message << "Could not create std::istringstream for "
1237  << "##$ACQ_echo_time" << std::endl
1238  << "The file is "
1239  << fileacqp;
1240  ExceptionObject exception(__FILE__, __LINE__,
1241  message.str(),
1242  ITK_LOCATION);
1243  throw exception;
1244  }
1245  echoTimeString >> numEchoes;
1246  //std::cout << "numEchoes = " << numEchoes << std::endl;
1247  if( numEchoes > 0 )
1248  {
1249  std::vector<double> Echo_time(numEchoes);
1252  echoTimes->resize(numEchoes);
1253  for(unsigned int i=0; i<(unsigned int)numEchoes; i++)
1254  {
1255  acqp_InputStream >> Echo_time[i];
1256  echoTimes->SetElement(i,Echo_time[i]);
1257  }
1258  EncapsulateMetaData<ACQEchoTimeContainerType::Pointer>(
1259  thisDic,ACQ_ECHO_TIME,echoTimes);
1260  echoTime = true;
1261  }
1262  else
1263  {
1264  acqp_InputStream.close();
1265  OStringStream message;
1266  message << "Could not retrieve ##$ACQ_echo_times" << std::endl
1267  << "The file is "
1268  << fileacqp;
1269  ExceptionObject exception(__FILE__, __LINE__,
1270  message.str(),
1271  ITK_LOCATION);
1272  throw exception;
1273  }
1274  }
1275 
1276  // Get repetition times.
1277  index = acqpFileString.find(ACQ_repetition_time);
1278  if( index != std::string::npos )
1279  {
1280  std::string tempString = ACQ_repetition_time;
1281  std::istringstream reptitionTimeString(acqpFileString.substr(
1282  index+tempString.length()));
1283  if (!reptitionTimeString)
1284  {
1285  acqp_InputStream.close();
1286  OStringStream message;
1287  message << "Could not create std::istringstream for "
1288  << "##$ACQ_repetition_time" << std::endl
1289  << "The file is "
1290  << fileacqp;
1291  ExceptionObject exception(__FILE__, __LINE__,
1292  message.str(),
1293  ITK_LOCATION);
1294  throw exception;
1295  }
1296  reptitionTimeString >> numRepetitions;
1297  //std::cout << "numRepetitions = " << numRepetitions << std::endl;
1298  if( numRepetitions > 0 )
1299  {
1300  std::vector<double> Repetition_time(numRepetitions);
1301  ACQRepetitionTimeContainerType::Pointer repetitionTimes =
1303  repetitionTimes->resize(numRepetitions);
1304  for(unsigned int i=0; i<(unsigned int)numRepetitions; i++)
1305  {
1306  acqp_InputStream >> Repetition_time[i];
1307  repetitionTimes->SetElement(i,Repetition_time[i]);
1308  }
1309  EncapsulateMetaData<ACQRepetitionTimeContainerType::Pointer>(
1310  thisDic,ACQ_REPETITION_TIME,repetitionTimes);
1311  repetitionTime = true;
1312  }
1313  else
1314  {
1315  acqp_InputStream.close();
1316  OStringStream message;
1317  message << "Could not retrieve ##$ACQ_repetition_time" << std::endl
1318  << "The file is "
1319  << fileacqp;
1320  ExceptionObject exception(__FILE__, __LINE__,
1321  message.str(),
1322  ITK_LOCATION);
1323  throw exception;
1324  }
1325  }
1326 
1327  // Get inversion times.
1328  index = acqpFileString.find(ACQ_inversion_time);
1329  if( index != std::string::npos )
1330  {
1331  std::string tempString = ACQ_inversion_time;
1332  std::istringstream inversionTimeString(acqpFileString.substr(
1333  index+tempString.length()));
1334  if (!inversionTimeString)
1335  {
1336  acqp_InputStream.close();
1337  OStringStream message;
1338  message << "Could not create std::istringstream for "
1339  << "##$ACQ_inversion_time" << std::endl
1340  << "The file is "
1341  << fileacqp;
1342  ExceptionObject exception(__FILE__, __LINE__,
1343  message.str(),
1344  ITK_LOCATION);
1345  throw exception;
1346  }
1347  inversionTimeString >> numInversionTimes;
1348  //std::cout << "numInversionTimes = " << numInversionTimes << std::endl;
1349  if( numInversionTimes > 0 )
1350  {
1351  std::vector<double> Inversion_time(numInversionTimes);
1352  ACQInversionTimeContainerType::Pointer inversionTimes =
1354  inversionTimes->resize(numInversionTimes);
1355  for(unsigned int i=0; i<(unsigned int)numInversionTimes; i++)
1356  {
1357  acqp_InputStream >> Inversion_time[i];
1358  inversionTimes->SetElement(i,Inversion_time[i]);
1359  }
1360  EncapsulateMetaData<ACQInversionTimeContainerType::Pointer>(
1361  thisDic,ACQ_INVERSION_TIME,inversionTimes);
1362  inversionTime = true;
1363  }
1364  else
1365  {
1366  acqp_InputStream.close();
1367  OStringStream message;
1368  message << "Could not retrieve ##$ACQ_inversion_time" << std::endl
1369  << "The file is "
1370  << fileacqp;
1371  ExceptionObject exception(__FILE__, __LINE__,
1372  message.str(),
1373  ITK_LOCATION);
1374  throw exception;
1375  }
1376  }
1377 
1378  // Get direction cosines.
1379  index = acqpFileString.find(ACQ_grad_matrix);
1380  if( index != std::string::npos )
1381  {
1382  std::string tempString = ACQ_grad_matrix;
1383  int numMatrix=0, dim1=0, dim2=0;
1384  tempString = acqpFileString.substr(index+tempString.length());
1385  // MS VC++ cannot handle commas, so replace with spaces.
1386  for(std::string::iterator iter = tempString.begin();
1387  iter != tempString.end(); iter++ )
1388  {
1389  if(*iter == ',')
1390  {
1391  *iter = ' ';
1392  }
1393  }
1394  std::istringstream gradMatrixString(tempString);
1395  if (!gradMatrixString)
1396  {
1397  acqp_InputStream.close();
1398  OStringStream message;
1399  message << "Could not create std::istringstream for "
1400  << "##$ACQ_grad_matrix" << std::endl
1401  << "The file is "
1402  << fileacqp;
1403  ExceptionObject exception(__FILE__, __LINE__,
1404  message.str(),
1405  ITK_LOCATION);
1406  throw exception;
1407  }
1408  gradMatrixString >> numMatrix >> dim1 >> dim2;
1409  //std::cout << "numMatrix = " << numMatrix << std::endl;
1410  //std::cout << "dim1 = " << dim1 << std::endl;
1411  //std::cout << "dim2 = " << dim2 << std::endl;
1412  if( numMatrix && (dim1 == 3) && (dim2 == 3) )
1413  {
1414  // OK, I need ACQ_dim at this point in the code,
1415  // so throw an exception if I don't have it.
1416  if( acq_dim < 0 )
1417  {
1418  acqp_InputStream.close();
1419  OStringStream message;
1420  message << "Invalid acqp file: Couldn't locate "
1421  << "'##$ACQ_dim=' tag" << std::endl
1422  << "The file is "
1423  << fileacqp;
1424  ExceptionObject exception(__FILE__, __LINE__,
1425  message.str(),
1426  ITK_LOCATION);
1427  throw exception;
1428  }
1429  int i=0;
1430  for(i=0; i<3; i++)
1431  {
1432  acqp_InputStream >> dirx[i];
1433  if( dirx[i] == -0 )
1434  {
1435  dirx[i] = 0;
1436  }
1437  //std::cout << "dirx[" << i << "]= " << dirx[i] << std::endl;
1438  }
1439  for(i=0; i<3; i++)
1440  {
1441  acqp_InputStream >> diry[i];
1442  if( diry[i] == -0 )
1443  {
1444  diry[i] = 0;
1445  }
1446  //std::cout << "diry[" << i << "]= " << diry[i] << std::endl;
1447  }
1448  for(i=0; i<3; i++)
1449  {
1450  acqp_InputStream >> dirz[i];
1451  if( dirz[i] == -0 )
1452  {
1453  dirz[i] = 0;
1454  }
1455  //std::cout << "dirz[" << i << "]= " << dirz[i] << std::endl;
1456  }
1457  // Ok, now that the directions are read in transpose if necessary.
1458  if( (acq_dim == 2) &&
1459  (numRecoTranspose == numMatrix) &&
1460  recoTransposition[0] )
1461  {
1462  // Transpose read/phase.
1463  transpose = 1;
1464  std::vector<double> temp(3,0);
1465  for(i=0; i<3; i++)
1466  {
1467  temp[i] = dirx[i];
1468  dirx[i] = diry[i];
1469  diry[i] = temp[i];
1470  }
1471  }
1472  else if( recoTransposition[0] == 1 )
1473  {
1474  // Transpose read/phase.
1475  transpose = 1;
1476  std::vector<double> temp(3,0);
1477  for(i=0; i<3; i++)
1478  {
1479  temp[i] = dirx[i];
1480  dirx[i] = diry[i];
1481  diry[i] = temp[i];
1482  }
1483  }
1484  else if( recoTransposition[0] == 2 )
1485  {
1486  // Transpose phase/slice.
1487  transpose = 2;
1488  std::vector<double> temp(3,0);
1489  for(i=0; i<3; i++)
1490  {
1491  temp[i] = diry[i];
1492  diry[i] = dirz[i];
1493  dirz[i] = temp[i];
1494  }
1495  }
1496  else if( recoTransposition[0] == 3 )
1497  {
1498  // Transpose read/slice.
1499  transpose = 3;
1500  std::vector<double> temp(3,0);
1501  for(i=0; i<3; i++)
1502  {
1503  temp[i] = dirx[i];
1504  dirx[i] = dirz[i];
1505  dirz[i] = temp[i];
1506  }
1507  }
1508  // Check to see if all of the slices are in the same orientation.
1509  // If not then only use the first slice (may change this behavior later).
1510  if( (numMatrix-1) > 0 )
1511  {
1512  std::vector<double> gradMatrixX(3,0);
1513  std::vector<double> gradMatrixY(3,0);
1514  std::vector<double> gradMatrixZ(3,0);
1515  for(int j=0; j<(numMatrix-1); j++)
1516  {
1517  int l=0;
1518  for(l=0; l<3; l++)
1519  {
1520  acqp_InputStream >> gradMatrixX[l];
1521  if( gradMatrixX[l] == -0 )
1522  {
1523  gradMatrixX[l] = 0;
1524  }
1525  }
1526  for(l=0; l<3; l++)
1527  {
1528  acqp_InputStream >> gradMatrixY[l];
1529  if( gradMatrixY[l] == -0 )
1530  {
1531  gradMatrixY[l] = 0;
1532  }
1533  }
1534  for(l=0; l<3; l++)
1535  {
1536  acqp_InputStream >> gradMatrixZ[l];
1537  if( gradMatrixZ[l] == -0 )
1538  {
1539  gradMatrixZ[l] = 0;
1540  }
1541  }
1542  // Transpose if necessary.
1543  if( (acq_dim == 2) && recoTransposition[j+1] )
1544  {
1545  // Transpose read/phase.
1546  std::vector<double> temp(3,0);
1547  for(i=0; i<3; i++)
1548  {
1549  temp[i] = gradMatrixX[i];
1550  gradMatrixX[i] = gradMatrixY[i];
1551  gradMatrixY[i] = temp[i];
1552  }
1553  }
1554  else if( recoTransposition[j+1] == 1 )
1555  {
1556  // Transpose read/phase.
1557  std::vector<double> temp(3,0);
1558  for(i=0; i<3; i++)
1559  {
1560  temp[i] = gradMatrixX[i];
1561  gradMatrixX[i] = gradMatrixY[i];
1562  gradMatrixY[i] = temp[i];
1563  }
1564  }
1565  else if( recoTransposition[j+1] == 2 )
1566  {
1567  // Transpose phase/slice.
1568  std::vector<double> temp(3,0);
1569  for(i=0; i<3; i++)
1570  {
1571  temp[i] = gradMatrixY[i];
1572  gradMatrixY[i] = gradMatrixZ[i];
1573  gradMatrixZ[i] = temp[i];
1574  }
1575  }
1576  else if( recoTransposition[j+1] == 3 )
1577  {
1578  // Transpose read/slice.
1579  std::vector<double> temp(3,0);
1580  for(i=0; i<3; i++)
1581  {
1582  temp[i] = gradMatrixX[i];
1583  gradMatrixX[i] = gradMatrixZ[i];
1584  gradMatrixZ[i] = temp[i];
1585  }
1586  }
1587  // Compare with original
1588  if( !std::equal(dirx.begin(),dirx.end(),gradMatrixX.begin()) ||
1589  !std::equal(diry.begin(),diry.end(),gradMatrixY.begin()) ||
1590  !std::equal(dirz.begin(),dirz.end(),gradMatrixZ.begin()) )
1591  {
1592  slicesNotInSameOrientation = true;
1593  break;
1594  }
1595  }
1596  }
1597  }
1598  else
1599  {
1600  acqp_InputStream.close();
1601  OStringStream message;
1602  message << "Could not retrieve ##$ACQ_grad_matrix" << std::endl
1603  << "The file is "
1604  << fileacqp;
1605  ExceptionObject exception(__FILE__, __LINE__,
1606  message.str(),
1607  ITK_LOCATION);
1608  throw exception;
1609  }
1610  }
1611  }
1612  acqp_InputStream.close();
1613 
1614  if( !echoTime )
1615  {
1616  OStringStream message;
1617  message << "Invalid acqp file: Couldn't locate "
1618  << "'##$ACQ_echo_time=( ' tag" << std::endl
1619  << "The file is "
1620  << fileacqp;
1621  ExceptionObject exception(__FILE__, __LINE__,
1622  message.str(),
1623  ITK_LOCATION);
1624  throw exception;
1625  }
1626 
1627  if( !sliceThickness )
1628  {
1629  OStringStream message;
1630  message << "Invalid acqp file: Couldn't locate "
1631  << "'##$ACQ_slice_thick=' tag" << std::endl
1632  << "The file is "
1633  << fileacqp;
1634  ExceptionObject exception(__FILE__, __LINE__,
1635  message.str(),
1636  ITK_LOCATION);
1637  throw exception;
1638  }
1639 
1640  if( !sliceSeperation )
1641  {
1642  OStringStream message;
1643  message << "Invalid acqp file: Couldn't locate "
1644  << "'##$ACQ_slice_sepn=(' tag" << std::endl
1645  << "The file is "
1646  << fileacqp;
1647  ExceptionObject exception(__FILE__, __LINE__,
1648  message.str(),
1649  ITK_LOCATION);
1650  throw exception;
1651  }
1652 
1653  if( !nr )
1654  {
1655  OStringStream message;
1656  message << "Invalid acqp file: Couldn't locate "
1657  << "'##$NR=' tag" << std::endl
1658  << "The file is "
1659  << fileacqp;
1660  ExceptionObject exception(__FILE__, __LINE__,
1661  message.str(),
1662  ITK_LOCATION);
1663  throw exception;
1664  }
1665 
1666  if( !ni )
1667  {
1668  OStringStream message;
1669  message << "Invalid acqp file: Couldn't locate "
1670  << "'##$NI=' tag" << std::endl
1671  << "The file is "
1672  << fileacqp;
1673  ExceptionObject exception(__FILE__, __LINE__,
1674  message.str(),
1675  ITK_LOCATION);
1676  throw exception;
1677  }
1678 
1679  if( !echoTime )
1680  {
1681  OStringStream message;
1682  message << "Invalid acqp file: Couldn't locate "
1683  << "'##$ACQ_echo_time=( ' tag" << std::endl
1684  << "The file is "
1685  << fileacqp;
1686  ExceptionObject exception(__FILE__, __LINE__,
1687  message.str(),
1688  ITK_LOCATION);
1689  throw exception;
1690  }
1691 
1692  if( !repetitionTime )
1693  {
1694  OStringStream message;
1695  message << "Invalid acqp file: Couldn't locate "
1696  << "'##$ACQ_repetition_time=( ' tag" << std::endl
1697  << "The file is "
1698  << fileacqp;
1699  ExceptionObject exception(__FILE__, __LINE__,
1700  message.str(),
1701  ITK_LOCATION);
1702  throw exception;
1703  }
1704 
1705  if( !inversionTime )
1706  {
1707  OStringStream message;
1708  message << "Invalid acqp file: Couldn't locate "
1709  << "'##$ACQ_inversion_time=( ' tag" << std::endl
1710  << "The file is "
1711  << fileacqp;
1712  ExceptionObject exception(__FILE__, __LINE__,
1713  message.str(),
1714  ITK_LOCATION);
1715  throw exception;
1716  }
1717 
1718  // This is definitely a hack that will not always be correct, but should work
1719  // for Bruker images that have been acquired as homogeneous volumes.
1720  if( imageFOV[2] == 0 )
1721  {
1722  imageFOV[2] = imageDim[2]*sliceThick;
1723  imageFOV[2] /= 10.0f; //Convert from mm to cm.
1724  }
1725 
1726  if( slicesNotInSameOrientation )
1727  {
1728  imageDim[2] = 1;
1729  }
1730 
1731  EncapsulateMetaData<unsigned int>(
1733 
1734  //
1735  // Transpose the dims and FOV if required.
1736  switch( transpose )
1737  {
1738  case 1:
1739  {
1740  double tempFOV;
1741  unsigned int tempDim;
1742  tempFOV = imageFOV[0];
1743  imageFOV[0] = imageFOV[1];
1744  imageFOV[1] = tempFOV;
1745  tempDim = imageDim[0];
1746  imageDim[0] = imageDim[1];
1747  imageDim[1] = tempDim;
1748  }
1749  break;
1750  case 2:
1751  {
1752  double tempFOV;
1753  unsigned int tempDim;
1754  tempFOV = imageFOV[1];
1755  imageFOV[1] = imageFOV[2];
1756  imageFOV[2] = tempFOV;
1757  tempDim = imageDim[1];
1758  imageDim[1] = imageDim[2];
1759  imageDim[2] = tempDim;
1760  }
1761  break;
1762  case 3:
1763  {
1764  double tempFOV;
1765  unsigned int tempDim;
1766  tempFOV = imageFOV[0];
1767  imageFOV[0] = imageFOV[2];
1768  imageFOV[2] = tempFOV;
1769  tempDim = imageDim[0];
1770  imageDim[0] = imageDim[2];
1771  imageDim[2] = tempDim;
1772  }
1773  break;
1774  }
1775 
1776  //
1777  // set up the dimension stuff
1778  for(dim = 0; dim < this->GetNumberOfDimensions(); dim++)
1779  {
1780  this->SetDimensions(dim,imageDim[dim]);
1781  imageFOV[dim] *= 10.0f; //Convert from cm to mm.
1782  this->SetSpacing(dim, imageFOV[dim]/(double)imageDim[dim]);
1783  this->SetOrigin(dim, -imageFOV[dim]/2.0f);
1784  }
1785  switch( this->GetNumberOfDimensions() )
1786  {
1787  case 1:
1788  this->SetDirection(0,dirx);
1789  break;
1790  case 2:
1791  this->SetDirection(0,dirx);
1792  this->SetDirection(1,diry);
1793  break;
1794  case 3:
1795  default:
1796  this->SetDirection(0,dirx);
1797  this->SetDirection(1,diry);
1798  this->SetDirection(2,dirz);
1799  }
1800 
1801  return;
1802 }
1803 
1804 
1805 } // end namespace itk

Generated at Sat Feb 2 2013 23:30:30 for Orfeo Toolbox with doxygen 1.8.1.1