PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
itkUlteriusImageIO.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2  Program: Plus
3  Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4  See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #ifdef _MSC_VER
8 #pragma warning ( disable : 4786 )
9 #endif
10 
11 #include "PlusConfigure.h"
12 
13 #include <string>
14 #include <stdlib.h>
15 #include "itkUlteriusImageIO.h"
16 #include "itkMacro.h"
17 //#include "itkSpatialOrientation.h"
18 //#include "itkSpatialOrientationAdapter.h"
19 #include "itkIOCommon.h"
20 #include <itksys/SystemTools.hxx>
21 
22 #include "ulterius_def.h"
23 
24 namespace itk
25 {
26 
28  {
29  m_FileType = Binary;
30  m_FileHeaderPtr = new uFileHeader;
31 
32  // :TODO: check all the supported file formats (extensions) on the Ultrasonix GUI and add them here
33 
34  /* From Ulterius1.2.1 SDK:
35  udtScreen->.avi
36  udtBPre->.bpr
37  udtBPost->.b8
38  udtBPost32->.b32
39  udtRF->.rf
40  udtMPre->.mpr
41  udtMPost->.m
42  udtPWRF->.drf
43  udtPWSpectrum->.pw
44  udtColorRF->.crf
45  udtColorPost->.col
46  udtColorVelocityVariance->.cvv
47  udtElastoCombined->.el
48  udtElastoOverlay->.elo
49  udtElastoPre->.epr
50  udtECG->.ecg
51  */
52 
53  this->AddSupportedReadExtension(".b8");
54  this->AddSupportedReadExtension(".b32");
55  }
56 
58  {
59  delete m_FileHeaderPtr;
60  m_FileHeaderPtr = NULL;
61  }
62 
63  void UlteriusImageIO::PrintSelf(std::ostream& os, Indent indent) const
64  {
65  Superclass::PrintSelf(os, indent);
66  os << indent << "Type: " << m_FileHeaderPtr->type << "\n";
67  os << indent << "Number of frames: " << m_FileHeaderPtr->frames << "\n";
68  }
69 
70  // This method will only test if the header looks like a
71  // UlteriusImage.
72  bool UlteriusImageIO::CanReadFile( const char* filename )
73  {
74  // First check the extension
75  std::string fname = filename;
76  if( fname == "" )
77  {
78  itkDebugMacro(<<"No filename specified.");
79  return false;
80  }
81 
82  if (!ReadHeader(fname.c_str()))
83  {
84  return false;
85  }
86 
87  switch (m_FileHeaderPtr->type)
88  {
89  case udtBPost:
90  case udtBPost32:
91  //supported data type
92  break;
93  default:
94  // unknown data type
95  return false;
96  }
97 
98  // :TODO: add some more checks (e.g., size is >0, sample size is not too big, not too small, etc.
99 
100  return true;
101  }
102 
103 
105  {
106  if (!ReadHeader(m_FileName.c_str()))
107  {
108  itkExceptionMacro("File cannot be read: "
109  << this->GetFileName() << " for reading."
110  << std::endl
111  << "Reason: cannot read header, "
112  << itksys::SystemTools::GetLastSystemError());
113  }
114 
115 
116  switch (m_FileHeaderPtr->type)
117  {
118  case udtBPost:
119  this->SetNumberOfComponents(1);
120  this->SetPixelType( SCALAR );
121  this->SetComponentType( CHAR );
122  break;
123  case udtBPost32:
124  this->SetNumberOfComponents(3);
125  this->SetPixelType( SCALAR );
126  this->SetComponentType( CHAR );
127  break;
128  default:
129  // unknown data type
130  itkExceptionMacro("File cannot be read: "
131  << this->GetFileName() << " for reading."
132  << std::endl
133  << "Reason: unknown data type " << m_FileHeaderPtr->type);
134  }
135 
136  if (m_FileHeaderPtr->frames<=0)
137  {
138  itkExceptionMacro("File cannot be read: "
139  << this->GetFileName() << " for reading."
140  << std::endl
141  << "Reason: invalid number of frames: " << m_FileHeaderPtr->frames);
142  }
143 
144  this->SetNumberOfDimensions(3);
145 
146  this->SetDimensions(0, m_FileHeaderPtr->w);
147  this->SetDimensions(1, m_FileHeaderPtr->h);
148  this->SetDimensions(2, m_FileHeaderPtr->frames);
149 
150  // :TODO: get spacing info, if possible
151  // it might be computed from line density (if pitch and native # elements is known)
152  this->SetSpacing(0, 1.0);
153  this->SetSpacing(1, 1.0);
154  this->SetSpacing(2, 1.0);
155 
156  this->SetOrigin(0, 0);
157  this->SetOrigin(1, 0);
158  this->SetOrigin(2, 0);
159 
160  vnl_vector< double > directionAxis( this->GetNumberOfDimensions() );
161 
162  directionAxis[0]=1.0;
163  directionAxis[1]=0.0;
164  directionAxis[2]=0.0;
165  this->SetDirection( 0, directionAxis );
166 
167  directionAxis[0]=0.0;
168  directionAxis[1]=1.0;
169  directionAxis[2]=0.0;
170  this->SetDirection( 1, directionAxis );
171 
172  directionAxis[0]=0.0;
173  directionAxis[1]=0.0;
174  directionAxis[2]=1.0;
175  this->SetDirection( 2, directionAxis );
176 
177  }
178 
179 
180  void UlteriusImageIO::Read(void* buffer)
181  {
182  std::ifstream file;
183  file.open( m_FileName.c_str(), std::ios::in | std::ios::binary );
184 
185  // Set the file read pointer to the start of the pixel data
186  file.seekg(sizeof(uFileHeader), std::ios::beg );
187 
188  file.read(static_cast<char*>(buffer), static_cast<std::streamsize>(this->GetImageSizeInBytes()));
189  // :TODO: add error handling here
190  }
191 
192  bool UlteriusImageIO::CanWriteFile( const char * name )
193  {
194  return false;
195  }
196 
197 
198  void
201  {
202  }
203 
204  void
206  ::Write( const void* buffer)
207  {
208  }
209 
210  bool UlteriusImageIO::ReadHeader(const char* filename)
211  {
212  std::ifstream local_InputStream;
213  local_InputStream.open( filename, std::ios::in | std::ios::binary );
214  if( local_InputStream.fail() )
215  {
216  return false;
217  }
218  if( ! this->ReadBufferAsBinary( local_InputStream,
219  (void *)this->m_FileHeaderPtr,
220  sizeof(uFileHeader) ) )
221  {
222  local_InputStream.close();
223  return false;
224  }
225  local_InputStream.close();
226 
227  return true;
228  }
229 
230 } // end namespace itk
231 
232 
void PrintSelf(std::ostream &os, Indent indent) const
bool ReadHeader(const char *filename)
virtual void WriteImageInformation()
char CHAR
Definition: ATC3DGm.h:443
virtual bool CanReadFile(const char *)
virtual void Read(void *buffer)
virtual void Write(const void *buffer)
virtual bool CanWriteFile(const char *)
uFileHeader * m_FileHeaderPtr
virtual void ReadImageInformation()