]> Creatis software - clitk.git/blobdiff - common/clitkGateAsciiImageIO.cxx
Remove warnings
[clitk.git] / common / clitkGateAsciiImageIO.cxx
index af42c058241ec8e10c3e53788e8256b09a2c687d..744f413f55e7dcc800e3b7a6676b97ef3da9026c 100644 (file)
@@ -72,7 +72,6 @@ void clitk::GateAsciiImageIO::ReadImageInformation()
     }
     assert(real_length == header.nb_value);
 
-
     // Set image information
     SetNumberOfDimensions(2);
     SetDimensions(0, real_length);
@@ -91,7 +90,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
@@ -156,17 +155,11 @@ bool clitk::GateAsciiImageIO::ReadHeader(FILE* handle, GateAsciiHeader& header)
     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);
+       assert(regcomp(&re_comment,"^#.+$",REG_EXTENDED|REG_NEWLINE) == 0);
+       assert(regcomp(&re_matrix_size,"^# +Matrix *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",REG_EXTENDED|REG_NEWLINE) == 0);
+       assert(regcomp(&re_resol,"^# +Resol *= +\\(([0-9]+),([0-9]+),([0-9]+)\\)$",REG_EXTENDED|REG_NEWLINE) == 0);
+       assert(regcomp(&re_voxel_size,"^# +Voxel *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",REG_EXTENDED|REG_NEWLINE) == 0);
+       assert(regcomp(&re_nb_value,"^# +nbVal *= +([0-9]+)$",REG_EXTENDED|REG_NEWLINE) == 0);
     }
 
     if (!ReadLine(handle,line)) return false;
@@ -236,11 +229,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);
 }