]> Creatis software - clitk.git/blobdiff - common/clitkGateAsciiImageIO.cxx
GateAsciiImageIO is now cross-platform using itksys::RegularExpression
[clitk.git] / common / clitkGateAsciiImageIO.cxx
index af42c058241ec8e10c3e53788e8256b09a2c687d..dd47ef46b61d6b670eead939d387869c41a2273f 100644 (file)
@@ -17,7 +17,6 @@
 ===========================================================================**/
 
 // std include
-#include <regex.h>
 #include <cstdio>
 #include <sstream>
 #include <iostream>
@@ -72,7 +71,6 @@ void clitk::GateAsciiImageIO::ReadImageInformation()
     }
     assert(real_length == header.nb_value);
 
-
     // Set image information
     SetNumberOfDimensions(2);
     SetDimensions(0, real_length);
@@ -91,7 +89,7 @@ bool clitk::GateAsciiImageIO::CanReadFile(const char* FileNameToRead)
 
     { // check extension
        std::string filenameext = GetExtension(filename);
-       if (filenameext != std::string("txt")) return false;
+       if (filenameext != "txt") return false;
     }
 
     { // check header
@@ -117,7 +115,7 @@ bool clitk::GateAsciiImageIO::ReadLine(FILE* handle, std::string& line)
        if (ferror(handle)) return false;
        if (fread(&item,1,1,handle) != 1) return false;
 
-       if (item=='\n' or feof(handle)) {
+       if (item=='\n' || feof(handle)) {
            line = stream.str();
            return true;
        }
@@ -126,11 +124,6 @@ bool clitk::GateAsciiImageIO::ReadLine(FILE* handle, std::string& line)
     }
 }
 
-std::string ExtractMatch(const std::string& base, const regmatch_t& match) 
-{
-    return base.substr(match.rm_so,match.rm_eo-match.rm_so);
-}
-
 template <typename T>
 T ConvertFromString(const std::string& value)
 {
@@ -141,63 +134,53 @@ T ConvertFromString(const std::string& value)
     return converted;
 }
 
+bool
+clitk::GateAsciiImageIO::FindRegularExpressionNextLine(itksys::RegularExpression &reg, std::string &s, FILE* handle)
+{
+  std::string line;
+  if(!ReadLine(handle,line))  return false;
+  if(!reg.compile(s.c_str())) return false;
+  return reg.find(line.c_str());
+}
+
 //--------------------------------------------------------------------
 // Read Image Header
 bool clitk::GateAsciiImageIO::ReadHeader(FILE* handle, GateAsciiHeader& header)
 {
     assert(handle);
-    std::string line;
-
-    regex_t re_comment;
-    regex_t re_matrix_size;
-    regex_t re_resol;
-    regex_t re_voxel_size;
-    regex_t re_nb_value;
-    regmatch_t matches[4];
-
-    { // build regex
-       int ret = 0;
-       ret = regcomp(&re_comment,"^#.+$",REG_EXTENDED|REG_NEWLINE);
-       assert(ret == 0);
-       ret = regcomp(&re_matrix_size,"^# +Matrix *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",REG_EXTENDED|REG_NEWLINE);
-       assert(ret == 0);
-       ret = regcomp(&re_resol,"^# +Resol *= +\\(([0-9]+),([0-9]+),([0-9]+)\\)$",REG_EXTENDED|REG_NEWLINE);
-       assert(ret == 0);
-       ret = regcomp(&re_voxel_size,"^# +Voxel *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",REG_EXTENDED|REG_NEWLINE);
-       assert(ret == 0);
-       ret = regcomp(&re_nb_value,"^# +nbVal *= +([0-9]+)$",REG_EXTENDED|REG_NEWLINE);
-       assert(ret == 0);
-    }
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_comment,line.c_str(),1,matches,0) == REG_NOMATCH) return false;
+  std::string regexstr[6] =
+    {"^#.+$",
+     "^# +Matrix *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",
+     "^# +Resol *= +\\(([0-9]+),([0-9]+),([0-9]+)\\)$",
+     "^# +Voxel *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",
+     "^# +nbVal *= +([0-9]+)$"};
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_matrix_size,line.c_str(),4,matches,0) == REG_NOMATCH) return false;
-    header.matrix_size[0] = ConvertFromString<double>(ExtractMatch(line,matches[1]));
-    header.matrix_size[1] = ConvertFromString<double>(ExtractMatch(line,matches[2]));
-    header.matrix_size[2] = ConvertFromString<double>(ExtractMatch(line,matches[3]));
+  itksys::RegularExpression regex;
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_resol,line.c_str(),4,matches,0) == REG_NOMATCH) return false;
-    header.resolution[0] = ConvertFromString<int>(ExtractMatch(line,matches[1]));
-    header.resolution[1] = ConvertFromString<int>(ExtractMatch(line,matches[2]));
-    header.resolution[2] = ConvertFromString<int>(ExtractMatch(line,matches[3]));
+  if(!FindRegularExpressionNextLine(regex, regexstr[0], handle)) return false;
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_voxel_size,line.c_str(),4,matches,0) == REG_NOMATCH) return false;
-    header.voxel_size[0] = ConvertFromString<double>(ExtractMatch(line,matches[1]));
-    header.voxel_size[1] = ConvertFromString<double>(ExtractMatch(line,matches[2]));
-    header.voxel_size[2] = ConvertFromString<double>(ExtractMatch(line,matches[3]));
+  if(!FindRegularExpressionNextLine(regex, regexstr[1], handle)) return false;
+  header.matrix_size[0] = ConvertFromString<double>(regex.match(1));
+  header.matrix_size[1] = ConvertFromString<double>(regex.match(2));
+  header.matrix_size[2] = ConvertFromString<double>(regex.match(3));
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_nb_value,line.c_str(),2,matches,0) == REG_NOMATCH) return false;
-    header.nb_value = ConvertFromString<int>(ExtractMatch(line,matches[1]));
+  if(!FindRegularExpressionNextLine(regex, regexstr[2], handle)) return false;
+  header.resolution[0] = ConvertFromString<int>(regex.match(1));
+  header.resolution[1] = ConvertFromString<int>(regex.match(2));
+  header.resolution[2] = ConvertFromString<int>(regex.match(3));
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_comment,line.c_str(),1,matches,0) == REG_NOMATCH) return false;
+  if(!FindRegularExpressionNextLine(regex, regexstr[3], handle)) return false;
+  header.voxel_size[0] = ConvertFromString<double>(regex.match(1));
+  header.voxel_size[1] = ConvertFromString<double>(regex.match(2));
+  header.voxel_size[2] = ConvertFromString<double>(regex.match(3));
 
