| Bsoft 2.0.5 Bernard's software package

rwSitus.cpp
Go to the documentation of this file.
1
9 #include "rwSitus.h"
10 #include <fstream>
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
27 int readSitus(Bimage* p, int readdata)
28 {
29 ifstream fimg;
30 fimg.open(p->file_name());
31 if ( fimg.fail() ) return -1;
32
33 if ( verbose & VERB_DEBUG )
34 cout << "DEBUG readSitus: Start reading text image" << endl;
35
36 size_t i(0), n(0);
37 double v;
38 Vector3<double> ori;
39 Vector3<long> size;
40
41 fimg >> v;
42 if ( verbose & VERB_DEBUG )
43 cout << "DEBUG readSitus: Pixel size = " << v << endl;
44 fimg >> ori[0] >> ori[1] >> ori[2];
45 if ( verbose & VERB_DEBUG )
46 cout << "DEBUG readSitus: Origin = " << -ori << endl;
47 fimg >> size[0] >> size[1] >> size[2];
48 if ( verbose & VERB_DEBUG )
49 cout << "DEBUG readSitus: Size = " << size << endl;
50
51 p->size(size);
52 p->images(1);
53 p->channels(1);
54 p->data_type(Float);
55 p->sampling(v,v,v);
56 p->origin(-ori);
57 p->data_alloc();
58
59 if ( readdata) {
60 n = (size_t) size[0]*size[1]*size[2];
61
62 while ( !fimg.eof() && i < n ) {
63 fimg >> v;
64 if ( fabs(v) < SMALLFLOAT ) v = 0;
65 p->set(i++, v);
66 }
67
68 if ( i < n )
69 cerr << "Error: The data read (" << i << ") is less than the size specified (" << n << ")" << endl;
70 }
71
72 fimg.close();
73
74 return 0;
75 }
76
82 int writeSitus(Bimage* p)
83 {
84 ofstream fimg;
85 fimg.open(p->file_name());
86 if ( fimg.fail() ) return -1;
87
88 long i;
89
90 fimg << p->sampling()[0];
91 for ( i=0; i<3; i++ ) fimg << " " << -p->image->origin()[i];
92 for ( i=0; i<3; i++ ) fimg << " " << p->size()[i];
93 fimg << endl;
94
95 for ( i=0; i<p->data_size(); i++ ) {
96 if ( i%10 ) fimg << " ";
97 else fimg << endl;
98 fimg << (*p)[i];
99 }
100
101 fimg << endl;
102
103 fimg.close();
104
105 return 0;
106 }
107
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:45
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
Float
Definition: utilities.h:157
writeSitus
int writeSitus(Bimage *p)
Writing a Situs image format.
Definition: rwSitus.cpp:82
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
verbose
int verbose
rwSitus.h
Header file for reading and writing Situs files.
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bimage::channels
long channels()
Definition: Bimage.h:371
readSitus
int readSitus(Bimage *p, int readdata)
Reading a Situs image format.
Definition: rwSitus.cpp:27
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
Vector3< double >
Bimage::images
long images()
Definition: Bimage.h:491
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::data_size
long data_size()
Definition: Bimage.h:255
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563