1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#define G4cout std::cout
#define G4cerr std::cerr
#define G4endl std::endl
#if defined(_MSC_VER) || defined(__MINGW32__)
#define G4strcasecmp _stricmp
#else
#define G4strcasecmp strcasecmp
#endif
enum EStatus {
eStatus_OK = 0,
eStatus_GeneralError = 1,
eStatus_FileNotFound = 2,
eStatus_InvalidValue = 3
};
enum EDataType {
eDataType_Unknown = -1,
eDataType_Float = 0,
eDataType_UInt
};
enum EDataByteOrder {
eDataByteOrder_Unknown = -1,
eDataByteOrder_BigEndian = 0,
eDataByteOrder_LittleEndian
};
void trim_in_place(std::string& str) {
size_t pos_from = str.find_first_not_of(' ');
if(str.npos == pos_from) {
str.clear();
} else {
size_t pos_to = str.find_last_not_of(' ');
str = str.substr(pos_from, pos_to - pos_from + 1);
}
}
bool parseValue(const std::string& value, EDataType& dataType) {
bool ok = true;
dataType = eDataType_Unknown;
if ( (G4strcasecmp(value.c_str(),"float")==0) )
dataType = eDataType_Float;
else if ( (G4strcasecmp(value.c_str(),"unsigned integer")==0) )
dataType = eDataType_UInt;
else
ok = false;
return ok;
}
const char* getName(EDataType dataType) {
const char* name = "UNKNOWN";
switch(dataType) {
case eDataType_Float: name = "FLOAT" ; break;
case eDataType_UInt : name = "UNSIGNED INTEGER"; break;
default: { /* already set */ }
}
return name;
}
std::ostream& operator<<(std::ostream& os, EDataType dataType) {
os << getName(dataType);
return os;
}
bool parseValue(const std::string& value, EDataByteOrder& dataByteOrder) {
bool ok = true;
dataByteOrder = eDataByteOrder_Unknown;
if ( G4strcasecmp(value.c_str(),"BIGENDIAN") == 0 )
dataByteOrder = eDataByteOrder_BigEndian;
else if ( G4strcasecmp(value.c_str(),"LITTLEENDIAN") == 0)
dataByteOrder = eDataByteOrder_LittleEndian;
else
ok = false;
return ok;
}
const char* getName(EDataByteOrder dataByteOrder) {
const char* name = "UNKNOWN";
switch(dataByteOrder) {
case eDataByteOrder_BigEndian : name = "BIGENDIAN" ; break;
case eDataByteOrder_LittleEndian: name = "LITTLEENDIAN"; break;
default: { /* already set */ }
}
return name;
}
std::ostream& operator<<(std::ostream& os, EDataByteOrder dataByteOrder) {
os << getName(dataByteOrder);
return os;
}
template <typename T>
bool parseValue(std::string value, T& t) {
trim_in_place(value);
std::istringstream ss(value);
ss >> t;
if (!ss.eof()) {
t = T();
return false;
}
return true;
}
bool parseValue(const std::string& value, std::string& str) {
str = value;
return !str.empty();
}
class Interfile_Header {
private:
int m_image;
int m_dim[2];
int m_numPlanes;
float m_pixelSize[2];
float m_planeThickness;
std::string m_dataFileName;
EDataType m_dataType;
EDataByteOrder m_dataByteOrder;
public:
Interfile_Header() : m_image(0), m_numPlanes(0), m_planeThickness(0) {
m_dim[0] = 0; m_pixelSize[0] = 0;
m_dim[1] = 0; m_pixelSize[1] = 0;
}
int load_from_file(const char* file_path);
void dump(std::ostream& os);
};
int main(int argc, char* argv[]) {
if(2 != argc) {
G4cerr << "usage: test <file path>" << G4endl;
return 0;
}
int status = eStatus_OK;
char* file_path = argv[1];
Interfile_Header header;
status = header.load_from_file(file_path);
if(eStatus_OK == status) {
header.dump(G4cout);
}
return status;
}
void warn_bad_value(const std::string& key, const std::string& value) {
G4cerr << "warning : Invalid value for \'" << key << "\' : \'" << value << "\'"
<< G4endl;
}
void warn_unhandled(const std::string& key, const std::string& value) {
G4cerr << "warning : Unhandled key-value-pair : \'" << key << "\' = \'" << value
<< "\'" << G4endl;
}
void report_error(const std::string& key, const std::string& value, int status) {
G4cerr << "error " << status << " : while processing key-value-pair: \'" << key
<< "\' = \'" << value << "\'" << G4endl;
}
template<typename TValue>
bool extract_value(const std::string& key, const std::string& value,
const std::string& match, TValue& output, int& status) {
bool ok = false;
if ( key == match ) {
ok = parseValue(value, output);
if (!ok) {
status = eStatus_InvalidValue;
warn_bad_value(key, match);
}
}
return ok;
}
bool strip_comment(std::string& str) {
size_t pos = str.find(";");
if(pos != str.npos) {
str = str.substr(0, pos);
trim_in_place(str);
return true;
}
return false;
}
bool split_key_value_pair(const std::string& str, std::string& key, std::string& value) {
size_t pos_from = str.find_first_not_of("! \n");
if(pos_from != str.npos) {
size_t pos_to = str.find(":=", pos_from);
if(pos_to != str.npos) {
key = str.substr(pos_from, pos_to - pos_from);
value = str.substr(pos_to + 2);
trim_in_place(key);
trim_in_place(value);
return true;
}
}
return false;
}
int Interfile_Header::load_from_file(const char* file_path) {
std::ifstream ifs(file_path);
if (!ifs.is_open()) {
G4cerr << G4endl << "Error: Could not open header file '" << file_path << "'!" << G4endl;
return eStatus_FileNotFound;
}
int status = eStatus_OK;
std::string line;
while((eStatus_OK == status) && std::getline(ifs, line)) {
strip_comment(line);
if(line.empty())
continue;
std::string key;
std::string value;
split_key_value_pair(line, key, value);
if(value.empty())
continue;
#ifdef _DEBUG
G4cerr << "key = " << key << G4endl
<< "value = " << value << G4endl;
#endif
if ( !extract_value(key, value, "total number of images" , m_image , status)
&& !extract_value(key, value, "matrix size [1]" , m_dim[0] , status)
&& !extract_value(key, value, "matrix size [2]" , m_dim[1] , status)
&& !extract_value(key, value, "number of slices" , m_numPlanes , status)
&& !extract_value(key, value, "scaling factor (mm/pixel) [1]", m_pixelSize[0], status)
&& !extract_value(key, value, "scaling factor (mm/pixel) [2]", m_pixelSize[1], status)
&& !extract_value(key, value, "slice thickness (mm/pixel)", m_planeThickness, status)
&& !extract_value(key, value, "name of data file" , m_dataFileName , status)
&& !extract_value(key, value, "number format" , m_dataType , status)
&& !extract_value(key, value, "imagedata byte order" , m_dataByteOrder, status) ) {
if(eStatus_OK == status) {
warn_unhandled(key, value);
} else {
report_error(key, value, status);
}
}
#ifdef _DEBUG
G4cerr << G4endl;
#endif
} // while((eStatus_OK == status) && std::getline(ifs, line)) {
if(eStatus_OK == status) {
if (m_numPlanes == 0) {
m_numPlanes = m_image;
}
}
return status;
}
void Interfile_Header::dump(std::ostream& os) {
os << "m_image = " << m_image << G4endl
<< "m_dim[0] = " << m_dim[0] << G4endl
<< "m_dim[1] = " << m_dim[0] << G4endl
<< "m_numPlanes = " << m_numPlanes << G4endl
<< std::scientific << std::setprecision(3) << std::showpos
<< "m_pixelSize[0] = " << m_pixelSize[0] << G4endl
<< "m_pixelSize[1] = " << m_pixelSize[1] << G4endl
<< std::fixed << std::setprecision(1) << std::noshowpos
<< "m_planeThickness = " << m_planeThickness << G4endl
<< "m_dataFileName = " << m_dataFileName << G4endl
<< "m_dataTypeName = " << m_dataType << G4endl
<< "m_dataByteOrder = " << m_dataByteOrder << G4endl;
}
|