Commit 6236289c5faa27cc3ef88e154c70403061eba2a0

Authored by David Mayerich
2 parents 721fd9d1 9b766f1f

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

stim/cuda/branch_detection.cuh
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 #include <stim/cuda/cuda_texture.cuh> 7 #include <stim/cuda/cuda_texture.cuh>
8 #include <stim/cuda/filter.cuh> 8 #include <stim/cuda/filter.cuh>
9 typedef unsigned int uint; 9 typedef unsigned int uint;
10 -typedef unsigned int uchar; 10 +//typedef unsigned int uchar;
11 11
12 12
13 std::vector< stim::vec<float> > 13 std::vector< stim::vec<float> >
stim/cuda/spider_cost.cuh
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 3
4 #include <assert.h> 4 #include <assert.h>
5 #include <cuda.h> 5 #include <cuda.h>
6 -#include <cuda_runtime.h> 6 +//#include <cuda_runtime.h>
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <stim/visualization/colormap.h> 8 #include <stim/visualization/colormap.h>
9 #include <sstream> 9 #include <sstream>
@@ -3,8 +3,10 @@ @@ -3,8 +3,10 @@
3 3
4 #include "../envi/envi_header.h" 4 #include "../envi/envi_header.h"
5 #include "../envi/hsi.h" 5 #include "../envi/hsi.h"
  6 +#include "../math/fd_coefficients.h"
6 #include <cstring> 7 #include <cstring>
7 #include <utility> 8 #include <utility>
  9 +#include <deque>
8 10
9 namespace stim{ 11 namespace stim{
10 12
@@ -22,26 +24,14 @@ template &lt;typename T&gt; @@ -22,26 +24,14 @@ template &lt;typename T&gt;
22 class bil: public hsi<T> { 24 class bil: public hsi<T> {
23 25
24 protected: 26 protected:
25 -  
26 - //std::vector<double> w; //band wavelengths  
27 27
28 - /*unsigned long long X(){  
29 - return R[0];  
30 - }  
31 - unsigned long long Y(){  
32 - return R[2];  
33 - }  
34 - unsigned long long Z(){  
35 - return R[1];  
36 - }*/ 28 +
37 using hsi<T>::w; //use the wavelength array in stim::hsi 29 using hsi<T>::w; //use the wavelength array in stim::hsi
38 using hsi<T>::nnz; 30 using hsi<T>::nnz;
39 using binary<T>::progress; 31 using binary<T>::progress;
40 -  
41 - /// Call the binary nnz() function for the BIL orientation  
42 - //unsigned long long nnz(unsigned char* mask){  
43 - // return binary<T>::nnz(mask, X()*Y());  
44 - //} 32 + using hsi<T>::X;
  33 + using hsi<T>::Y;
  34 + using hsi<T>::Z;
45 35
46 public: 36 public:
47 37
@@ -59,11 +49,11 @@ public: @@ -59,11 +49,11 @@ public:
59 /// @param B is the number of samples (bands) along dimension 3 49 /// @param B is the number of samples (bands) along dimension 3
60 /// @param header_offset is the number of bytes (if any) in the binary header 50 /// @param header_offset is the number of bytes (if any) in the binary header
61 /// @param wavelengths is an optional STL vector of size B specifying a numerical label for each band 51 /// @param wavelengths is an optional STL vector of size B specifying a numerical label for each band
62 - bool open(std::string filename,  
63 - unsigned long long X,  
64 - unsigned long long Y,  
65 - unsigned long long B,  
66 - unsigned long long header_offset, 52 + bool open(std::string filename,
  53 + unsigned long long X,
  54 + unsigned long long Y,
  55 + unsigned long long B,
  56 + unsigned long long header_offset,
67 std::vector<double> wavelengths){ 57 std::vector<double> wavelengths){
68 58
69 w = wavelengths; 59 w = wavelengths;
@@ -113,7 +103,7 @@ public: @@ -113,7 +103,7 @@ public:
113 unsigned long long S = XY * sizeof(T); //calculate the number of bytes of a band 103 unsigned long long S = XY * sizeof(T); //calculate the number of bytes of a band
114 104
115 unsigned long long page=0; //bands around the wavelength 105 unsigned long long page=0; //bands around the wavelength
116 - 106 +
117 107
118 //get the bands numbers around the wavelength 108 //get the bands numbers around the wavelength
119 109
@@ -173,7 +163,7 @@ public: @@ -173,7 +163,7 @@ public:
173 163
174 //if wavelength is smaller than the first one in header file 164 //if wavelength is smaller than the first one in header file
175 if ( w[page] > wavelength ){ 165 if ( w[page] > wavelength ){
176 - memcpy(p, c, L); 166 + memcpy(p, c, L);
177 return true; 167 return true;
178 } 168 }
179 169
@@ -193,16 +183,16 @@ public: @@ -193,16 +183,16 @@ public:
193 183
194 memcpy(p1, c + (page - 1) * X(), L); 184 memcpy(p1, c + (page - 1) * X(), L);
195 memcpy(p2, c + page * X(), L); 185 memcpy(p2, c + page * X(), L);
196 - 186 +
197 for(unsigned long long i=0; i < X(); i++){ 187 for(unsigned long long i=0; i < X(); i++){
198 double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); 188 double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]);
199 p[i] = (T)(((double)p2[i] - (double)p1[i]) * r + (double)p1[i]); 189 p[i] = (T)(((double)p2[i] - (double)p1[i]) * r + (double)p1[i]);
200 } 190 }
201 } 191 }
202 - else //if the wavelength is equal to a wavelength in header file 192 + else //if the wavelength is equal to a wavelength in header file
203 memcpy(p, c + page * X(), L); 193 memcpy(p, c + page * X(), L);
204 -  
205 - return true; 194 +
  195 + return true;
206 } 196 }
207 197
208 /// Retrieve a single spectrum (B-axis line) at a given (x, y) location and stores it in pre-allocated memory. 198 /// Retrieve a single spectrum (B-axis line) at a given (x, y) location and stores it in pre-allocated memory.
@@ -227,24 +217,24 @@ public: @@ -227,24 +217,24 @@ public:
227 //get the pixel 217 //get the pixel
228 return spectrum(p, x, y); 218 return spectrum(p, x, y);
229 } 219 }
230 - 220 +
231 //given a Y ,return a XZ slice 221 //given a Y ,return a XZ slice
232 bool read_plane_y(T * p, unsigned long long y){ 222 bool read_plane_y(T * p, unsigned long long y){
233 return binary<T>::read_plane_2(p, y); 223 return binary<T>::read_plane_2(p, y);
234 } 224 }
235 225
236 - 226 +
237 /// Perform baseline correction given a list of baseline points and stores the result in a new BSQ file. 227 /// Perform baseline correction given a list of baseline points and stores the result in a new BSQ file.
238 228
239 /// @param outname is the name of the output file used to store the resulting baseline-corrected data. 229 /// @param outname is the name of the output file used to store the resulting baseline-corrected data.
240 /// @param wls is the list of baseline points based on band labels. 230 /// @param wls is the list of baseline points based on band labels.
241 bool baseline(std::string outname, std::vector<double> wls, unsigned char* mask = NULL, bool PROGRESS = false){ 231 bool baseline(std::string outname, std::vector<double> wls, unsigned char* mask = NULL, bool PROGRESS = false){
242 - 232 +
243 unsigned long long N = wls.size(); //get the number of baseline points 233 unsigned long long N = wls.size(); //get the number of baseline points
244 - 234 +
245 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file 235 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file
246 - std::string headername = outname + ".hdr"; //the header file name  
247 - 236 + std::string headername = outname + ".hdr"; //the header file name
  237 +
248 //simplify image resolution 238 //simplify image resolution
249 unsigned long long ZX = Z() * X(); //calculate the number of points in a Y slice 239 unsigned long long ZX = Z() * X(); //calculate the number of points in a Y slice
250 unsigned long long L = ZX * sizeof(T); //calculate the number of bytes of a Y slice 240 unsigned long long L = ZX * sizeof(T); //calculate the number of bytes of a Y slice
@@ -252,10 +242,10 @@ public: @@ -252,10 +242,10 @@ public:
252 242
253 T* c; //pointer to the current Y slice 243 T* c; //pointer to the current Y slice
254 c = (T*)malloc(L); //memory allocation 244 c = (T*)malloc(L); //memory allocation
255 - 245 +
256 T* a; //pointer to the two YZ lines surrounding the current YZ line 246 T* a; //pointer to the two YZ lines surrounding the current YZ line
257 T* b; 247 T* b;
258 - 248 +
259 a = (T*)malloc(X() * sizeof(T)); 249 a = (T*)malloc(X() * sizeof(T));
260 b = (T*)malloc(X() * sizeof(T)); 250 b = (T*)malloc(X() * sizeof(T));
261 251
@@ -269,19 +259,19 @@ public: @@ -269,19 +259,19 @@ public:
269 exit(1); 259 exit(1);
270 } 260 }
271 // loop start correct every y slice 261 // loop start correct every y slice
272 - 262 +
273 for (unsigned long long k =0; k < Y(); k++) 263 for (unsigned long long k =0; k < Y(); k++)
274 { 264 {
275 //get the current y slice 265 //get the current y slice
276 read_plane_y(c, k); 266 read_plane_y(c, k);
277 -  
278 - //initialize lownum, highnum, low, high 267 +
  268 + //initialize lownum, highnum, low, high
279 ai = w[0]; 269 ai = w[0];
280 control=0; 270 control=0;
281 //if no baseline point is specified at band 0, 271 //if no baseline point is specified at band 0,
282 //set the baseline point at band 0 to 0 272 //set the baseline point at band 0 to 0
283 if(wls[0] != w[0]){ 273 if(wls[0] != w[0]){
284 - bi = wls[control]; 274 + bi = wls[control];
285 memset(a, (char)0, X() * sizeof(T) ); 275 memset(a, (char)0, X() * sizeof(T) );
286 } 276 }
287 //else get the low band 277 //else get the low band
@@ -292,39 +282,39 @@ public: @@ -292,39 +282,39 @@ public:
292 } 282 }
293 //get the high band 283 //get the high band
294 read_x_from_xz(b, c, bi); 284 read_x_from_xz(b, c, bi);
295 - 285 +
296 //correct every YZ line 286 //correct every YZ line
297 - 287 +
298 for(unsigned long long cii = 0; cii < B; cii++){ 288 for(unsigned long long cii = 0; cii < B; cii++){
299 289
300 //update baseline points, if necessary 290 //update baseline points, if necessary
301 if( w[cii] >= bi && cii != B - 1) { 291 if( w[cii] >= bi && cii != B - 1) {
302 //if the high band is now on the last BL point 292 //if the high band is now on the last BL point
303 if (control != N-1) { 293 if (control != N-1) {
304 - 294 +
305 control++; //increment the index 295 control++; //increment the index
306 - 296 +
307 std::swap(a, b); //swap the baseline band pointers 297 std::swap(a, b); //swap the baseline band pointers
308 - 298 +
309 ai = bi; 299 ai = bi;
310 bi = wls[control]; 300 bi = wls[control];
311 read_x_from_xz(b, c, bi); 301 read_x_from_xz(b, c, bi);
312 - 302 +
313 } 303 }
314 //if the last BL point on the last band of the file? 304 //if the last BL point on the last band of the file?
315 else if ( wls[control] < w[B - 1]) { 305 else if ( wls[control] < w[B - 1]) {
316 - 306 +
317 std::swap(a, b); //swap the baseline band pointers 307 std::swap(a, b); //swap the baseline band pointers
318 - 308 +
319 memset(b, (char)0, X() * sizeof(T) ); //clear the high band 309 memset(b, (char)0, X() * sizeof(T) ); //clear the high band
320 - 310 +
321 ai = bi; 311 ai = bi;
322 bi = w[B - 1]; 312 bi = w[B - 1];
323 } 313 }
324 } 314 }
325 - 315 +
326 ci = w[cii]; 316 ci = w[cii];
327 - 317 +
328 unsigned long long jump = cii * X(); 318 unsigned long long jump = cii * X();
329 //perform the baseline correction 319 //perform the baseline correction
330 for(unsigned i=0; i < X(); i++) 320 for(unsigned i=0; i < X(); i++)
@@ -332,22 +322,22 @@ public: @@ -332,22 +322,22 @@ public:
332 double r = (double) (ci - ai) / (double) (bi - ai); 322 double r = (double) (ci - ai) / (double) (bi - ai);
333 c[i + jump] =(T) ( c[i + jump] - (b[i] - a[i]) * r - a[i] ); 323 c[i + jump] =(T) ( c[i + jump] - (b[i] - a[i]) * r - a[i] );
334 } 324 }
335 -  
336 - }//loop for YZ line end  
337 - target.write(reinterpret_cast<const char*>(c), L); //write the corrected data into destination 325 +
  326 + }//loop for YZ line end
  327 + target.write(reinterpret_cast<const char*>(c), L); //write the corrected data into destination
