Commit ad2de689d3da8c688474be405a26fe903a6d6e21

Authored by pranathivemuri
2 parents 1ab6c061 f92397d2

Merge branch 'master' of https://git.stim.ee.uh.edu/codebase/stimlib into pranathi_stimlib

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