| Bsoft 2.0.5 Bernard's software package

rwRAW.cpp
Go to the documentation of this file.
1
12 #include "rwRAW.h"
13 #include "utilities.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
44 int readRAW(Bimage* p, int img_select)
45 {
46 // Get the part of the file name with the specifications
47 // Bstring specs(p->file_name().post('#'));
48 Bstring specs(p->file_name().substr(p->file_name().find("#")+1));
49 // string specs(p->file_name().substr(p->file_name().find("#")+1));
50 // transform(specs.begin(), specs.end(), specs.begin(), ::tolower);
51
52 // Get the real filename without specifications
53 // p->file_name(p->file_name().pre('#'));
54 p->file_name(p->file_name().substr(0, p->file_name().find('#')));
55
56 if ( verbose & VERB_DEBUG ) {
57 cout << "DEBUG rwRAW: Filename = " << p->file_name() << endl;
58 cout << "DEBUG rwRAW: Specs = " << specs << endl;
59 }
60
61 // Set defaults
62 p->data_offset(0);
63 long i(0), offset(0), pad(0), c(1), n(1);
64 int swap(0), vax(0);
65 char transform_type('n');
66 Vector3<long> size(1,1,1), pagesize(1,1,1);
67 Vector3<double> sam(1,1,1);
68
69 // Extract the parameters from the filename
70 Bstring item, value;
71 for ( specs = specs.lower(); specs.contains("="); specs = specs.post('#') ) {
72 item = specs.pre('#');
73 value = item.post('=');
74 if ( verbose & VERB_DEBUG )
75 cout << "DEBUG rwRAW: item=" << item << " value=" << value << endl;
76 if ( item[0] == 'h' ) sscanf(value.c_str(), "%ld", &offset);
77 if ( item[0] == 'd' ) p->data_type(getdatatype(value[0]));
78 if ( item[0] == 'x' ) sscanf(value.c_str(), "%ld,%ld,%ld", &size[0], &size[1], &size[2]);
79 if ( item[0] == 'p' ) sscanf(value.c_str(), "%ld,%ld,%ld", &pagesize[0], &pagesize[1], &pagesize[2]);
80 if ( item[0] == 's' ) i = sscanf(value.c_str(), "%lf,%lf,%lf", &sam[0], &sam[1], &sam[2]);
81 if ( item[0] == 'c' ) sscanf(value.c_str(), "%ld", &c);
82 if ( item[0] == 'n' ) sscanf(value.c_str(), "%ld", &n);
83 if ( item[0] == 'i' ) sscanf(value.c_str(), "%d", &img_select);
84 if ( item[0] == 'f' ) sscanf(value.c_str(), "%c", &transform_type);
85 if ( item[0] == 'b' ) sscanf(value.c_str(), "%d", &swap);
86 if ( item[0] == 'v' ) sscanf(value.c_str(), "%d", &vax);
87 if ( item[0] == 'a' ) sscanf(value.c_str(), "%ld", &pad);
88 }
89 if ( i == 1 ) {
90 if ( size[1] > 1 ) sam[1] = sam[0];
91 if ( size[2] > 1 ) sam[2] = sam[0];
92 }
93
94 p->data_offset(offset);
95 p->channels(c);
96 // p->images(n);
97
98 switch ( transform_type ) {
99 case 'n': p->fourier_type(NoTransform); break;
100 case 's': p->fourier_type(Standard); break;
101 case 'c': p->fourier_type(Centered); break;
102 case 'h': p->fourier_type(Hermitian); break;
103 case 'q': p->fourier_type(CentHerm); break;
104 }
105
106 if ( verbose & VERB_DEBUG ) {
107 cout << "DEBUG rwRAW: Specs: offset=" << p->data_offset() << " data type=" << p->data_type() << endl;
108 cout << "DEBUG rwRAW: size=" << size << endl;
109 cout << "DEBUG rwRAW: channels=" << p->channels() << "images=" << n << endl;
110 cout << "DEBUG rwRAW: transform=" << transform_type << " swap=" << swap << " vax=" << vax << " pad=" << pad << endl;
111 }
112
113 // Check parameters
114 p->compound_type(TSimple);
115 if ( p->channels() == 2 ) p->compound_type(TComplex);
116 if ( p->channels() == 3 ) p->compound_type(TRGB);
117 if ( p->channels() == 4 ) p->compound_type(TRGBA);
118 if ( pad ) pagesize[2] = 1;
119
120 // If only half of a transform is stored, it need to be handled
121 long xstore = size[0];
122 if ( p->fourier_type() == Hermitian || p->fourier_type() == CentHerm )
123 xstore = size[0]/2 + 1;
124
125 if ( p->data_type() == Bit ) {
126 xstore = (size[0] - 1)/8 + 1;
127 pagesize[0] = 8*xstore;
128 }
129
130 ifstream* fimg = new ifstream;
131 fimg->open(p->file_name());
132 if ( fimg->fail() ) return -1;
133
134 if ( verbose & VERB_DEBUG )
135 cout << "DEBUG rwRAW: File opened" << endl;
136
137 fimg->seekg(0, ios_base::end);
138 long len = fimg->tellg();
139 if ( verbose & VERB_DEBUG )
140 cout << "DEBUG rwRAW: File length = " << len << endl;
141
142 long datasize = len - p->data_offset();
143 long datatypesize = p->data_type_size();
144 long specsize = xstore*size[1]*size[2]*p->channels()*n*datatypesize;
145
146 if ( specsize > datasize ) {
147 cout << "File size=" << datasize << " < spec size=" << specsize << " (" <<
148 xstore << "," << size[1] << "," << size[2] << "," << p->channels() << "," << n << "," << datatypesize << ")" << endl;
149 p->channels(1);
150 size[2] = 1;
151 size[0] = xstore = (long) sqrt(1.0*datasize);
152 if ( p->fourier_type() == Hermitian || p->fourier_type() == CentHerm )
153 size[0] = 2*(xstore - 1);
154 size[1] = datasize/size[0];
155 p->data_type(UCharacter);
156 cout << "Assume a 2D image with 8 bit data of size " << p->sizeX() << "x" << p->sizeY() << endl;
157 }
158
159 if ( pagesize[0] < 2 ) pagesize[0] = size[0];
160 if ( pagesize[1] < 2 ) pagesize[1] = size[1];
161
162 if ( img_select > -1 ) {
163 if ( img_select >= n ) img_select = n - 1;
164 n = 1;
165 } else img_select = -1;
166
167 p->images(n);
168
169 p->size(size);
170 p->page_size(pagesize);
171 p->sampling(sam);
172
173
174 p->read_data(fimg, img_select, swap, vax, pad);
175
176 if ( verbose & VERB_DEBUG )
177 cout << "DEBUG rwRAW: File read" << endl;
178
179 fimg->close();
180
181 delete fimg;
182
183 return 0;
184 }
185
192 int writeRAW(Bimage* p)
193 {
194 long datasize = p->alloc_size();
195
196 ofstream fimg(p->file_name());
197 if ( fimg.fail() ) return -1;
198
199 fimg.write((char *)p->data_pointer(), datasize);
200
201 fimg.close();
202
203 return 0;
204 }
205
getdatatype
DataType getdatatype(char letter)
Get the data type indicated by a single letter code.
Definition: utilities.cpp:288
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:394
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1585
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:2188
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
TComplex
Definition: Bimage.h:59
verbose
int verbose
TSimple
Definition: Bimage.h:58
Bimage
General image parameter class.
Definition: Bimage.h:163
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
rwRAW.h
Header file for reading and writing Raw files.
CentHerm
Definition: Bimage.h:44
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:381
Standard
Definition: Bimage.h:41
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bimage::data_offset
long data_offset()
Definition: Bimage.h:265
readRAW
int readRAW(Bimage *p, int img_select)
Reading a raw block of data in a file.
Definition: rwRAW.cpp:44
Bimage::channels
long channels()
Definition: Bimage.h:371
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
TRGB
Definition: Bimage.h:62
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
UCharacter
Definition: utilities.h:149
Bstring
Definition: Bstring.h:29
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bit
Definition: utilities.h:148
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:252
Vector3< long >
Bstring::lower
Bstring lower()
Definition: Bstring.cpp:308
TRGBA
Definition: Bimage.h:63
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:480
Centered
Definition: Bimage.h:42
Bimage::images
long images()
Definition: Bimage.h:491
NoTransform
Definition: Bimage.h:40
Hermitian
Definition: Bimage.h:43
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
writeRAW
int writeRAW(Bimage *p)
Writing a raw block of data in a file.
Definition: rwRAW.cpp:192
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:367