338 328
339 if(PROGRESS) progress = (double)(k+1) / Y() * 100; 329 if(PROGRESS) progress = (double)(k+1) / Y() * 100;
340 - }//loop for Y slice end  
341 - 330 + }//loop for Y slice end
  331 +
342 free(a); 332 free(a);
343 free(b); 333 free(b);
344 free(c); 334 free(c);
345 target.close(); 335 target.close();
346 336
347 - return true;  
348 - 337 + return true;
  338 +
349 } 339 }
350 - 340 +
351 /// Normalize all spectra based on the value of a single band, storing the result in a new BSQ file. 341 /// Normalize all spectra based on the value of a single band, storing the result in a new BSQ file.
352 342
353 /// @param outname is the name of the output file used to store the resulting baseline-corrected data. 343 /// @param outname is the name of the output file used to store the resulting baseline-corrected data.
@@ -358,7 +348,7 @@ public: @@ -358,7 +348,7 @@ public:
358 unsigned long long B = Z(); //calculate the number of bands 348 unsigned long long B = Z(); //calculate the number of bands
359 unsigned long long ZX = Z() * X(); 349 unsigned long long ZX = Z() * X();
360 unsigned long long XY = X() * Y(); //calculate the number of pixels in a band 350 unsigned long long XY = X() * Y(); //calculate the number of pixels in a band
361 - unsigned long long S = XY * sizeof(T); //calculate the number of bytes in a band 351 + unsigned long long S = XY * sizeof(T); //calculate the number of bytes in a band
362 unsigned long long L = ZX * sizeof(T); 352 unsigned long long L = ZX * sizeof(T);
363 353
364 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file 354 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file
@@ -368,7 +358,7 @@ public: @@ -368,7 +358,7 @@ public:
368 T * b; //pointer to the standard band 358 T * b; //pointer to the standard band
369 359
370 b = (T*)malloc( S ); //memory allocation 360 b = (T*)malloc( S ); //memory allocation
371 - c = (T*)malloc( L ); 361 + c = (T*)malloc( L );
372 362
373 band(b, w); //get the certain band into memory 363 band(b, w); //get the certain band into memory
374 364
@@ -383,13 +373,13 @@ public: @@ -383,13 +373,13 @@ public:
383 c[m + i * X()] = (T)0.0; 373 c[m + i * X()] = (T)0.0;
384 else 374 else
385 c[m + i * X()] = c[m + i * X()] / b[m + j * X()]; 375 c[m + i * X()] = c[m + i * X()] / b[m + j * X()];
386 - } 376 + }
387 } 377 }
388 target.write(reinterpret_cast<const char*>(c), L); //write normalized data into destination 378 target.write(reinterpret_cast<const char*>(c), L); //write normalized data into destination
389 379
390 if(PROGRESS) progress = (double)(j+1) / Y() * 100; 380 if(PROGRESS) progress = (double)(j+1) / Y() * 100;
391 } 381 }
392 - 382 +
393 free(b); 383 free(b);
394 free(c); 384 free(c);
395 target.close(); 385 target.close();
@@ -397,26 +387,26 @@ public: @@ -397,26 +387,26 @@ public:
397 } 387 }
398 388
399 void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){} 389 void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){}
400 - 390 +
401 /// Convert the current BIL file to a BSQ file with the specified file name. 391 /// Convert the current BIL file to a BSQ file with the specified file name.
402 392
403 /// @param outname is the name of the output BSQ file to be saved to disk. 393 /// @param outname is the name of the output BSQ file to be saved to disk.
404 bool bsq(std::string outname, bool PROGRESS = false) 394 bool bsq(std::string outname, bool PROGRESS = false)
405 { 395 {
406 unsigned long long S = X() * Y() * sizeof(T); //calculate the number of bytes in a band 396 unsigned long long S = X() * Y() * sizeof(T); //calculate the number of bytes in a band
407 - 397 +
408 std::ofstream target(outname.c_str(), std::ios::binary); 398 std::ofstream target(outname.c_str(), std::ios::binary);
409 std::string headername = outname + ".hdr"; 399 std::string headername = outname + ".hdr";
410 - 400 +
411 T * p; //pointer to the current band 401 T * p; //pointer to the current band
412 p = (T*)malloc(S); 402 p = (T*)malloc(S);
413 - 403 +
414 for ( unsigned long long i = 0; i < Z(); i++) 404 for ( unsigned long long i = 0; i < Z(); i++)
415 - { 405 + {
416 band_index(p, i); 406 band_index(p, i);
417 target.write(reinterpret_cast<const char*>(p), S); //write a band data into target file 407 target.write(reinterpret_cast<const char*>(p), S); //write a band data into target file
418 408
419 - if(PROGRESS) progress = (double)(i+1) / Z() * 100; //store the progress for the current operation 409 + if(PROGRESS) progress = (double)(i+1) / Z() * 100; //store the progress for the current operation
420 } 410 }
421 411
422 free(p); 412 free(p);
@@ -430,17 +420,17 @@ public: @@ -430,17 +420,17 @@ public:
430 bool bip(std::string outname, bool PROGRESS = false) 420 bool bip(std::string outname, bool PROGRESS = false)
431 { 421 {
432 unsigned long long S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice 422 unsigned long long S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice
433 - 423 +
434 std::ofstream target(outname.c_str(), std::ios::binary); 424 std::ofstream target(outname.c_str(), std::ios::binary);
435 std::string headername = outname + ".hdr"; 425 std::string headername = outname + ".hdr";
436 - 426 +
437 T * p; //pointer to the current XZ slice for bil file 427 T * p; //pointer to the current XZ slice for bil file
438 p = (T*)malloc(S); 428 p = (T*)malloc(S);
439 T * q; //pointer to the current ZX slice for bip file 429 T * q; //pointer to the current ZX slice for bip file
440 q = (T*)malloc(S); 430 q = (T*)malloc(S);
441 - 431 +
442 for ( unsigned long long i = 0; i < Y(); i++) 432 for ( unsigned long long i = 0; i < Y(); i++)
443 - { 433 + {
444 read_plane_y(p, i); 434 read_plane_y(p, i);
445 for ( unsigned long long k = 0; k < Z(); k++) 435 for ( unsigned long long k = 0; k < Z(); k++)
446 { 436 {
@@ -448,10 +438,10 @@ public: @@ -448,10 +438,10 @@ public:
448 for ( unsigned long long j = 0; j < X(); j++) 438 for ( unsigned long long j = 0; j < X(); j++)
449 q[k + j * Z()] = p[ks + j]; 439 q[k + j * Z()] = p[ks + j];
450 440
451 - if(PROGRESS) progress = (double)((i+1) * Z() + k+1) / (Z() * Y()) * 100; //store the progress for the current operation 441 + if(PROGRESS) progress = (double)((i+1) * Z() + k+1) / (Z() * Y()) * 100; //store the progress for the current operation
452 } 442 }
453 -  
454 - target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file 443 +
  444 + target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file
455 } 445 }
456 446
457 free(p); 447 free(p);
@@ -498,7 +488,7 @@ public: @@ -498,7 +488,7 @@ public:
498 rp = (T*) malloc(S); 488 rp = (T*) malloc(S);
499 489
500 band(lp, lb); 490 band(lp, lb);
501 - band(rp, rb); 491 + band(rp, rb);
502 492
503 baseline_band(lb, rb, lp, rp, bandwavelength, result); 493 baseline_band(lb, rb, lp, rp, bandwavelength, result);
504 494
@@ -570,7 +560,7 @@ public: @@ -570,7 +560,7 @@ public:
570 } 560 }
571 baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part 561 baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part
572 baseline_band(lb, rb, lp, rp, w[ai], cur); 562 baseline_band(lb, rb, lp, rp, w[ai], cur);
573 - for(unsigned long long j = 0; j < XY; j++){ 563 + for(unsigned long long j = 0; j < XY; j++){
574 result[j] += (T)((w[ai] - lab) * ((double)cur[j] + (double)cur2[j]) / 2.0); 564 result[j] += (T)((w[ai] - lab) * ((double)cur[j] + (double)cur2[j]) / 2.0);
575 } 565 }
576 566
@@ -581,7 +571,7 @@ public: @@ -581,7 +571,7 @@ public:
581 baseline_band(lb, rb, lp, rp, w[ai], cur2); 571 baseline_band(lb, rb, lp, rp, w[ai], cur2);
582 for(unsigned long long j = 0; j < XY; j++) 572 for(unsigned long long j = 0; j < XY; j++)
583 { 573 {
584 - result[j] += (T)((w[ai] - w[ai-1]) * ((double)cur[j] + (double)cur2[j]) / 2.0); 574 + result[j] += (T)((w[ai] - w[ai-1]) * ((double)cur[j] + (double)cur2[j]) / 2.0);
585 } 575 }
586 std::swap(cur,cur2); //swap the band pointers 576 std::swap(cur,cur2); //swap the band pointers
587 } 577 }
@@ -606,7 +596,7 @@ public: @@ -606,7 +596,7 @@ public:
606 596
607 T* p1 = (T*)malloc(X() * Y() * sizeof(T)); 597 T* p1 = (T*)malloc(X() * Y() * sizeof(T));
608 T* p2 = (T*)malloc(X() * Y() * sizeof(T)); 598 T* p2 = (T*)malloc(X() * Y() * sizeof(T));
609 - 599 +
610 //get the two peak band 600 //get the two peak band
611 height(lb1, rb1, pos1, p1); 601 height(lb1, rb1, pos1, p1);
612 height(lb2, rb2, pos2, p2); 602 height(lb2, rb2, pos2, p2);
@@ -620,9 +610,9 @@ public: @@ -620,9 +610,9 @@ public:
620 610
621 free(p1); 611 free(p1);
622 free(p2); 612 free(p2);
623 - return true; 613 + return true;
624 } 614 }
625 - 615 +
626 /// Compute the ratio between a peak area and peak height. 616 /// Compute the ratio between a peak area and peak height.
627 617
628 /// @param lb1 is the label value for the left baseline point for the first peak (numerator) 618 /// @param lb1 is the label value for the left baseline point for the first peak (numerator)
@@ -634,10 +624,10 @@ public: @@ -634,10 +624,10 @@ public:
634 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 624 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
635 bool pa_to_ph(T* result, double lb1, double rb1, double lab1, double rab1, 625 bool pa_to_ph(T* result, double lb1, double rb1, double lab1, double rab1,
636 double lb2, double rb2, double pos, unsigned char* mask = NULL){ 626 double lb2, double rb2, double pos, unsigned char* mask = NULL){
637 - 627 +
638 T* p1 = (T*)malloc(X() * Y() * sizeof(T)); 628 T* p1 = (T*)malloc(X() * Y() * sizeof(T));
639 T* p2 = (T*)malloc(X() * Y() * sizeof(T)); 629 T* p2 = (T*)malloc(X() * Y() * sizeof(T));
640 - 630 +
641 //get the area and the peak band 631 //get the area and the peak band
642 area(lb1, rb1, lab1, rab1, p1); 632 area(lb1, rb1, lab1, rab1, p1);
643 height(lb2, rb2, pos, p2); 633 height(lb2, rb2, pos, p2);
@@ -651,9 +641,9 @@ public: @@ -651,9 +641,9 @@ public:
651 641
652 free(p1); 642 free(p1);
653 free(p2); 643 free(p2);
654 - return true;  
655 - }  
656 - 644 + return true;
  645 + }
  646 +
