| Bsoft 2.0.5 Bernard's software package

rwGRD.cpp
Go to the documentation of this file.
1
9 #include "rwGRD.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
31 int readGRD(Bimage *p, int readdata)
32 {
33 ifstream* fimg = new ifstream(p->file_name());
34 if ( fimg->fail() ) return -1;
35
36 GRDhead* header = new GRDhead;
37
38 fimg->read((char *)header, GRDSIZE);
39 if ( fimg->fail() ) return -2;
40
41 // Determine byte order and swap bytes if from little-endian machine
42 unsigned char* b = (unsigned char *) header;
43 int i, sb = 0;
44 if ( abs( header->mode ) > SWAPTRIG ) {
45 if ( verbose & VERB_PROCESS )
46 cerr << "Warning: Swapping header byte order for 4-byte types" << endl;
47 sb = 1;
48 int extent = GRDSIZE; // Swap whole header
49 for ( i=0; i<extent; i+=4 ) swapbytes(b+i, 4);
50 if ( b[16] > 0 ) header->origp = 1; // Little-endian (DEC,PC)
51 if ( b[19] > 0 ) header->origp = 2; // Big-endian (SGI,MAC)
52 }
53 /*
54 // Convert VAX floating point types if necessary
55 if ( header->amin > header->amax ) {
56 if ( verbose & VERB_PROCESS )
57 cerr << "Warning: Converting VAX floating point" << endl;
58 vax = 1;
59 for ( i=40; i<512; i+=4 ) // All floating point parameters
60 vax2ieee(b+i, sb);
61 }
62 */
63 // Map the parameters
64 p->size(header->nx, header->ny, header->nz);
65 p->images(1);
66 p->channels(1);
67 p->sampling(header->ux, header->uy, header->uz);
68 switch ( header->mode ) {
69 case 1: p->data_type(UCharacter); break;
70 case 2: p->data_type(Float); break;
71 case 3: p->data_type(Float);
72 p->compound_type(TComplex); p->channels(2);
73 p->fourier_type(Standard); break;
74 case 4: p->data_type(Float);
75 p->compound_type(TVector3); p->channels(3); break;
76 case 5: p->data_type(Float);
77 p->compound_type(TView); p->channels(4); break;
78 default: p->data_type(UCharacter); break;
79 }
80 if ( verbose & VERB_DEBUG )
81 cout << "DEBUG readGRD: mode=" << header->mode << " datatype=" << p->data_type() << endl;
82 p->data_offset(GRDSIZE*(1 + header->doffset));
83 p->minimum(header->amin);
84 p->maximum(header->amax);
85 p->average(header->amean);
86 p->standard_deviation(header->arms);
87 p->unit_cell(UnitCell(header->a, header->b, header->c, header->alpha, header->beta, header->gamma));
88
89 // Allocating the single sub-image and setting its parameters
90 p->background(0, header->bkg);
91 p->origin(header->ox, header->oy, header->oz);
92 p->view(header->vx, header->vy, header->vz, header->va);
93
94 tm* t = p->get_localtime();
95 sscanf(header->datetime, "%4d%2d%2d%2d%2d%2d",
96 &t->tm_year, &t->tm_mon, &t->tm_mday,
97 &t->tm_hour, &t->tm_min, &t->tm_sec);
98 t->tm_mon -= 1;
99 t->tm_year -= 1900;
100 p->set_time(t);
101
102 p->symmetry(header->symmetry);
103 p->label(header->label);
104
105 delete header;
106
107 if ( readdata ) {
108 p->data_alloc();
109 if ( fread_large(p->data_pointer(), p->alloc_size(), p->data_offset(), fimg) < 0 ) return -1;
110 if ( sb ) swapbytes(p->alloc_size(), p->data_pointer(), p->data_type_size());
111 }
112
113 fimg->close();
114 delete fimg;
115
116 return 0;
117 }
118
125 int writeGRD(Bimage *p)
126 {
127 p->color_to_simple();
128
129 if ( p->compound_type() == TComplex ) p->change_type(Float);
130
131 switch ( p->data_type() ) {
132 case Bit: case UCharacter: case SCharacter:
133 p->change_type(UCharacter); break;
134 default:
135 p->change_type(Float); break;
136 }
137
138 if ( verbose & VERB_DEBUG )
139 cout << "DEBUG writeGRD: header size=" << sizeof(GRDhead) << endl;
140
141 GRDhead* header = new GRDhead;
142 memset(header, 0, sizeof(GRDhead));
143
144 // Map the parameters
145 UnitCell uc = p->unit_cell();
146 header->nx = p->size()[0];
147 header->ny = p->size()[1];
148 header->nz = p->size()[2];
149 header->ux = p->sampling()[0];
150 header->uy = p->sampling()[1];
151 header->uz = p->sampling()[2];
152
153 switch ( systype(0) ) {
154 case BigIEEE: header->origp = 2; break; // MIPS, Motorola
155 case LittleIEEE: header->origp = 1; break; // Alpha, Intel
156 case LittleVAX: header->origp = 1; break; // VAX
157 default: break;
158 }
159 switch ( p->data_type() ) {
160 case UCharacter: header->mode = 1; break;
161 case Float:
162 switch ( p->compound_type() ) {
163 case TComplex: header->mode = 3; break;
164 case TVector3: header->mode = 4; break;
165 case TView: header->mode = 5; break;
166 default: header->mode = 2;
167 }
168 break;
169 default: header->mode = 1; break;
170 }
171
172 if ( verbose & VERB_DEBUG )
173 cout << "DEBUG writeGRD: mode=" << header->mode << endl;
174
175 header->doffset = 0;
176 p->data_offset(GRDSIZE);
177 header->fastest = 1;
178 header->medium = 2;
179 header->slowest = 3;
180 header->amin = p->minimum();
181 header->amax = p->maximum();
182 header->amean = p->average();
183 header->arms = p->standard_deviation();
184
185 header->a = uc.a();
186 header->b = uc.b();
187 header->c = uc.c();
188 header->alpha = uc.alpha()*180/M_PI;
189 header->beta = uc.beta()*180/M_PI;
190 header->gamma = uc.gamma()*180/M_PI;
191
192 header->bkg = p->background(long(0));
193 header->ox = p->image->origin()[0];
194 header->oy = p->image->origin()[1];
195 header->oz = p->image->origin()[2];
196 header->vx = p->image->view()[0];
197 header->vy = p->image->view()[1];
198 header->vz = p->image->view()[2];
199 header->va = p->image->view().angle();
200
201 if ( verbose & VERB_DEBUG )
202 cout << "DEBUG writeGRD: bkg=" << header->bkg << endl;
203
204 tm* t = p->get_localtime();
205 snprintf(header->datetime, 16, "%4d%02d%02d%02d%02d%02d", t->tm_year+1900,
206 t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
207
208 if ( verbose & VERB_DEBUG )
209 cout << "DEBUG writeGRD: datetime=" << header->datetime << endl;
210
211 strncpy(header->symmetry, p->symmetry().c_str(), 30);
212 strncpy(header->label, p->label().c_str(), 200);
213
214 if ( verbose & VERB_DEBUG )
215 cout << "DEBUG writeGRD: label=" << header->label << endl;
216
217 ofstream fimg(p->file_name());
218 if ( fimg.fail() ) return -1;
219
220 fimg.write((char *)header, p->data_offset());
221 if ( p->data_pointer() ) fimg.write((char *)p->data_pointer(), p->alloc_size());
222
223 fimg.close();
224
225 delete header;
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
SWAPTRIG
#define SWAPTRIG
Definition: rwimg.h:16
UnitCell
Definition: UnitCell.h:22
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
LittleIEEE
Definition: utilities.h:131
BigIEEE
Definition: utilities.h:129
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.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
TView
Definition: Bimage.h:61
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
UnitCell::beta
void beta(double d)
Definition: UnitCell.h:40
LittleVAX
Definition: utilities.h:133
GRDhead
Definition: rwGRD.h:15
readGRD
int readGRD(Bimage *p, int readdata)
Reading a Basel GRD map image file format.
Definition: rwGRD.cpp:31
SCharacter
Definition: utilities.h:150
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
UnitCell::a
void a(double d)
Definition: UnitCell.h:36
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
TComplex
Definition: Bimage.h:59
TVector3
Definition: Bimage.h:60
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
writeGRD
int writeGRD(Bimage *p)
Writing a Basel GRD map image file format.
Definition: rwGRD.cpp:125
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
M_PI
#define M_PI
Definition: utilities.h:59
Bimage::maximum
double maximum()
Definition: Bimage.h:535
Bimage::label
void label(string s)
Definition: Bimage.h:235
Bimage::set_time
void set_time(time_t t)
Definition: Bimage.h:249
Standard
Definition: Bimage.h:41
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)
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
GRDSIZE
#define GRDSIZE
Definition: rwGRD.h:13
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:537
Bimage::view
void view(double vx, double vy, double vz, double va)
Definition: Bimage.h:589
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
UnitCell::alpha
void alpha(double d)
Definition: UnitCell.h:39
UCharacter
Definition: utilities.h:149
verbose
int verbose
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::symmetry
string & symmetry()
Definition: Bimage.h:596
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
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
Bimage::images
long images()
Definition: Bimage.h:491
rwGRD.h
Header file for reading and writing Basel GRD files.
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:144
Bimage::background
double background(long nn)
Definition: Bimage.h:542
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
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