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"
33 #include <boost/filesystem/path.hpp>
36 #pragma warning(disable: 4996)
41 #define HEADER_SIZE 19
47 //=====================================================================
48 UltrasonixImageReader::UltrasonixImageReader()
50 SetName("Ultrasonix");
52 //=====================================================================
54 //=====================================================================
55 UltrasonixImageReader::~UltrasonixImageReader()
58 //=====================================================================
60 //=====================================================================
61 struct Ultrasonix_header
63 // frames, width, height, ultrasounds frequency, sampling rate
64 int type, frame, width, height, frequency, samplingRate;
66 //=====================================================================
69 //=====================================================================
70 bool ReadHeader( FILE *Ultrasonix_file, Ultrasonix_header& h )
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))
81 h.frequency = header[14];
82 h.samplingRate = header[15];
86 //=====================================================================
88 //=====================================================================
89 bool UltrasonixImageReader::CanRead(const std::string& filename)
93 FILE *Ultrasonix_file=fopen(filename.c_str(), "rb");
97 if (!ReadHeader(Ultrasonix_file, h) )
99 fclose(Ultrasonix_file);
100 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
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);
112 // check if the data size corresponds to the dimensions of the images
113 if (size == h.width * h.height * h.frame )
116 fclose(Ultrasonix_file);
120 //=====================================================================
121 void UltrasonixImageReader::getAttributes(const std::string filename,
122 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
127 //=====================================================================
128 vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
130 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
131 if (!Ultrasonix_file)
133 std::cout << "cannot open file [" << filename << "]" << std::endl;
137 if (!ReadHeader(Ultrasonix_file,h))
139 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
140 fclose(Ultrasonix_file);
144 long frame_size = h.height * h.width;
145 long im_size = frame_size * h.frame;
147 short *dataRF, *ptrRF;
148 char *dataB8, *ptrB8;
149 int *dataB32, *ptrB32;
156 dataRF = (short*)malloc(sizeof(short)*im_size);
159 for (int k=0; k<h.frame; k++)
162 fread(&frame_number, sizeof(int), 1, Ultrasonix_file);
163 fread(ptrRF,sizeof(short), frame_size, Ultrasonix_file);
166 fclose(Ultrasonix_file);
168 im = crea::NewVtkImageDataFromRaw<short>( dataRF, h.width, h.height, h.frame);
172 dataB8 = (char*)malloc(sizeof(char)*im_size);
174 for (int k=0; k<h.frame; k++)
176 fread(ptrB8,sizeof(char), frame_size, Ultrasonix_file);
179 // in mode b frames width and height are inverted
184 fclose(Ultrasonix_file);
186 im = crea::NewVtkImageDataFromRaw<char>( dataB8, h.width, h.height, h.frame);
190 dataB32 = (int*)malloc(sizeof(int)*im_size);
192 for (int k=0; k<h.frame; k++)
194 fread(ptrB32, sizeof(int), frame_size, Ultrasonix_file);
195 ptrB32 += frame_size;
197 // in B mode frames width and height are inverted
202 fclose(Ultrasonix_file);
204 im = crea::NewVtkImageDataFromRaw<int>( dataB32, h.width, h.height, h.frame);
210 //=====================================================================
213 //=====================================================================
214 void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
216 v.push_back("Ultrasonix");
218 //=====================================================================
222 //=====================================================================
223 void UltrasonixImageReader::ReadAttributes(const std::string& filename,
224 std::map<std::string,std::string>& attr)
226 GimmickMessage(2,"Reading attributes from '" << filename << std::endl);
228 FILE *Ultrasonix_file = fopen(filename.c_str(), "rb");
229 if (!Ultrasonix_file)
231 std::cout << "cannot open RF file [" << filename << "]" << std::endl;
236 if (!ReadHeader(Ultrasonix_file, h))
238 fclose(Ultrasonix_file);
239 std::cout << "cannot read Ultrasonix Attributes for RF file [" << filename << "]" << std::endl;
243 fclose(Ultrasonix_file);
247 sprintf(cols,"%i", h.width);
250 sprintf(rows,"%i", h.height);
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);
262 std::map<std::string,std::string>::iterator i;
263 if ( (i = attr.find("FullFileName")) != attr.end())
265 i->second = filename;
267 if ( (i = attr.find("D0004_1500")) != attr.end())
269 boost::filesystem::path full_path(filename);
270 std::string f = full_path.leaf().string();
273 if ( (i = attr.find("D0028_0010")) != attr.end())
277 if ( (i = attr.find("D0028_0011")) != attr.end())
281 if ( (i = attr.find("D0028_0012")) != attr.end())
285 if ( (i = attr.find("D003a_001a")) != attr.end())
287 i->second = samplingFrequency;
289 if ( (i = attr.find("D0018_6030")) != attr.end())
291 i->second = transducerFrequency;
294 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
297 //=====================================================================
299 } // namespace creaImageIO