657 /// Compute the ratio between two peak areas. 647 /// Compute the ratio between two peak areas.
658 648
659 /// @param lb1 is the label value for the left baseline point for the first peak (numerator) 649 /// @param lb1 is the label value for the left baseline point for the first peak (numerator)
@@ -663,14 +653,14 @@ public: @@ -663,14 +653,14 @@ public:
663 /// @param lb2 is the label value for the left baseline point for the second peak (denominator) 653 /// @param lb2 is the label value for the left baseline point for the second peak (denominator)
664 /// @param rb2 is the label value for the right baseline point for the second peak (denominator) 654 /// @param rb2 is the label value for the right baseline point for the second peak (denominator)
665 /// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator) 655 /// @param lab2 is the label value for the left bound (start 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) 656 + /// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator)
667 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 657 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
668 bool pa_to_pa(T* result, double lb1, double rb1, double lab1, double rab1, 658 bool pa_to_pa(T* result, double lb1, double rb1, double lab1, double rab1,
669 double lb2, double rb2, double lab2, double rab2, unsigned char* mask = NULL){ 659 double lb2, double rb2, double lab2, double rab2, unsigned char* mask = NULL){
670 - 660 +
671 T* p1 = (T*)malloc(X() * Y() * sizeof(T)); 661 T* p1 = (T*)malloc(X() * Y() * sizeof(T));
672 T* p2 = (T*)malloc(X() * Y() * sizeof(T)); 662 T* p2 = (T*)malloc(X() * Y() * sizeof(T));
673 - 663 +
674 //get the area and the peak band 664 //get the area and the peak band
675 area(lb1, rb1, lab1, rab1, p1); 665 area(lb1, rb1, lab1, rab1, p1);
676 area(lb2, rb2, lab2, rab2, p2); 666 area(lb2, rb2, lab2, rab2, p2);
@@ -684,8 +674,8 @@ public: @@ -684,8 +674,8 @@ public:
684 674
685 free(p1); 675 free(p1);
686 free(p2); 676 free(p2);
687 - return true;  
688 - } 677 + return true;
  678 + }
