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