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 ===========================================================================**/
27 #include "clitkGateAsciiImageIO.h"
28 #include "clitkCommon.h"
31 #include <itkMetaDataObject.h>
33 std::ostream& operator<<(std::ostream& os, const clitk::GateAsciiImageIO::GateAsciiHeader& header)
35 os << "matrix_size=[" << header.matrix_size[0] << "," << header.matrix_size[1] << "," << header.matrix_size[2] << "]" << endl;
36 os << "resolution=[" << header.resolution[0] << "," << header.resolution[1] << "," << header.resolution[2] << "]" << endl;
37 os << "voxel_size=[" << header.voxel_size[0] << "," << header.voxel_size[1] << "," << header.voxel_size[2] << "]" << endl;
38 os << "nb_value=" << header.nb_value << endl;
42 //--------------------------------------------------------------------
43 // Read Image Information
44 void clitk::GateAsciiImageIO::ReadImageInformation()
46 FILE* handle = fopen(m_FileName.c_str(),"r");
48 itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
52 GateAsciiHeader header;
53 if (!ReadHeader(handle,header)) {
54 itkGenericExceptionMacro(<< "Could not read header: " << m_FileName);
61 double real_spacing = 0;
62 for (int kk=0; kk<3; kk++) {
63 if (header.resolution[kk]>1) {
65 itkGenericExceptionMacro(<< "Could not image dimension: " << m_FileName);
68 real_length = header.resolution[kk];
69 real_spacing = header.voxel_size[kk];
72 assert(real_length == header.nb_value);
74 // Set image information
75 SetNumberOfDimensions(2);
76 SetDimensions(0, real_length);
78 SetSpacing(0, real_spacing);
82 SetComponentType(itk::ImageIOBase::DOUBLE);
85 //--------------------------------------------------------------------
86 bool clitk::GateAsciiImageIO::CanReadFile(const char* FileNameToRead)
88 std::string filename(FileNameToRead);
91 std::string filenameext = GetExtension(filename);
92 if (filenameext != "txt") return false;
96 FILE* handle = fopen(filename.c_str(),"r");
97 if (!handle) return false;
99 GateAsciiHeader header;
100 if (!ReadHeader(handle,header)) { fclose(handle); return false; }
107 //--------------------------------------------------------------------
109 bool clitk::GateAsciiImageIO::ReadLine(FILE* handle, std::string& line)
111 std::stringstream stream;
115 if (ferror(handle)) return false;
116 if (fread(&item,1,1,handle) != 1) return false;
118 if (item=='\n' || feof(handle)) {
127 template <typename T>
128 T ConvertFromString(const std::string& value)
130 std::stringstream stream;
138 clitk::GateAsciiImageIO::FindRegularExpressionNextLine(itksys::RegularExpression ®, std::string &s, FILE* handle)
141 if(!ReadLine(handle,line)) return false;
142 if(!reg.compile(s.c_str())) return false;
143 return reg.find(line.c_str());
146 //--------------------------------------------------------------------
148 bool clitk::GateAsciiImageIO::ReadHeader(FILE* handle, GateAsciiHeader& header)
152 std::string regexstr[6] =
154 "^# +Matrix *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",
155 "^# +Resol *= +\\(([0-9]+),([0-9]+),([0-9]+)\\)$",
156 "^# +Voxel *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",
157 "^# +nbVal *= +([0-9]+)$"};
159 itksys::RegularExpression regex;
161 if(!FindRegularExpressionNextLine(regex, regexstr[0], handle)) return false;
163 if(!FindRegularExpressionNextLine(regex, regexstr[1], handle)) return false;
164 header.matrix_size[0] = ConvertFromString<double>(regex.match(1));
165 header.matrix_size[1] = ConvertFromString<double>(regex.match(2));
166 header.matrix_size[2] = ConvertFromString<double>(regex.match(3));
168 if(!FindRegularExpressionNextLine(regex, regexstr[2], handle)) return false;
169 header.resolution[0] = ConvertFromString<int>(regex.match(1));
170 header.resolution[1] = ConvertFromString<int>(regex.match(2));
171 header.resolution[2] = ConvertFromString<int>(regex.match(3));
173 if(!FindRegularExpressionNextLine(regex, regexstr[3], handle)) return false;
174 header.voxel_size[0] = ConvertFromString<double>(regex.match(1));
175 header.voxel_size[1] = ConvertFromString<double>(regex.match(2));
176 header.voxel_size[2] = ConvertFromString<double>(regex.match(3));
178 if(!FindRegularExpressionNextLine(regex, regexstr[4], handle)) return false;
179 header.nb_value = ConvertFromString<int>(regex.match(1));
181 if(!FindRegularExpressionNextLine(regex, regexstr[0], handle)) return false;
186 //--------------------------------------------------------------------
187 // Read Image Content
188 void clitk::GateAsciiImageIO::Read(void* abstract_buffer)
190 FILE* handle = fopen(m_FileName.c_str(),"r");
192 itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
196 GateAsciiHeader header;
197 if (!ReadHeader(handle,header)) {
198 itkGenericExceptionMacro(<< "Could not read header: " << m_FileName);
204 double* buffer = static_cast<double*>(abstract_buffer);
208 if (!ReadLine(handle,line)) break;
209 *buffer = ConvertFromString<double>(line);
213 assert(read_count == header.nb_value);
219 //--------------------------------------------------------------------
220 bool clitk::GateAsciiImageIO::CanWriteFile(const char* FileNameToWrite)
222 if (GetExtension(std::string(FileNameToWrite)) != "txt") return false;
226 void clitk::GateAsciiImageIO::WriteImageInformation()
228 cout << GetNumberOfDimensions() << endl;
231 bool clitk::GateAsciiImageIO::SupportsDimension(unsigned long dim)
233 if (dim==2) return true;
237 //--------------------------------------------------------------------
239 void clitk::GateAsciiImageIO::Write(const void* abstract_buffer)
241 const unsigned long nb_value = GetDimensions(0)*GetDimensions(1);
242 std::stringstream stream;
243 stream << "######################" << endl;
244 stream << "# Matrix Size= (" << GetSpacing(0)*GetDimensions(0) << "," << GetSpacing(1)*GetDimensions(1) << ",1)" << endl;
245 stream << "# Resol = (" << GetDimensions(0) << "," << GetDimensions(1) << ",1)" << endl;
246 stream << "# VoxelSize = (" << GetSpacing(0) << "," << GetSpacing(1) << ",1)" << endl;
247 stream << "# nbVal = " << nb_value << endl;
248 stream << "######################" << endl;
250 const double* buffer = static_cast<const double*>(abstract_buffer);
251 for (unsigned long kk=0; kk<nb_value; kk++) { stream << buffer[kk] << endl; }
253 FILE* handle = fopen(m_FileName.c_str(),"w");
255 itkGenericExceptionMacro(<< "Could not open file (for writing): " << m_FileName);
259 fwrite(stream.str().c_str(),1,stream.str().size(),handle);