689 679
690 /// Compute the definite integral of a baseline corrected peak. 680 /// Compute the definite integral of a baseline corrected peak.
691 681
@@ -745,7 +735,7 @@ public: @@ -745,7 +735,7 @@ public:
745 } 735 }
746 baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part 736 baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part
747 baseline_band(lb, rb, lp, rp, w[ai], cur); 737 baseline_band(lb, rb, lp, rp, w[ai], cur);
748 - for(unsigned long long j = 0; j < XY; j++){ 738 + for(unsigned long long j = 0; j < XY; j++){
749 result[j] += (T)((w[ai] - lab) * (w[ai] + lab) * ((double)cur[j] + (double)cur2[j]) / 4.0); 739 result[j] += (T)((w[ai] - lab) * (w[ai] + lab) * ((double)cur[j] + (double)cur2[j]) / 4.0);
750 } 740 }
751 741
@@ -756,7 +746,7 @@ public: @@ -756,7 +746,7 @@ public:
756 baseline_band(lb, rb, lp, rp, w[ai], cur2); 746 baseline_band(lb, rb, lp, rp, w[ai], cur2);
757 for(unsigned long long j = 0; j < XY; j++) 747 for(unsigned long long j = 0; j < XY; j++)
758 { 748 {
759 - result[j] += (T)((w[ai] - w[ai-1]) * (w[ai] + w[ai-1]) * ((double)cur[j] + (double)cur2[j]) / 4.0); 749 + result[j] += (T)((w[ai] - w[ai-1]) * (w[ai] + w[ai-1]) * ((double)cur[j] + (double)cur2[j]) / 4.0);
760 } 750 }
761 std::swap(cur,cur2); //swap the band pointers 751 std::swap(cur,cur2); //swap the band pointers
762 } 752 }
@@ -778,7 +768,7 @@ public: @@ -778,7 +768,7 @@ public:
778 bool centroid(T* result, double lb, double rb, double lab, double rab, unsigned char* mask = NULL){ 768 bool centroid(T* result, double lb, double rb, double lab, double rab, unsigned char* mask = NULL){
779 T* p1 = (T*)malloc(X() * Y() * sizeof(T)); 769 T* p1 = (T*)malloc(X() * Y() * sizeof(T));
780 T* p2 = (T*)malloc(X() * Y() * sizeof(T)); 770 T* p2 = (T*)malloc(X() * Y() * sizeof(T));
781 - 771 +
782 //get the area and the peak band 772 //get the area and the peak band
783 x_area(lb, rb, lab, rab, p1); 773 x_area(lb, rb, lab, rab, p1);
784 area(lb, rb, lab, rab, p2); 774 area(lb, rb, lab, rab, p2);
@@ -790,7 +780,7 @@ public: @@ -790,7 +780,7 @@ public:
790 780
791 free(p1); 781 free(p1);
792 free(p2); 782 free(p2);
793 - return true; 783 + return true;
794 } 784 }
795 785
796 /// Create a mask based on a given band and threshold value. 786 /// Create a mask based on a given band and threshold value.
@@ -843,7 +833,7 @@ public: @@ -843,7 +833,7 @@ public:
843 continue; 833 continue;
844 } 834 }
845 } 835 }
846 - target.write(reinterpret_cast<const char*>(temp), L); //write a band data into target file 836 + target.write(reinterpret_cast<const char*>(temp), L); //write a band data into target file
847 if(PROGRESS) progress = (double)(i+1) / (double)Y() * 100; 837 if(PROGRESS) progress = (double)(i+1) / (double)Y() * 100;
848 } 838 }
849 target.close(); 839 target.close();
@@ -861,7 +851,7 @@ public: @@ -861,7 +851,7 @@ public:
861 T* line = (T*) malloc( Lbytes ); //allocate space for a line 851 T* line = (T*) malloc( Lbytes ); //allocate space for a line
862 852
863 file.seekg(0, std::ios::beg); //seek to the beginning of the file 853 file.seekg(0, std::ios::beg); //seek to the beginning of the file
864 - 854 +
865 size_t pl; 855 size_t pl;
866 size_t p = 0; //create counter variables 856 size_t p = 0; //create counter variables
867 for(size_t y = 0; y < Y(); y++){ //for each line in the data set 857 for(size_t y = 0; y < Y(); y++){ //for each line in the data set
@@ -903,7 +893,7 @@ public: @@ -903,7 +893,7 @@ public:
903 { 893 {
904 read_plane_y(slice, y); //retrieve an ZX page, store in "slice" 894 read_plane_y(slice, y); //retrieve an ZX page, store in "slice"
905 895
906 - //for each sample along X 896 + //for each sample along X
907 for (unsigned long long x = 0; x < X(); x++) //Select a pixel by choosing X coordinate in the page, X() 897 for (unsigned long long x = 0; x < X(); x++) //Select a pixel by choosing X coordinate in the page, X()
908 { 898 {
909 //if the mask != 0 at that xy pixel 899 //if the mask != 0 at that xy pixel
@@ -1082,7 +1072,7 @@ public: @@ -1082,7 +1072,7 @@ public:
1082 for (unsigned long long z = b0; z < b1; z++) 1072 for (unsigned long long z = b0; z < b1; z++)
1083 { 1073 {
1084 file.read((char *)(temp + z * samples), sizeof(T) * samples); 1074 file.read((char *)(temp + z * samples), sizeof(T) * samples);
1085 - file.seekg(jumpb, std::ios::cur); //go to the next band 1075 + file.seekg(jumpb, std::ios::cur); //go to the next band
1086 1076
1087 if(PROGRESS) progress = (double)(x * Z() + z+1) / (lines * Z()) * 100; 1077 if(PROGRESS) progress = (double)(x * Z() + z+1) / (lines * Z()) * 100;
1088 } 1078 }
@@ -1193,7 +1183,7 @@ public: @@ -1193,7 +1183,7 @@ public:
1193 size_t N = C.size(); //get the number of bands that the kernel spans 1183 size_t N = C.size(); //get the number of bands that the kernel spans
1194 std::deque<T*> frame(N, NULL); //create an array to store pointers to each frame 1184 std::deque<T*> frame(N, NULL); //create an array to store pointers to each frame
1195 for(size_t f = 0; f < N; f++) 1185 for(size_t f = 0; f < N; f++)
1196 - frame[f] = (T*)malloc(Xb); //allocate space for the frame 1186 + frame[f] = (T*)malloc(Xb); //allocate space for the frame
1197 1187
1198 T* outline = (T*)malloc(Xb); //allocate space for the output band 1188 T* outline = (T*)malloc(Xb); //allocate space for the output band
1199 1189
@@ -1219,23 +1209,23 @@ public: @@ -1219,23 +1209,23 @@ public:
1219 frame.pop_front(); //pop the first element 1209 frame.pop_front(); //pop the first element
1220 } 1210 }
1221 if(PROGRESS) progress = (double)(y+1) / (double)Y() * 100; 1211 if(PROGRESS) progress = (double)(y+1) / (double)Y() * 100;
1222 - } 1212 + }
1223 } 1213 }
1224 1214
1225 /// Approximate the spectral derivative of the image 1215 /// Approximate the spectral derivative of the image
1226 void deriv(std::string outfile, size_t d, size_t order, const std::vector<double> w = std::vector<double>(), unsigned char* mask = NULL, bool PROGRESS = false){ 1216 void deriv(std::string outfile, size_t d, size_t order, const std::vector<double> w = std::vector<double>(), unsigned char* mask = NULL, bool PROGRESS = false){
1227 std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing 1217 std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing
1228 - 1218 +
1229 size_t Xb = X() * sizeof(T); //calculate the size of a line (frame) in bytes 1219 size_t Xb = X() * sizeof(T); //calculate the size of a line (frame) in bytes
1230 size_t XBb = Xb * Z(); 1220 size_t XBb = Xb * Z();
1231 size_t B = Z(); 1221 size_t B = Z();
1232 1222
1233 - file.seekg(0, std::ios::beg); //seek to the beginning of the file 1223 + file.seekg(0, std::ios::beg); //seek to the beginning of the file
1234 1224
1235 size_t N = order + d; //approximating a derivative requires order + d samples 1225 size_t N = order + d; //approximating a derivative requires order + d samples
1236 std::deque<T*> frame(N, NULL); //create an array to store pointers to each frame 1226 std::deque<T*> frame(N, NULL); //create an array to store pointers to each frame
1237 for(size_t f = 0; f < N; f++) //for each frame 1227 for(size_t f = 0; f < N; f++) //for each frame
1238 - frame[f] = (T*)malloc(Xb); //allocate space for the frame 1228 + frame[f] = (T*)malloc(Xb); //allocate space for the frame
1239 1229
1240 T* outline = (T*)malloc(Xb); //allocate space for the output band 1230 T* outline = (T*)malloc(Xb); //allocate space for the output band
1241 1231
@@ -1273,7 +1263,7 @@ public: @@ -1273,7 +1263,7 @@ public:
1273 } 1263 }
1274 } 1264 }
1275 out.write((char*)outline, Xb); //output the band 1265 out.write((char*)outline, Xb); //output the band
1276 - 1266 +
1277 } 1267 }
1278 if(PROGRESS) progress = (double)(y+1) / (double)Y() * 100; 1268 if(PROGRESS) progress = (double)(y+1) / (double)Y() * 100;
1279 } 1269 }
@@ -28,15 +28,18 @@ template &lt;typename T&gt; @@ -28,15 +28,18 @@ template &lt;typename T&gt;
28 class bip: public hsi<T> { 28 class bip: public hsi<T> {
29 29
30 protected: 30 protected:
31 -  
32 - 31 +
  32 +
33 //std::vector<double> w; //band wavelength 33 //std::vector<double> w; //band wavelength
34 unsigned long long offset; //header offset 34 unsigned long long offset; //header offset
35 35
36 using hsi<T>::w; //use the wavelength array in stim::hsi 36 using hsi<T>::w; //use the wavelength array in stim::hsi
37 using hsi<T>::nnz; 37 using hsi<T>::nnz;
38 using binary<T>::progress; 38 using binary<T>::progress;
39 - 39 + using hsi<T>::X;
  40 + using hsi<T>::Y;
  41 + using hsi<T>::Z;
  42 +
40 public: 43 public:
41 44
42 using binary<T>::open; 45 using binary<T>::open;
@@ -54,11 +57,11 @@ public: @@ -54,11 +57,11 @@ public:
54 /// @param B is the number of samples (bands) along dimension 3 57 /// @param B is the number of samples (bands) along dimension 3
55 /// @param header_offset is the number of bytes (if any) in the binary header 58 /// @param header_offset is the number of bytes (if any) in the binary header
56 /// @param wavelengths is an optional STL vector of size B specifying a numerical label for each band 59 /// @param wavelengths is an optional STL vector of size B specifying a numerical label for each band
57 - bool open(std::string filename,  
58 - unsigned long long X,  
59 - unsigned long long Y,  
60 - unsigned long long B,  
61 - unsigned long long header_offset, 60 + bool open(std::string filename,
  61 + unsigned long long X,
  62 + unsigned long long Y,
  63 + unsigned long long B,
  64 + unsigned long long header_offset,
62 std::vector<double> wavelengths){ 65 std::vector<double> wavelengths){
63 66
64 //copy the wavelengths to the BSQ file structure 67 //copy the wavelengths to the BSQ file structure
@@ -67,7 +70,7 @@ public: @@ -67,7 +70,7 @@ public:
67 offset = header_offset; 70 offset = header_offset;
68 71
69 return open(filename, vec<unsigned long long>(B, X, Y), header_offset); 72 return open(filename, vec<unsigned long long>(B, X, Y), header_offset);
70 - 73 +
71 } 74 }
72 75
73 /// Retrieve a single band (based on index) and stores it in pre-allocated memory. 76 /// Retrieve a single band (based on index) and stores it in pre-allocated memory.
@@ -160,7 +163,7 @@ public: @@ -160,7 +163,7 @@ public:
160 for(unsigned long long j = 0; j < X(); j++) 163 for(unsigned long long j = 0; j < X(); j++)
161 { 164 {
162 p[j] = c[j * B]; 165 p[j] = c[j * B];
163 - } 166 + }
164 return true; 167 return true;
165 } 168 }
166 169
@@ -192,7 +195,7 @@ public: @@ -192,7 +195,7 @@ public:
192 { 195 {
193 p2[j] = c[j * B + page]; 196 p2[j] = c[j * B + page];
194 } 197 }
195 - 198 +
196 for(unsigned long long i=0; i < X(); i++){ 199 for(unsigned long long i=0; i < X(); i++){
197 double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); 200 double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]);
198 p[i] = (p2[i] - p1[i]) * r + p1[i]; 201 p[i] = (p2[i] - p1[i]) * r + p1[i];
@@ -209,7 +212,7 @@ public: @@ -209,7 +212,7 @@ public:
209 } 212 }
210 } 213 }
211 214
212 - return true; 215 + return true;
213 } 216 }
214 217
215 /// Retrieve a single pixel and store it in a pre-allocated double array. 218 /// Retrieve a single pixel and store it in a pre-allocated double array.
@@ -246,20 +249,20 @@ public: @@ -246,20 +249,20 @@ public:
246 file.read((char *)p, sizeof(T) * Z()); 249 file.read((char *)p, sizeof(T) * Z());
247 return true; 250 return true;
248 } 251 }
249 - 252 +
250 //given a Y ,return a ZX slice 253 //given a Y ,return a ZX slice
251 bool read_plane_y(T * p, unsigned long long y){ 254 bool read_plane_y(T * p, unsigned long long y){
252 return binary<T>::read_plane_2(p, y); 255 return binary<T>::read_plane_2(p, y);
253 } 256 }
254 - 257 +
255 /// Perform baseline correction given a list of baseline points and stores the result in a new BSQ file. 258 /// Perform baseline correction given a list of baseline points and stores the result in a new BSQ file.
256 259
257 /// @param outname is the name of the output file used to store the resulting baseline-corrected data. 260 /// @param outname is the name of the output file used to store the resulting baseline-corrected data.
258 - /// @param wls is the list of baseline points based on band labels. 261 + /// @param wls is the list of baseline points based on band labels.
259 bool baseline(std::string outname, std::vector<double> base_pts, unsigned char* mask = NULL, bool PROGRESS = false){ 262 bool baseline(std::string outname, std::vector<double> base_pts, unsigned char* mask = NULL, bool PROGRESS = false){
260 - 263 +
261 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file 264 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file
262 - 265 +
263 unsigned long long N = X() * Y(); //calculate the total number of pixels to be processed 266 unsigned long long N = X() * Y(); //calculate the total number of pixels to be processed
264 unsigned long long B = Z(); //get the number of bands 267 unsigned long long B = Z(); //get the number of bands
265 T* s = (T*)malloc(sizeof(T) * B); //allocate memory to store a pixel 268 T* s = (T*)malloc(sizeof(T) * B); //allocate memory to store a pixel
@@ -274,9 +277,9 @@ public: @@ -274,9 +277,9 @@ public:
274 memset(sbc, 0, sizeof(T) * B); //set the baseline corrected spectrum to zero 277 memset(sbc, 0, sizeof(T) * B); //set the baseline corrected spectrum to zero
275 } 278 }
276 else{ 279 else{
277 - 280 +
278 pixel(s, n); //retrieve the spectrum s 281 pixel(s, n); //retrieve the spectrum s
279 - base_vals = interp_spectrum(s, base_pts); //get the values at each baseline point 282 + base_vals = hsi<T>::interp_spectrum(s, base_pts); //get the values at each baseline point
280 283
281 ai = bi = 0; 284 ai = bi = 0;
282 aw = w[0]; //initialize the current baseline points (assume the spectrum starts at 0) 285 aw = w[0]; //initialize the current baseline points (assume the spectrum starts at 0)
@@ -298,23 +301,23 @@ public: @@ -298,23 +301,23 @@ public:
298 aw = base_pts[ai]; //set the wavelength for the lower bound baseline point 301 aw = base_pts[ai]; //set the wavelength for the lower bound baseline point
299 av = base_vals[ai]; //set the value for the lower bound baseline point 302 av = base_vals[ai]; //set the value for the lower bound baseline point
300 } 303 }
301 - sbc[b] = s[b] - lerp(w[b], av, aw, bv, bw); //perform the baseline correction and save the new value 304 + sbc[b] = s[b] - hsi<T>::lerp(w[b], av, aw, bv, bw); //perform the baseline correction and save the new value
302 } 305 }
303 } 306 }
304 - 307 +
305 if(PROGRESS) progress = (double)(n+1) / N * 100; //set the current progress 308 if(PROGRESS) progress = (double)(n+1) / N * 100; //set the current progress
306 309
307 target.write((char*)sbc, sizeof(T) * B); //write the corrected data into destination 310 target.write((char*)sbc, sizeof(T) * B); //write the corrected data into destination
308 } //end for each pixel 311 } //end for each pixel
309 - 312 +
310 free(s); 313 free(s);
311 free(sbc); 314 free(sbc);
312 target.close(); 315 target.close();
313 -  
314 - return true;  
315 - 316 +
  317 + return true;
  318 +
