Commit f47168a2e02062a2a0f25d9c5df3c3149aa72a55
1 parent
2b10d15c
Ziqi added create, writeZ in binary class
Showing
1 changed file
with
35 additions
and
0 deletions
Show diff stats
envi/binary.h
... | ... | @@ -81,6 +81,41 @@ public: |
81 | 81 | return test_file_size(); |
82 | 82 | } |
83 | 83 | |
84 | + //crete a new binary file | |
85 | + bool create(std::string filename, vec<unsigned int, D> r, unsigned int offset = 0){ | |
86 | + | |
87 | + std::ofstream target(filename.c_str(), std::ios::binary); | |
88 | + | |
89 | + //initialize binary file | |
90 | + T p = 0; | |
91 | + for(unsigned int i =0; i < r[0] * r[1] * r[2]; i++){ | |
92 | + target.write((char*)(&p), sizeof(T)); | |
93 | + } | |
94 | + | |
95 | + for(unsigned int i = 0; i < D; i++) //set the dimensions of the binary file object | |
96 | + R[i] = r[i]; | |
97 | + | |
98 | + header = offset; //save the header size | |
99 | + | |
100 | + if(!open_file(filename)) return false; //open the binary file | |
101 | + | |
102 | + return test_file_size(); | |
103 | + } | |
104 | + | |
105 | + //save one band from the memory to the file | |
106 | + bool writeZ( T * p, unsigned int page){ | |
107 | + | |
108 | + if(p == NULL){ | |
109 | + std::cout<<"ERROR: unable to write into file, empty pointer"<<std::endl; | |
110 | + exit(1); | |
111 | + } | |
112 | + | |
113 | + file.seekg(R[1] * R[0] * page * sizeof(T), std::ios::beg); //write into memory from the binary file | |
114 | + file.write((char *)p, R[0] * R[1] * sizeof(T)); | |
115 | + | |
116 | + return true; | |
117 | + } | |
118 | + | |
84 | 119 | //save one band of the file into the memory, and return the pointer |
85 | 120 | bool saveZ( T * p, unsigned int page){ |
86 | 121 | ... | ... |