]> Creatis software - creaImageIO.git/blob - src/creaImageIOUltrasonixImageReader.cpp
Bug 1805:
[creaImageIO.git] / src / creaImageIOUltrasonixImageReader.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28
29 #include "creaImageIOSystem.h"
30 #include "creaImageIOUltrasonixImageReader.h"
31 #include <creaVtk.h>
32 #include <boost/filesystem/path.hpp>
33 namespace creaImageIO
34 {
35 #define HEADER_SIZE     19
36 #define TYPE_RF         16
37 #define TYPE_B8         4
38 #define TYPE_B32        8
39
40
41   //=====================================================================
42   UltrasonixImageReader::UltrasonixImageReader()
43   {
44     SetName("Ultrasonix");
45   }
46   //=====================================================================
47   
48   //=====================================================================
49   UltrasonixImageReader::~UltrasonixImageReader()
50   {
51   }
52   //=====================================================================
53
54   //=====================================================================
55   struct Ultrasonix_header
56   {
57     // frames, width, height, ultrasounds frequency, sampling rate
58     int type, frame, width, height, frequency, samplingRate;
59   };
60   //=====================================================================
61
62
63   //=====================================================================
64   bool ReadHeader( FILE *Ultrasonix_file, Ultrasonix_header& h )
65   {
66     //int *header=(int*)malloc(sizeof(int)*HEADER_SIZE);
67     int header[HEADER_SIZE];
68     fread(header, sizeof(int), HEADER_SIZE, Ultrasonix_file);
69     if (ferror(Ultrasonix_file))
70         return false;
71     h.type         = header[1];
72     h.frame        = header[2];
73     h.height       = header[3];
74     h.width        = header[4];
75     h.frequency    = header[14];
76     h.samplingRate = header[15];
77     //free(header);  
78     return true;
79   }
80   //=====================================================================
81
82   //=====================================================================
83   bool UltrasonixImageReader::CanRead(const std::string& filename)
84   { 
85     long size = -1;
86     bool ok = false;
87     FILE *Ultrasonix_file=fopen(filename.c_str(), "rb");
88     if (Ultrasonix_file) 
89     {
90         Ultrasonix_header h;
91         if (!ReadHeader(Ultrasonix_file, h) )
92         {
93                 fclose(Ultrasonix_file);
94                 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;                                
95                 return false;   
96         }
97
98         fseek(Ultrasonix_file,0,SEEK_END);              // go to end of file
99         if (h.type == TYPE_RF)
100                 size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame) * sizeof(int)) / sizeof(short);
101         else if (h.type == 1)//TYPE_B8)
102                 size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame+4) *  sizeof(int)) / sizeof(char);
103         else if (h.type == TYPE_B32)
104                 size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(int);
105
106         // check if the data size corresponds to the dimensions of the images
107         if (size == h.width * h.height * h.frame )
108                 ok = true;
109
110         fclose(Ultrasonix_file);
111     }
112     return ok;
113   }
114   //=====================================================================
115   void UltrasonixImageReader::getAttributes(const std::string filename,
116                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
117   {
118           //TO DO
119   }
120   
121   //=====================================================================
122   vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
123   {
124     FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
125     if (!Ultrasonix_file) 
126     {
127         std::cout << "cannot open file [" << filename << "]" << std::endl;
128         return 0;
129     }
130     Ultrasonix_header h;
131     if (!ReadHeader(Ultrasonix_file,h)) 
132     {
133         std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
134         fclose(Ultrasonix_file);  
135         return 0;
136     }
137
138     long frame_size = h.height   * h.width;      
139     long im_size    = frame_size * h.frame;
140
141     short *dataRF,  *ptrRF;
142     char  *dataB8,  *ptrB8;
143     int   *dataB32, *ptrB32;
144     vtkImageData* im;
145     int temp;
146
147     switch (h.type)
148     {
149         case TYPE_RF:
150                 dataRF = (short*)malloc(sizeof(short)*im_size);
151                 ptrRF  = dataRF;
152
153                 for (int k=0; k<h.frame; k++)
154                 {
155                         int frame_number;
156                         fread(&frame_number, sizeof(int), 1, Ultrasonix_file);
157                         fread(ptrRF,sizeof(short), frame_size, Ultrasonix_file);
158                         ptrRF += frame_size;
159                 }
160                 fclose(Ultrasonix_file);  
161
162                 im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
163         break;
164
165         case TYPE_B8:
166                 dataB8 = (char*)malloc(sizeof(char)*im_size);
167                 ptrB8  = dataB8;
168                 for (int k=0; k<h.frame; k++)
169                 {
170                         fread(ptrB8,sizeof(char), frame_size, Ultrasonix_file);
171                         ptrB8 += frame_size;
172                 }
173                 // in mode b frames width and height are inverted
174                 temp     = h.width;
175                 h.width  = h.height;
176                 h.height = temp;
177
178                 fclose(Ultrasonix_file);  
179
180         im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
181         break;
182
183         case TYPE_B32:
184                 dataB32 = (int*)malloc(sizeof(int)*im_size);
185                 ptrB32  = dataB32;
186                 for (int k=0; k<h.frame; k++)
187                 {
188                         fread(ptrB32, sizeof(int), frame_size, Ultrasonix_file);
189                         ptrB32 += frame_size;
190                 }
191                 // in B mode frames width and height are inverted
192                 temp     = h.width;
193                 h.width  = h.height;
194                 h.height = temp;
195
196                 fclose(Ultrasonix_file);  
197
198                 im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
199         break;
200     }
201
202     return im;
203 }
204   //=====================================================================
205   
206
207   //=====================================================================
208   void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
209   {
210     v.push_back("Ultrasonix");
211   }
212   //=====================================================================
213  
214
215
216   //=====================================================================
217  void UltrasonixImageReader::ReadAttributes(const std::string& filename, 
218                                       std::map<std::string,std::string>& attr)
219  {
220     GimmickMessage(2,"Reading attributes from '" << filename << std::endl);
221
222     FILE *Ultrasonix_file = fopen(filename.c_str(), "rb");
223     if (!Ultrasonix_file)
224     {
225         std::cout << "cannot open RF file [" << filename << "]" << std::endl;
226         return;
227     }
228
229     Ultrasonix_header h;
230     if (!ReadHeader(Ultrasonix_file, h)) 
231     {
232         fclose(Ultrasonix_file);
233         std::cout << "cannot read Ultrasonix Attributes for RF file [" << filename << "]" << std::endl;  
234         return;
235     }
236
237     fclose(Ultrasonix_file);  
238   
239     // Columns
240     char cols[128];
241     sprintf(cols,"%i", h.width);
242     // Rows
243     char rows[128];
244     sprintf(rows,"%i", h.height);
245     // Planes 
246     char planes[128];
247     sprintf(planes,"%i", h.frame);
248     // Sampling frequency
249     char samplingFrequency[128];
250     sprintf(samplingFrequency,"%i", h.samplingRate);
251     // Transducer frequency
252     char transducerFrequency[128];
253     sprintf(transducerFrequency,"%i", h.frequency);
254    
255     // 
256     std::map<std::string,std::string>::iterator i;
257     if ( (i = attr.find("FullFileName")) != attr.end())
258     {
259            i->second = filename;
260     }
261     if ( (i = attr.find("D0004_1500")) != attr.end())
262     {
263            boost::filesystem::path full_path(filename);
264            std::string f = full_path.leaf().string();
265            i->second = f;
266     }
267     if ( (i = attr.find("D0028_0010")) != attr.end())
268     {
269            i->second = rows;
270     }
271     if ( (i = attr.find("D0028_0011")) != attr.end())
272     {
273            i->second = cols;
274     }
275     if ( (i = attr.find("D0028_0012")) != attr.end())
276     {
277            i->second = planes;
278     }
279     if ( (i = attr.find("D003a_001a")) != attr.end())
280     {
281        i->second = samplingFrequency;
282     }
283     if ( (i = attr.find("D0018_6030")) != attr.end())
284     {
285            i->second = transducerFrequency;
286     }
287     
288     GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
289     return;
290 }
291   //=====================================================================
292
293 } // namespace creaImageIO