Commit f92397d251bb523e5de835f4117573eaa5a8acc2
1 parent
945ee13c
fixed a Win32 bug in stim::filename, added support for non-zero headers in stim:…
…:binary and stim::envi
Showing
3 changed files
with
73 additions
and
39 deletions
Show diff stats
stim/envi/binary.h
@@ -66,6 +66,12 @@ protected: | @@ -66,6 +66,12 @@ protected: | ||
66 | 66 | ||
67 | } | 67 | } |
68 | 68 | ||
69 | + /// Private helper function that resets the file pointer to the beginning of the data | ||
70 | + | ||
71 | + void reset(){ | ||
72 | + file.seekg(header, std::ios_base::beg); | ||
73 | + } | ||
74 | + | ||
69 | /// Private helper file that opens a specified binary file. | 75 | /// Private helper file that opens a specified binary file. |
70 | 76 | ||
71 | /// @param filename is the name of the binary file to stream | 77 | /// @param filename is the name of the binary file to stream |
@@ -89,12 +95,14 @@ protected: | @@ -89,12 +95,14 @@ protected: | ||
89 | if(test_file_size()) //test the file size | 95 | if(test_file_size()) //test the file size |
90 | return true; | 96 | return true; |
91 | } | 97 | } |
92 | - | 98 | + |
93 | return false; | 99 | return false; |
94 | } | 100 | } |
95 | 101 | ||
96 | 102 | ||
97 | 103 | ||
104 | + | ||
105 | + | ||
98 | public: | 106 | public: |
99 | 107 | ||
100 | unsigned int get_thread_data(){ | 108 | unsigned int get_thread_data(){ |
@@ -119,6 +127,8 @@ public: | @@ -119,6 +127,8 @@ public: | ||
119 | 127 | ||
120 | if(!open_file(filename)) return false; //open the binary file | 128 | if(!open_file(filename)) return false; //open the binary file |
121 | 129 | ||
130 | + //reset(); | ||
131 | + | ||
122 | return test_file_size(); | 132 | return test_file_size(); |
123 | } | 133 | } |
124 | 134 | ||
@@ -158,7 +168,7 @@ public: | @@ -158,7 +168,7 @@ public: | ||
158 | exit(1); | 168 | exit(1); |
159 | } | 169 | } |
160 | 170 | ||
161 | - file.seekg(R[1] * R[0] * page * sizeof(T), std::ios::beg); //seek to the desired location on disk | 171 | + file.seekg(R[1] * R[0] * page * sizeof(T) + header, std::ios::beg); //seek to the desired location on disk |
162 | file.write((char *)p, R[0] * R[1] * sizeof(T)); //write binary data | 172 | file.write((char *)p, R[0] * R[1] * sizeof(T)); //write binary data |
163 | 173 | ||
164 | return true; | 174 | return true; |
@@ -175,7 +185,7 @@ public: | @@ -175,7 +185,7 @@ public: | ||
175 | return false; | 185 | return false; |
176 | } | 186 | } |
177 | 187 | ||
178 | - file.seekg(R[1] * R[0] * page * sizeof(T), std::ios::beg); //write into memory from the binary file | 188 | + file.seekg(R[1] * R[0] * page * sizeof(T) + header, std::ios::beg); //write into memory from the binary file |
179 | file.read((char *)p, R[0] * R[1] * sizeof(T)); | 189 | file.read((char *)p, R[0] * R[1] * sizeof(T)); |
180 | 190 | ||
181 | return true; | 191 | return true; |
@@ -220,7 +230,7 @@ public: | @@ -220,7 +230,7 @@ public: | ||
220 | 230 | ||
221 | file.seekg((z * R[0] * R[1] + y * R[0]) * sizeof(T), std::ios::beg); //seek to the start of the line | 231 | file.seekg((z * R[0] * R[1] + y * R[0]) * sizeof(T), std::ios::beg); //seek to the start of the line |
222 | file.read((char *)p, sizeof(T) * R[0]); //read the line | 232 | file.read((char *)p, sizeof(T) * R[0]); //read the line |
223 | - | 233 | + |
224 | return true; | 234 | return true; |
225 | } | 235 | } |
226 | 236 |
stim/envi/envi.h
@@ -15,7 +15,7 @@ namespace stim{ | @@ -15,7 +15,7 @@ namespace stim{ | ||
15 | files are stored on disk as a large binary file with a corresponding header. Code for reading and processing | 15 | files are stored on disk as a large binary file with a corresponding header. Code for reading and processing |
16 | ENVI header files is in the envi_header class. | 16 | ENVI header files is in the envi_header class. |
17 | */ | 17 | */ |
18 | -class envi{ | 18 | +class envi{ |
19 | 19 | ||
20 | void* file; //void pointer to the relevant file reader (bip, bsq, or bil - with appropriate data type) | 20 | void* file; //void pointer to the relevant file reader (bip, bsq, or bil - with appropriate data type) |
21 | 21 | ||
@@ -23,6 +23,14 @@ public: | @@ -23,6 +23,14 @@ public: | ||
23 | 23 | ||
24 | envi_header header; | 24 | envi_header header; |
25 | 25 | ||
26 | + /// Returns the size of the data type in bytes | ||
27 | + unsigned int type_size(){ | ||
28 | + if(header.data_type == envi_header::float32) return 4; | ||
29 | + if(header.data_type == envi_header::float64) return 8; | ||
30 | + | ||
31 | + exit(1); | ||
32 | + } | ||
33 | + | ||
26 | /// Returns the progress of the current processing operation as a percentage | 34 | /// Returns the progress of the current processing operation as a percentage |
27 | void reset_progress(){ | 35 | void reset_progress(){ |
28 | 36 | ||
@@ -123,17 +131,14 @@ public: | @@ -123,17 +131,14 @@ public: | ||
123 | 131 | ||
124 | } | 132 | } |
125 | 133 | ||
126 | - /// Open an existing ENVI file given the file and header names. | 134 | + /// Open an existing ENVI file given the filename and a header structure |
127 | 135 | ||
128 | /// @param filename is the name of the ENVI binary file | 136 | /// @param filename is the name of the ENVI binary file |
129 | - /// @param headername is the name of the ENVI header file | ||
130 | - bool open(std::string filename, std::string headername){ | ||
131 | - | ||
132 | - //allocate memory | 137 | + /// @param header is an ENVI header structure |
138 | + bool open(std::string filename, stim::envi_header h){ | ||
133 | allocate(); | 139 | allocate(); |
134 | 140 | ||
135 | - //load the header | ||
136 | - header.load(headername); | 141 | + header = h; |
137 | 142 | ||
138 | //load the file | 143 | //load the file |
139 | if(header.interleave == envi_header::BSQ) { //if the infile is bsq file | 144 | if(header.interleave == envi_header::BSQ) { //if the infile is bsq file |
@@ -157,7 +162,7 @@ public: | @@ -157,7 +162,7 @@ public: | ||
157 | else | 162 | else |
158 | return false; | 163 | return false; |
159 | } | 164 | } |
160 | - | 165 | + |
161 | else if(header.interleave == envi_header::BIP) { //if the infile is bip file | 166 | else if(header.interleave == envi_header::BIP) { //if the infile is bip file |
162 | if(header.data_type == envi_header::float32) { | 167 | if(header.data_type == envi_header::float32) { |
163 | return ((bip<float>*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); | 168 | return ((bip<float>*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); |
@@ -166,14 +171,30 @@ public: | @@ -166,14 +171,30 @@ public: | ||
166 | return ((bip<double>*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); | 171 | return ((bip<double>*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); |
167 | } | 172 | } |
168 | else | 173 | else |
169 | - return false; | ||
170 | - } | ||
171 | - | ||
172 | - else{ | ||
173 | - std::cout<<"ERROR: unidentified type file "<<headername<<std::endl; | ||
174 | - exit(1); | 174 | + return false; |
175 | } | 175 | } |
176 | 176 | ||
177 | + return true; | ||
178 | + | ||
179 | + } | ||
180 | + | ||
181 | + /// Open an existing ENVI file given the file and header names. | ||
182 | + | ||
183 | + /// @param filename is the name of the ENVI binary file | ||
184 | + /// @param headername is the name of the ENVI header file | ||
185 | + bool open(std::string filename, std::string headername){ | ||
186 | + | ||
187 | + //allocate memory | ||
188 | + //allocate(); | ||
189 | + | ||
190 | + stim::envi_header h; | ||
191 | + h.load(headername); | ||
192 | + | ||
193 | + //load the header | ||
194 | + //header.load(headername); | ||
195 | + | ||
196 | + return open(filename, h); | ||
197 | + | ||
177 | } | 198 | } |
178 | 199 | ||
179 | /// Normalize a hyperspectral ENVI file given a band number and threshold. | 200 | /// Normalize a hyperspectral ENVI file given a band number and threshold. |
@@ -182,7 +203,7 @@ public: | @@ -182,7 +203,7 @@ public: | ||
182 | /// @param band is the band label to be output | 203 | /// @param band is the band label to be output |
183 | /// @param threshold is a threshold value specified such that normalization will only be done to values in the band > threshold (preventing division by small numbers) | 204 | /// @param threshold is a threshold value specified such that normalization will only be done to values in the band > threshold (preventing division by small numbers) |
184 | bool normalize(std::string outfile, double band, double threshold = 0.0){ | 205 | bool normalize(std::string outfile, double band, double threshold = 0.0){ |
185 | - | 206 | + |
186 | if(header.interleave == envi_header::BSQ){ //if the infile is bsq file | 207 | if(header.interleave == envi_header::BSQ){ //if the infile is bsq file |
187 | if(header.data_type ==envi_header::float32) | 208 | if(header.data_type ==envi_header::float32) |
188 | return ((bsq<float>*)file)->normalize(outfile, band, threshold); | 209 | return ((bsq<float>*)file)->normalize(outfile, band, threshold); |
@@ -245,7 +266,7 @@ public: | @@ -245,7 +266,7 @@ public: | ||
245 | } | 266 | } |
246 | } | 267 | } |
247 | 268 | ||
248 | - else if(header.interleave == envi_header::BIP){ //if the infile is bip file | 269 | + else if(header.interleave == envi_header::BIP){ //if the infile is bip file |
249 | if(header.data_type ==envi_header::float32) | 270 | if(header.data_type ==envi_header::float32) |
250 | return ((bip<float>*)file)->baseline(outfile, w); | 271 | return ((bip<float>*)file)->baseline(outfile, w); |
251 | else if(header.data_type == envi_header::float64) | 272 | else if(header.data_type == envi_header::float64) |
@@ -291,10 +312,10 @@ public: | @@ -291,10 +312,10 @@ public: | ||
291 | else if(interleave == envi_header::BIP) | 312 | else if(interleave == envi_header::BIP) |
292 | return ((bsq<double>*)file)->bip(outfile); | 313 | return ((bsq<double>*)file)->bip(outfile); |
293 | } | 314 | } |
294 | - | 315 | + |
295 | else{ | 316 | else{ |
296 | std::cout<<"ERROR: unidentified data type"<<std::endl; | 317 | std::cout<<"ERROR: unidentified data type"<<std::endl; |
297 | - exit(1); | 318 | + exit(1); |
298 | } | 319 | } |
299 | } | 320 | } |
300 | 321 | ||
@@ -321,15 +342,15 @@ public: | @@ -321,15 +342,15 @@ public: | ||
321 | else if(interleave == envi_header::BIP) | 342 | else if(interleave == envi_header::BIP) |
322 | return ((bil<double>*)file)->bip(outfile); | 343 | return ((bil<double>*)file)->bip(outfile); |
323 | } | 344 | } |
324 | - | 345 | + |
325 | else{ | 346 | else{ |
326 | std::cout<<"ERROR: unidentified data type"<<std::endl; | 347 | std::cout<<"ERROR: unidentified data type"<<std::endl; |
327 | - exit(1); | 348 | + exit(1); |
328 | } | 349 | } |
329 | } | 350 | } |
330 | 351 | ||
331 | else if(header.interleave == envi_header::BIP){ | 352 | else if(header.interleave == envi_header::BIP){ |
332 | - | 353 | + |
333 | if(header.data_type ==envi_header::float32){ //if the data type of infile is float | 354 | if(header.data_type ==envi_header::float32){ //if the data type of infile is float |
334 | if(interleave == envi_header::BIP){ | 355 | if(interleave == envi_header::BIP){ |
335 | std::cout<<"ERROR: is already BIP file"<<std::endl; | 356 | std::cout<<"ERROR: is already BIP file"<<std::endl; |
@@ -351,19 +372,19 @@ public: | @@ -351,19 +372,19 @@ public: | ||
351 | else if(interleave == envi_header::BSQ) //if the target file is bsq file | 372 | else if(interleave == envi_header::BSQ) //if the target file is bsq file |
352 | return ((bip<double>*)file)->bsq(outfile); | 373 | return ((bip<double>*)file)->bsq(outfile); |
353 | } | 374 | } |
354 | - | 375 | + |
355 | else{ | 376 | else{ |
356 | std::cout<<"ERROR: unidentified data type"<<std::endl; | 377 | std::cout<<"ERROR: unidentified data type"<<std::endl; |
357 | - exit(1); | 378 | + exit(1); |
358 | } | 379 | } |
359 | } | 380 | } |
360 | - | 381 | + |
361 | else{ | 382 | else{ |
362 | std::cout<<"ERROR: unidentified interleave type"<<std::endl; | 383 | std::cout<<"ERROR: unidentified interleave type"<<std::endl; |
363 | exit(1); | 384 | exit(1); |
364 | } | 385 | } |
365 | return false; | 386 | return false; |
366 | - | 387 | + |
367 | } | 388 | } |
368 | 389 | ||
369 | /// Builds a mask from a band image and threshold value | 390 | /// Builds a mask from a band image and threshold value |
@@ -444,7 +465,7 @@ public: | @@ -444,7 +465,7 @@ public: | ||
444 | return false; | 465 | return false; |
445 | } | 466 | } |
446 | 467 | ||
447 | - /// sift-mask saves in an array only those spectra corresponding to nonzero values of the mask. | 468 | + /// sift-mask saves in an array only those spectra corresponding to nonzero values of the mask. |
448 | bool sift(std::string outfile, unsigned char* p) | 469 | bool sift(std::string outfile, unsigned char* p) |
449 | { | 470 | { |
450 | 471 | ||
@@ -459,7 +480,7 @@ public: | @@ -459,7 +480,7 @@ public: | ||
459 | 480 | ||
460 | //if a BIL file is sifted, it's saved as a BIP | 481 | //if a BIL file is sifted, it's saved as a BIP |
461 | if(header.interleave == envi_header::BIL) | 482 | if(header.interleave == envi_header::BIL) |
462 | - new_header.interleave = envi_header::BIP; | 483 | + new_header.interleave = envi_header::BIP; |
463 | 484 | ||
464 | //set the number of lines to 1 (this is a matrix with 1 line and N samples) | 485 | //set the number of lines to 1 (this is a matrix with 1 line and N samples) |
465 | new_header.lines = 1; | 486 | new_header.lines = 1; |
@@ -500,7 +521,7 @@ public: | @@ -500,7 +521,7 @@ public: | ||
500 | exit(1); | 521 | exit(1); |
501 | } | 522 | } |
502 | 523 | ||
503 | - | 524 | + |
504 | return false; | 525 | return false; |
505 | } | 526 | } |
506 | 527 | ||
@@ -525,12 +546,12 @@ public: | @@ -525,12 +546,12 @@ public: | ||
525 | } | 546 | } |
526 | 547 | ||
527 | else if (header.interleave == envi_header::BIL){ //if the infile is bil file | 548 | else if (header.interleave == envi_header::BIL){ //if the infile is bil file |
528 | - | 549 | + |
529 | std::cout << "ERROR in stim::envi::unsift - BIL files aren't supported yet" << std::endl; | 550 | std::cout << "ERROR in stim::envi::unsift - BIL files aren't supported yet" << std::endl; |
530 | } | 551 | } |
531 | 552 | ||
532 | else if (header.interleave == envi_header::BIP){ //if the infile is bip file | 553 | else if (header.interleave == envi_header::BIP){ //if the infile is bip file |
533 | - | 554 | + |
534 | if (header.data_type == envi_header::float32) | 555 | if (header.data_type == envi_header::float32) |
535 | return ((bip<float>*)file)->unsift(outfile, mask, samples, lines); | 556 | return ((bip<float>*)file)->unsift(outfile, mask, samples, lines); |
536 | else if (header.data_type == envi_header::float64) | 557 | else if (header.data_type == envi_header::float64) |
@@ -642,7 +663,7 @@ public: | @@ -642,7 +663,7 @@ public: | ||
642 | /// @param lb2 is the label value for the left baseline point for the second peak (denominator) | 663 | /// @param lb2 is the label value for the left baseline point for the second peak (denominator) |
643 | /// @param rb2 is the label value for the right baseline point for the second peak (denominator) | 664 | /// @param rb2 is the label value for the right baseline point for the second peak (denominator) |
644 | /// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator) | 665 | /// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator) |
645 | - /// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator) | 666 | + /// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator) |
646 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size | 667 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size |
647 | bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, | 668 | bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, |
648 | double lb2, double rb2, double lab2, double rab2, void* result){ | 669 | double lb2, double rb2, double lab2, double rab2, void* result){ |
@@ -922,7 +943,7 @@ public: | @@ -922,7 +943,7 @@ public: | ||
922 | } | 943 | } |
923 | return false; | 944 | return false; |
924 | } | 945 | } |
925 | - | 946 | + |
926 | /// Helper function that loads a mask into memory given a filename. | 947 | /// Helper function that loads a mask into memory given a filename. |
927 | 948 | ||
928 | /// @param mask is a pointer to pre-allocated memory of size X*Y | 949 | /// @param mask is a pointer to pre-allocated memory of size X*Y |
stim/parser/filename.h
@@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
3 | 3 | ||
4 | #include <stdio.h> /* defines FILENAME_MAX */ | 4 | #include <stdio.h> /* defines FILENAME_MAX */ |
5 | #ifdef _WIN32 | 5 | #ifdef _WIN32 |
6 | + #include <windows.h> | ||
6 | #include <direct.h> | 7 | #include <direct.h> |
7 | #define GetCurrentDir _getcwd | 8 | #define GetCurrentDir _getcwd |
8 | #define STIM_FILENAME_DIV '\\' | 9 | #define STIM_FILENAME_DIV '\\' |
@@ -17,6 +18,7 @@ | @@ -17,6 +18,7 @@ | ||
17 | #include <vector> | 18 | #include <vector> |
18 | #include <stack> | 19 | #include <stack> |
19 | #include <algorithm> | 20 | #include <algorithm> |
21 | +#include <iostream> | ||
20 | 22 | ||
21 | #include "../parser/parser.h" | 23 | #include "../parser/parser.h" |
22 | namespace stim{ | 24 | namespace stim{ |
@@ -203,7 +205,6 @@ public: | @@ -203,7 +205,6 @@ public: | ||
203 | 205 | ||
204 | 206 | ||
205 | #ifdef _WIN32 | 207 | #ifdef _WIN32 |
206 | -#include <windows.h> | ||
207 | //get a list of files matching the current template | 208 | //get a list of files matching the current template |
208 | std::vector<stim::filename> get_list(){ | 209 | std::vector<stim::filename> get_list(){ |
209 | 210 | ||
@@ -230,7 +231,7 @@ public: | @@ -230,7 +231,7 @@ public: | ||
230 | file_list.push_back(current_file); | 231 | file_list.push_back(current_file); |
231 | } | 232 | } |
232 | FindClose(hFind); | 233 | FindClose(hFind); |
233 | - | 234 | + |
234 | return file_list; | 235 | return file_list; |
235 | } | 236 | } |
236 | 237 | ||
@@ -312,4 +313,6 @@ public: | @@ -312,4 +313,6 @@ public: | ||
312 | 313 | ||
313 | } //end namespace stim | 314 | } //end namespace stim |
314 | 315 | ||
316 | + | ||
315 | #endif | 317 | #endif |
318 | + |