]> Creatis software - creaImageIO.git/blob - src2/creaImageIOUltrasonixImageReader.cpp
Add support for Ultrasonix images (.rf, .b8, .b32).
[creaImageIO.git] / src2 / creaImageIOUltrasonixImageReader.cpp
1
2 #include <creaImageIOUltrasonixImageReader.h>
3 #include <creaVtk.h>
4 #include <creaImageIOSystem.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
86           std::cout<<"UltrasonixImageReader reading "<<std::endl;
87
88     FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
89     if (!Ultrasonix_file) return false;
90     Ultrasonix_header h;
91     if (!ReadHeader(Ultrasonix_file,h)) 
92     {
93                 fclose(Ultrasonix_file);  
94                 return 0;
95     }
96     
97     long im_size = h.height * h.width * h.frame;
98     long frame_size = h.height * h.width;  
99          short *dataRF, *ptrRF;
100          char *dataB8, *ptrB8;
101          int *dataB32, *ptrB32;
102          vtkImageData* im;
103          int temp;
104
105          switch (h.type)
106          {
107                 case TYPE_RF:
108                         dataRF = (short*)malloc(sizeof(short)*im_size);
109                         ptrRF = dataRF;
110
111                         for (int k=0; k<h.frame; k++)
112                         {
113                                 int frame_number;
114                                 fread(&frame_number,sizeof(int),1,Ultrasonix_file);
115                                 fread(ptrRF,sizeof(short),frame_size,Ultrasonix_file);
116                                 ptrRF += frame_size;
117                         }
118                         fclose(Ultrasonix_file);  
119
120                         im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
121                 break;
122
123                 case TYPE_B8:
124                         dataB8 = (char*)malloc(sizeof(char)*im_size);
125                         ptrB8 = dataB8;
126                         for (int k=0; k<h.frame; k++)
127                         {
128                                 fread(ptrB8,sizeof(char),frame_size,Ultrasonix_file);
129                                 ptrB8 += frame_size;
130                         }
131                         // in mode b frames width and height are inverted
132                         temp = h.width;
133                         h.width = h.height;
134                         h.height = temp;
135                         
136                         fclose(Ultrasonix_file);  
137
138                         im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
139                 break;
140
141                 case TYPE_B32:
142                         dataB32 = (int*)malloc(sizeof(int)*im_size);
143                         ptrB32 = dataB32;
144                         for (int k=0; k<h.frame; k++)
145                         {
146                                 fread(ptrB32,sizeof(int),frame_size,Ultrasonix_file);
147                                 ptrB32 += frame_size;
148                         }
149                         // in B mode frames width and height are inverted
150                         temp = h.width;
151                         h.width = h.height;
152                         h.height = temp;
153
154                         fclose(Ultrasonix_file);  
155
156                         im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
157                 break;
158          }
159
160     return im;
161 }
162   //=====================================================================
163   
164  
165   //=====================================================================
166   void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
167   {
168     v.push_back("Ultrasonix");
169   }
170   //=====================================================================
171  
172
173
174   //=====================================================================
175   void UltrasonixImageReader::ReadAttributes(const std::string& filename, 
176                                       std::map<std::string,std::string>& attr)
177   {
178     //      std::cout << "UltrasonixImageReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
179     GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
180  
181
182     FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
183     if (!Ultrasonix_file) return;
184     Ultrasonix_header h;
185     if (!ReadHeader(Ultrasonix_file,h)) 
186       {
187         fclose(Ultrasonix_file);  
188         return;
189       }
190     fclose(Ultrasonix_file);  
191   
192     // Columns
193     char cols[128];
194     sprintf(cols,"%i",h.width);
195     // Rows
196     char rows[128];
197     sprintf(rows,"%i",h.height);
198     // Planes 
199     char planes[128];
200     sprintf(planes,"%i",h.frame);
201     
202     
203     // 
204     std::map<std::string,std::string>::iterator i;
205     if ( (i = attr.find("FullFileName")) != attr.end())
206       {
207         //        boost::filesystem::path full_path(filename);
208         // std::string f = full_path.leaf();
209         i->second = filename;
210       }
211     if ( (i = attr.find("D0004_1500")) != attr.end())
212       {
213         boost::filesystem::path full_path(filename);
214         std::string f = full_path.leaf();
215         i->second = f;
216       }
217     if ( (i = attr.find("D0028_0010")) != attr.end())
218       {
219         i->second = rows;
220       }
221     if ( (i = attr.find("D0028_0011")) != attr.end())
222       {
223         i->second = cols;
224       }
225     
226     if ( (i = attr.find("D0028_0012")) != attr.end())
227       {
228         i->second = planes;
229       }
230     
231    GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
232   }
233   //=====================================================================
234
235 } // namespace creaImageIO