| Bsoft 2.0.5 Bernard's software package

rwBrookhavenSTEM.cpp
Go to the documentation of this file.
1
9 #include "rwBrookhavenSTEM.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 #define LUTSIZE 256
17
18 int img_stem_lut_apply(Bimage* p, int neg_stain_flag)
19 {
20 if ( !p->data_pointer() || p->images() < 2 ) return -1;
21
22 long i;
23 long imgsize = p->sizeX()*p->sizeY();
24 unsigned char* data = p->data_pointer();
25 int j;
26 int prefac(146);
27 int la_gain(10);
28 int sa_gain(10);
29 int bf_gain(0);
30
31 if ( neg_stain_flag ) {
32 la_gain = 2;
33 sa_gain = 0;
34 bf_gain = 1;
35 }
36
37 double ila(0), isa(0), ibf(0), it;
38 float la_lut[LUTSIZE], sa_lut[LUTSIZE], bf_lut[LUTSIZE];
39
40 // Data originating from the microscope
41 for ( i=0; i<LUTSIZE; i++ )
42 la_lut[i] = sa_lut[i] = bf_lut[i] = i;
43
44 for ( j=0; j<7000 && ( ila < LUTSIZE-1 || isa < LUTSIZE-1 ); j++ ) {
45 ila = (1 - exp(-j/2500.0)) * exp(-j/10000.0);
46 isa = (1 - exp(-j/1964.0)) * exp(-j/2000.0);
47 ibf = exp(-j/1100.0);
48 it = ila + isa + ibf;
49 // cout << ila << " " << isa << " " << ibf << " " << it << endl;
50 ila = prefac*la_gain*ila/it;
51 isa = prefac*sa_gain*isa/it;
52 ibf = prefac*bf_gain*ibf/it;
53 // cout << j << " " << ila << " " << isa << " " << ibf << " " << it << endl;
54 if ( ila < LUTSIZE ) la_lut[(int)ila] = prefac*la_gain*j/2500.0;
55 if ( isa < LUTSIZE ) sa_lut[(int)isa] = prefac*sa_gain*j/1964.0;
56 if ( ibf < LUTSIZE ) bf_lut[(int)ibf] = prefac*bf_gain*(1 - j/1100.0);
57 }
58
59 for ( i=0; i<LUTSIZE; i++ ) {
60 // cout << i << " " << la_lut[i] - i << " " << sa_lut[i] - i << " " << bf_lut[i] - i << endl;
61 if ( la_lut[i] >= LUTSIZE ) la_lut[i] = LUTSIZE - 1;
62 if ( sa_lut[i] >= LUTSIZE ) sa_lut[i] = LUTSIZE - 1;
63 if ( bf_lut[i] >= LUTSIZE ) bf_lut[i] = LUTSIZE - 1;
64 if ( bf_lut[i] < 0 ) bf_lut[i] = 0;
65 }
66
67 for ( i=0; i<imgsize; i++ ) data[i] = (int)la_lut[data[i]];
68
69 if ( neg_stain_flag ) {
70 for ( i=0, j=imgsize; i<imgsize; i++, j++ ) {
71 data[j] = (int)bf_lut[data[j]];
72 it = data[j] - data[i]/la_gain;
73 if ( it < 0 ) it = 0;
74 data[i] = (int) it;
75 }
76 } else
77 for ( i=0, j=imgsize; i<imgsize; i++, j++ ) data[j] = (int)sa_lut[data[j]];
78
79 return 0;
80 }
81
101 int readBrookhavenSTEM(Bimage* p, int readdata)
102 {
103 ifstream* fimg = new ifstream(p->file_name());
104 if ( fimg->fail() ) return -1;
105
106 long i, j;
107 char* header = new char[BrookhavenSTEMSIZE];
108 for ( i=0; i<BrookhavenSTEMSIZE; i++ ) header[i] = 0;
109
110 fimg->read(header, BrookhavenSTEMSIZE-1);
111 if ( fimg->fail() ) return -2;
112
113 // Extract the image attributes from the file header
114 // p->label(header);
115
116 double pixel_size(1);
117 char* aptr = strstr(header, "SCAN");
118 if ( aptr ) aptr += 5;
119 else aptr = header + 72; // Old format
120 sscanf(aptr, "%lf", &pixel_size);
121 pixel_size /= 0.0512; // Convert from internal units to angstrom
122 if ( pixel_size < 1e-3 ) pixel_size = 1;
123
124 int neg_stain_flag(0);
125 char subhead[128];
126 strncpy(subhead, header+16, 40);
127 for ( i=0; i<40; i++ ) subhead[i] = tolower(subhead[i]);
128 if ( strstr(subhead, "van") ) neg_stain_flag = 1;
129 if ( strstr(subhead, "ur") ) neg_stain_flag = 1;
130
131 if ( verbose & VERB_DEBUG )
132 cout << "DEBUG readBrookhavenSTEM: image header parsed (" << neg_stain_flag << ")" << endl;
133
134 p->data_offset(BrookhavenSTEMSIZE);
135
136 fimg->seekg(0, ios::end);
137 long datasize = (long)fimg->tellg() - p->data_offset();
138
139 p->size(512, 512, 1);
140 p->channels(1);
141
142 p->data_type(UCharacter);
143 if ( datasize > 300000 ) p->images(2);
144 else p->images(1);
145
146 p->sampling(pixel_size, pixel_size, 1);
147
148 delete[] header;
149
150 if ( verbose & VERB_DEBUG )
151 cout << "DEBUG readBrookhavenSTEM: file_size=" << datasize << " alloc_size=" << p->alloc_size() << endl;
152
153 if ( readdata ) {
154 p->data_alloc();
155 fread_large(p->data_pointer(), p->alloc_size(), p->data_offset(), fimg);
156 }
157
158 fimg->close();
159 delete fimg;
160
161 // Unpack the interleaved images
162 if ( p->images() > 1 && p->data_pointer() ) {
163 long imgsize = p->sizeX()*p->sizeY();
164 unsigned char* data = p->data_pointer();
165 unsigned char* temp = new unsigned char[imgsize];
166 for ( i=j=0; i<imgsize; i++, j+=2 ) {
167 temp[i] = data[j+1];
168 data[i] = data[j];
169 }
170 for ( i=0, j=imgsize; i<imgsize; i++, j++ )
171 data[j] = temp[i];
172 delete[] temp;
173 if ( readdata > 1 ) img_stem_lut_apply(p, neg_stain_flag);
174 }
175
176 if ( verbose & VERB_DEBUG )
177 cout << "DEBUG readBrookhavenSTEM: image read" << endl << endl;
178
179 return 0;
180 }
181
188 int writeBrookhavenSTEM(Bimage* p)
189 {
190 p->change_type(UCharacter);
191
192 char* header = new char[BrookhavenSTEMSIZE];
193 memset(header, ' ', BrookhavenSTEMSIZE);
194
195 strcpy(header, p->label().c_str());
196
197 snprintf(header + 70, BrookhavenSTEMSIZE-70, "SCAN:%6.3f", p->sampling()[0]*0.0512);
198
199 p->data_offset(BrookhavenSTEMSIZE);
200
201 long i, j, k;
202 long imgsize = p->sizeX()*p->sizeY()*p->sizeZ();
203 long datasize = p->data_size();
204 unsigned char* data = p->data_pointer();
205 unsigned char* temp = data;
206
207 // Interleaved data
208 if ( p->images() > 1 ) {
209 temp = new unsigned char[datasize];
210 for ( i=0, j=imgsize, k=0; i<imgsize; i++, j++, k+=2 ) {
211 temp[k] = data[i];
212 temp[k+1] = data[j];
213 }
214 }
215
216 ofstream fimg(p->file_name());
217 if ( fimg.fail() ) return -1;
218
219 fimg.write((char *)header, p->data_offset());
220 if ( p->data_pointer() ) fimg.write((char *)temp, datasize);
221
222 fimg.close();
223
224 delete[] header;
225 if ( temp != data ) delete[] temp;
226
227 return 0;
228 }
229
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
BrookhavenSTEMSIZE
#define BrookhavenSTEMSIZE
Definition: rwBrookhavenSTEM.h:13
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
verbose
int verbose
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
img_stem_lut_apply
int img_stem_lut_apply(Bimage *p, int neg_stain_flag)
Definition: rwBrookhavenSTEM.cpp:18
Bimage::label
void label(string s)
Definition: Bimage.h:235
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
readBrookhavenSTEM
int readBrookhavenSTEM(Bimage *p, int readdata)
Reading a BrookhavenSTEM image file format.
Definition: rwBrookhavenSTEM.cpp:101
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
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
writeBrookhavenSTEM
int writeBrookhavenSTEM(Bimage *p)
Writing a BrookhavenSTEM image file format.
Definition: rwBrookhavenSTEM.cpp:188
rwBrookhavenSTEM.h
Header file for reading and writing Brookhaven STEM files.
Bimage::images
long images()
Definition: Bimage.h:491
LUTSIZE
#define LUTSIZE
Definition: rwBrookhavenSTEM.cpp:16
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::data_size
long data_size()
Definition: Bimage.h:255