| Bsoft 2.0.5 Bernard's software package

rwMFF.cpp
Go to the documentation of this file.
1
9 #include "rwMFF.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
32 int readMFF(Bimage* p, int readdata)
33 {
34 ifstream* fimg = new ifstream;
35 fimg->open(p->file_name());
36 if ( fimg->fail() ) return -1;
37
38 if ( verbose & VERB_DEBUG )
39 cout << "DEBUG readMFF: File opened" << endl;
40
41 MFFhead* header = new MFFhead;
42
43 fimg->read((char *)header, MFFSIZE);
44 if ( fimg->fail() ) return -2;
45
46 // Determine byte order and swap bytes if from little-endian machine
47 unsigned char* b = (unsigned char *) header;
48 int i, sb = 0, vax = 0;
49 if ( abs(header->nz) > SWAPTRIG ) {
50 if ( verbose & VERB_PROCESS )
51 cerr << "Warning: Swapping header byte order for 4-byte types" << endl;
52 sb = 1;
53 for ( i=84; i<MFFSIZE; i+=4 ) swapbytes(b+i, 4);
54 }
55
56 // Convert VAX floating point types if necessary
57 if ( header->a > SWAPTRIG || header->b > SWAPTRIG || header->c > SWAPTRIG ) {
58 if ( verbose & VERB_PROCESS )
59 cerr << "Warning: Converting VAX floating point" << endl;
60 vax = 1;
61 for ( i=92; i<136; i+=4 ) // Unit cell parameters & step sizes
62 vax2ieee(b+i, sb);
63 for ( i=204; i<MFFSIZE; i+=4 ) // Origin & skew matrix
64 vax2ieee(b+i, sb);
65 }
66
67 // Map the parameters
68 p->size(header->nx, header->ny, header->nz);
69 p->images(1);
70 p->channels(1);
71 p->data_type(Float);
72 p->data_offset(MFFSIZE);
73
74 UnitCell uc(header->a, header->b, header->c, header->alpha, header->beta, header->gamma);
75 p->unit_cell(uc);
76
77 p->sampling(header->stepu, header->stepv, header->stepw);
78 p->label(header->title);
79
80 p->page_size(p->size()[0], p->size()[1], 1);
81
82 // Allocating the single sub-image and setting its origin
83 p->origin(-header->uvworg[0]/header->uvwmat[0][0],
84 -header->uvworg[1]/header->uvwmat[1][1],
85 -header->uvworg[2]/header->uvwmat[2][2]);
86
87 delete header;
88
89 if ( readdata )
90 p->read_data( fimg, -1, sb, vax, 8 );
91
92 fimg->close();
93 delete fimg;
94
95 return 0;
96 }
97
104 int writeMFF(Bimage* p)
105 {
106 p->color_to_simple();
107 p->change_type(Float);
108
109 MFFhead* header = new MFFhead;
110 memset(header, 0, sizeof(MFFhead));
111
112 // Map the parameters
113 UnitCell uc = p->unit_cell();
114 header->nx = p->sizeX();
115 header->ny = p->sizeY();
116 header->nz = p->sizeZ();
117 header->idiru = 1; // Column, row, section order
118 header->idirv = 2;
119 header->idirw = 3;
120 header->uvwmat[0][0] = 1/uc.a();
121 header->uvwmat[1][1] = 1/uc.b();
122 header->uvwmat[2][2] = 1/uc.c();
123 header->uvworg[0] = p->image[0].origin()[0]*header->uvwmat[0][0];
124 header->uvworg[1] = p->image[0].origin()[1]*header->uvwmat[1][1];
125 header->uvworg[2] = p->image[0].origin()[2]*header->uvwmat[2][2];
126 header->stepu = p->sampling()[0];
127 header->stepv = p->sampling()[1];
128 header->stepw = p->sampling()[2];
129 header->a = uc.a();
130 header->b = uc.b();
131 header->c = uc.c();
132 header->alpha = uc.alpha()*180/M_PI;
133 header->beta = uc.beta()*180/M_PI;
134 header->gamma = uc.gamma()*180/M_PI;
135 header->ndiva = (int) (uc.a()/p->sampling()[0]); // Unit cell size in pixels
136 header->ndivb = (int) (uc.b()/p->sampling()[1]);
137 header->ndivc = (int) (uc.c()/p->sampling()[2]);
138 strncpy(header->title, p->label().c_str(), 80);
139
140 header->pad1 = 80; // Padding fields
141 header->pad2[0] = 80;
142 header->pad2[1] = 24;
143 header->pad3[0] = 24;
144 header->pad3[1] = 12;
145 header->pad4[0] = 12;
146 header->pad4[1] = 12;
147 header->pad5[1] = 12;
148 header->pad5[0] = 12;
149 header->pad5[1] = 12;
150 header->pad6[0] = 12;
151 header->pad6[1] = 12;
152 header->pad7[0] = 12;
153 header->pad7[1] = 12;
154 header->pad8[0] = 12;
155 header->pad8[1] = 36;
156 header->pad9[0] = 36;
157
158 p->data_offset(MFFSIZE);
159
160 long datatypesize = p->data_type_size();
161 long pagesize = header->nx*header->ny*datatypesize;
162 header->pad9[1] = pagesize;
163
164 unsigned char* data = p->data_pointer();
165
166 ofstream fimg(p->file_name());
167 if ( fimg.fail() ) return -1;
168
169 fimg.write((char *)header, p->data_offset());
170
171 for ( int i=0; i<header->nz; i++, data += pagesize ) {
172 fimg.write((char *)data, pagesize);
173 if ( i < header->nz - 1 )
174 fimg.write((char *)&pagesize, sizeof(unsigned long));
175 else
176 fimg.write((char *)&pagesize, sizeof(unsigned int));
177 }
178
179 fimg.close();
180
181 delete header;
182
183 return 0;
184 }
185
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
MFFhead::ndivb
int ndivb
Definition: rwMFF.h:35
UnitCell
Definition: UnitCell.h:22
MFFhead::nz
int nz
Definition: rwMFF.h:40
MFFhead::nx
int nx
Definition: rwMFF.h:38
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
UnitCell::gamma
void gamma(double d)
Definition: UnitCell.h:41
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.
Bimage::read_data
unsigned char * read_data(ifstream *fimg, int img_select, int sb, int vax, long pad)
Read image data in a generalized style.
Definition: Bimage.cpp:2188
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
UnitCell::beta
void beta(double d)
Definition: UnitCell.h:40
MFFhead::c
float c
Definition: rwMFF.h:21
MFFSIZE
#define MFFSIZE
Definition: rwMFF.h:13
rwMFF.h
Header file for reading and writing the What If MFF files.
MFFhead::uvworg
float uvworg[3]
Definition: rwMFF.h:42
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
UnitCell::a
void a(double d)
Definition: UnitCell.h:36
MFFhead::beta
float beta
Definition: rwMFF.h:23
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
MFFhead::gamma
float gamma
Definition: rwMFF.h:24
MFFhead::pad9
int pad9[2]
Definition: rwMFF.h:45
Float
Definition: utilities.h:157
MFFhead::pad5
int pad5[2]
Definition: rwMFF.h:33
Bimage
General image parameter class.
Definition: Bimage.h:163
MFFhead::pad8
int pad8[2]
Definition: rwMFF.h:43
MFFhead::uvwmat
float uvwmat[3][3]
Definition: rwMFF.h:44
M_PI
#define M_PI
Definition: utilities.h:59
MFFhead::ndivc
int ndivc
Definition: rwMFF.h:36
Bimage::label
void label(string s)
Definition: Bimage.h:235
verbose
int verbose
vax2ieee
void vax2ieee(unsigned char *v, int sb)
Converts VAX floating point format to IEEE floating point format.
Definition: utilities.cpp:548
MFFhead
Definition: rwMFF.h:15
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:601
UnitCell::b
void b(double d)
Definition: UnitCell.h:37
sb
int sb(0)
MFFhead::pad1
int pad1
Definition: rwMFF.h:16
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bimage::data_offset
long data_offset()
Definition: Bimage.h:265
MFFhead::ndiva
int ndiva
Definition: rwMFF.h:34
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bimage::channels
long channels()
Definition: Bimage.h:371
MFFhead::idirw
int idirw
Definition: rwMFF.h:32
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:504
MFFhead::b
float b
Definition: rwMFF.h:20
readMFF
int readMFF(Bimage *p, int readdata)
Reading a MFF image format.
Definition: rwMFF.cpp:32
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
MFFhead::pad3
int pad3[2]
Definition: rwMFF.h:25
MFFhead::stepu
float stepu
Definition: rwMFF.h:26
UnitCell::alpha
void alpha(double d)
Definition: UnitCell.h:39
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
MFFhead::stepv
float stepv
Definition: rwMFF.h:27
MFFhead::pad4
int pad4[2]
Definition: rwMFF.h:29
MFFhead::pad2
int pad2[2]
Definition: rwMFF.h:18
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
MFFhead::idiru
int idiru
Definition: rwMFF.h:30
MFFhead::alpha
float alpha
Definition: rwMFF.h:22
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
MFFhead::a
float a
Definition: rwMFF.h:19
MFFhead::pad6
int pad6[2]
Definition: rwMFF.h:37
writeMFF
int writeMFF(Bimage *p)
Writeing a MFF image format.
Definition: rwMFF.cpp:104
MFFhead::title
char title[80]
Definition: rwMFF.h:17
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:480
Bimage::images
long images()
Definition: Bimage.h:491
MFFhead::idirv
int idirv
Definition: rwMFF.h:31
MFFhead::stepw
float stepw
Definition: rwMFF.h:28
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
UnitCell::c
void c(double d)
Definition: UnitCell.h:38
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
MFFhead::pad7
int pad7[2]
Definition: rwMFF.h:41
MFFhead::ny
int ny
Definition: rwMFF.h:39