Bsoft  2.1.0
Bernard's software package
rwDX.cpp
Go to the documentation of this file.
1 
9 #include "rwDX.h"
10 #include <fstream>
11 
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14 
44 int readDX(Bimage* p, int readdata)
45 {
46  ifstream fimg;
47  fimg.open(p->file_name());
48  if ( fimg.fail() ) return -1;
49 
50  if ( verbose & VERB_DEBUG )
51  cout << "DEBUG readDX: Start reading text image" << endl;
52 
53  Bstring str, tag;
54  long i(0), j(0), n(0);
55  Vector3<long> size;
56  Vector3<double> ori, sam(1,1,1);
57 
58  // Set up default parameters
59  p->size(1, 1, 1);
60  p->images(1);
61  p->channels(1);
62 
63  p->data_type(Float);
64 
65  while ( !fimg.eof() ) {
66  fimg >> str;
67  if ( tag == "counts" && n < 1 ) {
68  if ( i < 3 ) size[i++] = str.integer();
69  else {
70  n = size[0]*size[1]*size[2];
71  p->size(size);
72  if ( readdata ) p->data_alloc();
73  if ( verbose & VERB_DEBUG )
74  cout << "DEBUG readDX: size=" << p->size() << endl;
75  }
76  }
77  if ( tag == "origin" && i < 3 )
78  ori[i++] = str.integer();
79  if ( tag == "delta" && i < 3 ) {
80  if ( i == j-1 ) sam[i] = str.real();
81 // cout << tag << tab << i << tab << j << endl;
82  i++;
83  }
84  if ( readdata && tag == "times" && i < n )
85  p->set(i++, str.real());
86  if ( str == "counts" || str == "origin" || str == "delta" || str == "times" ) {
87  tag = str;
88  i = 0;
89  if ( str == "delta" ) j++;
90  if ( str == "times" ) fimg >> str;
91  }
92  }
93 
94  p->origin(-ori);
95  p->sampling(sam);
96  if ( readdata ) {
97  if ( p->sizeZ() > 1 ) str = "zyx";
98  else if ( p->sizeY() > 1 ) str = "yxz";
99  p->reslice(str);
100  }
101 
102  fimg.close();
103 
104  return 0;
105 }
106 
113 {
114  ofstream fimg;
115  fimg.open(p->file_name());
116  if ( fimg.fail() ) return -1;
117 
118  long i, j, x, y, z, n = p->size()[0]*p->size()[1]*p->size()[2];
119 
120  fimg << "# Written by Bsoft" << endl;
121  fimg << "# " << p->label() << endl;
122 
123  fimg << "object 1 class gridpositions counts " << p->sizeX()
124  << " " << p->sizeY() << " " << p->sizeZ() << endl;
125  fimg << "origin " << -p->image->origin()[0]
126  << " " << -p->image->origin()[1] << " " << -p->image->origin()[2] << endl;
127  fimg << "delta " << p->sampling(0)[0] << " 0.0 0.0" << endl;
128  fimg << "delta 0.0 " << p->sampling(0)[1] << " 0.0" << endl;
129  fimg << "delta 0.0 0.0 " << p->sampling(0)[2] << endl;
130 
131  fimg << "object 2 class gridconnections counts " << p->sizeX()
132  << " " << p->sizeY() << " " << p->sizeZ() << endl;
133 
134  fimg << "object 3 class array type double rank 0 times " << n;
135  for ( j=x=0; x<p->sizeX(); x++ ) {
136  for ( y=0; y<p->sizeY(); y++ ) {
137  for ( z=0; z<p->sizeZ(); z++, j++ ) {
138  if ( j%3 ) fimg << " ";
139  else fimg << endl;
140  i = p->index(x,y,z);
141  fimg << (*p)[i];
142  }
143  }
144  }
145  fimg << endl;
146 
147  fimg << "attribute \"dep\" string \"positions\"" << endl;
148  fimg << "object \"regular positions regular connections\" class field" << endl;
149  fimg << "component \"positions\" value 1" << endl;
150  fimg << "component \"connections\" value 2" << endl;
151  fimg << "component \"data\" value 3" << endl;
152 
153  fimg << endl;
154 
155  fimg.close();
156 
157  return 0;
158 }
159 
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bimage::reslice
void reslice(const char *order)
Definition: Bimage.h:717
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bstring
Definition: Bstring.h:29
Vector3< long >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
verbose
int verbose
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::images
long images()
Definition: Bimage.h:504
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
rwDX.h
Header file for reading and writing OpenDX files.
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
writeDX
int writeDX(Bimage *p)
Writing a DX image format.
Definition: rwDX.cpp:112
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
readDX
int readDX(Bimage *p, int readdata)
Reading an OpenDX image format.
Definition: rwDX.cpp:44
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
Bimage::label
void label(string s)
Definition: Bimage.h:250