| Bsoft 2.0.5 Bernard's software package

rwSUPRIM.cpp
Go to the documentation of this file.
1
9 #include "rwSUPRIM.h"
10 #include "file_util.h"
11 #include "utilities.h"
12
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15
16 View view_from_suprim_euler(SUPRIMhead* header)
17 {
18 View view;
19
20 view[0] = cos(header->reg[PHI_BP].f)*sin(header->reg[THE_BP].f);
21 view[1] = sin(header->reg[PHI_BP].f)*sin(header->reg[THE_BP].f);
22 view[2] = cos(header->reg[THE_BP].f);
23 view[3] = angle_set_negPI_to_PI(header->reg[PSI_BP].f + header->reg[PHI_BP].f);
24
25 if ( verbose & VERB_DEBUG )
26 cout << "DEBUG view_from_imagic_euler: " << view << endl;
27
28 return view;
29 }
30
31 int suprim_euler_from_view(SUPRIMhead* header, View view)
32 {
33 if ( acos(view[2]) > 1e-14 )
34 header->reg[PHI_BP].f = atan2(view[1], view[0]);
35 header->reg[THE_BP].f = acos(view[2]);
36 header->reg[PSI_BP].f = view[3] - header->reg[PHI_BP].f;
37 header->reg[PSI_BP].f = angle_set_negPI_to_PI(header->reg[PSI_BP].f);
38
39 return 0;
40 }
41
42
60 int readSUPRIM(Bimage *p, int readdata)
61 {
62 ifstream* fimg = new ifstream(p->file_name());
63 if ( fimg->fail() ) return -1;
64
65 char version;
66 fimg->read(&version, 1);
67 if ( fimg->fail() ) {
68 error_show("Suprim version number incorrect", __FILE__, __LINE__);
69 return -2;
70 }
71
72 SUPRIMhead* header = new SUPRIMhead;
73 memset(header, 0, sizeof(SUPRIMhead));
74 fimg->read((char *)header, SUPRIMSIZE);
75 if ( fimg->fail() ) {
76 error_show("Suprim header not read", __FILE__, __LINE__);
77 return -2;
78 }
79
80 // Determine byte order and swap bytes if from little-endian machine
81 unsigned char* b = (unsigned char *) header;
82 int i, sb = 0;
83 if ( version < 0x40 || abs(header->reg[NSLICES].l) > SWAPTRIG ) {
84 if ( verbose & VERB_PROCESS )
85 cerr << "Warning: Swapping header byte order for 4-byte types" << endl;
86 sb = 1;
87 int extent = SUPRIMSIZE; // Swap whole header
88 for ( i=0; i<extent; i+=4 ) swapbytes(b+i, 4);
89 }
90
91 // Convert VAX floating point types if necessary
92 /* if ( header->min > header->max ) {
93 if ( verbose & VERB_PROCESS )
94 cerr << "Warning: Converting VAX floating point" << endl;
95 vax = 1;
96 for ( i=20; i<36; i+=4 ) // All floating point parameters
97 vax2ieee(b+i, sb);
98 }
99 */
100 if ( verbose & VERB_DEBUG ) {
101 cout << "DEBUG readSUPRIM: version byte = " << version << endl;
102 cout << "DEBUG readSUPRIM: data size = " << header->ncol << "," << header->nrow << "," << header->reg[NSLICES].l << endl;
103 }
104
105 // Trace records
106 unsigned char* ptr = (unsigned char *) header->trace;
107 int* iptr = (int *) ptr;
108 // fread( ptr, 4, 1, fimg );
109 fimg->read((char *)ptr, 4);
110 if ( sb ) swapbytes(ptr, 4);
111 int it = sizeof(int);
112 int narg = *iptr + 2;
113 if ( verbose & VERB_DEBUG ) cout << "DEBUG readSUPRIM: number of trace arguments = " << narg << endl;
114 if ( narg > 256 ) {
115 cerr << "Error: The number of trace arguments are too large! (" << narg << ")" << endl;
116 cerr << " First trace bytes: " << ptr[0] << " " << ptr[1] << " " << ptr[2] << " " << ptr[3] << endl;
117 return -3;
118 }
119 for ( i=0; i<narg; i++) {
120 // fread( &ptr[it], 4, 1, fimg );
121 fimg->read((char *)&ptr[it], 4);
122 if ( sb ) swapbytes(&ptr[it], 4);
123 iptr = (int *) &ptr[it];
124 it += sizeof(int);
125 // fread( &ptr[it], *iptr, 1, fimg );
126 fimg->read((char *)&ptr[it], *iptr);
127 if ( verbose & VERB_DEBUG )
128 cout << "DEBUG readSUPRIM: trace " << i+1 << " (" << *iptr << "): " << &ptr[it] << endl;
129 if ( i == 2 ) p->label((char *) &ptr[it]);
130 if ( i==2 && (verbose & VERB_DEBUG) )
131 cout << "DEBUG readSUPRIM: label: " << p->label() << endl;
132 it += *iptr;
133 }
134 it += sizeof(int);
135 p->data_offset(SUPRIMSIZE + it + 1);
136
137 if ( verbose & VERB_DEBUG )
138 cout << "DEBUG readSUPRIM: data offset = " << p->data_offset() << endl;
139
140 // Map the parameters
141 p->size(header->ncol, header->nrow, header->reg[NSLICES].l);
142 if ( header->type == 2 ) p->fourier_type(Standard);
143 p->compound_type(TSimple);
144 p->images(1);
145 p->channels(1);
146 switch ( header->intern ) {
147 case 1: p->data_type(UCharacter); break; // BINARY
148 case 2: p->data_type(Float); break;
149 case 3: p->data_type(Float); p->channels(2); p->compound_type(TComplex); break;
150 case 4: p->data_type(Float); break; // RLC_DATA
151 case 5: p->data_type(Short); break;
152 case 6: p->data_type(UCharacter); p->channels(3); p->compound_type(TRGB); break;
153 case 7: p->data_type(Integer); break;
154 case 8: p->data_type(Double); break; // DOUBLE
155 default: p->data_type(UCharacter); break;
156 }
157 p->sampling(header->reg[SAMP_DIST].f, header->reg[SAMP_DIST].f, header->reg[SAMP_DIST].f);
158 p->minimum(header->min);
159 p->maximum(header->max);
160 p->average(header->av);
161 p->standard_deviation(header->sd);
162
163 if ( verbose & VERB_DEBUG )
164 cout << "DEBUG readSUPRIM: data type = " << p->data_type() << endl;
165
166 // p->image->origin(header->reg[COL_ORG].f, header->reg[ROW_ORG].f, header->reg[SLICE_ORG].f);
167 p->origin(header->reg[COL_ORG].f, header->reg[ROW_ORG].f, header->reg[SLICE_ORG].f);
168 p->background(0, header->reg[BACKGROUND].f);
169
170 if ( verbose & VERB_DEBUG )
171 cout << "DEBUG readSUPRIM: background = " << p->background(long(0)) << endl;
172
173 p->image->view(view_from_suprim_euler(header));
174
175 delete header;
176
177 if ( verbose & VERB_DEBUG )
178 cout << "DEBUG readSUPRIM: Header setup done" << endl;
179
180 long readsize = p->alloc_size();
181
182 if ( readdata ) {
183 p->data_alloc();
184 fread_large(p->data_pointer(), readsize, p->data_offset(), fimg);
185 if ( sb ) swapbytes(readsize, p->data_pointer(), p->data_type_size());
186 }
187
188 fimg->close();
189 delete fimg;
190
191 if ( verbose & VERB_DEBUG )
192 cout << "DEBUG readSUPRIM: Data read" << endl;
193
194 return 0;
195 }
196
203 int writeSUPRIM(Bimage *p)
204 {
205 if ( p->compound_type() == TComplex ) p->change_type(Float);
206
207 switch ( p->data_type() ) {
208 case Bit: case UCharacter: case SCharacter:
209 p->change_type(UCharacter); break;
210 case UShort: case Short:
211 p->change_type(Short); break;
212 case UInteger: case Integer:
213 p->change_type(Integer); break;
214 default: break;
215 }
216
217 SUPRIMhead* header = new SUPRIMhead;
218 memset(header, 0, sizeof(SUPRIMhead));
219
220 // Map the parameters
221 char version = 0x43;
222 header->ncol = p->sizeX();
223 header->nrow = p->sizeY();
224 header->reg[NSLICES].l = p->sizeZ();
225 header->reg[SAMP_DIST].f = p->sampling()[0];
226 header->type = 1; // Image
227 if ( p->fourier_type() ) header->type = 2; // Fourier transform
228 switch ( p->data_type() ) {
229 case UCharacter: header->intern = 1; // BINARY
230 if ( p->compound_type() == TRGB && p->channels() == 3 )
231 header->intern = 6;
232 break; // RGB
233 case Float: header->intern = ( p->compound_type() == TComplex )? 3: 2; break;
234 // case Float: header->intern = 4; break; // RLC_DATA
235 case Short: header->intern = 5; break;
236 case Integer: header->intern = 7; break;
237 case Double: header->intern = 8; break; // DOUBLE
238 default: header->intern = 1; break;
239 }
240 header->min = p->minimum();
241 header->max = p->maximum();
242 header->av = p->average();
243 header->sd = p->standard_deviation();
244
245 header->reg[COL_ORG].f = p->image->origin()[0];
246 header->reg[ROW_ORG].f = p->image->origin()[1];
247 header->reg[SLICE_ORG].f = p->image->origin()[2];
248 header->reg[BACKGROUND].f = p->background(long(0));
249
250 suprim_euler_from_view(header, p->image->view());
251
252 // Trace records
253 char* ptr = (char *) header->trace;
254 int* iptr = (int *) ptr;
255 iptr[0] = 1;
256 iptr[1] = 12;
257 memcpy(ptr + 8, "Bsoft1.4", 8);
258 iptr = (int *) (ptr + 20);
259 *iptr = 24;
260 memcpy(ptr + 24, asctime(p->get_localtime())+4, 20);
261 iptr = (int *) (ptr + 48);
262 // *iptr = 8;
263 // memcpy(ptr + 52, "unknown", 7);
264 // p->data_offset(SUPRIMSIZE + 65);
265 long llen = p->label().length();
266 if ( llen > 800 ) llen = 800;
267 *iptr = llen;
268 memcpy(ptr + 52, p->label().c_str(), llen);
269 p->data_offset(SUPRIMSIZE + llen + 57);
270
271 long datatypesize = p->data_type_size();
272 header->format = datatypesize;
273 long datasize = p->alloc_size();
274
275 if ( verbose & VERB_DEBUG ) {
276 cout << "DEBUG writeSUPRIM: version byte=" << version << endl;
277 cout << "DEBUG writeSUPRIM: size:" << header->ncol << "," << header->nrow << "," << header->reg[NSLICES].l << endl;
278 cout << "DEBUG writeSUPRIM: offset=" << p->data_offset() << " data size=" << datasize << endl;
279 cout << "DEBUG writeSUPRIM: number of trace arguments = " << iptr[0] << endl;
280 cout << "DEBUG writeSUPRIM: trace = ";
281 for ( int i=0; i<20; i++ ) cout << i << " " << header->trace[i] << " " << header->trace[i] << endl;
282 cout << endl;
283 }
284
285 ofstream fimg(p->file_name());
286 if ( fimg.fail() ) return -1;
287
288 fimg.write(&version, 1);
289 fimg.write((char *)header, p->data_offset() - 1);
290
291 if ( p->data_pointer() ) fimg.write((char *)p->data_pointer(), datasize);
292
293 fimg.close();
294
295 delete header;
296
297 return 0;
298 }
299
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
SWAPTRIG
#define SWAPTRIG
Definition: rwimg.h:16
view_from_suprim_euler
View view_from_suprim_euler(SUPRIMhead *header)
Definition: rwSUPRIM.cpp:16
SUPRIMhead::max
float max
Definition: rwSUPRIM.h:85
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:143
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
THE_BP
#define THE_BP
Definition: rwSUPRIM.h:30
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1585
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:594
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
Integer
Definition: utilities.h:154
REG::f
float f
Definition: rwSUPRIM.h:75
ROW_ORG
#define ROW_ORG
Definition: rwSUPRIM.h:21
REG::l
int l
Definition: rwSUPRIM.h:74
SUPRIMhead::type
int type
Definition: rwSUPRIM.h:83
SCharacter
Definition: utilities.h:150
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
SUPRIMhead::min
float min
Definition: rwSUPRIM.h:84
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
SUPRIMhead::sd
float sd
Definition: rwSUPRIM.h:87
TComplex
Definition: Bimage.h:59
SLICE_ORG
#define SLICE_ORG
Definition: rwSUPRIM.h:48
rwSUPRIM.h
Header file for reading and writing SUPRIM files.
TSimple
Definition: Bimage.h:58
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
SUPRIMhead::format
int format
Definition: rwSUPRIM.h:81
UInteger
Definition: utilities.h:153
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
SUPRIMhead::av
float av
Definition: rwSUPRIM.h:86
Bimage::maximum
double maximum()
Definition: Bimage.h:535
Double
Definition: utilities.h:158
NSLICES
#define NSLICES
Definition: rwSUPRIM.h:27
Bimage::label
void label(string s)
Definition: Bimage.h:235
SAMP_DIST
#define SAMP_DIST
Definition: rwSUPRIM.h:26
Standard
Definition: Bimage.h:41
sb
int sb(0)
SUPRIMhead
Definition: rwSUPRIM.h:78
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
Bimage::get_localtime
tm * get_localtime()
Definition: Bimage.h:245
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bimage::channels
long channels()
Definition: Bimage.h:371
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:504
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:537
SUPRIMhead::trace
char trace[1024]
Definition: rwSUPRIM.h:89
TRGB
Definition: Bimage.h:62
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
UCharacter
Definition: utilities.h:149
SUPRIMhead::reg
REG reg[MAXREG]
Definition: rwSUPRIM.h:88
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bit
Definition: utilities.h:148
SUPRIMSIZE
#define SUPRIMSIZE
Definition: rwSUPRIM.h:69
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
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
BACKGROUND
#define BACKGROUND
Definition: rwSUPRIM.h:16
SUPRIMhead::nrow
int nrow
Definition: rwSUPRIM.h:79
PHI_BP
#define PHI_BP
Definition: rwSUPRIM.h:31
View
Definition: View.h:26
SUPRIMhead::intern
int intern
Definition: rwSUPRIM.h:82
version
int version(0)
SUPRIMhead::ncol
int ncol
Definition: rwSUPRIM.h:80
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1349
Short
Definition: utilities.h:152
PSI_BP
#define PSI_BP
Definition: rwSUPRIM.h:51
suprim_euler_from_view
int suprim_euler_from_view(SUPRIMhead *header, View view)
Definition: rwSUPRIM.cpp:31
UShort
Definition: utilities.h:151
Bimage::images
long images()
Definition: Bimage.h:491
verbose
int verbose
COL_ORG
#define COL_ORG
Definition: rwSUPRIM.h:22
Bimage::background
double background(long nn)
Definition: Bimage.h:542
writeSUPRIM
int writeSUPRIM(Bimage *p)
Writing a SUPRIM map image file format. A 3D image format used in electron microscopy.
Definition: rwSUPRIM.cpp:203
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
readSUPRIM
int readSUPRIM(Bimage *p, int readdata)
Reading a SUPRIM map image file format. A 3D image format used in electron microscopy....
Definition: rwSUPRIM.cpp:60
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::minimum
double minimum()
Definition: Bimage.h:534
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:367
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:755
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563