316 } 319 }
317 - 320 +
318 /// Normalize all spectra based on the value of a single band, storing the result in a new BSQ file. 321 /// Normalize all spectra based on the value of a single band, storing the result in a new BSQ file.
319 322
320 /// @param outname is the name of the output file used to store the resulting baseline-corrected data. 323 /// @param outname is the name of the output file used to store the resulting baseline-corrected data.
@@ -323,8 +326,8 @@ public: @@ -323,8 +326,8 @@ public:
323 bool ratio(std::string outname, double w, unsigned char* mask = NULL, bool PROGRESS = false) 326 bool ratio(std::string outname, double w, unsigned char* mask = NULL, bool PROGRESS = false)
324 { 327 {
325 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file 328 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file
326 - std::string headername = outname + ".hdr"; //the header file name  
327 - 329 + std::string headername = outname + ".hdr"; //the header file name
  330 +
328 unsigned long long N = X() * Y(); //calculate the total number of pixels to be processed 331 unsigned long long N = X() * Y(); //calculate the total number of pixels to be processed
329 unsigned long long B = Z(); //get the number of bands 332 unsigned long long B = Z(); //get the number of bands
330 T* s = (T*)malloc(sizeof(T) * B); //allocate memory to store a pixel 333 T* s = (T*)malloc(sizeof(T) * B); //allocate memory to store a pixel
@@ -334,24 +337,24 @@ public: @@ -334,24 +337,24 @@ public:
334 memset(s, 0, sizeof(T) * B); //set the output to zero 337 memset(s, 0, sizeof(T) * B); //set the output to zero
335 else{ 338 else{
336 pixel(s, n); //retrieve the spectrum s 339 pixel(s, n); //retrieve the spectrum s
337 - nv = interp_spectrum(s, w); //find the value of the normalization band  
338 - 340 + nv = hsi<T>::interp_spectrum(s, w); //find the value of the normalization band
  341 +
339 for(unsigned long long b = 0; b < B; b++) //for each band in the spectrum 342 for(unsigned long long b = 0; b < B; b++) //for each band in the spectrum
340 s[b] /= nv; //divide by the normalization value 343 s[b] /= nv; //divide by the normalization value
341 } 344 }
342 - 345 +
343 if(PROGRESS) progress = (double)(n+1) / N * 100; //set the current progress 346 if(PROGRESS) progress = (double)(n+1) / N * 100; //set the current progress
344 347
345 target.write((char*)s, sizeof(T) * B); //write the corrected data into destination 348 target.write((char*)s, sizeof(T) * B); //write the corrected data into destination
346 } //end for each pixel 349 } //end for each pixel
347 - 350 +
348 free(s); 351 free(s);
349 target.close(); 352 target.close();
350 return true; 353 return true;
351 } 354 }
352 355
353 void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){} 356 void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){}
354 - 357 +
355 358
356 /// Convert the current BIP file to a BIL file with the specified file name. 359 /// Convert the current BIP file to a BIL file with the specified file name.
357 360
@@ -359,17 +362,17 @@ public: @@ -359,17 +362,17 @@ public:
359 bool bil(std::string outname, bool PROGRESS = false) 362 bool bil(std::string outname, bool PROGRESS = false)
360 { 363 {
361 unsigned long long S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice 364 unsigned long long S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice
362 - 365 +
363 std::ofstream target(outname.c_str(), std::ios::binary); 366 std::ofstream target(outname.c_str(), std::ios::binary);
364 //std::string headername = outname + ".hdr"; 367 //std::string headername = outname + ".hdr";
365 - 368 +
366 T * p; //pointer to the current ZX slice for bip file 369 T * p; //pointer to the current ZX slice for bip file
367 p = (T*)malloc(S); 370 p = (T*)malloc(S);
368 T * q; //pointer to the current XZ slice for bil file 371 T * q; //pointer to the current XZ slice for bil file
369 q = (T*)malloc(S); 372 q = (T*)malloc(S);
370 - 373 +
371 for ( unsigned long long i = 0; i < Y(); i++) 374 for ( unsigned long long i = 0; i < Y(); i++)
372 - { 375 + {
373 read_plane_y(p, i); 376 read_plane_y(p, i);
374 for ( unsigned long long k = 0; k < Z(); k++) 377 for ( unsigned long long k = 0; k < Z(); k++)
375 { 378 {
@@ -379,7 +382,7 @@ public: @@ -379,7 +382,7 @@ public:
379 382
380 if(PROGRESS) progress = (double)(i * Z() + k+1) / (Y() * Z()) * 100; 383 if(PROGRESS) progress = (double)(i * Z() + k+1) / (Y() * Z()) * 100;
381 } 384 }
382 - target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file 385 + target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file
383 } 386 }
384 387
385 free(p); 388 free(p);
@@ -408,7 +411,7 @@ public: @@ -408,7 +411,7 @@ public:
408 } 411 }
409 return true; 412 return true;
410 } 413 }
411 - 414 +
412 /// Return a baseline corrected band given two adjacent baseline points. The result is stored in a pre-allocated array. 415 /// Return a baseline corrected band given two adjacent baseline points. The result is stored in a pre-allocated array.
413 416
414 /// @param lb is the label value for the left baseline point 417 /// @param lb is the label value for the left baseline point
@@ -425,7 +428,7 @@ public: @@ -425,7 +428,7 @@ public:
425 rp = (T*) malloc(S); 428 rp = (T*) malloc(S);
426 429
427 band(lp, lb); 430 band(lp, lb);
428 - band(rp, rb); 431 + band(rp, rb);
429 432
430 baseline_band(lb, rb, lp, rp, bandwavelength, result); 433 baseline_band(lb, rb, lp, rp, bandwavelength, result);
431 434
@@ -496,7 +499,7 @@ public: @@ -496,7 +499,7 @@ public:
496 } 499 }
497 baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part 500 baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part
498 baseline_band(lb, rb, lp, rp, w[ai], cur); 501 baseline_band(lb, rb, lp, rp, w[ai], cur);
499 - for(unsigned long long j = 0; j < XY; j++){ 502 + for(unsigned long long j = 0; j < XY; j++){
500 result[j] += (T)((w[ai] - lab) * ((double)cur[j] + (double)cur2[j]) / 2.0); 503 result[j] += (T)((w[ai] - lab) * ((double)cur[j] + (double)cur2[j]) / 2.0);
501 } 504 }
502 505
@@ -507,7 +510,7 @@ public: @@ -507,7 +510,7 @@ public:
507 baseline_band(lb, rb, lp, rp, w[ai], cur2); 510 baseline_band(lb, rb, lp, rp, w[ai], cur2);
508 for(unsigned long long j = 0; j < XY; j++) 511 for(unsigned long long j = 0; j < XY; j++)
509 { 512 {
510 - result[j] += (T)((w[ai] - w[ai-1]) * ((double)cur[j] + (double)cur2[j]) / 2.0); 513 + result[j] += (T)((w[ai] - w[ai-1]) * ((double)cur[j] + (double)cur2[j]) / 2.0);
511 } 514 }
512 std::swap(cur,cur2); //swap the band pointers 515 std::swap(cur,cur2); //swap the band pointers
513 } 516 }
@@ -530,9 +533,9 @@ public: @@ -530,9 +533,9 @@ public:
530 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 533 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
531 bool ph_to_ph(T* result, double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, unsigned char* mask = NULL){ 534 bool ph_to_ph(T* result, double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, unsigned char* mask = NULL){
532 535
533 - T* p1 = (T*)malloc(X() * Y() * sizeof(T));  
534 - T* p2 = (T*)malloc(X() * Y() * sizeof(T));  
535 - 536 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  537 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
  538 +
536 //get the two peak band 539 //get the two peak band
537 height(lb1, rb1, pos1, p1); 540 height(lb1, rb1, pos1, p1);
538 height(lb2, rb2, pos2, p2); 541 height(lb2, rb2, pos2, p2);
@@ -546,9 +549,9 @@ public: @@ -546,9 +549,9 @@ public:
546 549
547 free(p1); 550 free(p1);
548 free(p2); 551 free(p2);
549 - return true; 552 + return true;
550 } 553 }
551 - 554 +
552 /// Compute the ratio between a peak area and peak height. 555 /// Compute the ratio between a peak area and peak height.
553 556
554 /// @param lb1 is the label value for the left baseline point for the first peak (numerator) 557 /// @param lb1 is the label value for the left baseline point for the first peak (numerator)
@@ -560,10 +563,10 @@ public: @@ -560,10 +563,10 @@ public:
560 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 563 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
561 bool pa_to_ph(T* result, double lb1, double rb1, double lab1, double rab1, 564 bool pa_to_ph(T* result, double lb1, double rb1, double lab1, double rab1,
562 double lb2, double rb2, double pos, unsigned char* mask = NULL){ 565 double lb2, double rb2, double pos, unsigned char* mask = NULL){
563 -  
564 - T* p1 = (T*)malloc(X() * Y() * sizeof(T));  
565 - T* p2 = (T*)malloc(X() * Y() * sizeof(T));  
566 - 566 +
  567 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  568 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
  569 +
567 //get the area and the peak band 570 //get the area and the peak band
568 area(lb1, rb1, lab1, rab1, p1); 571 area(lb1, rb1, lab1, rab1, p1);
569 height(lb2, rb2, pos, p2); 572 height(lb2, rb2, pos, p2);
@@ -577,9 +580,9 @@ public: @@ -577,9 +580,9 @@ public:
577 580
578 free(p1); 581 free(p1);
579 free(p2); 582 free(p2);
580 - return true;  
581 - }  
582 - 583 + return true;
  584 + }
  585 +
583 /// Compute the ratio between two peak areas. 586 /// Compute the ratio between two peak areas.
584 587
585 /// @param lb1 is the label value for the left baseline point for the first peak (numerator) 588 /// @param lb1 is the label value for the left baseline point for the first peak (numerator)
@@ -589,14 +592,14 @@ public: @@ -589,14 +592,14 @@ public:
589 /// @param lb2 is the label value for the left baseline point for the second peak (denominator) 592 /// @param lb2 is the label value for the left baseline point for the second peak (denominator)
590 /// @param rb2 is the label value for the right baseline point for the second peak (denominator) 593 /// @param rb2 is the label value for the right baseline point for the second peak (denominator)
591 /// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator) 594 /// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator)
592 - /// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator) 595 + /// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator)
593 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 596 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
594 bool pa_to_pa(T* result, double lb1, double rb1, double lab1, double rab1, 597 bool pa_to_pa(T* result, double lb1, double rb1, double lab1, double rab1,
595 double lb2, double rb2, double lab2, double rab2, unsigned char* mask = NULL){ 598 double lb2, double rb2, double lab2, double rab2, unsigned char* mask = NULL){
596 -  
597 - T* p1 = (T*)malloc(X() * Y() * sizeof(T));  
598 - T* p2 = (T*)malloc(X() * Y() * sizeof(T));  
599 - 599 +
  600 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  601 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
  602 +
600 //get the area and the peak band 603 //get the area and the peak band
601 area(lb1, rb1, lab1, rab1, p1); 604 area(lb1, rb1, lab1, rab1, p1);
602 area(lb2, rb2, lab2, rab2, p2); 605 area(lb2, rb2, lab2, rab2, p2);
@@ -610,8 +613,8 @@ public: @@ -610,8 +613,8 @@ public:
610 613
611 free(p1); 614 free(p1);
612 free(p2); 615 free(p2);
613 - return true;  
614 - } 616 + return true;
  617 + }
