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