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 // std::cout << "UltrasonixImageReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
176 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
179 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
180 if (!Ultrasonix_file) return;
182 if (!ReadHeader(Ultrasonix_file,h))
184 fclose(Ultrasonix_file);
187 fclose(Ultrasonix_file);
191 sprintf(cols,"%i",h.width);
194 sprintf(rows,"%i",h.height);
197 sprintf(planes,"%i",h.frame);
201 std::map<std::string,std::string>::iterator i;
202 if ( (i = attr.find("FullFileName")) != attr.end())
204 // boost::filesystem::path full_path(filename);
205 // std::string f = full_path.leaf();
206 i->second = filename;
208 if ( (i = attr.find("D0004_1500")) != attr.end())
210 boost::filesystem::path full_path(filename);
211 std::string f = full_path.leaf();
214 if ( (i = attr.find("D0028_0010")) != attr.end())
218 if ( (i = attr.find("D0028_0011")) != attr.end())
223 if ( (i = attr.find("D0028_0012")) != attr.end())
228 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
230 //=====================================================================
232 } // namespace creaImageIO