Bsoft  2.1.0
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 
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 
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);
132  }
133  if ( strstr(header->type,"COMP") ) {
134  p->data_type(Float); // Complex transform data
135  p->compound_type(TComplex); p->channels(2);
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 
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  int* rt = (int *) machine_stamp;
268  switch ( systype(0) ) {
269  case BigIEEE:
270  for ( i=0; i<4; i++ ) machine_stamp[i] = 4;
271  break;
272  case LittleIEEE:
273  for ( i=0; i<4; i++ ) machine_stamp[i] = 2;
274  break;
275  case LittleVAX:
276  machine_stamp[0] = 1;
277  break;
278  default:
279  break;
280  }
281 // header->realtype = *((int *) machine_stamp);
282  header->realtype = *rt;
283 
284  // Fill in the file header
285  header->nhfr = 1;
286  header->npix2 = p->sizeX()*p->sizeY();
287  header->npixel = p->sizeX()*p->sizeY();
288  header->iylp = p->sizeX();
289  header->ixlp = p->sizeY();
290  header->izlp = p->sizeZ();
291  header->i4lp = p->images();
292 
293  header->resolx = p->sampling(0)[0];
294  header->resoly = p->sampling(0)[1];
295  header->resolz = p->sampling(0)[2];
296 
297  tm* t = p->get_localtime();
298  header->ndate = t->tm_mday;
299  header->nmonth = t->tm_mon + 1;
300  header->nyear = t->tm_year + 1900;
301  header->nhour = t->tm_hour;
302  header->nminut = t->tm_min;
303  header->nsec = t->tm_sec;
304 
305  switch( p->data_type() ){
306  case UCharacter : strncpy(header->type,"PACK", 4); break;
307  case Short : strncpy(header->type,"INTG", 4); break;
308  case Float : strncpy(header->type,"REAL", 4);
309  if ( p->compound_type() == TComplex ) strncpy(header->type,"RECO", 4);
310  if ( p->fourier_type() >= Standard ) strncpy(header->type,"COMP", 4); break;
311  default : strncpy(header->type,"PACK", 4);
312  }
313 
314  header->densmin = p->minimum();
315  header->densmax = p->maximum();
316  header->avdens = header->oldavd = p->average();
317  header->sigma = p->standard_deviation();
318  header->varian = p->standard_deviation()*p->standard_deviation();
319 
320  strncpy(header->name, p->file_name().c_str(), 80);
321  strncpy(header->history, p->label().c_str(), 228);
322 
323  // get the filename without extension to find the header file
324  string filename(p->file_name());
325  string headername = replace_extension(filename, "hed");
326  filename = replace_extension(filename, "img");
327  p->file_name(filename);
328 
329  if ( verbose & VERB_LABEL )
330  cout << "Writing header file: " << headername << endl;
331 
332  ofstream fhed(headername);
333  if ( fhed.fail() ) return -1;
334 
335  for ( n=0; n<p->images(); n++ ) {
336  header->imn = n + 1; // Image number
337  header->ifn = p->images() - n - 1; // Number of images following this one
338  header->iyold = (int) -(p->image[n].origin()[0] + 0.5);
339  header->ixold = (int) -(p->image[n].origin()[1] + 0.5);
340  header->nxstart = (int) -(p->image[n].origin()[0] + 0.5);
341  header->nystart = (int) -(p->image[n].origin()[1] + 0.5);
342  header->nzstart = (int) -(p->image[n].origin()[2] + 0.5);
343  header->densmin = p->image[n].minimum();
344  header->densmax = p->image[n].maximum();
345  header->avdens = header->oldavd = p->image[n].average();
346  header->sigma = p->image[n].standard_deviation();
347  header->varian = p->image[n].standard_deviation()*p->image[n].standard_deviation();
348  imagic_euler_from_view(header, p->image[n].view());
349  fhed.write((char *)header, IMAGICSIZE);
350  }
351 
352  fhed.close();
353 
354  delete header;
355 
356  if ( !p->data_pointer() ) return -2;
357 
358  ofstream fimg(p->file_name());
359  if ( fimg.fail() ) return -3;
360 
361  long datasize = p->alloc_size();
362  unsigned char* data = NULL;
363 
364  if ( p->compound_type() < TComplex ) {
365  fimg.write((char *)p->data_pointer(), datasize);
366  } else {
367  data = new unsigned char[datasize];
368  p->pack_transform(data, Centered);
369  fimg.write((char *)data, datasize);
370  delete[] data;
371  }
372 
373  fimg.close();
374 
375  return 0;
376 }
377 
Bsub_image::average
double average()
Definition: Bimage.h:119
IMAGIChead::ixold
int ixold
Definition: rwIMAGIC.h:31
IMAGIChead::densmax
float densmax
Definition: rwIMAGIC.h:37
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
IMAGIChead::ndate
int ndate
Definition: rwIMAGIC.h:20
IMAGIChead::sigma
float sigma
Definition: rwIMAGIC.h:34
IMAGIChead::imn
int imn
Definition: rwIMAGIC.h:16
Bimage::get_localtime
tm * get_localtime()
Definition: Bimage.h:260
Bit
@ Bit
Definition: utilities.h:152
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
IMAGIChead::nhfr
int nhfr
Definition: rwIMAGIC.h:19
IMAGICSIZE
#define IMAGICSIZE
Definition: rwIMAGIC.h:13
UCharacter
@ UCharacter
Definition: utilities.h:153
file_util.h
Header file for file checking functions.
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
Bsub_image::standard_deviation
double standard_deviation()
Definition: Bimage.h:120
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:151
IMAGIChead::type
char type[4]
Definition: rwIMAGIC.h:30
Bimage::set_time
void set_time(time_t t)
Definition: Bimage.h:264
IMAGIChead::beta
float beta
Definition: rwIMAGIC.h:62
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
rwIMAGIC.h
Header file for reading and writing Image Science's Imagic files.
IMAGIChead::iylp
int iylp
Definition: rwIMAGIC.h:29
IMAGIChead::name
char name[80]
Definition: rwIMAGIC.h:45
IMAGIChead::realtype
int realtype
Definition: rwIMAGIC.h:65
TComplex
@ TComplex
Definition: Bimage.h:60
IMAGIChead::izlp
int izlp
Definition: rwIMAGIC.h:57
IMAGIChead::iyold
int iyold
Definition: rwIMAGIC.h:32
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
sb
int sb(0)
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1607
View
Definition: View.h:26
IMAGIChead::nminut
int nminut
Definition: rwIMAGIC.h:24
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::maximum
double maximum()
Definition: Bimage.h:542
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:558
Standard
@ Standard
Definition: Bimage.h:42
IMAGIChead::varian
float varian
Definition: rwIMAGIC.h:35
IMAGIChead::avdens
float avdens
Definition: rwIMAGIC.h:33
Bimage::images
long images()
Definition: Bimage.h:504
IMAGIChead::resolz
float resolz
Definition: rwIMAGIC.h:93
Bsub_image::maximum
double maximum()
Definition: Bimage.h:118
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Short
@ Short
Definition: utilities.h:156
TSimple
@ TSimple
Definition: Bimage.h:59
writeIMAGIC
int writeIMAGIC(Bimage *p)
Writing an IMAGIC image format. A file format for the IMAGIC package.
Definition: rwIMAGIC.cpp:246
verbose
int verbose
Bimage::pack_transform
int pack_transform(unsigned char *data, FourierType tf)
Definition: Bimage.cpp:2441
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:267
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:468
Bimage::data_offset
long data_offset()
Definition: Bimage.h:288
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
UShort
@ UShort
Definition: utilities.h:155
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
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:1371
IMAGIChead::resoly
float resoly
Definition: rwIMAGIC.h:92
IMAGIChead::nsec
int nsec
Definition: rwIMAGIC.h:25
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
view_from_imagic_euler
View view_from_imagic_euler(IMAGIChead *header)
Definition: rwIMAGIC.cpp:16
LittleIEEE
@ LittleIEEE
Definition: utilities.h:135
BigIEEE
@ BigIEEE
Definition: utilities.h:133
IMAGIChead::ixlp
int ixlp
Definition: rwIMAGIC.h:28
Centered
@ Centered
Definition: Bimage.h:43
IMAGIChead::gamma
float gamma
Definition: rwIMAGIC.h:63
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
IMAGIChead::nystart
int nystart
Definition: rwIMAGIC.h:52
IMAGIChead::nzstart
int nzstart
Definition: rwIMAGIC.h:53
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
IMAGIChead::oldavd
float oldavd
Definition: rwIMAGIC.h:36
utilities.h
Header file for general utilities.
IMAGIChead::nmonth
int nmonth
Definition: rwIMAGIC.h:21
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
IMAGIChead::i4lp
int i4lp
Definition: rwIMAGIC.h:58
replace_extension
string replace_extension(string &filename, string ext)
Definition: string_util.cpp:41
SWAPTRIG
#define SWAPTRIG
Definition: rwimg.h:16
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
IMAGIChead::ifn
int ifn
Definition: rwIMAGIC.h:17
IMAGIChead::nhour
int nhour
Definition: rwIMAGIC.h:23
LittleVAX
@ LittleVAX
Definition: utilities.h:137
imagic_euler_from_view
int imagic_euler_from_view(IMAGIChead *header, View view)
Definition: rwIMAGIC.cpp:31
SCharacter
@ SCharacter
Definition: utilities.h:154
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
IMAGIChead::npixel
int npixel
Definition: rwIMAGIC.h:27
IMAGIChead::densmin
float densmin
Definition: rwIMAGIC.h:38
Bimage::minimum
double minimum()
Definition: Bimage.h:541
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
IMAGIChead::nxstart
int nxstart
Definition: rwIMAGIC.h:51
IMAGIChead::resolx
float resolx
Definition: rwIMAGIC.h:91
IMAGIChead::npix2
int npix2
Definition: rwIMAGIC.h:26
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bsub_image::minimum
double minimum()
Definition: Bimage.h:117
Bimage::unpack_transform
int unpack_transform(unsigned char *data, FourierType tf)
Definition: Bimage.cpp:2356
IMAGIChead::nyear
int nyear
Definition: rwIMAGIC.h:22
IMAGIChead::history
char history[228]
Definition: rwIMAGIC.h:100
NoTransform
@ NoTransform
Definition: Bimage.h:41
IMAGIChead
Definition: rwIMAGIC.h:15
IMAGIChead::alpha
float alpha
Definition: rwIMAGIC.h:61
Bimage::label
void label(string s)
Definition: Bimage.h:250