]> Creatis software - creaImageIO.git/blob - src/creaImageIOUltrasonixImageReader.cpp
thread bug correction
[creaImageIO.git] / src / 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[1];
45     h.frame        = header[2];
46     h.height       = header[3];
47     h.width        = header[4];
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 = -1;
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 == 1)//TYPE_B8)
75                 size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame+4) *  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   void UltrasonixImageReader::getAttributes(const std::string filename,
89                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
90   {
91           //TO DO
92   }
93   
94   //=====================================================================
95   vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
96   {
97     FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
98     if (!Ultrasonix_file) 
99     {
100         std::cout << "cannot open file [" << filename << "]" << std::endl;
101         return 0;
102     }
103     Ultrasonix_header h;
104     if (!ReadHeader(Ultrasonix_file,h)) 
105     {
106         std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
107         fclose(Ultrasonix_file);  
108         return 0;
109     }
110
111     long frame_size = h.height   * h.width;      
112     long im_size    = frame_size * h.frame;
113
114     short *dataRF,  *ptrRF;
115     char  *dataB8,  *ptrB8;
116     int   *dataB32, *ptrB32;
117     vtkImageData* im;
118     int temp;
119
120     switch (h.type)
121     {
122         case TYPE_RF:
123                 dataRF = (short*)malloc(sizeof(short)*im_size);
124                 ptrRF  = dataRF;
125
126                 for (int k=0; k<h.frame; k++)
127                 {
128                         int frame_number;
129                         fread(&frame_number, sizeof(int), 1, Ultrasonix_file);
130                         fread(ptrRF,sizeof(short), frame_size, Ultrasonix_file);
131                         ptrRF += frame_size;
132                 }
133                 fclose(Ultrasonix_file);  
134
135                 im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
136         break;
137
138         case TYPE_B8:
139                 dataB8 = (char*)malloc(sizeof(char)*im_size);
140                 ptrB8  = dataB8;
141                 for (int k=0; k<h.frame; k++)
142                 {
143                         fread(ptrB8,sizeof(char), frame_size, Ultrasonix_file);
144                         ptrB8 += frame_size;
145                 }
146                 // in mode b 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( dataB8, h.width, h.height, h.frame);
154         break;
155
156         case TYPE_B32:
157                 dataB32 = (int*)malloc(sizeof(int)*im_size);
158                 ptrB32  = dataB32;
159                 for (int k=0; k<h.frame; k++)
160                 {
161                         fread(ptrB32, sizeof(int), frame_size, Ultrasonix_file);
162                         ptrB32 += frame_size;
163                 }
164                 // in B mode frames width and height are inverted
165                 temp     = h.width;
166                 h.width  = h.height;
167                 h.height = temp;
168
169                 fclose(Ultrasonix_file);  
170
171                 im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
172         break;
173     }
174
175     return im;
176 }
177   //=====================================================================
178   
179
180   //=====================================================================
181   void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
182   {
183     v.push_back("Ultrasonix");
184   }
185   //=====================================================================
186  
187
188
189   //=====================================================================
190  void UltrasonixImageReader::ReadAttributes(const std::string& filename, 
191                                       std::map<std::string,std::string>& attr)
192  {
193     GimmickMessage(2,"Reading attributes from '" << filename << std::endl);
194
195     FILE *Ultrasonix_file = fopen(filename.c_str(), "rb");
196     if (!Ultrasonix_file)
197     {
198         std::cout << "cannot open RF file [" << filename << "]" << std::endl;
199         return;
200     }
201
202     Ultrasonix_header h;
203     if (!ReadHeader(Ultrasonix_file, h)) 
204     {
205         fclose(Ultrasonix_file);
206         std::cout << "cannot read Ultrasonix Attributes for RF file [" << filename << "]" << std::endl;  
207         return;
208     }
209
210     fclose(Ultrasonix_file);  
211   
212     // Columns
213     char cols[128];
214     sprintf(cols,"%i", h.width);
215     // Rows
216     char rows[128];
217     sprintf(rows,"%i", h.height);
218     // Planes 
219     char planes[128];
220     sprintf(planes,"%i", h.frame);
221     // Sampling frequency
222     char samplingFrequency[128];
223     sprintf(samplingFrequency,"%i", h.samplingRate);
224     // Transducer frequency
225     char transducerFrequency[128];
226     sprintf(transducerFrequency,"%i", h.frequency);
227    
228     // 
229     std::map<std::string,std::string>::iterator i;
230     if ( (i = attr.find("FullFileName")) != attr.end())
231     {
232            i->second = filename;
233     }
234     if ( (i = attr.find("D0004_1500")) != attr.end())
235     {
236            boost::filesystem::path full_path(filename);
237            std::string f = full_path.leaf();
238            i->second = f;
239     }
240     if ( (i = attr.find("D0028_0010")) != attr.end())
241     {
242            i->second = rows;
243     }
244     if ( (i = attr.find("D0028_0011")) != attr.end())
245     {
246            i->second = cols;
247     }
248     if ( (i = attr.find("D0028_0012")) != attr.end())
249     {
250            i->second = planes;
251     }
252     if ( (i = attr.find("D003a_001a")) != attr.end())
253     {
254        i->second = samplingFrequency;
255     }
256     if ( (i = attr.find("D0018_6030")) != attr.end())
257     {
258            i->second = transducerFrequency;
259     }
260     
261     GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
262     return;
263 }
264   //=====================================================================
265
266 } // namespace creaImageIO