| Bsoft 2.0.5 Bernard's software package

rwSER.cpp
Go to the documentation of this file.
1
9 #include <time.h>
10
11 #include "rwSER.h"
12 #include "file_util.h"
13 #include "utilities.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18 int read_field(ifstream* fimg, char* p, size_t n)
19 {
20 size_t where = fimg->tellg();
21
22 fimg->read(p, n);
23
24 if ( fimg->fail() ) {
25 cerr << "Reading failed at " << where << endl;
26 return -2;
27 }
28
29 return n;
30 }
31
32 int readSER(Bimage* p, int readdata)
33 {
34 ifstream* fimg = new ifstream(p->file_name());
35 if ( fimg->fail() ) return -1;
36
37 if ( verbose & VERB_DEBUG )
38 cout << "DEBUG readSER: File opened" << endl;
39
40 int offset, iv, nx, ny;
41 short datatype;
42 double ox, oy, ux, uy;
43
44 fimg->seekg(22);
45 read_field(fimg, (char *) &offset, sizeof(int));
46 if ( verbose & VERB_DEBUG )
47 cout << "DEBUG readSER: offset: " << offset << endl;
48
49 fimg->seekg(offset);
50 read_field(fimg, (char *) &offset, sizeof(int));
51 if ( verbose & VERB_DEBUG )
52 cout << "DEBUG readSER: offset: " << offset << endl;
53
54 fimg->seekg(offset);
55
56 read_field(fimg, (char *) &ox, sizeof(double));
57 read_field(fimg, (char *) &ux, sizeof(double));
58 read_field(fimg, (char *) &iv, sizeof(int));
59
60 read_field(fimg, (char *) &oy, sizeof(double));
61 read_field(fimg, (char *) &uy, sizeof(double));
62 read_field(fimg, (char *) &iv, sizeof(int));
63
64 read_field(fimg, (char *) &datatype, sizeof(short));
65
66 read_field(fimg, (char *) &nx, sizeof(int));
67 read_field(fimg, (char *) &ny, sizeof(int));
68
69 p->size(nx, ny, 1);
70 p->channels(1);
71 p->images(1);
72
73 p->data_offset(offset+50);
74 if ( verbose & VERB_DEBUG )
75 cout << "DEBUG readSER: data offset: " << p->data_offset() << endl;
76
77 p->sampling(ux*1e10, uy*1e10, 1);
78 // p->image->origin(-ox/ux, -oy/uy, 0);
79 p->origin(-ox/ux, -oy/uy, 0.0);
80
81 switch ( datatype ) {
82 case 1: p->data_type(UCharacter); break;
83 case 2: p->data_type(UShort); break;
84 case 3: p->data_type(UInteger); break;
85 case 4: p->data_type(SCharacter); break;
86 case 5: p->data_type(Short); break;
87 case 6: p->data_type(Integer); break;
88 case 7: p->data_type(Float); break;
89 case 8: p->data_type(Double); break;
90 case 9: p->data_type(Float); p->compound_type(TComplex); break;
91 case 10: p->data_type(Double); p->compound_type(TComplex); break;
92 }
93
94 if ( readdata ) {
95 p->data_alloc();
96 fread_large(p->data_pointer(), p->alloc_size(), p->data_offset(), fimg);
97 }
98
99 fimg->close();
100 delete fimg;
101
102 if ( verbose & VERB_DEBUG )
103 cout << "DEBUG rwSER: Data read" << endl;
104
105 return 0;
106 }
107
108 int writeSER(Bimage* p)
109 {
110 // cerr << "Writing is not supported for this format!" << endl;
111
112 if ( p->data_type() == ULong ) p->change_type(UInteger);
113 if ( p->data_type() == Long ) p->change_type(Integer);
114
115 p->data_offset(126);
116
117 short sv;
118 int iv;
119 // float fv;
120 double dv;
121 char str[8] = "Number";
122
123 ofstream fimg(p->file_name());
124 if ( fimg.fail() ) return -1;
125
126 sv = 0x4949; // ByteOrder
127 fimg.write((char *) &sv, sizeof(short));
128
129 sv = 0; // SeriesID
130 fimg.write((char *) &sv, sizeof(short));
131
132 sv = 0x0210; // SeriesVersion
133 fimg.write((char *) &sv, sizeof(short));
134
135 iv = 0x4122; // DataTypeID
136 fimg.write((char *) &iv, sizeof(int));
137
138 iv = 0x4152; // TagTypeID
139 fimg.write((char *) &iv, sizeof(int));
140
141 iv = 1; // TotalNumberElements
142 fimg.write((char *) &iv, sizeof(int));
143
144 iv = 1; // ValidNumberElements
145 fimg.write((char *) &iv, sizeof(int));
146
147 iv = 68; // OffsetArrayOffset
148 fimg.write((char *) &iv, sizeof(int));
149
150 iv = 1; // NumberDimensions
151 fimg.write((char *) &iv, sizeof(int));
152
153 iv = 1; // DimensionSize
154 fimg.write((char *) &iv, sizeof(int));
155
156 dv = 0; // CalibrationOffset
157 fimg.write((char *) &dv, sizeof(double));
158
159 dv = 0; // CalibrationDelta
160 fimg.write((char *) &dv, sizeof(double));
161
162 iv = 0; // CalibrationElement
163 fimg.write((char *) &iv, sizeof(int));
164
165 iv = 6; // DescriptionLength
166 fimg.write((char *) &iv, sizeof(int));
167 if ( iv ) fimg.write(str, iv);
168
169 iv = 0; // UnitsLength: 0
170 fimg.write((char *) &iv, sizeof(int));
171 if ( iv ) fimg.write(str, iv);
172
173 iv = 76; // DataOffset[0]: 76
174 fimg.write((char *) &iv, sizeof(int));
175
176 iv = p->data_offset() + p->alloc_size(); // TagOffset[0]: 67108990
177 fimg.write((char *) &iv, sizeof(int));
178
179 dv = -p->image->origin()[0]*p->sampling()[0]*1e-10; // CalibrationOffsetX
180 fimg.write((char *) &dv, sizeof(double));
181
182 dv = p->sampling()[0]*1e-10; // CalibrationDeltaX
183 fimg.write((char *) &dv, sizeof(double));
184
185 iv = 0; // CalibrationElementX
186 fimg.write((char *) &iv, sizeof(int));
187
188 dv = -p->image->origin()[1]*p->sampling()[1]*1e-10; // CalibrationOffsetY
189 fimg.write((char *) &dv, sizeof(double));
190
191 dv = p->sampling()[1]*1e-10; // CalibrationDeltaY
192 fimg.write((char *) &dv, sizeof(double));
193
194 iv = 0; // CalibrationElementY
195 fimg.write((char *) &iv, sizeof(int));
196
197 // DataType
198 switch ( p->data_type() ) {
199 case Bit: sv = 1; break;
200 case UCharacter: sv = 1; break;
201 case SCharacter: sv = 4; break;
202 case UShort: sv = 2; break;
203 case Short: sv = 5; break;
204 case UInteger: sv = 3; break;
205 case Integer: sv = 6; break;
206 // case ULong: sv = 3; break;
207 // case Long: sv = 4; break;
208 case Float: sv = 7; break;
209 case Double: sv = 8; break;
210 default: break;
211 }
212 if ( p->compound_type() == TComplex ) sv += 2;
213 fimg.write((char *) &sv, sizeof(short));
214
215 iv = p->sizeX(); // ArraySizeX
216 fimg.write((char *) &iv, sizeof(int));
217
218 iv = p->sizeY(); // ArraySizeY
219 fimg.write((char *) &iv, sizeof(int));
220
221 // Data
222 fimg.write((char *) p->data_pointer(), p->alloc_size());
223
224 sv = 0x4152; // TagTypeID
225 fimg.write((char *) &sv, sizeof(short));
226
227 time_t t = time(NULL); // Time
228 fimg.write((char *) &t, sizeof(time_t));
229
230 fimg.close();
231
232 return 0;
233 }
234
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
Integer
Definition: utilities.h:154
read_field
int read_field(ifstream *fimg, char *p, size_t n)
Definition: rwSER.cpp:18
SCharacter
Definition: utilities.h:150
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
Long
Definition: utilities.h:156
TComplex
Definition: Bimage.h:59
Float
Definition: utilities.h:157
writeSER
int writeSER(Bimage *p)
Definition: rwSER.cpp:108
Bimage
General image parameter class.
Definition: Bimage.h:163
UInteger
Definition: utilities.h:153
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
Double
Definition: utilities.h:158
ULong
Definition: utilities.h:155
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
file_util.h
Header file for file checking functions.
Bimage::data_offset
long data_offset()
Definition: Bimage.h:265
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bimage::channels
long channels()
Definition: Bimage.h:371
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
UCharacter
Definition: utilities.h:149
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bit
Definition: utilities.h:148
verbose
int verbose
fread_large
int fread_large(unsigned char *aptr, size_t pagesize, size_t offset, ifstream *fimg)
Reads blocks of memeory no larger than 1Gb.
Definition: file_util.cpp:347
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:252
readSER
int readSER(Bimage *p, int readdata)
Definition: rwSER.cpp:32
Short
Definition: utilities.h:152
UShort
Definition: utilities.h:151
Bimage::images
long images()
Definition: Bimage.h:491
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
rwSER.h
Header file for reading and writing FEI Ser files.