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 == TYPE_B8)
75 size = (ftell(Ultrasonix_file) - HEADER_SIZE * 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 //=====================================================================
89 //=====================================================================
90 vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
92 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
95 std::cout << "cannot open file [" << filename << "]" << std::endl;
99 if (!ReadHeader(Ultrasonix_file,h))
101 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
102 fclose(Ultrasonix_file);
106 long frame_size = h.height * h.width;
107 long im_size = frame_size * h.frame;
109 short *dataRF, *ptrRF;
110 char *dataB8, *ptrB8;
111 int *dataB32, *ptrB32;
118 dataRF = (short*)malloc(sizeof(short)*im_size);
121 for (int k=0; k<h.frame; k++)
124 fread(&frame_number, sizeof(int), 1, Ultrasonix_file);
125 fread(ptrRF,sizeof(short), frame_size, Ultrasonix_file);
128 fclose(Ultrasonix_file);
130 im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
134 dataB8 = (char*)malloc(sizeof(char)*im_size);
136 for (int k=0; k<h.frame; k++)
138 fread(ptrB8,sizeof(char), frame_size, Ultrasonix_file);
141 // in mode b frames width and height are inverted
146 fclose(Ultrasonix_file);
148 im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
152 dataB32 = (int*)malloc(sizeof(int)*im_size);
154 for (int k=0; k<h.frame; k++)
156 fread(ptrB32, sizeof(int), frame_size, Ultrasonix_file);
157 ptrB32 += frame_size;
159 // in B mode frames width and height are inverted
164 fclose(Ultrasonix_file);
166 im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
172 //=====================================================================
175 //=====================================================================
176 void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
178 v.push_back("Ultrasonix");
180 //=====================================================================
184 //=====================================================================
185 void UltrasonixImageReader::ReadAttributes(const std::string& filename,
186 std::map<std::string,std::string>& attr)
188 GimmickMessage(2,"Reading attributes from '" << filename << std::endl);
190 FILE *Ultrasonix_file = fopen(filename.c_str(), "rb");
191 if (!Ultrasonix_file)
193 std::cout << "cannot open RF file [" << filename << "]" << std::endl;
198 if (!ReadHeader(Ultrasonix_file, h))
200 fclose(Ultrasonix_file);
201 std::cout << "cannot read Ultrasonix Attributes for RF file [" << filename << "]" << std::endl;
205 fclose(Ultrasonix_file);
209 sprintf(cols,"%i", h.width);
212 sprintf(rows,"%i", h.height);
215 sprintf(planes,"%i", h.frame);
216 // Sampling frequency
217 char samplingFrequency[128];
218 sprintf(samplingFrequency,"%i", h.samplingRate);
219 // Transducer frequency
220 char transducerFrequency[128];
221 sprintf(transducerFrequency,"%i", h.frequency);
224 std::map<std::string,std::string>::iterator i;
225 if ( (i = attr.find("FullFileName")) != attr.end())
227 i->second = filename;
229 if ( (i = attr.find("D0004_1500")) != attr.end())
231 boost::filesystem::path full_path(filename);
232 std::string f = full_path.leaf();
235 if ( (i = attr.find("D0028_0010")) != attr.end())
239 if ( (i = attr.find("D0028_0011")) != attr.end())
243 if ( (i = attr.find("D0028_0012")) != attr.end())
247 if ( (i = attr.find("D003a_001a")) != attr.end())
249 i->second = samplingFrequency;
251 if ( (i = attr.find("D0018_6030")) != attr.end())
253 i->second = transducerFrequency;
256 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
259 //=====================================================================
261 } // namespace creaImageIO