]> Creatis software - creaImageIO.git/commitdiff
Add support for Ultrasonix images (.rf, .b8, .b32).
authormaltaverne <maltaverne>
Tue, 17 Mar 2009 16:41:08 +0000 (16:41 +0000)
committermaltaverne <maltaverne>
Tue, 17 Mar 2009 16:41:08 +0000 (16:41 +0000)
src2/CMakeLists.txt
src2/creaImageIOUltrasonixImageReader.cpp [new file with mode: 0644]
src2/creaImageIOUltrasonixImageReader.h [new file with mode: 0644]

index 1ce13d9f87580a3818bfd2bd7eb48a37b4b79716..cac05e519f880b8209d4625ddd49f9d1f030184a 100644 (file)
@@ -20,7 +20,7 @@ SET( SRCS
   creaImageIOAbstractImageReader
   creaImageIOVtkImageReader
   creaImageIODicomImageReader
-  creaImageIORFImageReader
+  creaImageIOUltrasonixImageReader
   creaImageIOImageReader
   creaImageIOMultiThreadImageReader
 
diff --git a/src2/creaImageIOUltrasonixImageReader.cpp b/src2/creaImageIOUltrasonixImageReader.cpp
new file mode 100644 (file)
index 0000000..12c7440
--- /dev/null
@@ -0,0 +1,235 @@
+
+#include <creaImageIOUltrasonixImageReader.h>
+#include <creaVtk.h>
+#include <creaImageIOSystem.h>
+#include "boost/filesystem/path.hpp"
+
+namespace creaImageIO
+{
+#define HEADER_SIZE    19
+#define TYPE_RF                16
+#define TYPE_B8                4
+#define TYPE_B32               8
+
+
+  //=====================================================================
+  UltrasonixImageReader::UltrasonixImageReader()
+  {
+    SetName("Ultrasonix");
+  }
+  //=====================================================================
+  
+  //=====================================================================
+  UltrasonixImageReader::~UltrasonixImageReader()
+  {
+  }
+  //=====================================================================
+
+  //=====================================================================
+  struct Ultrasonix_header
+  {
+    // frames, width, height, ultrasounds frequency, sampling rate
+    int type,frame,width,height,frequency,samplingRate;
+  };
+  //=====================================================================
+
+
+  //=====================================================================
+  bool ReadHeader( FILE *Ultrasonix_file, Ultrasonix_header& h )
+  {
+    int *header=(int*)malloc(sizeof(int)*HEADER_SIZE);
+    fread(header,sizeof(int),HEADER_SIZE,Ultrasonix_file);
+        h.type = header[0];
+    h.frame  = header[1];
+    h.height = header[2];
+    h.width = header[3];
+        h.frequency = header[14];
+        h.samplingRate = header[15];
+        free(header);  
+    return true;
+  }
+  //=====================================================================
+
+  //=====================================================================
+  bool UltrasonixImageReader::CanRead(const std::string& filename)
+  { 
+    bool ok = false;
+    FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
+        long size;
+    if (Ultrasonix_file) 
+    {
+               Ultrasonix_header h;
+               ReadHeader(Ultrasonix_file,h);
+
+               fseek(Ultrasonix_file,0,SEEK_END);              // go to end of file
+               if (h.type == TYPE_RF)
+                       size = (ftell(Ultrasonix_file) - (HEADER_SIZE+h.frame) * sizeof(int)) / sizeof(short);
+               else if (h.type == TYPE_B8)
+                       size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(char);
+               else if (h.type == TYPE_B32)
+                       size = (ftell(Ultrasonix_file) - HEADER_SIZE * sizeof(int)) / sizeof(int);
+
+               // check if the data size corresponds to the dimensions of the images
+               if (size == h.width * h.height * h.frame)
+                       ok = true;
+               
+               fclose(Ultrasonix_file);
+    }
+    return ok;
+  }
+  //=====================================================================
+  
+  //=====================================================================
+  vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename)
+  {
+
+         std::cout<<"UltrasonixImageReader reading "<<std::endl;
+
+    FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
+    if (!Ultrasonix_file) return false;
+    Ultrasonix_header h;
+    if (!ReadHeader(Ultrasonix_file,h)) 
+    {
+               fclose(Ultrasonix_file);  
+               return 0;
+    }
+    
+    long im_size = h.height * h.width * h.frame;
+    long frame_size = h.height * h.width;  
+        short *dataRF, *ptrRF;
+        char *dataB8, *ptrB8;
+        int *dataB32, *ptrB32;
+        vtkImageData* im;
+        int temp;
+
+        switch (h.type)
+        {
+               case TYPE_RF:
+                       dataRF = (short*)malloc(sizeof(short)*im_size);
+                       ptrRF = dataRF;
+
+                       for (int k=0; k<h.frame; k++)
+                       {
+                               int frame_number;
+                               fread(&frame_number,sizeof(int),1,Ultrasonix_file);
+                               fread(ptrRF,sizeof(short),frame_size,Ultrasonix_file);
+                               ptrRF += frame_size;
+                       }
+                       fclose(Ultrasonix_file);  
+
+                       im = crea::NewVtkImageDataFromRaw( dataRF, h.width, h.height, h.frame);
+               break;
+
+               case TYPE_B8:
+                       dataB8 = (char*)malloc(sizeof(char)*im_size);
+                       ptrB8 = dataB8;
+                       for (int k=0; k<h.frame; k++)
+                       {
+                               fread(ptrB8,sizeof(char),frame_size,Ultrasonix_file);
+                               ptrB8 += frame_size;
+                       }
+                       // in mode b frames width and height are inverted
+                       temp = h.width;
+                       h.width = h.height;
+                       h.height = temp;
+                       
+                       fclose(Ultrasonix_file);  
+
+                       im = crea::NewVtkImageDataFromRaw( dataB8, h.width, h.height, h.frame);
+               break;
+
+               case TYPE_B32:
+                       dataB32 = (int*)malloc(sizeof(int)*im_size);
+                       ptrB32 = dataB32;
+                       for (int k=0; k<h.frame; k++)
+                       {
+                               fread(ptrB32,sizeof(int),frame_size,Ultrasonix_file);
+                               ptrB32 += frame_size;
+                       }
+                       // in B mode frames width and height are inverted
+                       temp = h.width;
+                       h.width = h.height;
+                       h.height = temp;
+
+                       fclose(Ultrasonix_file);  
+
+                       im = crea::NewVtkImageDataFromRaw( dataB32, h.width, h.height, h.frame);
+               break;
+        }
+
+    return im;
+}
+  //=====================================================================
+  
+  //=====================================================================
+  void UltrasonixImageReader::PushBackExtensions(std::vector<std::string>& v)
+  {
+    v.push_back("Ultrasonix");
+  }
+  //=====================================================================
+
+
+  //=====================================================================
+  void UltrasonixImageReader::ReadAttributes(const std::string& filename, 
+                                     std::map<std::string,std::string>& attr)
+  {
+    //      std::cout << "UltrasonixImageReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
+    GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
+
+    FILE *Ultrasonix_file=fopen(filename.c_str(),"rb");
+    if (!Ultrasonix_file) return;
+    Ultrasonix_header h;
+    if (!ReadHeader(Ultrasonix_file,h)) 
+      {
+        fclose(Ultrasonix_file);  
+       return;
+      }
+    fclose(Ultrasonix_file);  
+  
+    // Columns
+    char cols[128];
+    sprintf(cols,"%i",h.width);
+    // Rows
+    char rows[128];
+    sprintf(rows,"%i",h.height);
+    // Planes 
+    char planes[128];
+    sprintf(planes,"%i",h.frame);
+    
+    
+    // 
+    std::map<std::string,std::string>::iterator i;
+    if ( (i = attr.find("FullFileName")) != attr.end())
+      {
+       //        boost::filesystem::path full_path(filename);
+       // std::string f = full_path.leaf();
+       i->second = filename;
+      }
+    if ( (i = attr.find("D0004_1500")) != attr.end())
+      {
+       boost::filesystem::path full_path(filename);
+       std::string f = full_path.leaf();
+       i->second = f;
+      }
+    if ( (i = attr.find("D0028_0010")) != attr.end())
+      {
+       i->second = rows;
+      }
+    if ( (i = attr.find("D0028_0011")) != attr.end())
+      {
+       i->second = cols;
+      }
+    
+    if ( (i = attr.find("D0028_0012")) != attr.end())
+      {
+       i->second = planes;
+      }
+    
+   GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
+  }
+  //=====================================================================
+
+} // namespace creaImageIO
diff --git a/src2/creaImageIOUltrasonixImageReader.h b/src2/creaImageIOUltrasonixImageReader.h
new file mode 100644 (file)
index 0000000..29a58a2
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef __creaImageIOUltrasonixImageReader_h_INCLUDED__
+#define __creaImageIOUltrasonixImageReader_h_INCLUDED__
+
+
+#include <creaImageIOAbstractImageReader.h>
+#include <creaImageIOSystem.h>
+
+namespace creaImageIO
+{
+
+
+  /**
+   * \ingroup IO
+   */
+  
+  //=====================================================================
+  /// Concrete image reader for ultrasonix 'rf' files
+  class CREAIMAGEIO_EXPORT UltrasonixImageReader : virtual public AbstractImageReader
+  {
+  public:
+    UltrasonixImageReader();
+
+    virtual ~UltrasonixImageReader();
+
+    virtual void PushBackExtensions(std::vector<std::string>&);
+    virtual bool CanRead(const std::string& filename);
+    virtual vtkImageData* ReadImage(const std::string& filename);
+    virtual void ReadAttributes(const std::string& filename, 
+                               tree::AttributeMapType& attr);
+
+  };
+  //=====================================================================
+
+
+
+} // namespace creaImageIO
+
+
+
+#endif // #ifndef __creaImageIOUltrasonixImageReader_h_INCLUDED__