615 618
616 /// Compute the definite integral of a baseline corrected peak. 619 /// Compute the definite integral of a baseline corrected peak.
617 620
@@ -671,7 +674,7 @@ public: @@ -671,7 +674,7 @@ public:
671 } 674 }
672 baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part 675 baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part
673 baseline_band(lb, rb, lp, rp, w[ai], cur); 676 baseline_band(lb, rb, lp, rp, w[ai], cur);
674 - for(unsigned long long j = 0; j < XY; j++){ 677 + for(unsigned long long j = 0; j < XY; j++){
675 result[j] += (T)((w[ai] - lab) * (w[ai] + lab) * ((double)cur[j] + (double)cur2[j]) / 4.0); 678 result[j] += (T)((w[ai] - lab) * (w[ai] + lab) * ((double)cur[j] + (double)cur2[j]) / 4.0);
676 } 679 }
677 680
@@ -682,7 +685,7 @@ public: @@ -682,7 +685,7 @@ public:
682 baseline_band(lb, rb, lp, rp, w[ai], cur2); 685 baseline_band(lb, rb, lp, rp, w[ai], cur2);
683 for(unsigned long long j = 0; j < XY; j++) 686 for(unsigned long long j = 0; j < XY; j++)
684 { 687 {
685 - result[j] += (T)((w[ai] - w[ai-1]) * (w[ai] + w[ai-1]) * ((double)cur[j] + (double)cur2[j]) / 4.0); 688 + result[j] += (T)((w[ai] - w[ai-1]) * (w[ai] + w[ai-1]) * ((double)cur[j] + (double)cur2[j]) / 4.0);
686 } 689 }
687 std::swap(cur,cur2); //swap the band pointers 690 std::swap(cur,cur2); //swap the band pointers
688 } 691 }
@@ -702,9 +705,9 @@ public: @@ -702,9 +705,9 @@ public:
702 /// @param rab is the label for the end of the peak 705 /// @param rab is the label for the end of the peak
703 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 706 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
704 bool centroid(T* result, double lb, double rb, double lab, double rab, unsigned char* mask = NULL){ 707 bool centroid(T* result, double lb, double rb, double lab, double rab, unsigned char* mask = NULL){
705 - T* p1 = (T*)malloc(X() * Y() * sizeof(T));  
706 - T* p2 = (T*)malloc(X() * Y() * sizeof(T));  
707 - 708 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  709 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
  710 +
708 //get the area and the peak band 711 //get the area and the peak band
709 x_area(lb, rb, lab, rab, p1); 712 x_area(lb, rb, lab, rab, p1);
710 area(lb, rb, lab, rab, p2); 713 area(lb, rb, lab, rab, p2);
@@ -716,7 +719,7 @@ public: @@ -716,7 +719,7 @@ public:
716 719
717 free(p1); 720 free(p1);
718 free(p2); 721 free(p2);
719 - return true; 722 + return true;
720 } 723 }
721 724
722 /// Create a mask based on a given band and threshold value. 725 /// Create a mask based on a given band and threshold value.
@@ -788,7 +791,7 @@ public: @@ -788,7 +791,7 @@ public:
788 T* band = (T*) malloc( Bbytes ); //allocate space for a line 791 T* band = (T*) malloc( Bbytes ); //allocate space for a line
789 792
790 file.seekg(0, std::ios::beg); //seek to the beginning of the file 793 file.seekg(0, std::ios::beg); //seek to the beginning of the file
791 - 794 +
792 size_t p = 0; //create counter variables 795 size_t p = 0; //create counter variables
793 for(size_t xy = 0; xy < XY; xy++){ //for each pixel 796 for(size_t xy = 0; xy < XY; xy++){ //for each pixel
794 if(mask[xy]){ //if the current pixel is masked 797 if(mask[xy]){ //if the current pixel is masked
@@ -800,7 +803,7 @@ public: @@ -800,7 +803,7 @@ public:
800 p++; //increment the pixel pointer 803 p++; //increment the pixel pointer
801 } 804 }
802 else 805 else
803 - file.seekg(Bbytes, std::ios::cur); //otherwise skip this band 806 + file.seekg(Bbytes, std::ios::cur); //otherwise skip this band
804 } 807 }
805 return true; 808 return true;
806 } 809 }
@@ -809,7 +812,7 @@ public: @@ -809,7 +812,7 @@ public:
809 bool sift(std::string outfile, unsigned char* mask, bool PROGRESS = false){ 812 bool sift(std::string outfile, unsigned char* mask, bool PROGRESS = false){
810 813
811 //reset the file pointer to the beginning of the file 814 //reset the file pointer to the beginning of the file
812 - file.seekg(0, std::ios::beg); 815 + file.seekg(0, std::ios::beg);
813 816
814 // open an output stream 817 // open an output stream
815 std::ofstream target(outfile.c_str(), std::ios::binary); 818 std::ofstream target(outfile.c_str(), std::ios::binary);
@@ -862,7 +865,7 @@ public: @@ -862,7 +865,7 @@ public:
862 std::ofstream target(outfile.c_str(), std::ios::binary); 865 std::ofstream target(outfile.c_str(), std::ios::binary);
863 866
864 //reset the file pointer to the beginning of the file 867 //reset the file pointer to the beginning of the file
865 - file.seekg(0, std::ios::beg); 868 + file.seekg(0, std::ios::beg);
866 869
867 //allocate space for a single spectrum 870 //allocate space for a single spectrum
868 unsigned long long B = Z(); 871 unsigned long long B = Z();
@@ -942,10 +945,10 @@ public: @@ -942,10 +945,10 @@ public:
942 /// Note that cuBLAS only supports integer-sized arrays, so there may be issues with large spectra 945 /// Note that cuBLAS only supports integer-sized arrays, so there may be issues with large spectra
943 bool co_matrix_cublas(double* co, double* avg, unsigned char *mask, bool PROGRESS = false){ 946 bool co_matrix_cublas(double* co, double* avg, unsigned char *mask, bool PROGRESS = false){
944 947
945 - cudaError_t cudaStat; 948 + cudaError_t cudaStat;
946 cublasStatus_t stat; 949 cublasStatus_t stat;
947 cublasHandle_t handle; 950 cublasHandle_t handle;
948 - 951 +
949 progress = 0; //initialize the progress to zero (0) 952 progress = 0; //initialize the progress to zero (0)
950 unsigned long long XY = X() * Y(); //calculate the number of elements in a band image 953 unsigned long long XY = X() * Y(); //calculate the number of elements in a band image
951 unsigned long long B = Z(); //calculate the number of spectral elements 954 unsigned long long B = Z(); //calculate the number of spectral elements
@@ -959,7 +962,7 @@ public: @@ -959,7 +962,7 @@ public:
959 cudaStat = cudaMemset(A_dev, 0, B * B * sizeof(double)); //initialize the covariance matrix to zero (0) 962 cudaStat = cudaMemset(A_dev, 0, B * B * sizeof(double)); //initialize the covariance matrix to zero (0)
960 cudaStat = cudaMalloc(&avg_dev, B * sizeof(double)); //allocate space on the CUDA device for the average spectrum 963 cudaStat = cudaMalloc(&avg_dev, B * sizeof(double)); //allocate space on the CUDA device for the average spectrum
961 stat = cublasSetVector((int)B, sizeof(double), avg, 1, avg_dev, 1); //copy the average spectrum to the CUDA device 964 stat = cublasSetVector((int)B, sizeof(double), avg, 1, avg_dev, 1); //copy the average spectrum to the CUDA device
962 - 965 +
963 double ger_alpha = 1.0/(double)XY; //scale the outer product by the inverse of the number of samples (mean outer product) 966 double ger_alpha = 1.0/(double)XY; //scale the outer product by the inverse of the number of samples (mean outer product)
964 double axpy_alpha = -1; //multiplication factor for the average spectrum (in order to perform a subtraction) 967 double axpy_alpha = -1; //multiplication factor for the average spectrum (in order to perform a subtraction)
965 968
@@ -994,7 +997,7 @@ public: @@ -994,7 +997,7 @@ public:
994 return true; 997 return true;
995 } 998 }
996 #endif 999 #endif
997 - 1000 +
998 /// Calculate the covariance matrix for all masked pixels in the image with 64-bit floating point precision. 1001 /// Calculate the covariance matrix for all masked pixels in the image with 64-bit floating point precision.
999 1002
1000 /// @param co is a pointer to pre-allocated memory of size [B * B] that stores the resulting covariance matrix 1003 /// @param co is a pointer to pre-allocated memory of size [B * B] that stores the resulting covariance matrix
@@ -1074,7 +1077,7 @@ public: @@ -1074,7 +1077,7 @@ public:
1074 } 1077 }
1075 } 1078 }
1076 } 1079 }
1077 - 1080 +
1078 target.write(reinterpret_cast<const char*>(rs), sizeof(T) * M); //write the projected vector 1081 target.write(reinterpret_cast<const char*>(rs), sizeof(T) * M); //write the projected vector
1079 if(PROGRESS) progress = (double)(xy+1) / XY * 100; 1082 if(PROGRESS) progress = (double)(xy+1) / XY * 100;
1080 } 1083 }
@@ -1104,7 +1107,7 @@ public: @@ -1104,7 +1107,7 @@ public:
1104 memset(s, 0, sizeof(T) * B); //initialize the spectrum to zero (0) 1107 memset(s, 0, sizeof(T) * B); //initialize the spectrum to zero (0)
1105 for(unsigned long long c = 0; c < C; c++){ //for each basis vector coefficient 1108 for(unsigned long long c = 0; c < C; c++){ //for each basis vector coefficient
1106 bv = &basis[c * B]; //assign 'bv' to the beginning of the basis vector 1109 bv = &basis[c * B]; //assign 'bv' to the beginning of the basis vector
1107 - for(unsigned long long b = 0; b < B; b++){ //for each component of the basis vector 1110 + for(unsigned long long b = 0; b < B; b++){ //for each component of the basis vector
1108 s[b] += (T)((double)coeff[c] * bv[b] + center[b]); //calculate the contribution of each element of the basis vector in the final spectrum 1111 s[b] += (T)((double)coeff[c] * bv[b] + center[b]); //calculate the contribution of each element of the basis vector in the final spectrum
1109 } 1112 }
1110 } 1113 }
@@ -1172,7 +1175,7 @@ public: @@ -1172,7 +1175,7 @@ public:
1172 //read the cropped spectral region 1175 //read the cropped spectral region
1173 file.read( (char*) temp, L ); 1176 file.read( (char*) temp, L );
1174 //pixel(temp, sp + x + y * X()); 1177 //pixel(temp, sp + x + y * X());
1175 - out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file 1178 + out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file
1176 1179
1177 file.seekg(jump_sample, std::ios::cur); 1180 file.seekg(jump_sample, std::ios::cur);
1178 1181
@@ -1253,7 +1256,7 @@ public: @@ -1253,7 +1256,7 @@ public:
1253 memset(spec, 0, spec_bytes); 1256 memset(spec, 0, spec_bytes);
1254 out.write((char*)spec, spec_bytes); //write to the output file 1257 out.write((char*)spec, spec_bytes); //write to the output file
1255 } 1258 }
1256 - if(PROGRESS) progress = (double)( (y+1) * S[0] + 1) / (double) (S[0] * S[1]) * 100; 1259 + if(PROGRESS) progress = (double)( (y+1) * S[0] + 1) / (double) (S[0] * S[1]) * 100;
1257 } 1260 }
1258 } 1261 }
1259 1262
@@ -1267,7 +1270,7 @@ public: @@ -1267,7 +1270,7 @@ public:
1267 1270
1268 void convolve(std::string outfile, std::vector<double> C, size_t start, size_t end, unsigned char* mask = NULL, bool PROGRESS = false){ 1271 void convolve(std::string outfile, std::vector<double> C, size_t start, size_t end, unsigned char* mask = NULL, bool PROGRESS = false){
1269 std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing 1272 std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing
1270 - 1273 +
1271 size_t N = end - start + 1; //number of bands in the output spectrum 1274 size_t N = end - start + 1; //number of bands in the output spectrum
1272 size_t Nb = N * sizeof(T); //size of the output spectrum in bytes 1275 size_t Nb = N * sizeof(T); //size of the output spectrum in bytes
1273 size_t B = Z(); //calculate the number of values in a spectrum 1276 size_t B = Z(); //calculate the number of values in a spectrum
@@ -1297,8 +1300,8 @@ public: @@ -1297,8 +1300,8 @@ public:
1297 1300
1298 void deriv(std::string outfile, size_t d, size_t order, const std::vector<double> w, unsigned char* mask = NULL, bool PROGRESS = false){ 1301 void deriv(std::string outfile, size_t d, size_t order, const std::vector<double> w, unsigned char* mask = NULL, bool PROGRESS = false){
1299 std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing 1302 std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing
1300 -  
1301 - 1303 +
  1304 +
1302 size_t B = Z(); //calculate the number of values in a spectrum 1305 size_t B = Z(); //calculate the number of values in a spectrum
1303 size_t Bb = B * sizeof(T); //calculate the size of a spectrum in bytes 1306 size_t Bb = B * sizeof(T); //calculate the size of a spectrum in bytes
1304 1307
@@ -37,6 +37,9 @@ protected: @@ -37,6 +37,9 @@ protected:
37 using hsi<T>::w; //use the wavelength array in stim::hsi 37 using hsi<T>::w; //use the wavelength array in stim::hsi
38 using hsi<T>::nnz; 38 using hsi<T>::nnz;
39 using binary<T>::progress; 39 using binary<T>::progress;
  40 + using hsi<T>::X;
  41 + using hsi<T>::Y;
  42 + using hsi<T>::Z;
40 43
41 public: 44 public:
42 45
@@ -55,11 +58,11 @@ public: @@ -55,11 +58,11 @@ public:
55 /// @param B is the number of samples (bands) along dimension 3 58 /// @param B is the number of samples (bands) along dimension 3
56 /// @param header_offset is the number of bytes (if any) in the binary header 59 /// @param header_offset is the number of bytes (if any) in the binary header
57 /// @param wavelengths is an STL vector of size B specifying a numerical label for each band 60 /// @param wavelengths is an STL vector of size B specifying a numerical label for each band
58 - bool open(std::string filename,  
59 - unsigned long long X,  
60 - unsigned long long Y,  
61 - unsigned long long B,  
62 - unsigned long long header_offset, 61 + bool open(std::string filename,
  62 + unsigned long long X,
  63 + unsigned long long Y,
  64 + unsigned long long B,
  65 + unsigned long long header_offset,
63 std::vector<double> wavelengths){ 66 std::vector<double> wavelengths){
64 67
65 //copy the wavelengths to the BSQ file structure 68 //copy the wavelengths to the BSQ file structure
@@ -366,7 +369,7 @@ public: @@ -366,7 +369,7 @@ public:
366 out.write((char*)band, XYb); 369 out.write((char*)band, XYb);
367 if(PROGRESS) progress = (double) (b+1) / (double)B * 50 + 50; 370 if(PROGRESS) progress = (double) (b+1) / (double)B * 50 + 50;
368 } 371 }
369 - 372 +
370 } 373 }
371 374
372 /// Convert the current BSQ file to a BIL file with the specified file name. 375 /// Convert the current BSQ file to a BIL file with the specified file name.
@@ -489,7 +492,7 @@ public: @@ -489,7 +492,7 @@ public:
489 } 492 }
490 493
491 //find the indices of the left and right baseline points 494 //find the indices of the left and right baseline points
492 - while (lab >= w[ai]){ 495 + while (lab >= w[ai]){
493 ai++; 496 ai++;
494 } 497 }
495 while (rab <= w[bi]){ 498 while (rab <= w[bi]){
@@ -822,13 +825,13 @@ public: @@ -822,13 +825,13 @@ public:
822 i++; 825 i++;
823 //std::cout<<i<<std::endl; 826 //std::cout<<i<<std::endl;
824 } 827 }
825 - 828 +
826 } 829 }
827 } 830 }
828 831
829 return true; 832 return true;
830 } 833 }
831 - 834 +
832 /// Saves to disk only those spectra corresponding to mask values != 0 835 /// Saves to disk only those spectra corresponding to mask values != 0
833 /// @param outfile is the name of the sifted ENVI file to be written to disk 836 /// @param outfile is the name of the sifted ENVI file to be written to disk
834 /// @param unsigned char* p is the mask file used for sifting 837 /// @param unsigned char* p is the mask file used for sifting
@@ -853,7 +856,7 @@ public: @@ -853,7 +856,7 @@ public:
853 } 856 }
854 else{ 857 else{
855 continue; 858 continue;
856 - } 859 + }
857 } 860 }
858 if(PROGRESS) progress = (double)(i+1)/ B * 100; 861 if(PROGRESS) progress = (double)(i+1)/ B * 100;
859 } 862 }
@@ -973,7 +976,7 @@ public: @@ -973,7 +976,7 @@ public:
973 free(temp); 976 free(temp);
974 return true; 977 return true;
975 } 978 }
976 - 979 +
977 /// Crop a region of the image and save it to a new file. 980 /// Crop a region of the image and save it to a new file.
978 981
979 /// @param outfile is the file name for the new cropped image 982 /// @param outfile is the file name for the new cropped image
@@ -1035,7 +1038,7 @@ public: @@ -1035,7 +1038,7 @@ public:
1035 /// @param outfile is the file name for the output hyperspectral image (with trimmed bands) 1038 /// @param outfile is the file name for the output hyperspectral image (with trimmed bands)
1036 /// @param b is an array of bands to be eliminated 1039 /// @param b is an array of bands to be eliminated
1037 void trim(std::string outfile, std::vector<size_t> band_array, bool PROGRESS = false){ 1040 void trim(std::string outfile, std::vector<size_t> band_array, bool PROGRESS = false){
1038 - 1041 +
1039 std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing 1042 std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing
1040 file.seekg(0, std::ios::beg); //move to the beginning of the input file 1043 file.seekg(0, std::ios::beg); //move to the beginning of the input file
1041 1044
@@ -1185,7 +1188,7 @@ public: @@ -1185,7 +1188,7 @@ public:
1185 1188
1186 void deriv(std::string outfile, size_t d, size_t order, const std::vector<double> w = std::vector<double>(), unsigned char* mask = NULL, bool PROGRESS = false){ 1189 void deriv(std::string outfile, size_t d, size_t order, const std::vector<double> w = std::vector<double>(), unsigned char* mask = NULL, bool PROGRESS = false){
1187 std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing 1190 std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing
1188 - 1191 +
1189 size_t XY = X() * Y(); //calculate the number of values in a band 1192 size_t XY = X() * Y(); //calculate the number of values in a band
1190 size_t XYb = XY * sizeof(T); //calculate the size of a band (frame) in bytes 1193 size_t XYb = XY * sizeof(T); //calculate the size of a band (frame) in bytes
1191 size_t B = Z(); 1194 size_t B = Z();
@@ -1227,7 +1230,7 @@ public: @@ -1227,7 +1230,7 @@ public:
1227 } 1230 }
1228 } 1231 }
1229 } 1232 }
1230 - out.write((char*)outband, XYb); //output the band 1233 + out.write((char*)outband, XYb); //output the band
1231 if(PROGRESS) progress = (double)(b+1) / (double)B * 100; 1234 if(PROGRESS) progress = (double)(b+1) / (double)B * 100;
1232 } 1235 }
1233 1236
@@ -32,6 +32,9 @@ protected: @@ -32,6 +32,9 @@ protected:
32 32
33 std::vector<double> w; //band wavelengths 33 std::vector<double> w; //band wavelengths
34 34
  35 + using binary<T>::R;
  36 + using binary<T>::progress;
  37 +
