| Bsoft 2.0.5 Bernard's software package

rwEM.cpp
Go to the documentation of this file.
1
9 #include "rwEM.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
41 int readEM(Bimage* p, int readdata)
42 {
43 ifstream* fimg = new ifstream(p->file_name());
44 if ( fimg->fail() ) return -1;
45
46 EMhead* header = new EMhead;
47
48 fimg->read((char *)header, EMSIZE);
49 if ( fimg->fail() ) return -2;
50
51 // Determine byte order and swap bytes if necessary
52 unsigned char* b = (unsigned char *) header;
53 int i, sb = 0;
54 if ( abs(header->nz) > SWAPTRIG || header->nz < 1 ) {
55 if ( verbose & VERB_PROCESS )
56 cerr << "Warning: Swapping header byte order for 4-byte types" << endl;
57 sb = 1;
58 int extent = EMSIZE; // exclude labels from swapping
59 for ( i=4; i<16; i+=4 ) swapbytes(b+i, 4); // Image size fields
60 for ( i=96; i<extent; i+=4 ) swapbytes(b+i, 4); // Extras
61 }
62
63 // Map the parameters
64 p->size(header->nx, header->ny, header->nz);
65 p->images(1);
66 p->channels(1);
67
68 switch( header->stamp[3] ){
69 case 1 : p->data_type(UCharacter); break; // Images
70 case 2 : p->data_type(Short); break;
71 case 4 : p->data_type(Integer); break;
72 case 5 : p->data_type(Float); break;
73 case 8 : p->data_type(Float); // Transform
74 p->compound_type(TComplex); p->channels(2);
75 break;
76 default : p->data_type(UCharacter);
77 }
78
79 p->data_offset(EMSIZE);
80 p->label(header->label);
81
82
83 long readsize = p->alloc_size();
84 unsigned char* data = NULL;
85
86 if ( readdata ) {
87 p->data_alloc();
88 if ( p->compound_type() == TSimple ) {
89 fread_large(p->data_pointer(), readsize, p->data_offset(), fimg);
90 if ( sb ) swapbytes(readsize, p->data_pointer(), p->data_type_size());
91 } else {
92 readsize = p->channels()*(p->sizeX()/2+1)*p->sizeY()*p->sizeZ()*p->data_type_size();
93 data = new unsigned char[readsize];
94 fread_large(data, readsize, p->data_offset(), fimg);
95 if ( sb ) swapbytes(readsize, data, p->data_type_size());
96 p->unpack_transform(data, Hermitian);
97 delete[] data;
98 }
99 }
100
101 fimg->close();
102 delete fimg;
103
104 delete header;
105
106 return 0;
107 }
108
115 int writeEM(Bimage* p)
116 {
117 p->color_to_simple();
118
119 if ( p->compound_type() == TComplex ) p->change_type(Float);
120
121 switch ( p->data_type() ) {
122 case Bit: case UCharacter: case SCharacter:
123 p->change_type(UCharacter); break;
124 case UShort: case Short:
125 p->change_type(Short); break;
126 case UInteger: case Integer:
127 p->change_type(Integer); break;
128 default:
129 p->change_type(Float); break;
130 }
131
132 switch ( p->data_type() ) {
133 case SCharacter: p->change_type(UCharacter); break;
134 case UShort: p->change_type(Short); break;
135 default: break;
136 }
137
138 EMhead* header = new EMhead;
139 memset(header, 0, sizeof(EMhead));
140
141 // Map the parameters
142 header->nx = p->sizeX();
143 header->ny = p->sizeY();
144 header->nz = p->sizeZ();
145 switch ( systype(0) ) {
146 case BigIEEE: header->stamp[0] = 3; break; // SGI machine stamp
147 case LittleIEEE: header->stamp[0] = 6; break; // Alpha, Intel
148 case LittleVAX: header->stamp[0] = 1; break; // VAX
149 default: break;
150 }
151 switch ( p->data_type() ) {
152 case UCharacter : header->stamp[3] = 1; break; // Images
153 case Short : header->stamp[3] = 2; break;
154 case Integer : header->stamp[3] = 4; break;
155 case Float : header->stamp[3] = 5;
156 if ( p->compound_type() == TComplex ) header->stamp[3] = 8; break; // Transform
157 default : header->stamp[3] = 1;
158 }
159
160 strncpy(header->label, p->label().c_str(), 80);
161
162 p->data_offset(EMSIZE);
163
164 // If a transform, then the physical storage in x is only half+1
165 int xstore = header->nx;
166 if ( p->compound_type() == TComplex ) xstore = header->nx/2 + 1;
167
168 int datatypesize = p->channels()*p->data_type_size();
169 long datasize = (long) xstore*header->ny*header->nz*datatypesize;
170
171 unsigned char* data = NULL;
172
173 ofstream fimg(p->file_name().c_str());
174 if ( fimg.fail() ) return -1;
175
176 fimg.write((char *)header, p->data_offset());
177
178 if ( p->data_pointer() ) {
179 if ( p->compound_type() < TComplex ) {
180 fimg.write((char *)p->data_pointer(), datasize);
181 } else {
182 data = new unsigned char[datasize];
183 p->pack_transform(data, Hermitian);
184 fimg.write((char *)data, datasize);
185 delete[] data;
186 }
187 }
188
189 fimg.close();
190
191 delete header;
192
193 return 0;
194 }
195
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
EMhead::ny
int ny
Definition: rwEM.h:18
EMSIZE
#define EMSIZE
Definition: rwEM.h:13
LittleIEEE
Definition: utilities.h:131
BigIEEE
Definition: utilities.h:129
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::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
LittleVAX
Definition: utilities.h:133
Integer
Definition: utilities.h:154
EMhead::stamp
char stamp[4]
Definition: rwEM.h:16
EMhead::nz
int nz
Definition: rwEM.h:19
SCharacter
Definition: utilities.h:150
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
TComplex
Definition: Bimage.h:59
Bimage::pack_transform
int pack_transform(unsigned char *data, FourierType tf)
Definition: Bimage.cpp:2422
TSimple
Definition: Bimage.h:58
Float
Definition: utilities.h:157
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
rwEM.h
Header file for reading and writing EM files.
writeEM
int writeEM(Bimage *p)
Writing a EM map image file format. A 3D image format used in electron microscopy.
Definition: rwEM.cpp:115
EMhead
Definition: rwEM.h:15
Bimage::label
void label(string s)
Definition: Bimage.h:235
verbose
int verbose
sb
int sb(0)
EMhead::label
char label[80]
Definition: rwEM.h:20
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::channels
long channels()
Definition: Bimage.h:371
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:504
Bimage::unpack_transform
int unpack_transform(unsigned char *data, FourierType tf)
Definition: Bimage.cpp:2337
readEM
int readEM(Bimage *p, int readdata)
Reading a EM map image file format. A 3D image format used in electron microscopy....
Definition: rwEM.cpp:41
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
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
EMhead::nx
int nx
Definition: rwEM.h:17
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
Short
Definition: utilities.h:152
UShort
Definition: utilities.h:151
Bimage::images
long images()
Definition: Bimage.h:491
Hermitian
Definition: Bimage.h:43
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:144
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349