+ std::string filename(FileNameToRead);
+
+ { // check extension
+ std::string filenameext = GetExtension(filename);
+ if (filenameext != "txt") return false;
+ }
+
+ { // check header
+ FILE* handle = fopen(filename.c_str(),"r");
+ if (!handle) return false;
+
+ GateAsciiHeader header;
+ if (!ReadHeader(handle,header)) { fclose(handle); return false; }
+ fclose(handle);
+ }
+
+ return true;
+}
+
+//--------------------------------------------------------------------
+// Read Line in file
+bool clitk::GateAsciiImageIO::ReadLine(FILE* handle, std::string& line)
+{
+ std::stringstream stream;
+ while (true)
+ {
+ char item;
+ if (ferror(handle)) return false;
+ if (fread(&item,1,1,handle) != 1) return false;
+
+ if (item=='\n' || feof(handle)) {
+ line = stream.str();
+ return true;
+ }
+
+ stream << item;
+ }
+}
+
+template <typename T>
+T ConvertFromString(const std::string& value)
+{
+ std::stringstream stream;
+ stream << value;
+ T converted;
+ stream >> converted;
+ return converted;
+}
+
+bool
+clitk::GateAsciiImageIO::FindRegularExpressionNextLine(itksys::RegularExpression ®, 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 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]+)$"};
+
+ itksys::RegularExpression regex;
+
+ if(!FindRegularExpressionNextLine(regex, regexstr[0], handle)) return false;
+
+ 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(!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(!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));
+
+ if(!FindRegularExpressionNextLine(regex, regexstr[4], handle)) return false;
+ header.nb_value = ConvertFromString<int>(regex.match(1));
+
+ if(!FindRegularExpressionNextLine(regex, regexstr[0], handle)) return false;
+