2 #include <creaImageIOSystem.h>
3 #include <creaImageIOUltrasonixImageReader.h>
5 #include "boost/filesystem/path.hpp"
15 //=====================================================================
16 UltrasonixImageReader::UltrasonixImageReader()
18 SetName("Ultrasonix");
20 //=====================================================================
22 //=====================================================================
23 UltrasonixImageReader::~UltrasonixImageReader()
26 //=====================================================================
28 //=====================================================================
29 struct Ultrasonix_header
31 // frames, width, height, ultrasounds frequency, sampling rate
32 int type,frame,width,height,frequency,samplingRate;
34 //=====================================================================
37 //=====================================================================
38 bool ReadHeader( FILE *Ultrasonix_file, Ultrasonix_header& h )
40 int *header=(int*)malloc(sizeof(int)*HEADER_SIZE);
41 fread(header,sizeof(int),HEADER_SIZE,Ultrasonix_file);
46 h.frequency = header[14];
47 h.samplingRate = header[15];
51 //=====================================================================
53 //=====================================================================
54 bool UltrasonixImageReader::CanRead(const std::string& filename)
57 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
62 ReadHeader(Ultrasonix_file,h);
64 fseek(Ultrasonix_file,0,SEEK_END); // go to end of file
65 if (h.type == TYPE_RF)
66 size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame) * sizeof(int)) / sizeof(short);
67 else if (h.type == TYPE_B8)
68 size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(char);
69 else if (h.type == TYPE_B32)
70 size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(int);
72 // check if the data size corresponds to the dimensions of the images
73 if (size == h.width * h.height * h.frame)
76 fclose(Ultrasonix_file);
80 //=====================================================================
82 //=====================================================================
83 vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
85 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
86 if (!Ultrasonix_file) return false;
88 if (!ReadHeader(Ultrasonix_file,h))
90 fclose(Ultrasonix_file);
94 long im_size = h.height * h.width * h.frame;
95 long frame_size = h.height * h.width;
96 short *dataRF, *ptrRF;
98 int *dataB32, *ptrB32;
105 dataRF = (short*)malloc(sizeof(short)*im_size);
108 for (int k=0; k<h.frame; k++)
111 fread(&frame_number,sizeof(int),1,Ultrasonix_file);
112 fread(ptrRF,sizeof(short),frame_size,Ultrasonix_file);
115 fclose(Ultrasonix_file);
117 im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
121 dataB8 = (char*)malloc(sizeof(char)*im_size);
123 for (int k=0; k<h.frame; k++)
125 fread(ptrB8,sizeof(char),frame_size,Ultrasonix_file);
128 // in mode b frames width and height are inverted
133 fclose(Ultrasonix_file);
135 im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
139 dataB32 = (int*)malloc(sizeof(int)*im_size);
141 for (int k=0; k<h.frame; k++)
143 fread(ptrB32,sizeof(int),frame_size,Ultrasonix_file);
144 ptrB32 += frame_size;
146 // in B mode frames width and height are inverted
151 fclose(Ultrasonix_file);
153 im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
159 //=====================================================================
162 //=====================================================================
163 void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
165 v.push_back("Ultrasonix");
167 //=====================================================================
171 //=====================================================================
172 void UltrasonixImageReader::ReadAttributes(const std::string& filename,
173 std::map<std::string,std::string>& attr)
175 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
177 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
178 if (!Ultrasonix_file) return;
180 if (!ReadHeader(Ultrasonix_file,h))
182 fclose(Ultrasonix_file);
185 fclose(Ultrasonix_file);
189 sprintf(cols,"%i",h.width);
192 sprintf(rows,"%i",h.height);
195 sprintf(planes,"%i",h.frame);
196 // Sampling frequency
197 char samplingFrequency[128];
198 sprintf(samplingFrequency,"%i",h.samplingRate);
199 // Transducer frequency
200 char transducerFrequency[128];
201 sprintf(transducerFrequency,"%i",h.frequency);
205 std::map<std::string,std::string>::iterator i;
206 if ( (i = attr.find("FullFileName")) != attr.end())
208 i->second = filename;
210 if ( (i = attr.find("D0004_1500")) != attr.end())
212 boost::filesystem::path full_path(filename);
213 std::string f = full_path.leaf();
216 if ( (i = attr.find("D0028_0010")) != attr.end())
220 if ( (i = attr.find("D0028_0011")) != attr.end())
224 if ( (i = attr.find("D0028_0012")) != attr.end())
228 if ( (i = attr.find("D003a_001a")) != attr.end())
230 i->second = samplingFrequency;
232 if ( (i = attr.find("D0018_6030")) != attr.end())
234 i->second = transducerFrequency;
238 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
240 //=====================================================================
242 } // namespace creaImageIO