creaImageIOUltrasonixImageReader.cpp

Go to the documentation of this file.
00001 
00002 #include "creaImageIOSystem.h"
00003 #include "creaImageIOUltrasonixImageReader.h"
00004 #include <creaVtk.h>
00005 #include <boost/filesystem/path.hpp>
00006 namespace creaImageIO
00007 {
00008 #define HEADER_SIZE     19
00009 #define TYPE_RF         16
00010 #define TYPE_B8         4
00011 #define TYPE_B32        8
00012 
00013 
00014   //=====================================================================
00015   UltrasonixImageReader::UltrasonixImageReader()
00016   {
00017     SetName("Ultrasonix");
00018   }
00019   //=====================================================================
00020   
00021   //=====================================================================
00022   UltrasonixImageReader::~UltrasonixImageReader()
00023   {
00024   }
00025   //=====================================================================
00026 
00027   //=====================================================================
00028   struct Ultrasonix_header
00029   {
00030     // frames, width, height, ultrasounds frequency, sampling rate
00031     int type, frame, width, height, frequency, samplingRate;
00032   };
00033   //=====================================================================
00034 
00035 
00036   //=====================================================================
00037   bool ReadHeader( FILE *Ultrasonix_file, Ultrasonix_header& h )
00038   {
00039     //int *header=(int*)malloc(sizeof(int)*HEADER_SIZE);
00040     int header[HEADER_SIZE];
00041     fread(header, sizeof(int), HEADER_SIZE, Ultrasonix_file);
00042     if (ferror(Ultrasonix_file))
00043         return false;
00044     h.type         = header[1];
00045     h.frame        = header[2];
00046     h.height       = header[3];
00047     h.width        = header[4];
00048     h.frequency    = header[14];
00049     h.samplingRate = header[15];
00050     //free(header);  
00051     return true;
00052   }
00053   //=====================================================================
00054 
00055   //=====================================================================
00056   bool UltrasonixImageReader::CanRead(const std::string& filename)
00057   { 
00058     long size = -1;
00059     bool ok = false;
00060     FILE *Ultrasonix_file=fopen(filename.c_str(), "rb");
00061     if (Ultrasonix_file) 
00062     {
00063         Ultrasonix_header h;
00064         if (!ReadHeader(Ultrasonix_file, h) )
00065         {
00066                 fclose(Ultrasonix_file);
00067                 std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;                                
00068                 return false;   
00069         }
00070 
00071         fseek(Ultrasonix_file,0,SEEK_END);              // go to end of file
00072         if (h.type == TYPE_RF)
00073                 size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame) * sizeof(int)) / sizeof(short);
00074         else if (h.type == 1)//TYPE_B8)
00075                 size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame+4) *  sizeof(int)) / sizeof(char);
00076         else if (h.type == TYPE_B32)
00077                 size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(int);
00078 
00079         // check if the data size corresponds to the dimensions of the images
00080         if (size == h.width * h.height * h.frame )
00081                 ok = true;
00082 
00083         fclose(Ultrasonix_file);
00084     }
00085     return ok;
00086   }
00087   //=====================================================================
00088   void UltrasonixImageReader::getAttributes(const std::string filename,
00089                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
00090   {
00091           //TO DO
00092   }
00093   
00094   //=====================================================================
00095   vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
00096   {
00097     FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
00098     if (!Ultrasonix_file) 
00099     {
00100         std::cout << "cannot open file [" << filename << "]" << std::endl;
00101         return 0;
00102     }
00103     Ultrasonix_header h;
00104     if (!ReadHeader(Ultrasonix_file,h)) 
00105     {
00106         std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl;
00107         fclose(Ultrasonix_file);  
00108         return 0;
00109     }
00110 
00111     long frame_size = h.height   * h.width;      
00112     long im_size    = frame_size * h.frame;
00113 
00114     short *dataRF,  *ptrRF;
00115     char  *dataB8,  *ptrB8;
00116     int   *dataB32, *ptrB32;
00117     vtkImageData* im;
00118     int temp;
00119 
00120     switch (h.type)
00121     {
00122         case TYPE_RF:
00123                 dataRF = (short*)malloc(sizeof(short)*im_size);
00124                 ptrRF  = dataRF;
00125 
00126                 for (int k=0; k<h.frame; k++)
00127                 {
00128                         int frame_number;
00129                         fread(&frame_number, sizeof(int), 1, Ultrasonix_file);
00130                         fread(ptrRF,sizeof(short), frame_size, Ultrasonix_file);
00131                         ptrRF += frame_size;
00132                 }
00133                 fclose(Ultrasonix_file);  
00134 
00135                 im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
00136         break;
00137 
00138         case TYPE_B8:
00139                 dataB8 = (char*)malloc(sizeof(char)*im_size);
00140                 ptrB8  = dataB8;
00141                 for (int k=0; k<h.frame; k++)
00142                 {
00143                         fread(ptrB8,sizeof(char), frame_size, Ultrasonix_file);
00144                         ptrB8 += frame_size;
00145                 }
00146                 // in mode b frames width and height are inverted
00147                 temp     = h.width;
00148                 h.width  = h.height;
00149                 h.height = temp;
00150 
00151                 fclose(Ultrasonix_file);  
00152 
00153         im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
00154         break;
00155 
00156         case TYPE_B32:
00157                 dataB32 = (int*)malloc(sizeof(int)*im_size);
00158                 ptrB32  = dataB32;
00159                 for (int k=0; k<h.frame; k++)
00160                 {
00161                         fread(ptrB32, sizeof(int), frame_size, Ultrasonix_file);
00162                         ptrB32 += frame_size;
00163                 }
00164                 // in B mode frames width and height are inverted
00165                 temp     = h.width;
00166                 h.width  = h.height;
00167                 h.height = temp;
00168 
00169                 fclose(Ultrasonix_file);  
00170 
00171                 im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
00172         break;
00173     }
00174 
00175     return im;
00176 }
00177   //=====================================================================
00178   
00179 
00180   //=====================================================================
00181   void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
00182   {
00183     v.push_back("Ultrasonix");
00184   }
00185   //=====================================================================
00186  
00187 
00188 
00189   //=====================================================================
00190  void UltrasonixImageReader::ReadAttributes(const std::string& filename, 
00191                                       std::map<std::string,std::string>& attr)
00192  {
00193     GimmickMessage(2,"Reading attributes from '" << filename << std::endl);
00194 
00195     FILE *Ultrasonix_file = fopen(filename.c_str(), "rb");
00196     if (!Ultrasonix_file)
00197     {
00198         std::cout << "cannot open RF file [" << filename << "]" << std::endl;
00199         return;
00200     }
00201 
00202     Ultrasonix_header h;
00203     if (!ReadHeader(Ultrasonix_file, h)) 
00204     {
00205         fclose(Ultrasonix_file);
00206         std::cout << "cannot read Ultrasonix Attributes for RF file [" << filename << "]" << std::endl;  
00207         return;
00208     }
00209 
00210     fclose(Ultrasonix_file);  
00211   
00212     // Columns
00213     char cols[128];
00214     sprintf(cols,"%i", h.width);
00215     // Rows
00216     char rows[128];
00217     sprintf(rows,"%i", h.height);
00218     // Planes 
00219     char planes[128];
00220     sprintf(planes,"%i", h.frame);
00221     // Sampling frequency
00222     char samplingFrequency[128];
00223     sprintf(samplingFrequency,"%i", h.samplingRate);
00224     // Transducer frequency
00225     char transducerFrequency[128];
00226     sprintf(transducerFrequency,"%i", h.frequency);
00227    
00228     // 
00229     std::map<std::string,std::string>::iterator i;
00230     if ( (i = attr.find("FullFileName")) != attr.end())
00231     {
00232            i->second = filename;
00233     }
00234     if ( (i = attr.find("D0004_1500")) != attr.end())
00235     {
00236            boost::filesystem::path full_path(filename);
00237            std::string f = full_path.leaf();
00238            i->second = f;
00239     }
00240     if ( (i = attr.find("D0028_0010")) != attr.end())
00241     {
00242            i->second = rows;
00243     }
00244     if ( (i = attr.find("D0028_0011")) != attr.end())
00245     {
00246            i->second = cols;
00247     }
00248     if ( (i = attr.find("D0028_0012")) != attr.end())
00249     {
00250            i->second = planes;
00251     }
00252     if ( (i = attr.find("D003a_001a")) != attr.end())
00253     {
00254        i->second = samplingFrequency;
00255     }
00256     if ( (i = attr.find("D0018_6030")) != attr.end())
00257     {
00258            i->second = transducerFrequency;
00259     }
00260     
00261     GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
00262     return;
00263 }
00264   //=====================================================================
00265 
00266 } // namespace creaImageIO