X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src2%2FcreaImageIOUltrasonixImageReader.cpp;h=a8905ccb4f033afe05b4b1ee8f7bd836e11634b2;hb=da80530068246081505e9649523d10e8da8e6acd;hp=014e0953c7a6be97215f1ff10844922d61fd61d4;hpb=96ca5a0d7b7ccd6b4291160c19afd48e3207db2a;p=creaImageIO.git diff --git a/src2/creaImageIOUltrasonixImageReader.cpp b/src2/creaImageIOUltrasonixImageReader.cpp index 014e095..a8905cc 100644 --- a/src2/creaImageIOUltrasonixImageReader.cpp +++ b/src2/creaImageIOUltrasonixImageReader.cpp @@ -1,15 +1,15 @@ -#include -#include +#include "creaImageIOSystem.h" +#include "creaImageIOUltrasonixImageReader.h" #include -#include "boost/filesystem/path.hpp" +#include namespace creaImageIO { #define HEADER_SIZE 19 #define TYPE_RF 16 #define TYPE_B8 4 -#define TYPE_B32 8 +#define TYPE_B32 8 //===================================================================== @@ -29,7 +29,7 @@ namespace creaImageIO struct Ultrasonix_header { // frames, width, height, ultrasounds frequency, sampling rate - int type,frame,width,height,frequency,samplingRate; + int type, frame, width, height, frequency, samplingRate; }; //===================================================================== @@ -37,15 +37,18 @@ namespace creaImageIO //===================================================================== 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); + //int *header=(int*)malloc(sizeof(int)*HEADER_SIZE); + int header[HEADER_SIZE]; + fread(header, sizeof(int), HEADER_SIZE, Ultrasonix_file); + if (ferror(Ultrasonix_file)) + return false; + 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; } //===================================================================== @@ -53,27 +56,32 @@ namespace creaImageIO //===================================================================== bool UltrasonixImageReader::CanRead(const std::string& filename) { + long size; bool ok = false; - FILE *Ultrasonix_file=fopen(filename.c_str(),"rb"); - long size; + FILE *Ultrasonix_file=fopen(filename.c_str(), "rb"); 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; - + Ultrasonix_header h; + if (!ReadHeader(Ultrasonix_file, h) ) + { fclose(Ultrasonix_file); + std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl; + return false; + } + + 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; } @@ -83,82 +91,88 @@ namespace creaImageIO vtkImageData* UltrasonixImageReader::ReadImage(const std::string& filename) { FILE *Ultrasonix_file=fopen(filename.c_str(),"rb"); - if (!Ultrasonix_file) return false; + if (!Ultrasonix_file) + { + std::cout << "cannot open file [" << filename << "]" << std::endl; + return 0; + } Ultrasonix_header h; if (!ReadHeader(Ultrasonix_file,h)) { + std::cout << "cannot read Ultrasonix header for file [" << filename << "]" << std::endl; + fclose(Ultrasonix_file); + return 0; + } + + long frame_size = h.height * h.width; + long im_size = frame_size * h.frame; + + 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& v) { @@ -169,74 +183,80 @@ namespace creaImageIO //===================================================================== - void UltrasonixImageReader::ReadAttributes(const std::string& filename, + void UltrasonixImageReader::ReadAttributes(const std::string& filename, std::map& attr) - { - GimmickMessage(2,"Reading attributes from '"<::iterator i; if ( (i = attr.find("FullFileName")) != attr.end()) { - i->second = filename; + 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; + 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; + i->second = rows; } if ( (i = attr.find("D0028_0011")) != attr.end()) { - i->second = cols; + i->second = cols; } if ( (i = attr.find("D0028_0012")) != attr.end()) { - i->second = planes; + i->second = planes; + } + if ( (i = attr.find("D003a_001a")) != attr.end()) + { + i->second = samplingFrequency; + } + if ( (i = attr.find("D0018_6030")) != attr.end()) + { + i->second = transducerFrequency; } - if ( (i = attr.find("D003a_001a")) != attr.end()) - { - i->second = samplingFrequency; - } - if ( (i = attr.find("D0018_6030")) != attr.end()) - { - i->second = transducerFrequency; - } - - GimmickMessage(2,"Attributes map:"<