35 unsigned long long X(){ return R[O[0]]; } 38 unsigned long long X(){ return R[O[0]]; }
36 unsigned long long Y(){ return R[O[1]]; } 39 unsigned long long Y(){ return R[O[1]]; }
37 unsigned long long Z(){ return R[O[2]]; } 40 unsigned long long Z(){ return R[O[2]]; }
@@ -70,8 +73,8 @@ protected: @@ -70,8 +73,8 @@ protected:
70 } 73 }
71 74
72 /// Get the list of band numbers that bound a list of wavelengths 75 /// Get the list of band numbers that bound a list of wavelengths
73 - void band_bounds(std::vector<double> wavelengths,  
74 - std::vector<unsigned long long>& low_bands, 76 + void band_bounds(std::vector<double> wavelengths,
  77 + std::vector<unsigned long long>& low_bands,
75 std::vector<unsigned long long>& high_bands){ 78 std::vector<unsigned long long>& high_bands){
76 79
77 unsigned long long W = w.size(); //get the number of wavelengths in the list 80 unsigned long long W = w.size(); //get the number of wavelengths in the list
@@ -92,7 +95,7 @@ protected: @@ -92,7 +95,7 @@ protected:
92 band_bounds(wavelength, low, high); //get the surrounding band indices 95 band_bounds(wavelength, low, high); //get the surrounding band indices
93 96
94 if(high == w.size()) return s[w.size()-1]; //if the high band is above the wavelength range, return the highest wavelength value 97 if(high == w.size()) return s[w.size()-1]; //if the high band is above the wavelength range, return the highest wavelength value
95 - 98 +
96 return lerp(wavelength, s[low], w[low], s[high], w[high]); 99 return lerp(wavelength, s[low], w[low], s[high], w[high]);
97 } 100 }
98 101
@@ -140,11 +143,11 @@ public: @@ -140,11 +143,11 @@ public:
140 size_t XY = X() * Y(); 143 size_t XY = X() * Y();
141 memset(mask, 255, XY * sizeof(unsigned char)); //initialize the mask to zero (0) 144 memset(mask, 255, XY * sizeof(unsigned char)); //initialize the mask to zero (0)
142 T* page = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate space for a page of data 145 T* page = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate space for a page of data
143 - 146 +
144 for(size_t p = 0; p < R[2]; p++){ //for each page 147 for(size_t p = 0; p < R[2]; p++){ //for each page
145 binary<T>::read_page(page, p); //read a page 148 binary<T>::read_page(page, p); //read a page
146 for(size_t i = 0; i < R[0] * R[1]; i++){ //for each pixel in that page 149 for(size_t i = 0; i < R[0] * R[1]; i++){ //for each pixel in that page
147 - 150 +
148 #ifdef _WIN32 151 #ifdef _WIN32
149 if(!_finite(page[i])){ //if the value at index i is finite 152 if(!_finite(page[i])){ //if the value at index i is finite
150 #else 153 #else
@@ -171,7 +174,7 @@ public: @@ -171,7 +174,7 @@ public:
171 } 174 }
172 175
173 /// Calculates the necessary image size required to combine two images given the specified offset (position) of the second image 176 /// Calculates the necessary image size required to combine two images given the specified offset (position) of the second image
174 - void calc_combined_size(long long xp, long long yp, long long Sx, long long Sy, 177 + void calc_combined_size(long long xp, long long yp, long long Sx, long long Sy,
175 size_t& Sfx, size_t& Sfy, 178 size_t& Sfx, size_t& Sfy,
176 size_t& p0_x, size_t& p0_y, 179 size_t& p0_x, size_t& p0_y,
177 size_t& p1_x, size_t& p1_y){ 180 size_t& p1_x, size_t& p1_y){
@@ -179,7 +182,7 @@ public: @@ -179,7 +182,7 @@ public:
179 calc_combined_size(xp, yp, Sx, Sy, Sfx, Sfy); 182 calc_combined_size(xp, yp, Sx, Sy, Sfx, Sfy);
180 183
181 p0_x = p0_y = p1_x = p1_y = 0; //initialize all boundary positions to zero 184 p0_x = p0_y = p1_x = p1_y = 0; //initialize all boundary positions to zero
182 - 185 +
183 if(xp < 0) p0_x = -xp; //set the left positions of the current and source image 186 if(xp < 0) p0_x = -xp; //set the left positions of the current and source image
184 else p1_x = xp; 187 else p1_x = xp;
185 if(yp < 0) p0_y = -yp; 188 if(yp < 0) p0_y = -yp;
@@ -191,8 +194,8 @@ public: @@ -191,8 +194,8 @@ public:
191 194
192 size_t w = X(); //calculate the number of pixels in a line 195 size_t w = X(); //calculate the number of pixels in a line
193 size_t wb = w * sizeof(T); //calculate the number of bytes in a line 196 size_t wb = w * sizeof(T); //calculate the number of bytes in a line
194 - pw = (X() + x0 + x1); //calculate the width of the padded image  
195 - pwb = pw * sizeof(T); //calculate the number of bytes in a line of the padded image 197 + size_t pw = (X() + x0 + x1); //calculate the width of the padded image
  198 + size_t pwb = pw * sizeof(T); //calculate the number of bytes in a line of the padded image
196 199
197 for(size_t y = 0; y < Y(); y++){ //for each line in the real image 200 for(size_t y = 0; y < Y(); y++){ //for each line in the real image
198 memcpy( &padded[ (y + y0) * pw + x0 ], &src[y * w], wb ); //use memcpy to copy the line to the appropriate place in the padded image 201 memcpy( &padded[ (y + y0) * pw + x0 ], &src[y * w], wb ); //use memcpy to copy the line to the appropriate place in the padded image
stim/grids/image_stack.h
@@ -68,7 +68,7 @@ public: @@ -68,7 +68,7 @@ public:
68 stim::image<T> I(file_list[i].str()); 68 stim::image<T> I(file_list[i].str());
69 69
70 //retrieve the interlaced data from the image - store it in the grid 70 //retrieve the interlaced data from the image - store it in the grid
71 - I.data_interleaved(&ptr[ i * R[0] * R[1] * R[2] ]); 71 + I.get_interleaved_rgb(&ptr[ i * R[0] * R[1] * R[2] ]);
72 72
73 } 73 }
74 } 74 }
stim/image/image.h
@@ -32,7 +32,7 @@ class image{ @@ -32,7 +32,7 @@ class image{
32 void unalloc(){ //frees any resources associated with the image 32 void unalloc(){ //frees any resources associated with the image
33 if(img) free(img); //if memory has been allocated, free it 33 if(img) free(img); //if memory has been allocated, free it
34 } 34 }
35 - 35 +
36 36
37 void clear(){ //clears all image data 37 void clear(){ //clears all image data
38 unalloc(); //unallocate previous memory 38 unalloc(); //unallocate previous memory
@@ -53,7 +53,7 @@ class image{ @@ -53,7 +53,7 @@ class image{
53 size_t idx(size_t x, size_t y, size_t c = 0){ 53 size_t idx(size_t x, size_t y, size_t c = 0){
54 return y * C() * X() + x * C() + c; 54 return y * C() * X() + x * C() + c;
55 } 55 }
56 - 56 +
57 57
58 int cv_type(){ 58 int cv_type(){
59 if(std::is_same<T, unsigned char>::value) return CV_MAKETYPE(CV_8U, (int)C()); 59 if(std::is_same<T, unsigned char>::value) return CV_MAKETYPE(CV_8U, (int)C());
@@ -63,7 +63,7 @@ class image{ @@ -63,7 +63,7 @@ class image{
63 if(std::is_same<T, int>::value) return CV_MAKETYPE(CV_32S, (int)C()); 63 if(std::is_same<T, int>::value) return CV_MAKETYPE(CV_32S, (int)C());
64 if(std::is_same<T, float>::value) return CV_MAKETYPE(CV_32F, (int)C()); 64 if(std::is_same<T, float>::value) return CV_MAKETYPE(CV_32F, (int)C());
65 if(std::is_same<T, double>::value) return CV_MAKETYPE(CV_64F, (int)C()); 65 if(std::is_same<T, double>::value) return CV_MAKETYPE(CV_64F, (int)C());
66 - 66 +
67 std::cout<<"ERROR in stim::image::cv_type - no valid data type found"<<std::endl; 67 std::cout<<"ERROR in stim::image::cv_type - no valid data type found"<<std::endl;
68 exit(1); 68 exit(1);
69 } 69 }
@@ -94,7 +94,7 @@ public: @@ -94,7 +94,7 @@ public:
94 } 94 }
95 95
96 /// Create a new image from scratch given a number of samples and channels 96 /// Create a new image from scratch given a number of samples and channels
97 - image(size_t x, size_t y = 1, size_t c = 1){ 97 + image(size_t x, size_t y = 1, size_t c = 1){
98 allocate(x, y, c); 98 allocate(x, y, c);
99 } 99 }
100 100
@@ -148,7 +148,7 @@ public: @@ -148,7 +148,7 @@ public:
148 if(C() == 1) 148 if(C() == 1)
149 memcpy(buffer, img, bytes()); 149 memcpy(buffer, img, bytes());
150 else if(C() == 3) 150 else if(C() == 3)
151 - data_interleaved_bgr(buffer); 151 + get_interleaved_bgr(buffer);
152 cv::Mat cvImage((int)Y(), (int)X(), cv_type(), buffer); 152 cv::Mat cvImage((int)Y(), (int)X(), cv_type(), buffer);
153 cv::imwrite(filename, cvImage); 153 cv::imwrite(filename, cvImage);
154 } 154 }
@@ -170,7 +170,7 @@ public: @@ -170,7 +170,7 @@ public:
170 } 170 }
171 } 171 }
172 172
173 - void data_interleaved_bgr(T* data){ 173 + void get_interleaved_bgr(T* data){
174 174
175 //for each channel 175 //for each channel
176 for(size_t y = 0; y < Y(); y++){ 176 for(size_t y = 0; y < Y(); y++){
@@ -182,6 +182,11 @@ public: @@ -182,6 +182,11 @@ public:
182 } 182 }
183 } 183 }
184 184
  185 + void get_interleaved_rgb(T* data){
  186 + memcpy(data, img, bytes());
  187 + }
  188 +
  189 +
