1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
28 #include "clitkGateAsciiImageIO.h"
29 #include "clitkCommon.h"
32 #include <itkMetaDataObject.h>
34 std::ostream& operator<<(std::ostream& os, const clitk::GateAsciiImageIO::GateAsciiHeader& header)
36 os << "matrix_size=[" << header.matrix_size[0] << "," << header.matrix_size[1] << "," << header.matrix_size[2] << "]" << endl;
37 os << "resolution=[" << header.resolution[0] << "," << header.resolution[1] << "," << header.resolution[2] << "]" << endl;
38 os << "voxel_size=[" << header.voxel_size[0] << "," << header.voxel_size[1] << "," << header.voxel_size[2] << "]" << endl;
39 os << "nb_value=" << header.nb_value << endl;
43 //--------------------------------------------------------------------
44 // Read Image Information
45 void clitk::GateAsciiImageIO::ReadImageInformation()
47 FILE* handle = fopen(m_FileName.c_str(),"r");
49 itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
53 GateAsciiHeader header;
54 if (!ReadHeader(handle,header)) {
55 itkGenericExceptionMacro(<< "Could not read header: " << m_FileName);
62 double real_spacing = 0;
63 for (int kk=0; kk<3; kk++) {
64 if (header.resolution[kk]>1) {
66 itkGenericExceptionMacro(<< "Could not image dimension: " << m_FileName);
69 real_length = header.resolution[kk];
70 real_spacing = header.voxel_size[kk];
73 assert(real_length == header.nb_value);
76 // Set image information
77 SetNumberOfDimensions(2);
78 SetDimensions(0, real_length);
80 SetSpacing(0, real_spacing);
84 SetComponentType(itk::ImageIOBase::DOUBLE);
87 //--------------------------------------------------------------------
88 bool clitk::GateAsciiImageIO::CanReadFile(const char* FileNameToRead)
90 std::string filename(FileNameToRead);
93 std::string filenameext = GetExtension(filename);
94 if (filenameext != std::string("txt")) return false;
98 FILE* handle = fopen(filename.c_str(),"r");
99 if (!handle) return false;
101 GateAsciiHeader header;
102 if (!ReadHeader(handle,header)) { fclose(handle); return false; }
109 //--------------------------------------------------------------------
111 bool clitk::GateAsciiImageIO::ReadLine(FILE* handle, std::string& line)
113 std::stringstream stream;
117 if (ferror(handle)) return false;
118 if (fread(&item,1,1,handle) != 1) return false;
120 if (item=='\n' or feof(handle)) {
129 std::string ExtractMatch(const std::string& base, const regmatch_t& match)
131 return base.substr(match.rm_so,match.rm_eo-match.rm_so);
134 template <typename T>
135 T ConvertFromString(const std::string& value)
137 std::stringstream stream;
144 //--------------------------------------------------------------------
146 bool clitk::GateAsciiImageIO::ReadHeader(FILE* handle, GateAsciiHeader& header)
152 regex_t re_matrix_size;
154 regex_t re_voxel_size;
156 regmatch_t matches[4];
160 ret = regcomp(&re_comment,"^#.+$",REG_EXTENDED|REG_NEWLINE);
162 ret = regcomp(&re_matrix_size,"^# +Matrix *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",REG_EXTENDED|REG_NEWLINE);
164 ret = regcomp(&re_resol,"^# +Resol *= +\\(([0-9]+),([0-9]+),([0-9]+)\\)$",REG_EXTENDED|REG_NEWLINE);
166 ret = regcomp(&re_voxel_size,"^# +Voxel *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",REG_EXTENDED|REG_NEWLINE);
168 ret = regcomp(&re_nb_value,"^# +nbVal *= +([0-9]+)$",REG_EXTENDED|REG_NEWLINE);
172 if (!ReadLine(handle,line)) return false;
173 if (regexec(&re_comment,line.c_str(),1,matches,0) == REG_NOMATCH) return false;
175 if (!ReadLine(handle,line)) return false;
176 if (regexec(&re_matrix_size,line.c_str(),4,matches,0) == REG_NOMATCH) return false;
177 header.matrix_size[0] = ConvertFromString<double>(ExtractMatch(line,matches[1]));
178 header.matrix_size[1] = ConvertFromString<double>(ExtractMatch(line,matches[2]));
179 header.matrix_size[2] = ConvertFromString<double>(ExtractMatch(line,matches[3]));
181 if (!ReadLine(handle,line)) return false;
182 if (regexec(&re_resol,line.c_str(),4,matches,0) == REG_NOMATCH) return false;
183 header.resolution[0] = ConvertFromString<int>(ExtractMatch(line,matches[1]));
184 header.resolution[1] = ConvertFromString<int>(ExtractMatch(line,matches[2]));
185 header.resolution[2] = ConvertFromString<int>(ExtractMatch(line,matches[3]));
187 if (!ReadLine(handle,line)) return false;
188 if (regexec(&re_voxel_size,line.c_str(),4,matches,0) == REG_NOMATCH) return false;
189 header.voxel_size[0] = ConvertFromString<double>(ExtractMatch(line,matches[1]));
190 header.voxel_size[1] = ConvertFromString<double>(ExtractMatch(line,matches[2]));
191 header.voxel_size[2] = ConvertFromString<double>(ExtractMatch(line,matches[3]));
193 if (!ReadLine(handle,line)) return false;
194 if (regexec(&re_nb_value,line.c_str(),2,matches,0) == REG_NOMATCH) return false;
195 header.nb_value = ConvertFromString<int>(ExtractMatch(line,matches[1]));
197 if (!ReadLine(handle,line)) return false;
198 if (regexec(&re_comment,line.c_str(),1,matches,0) == REG_NOMATCH) return false;
203 //--------------------------------------------------------------------
204 // Read Image Content
205 void clitk::GateAsciiImageIO::Read(void* abstract_buffer)
207 FILE* handle = fopen(m_FileName.c_str(),"r");
209 itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
213 GateAsciiHeader header;
214 if (!ReadHeader(handle,header)) {
215 itkGenericExceptionMacro(<< "Could not read header: " << m_FileName);
221 double* buffer = static_cast<double*>(abstract_buffer);
225 if (!ReadLine(handle,line)) break;
226 *buffer = ConvertFromString<double>(line);
230 assert(read_count == header.nb_value);
236 //--------------------------------------------------------------------
237 bool clitk::GateAsciiImageIO::CanWriteFile(const char* FileNameToWrite)
242 //--------------------------------------------------------------------
244 void clitk::GateAsciiImageIO::Write(const void* buffer)