2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
29 #include "creaImageIOSystem.h"
30 #include "creaImageIOUltrasonixImageReader.h"
32 #include <boost/filesystem/path.hpp>
35 #define HEADER_SIZE 19
41 //=====================================================================
42 UltrasonixImageReader::UltrasonixImageReader()
44 SetName("Ultrasonix");
46 //=====================================================================
48 //=====================================================================
49 UltrasonixImageReader::~UltrasonixImageReader()
52 //=====================================================================
54 //=====================================================================
55 struct Ultrasonix_header
57 // frames, width, height, ultrasounds frequency, sampling rate
58 int type, frame, width, height, frequency, samplingRate;
60 //=====================================================================
63 //=====================================================================
64 bool ReadHeader( FILE *Ultrasonix_file, Ultrasonix_header& h )
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))
75 h.frequency = header[14];
76 h.samplingRate = header[15];
80 //=====================================================================
82 //=====================================================================
83 bool UltrasonixImageReader::CanRead(const std::string& filename)
87 FILE *Ultrasonix_file=fopen(filename.c_str(), "rb");
91 if (!ReadHeader(Ultrasonix_file, h) )
93 fclose(Ultrasonix_file);
94 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
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);
106 // check if the data size corresponds to the dimensions of the images
107 if (size == h.width * h.height * h.frame )
110 fclose(Ultrasonix_file);
114 //=====================================================================
115 void UltrasonixImageReader::getAttributes(const std::string filename,
116 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
121 //=====================================================================
122 vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
124 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
125 if (!Ultrasonix_file)
127 std::cout << "cannot open file [" << filename << "]" << std::endl;
131 if (!ReadHeader(Ultrasonix_file,h))
133 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
134 fclose(Ultrasonix_file);
138 long frame_size = h.height * h.width;
139 long im_size = frame_size * h.frame;
141 short *dataRF, *ptrRF;
142 char *dataB8, *ptrB8;
143 int *dataB32, *ptrB32;
150 dataRF = (short*)malloc(sizeof(short)*im_size);
153 for (int k=0; k<h.frame; k++)
156 fread(&frame_number, sizeof(int), 1, Ultrasonix_file);
157 fread(ptrRF,sizeof(short), frame_size, Ultrasonix_file);
160 fclose(Ultrasonix_file);
162 im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
166 dataB8 = (char*)malloc(sizeof(char)*im_size);
168 for (int k=0; k<h.frame; k++)
170 fread(ptrB8,sizeof(char), frame_size, Ultrasonix_file);
173 // in mode b frames width and height are inverted
178 fclose(Ultrasonix_file);
180 im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
184 dataB32 = (int*)malloc(sizeof(int)*im_size);
186 for (int k=0; k<h.frame; k++)
188 fread(ptrB32, sizeof(int), frame_size, Ultrasonix_file);
189 ptrB32 += frame_size;
191 // in B mode frames width and height are inverted
196 fclose(Ultrasonix_file);
198 im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
204 //=====================================================================
207 //=====================================================================
208 void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
210 v.push_back("Ultrasonix");
212 //=====================================================================
216 //=====================================================================
217 void UltrasonixImageReader::ReadAttributes(const std::string& filename,
218 std::map<std::string,std::string>& attr)
220 GimmickMessage(2,"Reading attributes from '" << filename << std::endl);
222 FILE *Ultrasonix_file = fopen(filename.c_str(), "rb");
223 if (!Ultrasonix_file)
225 std::cout << "cannot open RF file [" << filename << "]" << std::endl;
230 if (!ReadHeader(Ultrasonix_file, h))
232 fclose(Ultrasonix_file);
233 std::cout << "cannot read Ultrasonix Attributes for RF file [" << filename << "]" << std::endl;
237 fclose(Ultrasonix_file);
241 sprintf(cols,"%i", h.width);
244 sprintf(rows,"%i", h.height);
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);
256 std::map<std::string,std::string>::iterator i;
257 if ( (i = attr.find("FullFileName")) != attr.end())
259 i->second = filename;
261 if ( (i = attr.find("D0004_1500")) != attr.end())
263 boost::filesystem::path full_path(filename);
264 std::string f = full_path.leaf();
267 if ( (i = attr.find("D0028_0010")) != attr.end())
271 if ( (i = attr.find("D0028_0011")) != attr.end())
275 if ( (i = attr.find("D0028_0012")) != attr.end())
279 if ( (i = attr.find("D003a_001a")) != attr.end())
281 i->second = samplingFrequency;
283 if ( (i = attr.find("D0018_6030")) != attr.end())
285 i->second = transducerFrequency;
288 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
291 //=====================================================================
293 } // namespace creaImageIO