185 image<T> channel(size_t c){ 190 image<T> channel(size_t c){
186 191
187 //create a new image 192 //create a new image
@@ -284,7 +289,7 @@ public: @@ -284,7 +289,7 @@ public:
284 return s; //return the index list 289 return s; //return the index list
285 } 290 }
286 291
287 - 292 +
288 /// Returns the maximum pixel value in the image 293 /// Returns the maximum pixel value in the image
289 T maxv(){ 294 T maxv(){
290 T max_val = img[0]; //initialize the maximum value to the first one 295 T max_val = img[0]; //initialize the maximum value to the first one
@@ -294,10 +299,10 @@ public: @@ -294,10 +299,10 @@ public:
294 299
295 if (img[n] > max_val){ //if the value is higher than the current max 300 if (img[n] > max_val){ //if the value is higher than the current max
296 max_val = img[n]; 301 max_val = img[n];
297 - } 302 + }
298 } 303 }
299 304
300 - return max; 305 + return max_val;
301 } 306 }
302 307
303 /// Returns the maximum pixel value in the image 308 /// Returns the maximum pixel value in the image
@@ -308,10 +313,10 @@ public: @@ -308,10 +313,10 @@ public:
308 for (size_t n=0; n<N; n++){ //for every value 313 for (size_t n=0; n<N; n++){ //for every value
309 if (img[n] < min_val){ //if the value is higher than the current max 314 if (img[n] < min_val){ //if the value is higher than the current max
310 min_val = img[n]; 315 min_val = img[n];
311 - } 316 + }
312 } 317 }
313 318
314 - return max; 319 + return min_val;
315 } 320 }
316 321
317 /// Invert an image by calculating I1 = alpha - I0, where alpha is the maximum image value 322 /// Invert an image by calculating I1 = alpha - I0, where alpha is the maximum image value
@@ -320,7 +325,7 @@ public: @@ -320,7 +325,7 @@ public:
320 image<T> r(X(), Y(), C()); //allocate space for the resulting image 325 image<T> r(X(), Y(), C()); //allocate space for the resulting image
321 for(size_t n = 0; n < N; n++) 326 for(size_t n = 0; n < N; n++)
322 r.img[n] = white_val - img[n]; //perform the inversion 327 r.img[n] = white_val - img[n]; //perform the inversion
323 - 328 +
324 return r; //return the inverted image 329 return r; //return the inverted image
325 } 330 }
326 331
@@ -330,7 +335,7 @@ public: @@ -330,7 +335,7 @@ public:
330 } 335 }
331 336
332 image<T> convolve2(image<T> mask){ 337 image<T> convolve2(image<T> mask){
333 - 338 +
334 std::cout<<"ERROR stim::image::convolve2 - function has been broken, and shouldn't really be in here."<<std::endl; 339 std::cout<<"ERROR stim::image::convolve2 - function has been broken, and shouldn't really be in here."<<std::endl;
335 exit(1); 340 exit(1);
336 } 341 }
@@ -347,7 +352,7 @@ public: @@ -347,7 +352,7 @@ public:
347 std::cout<<"ERROR stim::image::set_interleaved3 - stim::image no longer supports 3D images."<<std::endl; 352 std::cout<<"ERROR stim::image::set_interleaved3 - stim::image no longer supports 3D images."<<std::endl;
348 exit(1); 353 exit(1);
349 } 354 }
350 - 355 +
351 }; 356 };
352 357
353 }; //end namespace stim 358 }; //end namespace stim
stim/math/circle.h
@@ -175,5 +175,4 @@ public: @@ -175,5 +175,4 @@ public:
175 175
176 }; 176 };
177 } 177 }
178 ->>>>>>> origin/Master_Clone_W_Branch  
179 #endif 178 #endif
stim/parser/filename.h
@@ -22,6 +22,9 @@ @@ -22,6 +22,9 @@
22 #include <iostream> 22 #include <iostream>
23 23
24 #include "../parser/parser.h" 24 #include "../parser/parser.h"
  25 +#ifdef BOOST_PRECOMPILED
  26 +#include <boost/filesystem.hpp>
  27 +#endif
25 namespace stim{ 28 namespace stim{
26 29
27 //filename class designed to work with both Windows and Unix 30 //filename class designed to work with both Windows and Unix
@@ -234,8 +237,40 @@ public: @@ -234,8 +237,40 @@ public:
234 } 237 }
235 return file_list; 238 return file_list;
236 #else 239 #else
237 - std::cout<<"File lists aren't currently supported on Unix/Linux systems."<<std::endl;  
238 - exit(1); 240 +
  241 + boost::filesystem::path p(dir()); //create a path from the current filename
  242 + std::vector<stim::filename> file_list;
  243 + if(boost::filesystem::exists(p)){
  244 + if(boost::filesystem::is_directory(p)){
  245 + typedef std::vector<boost::filesystem::path> vec; // store paths,
  246 + vec v; // so we can sort them later
  247 + std::copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(v));
  248 + std::sort(v.begin(), v.end()); // sort, since directory iteration
  249 + // is not ordered on some file systems
  250 + //compare file names to the current template (look for wild cards)
  251