| Bsoft 2.0.5 Bernard's software package

rwBRIX.cpp
Go to the documentation of this file.
1
9 #include "rwBRIX.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
37 int readBRIX(Bimage* p, int readdata)
38 {
39 ifstream* fimg = new ifstream;
40 fimg->open(p->file_name());
41 if ( fimg->fail() ) return -1;
42
43 if ( verbose & VERB_DEBUG )
44 cout << "DEBUG readBRIX: File opened" << endl;
45
46 char* header = new char[BRIXSIZE];
47
48 fimg->read((char *)header, BRIXSIZE);
49 if ( fimg->fail() ) return -2;
50
51 int mx(0), my(0), mz(0);
52 long x(1), y(1), z(1);
53 float prod(1);
54 int plus(127);
55 float sigma(1);
56 Vector3<double> ori;
57
58 // Allocating the single sub-image
59 p->images(1);
60
61 // extract the image attributes from the file header
62 char *var;
63 UnitCell uc;
64 for ( int i=0; i<BRIXSIZE; i++ ) header[i] = tolower(header[i]);
65 if ( ( var = strstr(header,"origin") ) != NULL ) // Origin
66 sscanf(var,"origin %lf %lf %lf", &ori[0], &ori[1], &ori[2]);
67 if ( ( var = strstr(header,"extent") ) != NULL )
68 sscanf(var,"extent %ld %ld %ld", &x, &y, &z); // Size
69 if ( ( var = strstr(header,"grid") ) != NULL )
70 sscanf(var,"grid %d %d %d", &mx, &my, &mz); // Unit cell size
71 if ( ( var = strstr(header,"cell") ) != NULL )
72 sscanf(var,"cell %lf %lf %lf %lf %lf %lf",
73 &uc[0], &uc[1], &uc[2], &uc[3], &uc[4], &uc[5]); // Unit cell parameters
74 if ( ( var = strstr(header,"prod") ) != NULL )
75 sscanf(var,"prod %f", &prod); // Scale product
76 if ( ( var = strstr(header,"plus") ) != NULL )
77 sscanf(var,"plus %d", &plus); // Scale shift
78 if ( ( var = strstr(header,"sigma") ) != NULL )
79 sscanf(var,"sigma %f", &sigma); // RMS
80
81 // Negating the origin to get the correct convention for Bsoft
82 p->image->origin(-ori);
83
84 if ( verbose & VERB_DEBUG )
85 cout << "DEBUG readBRIX: image header parsed" << endl;
86
87 p->size(x, y, z);
88 p->page_size(8, 8, 8); // Page size = one brick
89 if ( p->sizeZ() < 8 ) p->page_size(8, 8, p->sizeZ());
90 p->channels(1);
91 p->data_type(UCharacter);
92 p->data_offset(BRIXSIZE);
93 p->minimum(0);
94 p->maximum(255);
95 // p->average(plus);
96 // p->scale = prod;
97 p->standard_deviation(sigma*prod); // Scaled to correct value
98 if ( mx && my && mz ) p->sampling(uc.a()/mx, uc.b()/my, uc.c()/mz);
99
100 delete[] header;
101
102 if ( readdata )
103 p->read_data( fimg, -1, 0, 0, 0 );
104
105 if ( verbose & VERB_DEBUG )
106 cout << "DEBUG readBRIX: image read" << endl << endl;
107
108 fimg->close();
109 delete fimg;
110
111 return 0;
112 }
113
120 int writeBRIX(Bimage* p)
121 {
122 p->change_type(UCharacter);
123 p->color_to_simple();
124
125 char* header = new char[BRIXSIZE];
126 memset(header, ' ', BRIXSIZE);
127
128 // Ensure that the scaling is done correctly
129 // if ( p->scale < 250/(p->max - p->min) || p->scale > 255.5/(p->max - p->min) )
130 // img_rescale_to_min_max(p, 0, 255);
131
132 // Map the parameters
133 double scale(1);
134 Vector3<double> u(p->sampling(0));
135 UnitCell uc = p->unit_cell();
136 int mx = (int) (uc.a()/u[0]);
137 int my = (int) (uc.b()/u[1]);
138 int mz = (int) (uc.c()/u[2]);
139 float prod(scale);
140 int plus = (int) p->minimum();
141 float sigma = p->standard_deviation()/scale; // Sigma scaled to original value
142
143 // This is a band-aid to overcome the limitations of the image format
144 if ( fabs(uc.a() - u[0]*mx) > 0.001 || fabs(uc.b() - u[1]*my) || fabs(uc.c() - u[2]*mz) ) {
145 uc.a(u[0]*mx);
146 uc.b(u[1]*my);
147 uc.c(u[2]*mz);
148 if ( verbose & VERB_FULL )
149 cerr << "Warning: Resetting the unit cell to: " << uc.a() << " " << uc.b() << " " << uc.c() << " A" << endl;
150 }
151
152 snprintf(header, BRIXSIZE, ":-) Origin%5.0f%5.0f%5.0f Extent%5ld%5ld%5ld Grid%5d%5d%5d "
153 "Cell %10.3f%10.3f%10.3f%10.3f%10.3f%10.3f "
154 "Prod%12.5f Plus%8d Sigma %12.5f",
155 -p->image->origin()[0], -p->image->origin()[1], -p->image->origin()[2], p->sizeX(), p->sizeY(), p->sizeZ(), mx, my, mz,
156 uc.a(), uc.b(), uc.c(), uc.alpha()*180/M_PI, uc.beta()*180/M_PI, uc.gamma()*180/M_PI, prod, plus, sigma);
157
158 p->data_offset(BRIXSIZE);
159 p->page_size(8, 8, 8);
160 if ( p->sizeZ() < 8 ) p->page_size(8, 8, p->sizeZ());
161
162 long i, j, x, y, z, px, py, pz;
163 long datatypesize = p->data_type_size();
164 long valuesize = datatypesize*p->channels();
165 long pagesize = p->page_size()[0]*p->page_size()[1]*p->page_size()[2]*valuesize;
166 unsigned char* data = p->data_pointer();
167 unsigned char* page = new unsigned char[pagesize];
168 memset(page, 0, pagesize);
169
170 ofstream fimg(p->file_name());
171 if ( fimg.fail() ) return -1;
172
173 fimg.write((char *)header, p->data_offset());
174
175 for ( pz=0; pz<p->sizeZ(); pz+=p->page_size()[2] )
176 for ( py=0; py<p->sizeY(); py+=p->page_size()[1] )
177 for ( px=0; px<p->sizeX(); px+=p->page_size()[0] ) {
178 for ( x=0; x<p->page_size()[0] && x<p->sizeX()-px; x++ )
179 for ( y=0; y<p->page_size()[1] && y<p->sizeY()-py; y++ )
180 for ( z=0; z<p->page_size()[2] && z<p->sizeZ()-pz; z++ ) {
181 i = (((z+pz)*p->sizeY() + y+py)*p->sizeX() + x+px)*valuesize;
182 j = ((z*p->page_size()[1] + y)*p->page_size()[0] + x)*valuesize;
183 memcpy(page+j, data+i, valuesize);
184 }
185 fimg.write((char *)page, pagesize);
186 }
187
188 fimg.close();
189
190 delete[] page;
191
192 delete[] header;
193
194 return 0;
195 }
196
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
UnitCell
Definition: UnitCell.h:22
BRIXSIZE
#define BRIXSIZE
Definition: rwBRIX.h:13
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
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::a
void a(double d)
Definition: UnitCell.h:36
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
readBRIX
int readBRIX(Bimage *p, int readdata)
Reading a BRIX image file format.
Definition: rwBRIX.cpp:37
Bimage
General image parameter class.
Definition: Bimage.h:163
M_PI
#define M_PI
Definition: utilities.h:59
Bimage::maximum
double maximum()
Definition: Bimage.h:535
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:601
UnitCell::b
void b(double d)
Definition: UnitCell.h:37
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
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
rwBRIX.h
Header file for reading and writing Brix files.
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:537
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
UCharacter
Definition: utilities.h:149
writeBRIX
int writeBRIX(Bimage *p)
Writing a BRIX image file format.
Definition: rwBRIX.cpp:120
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< double >
verbose
int verbose
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:480
Bimage::images
long images()
Definition: Bimage.h:491
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::minimum
double minimum()
Definition: Bimage.h:534
UnitCell::c
void c(double d)
Definition: UnitCell.h:38
Bimage::image
Bsub_image * image
Definition: Bimage.h:188