]> Creatis software - creaImageIO.git/blob - src2/creaImageIOUltrasonixImageReader.cpp
Clean-Up with Juan Sebastien
[creaImageIO.git] / src2 / creaImageIOUltrasonixImageReader.cpp
1
2 #include <creaImageIOSystem.h>
3 #include <creaImageIOUltrasonixImageReader.h>
4 #include <creaVtk.h>
5 #include "boost/filesystem/path.hpp"
6
7 namespace creaImageIO
8 {
9 #define HEADER_SIZE     19
10 #define TYPE_RF         16
11 #define TYPE_B8         4
12 #define TYPE_B32                8
13
14
15   //=====================================================================
16   UltrasonixImageReader::UltrasonixImageReader()
17   {
18     SetName("Ultrasonix");
19   }
20   //=====================================================================
21   
22   //=====================================================================
23   UltrasonixImageReader::~UltrasonixImageReader()
24   {
25   }
26   //=====================================================================
27
28   //=====================================================================
29   struct Ultrasonix_header
30   {
31     // frames, width, height, ultrasounds frequency, sampling rate
32     int type,frame,width,height,frequency,samplingRate;
33   };
34   //=====================================================================
35
36
37   //=====================================================================
38   bool ReadHeader( FILE *Ultrasonix_file, Ultrasonix_header& h )
39   {
40     int *header=(int*)malloc(sizeof(int)*HEADER_SIZE);
41     fread(header,sizeof(int),HEADER_SIZE,Ultrasonix_file);
42          h.type = header[0];
43     h.frame  = header[1];
44     h.height = header[2];
45     h.width = header[3];
46          h.frequency = header[14];
47          h.samplingRate = header[15];
48          free(header);  
49     return true;
50   }
51   //=====================================================================
52
53   //=====================================================================
54   bool UltrasonixImageReader::CanRead(const std::string& filename)
55   { 
56     bool ok = false;
57     FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
58          long size;
59     if (Ultrasonix_file) 
60     {
61                 Ultrasonix_header h;
62                 ReadHeader(Ultrasonix_file,h);
63
64                 fseek(Ultrasonix_file,0,SEEK_END);              // go to end of file
65                 if (h.type == TYPE_RF)
66                         size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame) * sizeof(int)) / sizeof(short);
67                 else if (h.type == TYPE_B8)
68                         size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(char);
69                 else if (h.type == TYPE_B32)
70                         size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(int);
71
72                 // check if the data size corresponds to the dimensions of the images
73                 if (size == h.width * h.height * h.frame)
74                         ok = true;
75                 
76                 fclose(Ultrasonix_file);
77     }
78     return ok;
79   }
80   //=====================================================================
81   
82   //=====================================================================
83   vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
84   {
85     FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
86     if (!Ultrasonix_file) return false;
87     Ultrasonix_header h;
88     if (!ReadHeader(Ultrasonix_file,h)) 
89     {
90                 fclose(Ultrasonix_file);  
91                 return 0;
92     }
93     
94     long im_size = h.height * h.width * h.frame;
95     long frame_size = h.height * h.width;  
96          short *dataRF, *ptrRF;
97          char *dataB8, *ptrB8;
98          int *dataB32, *ptrB32;
99          vtkImageData* im;
100          int temp;
101
102          switch (h.type)
103          {
104                 case TYPE_RF:
105                         dataRF = (short*)malloc(sizeof(short)*im_size);
106                         ptrRF = dataRF;
107
108                         for (int k=0; k<h.frame; k++)
109                         {
110                                 int frame_number;
111                                 fread(&frame_number,sizeof(int),1,Ultrasonix_file);
112                                 fread(ptrRF,sizeof(short),frame_size,Ultrasonix_file);
113                                 ptrRF += frame_size;
114                         }
115                         fclose(Ultrasonix_file);  
116
117                         im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
118                 break;
119
120                 case TYPE_B8:
121                         dataB8 = (char*)malloc(sizeof(char)*im_size);
122                         ptrB8 = dataB8;
123                         for (int k=0; k<h.frame; k++)
124                         {
125                                 fread(ptrB8,sizeof(char),frame_size,Ultrasonix_file);
126                                 ptrB8 += frame_size;
127                         }
128                         // in mode b frames width and height are inverted
129                         temp = h.width;
130                         h.width = h.height;
131                         h.height = temp;
132                         
133                         fclose(Ultrasonix_file);  
134
135                         im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
136                 break;
137
138                 case TYPE_B32:
139                         dataB32 = (int*)malloc(sizeof(int)*im_size);
140                         ptrB32 = dataB32;
141                         for (int k=0; k<h.frame; k++)
142                         {
143                                 fread(ptrB32,sizeof(int),frame_size,Ultrasonix_file);
144                                 ptrB32 += frame_size;
145                         }
146                         // in B mode frames width and height are inverted
147                         temp = h.width;
148                         h.width = h.height;
149                         h.height = temp;
150
151                         fclose(Ultrasonix_file);  
152
153                         im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
154                 break;
155          }
156
157     return im;
158 }
159   //=====================================================================
160   
161  
162   //=====================================================================
163   void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
164   {
165     v.push_back("Ultrasonix");
166   }
167   //=====================================================================
168  
169
170
171   //=====================================================================
172   void UltrasonixImageReader::ReadAttributes(const std::string& filename, 
173                                       std::map<std::string,std::string>& attr)
174   {
175     GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
176
177     FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
178     if (!Ultrasonix_file) return;
179     Ultrasonix_header h;
180     if (!ReadHeader(Ultrasonix_file,h)) 
181       {
182         fclose(Ultrasonix_file);  
183         return;
184       }
185     fclose(Ultrasonix_file);  
186   
187     // Columns
188     char cols[128];
189     sprintf(cols,"%i",h.width);
190     // Rows
191     char rows[128];
192     sprintf(rows,"%i",h.height);
193     // Planes 
194     char planes[128];
195     sprintf(planes,"%i",h.frame);
196          // Sampling frequency
197          char samplingFrequency[128];
198          sprintf(samplingFrequency,"%i",h.samplingRate);
199          // Transducer frequency
200          char transducerFrequency[128];
201          sprintf(transducerFrequency,"%i",h.frequency);
202     
203     
204     // 
205     std::map<std::string,std::string>::iterator i;
206     if ( (i = attr.find("FullFileName")) != attr.end())
207     {
208                 i->second = filename;
209     }
210     if ( (i = attr.find("D0004_1500")) != attr.end())
211     {
212                 boost::filesystem::path full_path(filename);
213                 std::string f = full_path.leaf();
214                 i->second = f;
215     }
216     if ( (i = attr.find("D0028_0010")) != attr.end())
217     {
218                 i->second = rows;
219     }
220     if ( (i = attr.find("D0028_0011")) != attr.end())
221     {
222                 i->second = cols;
223     }
224     if ( (i = attr.find("D0028_0012")) != attr.end())
225     {
226                 i->second = planes;
227     }
228          if ( (i = attr.find("D003a_001a")) != attr.end())
229          {
230                  i->second = samplingFrequency;
231          }
232          if ( (i = attr.find("D0018_6030")) != attr.end())
233          {
234                  i->second = transducerFrequency;
235          }
236
237     
238    GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
239   }
240   //=====================================================================
241
242 } // namespace creaImageIO