Imagine++
Analyze.h
1// ===========================================================================
2// Imagine++ Libraries
3// Copyright (C) Imagine
4// For detailed information: http://imagine.enpc.fr/software
5// ===========================================================================
6
7#include "AnalyzeHeader.h"
8#include "Buffer.h"
9
10namespace Imagine {
13
15 // Analyze type constants
16#ifndef DOXYGEN_SHOULD_SKIP_THIS
17 template <typename T> inline short type_Analyze() { return DT_UNKNOWN; }
18 template <> inline short type_Analyze<unsigned char>() { return DT_UNSIGNED_CHAR; }
19 template <> inline short type_Analyze<short>() { return DT_SIGNED_SHORT; }
20 template <> inline short type_Analyze<int>() { return DT_SIGNED_INT; }
21 template <> inline short type_Analyze<float>() { return DT_FLOAT; }
22 template <> inline short type_Analyze<double>() { return DT_DOUBLE; }
23#endif
24
35 template <typename T, int dim>
36 inline bool loadAnalyze(Image<T,dim> &I, const std::string name) {
37
38 // Remove extension from filename if any
39 std::string noextension(name);
40 std::string::size_type size = noextension.rfind('.');
41 if (size != std::string::npos) noextension.resize(size);
42
43 // Build header and data filenames and open files
44 std::string dataname = noextension + ".img";
45 std::string headername = noextension + ".hdr";
46 std::ifstream headerfile(headername.c_str(),std::ios::binary);
47 if (!headerfile.is_open()) {
48 std::cerr << "Unable to open '" << headername << "'" << std::endl;
49 return false;
50 }
51 std::ifstream datafile(dataname.c_str(),std::ios::binary);
52 if (!datafile.is_open()) {
53 std::cerr << "Unable to open '" << dataname << "'" << std::endl;
54 return false;
55 }
56
57 // Read header
58 Analyze::dsr header;
59 headerfile.read((char *)(&header),sizeof(Analyze::dsr));
60 headerfile.close();
61
62 // Read image dimensions and setSize image
63 int ndim = header.dime.dim[0];
64 if (ndim < dim) {
65 std::cerr << "Dimension mismatch" << std::endl;
66 return false;
67 }
68 Coords<dim> dm;
69 for (int i=0;i<dim;i++) dm[i] = header.dime.dim[i+1];
70 I.setSize(dm);
71
72 // Read data
73 switch (header.dime.datatype) {
74 case DT_UNSIGNED_CHAR : return readBuffer<unsigned char,T,dim>(I,datafile);
75 case DT_SIGNED_SHORT : return readBuffer<short int,T,dim>(I,datafile);
76 case DT_SIGNED_INT : return readBuffer<int,T,dim>(I,datafile);
77 case DT_FLOAT : return readBuffer<float,T,dim>(I,datafile);
78 case DT_DOUBLE : return readBuffer<double,T,dim>(I,datafile);
79 }
80
81 std::cerr << "Unkwnown data type" << std::endl;
82 return false;
83 }
84
85
98 template <typename TO, typename TI, int dim>
99 inline bool saveAnalyze(const Image<TI,dim> &I, const std::string name) {
100 short type = type_Analyze<TO>();
101 if (type == DT_UNKNOWN) {
102 std::cerr << "Data not handled by the Analyze format" << std::endl;
103 return false;
104 }
105
106 // Remove extension from filename if any
107 std::string noextension(name);
108 std::string::size_type size = noextension.rfind('.');
109 if (size != std::string::npos) noextension.resize(size);
110
112 std::string dataname = noextension + ".img";
113 std::string headername = noextension + ".hdr";
114 std::ofstream headerfile(headername.c_str(),std::ios::binary);
115 if (!headerfile.is_open()) {
116 std::cerr << "Unable to open '" << headername << "'" << std::endl;
117 return false;
118 }
119 std::ofstream datafile(dataname.c_str(),std::ios::binary);
120 if (!datafile.is_open()) {
121 std::cerr << "Unable to open '" << dataname << "'" << std::endl;
122 return false;
123 }
124
125 // Fill and write header
126 Analyze::dsr header;
127 memset(&header,0,sizeof(Analyze::dsr));
128 header.hk.sizeof_hdr = sizeof(Analyze::dsr);
129 header.hk.regular = 'r';
130 header.dime.dim[0] = dim;
131 for (int i=0;i<dim;i++) header.dime.dim[i+1] = I.size(i);
132 header.dime.datatype = type;
133 header.dime.pixdim[1] = 1.f;
134 header.dime.pixdim[2] = 1.f;
135 header.dime.pixdim[3] = 1.f;
136 headerfile.write((const char *)(&header),sizeof(Analyze::dsr));
137 headerfile.close();
138
139 // Write the data buffer
140 return writeBuffer<TO,TI,dim>(I,datafile);
141 }
142
144}
Coordinates.
Definition: Coords.h:16
Image.
Definition: Image.h:24
void setSize(const Coords< dim > &sz)
Change sizes.
Definition: MultiArray.h:146
int size(int i) const
ith size.
Definition: MultiArray.h:225
bool saveAnalyze(const Image< TI, dim > &I, const std::string name)
Save Analyze file.
Definition: Analyze.h:99
bool loadAnalyze(Image< T, dim > &I, const std::string name)
Load Analyze file.
Definition: Analyze.h:36
Imagine++ namespace.