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 #pragma warning(disable: 4996)
40 #define HEADER_SIZE 19
46 //=====================================================================
47 UltrasonixImageReader::UltrasonixImageReader()
49 SetName("Ultrasonix");
51 //=====================================================================
53 //=====================================================================
54 UltrasonixImageReader::~UltrasonixImageReader()
57 //=====================================================================
59 //=====================================================================
60 struct Ultrasonix_header
62 // frames, width, height, ultrasounds frequency, sampling rate
63 int type, frame, width, height, frequency, samplingRate;
65 //=====================================================================
68 //=====================================================================
69 bool ReadHeader( FILE *Ultrasonix_file, Ultrasonix_header& h )
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))
80 h.frequency = header[14];
81 h.samplingRate = header[15];
85 //=====================================================================
87 //=====================================================================
88 bool UltrasonixImageReader::CanRead(const std::string& filename)
92 FILE *Ultrasonix_file=fopen(filename.c_str(), "rb");
96 if (!ReadHeader(Ultrasonix_file, h) )
98 fclose(Ultrasonix_file);
99 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
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);
111 // check if the data size corresponds to the dimensions of the images
112 if (size == h.width * h.height * h.frame )
115 fclose(Ultrasonix_file);
119 //=====================================================================
120 void UltrasonixImageReader::getAttributes(const std::string filename,
121 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
126 //=====================================================================
127 vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
129 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
130 if (!Ultrasonix_file)
132 std::cout << "cannot open file [" << filename << "]" << std::endl;
136 if (!ReadHeader(Ultrasonix_file,h))
138 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
139 fclose(Ultrasonix_file);
143 long frame_size = h.height * h.width;
144 long im_size = frame_size * h.frame;
146 short *dataRF, *ptrRF;
147 char *dataB8, *ptrB8;
148 int *dataB32, *ptrB32;
155 dataRF = (short*)malloc(sizeof(short)*im_size);
158 for (int k=0; k<h.frame; k++)
161 fread(&frame_number, sizeof(int), 1, Ultrasonix_file);
162 fread(ptrRF,sizeof(short), frame_size, Ultrasonix_file);
165 fclose(Ultrasonix_file);
167 im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
171 dataB8 = (char*)malloc(sizeof(char)*im_size);
173 for (int k=0; k<h.frame; k++)
175 fread(ptrB8,sizeof(char), frame_size, Ultrasonix_file);
178 // in mode b frames width and height are inverted
183 fclose(Ultrasonix_file);
185 im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
189 dataB32 = (int*)malloc(sizeof(int)*im_size);
191 for (int k=0; k<h.frame; k++)
193 fread(ptrB32, sizeof(int), frame_size, Ultrasonix_file);
194 ptrB32 += frame_size;
196 // in B mode frames width and height are inverted
201 fclose(Ultrasonix_file);
203 im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
209 //=====================================================================
212 //=====================================================================
213 void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
215 v.push_back("Ultrasonix");
217 //=====================================================================
221 //=====================================================================
222 void UltrasonixImageReader::ReadAttributes(const std::string& filename,
223 std::map<std::string,std::string>& attr)
225 GimmickMessage(2,"Reading attributes from '" << filename << std::endl);
227 FILE *Ultrasonix_file = fopen(filename.c_str(), "rb");
228 if (!Ultrasonix_file)
230 std::cout << "cannot open RF file [" << filename << "]" << std::endl;
235 if (!ReadHeader(Ultrasonix_file, h))
237 fclose(Ultrasonix_file);
238 std::cout << "cannot read Ultrasonix Attributes for RF file [" << filename << "]" << std::endl;
242 fclose(Ultrasonix_file);
246 sprintf(cols,"%i", h.width);
249 sprintf(rows,"%i", h.height);
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);
261 std::map<std::string,std::string>::iterator i;
262 if ( (i = attr.find("FullFileName")) != attr.end())
264 i->second = filename;
266 if ( (i = attr.find("D0004_1500")) != attr.end())
268 boost::filesystem::path full_path(filename);
269 std::string f = full_path.leaf().string();
272 if ( (i = attr.find("D0028_0010")) != attr.end())
276 if ( (i = attr.find("D0028_0011")) != attr.end())
280 if ( (i = attr.find("D0028_0012")) != attr.end())
284 if ( (i = attr.find("D003a_001a")) != attr.end())
286 i->second = samplingFrequency;
288 if ( (i = attr.find("D0018_6030")) != attr.end())
290 i->second = transducerFrequency;
293 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
296 //=====================================================================
298 } // namespace creaImageIO