2 #include <creaImageIOUltrasonixImageReader.h>
4 #include <creaImageIOSystem.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)
86 std::cout<<"UltrasonixImageReader reading "<<std::endl;
88 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
89 if (!Ultrasonix_file) return false;
91 if (!ReadHeader(Ultrasonix_file,h))
93 fclose(Ultrasonix_file);
97 long im_size = h.height * h.width * h.frame;
98 long frame_size = h.height * h.width;
99 short *dataRF, *ptrRF;
100 char *dataB8, *ptrB8;
101 int *dataB32, *ptrB32;
108 dataRF = (short*)malloc(sizeof(short)*im_size);
111 for (int k=0; k<h.frame; k++)
114 fread(&frame_number,sizeof(int),1,Ultrasonix_file);
115 fread(ptrRF,sizeof(short),frame_size,Ultrasonix_file);
118 fclose(Ultrasonix_file);
120 im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
124 dataB8 = (char*)malloc(sizeof(char)*im_size);
126 for (int k=0; k<h.frame; k++)
128 fread(ptrB8,sizeof(char),frame_size,Ultrasonix_file);
131 // in mode b frames width and height are inverted
136 fclose(Ultrasonix_file);
138 im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
142 dataB32 = (int*)malloc(sizeof(int)*im_size);
144 for (int k=0; k<h.frame; k++)
146 fread(ptrB32,sizeof(int),frame_size,Ultrasonix_file);
147 ptrB32 += frame_size;
149 // in B mode frames width and height are inverted
154 fclose(Ultrasonix_file);
156 im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
162 //=====================================================================
165 //=====================================================================
166 void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
168 v.push_back("Ultrasonix");
170 //=====================================================================
174 //=====================================================================
175 void UltrasonixImageReader::ReadAttributes(const std::string& filename,
176 std::map<std::string,std::string>& attr)
178 // std::cout << "UltrasonixImageReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
179 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
182 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
183 if (!Ultrasonix_file) return;
185 if (!ReadHeader(Ultrasonix_file,h))
187 fclose(Ultrasonix_file);
190 fclose(Ultrasonix_file);
194 sprintf(cols,"%i",h.width);
197 sprintf(rows,"%i",h.height);
200 sprintf(planes,"%i",h.frame);
204 std::map<std::string,std::string>::iterator i;
205 if ( (i = attr.find("FullFileName")) != attr.end())
207 // boost::filesystem::path full_path(filename);
208 // std::string f = full_path.leaf();
209 i->second = filename;
211 if ( (i = attr.find("D0004_1500")) != attr.end())
213 boost::filesystem::path full_path(filename);
214 std::string f = full_path.leaf();
217 if ( (i = attr.find("D0028_0010")) != attr.end())
221 if ( (i = attr.find("D0028_0011")) != attr.end())
226 if ( (i = attr.find("D0028_0012")) != attr.end())
231 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
233 //=====================================================================
235 } // namespace creaImageIO