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 int header[HEADER_SIZE];
42 fread(header, sizeof(int), HEADER_SIZE, Ultrasonix_file);
43 if (ferror(Ultrasonix_file))
49 h.frequency = header[14];
50 h.samplingRate = header[15];
54 //=====================================================================
56 //=====================================================================
57 bool UltrasonixImageReader::CanRead(const std::string& filename)
61 FILE *Ultrasonix_file=fopen(filename.c_str(), "rb");
65 if (!ReadHeader(Ultrasonix_file, h) )
67 fclose(Ultrasonix_file);
68 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
72 fseek(Ultrasonix_file,0,SEEK_END); // go to end of file
73 if (h.type == TYPE_RF)
74 size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame) * sizeof(int)) / sizeof(short);
75 else if (h.type == TYPE_B8)
76 size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(char);
77 else if (h.type == TYPE_B32)
78 size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(int);
80 // check if the data size corresponds to the dimensions of the images
81 if (size == h.width * h.height * h.frame)
84 fclose(Ultrasonix_file);
88 //=====================================================================
90 //=====================================================================
91 vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
93 FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
96 std::cout << "cannot open file [" << filename << "]" << std::endl;
100 if (!ReadHeader(Ultrasonix_file,h))
102 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
103 fclose(Ultrasonix_file);
107 long frame_size = h.height * h.width;
108 long im_size = frame_size * h.frame;
110 short *dataRF, *ptrRF;
111 char *dataB8, *ptrB8;
112 int *dataB32, *ptrB32;
119 dataRF = (short*)malloc(sizeof(short)*im_size);
122 for (int k=0; k<h.frame; k++)
125 fread(&frame_number, sizeof(int), 1, Ultrasonix_file);
126 fread(ptrRF,sizeof(short), frame_size, Ultrasonix_file);
129 fclose(Ultrasonix_file);
131 im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
135 dataB8 = (char*)malloc(sizeof(char)*im_size);
137 for (int k=0; k<h.frame; k++)
139 fread(ptrB8,sizeof(char), frame_size, Ultrasonix_file);
142 // in mode b frames width and height are inverted
147 fclose(Ultrasonix_file);
149 im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
153 dataB32 = (int*)malloc(sizeof(int)*im_size);
155 for (int k=0; k<h.frame; k++)
157 fread(ptrB32, sizeof(int), frame_size, Ultrasonix_file);
158 ptrB32 += frame_size;
160 // in B mode frames width and height are inverted
165 fclose(Ultrasonix_file);
167 im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
173 //=====================================================================
176 //=====================================================================
177 void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
179 v.push_back("Ultrasonix");
181 //=====================================================================
185 //=====================================================================
186 void UltrasonixImageReader::ReadAttributes(const std::string& filename,
187 std::map<std::string,std::string>& attr)
189 GimmickMessage(2,"Reading attributes from '" << filename << std::endl);
191 FILE *Ultrasonix_file = fopen(filename.c_str(), "rb");
192 if (!Ultrasonix_file)
194 std::cout << "cannot open RF file [" << filename << "]" << std::endl;
199 if (!ReadHeader(Ultrasonix_file, h))
201 fclose(Ultrasonix_file);
202 std::cout << "cannot read Ultrasonix Attributes for RF file [" << filename << "]" << std::endl;
206 fclose(Ultrasonix_file);
210 sprintf(cols,"%i", h.width);
213 sprintf(rows,"%i", h.height);
216 sprintf(planes,"%i", h.frame);
217 // Sampling frequency
218 char samplingFrequency[128];
219 sprintf(samplingFrequency,"%i", h.samplingRate);
220 // Transducer frequency
221 char transducerFrequency[128];
222 sprintf(transducerFrequency,"%i", h.frequency);
225 std::map<std::string,std::string>::iterator i;
226 if ( (i = attr.find("FullFileName")) != attr.end())
228 i->second = filename;
230 if ( (i = attr.find("D0004_1500")) != attr.end())
232 boost::filesystem::path full_path(filename);
233 std::string f = full_path.leaf();
236 if ( (i = attr.find("D0028_0010")) != attr.end())
240 if ( (i = attr.find("D0028_0011")) != attr.end())
244 if ( (i = attr.find("D0028_0012")) != attr.end())
248 if ( (i = attr.find("D003a_001a")) != attr.end())
250 i->second = samplingFrequency;
252 if ( (i = attr.find("D0018_6030")) != attr.end())
254 i->second = transducerFrequency;
257 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
260 //=====================================================================
262 } // namespace creaImageIO