| Bsoft 2.0.5 Bernard's software package

rwIMAGIC.cpp
Go to the documentation of this file.
1
9 #include "rwIMAGIC.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 View view_from_imagic_euler(IMAGIChead* header)
17 {
18 View view;
19
20 view[0] = cos(header->gamma)*sin(header->beta);
21 view[1] = sin(header->gamma)*sin(header->beta);
22 view[2] = cos(header->beta);
23 view[3] = angle_set_negPI_to_PI(header->alpha + header->gamma);
24
25 if ( verbose & VERB_DEBUG )
26 cout << "DEBUG view_from_imagic_euler: " << view << endl;
27
28 return view;
29 }
30
31 int imagic_euler_from_view(IMAGIChead* header, View view)
32 {
33 if ( acos(view[2]) > 1e-14 )
34 header->gamma = atan2(view[1], view[0]);
35 header->beta = acos(view[2]);
36 header->alpha = view[3] - header->gamma;
37 header->alpha = angle_set_negPI_to_PI(header->alpha);
38
39 return 0;
40 }
41
65 int readIMAGIC(Bimage* p, int readdata, int img_select)
66 {
67 // get the filename without extension to find the header file
68 string filename(p->file_name());
69 string headername = replace_extension(filename, "hed");
70 filename = replace_extension(filename, "img");
71 p->file_name(filename);
72
73 if ( verbose & VERB_LABEL )
74 cout << "Reading header file: " << headername << endl;
75
76 // open and read the header file
77 ifstream* fhed = new ifstream(headername);
78 if ( fhed->fail() ) return -1;
79
80 IMAGIChead* header = new IMAGIChead;
81
82 fhed->read((char *)header, IMAGICSIZE);
83 if ( fhed->fail() ) return -2;
84
85 if ( verbose & VERB_DEBUG )
86 cout << "DEBUG readIMAGIC: sizeof(IMAGIChead)=" << sizeof(IMAGIChead) << " IMAGICSIZE=" << IMAGICSIZE << endl;
87
88 // Determine byte order and swap bytes if from little-endian machine
89 unsigned char* b = (unsigned char *) header;
90 int sb = 0;
91 long i, extent = IMAGICSIZE - 916; // exclude char bytes from swapping
92 if ( ( abs(header->nyear) > SWAPTRIG ) || ( header->ixlp > SWAPTRIG ) ) {
93 if ( verbose & VERB_PROCESS )
94 cerr << "Warning: Swapping header byte order for 4-byte types" << endl;
95 sb = 1;
96 for ( i=0; i<extent; i+=4 )
97 if ( i != 56 ) // exclude type string
98 swapbytes(b+i, 4);
99 }
100
101 // Map the parameters
102 long nimg(0);
103 if ( header->i4lp < 1 ) { // This deals with the old header
104 p->size(header->iylp, header->ixlp, 1);
105 nimg = header->ifn + 1;
106 } else {
107 p->size(header->iylp, header->ixlp, header->izlp);
108 nimg = header->i4lp;
109 }
110
111 p->channels(1);
112
113 long j(0), n(0), imgstart(0), imgend(nimg-1);
114 double min(1e37), max(-1e37), sum(0), ssum(0);
115 unsigned char* hend;
116 if ( img_select > -1 ) {
117 if ( img_select >= nimg ) img_select = nimg - 1;
118 p->images(1);
119 imgstart = imgend = img_select;
120 } else {
121 img_select = -1;
122 p->images(nimg);
123 }
124
125 if ( strstr(header->type,"PACK") ) p->data_type(UCharacter);
126 if ( strstr(header->type,"INTG") ) p->data_type(Short);
127 if ( strstr(header->type,"REAL") ) p->data_type(Float);
128 if ( strstr(header->type,"RECO") ) {
129 p->data_type(Float); // Complex data
130 p->compound_type(TComplex); p->channels(2);
131 p->fourier_type(NoTransform);
132 }
133 if ( strstr(header->type,"COMP") ) {
134 p->data_type(Float); // Complex transform data
135 p->compound_type(TComplex); p->channels(2);
136 p->fourier_type(Centered);
137 }
138
139 p->sampling(header->resolx, header->resoly, header->resolz);
140
141 // Set min-max values and other statistical values
142 if ( header->sigma == 0 && header->varian != 0 )
143 header->sigma = sqrt(header->varian);
144 if ( header->densmax == 0 && header->densmin == 0 && header->sigma != 0 ) {
145 header->densmin = header->avdens - header->sigma;
146 header->densmax = header->avdens + header->sigma;
147 }
148 p->minimum(header->densmin);
149 p->maximum(header->densmax);
150 p->average(header->avdens);
151 p->standard_deviation(header->sigma);
152
153 p->label(header->history);
154
155 tm* t = p->get_localtime();
156 t->tm_mday = header->ndate;
157 t->tm_mon = header->nmonth - 1;
158 t->tm_year = header->nyear - 1900;
159 t->tm_hour = header->nhour;
160 t->tm_min = header->nminut;
161 t->tm_sec = header->nsec;
162 p->set_time(t);
163
164
165 // Get the sub-image information
166
167 fhed->seekg(0, ios::beg);
168 for ( i=n=0; i<nimg; i++ ) {
169 fhed->read((char *)header, IMAGICSIZE);
170 if ( fhed->fail() ) return -3;
171 if ( img_select < 0 || img_select == i ) {
172 hend = (unsigned char *) header + extent;
173 if ( sb ) for ( b = (unsigned char *) header; b<hend; b+=4 ) swapbytes(b, 4);
174 if ( header->sigma == 0 && header->varian != 0 )
175 header->sigma = sqrt(header->varian);
176 if ( header->densmax == 0 && header->densmin == 0 && header->sigma != 0 ) {
177 header->densmin = header->avdens - header->sigma;
178 header->densmax = header->avdens + header->sigma;
179 }
180 p->image[j].minimum(header->densmin);
181 p->image[j].maximum(header->densmax);
182 p->image[j].average(header->avdens);
183 p->image[j].standard_deviation(header->sigma);
184 // p->image[j].ox = -header->iyold;
185 // p->image[j].oy = -header->ixold;
186 p->image[j].origin(-header->nxstart, -header->nystart, -header->nzstart); // New header
187 p->image[j].view(view_from_imagic_euler(header));
188 j++;
189 if ( min > header->densmin ) min = header->densmin;
190 if ( max < header->densmax ) max = header->densmax;
191 sum += header->avdens;
192 ssum += header->sigma*header->sigma;
193 n++;
194 }
195 }
196
197 p->minimum(min);
198 p->maximum(max);
199 p->average(sum/n);
200 p->standard_deviation(sqrt(ssum/n));
201
202 fhed->close();
203 delete fhed;
204
205 delete header;
206
207 if ( !readdata ) return 0;
208
209 ifstream* fimg = new ifstream(p->file_name());
210 if ( fimg->fail() ) return -1;
211
212 long pagesize = p->channels()*p->sizeX()*p->sizeY()*p->sizeZ()*p->data_type_size();
213 long offset(p->data_offset() + imgstart*pagesize);
214 unsigned char* data = NULL;
215
216 if ( readdata ) {
217 p->data_alloc();
218 if ( p->compound_type() == TSimple ) {
219 for ( i=imgstart, data=p->data_pointer(); i<=imgend; i++, data+=pagesize, offset+=pagesize ) {
220 fread_large(data, pagesize, offset, fimg);
221 }
222 if ( sb ) swapbytes(p->alloc_size(), p->data_pointer(), p->data_type_size());
223 } else {
224 data = new unsigned char[pagesize];
225 for ( i=imgstart; i<=imgend; i++, offset+=pagesize ) {
226 fread_large(data, pagesize, offset, fimg);
227 if ( sb ) swapbytes(pagesize, data, p->data_type_size());
228 p->unpack_transform(i-imgstart, data, Centered);
229 }
230 delete[] data;
231 }
232 }
233
234 fimg->close();
235 delete fimg;
236
237 return 0;
238 }
239
246 int writeIMAGIC(Bimage* p)
247 {
248 p->color_to_simple();
249
250 if ( p->compound_type() == TComplex ) p->change_type(Float);
251
252 switch ( p->data_type() ) {
253 case Bit: case UCharacter: case SCharacter:
254 p->change_type(UCharacter); break;
255 case UShort: case Short:
256 p->change_type(Short); break;
257 default:
258 p->change_type(Float); break;
259 }
260
261 IMAGIChead* header = new IMAGIChead;
262 memset(header, 0, sizeof(IMAGIChead));
263
264 // Machine stamp
265 long i, n;
266 char machine_stamp[4] = {0,0,0,0};
267 switch ( systype(0) ) {
268 case BigIEEE:
269 for ( i=0; i<4; i++ ) machine_stamp[i] = 4;
270 break;
271 case LittleIEEE:
272 for ( i=0; i<4; i++ ) machine_stamp[i] = 2;
273 break;
274 case LittleVAX:
275 machine_stamp[0] = 1;
276 break;
277 default:
278 break;
279 }
280 header->realtype = *((int *) machine_stamp);
281
282 // Fill in the file header
283 header->nhfr = 1;
284 header->npix2 = p->sizeX()*p->sizeY();
285 header->npixel = p->sizeX()*p->sizeY();
286 header->iylp = p->sizeX();
287 header->ixlp = p->sizeY();
288 header->izlp = p->sizeZ();
289 header->i4lp = p->images();
290
291 header->resolx = p->sampling()[0];
292 header->resoly = p->sampling()[1];
293 header->resolz = p->sampling()[2];
294
295 tm* t = p->get_localtime();
296 header->ndate = t->tm_mday;
297 header->nmonth = t->tm_mon + 1;
298 header->nyear = t->tm_year + 1900;
299 header->nhour = t->tm_hour;
300 header->nminut = t->tm_min;
301 header->nsec = t->tm_sec;
302
303 switch( p->data_type() ){
304 case UCharacter : strncpy(header->type,"PACK", 4); break;
305 case Short : strncpy(header->type,"INTG", 4); break;
306 case Float : strncpy(header->type,"REAL", 4);
307 if ( p->compound_type() == TComplex ) strncpy(header->type,"RECO", 4);
308 if ( p->fourier_type() >= Standard ) strncpy(header->type,"COMP", 4); break;
309 default : strncpy(header->type,"PACK", 4);
310 }
311
312 header->densmin = p->minimum();
313 header->densmax = p->maximum();
314 header->avdens = header->oldavd = p->average();
315 header->sigma = p->standard_deviation();
316 header->varian = p->standard_deviation()*p->standard_deviation();
317
318 strncpy(header->name, p->file_name().c_str(), 80);
319 strncpy(header->history, p->label().c_str(), 228);
320
321 // get the filename without extension to find the header file
322 string filename(p->file_name());
323 string headername = replace_extension(filename, "hed");
324 filename = replace_extension(filename, "img");
325 p->file_name(filename);
326
327 if ( verbose & VERB_LABEL )
328 cout << "Writing header file: " << headername << endl;
329
330 ofstream fhed(headername);
331 if ( fhed.fail() ) return -1;
332
333 for ( n=0; n<p->images(); n++ ) {
334 header->imn = n + 1; // Image number
335 header->ifn = p->images() - n - 1; // Number of images following this one
336 header->iyold = (int) -(p->image[n].origin()[0] + 0.5);
337 header->ixold = (int) -(p->image[n].origin()[1] + 0.5);
338 header->nxstart = (int) -(p->image[n].origin()[0] + 0.5);
339 header->nystart = (int) -(p->image[n].origin()[1] + 0.5);
340 header->nzstart = (int) -(p->image[n].origin()[2] + 0.5);
341 header->densmin = p->image[n].minimum();
342 header->densmax = p->image[n].maximum();
343 header->avdens = header->oldavd = p->image[n].average();
344 header->sigma = p->image[n].standard_deviation();
345 header->varian = p->image[n].standard_deviation()*p->image[n].standard_deviation();
346 imagic_euler_from_view(header, p->image[n].view());
347 fhed.write((char *)header, IMAGICSIZE);
348 }
349
350 fhed.close();
351
352 delete header;
353
354 if ( !p->data_pointer() ) return -2;
355
356 ofstream fimg(p->file_name());
357 if ( fimg.fail() ) return -3;
358
359 long datasize = p->alloc_size();
360 unsigned char* data = NULL;
361
362 if ( p->compound_type() < TComplex ) {
363 fimg.write((char *)p->data_pointer(), datasize);
364 } else {
365 data = new unsigned char[datasize];
366 p->pack_transform(data, Centered);
367 fimg.write((char *)data, datasize);
368 delete[] data;
369 }
370
371 fimg.close();
372
373 return 0;
374 }
375
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
verbose
int verbose
IMAGIChead::resolx
float resolx
Definition: rwIMAGIC.h:91
IMAGIChead::npix2
int npix2
Definition: rwIMAGIC.h:26
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:143
IMAGIChead::iyold
int iyold
Definition: rwIMAGIC.h:32
IMAGIChead::ixold
int ixold
Definition: rwIMAGIC.h:31
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
LittleIEEE
Definition: utilities.h:131
IMAGIChead::ndate
int ndate
Definition: rwIMAGIC.h:20
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
IMAGIChead::sigma
float sigma
Definition: rwIMAGIC.h:34
IMAGIChead::alpha
float alpha
Definition: rwIMAGIC.h:61
utilities.h
Header file for general utilities.
IMAGIChead::type
char type[4]
Definition: rwIMAGIC.h:30
IMAGIChead::nhfr
int nhfr
Definition: rwIMAGIC.h:19
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:594
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
IMAGIChead::iylp
int iylp
Definition: rwIMAGIC.h:29
LittleVAX
Definition: utilities.h:133
IMAGICSIZE
#define IMAGICSIZE
Definition: rwIMAGIC.h:13
IMAGIChead::realtype
int realtype
Definition: rwIMAGIC.h:65
IMAGIChead::varian
float varian
Definition: rwIMAGIC.h:35
IMAGIChead::nminut
int nminut
Definition: rwIMAGIC.h:24
IMAGIChead::beta
float beta
Definition: rwIMAGIC.h:62
IMAGIChead::resolz
float resolz
Definition: rwIMAGIC.h:93
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
imagic_euler_from_view
int imagic_euler_from_view(IMAGIChead *header, View view)
Definition: rwIMAGIC.cpp:31
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
IMAGIChead::name
char name[80]
Definition: rwIMAGIC.h:45
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
IMAGIChead::izlp
int izlp
Definition: rwIMAGIC.h:57
IMAGIChead::avdens
float avdens
Definition: rwIMAGIC.h:33
IMAGIChead::ixlp
int ixlp
Definition: rwIMAGIC.h:28
replace_extension
string replace_extension(string &filename, string ext)
Definition: string_util.cpp:41
Bimage::maximum
double maximum()
Definition: Bimage.h:535
view_from_imagic_euler
View view_from_imagic_euler(IMAGIChead *header)
Definition: rwIMAGIC.cpp:16
IMAGIChead::oldavd
float oldavd
Definition: rwIMAGIC.h:36
IMAGIChead::nsec
int nsec
Definition: rwIMAGIC.h:25
IMAGIChead::nmonth
int nmonth
Definition: rwIMAGIC.h:21
Bimage::label
void label(string s)
Definition: Bimage.h:235
Bimage::set_time
void set_time(time_t t)
Definition: Bimage.h:249
IMAGIChead::i4lp
int i4lp
Definition: rwIMAGIC.h:58
Standard
Definition: Bimage.h:41
sb
int sb(0)
IMAGIChead::nystart
int nystart
Definition: rwIMAGIC.h:52
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
IMAGIChead::nhour
int nhour
Definition: rwIMAGIC.h:23
file_util.h
Header file for file checking functions.
Bsub_image::maximum
double maximum()
Definition: Bimage.h:114
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
Bimage::unpack_transform
int unpack_transform(unsigned char *data, FourierType tf)
Definition: Bimage.cpp:2337
IMAGIChead::densmin
float densmin
Definition: rwIMAGIC.h:38
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:537
IMAGIChead::resoly
float resoly
Definition: rwIMAGIC.h:92
IMAGIChead::npixel
int npixel
Definition: rwIMAGIC.h:27
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
rwIMAGIC.h
Header file for reading and writing Image Science's Imagic files.
Bsub_image::minimum
double minimum()
Definition: Bimage.h:113
IMAGIChead::gamma
float gamma
Definition: rwIMAGIC.h:63
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
readIMAGIC
int readIMAGIC(Bimage *p, int readdata, int img_select)
Reading an IMAGIC image format. A 2D file format for the IMAGIC package. The header is stored in a se...
Definition: rwIMAGIC.cpp:65
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:252
IMAGIChead::nzstart
int nzstart
Definition: rwIMAGIC.h:53
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
IMAGIChead::nxstart
int nxstart
Definition: rwIMAGIC.h:51
View
Definition: View.h:26
IMAGIChead::history
char history[228]
Definition: rwIMAGIC.h:100
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
Short
Definition: utilities.h:152
Bsub_image::average
double average()
Definition: Bimage.h:115
UShort
Definition: utilities.h:151
Centered
Definition: Bimage.h:42
Bimage::images
long images()
Definition: Bimage.h:491
IMAGIChead::ifn
int ifn
Definition: rwIMAGIC.h:17
NoTransform
Definition: Bimage.h:40
IMAGIChead::imn
int imn
Definition: rwIMAGIC.h:16
IMAGIChead::nyear
int nyear
Definition: rwIMAGIC.h:22
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:144
IMAGIChead::densmax
float densmax
Definition: rwIMAGIC.h:37
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
writeIMAGIC
int writeIMAGIC(Bimage *p)
Writing an IMAGIC image format. A file format for the IMAGIC package.
Definition: rwIMAGIC.cpp:246
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::minimum
double minimum()
Definition: Bimage.h:534
Bsub_image::standard_deviation
double standard_deviation()
Definition: Bimage.h:116
IMAGIChead
Definition: rwIMAGIC.h:15
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:367
Bimage::image
Bsub_image * image
Definition: Bimage.h:188