Bsoft  2.1.0
Bernard's software package
rwDSN6.cpp
Go to the documentation of this file.
1 
9 #include "rwDSN6.h"
10 #include "utilities.h"
11 
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14 
27 int readDSN6(Bimage* p, int readdata)
28 {
29  ifstream* fimg = new ifstream;
30  fimg->open(p->file_name());
31  if ( fimg->fail() ) return -1;
32 
33  if ( verbose & VERB_DEBUG )
34  cout << "DEBUG readDSN6: File opened" << endl;
35 
36  DSN6head* header = new DSN6head;
37 
38  fimg->read((char *)header, DSN6SIZE);
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;
44 // int sb = 0;
45  if ( header->scale > 255 && header->cell_scale > 255 ) {
46  if ( verbose & VERB_PROCESS )
47  cerr << "Warning: Swapping header byte order for 2-byte types" << endl;
48 // sb = 2;
49  for ( i=0; i<40; i+=2 ) swapbytes(b+i, 2);
50  }
51 
52  if ( header->x_extent < 1 || header->y_extent < 1 || header->z_extent < 1 ) {
53  cerr << "Error readDSN6: The file " << p->file_name() << " is not a valid DSN6 image!" << endl;
54  fimg->close();
55  delete fimg;
56  return -3;
57  }
58 
59  // Map the parameters
60  p->size(header->x_extent, header->y_extent, header->z_extent);
61  p->images(1);
62  p->channels(1);
65 
66  UnitCell uc(header->a*1.0/header->cell_scale, header->b*1.0/header->cell_scale,
67  header->c*1.0/header->cell_scale, header->alpha*1.0/header->cell_scale,
68  header->beta*1.0/header->cell_scale, header->gamma*1.0/header->cell_scale);
69 
70  p->unit_cell(uc);
71 
72  if ( header->x_sampling && header->y_sampling && header->z_sampling )
73  p->sampling(uc.a()/header->x_sampling, uc.b()/header->y_sampling, uc.c()/header->z_sampling);
74 
75  // Allocating the single sub-image and setting its origin
76  p->origin(-header->x_start, -header->y_start, -header->z_start);
77 
78  float min = (3 - header->plus)*header->scale*1.0/header->product;
79  float max = (253 - header->plus)*header->scale*1.0/header->product;
80 
81  if ( verbose & VERB_DEBUG )
82  cout << "DEBUG readDSN6: min=" << min << " max=" << max << endl;
83 
84  delete header;
85 
86  if ( verbose & VERB_DEBUG )
87  cout << "DEBUG rwDSN6: Memory freed" << endl;
88 
89  p->page_size(8, 8, 8); // Page size = one brick
90 
91  if ( readdata)
92  p->read_data( fimg, -1, 2, 0, 0 );
93 
94  p->minimum(0);
95  p->maximum(255);
96 
97  fimg->close();
98  delete fimg;
99 
100  if ( verbose & VERB_DEBUG )
101  cout << "DEBUG rwDSN6: Data read" << endl;
102 
103  return 0;
104 }
105 
113 {
114  p->color_to_simple();
115 
116  float min = p->minimum();
117  float max = p->maximum();
118 
120 
121  if ( verbose & VERB_DEBUG )
122  cout << "DEBUG rwDSN6: Writing DSN6 file" << endl;
123 
124  DSN6head* header = new DSN6head;
125  memset(header, 0, sizeof(DSN6head));
126 
127  // Map the parameters
128  UnitCell uc = p->unit_cell();
129  header->x_extent = p->sizeX();
130  header->y_extent = p->sizeY();
131  header->z_extent = p->sizeZ();
132  header->x_start = (short) (-p->image[0].origin()[0] - 0.5);
133  header->y_start = (short) (-p->image[0].origin()[1] - 0.5);
134  header->z_start = (short) (-p->image[0].origin()[2] - 0.5);
135  header->x_sampling = (short) (uc.a()/p->sampling(0)[0] + 0.5);
136  header->y_sampling = (short) (uc.b()/p->sampling(0)[1] + 0.5);
137  header->z_sampling = (short) (uc.c()/p->sampling(0)[2] + 0.5);
138 
139  header->cell_scale = 100;
140  header->a = (short) (uc.a()*header->cell_scale + 0.5);
141  header->b = (short) (uc.b()*header->cell_scale + 0.5);
142  header->c = (short) (uc.c()*header->cell_scale + 0.5);
143  header->alpha = (short) (uc.alpha()*header->cell_scale*180/M_PI + 0.5);
144  header->beta = (short) (uc.beta()*header->cell_scale*180/M_PI + 0.5);
145  header->gamma = (short) (uc.gamma()*header->cell_scale*180/M_PI + 0.5);
146 
147  header->scale = 100;
148  header->product = (short) (header->scale*(253 - 3)/(max - min) + 0.5);
149  header->plus = (short) ((3*max - 253*min)/(max - min) + 0.5);
150 
151  if ( verbose & VERB_DEBUG )
152  cout << "DEBUG writeDSN6: product=" << header->product << " plus=" << header->plus << endl;
153 
154  p->data_offset(DSN6SIZE);
155  p->page_size(8, 8, 8); // Page size = one brick
156 
157  unsigned char* b = (unsigned char *) header;
158  long i, j, x, y, z, px, py, pz;
159 // int sb = 0;
160  long datatypesize = p->data_type_size();
161  long valuesize = datatypesize*p->channels();
162  long pagesize = p->page_size()[0]*p->page_size()[1]*p->page_size()[2]*valuesize;
163  unsigned char* data = p->data_pointer();
164  unsigned char* page = new unsigned char[pagesize];
165  memset(page, 0, pagesize);
166 
167  if ( systype(0) == LittleIEEE ) {
168 // sb = 1;
169  for ( i=0; i<40; i+=2 ) swapbytes(b+i, 2);
170  if ( verbose & VERB_PROCESS )
171  cout << "Swapping byte pairs for the DSN6 format" << endl;
172  }
173 
174  ofstream fimg(p->file_name().c_str());
175  if ( fimg.fail() ) return -1;
176 
177  fimg.write((char *)header, DSN6SIZE);
178 
179  for ( pz=0; pz<p->sizeZ(); pz+=p->page_size()[2] )
180  for ( py=0; py<p->sizeY(); py+=p->page_size()[1] )
181  for ( px=0; px<p->sizeX(); px+=p->page_size()[0] ) {
182  for ( x=0; x<p->page_size()[0] && x<p->sizeX()-px; x++ )
183  for ( y=0; y<p->page_size()[1] && y<p->sizeY()-py; y++ )
184  for ( z=0; z<p->page_size()[2] && z<p->sizeZ()-pz; z++ ) {
185  i = (((z+pz)*p->sizeY() + y+py)*p->sizeX() + x+px)*valuesize;
186  j = ((z*p->page_size()[1] + y)*p->page_size()[0] + x)*valuesize;
187  memcpy(page+j, data+i, valuesize);
188  }
189 // if ( sb ) for ( i=0; i<pagesize; i+=2 ) swapbytes(page+i, 2);
190  for ( i=0; i<pagesize; i+=2 ) swapbytes(page+i, 2);
191  fimg.write((char *)page, pagesize);
192  }
193 
194  fimg.close();
195 
196  delete[] page;
197 
198  delete header;
199 
200  return 0;
201 }
202 
DSN6head::y_sampling
short y_sampling
Definition: rwDSN6.h:23
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
readDSN6
int readDSN6(Bimage *p, int readdata)
Reading a DSN6 map image file format.
Definition: rwDSN6.cpp:27
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
UCharacter
@ UCharacter
Definition: utilities.h:153
DSN6head::x_start
short x_start
Definition: rwDSN6.h:16
UnitCell::c
void c(double d)
Definition: UnitCell.h:38
DSN6head::z_extent
short z_extent
Definition: rwDSN6.h:21
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:151
DSN6head::x_extent
short x_extent
Definition: rwDSN6.h:19
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:493
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:608
DSN6head
Definition: rwDSN6.h:15
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
UnitCell::b
void b(double d)
Definition: UnitCell.h:37
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1607
DSN6head::y_start
short y_start
Definition: rwDSN6.h:17
DSN6head::plus
short plus
Definition: rwDSN6.h:32
DSN6head::alpha
short alpha
Definition: rwDSN6.h:28
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::images
long images()
Definition: Bimage.h:504
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
DSN6head::x_sampling
short x_sampling
Definition: rwDSN6.h:22
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:468
DSN6head::scale
short scale
Definition: rwDSN6.h:34
DSN6head::z_start
short z_start
Definition: rwDSN6.h:18
Bimage::data_offset
long data_offset()
Definition: Bimage.h:288
Bimage
General image parameter class.
Definition: Bimage.h:177
DSN6head::beta
short beta
Definition: rwDSN6.h:29
UnitCell::alpha
void alpha(double d)
Definition: UnitCell.h:39
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
UnitCell::a
void a(double d)
Definition: UnitCell.h:36
LittleIEEE
@ LittleIEEE
Definition: utilities.h:135
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
writeDSN6
int writeDSN6(Bimage *p)
Writing a DSN6 map image file format.
Definition: rwDSN6.cpp:112
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
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:2207
rwDSN6.h
Header file for reading and writing DSN6 files.
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
DSN6SIZE
#define DSN6SIZE
Definition: rwDSN6.h:13
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
DSN6head::y_extent
short y_extent
Definition: rwDSN6.h:20
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
DSN6head::z_sampling
short z_sampling
Definition: rwDSN6.h:24
Bimage::minimum
double minimum()
Definition: Bimage.h:541
UnitCell::beta
void beta(double d)
Definition: UnitCell.h:40
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
UnitCell
Definition: UnitCell.h:22
DSN6head::c
short c
Definition: rwDSN6.h:27
verbose
int verbose
DSN6head::a
short a
Definition: rwDSN6.h:25
DSN6head::b
short b
Definition: rwDSN6.h:26
Bimage::channels
long channels()
Definition: Bimage.h:384
DSN6head::gamma
short gamma
Definition: rwDSN6.h:30
DSN6head::product
short product
Definition: rwDSN6.h:31
DSN6head::cell_scale
short cell_scale
Definition: rwDSN6.h:33
UnitCell::gamma
void gamma(double d)
Definition: UnitCell.h:41