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