binary.h
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//make sure that this header file is only loaded once
#ifndef RTS_BINARY_H
#define RTS_BINARY_H
#include "../envi/envi_header.h"
#include "../math/vector.h"
#include <fstream>
#include <sys/stat.h>
namespace stim{
/** This class manages the streaming of large multidimensional binary files.
* Generally these are hyperspectral files with 2 spatial and 1 spectral dimension. However, this class supports
* other dimensions via the template parameter D.
*
* @param T is the data type used to store data to disk (generally float or double)
* @param D is the dimension of the data (default 3)
*/
template< typename T, unsigned int D = 3 >
class binary{
protected:
std::fstream file; //file stream used for reading and writing
std::string name; //file name
unsigned long long int R[D]; //resolution
unsigned int header; //header size (in bytes)
unsigned char* mask; //pointer to a character array: 0 = background, 1 = foreground (or valid data)
/// Private initialization function used to set default parameters in the data structure.
void init(){
memset(R, 0, sizeof(unsigned int) * D); //initialize the resolution to zero
header = 0; //initialize the header size to zero
mask = NULL;
}
/// Private helper function that returns the size of the file on disk using system functions.
long long int get_file_size(){
#ifdef _WIN32
struct _stat64 results;
if(_stat64(name.c_str(), &results) == 0)
return results.st_size;
#else
struct stat results;
if(stat(name.c_str(), &results) == 0)
return results.st_size;
#endif
else return 0;
}
/// Private helper function that tests to make sure that the calculated data size specified by the structure is the same as the data size on disk.
bool test_file_size(){
long long int npts = 1; //initialize the number of data points to 1
for(unsigned int i = 0; i<D; i++) //iterate over each dimension
npts *= R[i]; //compute the total number of data points in the file
long long int datasize = npts * sizeof(T);//multiply the sum by the size of the template parameter
if(datasize + header == get_file_size()) return true; //if the byte size matches the file size, we're golden
else return false; //otherwise return an error
}
/// Private helper file that opens a specified binary file.
/// @param filename is the name of the binary file to stream
bool open_file(std::string filename){
//open the file as binary for reading and writing
file.open(filename.c_str(), std::ios::in | std::ios::out | std::ios::binary);
//if the file is successful
if(file){
name = filename; //set the name
if(test_file_size()) //test the file size
return true;
}
return false;
}
public:
/// Open a binary file for streaming.
/// @param filename is the name of the binary file
/// @param r is a STIM vector specifying the size of the binary file along each dimension
/// @param h is the length (in bytes) of any header file (default zero)
bool open(std::string filename, vec<unsigned int, D> r, unsigned int h = 0){
for(unsigned int i = 0; i < D; i++) //set the dimensions of the binary file object
R[i] = r[i];
header = h; //save the header size
if(!open_file(filename)) return false; //open the binary file
return test_file_size();
}
/// Creates a new binary file for streaming
/// @param filename is the name of the binary file to be created
/// @param r is a STIM vector specifying the size of the file along each dimension
/// @offset specifies how many bytes to offset the file (used to leave room for a header)
bool create(std::string filename, vec<unsigned int, D> r, unsigned int offset = 0){
std::ofstream target(filename.c_str(), std::ios::binary);
//initialize binary file
T p = 0;
for(unsigned int i =0; i < r[0] * r[1] * r[2]; i++){
target.write((char*)(&p), sizeof(T));
}
for(unsigned int i = 0; i < D; i++) //set the dimensions of the binary file object
R[i] = r[i];
header = offset; //save the header size
if(!open_file(filename)) return false; //open the binary file
return test_file_size();
}
/// Writes a single page of data to disk. A page consists of a sequence of data of size R[0] * R[1] * ... * R[D-1].
/// @param p is a pointer to the data to be written
/// @param page is the page number (index of the highest-numbered dimension)
bool write_page( T * p, unsigned int page){
if(p == NULL){
std::cout<<"ERROR: unable to write into file, empty pointer"<<std::endl;
exit(1);
}
file.seekg(R[1] * R[0] * page * sizeof(T), std::ios::beg); //seek to the desired location on disk
file.write((char *)p, R[0] * R[1] * sizeof(T)); //write binary data
return true;
}
/// Reads a page from disk. A page consists of a sequence of data of size R[0] * R[1] * ... * R[D-1].
/// @param p is a pointer to pre-allocated memory equal to the page size
/// @param page is the index of the page
bool read_page( T * p, unsigned int page){
if (page >= R[2]){ //make sure the bank number is right
std::cout<<"ERROR: page out of range"<<std::endl;
return false;
}
file.seekg(R[1] * R[0] * page * sizeof(T), std::ios::beg); //write into memory from the binary file
file.read((char *)p, R[0] * R[1] * sizeof(T));
return true;
}
///Reads a line Z (slowest dimension) for a given XY value
/// @param p is a pointer to pre-allocated memory equal to the line size R[2]
/// @param x is the x coordinate
/// @param y is the y coordinate
bool read_line_01( T * p, unsigned int x, unsigned int y){
unsigned int i;
if ( x >= R[0] || y >= R[1]){ //make sure the sample and line number is right
std::cout<<"ERROR: sample or line out of range"<<std::endl;
return false;
}
file.seekg((x + y * R[0]) * sizeof(T), std::ios::beg); //point to the certain sample and line
for (i = 0; i < R[2]; i++)
{
file.read((char *)(p + i), sizeof(T));
file.seekg((R[1] * R[0] - 1) * sizeof(T), std::ios::cur); //go to the next band
}
return true;
}
///Reads a line X (fastest dimension) for a given YZ value
/// @param p is a pointer to pre-allocated memory equal to the line size R[2]
/// @param x is the y coordinate
/// @param y is the z coordinate
bool read_line_12(T * p, unsigned int y, unsigned int z){
//test to make sure the specified value is within range
if( y >= R[1] || z >= R[2] ){
std::cout<<"ERROR: sample or line out of range"<<std::endl;
return false;
}
file.seekg((z * R[0] * R[1] + y * R[0]) * sizeof(T), std::ios::beg); //seek to the start of the line
file.read((char *)p, sizeof(T) * R[0]); //read the line
return true;
}
///Reads a line Y (second fastest dimension) for a given XZ value
/// @param p is a pointer to pre-allocated memory equal to the line size R[2]
/// @param x is the y coordinate
/// @param z is the z coordinate
bool read_line_02(T * p, unsigned int x, unsigned int z){
//test to make sure the specified value is within range
if( x >= R[0] || z >= R[2] ){
std::cout<<"ERROR: sample or line out of range"<<std::endl;
return false;
}
file.seekg((z * R[0] * R[1] + x) * sizeof(T), std::ios::beg); //seek to the start of the line
for (unsigned int i = 0; i < R[1]; i++){ //for each pixel in the line
file.read((char *)(p + i), sizeof(T)); //read the pixel
file.seekg((R[0] - 1) * sizeof(T), std::ios::cur); //seek to the next pixel in the line
}
return true;
}
/// Reads a plane given a coordinate along the 0-axis (YZ plane)
/// @param p is a pointer to pre-allocated memory of size R[1] * R[2] * sizeof(T)
/// @param n is the 0-axis coordinate used to retrieve the plane
bool read_plane_0(T* p, unsigned int n){
if (n >= R[0]){ //make sure the number is within the possible range
std::cout<<"ERROR read_plane_0: page out of range"<<std::endl;
return false;
}
unsigned int jump = (R[0] - 1) * sizeof(T); //number of bytes to skip between samples
//seek to the start of the plane
file.seekg(n * sizeof(T), std::ios::beg);
unsigned int N = R[1] * R[2];
for(unsigned int i = 0; i<N; i++){
file.read((char*)(p+i), sizeof(T));
file.seekg(jump, std::ios::cur);
}
return true;
}
/// Reads a plane given a coordinate along the 1-axis (XZ plane)
/// @param p is a pointer to pre-allocated memory of size R[0] * R[2] * sizeof(T)
/// @param n is the 1-axis coordinate used to retrieve the plane
bool read_plane_1(T* p, unsigned int n){
unsigned int L = R[0] * sizeof(T); //caculate the number of bytes in a sample line
unsigned int jump = R[0] * (R[1] - 1) * sizeof(T);
if (n >= R[1]){ //make sure the bank number is right
std::cout<<"ERROR read_plane_1: page out of range"<<std::endl;
return false;
}
file.seekg(R[0] * n * sizeof(T), std::ios::beg);
for (unsigned i = 0; i < R[2]; i++){
file.read((char *)(p + i * R[0]), L);
file.seekg( jump, std::ios::cur);
std::cout<<i<<" ";
}
return true;
}
/// Reads a plane given a coordinate along the 2-axis (XY plane)
/// @param p is a pointer to pre-allocated memory of size R[0] * R[1] * sizeof(T)
/// @param n is the 2-axis coordinate used to retrieve the plane
bool read_plane_2(T* p, unsigned int n){
return read_page(p, n);
}
/// Reads a single pixel, treating the entire data set as a linear array
/// @param p is a pointer to pre-allocated memory of size sizeof(T)
/// @param i is the index to the pixel using linear indexing
bool read_pixel(T* p, unsigned int i){
if(i >= R[0] * R[1] * R[2]){
std::cout<<"ERROR read_pixel: n is out of range"<<std::endl;
return false;
}
file.seekg(i * sizeof(T), std::ios::cur);
file.read((char*)p, sizeof(T));
}
/// Reads a single pixel, given an x, y, z coordinate
/// @param p is a pointer to pre-allocated memory of size sizeof(T)
/// @param x is the x (0) axis coordinate
/// @param y is the y (1) axis coordinate
/// @param z is the z (2) axis coordinate
bool read_pixel(T* p, unsigned int x, unsigned int y, unsigned int z){
if(x < 0 || x >= R[0] || y < 0 || y >= R[1] || z < 0 || z > R[2]){
std::cout<<"ERROR read_pixel: (x,y,z) is out of range"<<std::endl;
return false;
}
unsigned int i = z * R[0] * R[1] + y * R[0] + z;
return read_pixel(p, i);
}
};
}
#endif