2 #include "creaImageIOSystem.h"
3 #include "creaImageIOUltrasonixImageReader.h"
5 #include <boost/filesystem/path.hpp>
14 //=====================================================================
15 UltrasonixImageReader::UltrasonixImageReader()
17 SetName("Ultrasonix");
19 //=====================================================================
21 //=====================================================================
22 UltrasonixImageReader::~UltrasonixImageReader()
25 //=====================================================================
27 //=====================================================================
28 struct Ultrasonix_header
30 // frames, width, height, ultrasounds frequency, sampling rate
31 int type, frame, width, height, frequency, samplingRate;
33 //=====================================================================
36 //=====================================================================
37 bool ReadHeader( FILE *Ultrasonix_file, Ultrasonix_header& h )
39 //int *header=(int*)malloc(sizeof(int)*HEADER_SIZE);
40 int header[HEADER_SIZE];
41 fread(header, sizeof(int), HEADER_SIZE, Ultrasonix_file);
42 if (ferror(Ultrasonix_file))
48 h.frequency = header[14];
49 h.samplingRate = header[15];
53 //=====================================================================
55 //=====================================================================
56 bool UltrasonixImageReader::CanRead(const std::string& filename)
60 FILE *Ultrasonix_file=fopen(filename.c_str(), "rb");
64 if (!ReadHeader(Ultrasonix_file, h) )
66 fclose(Ultrasonix_file);
67 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
71 fseek(Ultrasonix_file,0,SEEK_END); // go to end of file
72 if (h.type == TYPE_RF)
73 size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame) * sizeof(int)) / sizeof(short);
74 else if (h.type == 1)//TYPE_B8)
75 size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame+4) * sizeof(int)) / sizeof(char);
76 else if (h.type == TYPE_B32)
77 size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(int);
79 // check if the data size corresponds to the dimensions of the images
80 if (size == h.width * h.height * h.frame )
83 fclose(Ultrasonix_file);
87 //=====================================================================
88 void UltrasonixImageReader::getAttributes(const std::string filename,
89 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
94 //=====================================================================
95 vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
97 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
100 std::cout << "cannot open file [" << filename << "]" << std::endl;
104 if (!ReadHeader(Ultrasonix_file,h))
106 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
107 fclose(Ultrasonix_file);
111 long frame_size = h.height * h.width;
112 long im_size = frame_size * h.frame;
114 short *dataRF, *ptrRF;
115 char *dataB8, *ptrB8;
116 int *dataB32, *ptrB32;
123 dataRF = (short*)malloc(sizeof(short)*im_size);
126 for (int k=0; k<h.frame; k++)
129 fread(&frame_number, sizeof(int), 1, Ultrasonix_file);
130 fread(ptrRF,sizeof(short), frame_size, Ultrasonix_file);
133 fclose(Ultrasonix_file);
135 im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
139 dataB8 = (char*)malloc(sizeof(char)*im_size);
141 for (int k=0; k<h.frame; k++)
143 fread(ptrB8,sizeof(char), frame_size, Ultrasonix_file);
146 // in mode b frames width and height are inverted
151 fclose(Ultrasonix_file);
153 im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
157 dataB32 = (int*)malloc(sizeof(int)*im_size);
159 for (int k=0; k<h.frame; k++)
161 fread(ptrB32, sizeof(int), frame_size, Ultrasonix_file);
162 ptrB32 += frame_size;
164 // in B mode frames width and height are inverted
169 fclose(Ultrasonix_file);
171 im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
177 //=====================================================================
180 //=====================================================================
181 void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
183 v.push_back("Ultrasonix");
185 //=====================================================================
189 //=====================================================================
190 void UltrasonixImageReader::ReadAttributes(const std::string& filename,
191 std::map<std::string,std::string>& attr)
193 GimmickMessage(2,"Reading attributes from '" << filename << std::endl);
195 FILE *Ultrasonix_file = fopen(filename.c_str(), "rb");
196 if (!Ultrasonix_file)
198 std::cout << "cannot open RF file [" << filename << "]" << std::endl;
203 if (!ReadHeader(Ultrasonix_file, h))
205 fclose(Ultrasonix_file);
206 std::cout << "cannot read Ultrasonix Attributes for RF file [" << filename << "]" << std::endl;
210 fclose(Ultrasonix_file);
214 sprintf(cols,"%i", h.width);
217 sprintf(rows,"%i", h.height);
220 sprintf(planes,"%i", h.frame);
221 // Sampling frequency
222 char samplingFrequency[128];
223 sprintf(samplingFrequency,"%i", h.samplingRate);
224 // Transducer frequency
225 char transducerFrequency[128];
226 sprintf(transducerFrequency,"%i", h.frequency);
229 std::map<std::string,std::string>::iterator i;
230 if ( (i = attr.find("FullFileName")) != attr.end())
232 i->second = filename;
234 if ( (i = attr.find("D0004_1500")) != attr.end())
236 boost::filesystem::path full_path(filename);
237 std::string f = full_path.leaf();
240 if ( (i = attr.find("D0028_0010")) != attr.end())
244 if ( (i = attr.find("D0028_0011")) != attr.end())
248 if ( (i = attr.find("D0028_0012")) != attr.end())
252 if ( (i = attr.find("D003a_001a")) != attr.end())
254 i->second = samplingFrequency;
256 if ( (i = attr.find("D0018_6030")) != attr.end())
258 i->second = transducerFrequency;
261 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
264 //=====================================================================
266 } // namespace creaImageIO