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