-    return true;
+  if(!FindRegularExpressionNextLine(regex, regexstr[4], handle)) return false;
+  header.nb_value = ConvertFromString<int>(regex.match(1));
+
+  if(!FindRegularExpressionNextLine(regex, regexstr[0], handle)) return false;
+
+  return true;
 }
 
 //--------------------------------------------------------------------
@@ -236,11 +219,44 @@ void clitk::GateAsciiImageIO::Read(void* abstract_buffer)
 //--------------------------------------------------------------------
 bool clitk::GateAsciiImageIO::CanWriteFile(const char* FileNameToWrite)
 {
-  return false;
+    if (GetExtension(std::string(FileNameToWrite)) != "txt") return false;
+    return true;
+}
+
+void clitk::GateAsciiImageIO::WriteImageInformation()
+{
+    cout << GetNumberOfDimensions() << endl;
+}
+
+bool clitk::GateAsciiImageIO::SupportsDimension(unsigned long dim)
+{
+    if (dim==2) return true;
+    return false;
 }
 
 //--------------------------------------------------------------------
 // Write Image
-void clitk::GateAsciiImageIO::Write(const void* buffer)
+void clitk::GateAsciiImageIO::Write(const void* abstract_buffer)
 {
+    const unsigned long nb_value = GetDimensions(0)*GetDimensions(1);
+    std::stringstream stream;
+    stream << "######################" << endl;
+    stream << "# Matrix Size= (" << GetSpacing(0)*GetDimensions(0) << "," << GetSpacing(1)*GetDimensions(1) << ",1)" << endl;
+    stream << "# Resol      = (" << GetDimensions(0) << "," << GetDimensions(1) << ",1)" << endl;
+    stream << "# VoxelSize  = (" << GetSpacing(0) << "," << GetSpacing(1) << ",1)" << endl;
+    stream << "# nbVal      = " << nb_value << endl;
+    stream << "######################" << endl;
+
+    const double* buffer = static_cast<const double*>(abstract_buffer);
+    for (unsigned long kk=0; kk<nb_value; kk++) { stream << buffer[kk] << endl; }
+
+    FILE* handle = fopen(m_FileName.c_str(),"w");
+    if (!handle) {
+       itkGenericExceptionMacro(<< "Could not open file (for writing): " << m_FileName);
+       return;
+    }
+
+    fwrite(stream.str().c_str(),1,stream.str().size(),handle);
+
+    fclose(handle);
 }