Commit 724ec3470fd1d9a8570e991a2bfa6e5a97e57487

Authored by David Mayerich
1 parent 5f81932b

simplified the ENVI classes, moved more stuff into binary.h

@@ -25,6 +25,16 @@ protected: @@ -25,6 +25,16 @@ protected:
25 25
26 std::vector<double> w; //band wavelength 26 std::vector<double> w; //band wavelength
27 27
  28 + unsigned long X(){
  29 + return R[0];
  30 + }
  31 + unsigned long Y(){
  32 + return R[2];
  33 + }
  34 + unsigned long Z(){
  35 + return R[1];
  36 + }
  37 +
28 public: 38 public:
29 39
30 using binary<T>::open; 40 using binary<T>::open;
@@ -43,7 +53,7 @@ public: @@ -43,7 +53,7 @@ public:
43 53
44 w = wavelengths; 54 w = wavelengths;
45 55
46 - return open(filename, vec<unsigned int>(X, Y, B), header_offset); 56 + return open(filename, vec<unsigned int>(X, B, Y), header_offset);
47 57
48 } 58 }
49 59
@@ -52,19 +62,20 @@ public: @@ -52,19 +62,20 @@ public:
52 /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. 62 /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size.
53 /// @param page <= B is the integer number of the band to be copied. 63 /// @param page <= B is the integer number of the band to be copied.
54 bool band_index( T * p, unsigned int page){ 64 bool band_index( T * p, unsigned int page){
  65 + //return binary<T>::read_plane_1(p, page);
55 66
56 - unsigned int L = R[0] * sizeof(T); //caculate the number of bytes in a sample line  
57 - unsigned int jump = R[0] * (R[2] - 1) * sizeof(T); 67 + unsigned int L = X() * sizeof(T); //caculate the number of bytes in a sample line
  68 + unsigned int jump = X() * (Z() - 1) * sizeof(T);
58 69
59 - if (page >= R[2]){ //make sure the bank number is right 70 + if (page >= Z()){ //make sure the bank number is right
60 std::cout<<"ERROR: page out of range"<<std::endl; 71 std::cout<<"ERROR: page out of range"<<std::endl;
61 return false; 72 return false;
62 } 73 }
63 74
64 - file.seekg(R[0] * page * sizeof(T), std::ios::beg);  
65 - for (unsigned i = 0; i < R[1]; i++) 75 + file.seekg(X() * page * sizeof(T), std::ios::beg);
  76 + for (unsigned i = 0; i < Y(); i++)
66 { 77 {
67 - file.read((char *)(p + i * R[0]), L); 78 + file.read((char *)(p + i * X()), L);
68 file.seekg( jump, std::ios::cur); 79 file.seekg( jump, std::ios::cur);
69 } 80 }
70 81
@@ -81,7 +92,7 @@ public: @@ -81,7 +92,7 @@ public:
81 if(w.size() == 0) 92 if(w.size() == 0)
82 return band_index(p, (unsigned int)wavelength); 93 return band_index(p, (unsigned int)wavelength);
83 94
84 - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band 95 + unsigned int XY = X() * Y(); //calculate the number of pixels in a band
85 unsigned int S = XY * sizeof(T); //calculate the number of bytes of a band 96 unsigned int S = XY * sizeof(T); //calculate the number of bytes of a band
86 97
87 unsigned page=0; //bands around the wavelength 98 unsigned page=0; //bands around the wavelength
@@ -99,8 +110,8 @@ public: @@ -99,8 +110,8 @@ public:
99 { 110 {
100 page++; 111 page++;
101 //if wavelength is larger than the last wavelength in header file 112 //if wavelength is larger than the last wavelength in header file
102 - if (page == R[2]) {  
103 - band_index(p, R[2]-1); 113 + if (page == Z()) {
  114 + band_index(p, Z()-1);
104 return true; 115 return true;
105 } 116 }
106 } 117 }
@@ -127,12 +138,15 @@ public: @@ -127,12 +138,15 @@ public:
127 return true; 138 return true;
128 } 139 }
129 140
130 - //get YZ line from the a Y slice, Y slice data should be already IN the MEMORY  
131 - bool getYZ(T* p, T* c, double wavelength) 141 + /// Retrieves a band of x values from a given xz plane.
  142 +
  143 + /// @param p is a pointer to pre-allocated memory at least Z * sizeof(T) in size
  144 + /// @param c is a pointer to an existing XZ plane (size X*Z*sizeof(T))
  145 + /// @param wavelength is the wavelength to retrieve
  146 + bool read_x_from_xz(T* p, T* c, double wavelength)
132 { 147 {
133 - unsigned int X = R[0]; //calculate the number of pixels in a sample  
134 - unsigned int B = R[2];  
135 - unsigned int L = X * sizeof(T); 148 + unsigned int B = Z();
  149 + unsigned int L = X() * sizeof(T);
136 150
137 unsigned page=0; //samples around the wavelength 151 unsigned page=0; //samples around the wavelength
138 T * p1; 152 T * p1;
@@ -151,7 +165,7 @@ public: @@ -151,7 +165,7 @@ public:
151 page++; 165 page++;
152 //if wavelength is larger than the last wavelength in header file 166 //if wavelength is larger than the last wavelength in header file
153 if (page == B) { 167 if (page == B) {
154 - memcpy(p, c + (B - 1) * X, L); 168 + memcpy(p, c + (B - 1) * X(), L);
155 return true; 169 return true;
156 } 170 }
157 } 171 }
@@ -160,16 +174,16 @@ public: @@ -160,16 +174,16 @@ public:
160 p1=(T*)malloc( L ); //memory allocation 174 p1=(T*)malloc( L ); //memory allocation
161 p2=(T*)malloc( L ); 175 p2=(T*)malloc( L );
162 176
163 - memcpy(p1, c + (page - 1) * X, L);  
164 - memcpy(p2, c + page * X, L); 177 + memcpy(p1, c + (page - 1) * X(), L);
  178 + memcpy(p2, c + page * X(), L);
165 179
166 - for(unsigned i=0; i < X; i++){ 180 + for(unsigned i=0; i < X(); i++){
167 double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); 181 double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]);
168 p[i] = (p2[i] - p1[i]) * r + p1[i]; 182 p[i] = (p2[i] - p1[i]) * r + p1[i];
169 } 183 }
170 } 184 }
171 else //if the wavelength is equal to a wavelength in header file 185 else //if the wavelength is equal to a wavelength in header file
172 - memcpy(p, c + page * X, L); 186 + memcpy(p, c + page * X(), L);
173 187
174 return true; 188 return true;
175 } 189 }
@@ -180,24 +194,7 @@ public: @@ -180,24 +194,7 @@ public:
180 /// @param x is the x-coordinate (dimension 1) of the spectrum. 194 /// @param x is the x-coordinate (dimension 1) of the spectrum.
181 /// @param y is the y-coordinate (dimension 2) of the spectrum. 195 /// @param y is the y-coordinate (dimension 2) of the spectrum.
182 bool spectrum(T * p, unsigned x, unsigned y){ 196 bool spectrum(T * p, unsigned x, unsigned y){
183 -  
184 - if ( x >= R[0] || y >= R[1]){ //make sure the sample and line number is right  
185 - std::cout<<"ERROR: sample or line out of range"<<std::endl;  
186 - exit(1);  
187 - }  
188 -  
189 - unsigned jump = (R[0] - 1) * sizeof(T);  
190 -  
191 - file.seekg((y * R[0] * R[2] + x) * sizeof(T), std::ios::beg);  
192 -  
193 - for(unsigned i = 0; i < R[2]; i++)  
194 - {  
195 - //point to the certain sample and line  
196 - file.read((char *)(p + i), sizeof(T));  
197 - file.seekg(jump, std::ios::cur);  
198 - }  
199 -  
200 - return true; 197 + return binary<T>::read_line_02(p, x, y);
201 } 198 }
202 199
203 /// Retrieve a single pixel and stores it in pre-allocated memory. 200 /// Retrieve a single pixel and stores it in pre-allocated memory.
@@ -207,26 +204,16 @@ public: @@ -207,26 +204,16 @@ public:
207 bool pixel(T * p, unsigned n){ 204 bool pixel(T * p, unsigned n){
208 205
209 //calculate the corresponding x, y 206 //calculate the corresponding x, y
210 - unsigned int x = n % R[0];  
211 - unsigned int y = n / R[0]; 207 + unsigned int x = n % X();
  208 + unsigned int y = n / X();
212 209
213 //get the pixel 210 //get the pixel
214 return spectrum(p, x, y); 211 return spectrum(p, x, y);
215 } 212 }
216 213
217 //given a Y ,return a XZ slice 214 //given a Y ,return a XZ slice
218 - bool getY(T * p, unsigned y)  
219 - {  
220 - if ( y >= R[1]){ //make sure the line number is right  
221 - std::cout<<"ERROR: line out of range"<<std::endl;  
222 - exit(1);  
223 - }  
224 -  
225 - file.seekg(y * R[2] * R[0] * sizeof(T), std::ios::beg);  
226 - file.read((char *)p, sizeof(T) * R[2] * R[0]);  
227 -  
228 - return true;  
229 - 215 + bool read_plane_y(T * p, unsigned y){
  216 + return binary<T>::read_plane_2(p, y);
230 } 217 }
231 218
232 219
@@ -242,19 +229,18 @@ public: @@ -242,19 +229,18 @@ public:
242 std::string headername = outname + ".hdr"; //the header file name 229 std::string headername = outname + ".hdr"; //the header file name
243 230
244 //simplify image resolution 231 //simplify image resolution
245 - unsigned int ZX = R[2] * R[0]; //calculate the number of points in a Y slice 232 + unsigned int ZX = Z() * X(); //calculate the number of points in a Y slice
246 unsigned int L = ZX * sizeof(T); //calculate the number of bytes of a Y slice 233 unsigned int L = ZX * sizeof(T); //calculate the number of bytes of a Y slice
247 - unsigned int B = R[2];  
248 - unsigned int X = R[0];  
249 - 234 + unsigned int B = Z();
  235 +
250 T* c; //pointer to the current Y slice 236 T* c; //pointer to the current Y slice
251 c = (T*)malloc(L); //memory allocation 237 c = (T*)malloc(L); //memory allocation
252 238
253 T* a; //pointer to the two YZ lines surrounding the current YZ line 239 T* a; //pointer to the two YZ lines surrounding the current YZ line
254 T* b; 240 T* b;
255 241
256 - a = (T*)malloc(X * sizeof(T));  
257 - b = (T*)malloc(X * sizeof(T)); 242 + a = (T*)malloc(X() * sizeof(T));
  243 + b = (T*)malloc(X() * sizeof(T));
258 244
259 245
260 double ai, bi; //stores the two baseline points wavelength surrounding the current band 246 double ai, bi; //stores the two baseline points wavelength surrounding the current band
@@ -267,10 +253,10 @@ public: @@ -267,10 +253,10 @@ public:
267 } 253 }
268 // loop start correct every y slice 254 // loop start correct every y slice
269 255
270 - for (unsigned k =0; k < R[1]; k++) 256 + for (unsigned k =0; k < Y(); k++)
271 { 257 {
272 //get the current y slice 258 //get the current y slice
273 - getY(c, k); 259 + read_plane_y(c, k);
274 260
275 //initialize lownum, highnum, low, high 261 //initialize lownum, highnum, low, high
276 ai = w[0]; 262 ai = w[0];
@@ -279,16 +265,16 @@ public: @@ -279,16 +265,16 @@ public:
279 //set the baseline point at band 0 to 0 265 //set the baseline point at band 0 to 0
280 if(wls[0] != w[0]){ 266 if(wls[0] != w[0]){
281 bi = wls[control]; 267 bi = wls[control];
282 - memset(a, (char)0, X * sizeof(T) ); 268 + memset(a, (char)0, X() * sizeof(T) );
283 } 269 }
284 //else get the low band 270 //else get the low band
285 else{ 271 else{
286 control++; 272 control++;
287 - getYZ(a, c, ai); 273 + read_x_from_xz(a, c, ai);
288 bi = wls[control]; 274 bi = wls[control];
289 } 275 }
290 //get the high band 276 //get the high band
291 - getYZ(b, c, bi); 277 + read_x_from_xz(b, c, bi);
292 278
293 //correct every YZ line 279 //correct every YZ line
294 280
@@ -305,7 +291,7 @@ public: @@ -305,7 +291,7 @@ public:
305 291
306 ai = bi; 292 ai = bi;
307 bi = wls[control]; 293 bi = wls[control];
308 - getYZ(b, c, bi); 294 + read_x_from_xz(b, c, bi);
309 295
310 } 296 }
311 //if the last BL point on the last band of the file? 297 //if the last BL point on the last band of the file?
@@ -313,7 +299,7 @@ public: @@ -313,7 +299,7 @@ public:
313 299
314 std::swap(a, b); //swap the baseline band pointers 300 std::swap(a, b); //swap the baseline band pointers
315 301
316 - memset(b, (char)0, X * sizeof(T) ); //clear the high band 302 + memset(b, (char)0, X() * sizeof(T) ); //clear the high band
317 303
318 ai = bi; 304 ai = bi;
319 bi = w[B - 1]; 305 bi = w[B - 1];
@@ -322,9 +308,9 @@ public: @@ -322,9 +308,9 @@ public:
322 308
323 ci = w[cii]; 309 ci = w[cii];
324 310
325 - unsigned jump = cii * X; 311 + unsigned jump = cii * X();
326 //perform the baseline correction 312 //perform the baseline correction
327 - for(unsigned i=0; i < X; i++) 313 + for(unsigned i=0; i < X(); i++)
328 { 314 {
329 double r = (double) (ci - ai) / (double) (bi - ai); 315 double r = (double) (ci - ai) / (double) (bi - ai);
330 c[i + jump] =(T) ( c[i + jump] - (b[i] - a[i]) * r - a[i] ); 316 c[i + jump] =(T) ( c[i + jump] - (b[i] - a[i]) * r - a[i] );
@@ -349,11 +335,9 @@ public: @@ -349,11 +335,9 @@ public:
349 /// @param t is a threshold specified such that a spectrum with a value at w less than t is set to zero. Setting this threshold allows the user to limit division by extremely small numbers. 335 /// @param t is a threshold specified such that a spectrum with a value at w less than t is set to zero. Setting this threshold allows the user to limit division by extremely small numbers.
350 bool normalize(std::string outname, double w, double t = 0.0) 336 bool normalize(std::string outname, double w, double t = 0.0)
351 { 337 {
352 - unsigned int B = R[2]; //calculate the number of bands  
353 - unsigned int Y = R[1];  
354 - unsigned int X = R[0];  
355 - unsigned int ZX = R[2] * R[0];  
356 - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band 338 + unsigned int B = Z(); //calculate the number of bands
  339 + unsigned int ZX = Z() * X();
  340 + unsigned int XY = X() * Y(); //calculate the number of pixels in a band
357 unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band 341 unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band
358 unsigned int L = ZX * sizeof(T); 342 unsigned int L = ZX * sizeof(T);
359 343
@@ -368,17 +352,17 @@ public: @@ -368,17 +352,17 @@ public:
368 352
369 band(b, w); //get the certain band into memory 353 band(b, w); //get the certain band into memory
370 354
371 - for(unsigned j = 0; j < Y; j++) 355 + for(unsigned j = 0; j < Y(); j++)
372 { 356 {
373 - getY(c, j); 357 + read_plane_y(c, j);
374 for(unsigned i = 0; i < B; i++) 358 for(unsigned i = 0; i < B; i++)
375 { 359 {
376 - for(unsigned m = 0; m < X; m++) 360 + for(unsigned m = 0; m < X(); m++)
377 { 361 {
378 - if( b[m + j * X] < t )  
379 - c[m + i * X] = (T)0.0; 362 + if( b[m + j * X()] < t )
  363 + c[m + i * X()] = (T)0.0;
380 else 364 else
381 - c[m + i * X] = c[m + i * X] / b[m + j * X]; 365 + c[m + i * X()] = c[m + i * X()] / b[m + j * X()];
382 } 366 }
383 } 367 }
384 target.write(reinterpret_cast<const char*>(c), L); //write normalized data into destination 368 target.write(reinterpret_cast<const char*>(c), L); //write normalized data into destination
@@ -395,7 +379,7 @@ public: @@ -395,7 +379,7 @@ public:
395 /// @param outname is the name of the output BSQ file to be saved to disk. 379 /// @param outname is the name of the output BSQ file to be saved to disk.
396 bool bsq(std::string outname) 380 bool bsq(std::string outname)
397 { 381 {
398 - unsigned int S = R[0] * R[1] * sizeof(T); //calculate the number of bytes in a band 382 + unsigned int S = X() * Y() * sizeof(T); //calculate the number of bytes in a band
399 383
400 std::ofstream target(outname.c_str(), std::ios::binary); 384 std::ofstream target(outname.c_str(), std::ios::binary);
401 std::string headername = outname + ".hdr"; 385 std::string headername = outname + ".hdr";
@@ -403,7 +387,7 @@ public: @@ -403,7 +387,7 @@ public:
403 T * p; //pointer to the current band 387 T * p; //pointer to the current band
404 p = (T*)malloc(S); 388 p = (T*)malloc(S);
405 389
406 - for ( unsigned i = 0; i < R[2]; i++) 390 + for ( unsigned i = 0; i < Z(); i++)
407 { 391 {
408 band_index(p, i); 392 band_index(p, i);
409 target.write(reinterpret_cast<const char*>(p), S); //write a band data into target file 393 target.write(reinterpret_cast<const char*>(p), S); //write a band data into target file
@@ -419,7 +403,7 @@ public: @@ -419,7 +403,7 @@ public:
419 /// @param outname is the name of the output BIP file to be saved to disk. 403 /// @param outname is the name of the output BIP file to be saved to disk.
420 bool bip(std::string outname) 404 bool bip(std::string outname)
421 { 405 {
422 - unsigned int S = R[0] * R[2] * sizeof(T); //calculate the number of bytes in a ZX slice 406 + unsigned int S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice
423 407
424 std::ofstream target(outname.c_str(), std::ios::binary); 408 std::ofstream target(outname.c_str(), std::ios::binary);
425 std::string headername = outname + ".hdr"; 409 std::string headername = outname + ".hdr";
@@ -429,14 +413,14 @@ public: @@ -429,14 +413,14 @@ public:
429 T * q; //pointer to the current ZX slice for bip file 413 T * q; //pointer to the current ZX slice for bip file
430 q = (T*)malloc(S); 414 q = (T*)malloc(S);
431 415
432 - for ( unsigned i = 0; i < R[1]; i++) 416 + for ( unsigned i = 0; i < Y(); i++)
433 { 417 {
434 - getY(p, i);  
435 - for ( unsigned k = 0; k < R[2]; k++) 418 + read_plane_y(p, i);
  419 + for ( unsigned k = 0; k < Z(); k++)
436 { 420 {
437 - unsigned ks = k * R[0];  
438 - for ( unsigned j = 0; j < R[0]; j++)  
439 - q[k + j * R[2]] = p[ks + j]; 421 + unsigned ks = k * X();
  422 + for ( unsigned j = 0; j < X(); j++)
  423 + q[k + j * Z()] = p[ks + j];
440 } 424 }
441 425
442 target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file 426 target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file
@@ -460,7 +444,7 @@ public: @@ -460,7 +444,7 @@ public:
460 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. 444 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size.
461 bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){ 445 bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){
462 446
463 - unsigned XY = R[0] * R[1]; 447 + unsigned XY = X() * Y();
464 band(result, wavelength); //get band 448 band(result, wavelength); //get band
465 449
466 //perform the baseline correction 450 //perform the baseline correction
@@ -481,7 +465,7 @@ public: @@ -481,7 +465,7 @@ public:
481 465
482 T* lp; 466 T* lp;
483 T* rp; 467 T* rp;
484 - unsigned XY = R[0] * R[1]; 468 + unsigned XY = X() * Y();
485 unsigned S = XY * sizeof(T); 469 unsigned S = XY * sizeof(T);
486 lp = (T*) malloc(S); //memory allocation 470 lp = (T*) malloc(S); //memory allocation
487 rp = (T*) malloc(S); 471 rp = (T*) malloc(S);
@@ -512,7 +496,7 @@ public: @@ -512,7 +496,7 @@ public:
512 T* cur; //current band 1 496 T* cur; //current band 1
513 T* cur2; //current band 2 497 T* cur2; //current band 2
514 498
515 - unsigned XY = R[0] * R[1]; 499 + unsigned XY = X() * Y();
516 unsigned S = XY * sizeof(T); 500 unsigned S = XY * sizeof(T);
517 501
518 lp = (T*) malloc(S); //memory allocation 502 lp = (T*) malloc(S); //memory allocation
@@ -593,14 +577,14 @@ public: @@ -593,14 +577,14 @@ public:
593 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 577 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
594 bool ph_to_ph(double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, T * result){ 578 bool ph_to_ph(double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, T * result){
595 579
596 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
597 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 580 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  581 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
598 582
599 //get the two peak band 583 //get the two peak band
600 height(lb1, rb1, pos1, p1); 584 height(lb1, rb1, pos1, p1);
601 height(lb2, rb2, pos2, p2); 585 height(lb2, rb2, pos2, p2);
602 //calculate the ratio in result 586 //calculate the ratio in result
603 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 587 + for(unsigned i = 0; i < X() * Y(); i++){
604 if(p1[i] == 0 && p2[i] ==0) 588 if(p1[i] == 0 && p2[i] ==0)
605 result[i] = 1; 589 result[i] = 1;
606 else 590 else
@@ -624,14 +608,14 @@ public: @@ -624,14 +608,14 @@ public:
624 bool pa_to_ph(double lb1, double rb1, double lab1, double rab1, 608 bool pa_to_ph(double lb1, double rb1, double lab1, double rab1,
625 double lb2, double rb2, double pos, T* result){ 609 double lb2, double rb2, double pos, T* result){
626 610
627 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
628 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 611 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  612 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
629 613
630 //get the area and the peak band 614 //get the area and the peak band
631 area(lb1, rb1, lab1, rab1, p1); 615 area(lb1, rb1, lab1, rab1, p1);
632 height(lb2, rb2, pos, p2); 616 height(lb2, rb2, pos, p2);
633 //calculate the ratio in result 617 //calculate the ratio in result
634 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 618 + for(unsigned i = 0; i < X() * Y(); i++){
635 if(p1[i] == 0 && p2[i] ==0) 619 if(p1[i] == 0 && p2[i] ==0)
636 result[i] = 1; 620 result[i] = 1;
637 else 621 else
@@ -657,14 +641,14 @@ public: @@ -657,14 +641,14 @@ public:
657 bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, 641 bool pa_to_pa(double lb1, double rb1, double lab1, double rab1,
658 double lb2, double rb2, double lab2, double rab2, T* result){ 642 double lb2, double rb2, double lab2, double rab2, T* result){
659 643
660 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
661 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 644 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  645 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
662 646
663 //get the area and the peak band 647 //get the area and the peak band
664 area(lb1, rb1, lab1, rab1, p1); 648 area(lb1, rb1, lab1, rab1, p1);
665 area(lb2, rb2, lab2, rab2, p2); 649 area(lb2, rb2, lab2, rab2, p2);
666 //calculate the ratio in result 650 //calculate the ratio in result
667 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 651 + for(unsigned i = 0; i < X() * Y(); i++){
668 if(p1[i] == 0 && p2[i] ==0) 652 if(p1[i] == 0 && p2[i] ==0)
669 result[i] = 1; 653 result[i] = 1;
670 else 654 else
@@ -689,7 +673,7 @@ public: @@ -689,7 +673,7 @@ public:
689 T* cur; //current band 1 673 T* cur; //current band 1
690 T* cur2; //current band 2 674 T* cur2; //current band 2
691 675
692 - unsigned XY = R[0] * R[1]; 676 + unsigned XY = X() * Y();
693 unsigned S = XY * sizeof(T); 677 unsigned S = XY * sizeof(T);
694 678
695 lp = (T*) malloc(S); //memory allocation 679 lp = (T*) malloc(S); //memory allocation
@@ -765,14 +749,14 @@ public: @@ -765,14 +749,14 @@ public:
765 /// @param rab is the label for the end of the peak 749 /// @param rab is the label for the end of the peak
766 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 750 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
767 bool cpoint(double lb, double rb, double lab, double rab, T* result){ 751 bool cpoint(double lb, double rb, double lab, double rab, T* result){
768 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
769 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 752 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  753 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
770 754
771 //get the area and the peak band 755 //get the area and the peak band
772 x_area(lb, rb, lab, rab, p1); 756 x_area(lb, rb, lab, rab, p1);
773 area(lb, rb, lab, rab, p2); 757 area(lb, rb, lab, rab, p2);
774 //calculate the ratio in result 758 //calculate the ratio in result
775 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 759 + for(unsigned i = 0; i < X() * Y(); i++){
776 if(p1[i] == 0 && p2[i] ==0) 760 if(p1[i] == 0 && p2[i] ==0)
777 result[i] = 1; 761 result[i] = 1;
778 else 762 else
@@ -793,10 +777,10 @@ public: @@ -793,10 +777,10 @@ public:
793 /// @param p is a pointer to a pre-allocated array at least X * Y in size 777 /// @param p is a pointer to a pre-allocated array at least X * Y in size
794 bool build_mask(double mask_band, double threshold, unsigned char* p){ 778 bool build_mask(double mask_band, double threshold, unsigned char* p){
795 779
796 - T* temp = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate memory for the certain band 780 + T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band
797 band(temp, mask_band); 781 band(temp, mask_band);
798 782
799 - for (unsigned int i = 0; i < R[0] * R[1]; i++) { 783 + for (unsigned int i = 0; i < X() * Y(); i++) {
800 if (temp[i] < threshold) 784 if (temp[i] < threshold)
801 p[i] = 0; 785 p[i] = 0;
802 else 786 else
@@ -816,20 +800,20 @@ public: @@ -816,20 +800,20 @@ public:
816 std::ofstream target(outfile.c_str(), std::ios::binary); 800 std::ofstream target(outfile.c_str(), std::ios::binary);
817 801
818 //I THINK THIS IS WRONG 802 //I THINK THIS IS WRONG
819 - unsigned XZ = R[0] * R[2]; //calculate the number of values in a page on disk 803 + unsigned XZ = X() * Z(); //calculate the number of values in a page on disk
820 unsigned L = XZ * sizeof(T); //calculate the size of the page (in bytes) 804 unsigned L = XZ * sizeof(T); //calculate the size of the page (in bytes)
821 805
822 T * temp = (T*)malloc(L); //allocate memory for a temporary page 806 T * temp = (T*)malloc(L); //allocate memory for a temporary page
823 807
824 - for (unsigned i = 0; i < R[1]; i++) //for each value in R[1] (BIP should be X) 808 + for (unsigned i = 0; i < Y(); i++) //for each value in Y() (BIP should be X)
825 { 809 {
826 - getY(temp, i); //retrieve an ZX slice, stored in temp  
827 - for ( unsigned j = 0; j < R[2]; j++) //for each R[2] (Y) 810 + read_plane_y(temp, i); //retrieve an ZX slice, stored in temp
  811 + for ( unsigned j = 0; j < Z(); j++) //for each Z() (Y)
828 { 812 {
829 - for (unsigned k = 0; k < R[0]; k++) //for each band 813 + for (unsigned k = 0; k < X(); k++) //for each band
830 { 814 {
831 - if(p[i * R[0] + k] == 0)  
832 - temp[j * R[0] + k] = 0; 815 + if(p[i * X() + k] == 0)
  816 + temp[j * X() + k] = 0;
833 else 817 else
834 continue; 818 continue;
835 } 819 }
@@ -843,30 +827,29 @@ public: @@ -843,30 +827,29 @@ public:
843 827
844 ///Saves to disk only those spectra corresponding to mask values != 0 828 ///Saves to disk only those spectra corresponding to mask values != 0
845 bool sift_mask(std::string outfile, unsigned char* p){ 829 bool sift_mask(std::string outfile, unsigned char* p){
846 - // Assume R[0] = X, R[1] = Y, R[2] = Z. 830 + // Assume X() = X, Y() = Y, Z() = Z.
847 std::ofstream target(outfile.c_str(), std::ios::binary); 831 std::ofstream target(outfile.c_str(), std::ios::binary);
848 832
849 //for loading pages: 833 //for loading pages:
850 - unsigned XZ = R[0] * R[2]; //calculate the number of values in an XZ page on disk 834 + unsigned XZ = X() * Z(); //calculate the number of values in an XZ page on disk
851 unsigned L = XZ * sizeof(T); //calculate the size of the page (in bytes) 835 unsigned L = XZ * sizeof(T); //calculate the size of the page (in bytes)
852 T * temp = (T*)malloc(L); //allocate memory for a temporary page 836 T * temp = (T*)malloc(L); //allocate memory for a temporary page
853 837
854 //for saving spectra: 838 //for saving spectra:
855 - unsigned Z = R[2]; //calculate the number of values in a spectrum  
856 - unsigned LZ = Z * sizeof(T); //calculate the size of the spectrum (in bytes) 839 + unsigned LZ = Z() * sizeof(T); //calculate the size of the spectrum (in bytes)
857 T * tempZ = (T*)malloc(LZ); //allocate memory for a temporary spectrum 840 T * tempZ = (T*)malloc(LZ); //allocate memory for a temporary spectrum
858 - spectrum(tempZ, R[0] - 1, R[1] - 1); //creates a dummy spectrum by taking the last spectrum in the image 841 + spectrum(tempZ, X() - 1, Y() - 1); //creates a dummy spectrum by taking the last spectrum in the image
859 842
860 - for (unsigned i = 0; i < R[1]; i++) //Select a page by choosing Y coordinate, R[1] 843 + for (unsigned i = 0; i < Y(); i++) //Select a page by choosing Y coordinate, Y()
861 { 844 {
862 - getY(temp, i); //retrieve an ZX page, store in "temp"  
863 - for (unsigned j = 0; j < R[0]; j++) //Select a pixel by choosing X coordinate in the page, R[0] 845 + read_plane_y(temp, i); //retrieve an ZX page, store in "temp"
  846 + for (unsigned j = 0; j < X(); j++) //Select a pixel by choosing X coordinate in the page, X()
864 { 847 {
865 - if (p[j * R[0] + i] != 0) //if the mask != 0 at that XY pixel 848 + if (p[j * X() + i] != 0) //if the mask != 0 at that XY pixel
866 { 849 {
867 - for (unsigned k = 0; k < R[2]; k++) //Select a voxel by choosing Z coordinate at the pixel 850 + for (unsigned k = 0; k < Z(); k++) //Select a voxel by choosing Z coordinate at the pixel
868 { 851 {
869 - tempZ[k] = temp[k*R[0] + i]; //Pass the correct spectral value from XZ page into the spectrum to be saved. 852 + tempZ[k] = temp[k*X() + i]; //Pass the correct spectral value from XZ page into the spectrum to be saved.
870 } 853 }
871 target.write(reinterpret_cast<const char*>(tempZ), LZ); //write that spectrum to disk. Size is L2. 854 target.write(reinterpret_cast<const char*>(tempZ), LZ); //write that spectrum to disk. Size is L2.
872 } 855 }
@@ -883,25 +866,25 @@ public: @@ -883,25 +866,25 @@ public:
883 866
884 /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages. 867 /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages.
885 bool band_avg(T* p){ 868 bool band_avg(T* p){
886 - unsigned long long XZ = R[0] * R[2]; 869 + unsigned long long XZ = X() * Z();
887 T* temp = (T*)malloc(sizeof(T) * XZ); 870 T* temp = (T*)malloc(sizeof(T) * XZ);
888 - T* line = (T*)malloc(sizeof(T) * R[0]); 871 + T* line = (T*)malloc(sizeof(T) * X());
889 872
890 - for (unsigned i = 0; i < R[1]; i++){ 873 + for (unsigned i = 0; i < Y(); i++){
891 getY(temp, i); 874 getY(temp, i);
892 //initialize x-line 875 //initialize x-line
893 - for (unsigned j = 0; j < R[0]; j++){ 876 + for (unsigned j = 0; j < X(); j++){
894 line[j] = 0; 877 line[j] = 0;
895 } 878 }
896 unsigned c = 0; 879 unsigned c = 0;
897 - for (unsigned j = 0; j < R[2]; j++){  
898 - for (unsigned k = 0; k < R[0]; k++){  
899 - line[k] += temp[c] / (T)R[2]; 880 + for (unsigned j = 0; j < Z(); j++){
  881 + for (unsigned k = 0; k < X(); k++){
  882 + line[k] += temp[c] / (T)Z();
900 c++; 883 c++;
901 } 884 }
902 } 885 }
903 - for (unsigned j = 0; j < R[0]; j++){  
904 - p[j + i * R[0]] = line[j]; 886 + for (unsigned j = 0; j < X(); j++){
  887 + p[j + i * X()] = line[j];
905 } 888 }
906 } 889 }
907 free(temp); 890 free(temp);
@@ -913,10 +896,10 @@ public: @@ -913,10 +896,10 @@ public:
913 /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum 896 /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum
914 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location 897 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
915 bool avg_band(T*p, unsigned char* mask){ 898 bool avg_band(T*p, unsigned char* mask){
916 - unsigned long long XZ = R[0] * R[2];  
917 - unsigned long long XY = R[0] * R[1]; 899 + unsigned long long XZ = X() * Z();
  900 + unsigned long long XY = X() * Y();
918 T* temp = (T*)malloc(sizeof(T) * XZ); 901 T* temp = (T*)malloc(sizeof(T) * XZ);
919 - for (unsigned j = 0; j < R[2]; j++){ 902 + for (unsigned j = 0; j < Z(); j++){
920 p[j] = 0; 903 p[j] = 0;
921 } 904 }
922 //calculate vaild number in a band 905 //calculate vaild number in a band
@@ -926,13 +909,13 @@ public: @@ -926,13 +909,13 @@ public:
926 count++; 909 count++;
927 } 910 }
928 } 911 }
929 - for (unsigned k = 0; k < R[1]; k++){  
930 - getY(temp, k);  
931 - unsigned kx = k * R[0];  
932 - for (unsigned i = 0; i < R[0]; i++){ 912 + for (unsigned k = 0; k < Y(); k++){
  913 + read_plane_y(temp, k);
  914 + unsigned kx = k * X();
  915 + for (unsigned i = 0; i < X(); i++){
933 if (mask[kx + i] != 0){ 916 if (mask[kx + i] != 0){
934 - for (unsigned j = 0; j < R[2]; j++){  
935 - p[j] += temp[j * R[0] + i] / (T)count; 917 + for (unsigned j = 0; j < Z(); j++){
  918 + p[j] += temp[j * X() + i] / (T)count;
936 } 919 }
937 } 920 }
938 } 921 }
@@ -948,8 +931,8 @@ public: @@ -948,8 +931,8 @@ public:
948 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location 931 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
949 bool co_matrix(T* co, T* avg, unsigned char *mask){ 932 bool co_matrix(T* co, T* avg, unsigned char *mask){
950 //memory allocation 933 //memory allocation
951 - unsigned long long xy = R[0] * R[1];  
952 - unsigned int B = R[2]; 934 + unsigned long long xy = X() * Y();
  935 + unsigned int B = Z();
953 T* temp = (T*)malloc(sizeof(T) * B); 936 T* temp = (T*)malloc(sizeof(T) * B);
954 //count vaild pixels in a band 937 //count vaild pixels in a band
955 unsigned count = 0; 938 unsigned count = 0;
@@ -999,16 +982,16 @@ public: @@ -999,16 +982,16 @@ public:
999 //calculate the new number of samples and lines 982 //calculate the new number of samples and lines
1000 unsigned long long sam = x1 - x0; //samples 983 unsigned long long sam = x1 - x0; //samples
1001 unsigned long long lin = y1 - y0; //lines 984 unsigned long long lin = y1 - y0; //lines
1002 - unsigned long long L = sam * R[2] * sizeof(T); 985 + unsigned long long L = sam * Z() * sizeof(T);
1003 //get specified band and save 986 //get specified band and save
1004 T* temp = (T*)malloc(L); 987 T* temp = (T*)malloc(L);
1005 std::ofstream out(outfile.c_str(), std::ios::binary); 988 std::ofstream out(outfile.c_str(), std::ios::binary);
1006 - unsigned long long jumpb = (R[0] - sam) * sizeof(T); //jump pointer to the next band 989 + unsigned long long jumpb = (X() - sam) * sizeof(T); //jump pointer to the next band
1007 //get start 990 //get start
1008 - file.seekg((y0 * R[0] * R[2] + x0) * sizeof(T), std::ios::beg); 991 + file.seekg((y0 * X() * Z() + x0) * sizeof(T), std::ios::beg);
1009 for (unsigned i = 0; i < lin; i++) 992 for (unsigned i = 0; i < lin; i++)
1010 { 993 {
1011 - for (unsigned j = 0; j < R[2]; j++) 994 + for (unsigned j = 0; j < Z(); j++)
1012 { 995 {
1013 file.read((char *)(temp + j * sam), sizeof(T) * sam); 996 file.read((char *)(temp + j * sam), sizeof(T) * sam);
1014 file.seekg(jumpb, std::ios::cur); //go to the next band 997 file.seekg(jumpb, std::ios::cur); //go to the next band
stim/envi/binary.h
@@ -81,6 +81,8 @@ protected: @@ -81,6 +81,8 @@ protected:
81 return false; 81 return false;
82 } 82 }
83 83
  84 +
  85 +
84 public: 86 public:
85 87
86 /// Open a binary file for streaming. 88 /// Open a binary file for streaming.
@@ -159,8 +161,141 @@ public: @@ -159,8 +161,141 @@ public:
159 return true; 161 return true;
160 } 162 }
161 163
  164 +
  165 +
  166 + ///Reads a line Z (slowest dimension) for a given XY value
  167 +
  168 + /// @param p is a pointer to pre-allocated memory equal to the line size R[2]
  169 + /// @param x is the x coordinate
  170 + /// @param y is the y coordinate
  171 + bool read_line_01( T * p, unsigned int x, unsigned int y){
  172 + unsigned int i;
  173 +
  174 + if ( x >= R[0] || y >= R[1]){ //make sure the sample and line number is right
  175 + std::cout<<"ERROR: sample or line out of range"<<std::endl;
  176 + return false;
  177 + }
  178 +
  179 + file.seekg((x + y * R[0]) * sizeof(T), std::ios::beg); //point to the certain sample and line
  180 + for (i = 0; i < R[2]; i++)
  181 + {
  182 + file.read((char *)(p + i), sizeof(T));
  183 + file.seekg((R[1] * R[0] - 1) * sizeof(T), std::ios::cur); //go to the next band
  184 + }
  185 +
  186 + return true;
  187 + }
  188 +
  189 + ///Reads a line X (fastest dimension) for a given YZ value
  190 +
  191 + /// @param p is a pointer to pre-allocated memory equal to the line size R[2]
  192 + /// @param x is the y coordinate
  193 + /// @param y is the z coordinate
  194 + bool read_line_12(T * p, unsigned int y, unsigned int z){
  195 + //test to make sure the specified value is within range
  196 + if( y >= R[1] || z >= R[2] ){
  197 + std::cout<<"ERROR: sample or line out of range"<<std::endl;
  198 + return false;
  199 + }
  200 +
  201 + file.seekg((z * R[0] * R[1] + y * R[0]) * sizeof(T), std::ios::beg); //seek to the start of the line
  202 + file.read((char *)p, sizeof(T) * R[0]); //read the line
  203 +
  204 + return true;
  205 + }
  206 +
  207 + ///Reads a line Y (second fastest dimension) for a given XZ value
  208 +
  209 + /// @param p is a pointer to pre-allocated memory equal to the line size R[2]
  210 + /// @param x is the y coordinate
  211 + /// @param z is the z coordinate
  212 + bool read_line_02(T * p, unsigned int x, unsigned int z){
  213 + //test to make sure the specified value is within range
  214 + if( x >= R[0] || z >= R[2] ){
  215 + std::cout<<"ERROR: sample or line out of range"<<std::endl;
  216 + return false;
  217 + }
  218 +
  219 + file.seekg((z * R[0] * R[1] + x) * sizeof(T), std::ios::beg); //seek to the start of the line
  220 + for (unsigned int i = 0; i < R[1]; i++){ //for each pixel in the line
  221 + file.read((char *)(p + i), sizeof(T)); //read the pixel
  222 + file.seekg((R[0] - 1) * sizeof(T), std::ios::cur); //seek to the next pixel in the line
  223 + }
  224 +
  225 + return true;
  226 + }
  227 +
  228 + bool read_plane_0(T* p, unsigned int n){
  229 +
  230 + if (n >= R[0]){ //make sure the number is within the possible range
  231 + std::cout<<"ERROR read_plane_0: page out of range"<<std::endl;
  232 + return false;
  233 + }
  234 + unsigned int jump = (R[0] - 1) * sizeof(T); //number of bytes to skip between samples
  235 +
  236 + //seek to the start of the plane
  237 + file.seekg(n * sizeof(T), std::ios::beg);
  238 +
  239 + unsigned int N = R[1] * R[2];
  240 + for(unsigned int i = 0; i<N; i++){
  241 + file.read((char*)(p+i), sizeof(T));
  242 + file.seekg(jump, std::ios::cur);
  243 + }
  244 +
  245 + return true;
  246 +
  247 +
  248 + }
  249 +
  250 + bool read_plane_1(T* p, unsigned int n){
  251 +
  252 + unsigned int L = R[0] * sizeof(T); //caculate the number of bytes in a sample line
  253 + unsigned int jump = R[0] * (R[1] - 1) * sizeof(T);
  254 +
  255 + if (n >= R[1]){ //make sure the bank number is right
  256 + std::cout<<"ERROR read_plane_1: page out of range"<<std::endl;
  257 + return false;
  258 + }
  259 +
  260 + file.seekg(R[0] * n * sizeof(T), std::ios::beg);
  261 + for (unsigned i = 0; i < R[2]; i++){
  262 +
  263 + file.read((char *)(p + i * R[0]), L);
  264 + file.seekg( jump, std::ios::cur);
  265 + std::cout<<i<<" ";
  266 + }
  267 +
  268 + return true;
  269 + }
  270 +
  271 + bool read_plane_2(T* p, unsigned int n){
  272 + return read_page(p, n);
  273 + }
  274 +
  275 + bool read_pixel(T* p, unsigned int i){
  276 + if(i >= R[0] * R[1] * R[2]){
  277 + std::cout<<"ERROR read_pixel: n is out of range"<<std::endl;
  278 + return false;
  279 + }
  280 +
  281 + file.seekg(i * sizeof(T), std::ios::cur);
  282 + file.read((char*)p, sizeof(T));
  283 +
  284 + }
  285 +
  286 + bool read_pixel(T* p, unsigned int x, unsigned int y, unsigned int z){
  287 +
  288 + if(x < 0 || x >= R[0] || y < 0 || y >= R[1] || z < 0 || z > R[2]){
  289 + std::cout<<"ERROR read_pixel: (x,y,z) is out of range"<<std::endl;
  290 + return false;
  291 + }
  292 +
  293 + unsigned int i = z * R[0] * R[1] + y * R[0] + z;
  294 + return read_pixel(p, i);
  295 + }
  296 +
162 //saves a hyperplane orthogonal to dimension d at intersection n 297 //saves a hyperplane orthogonal to dimension d at intersection n
163 - bool read_plane(T * dest, unsigned int d, unsigned int n){ 298 + /*bool read_plane(T * dest, unsigned int d, unsigned int n){
164 299
165 //reset the file pointer back to the beginning of the file 300 //reset the file pointer back to the beginning of the file
166 file.seekg(0, std::ios::beg); 301 file.seekg(0, std::ios::beg);
@@ -184,10 +319,10 @@ public: @@ -184,10 +319,10 @@ public:
184 319
185 return true; 320 return true;
186 321
187 - } 322 + }*/
188 323
189 //save one pixel of the file into the memory, and return the pointer 324 //save one pixel of the file into the memory, and return the pointer
190 - bool read_spectrum(T * p, unsigned x, unsigned y){ 325 + /*bool read_spectrum(T * p, unsigned x, unsigned y){
191 326
192 unsigned int i; 327 unsigned int i;
193 328
@@ -204,7 +339,7 @@ public: @@ -204,7 +339,7 @@ public:
204 } 339 }
205 340
206 return true; 341 return true;
207 - } 342 + }*/
208 343
209 344
210 }; 345 };
@@ -27,11 +27,22 @@ protected: @@ -27,11 +27,22 @@ protected:
27 std::vector<double> w; //band wavelength 27 std::vector<double> w; //band wavelength
28 unsigned int offset; //header offset 28 unsigned int offset; //header offset
29 29
  30 + unsigned long X(){
  31 + return R[1];
  32 + }
  33 + unsigned long Y(){
  34 + return R[2];
  35 + }
  36 + unsigned long Z(){
  37 + return R[0];
  38 + }
  39 +
30 public: 40 public:
31 41
32 using binary<T>::open; 42 using binary<T>::open;
33 using binary<T>::file; 43 using binary<T>::file;
34 using binary<T>::R; 44 using binary<T>::R;
  45 + using binary<T>::read_line_12;
35 46
36 /// Open a data file for reading using the class interface. 47 /// Open a data file for reading using the class interface.
37 48
@@ -48,7 +59,7 @@ public: @@ -48,7 +59,7 @@ public:
48 //copy the offset to the structure 59 //copy the offset to the structure
49 offset = header_offset; 60 offset = header_offset;
50 61
51 - return open(filename, vec<unsigned int>(X, Y, B), header_offset); 62 + return open(filename, vec<unsigned int>(B, X, Y), header_offset);
52 63
53 } 64 }
54 65
@@ -57,21 +68,7 @@ public: @@ -57,21 +68,7 @@ public:
57 /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. 68 /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size.
58 /// @param page <= B is the integer number of the band to be copied. 69 /// @param page <= B is the integer number of the band to be copied.
59 bool band_index( T * p, unsigned int page){ 70 bool band_index( T * p, unsigned int page){
60 -  
61 - if (page >= R[2]){ //make sure the bank number is right  
62 - std::cout<<"ERROR: page out of range"<<std::endl;  
63 - return false;  
64 - }  
65 -  
66 - //binary::getSlice(p, 0, page); //I met some problems when I called this function?????  
67 - file.seekg(page * sizeof(T), std::ios::beg);  
68 - for (unsigned i = 0; i < R[0] * R[1]; i++)  
69 - {  
70 - file.read((char *)(p + i), sizeof(T));  
71 - file.seekg( (R[2] - 1) * sizeof(T), std::ios::cur);  
72 - }  
73 -  
74 - return true; 71 + return binary<T>::read_plane_0(p, page);
75 } 72 }
76 73
77 /// Retrieve a single band (by numerical label) and stores it in pre-allocated memory. 74 /// Retrieve a single band (by numerical label) and stores it in pre-allocated memory.
@@ -84,7 +81,7 @@ public: @@ -84,7 +81,7 @@ public:
84 if(w.size() == 0) 81 if(w.size() == 0)
85 return band_index(p, (unsigned int)wavelength); 82 return band_index(p, (unsigned int)wavelength);
86 83
87 - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band 84 + unsigned int XY = X() * Y(); //calculate the number of pixels in a band
88 85
89 unsigned page=0; //bands around the wavelength 86 unsigned page=0; //bands around the wavelength
90 87
@@ -101,8 +98,8 @@ public: @@ -101,8 +98,8 @@ public:
101 { 98 {
102 page++; 99 page++;
103 //if wavelength is larger than the last wavelength in header file 100 //if wavelength is larger than the last wavelength in header file
104 - if (page == R[2]) {  
105 - band_index(p, R[2]-1); 101 + if (page == Z()) {
  102 + band_index(p, Z()-1);
106 return true; 103 return true;
107 } 104 }
108 } 105 }
@@ -128,11 +125,24 @@ public: @@ -128,11 +125,24 @@ public:
128 125
129 return true; 126 return true;
130 } 127 }
131 - //get YZ line from the a Y slice, Y slice data should be already IN the MEMORY  
132 - bool getYZ(T* p, T* c, double wavelength) 128 +
  129 + /// Retrieve a single spectrum (Z-axis line) at a given (x, y) location and stores it in pre-allocated memory.
  130 +
  131 + /// @param p is a pointer to pre-allocated memory at least B * sizeof(T) in size.
  132 + /// @param x is the x-coordinate (dimension 1) of the spectrum.
  133 + /// @param y is the y-coordinate (dimension 2) of the spectrum.
  134 + bool spectrum(T * p, unsigned x, unsigned y){
  135 + return read_line_12(p, x, y); //read a line in the binary YZ plane (dimension order for BIP is ZXY)
  136 + }
  137 +
  138 + /// Retrieves a band of x values from a given xz plane.
  139 +
  140 + /// @param p is a pointer to pre-allocated memory at least X * sizeof(T) in size
  141 + /// @param c is a pointer to an existing XZ plane (size X*Z*sizeof(T))
  142 + /// @param wavelength is the wavelength of X values to retrieve
  143 + bool read_x_from_xz(T* p, T* c, double wavelength)
133 { 144 {
134 - unsigned int X = R[0]; //calculate the number of pixels in a sample  
135 - unsigned int B = R[2]; 145 + unsigned int B = Z();
136 146
137 unsigned page=0; //samples around the wavelength 147 unsigned page=0; //samples around the wavelength
138 148
@@ -141,7 +151,7 @@ public: @@ -141,7 +151,7 @@ public:
141 151
142 //if wavelength is smaller than the first one in header file 152 //if wavelength is smaller than the first one in header file
143 if ( w[page] > wavelength ){ 153 if ( w[page] > wavelength ){
144 - for(unsigned j = 0; j < R[0]; j++) 154 + for(unsigned j = 0; j < X(); j++)
145 { 155 {
146 p[j] = c[j * B]; 156 p[j] = c[j * B];
147 } 157 }
@@ -153,7 +163,7 @@ public: @@ -153,7 +163,7 @@ public:
153 page++; 163 page++;
154 //if wavelength is larger than the last wavelength in header file 164 //if wavelength is larger than the last wavelength in header file
155 if (page == B) { 165 if (page == B) {
156 - for(unsigned j = 0; j < R[0]; j++) 166 + for(unsigned j = 0; j < X(); j++)
157 { 167 {
158 p[j] = c[(j + 1) * B - 1]; 168 p[j] = c[(j + 1) * B - 1];
159 } 169 }
@@ -164,20 +174,20 @@ public: @@ -164,20 +174,20 @@ public:
164 { 174 {
165 T * p1; 175 T * p1;
166 T * p2; 176 T * p2;
167 - p1=(T*)malloc( X * sizeof(T)); //memory allocation  
168 - p2=(T*)malloc( X * sizeof(T)); 177 + p1=(T*)malloc( X() * sizeof(T)); //memory allocation
  178 + p2=(T*)malloc( X() * sizeof(T));
169 //band_index(p1, page - 1); 179 //band_index(p1, page - 1);
170 - for(unsigned j = 0; j < X; j++) 180 + for(unsigned j = 0; j < X(); j++)
171 { 181 {
172 p1[j] = c[j * B + page - 1]; 182 p1[j] = c[j * B + page - 1];
173 } 183 }
174 //band_index(p2, page ); 184 //band_index(p2, page );
175 - for(unsigned j = 0; j < X; j++) 185 + for(unsigned j = 0; j < X(); j++)
176 { 186 {
177 p2[j] = c[j * B + page]; 187 p2[j] = c[j * B + page];
178 } 188 }
179 189
180 - for(unsigned i=0; i < X; i++){ 190 + for(unsigned i=0; i < X(); i++){
181 double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); 191 double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]);
182 p[i] = (p2[i] - p1[i]) * r + p1[i]; 192 p[i] = (p2[i] - p1[i]) * r + p1[i];
183 } 193 }
@@ -187,7 +197,7 @@ public: @@ -187,7 +197,7 @@ public:
187 else //if the wavelength is equal to a wavelength in header file 197 else //if the wavelength is equal to a wavelength in header file
188 { 198 {
189 //band_index(p, page); 199 //band_index(p, page);
190 - for(unsigned j = 0; j < X; j++) 200 + for(unsigned j = 0; j < X(); j++)
191 { 201 {
192 p[j] = c[j * B + page]; 202 p[j] = c[j * B + page];
193 } 203 }
@@ -195,98 +205,6 @@ public: @@ -195,98 +205,6 @@ public:
195 205
196 return true; 206 return true;
197 } 207 }
198 - //given a y and a wavelength, return the y-band data  
199 - //I do not use it right now, to accelerate the processing speed, I try to read a YZ whole slice into memory first, and then we can call "getYZ"  
200 - bool get_x_wavelength(T * p, unsigned y, double wavelength)  
201 - {  
202 - unsigned int X = R[0]; //calculate the number of pixels in a sample  
203 -  
204 - unsigned page=0; //samples around the wavelength  
205 - T * p1;  
206 - T * p2;  
207 -  
208 - //get the bands numbers around the wavelength  
209 -  
210 - //if wavelength is smaller than the first one in header file  
211 - if ( w[page] > wavelength ){  
212 - file.seekg( y * R[2] * R[0] * sizeof(T), std::ios::beg);  
213 - for(unsigned j = 0; j < R[0]; j++)  
214 - {  
215 - file.read((char *)(p + j), sizeof(T));  
216 - file.seekg((R[2] - 1) * sizeof(T), std::ios::cur);  
217 - }  
218 - return true;  
219 - }  
220 -  
221 - while( w[page] < wavelength )  
222 - {  
223 - page++;  
224 - //if wavelength is larger than the last wavelength in header file  
225 - if (page == R[2]) {  
226 - file.seekg( (y * R[2] * R[0] + R[2] - 1)* sizeof(T), std::ios::beg);  
227 - for(unsigned j = 0; j < R[0]; j++)  
228 - {  
229 - file.read((char *)(p + j), sizeof(T));  
230 - file.seekg((R[2] - 1) * sizeof(T), std::ios::cur);  
231 - }  
232 - return true;  
233 - }  
234 - }  
235 - if ( wavelength < w[page] )  
236 - {  
237 - p1=(T*)malloc( X * sizeof(T)); //memory allocation  
238 - p2=(T*)malloc( X * sizeof(T));  
239 - //band_index(p1, page - 1);  
240 - file.seekg( (y * R[2] * R[0] + page - 1)* sizeof(T), std::ios::beg);  
241 - for(unsigned j = 0; j < R[0]; j++)  
242 - {  
243 - file.read((char *)(p1 + j), sizeof(T));  
244 - file.seekg((R[2] - 1) * sizeof(T), std::ios::cur);  
245 - }  
246 - //band_index(p2, page );  
247 - file.seekg( (y * R[2] * R[0] + page)* sizeof(T), std::ios::beg);  
248 - for(unsigned j = 0; j < R[0]; j++)  
249 - {  
250 - file.read((char *)(p2 + j), sizeof(T));  
251 - file.seekg((R[2] - 1) * sizeof(T), std::ios::cur);  
252 - }  
253 -  
254 - for(unsigned i=0; i < R[0]; i++){  
255 - double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]);  
256 - p[i] = (p2[i] - p1[i]) * r + p1[i];  
257 - }  
258 - }  
259 - else //if the wavelength is equal to a wavelength in header file  
260 - {  
261 - //band_index(p, page);  
262 - file.seekg( (y * R[2] * R[0] + page) * sizeof(T), std::ios::beg);  
263 - for(unsigned j = 0; j < R[0]; j++)  
264 - {  
265 - file.read((char *)(p + j), sizeof(T));  
266 - file.seekg((R[2] - 1) * sizeof(T), std::ios::cur);  
267 - }  
268 - }  
269 - return true;  
270 - }  
271 -  
272 - /// Retrieve a single spectrum (B-axis line) at a given (x, y) location and stores it in pre-allocated memory.  
273 -  
274 - /// @param p is a pointer to pre-allocated memory at least B * sizeof(T) in size.  
275 - /// @param x is the x-coordinate (dimension 1) of the spectrum.  
276 - /// @param y is the y-coordinate (dimension 2) of the spectrum.  
277 - bool spectrum(T * p, unsigned x, unsigned y){  
278 -  
279 - if ( x >= R[0] || y >= R[1]){ //make sure the sample and line number is right  
280 - std::cout<<"ERROR: sample or line out of range"<<std::endl;  
281 - exit(1);  
282 - }  
283 -  
284 - file.seekg((x + y * R[0]) * R[2] * sizeof(T), std::ios::beg); //point to the certain sample and line  
285 -  
286 - file.read((char *)p, sizeof(T) * R[2]);  
287 -  
288 - return true;  
289 - }  
290 208
291 /// Retrieve a single pixel and stores it in pre-allocated memory. 209 /// Retrieve a single pixel and stores it in pre-allocated memory.
292 210
@@ -294,30 +212,20 @@ public: @@ -294,30 +212,20 @@ public:
294 /// @param n is an integer index to the pixel using linear array indexing. 212 /// @param n is an integer index to the pixel using linear array indexing.
295 bool pixel(T * p, unsigned n){ 213 bool pixel(T * p, unsigned n){
296 214
297 - unsigned bandnum = R[0] * R[1]; //calculate numbers in one band 215 + unsigned bandnum = X() * Y(); //calculate numbers in one band
298 if ( n >= bandnum){ //make sure the pixel number is right 216 if ( n >= bandnum){ //make sure the pixel number is right
299 std::cout<<"ERROR: sample or line out of range"<<std::endl; 217 std::cout<<"ERROR: sample or line out of range"<<std::endl;
300 return false; 218 return false;
301 } 219 }
302 220
303 - file.seekg(n * R[2] * sizeof(T), std::ios::beg); //point to the certain pixel  
304 - file.read((char *)p, sizeof(T) * R[2]);  
305 - return true; 221 + file.seekg(n * Z() * sizeof(T), std::ios::beg); //point to the certain pixel
  222 + file.read((char *)p, sizeof(T) * Z());
  223 + return true;
306 } 224 }
307 225
308 //given a Y ,return a ZX slice 226 //given a Y ,return a ZX slice
309 - bool getY(T * p, unsigned y)  
310 - {  
311 - if ( y >= R[1]){ //make sure the line number is right  
312 - std::cout<<"ERROR: line out of range"<<std::endl;  
313 - exit(1);  
314 - }  
315 -  
316 - file.seekg(y * R[2] * R[0] * sizeof(T), std::ios::beg);  
317 - file.read((char *)p, sizeof(T) * R[2] * R[0]);  
318 -  
319 - return true;  
320 - 227 + bool read_plane_y(T * p, unsigned y){
  228 + return binary<T>::read_plane_2(p, y);
321 } 229 }
322 230
323 /// Perform baseline correction given a list of baseline points and stores the result in a new BSQ file. 231 /// Perform baseline correction given a list of baseline points and stores the result in a new BSQ file.
@@ -332,10 +240,9 @@ public: @@ -332,10 +240,9 @@ public:
332 std::string headername = outname + ".hdr"; //the header file name 240 std::string headername = outname + ".hdr"; //the header file name
333 241
334 //simplify image resolution 242 //simplify image resolution
335 - unsigned int ZX = R[2] * R[0]; //calculate the number of points in a Y slice 243 + unsigned int ZX = Z() * X(); //calculate the number of points in a Y slice
336 unsigned int L = ZX * sizeof(T); //calculate the number of bytes of a Y slice 244 unsigned int L = ZX * sizeof(T); //calculate the number of bytes of a Y slice
337 - unsigned int B = R[2];  
338 - unsigned int X = R[0]; 245 + unsigned int B = Z();
339 246
340 T* c; //pointer to the current Y slice 247 T* c; //pointer to the current Y slice
341 c = (T*)malloc(L); //memory allocation 248 c = (T*)malloc(L); //memory allocation
@@ -343,8 +250,8 @@ public: @@ -343,8 +250,8 @@ public:
343 T* a; //pointer to the two YZ lines surrounding the current YZ line 250 T* a; //pointer to the two YZ lines surrounding the current YZ line
344 T* b; 251 T* b;
345 252
346 - a = (T*)malloc(X * sizeof(T));  
347 - b = (T*)malloc(X * sizeof(T)); 253 + a = (T*)malloc(X() * sizeof(T));
  254 + b = (T*)malloc(X() * sizeof(T));
348 255
349 256
350 double ai, bi; //stores the two baseline points wavelength surrounding the current band 257 double ai, bi; //stores the two baseline points wavelength surrounding the current band
@@ -357,10 +264,10 @@ public: @@ -357,10 +264,10 @@ public:
357 } 264 }
358 // loop start correct every y slice 265 // loop start correct every y slice
359 266
360 - for (unsigned k =0; k < R[1]; k++) 267 + for (unsigned k =0; k < Y(); k++)
361 { 268 {
362 //get the current y slice 269 //get the current y slice
363 - getY(c, k); 270 + read_plane_y(c, k);
364 271
365 //initialize lownum, highnum, low, high 272 //initialize lownum, highnum, low, high
366 control=0; 273 control=0;
@@ -369,16 +276,16 @@ public: @@ -369,16 +276,16 @@ public:
369 //set the baseline point at band 0 to 0 276 //set the baseline point at band 0 to 0
370 if(wls[0] != w[0]){ 277 if(wls[0] != w[0]){
371 bi = wls[control]; 278 bi = wls[control];
372 - memset(a, (char)0, X * sizeof(T) ); 279 + memset(a, (char)0, X() * sizeof(T) );
373 } 280 }
374 //else get the low band 281 //else get the low band
375 else{ 282 else{
376 control++; 283 control++;
377 - getYZ(a, c, ai); 284 + read_x_from_xz(a, c, ai);
378 bi = wls[control]; 285 bi = wls[control];
379 } 286 }
380 //get the high band 287 //get the high band
381 - getYZ(b, c, bi); 288 + read_x_from_xz(b, c, bi);
382 289
383 //correct every YZ line 290 //correct every YZ line
384 291
@@ -394,7 +301,7 @@ public: @@ -394,7 +301,7 @@ public:
394 301
395 ai = bi; 302 ai = bi;
396 bi = wls[control]; 303 bi = wls[control];
397 - getYZ(b, c, bi); 304 + read_x_from_xz(b, c, bi);
398 305
399 } 306 }
400 //if the last BL point on the last band of the file? 307 //if the last BL point on the last band of the file?
@@ -402,7 +309,7 @@ public: @@ -402,7 +309,7 @@ public:
402 309
403 std::swap(a, b); //swap the baseline band pointers 310 std::swap(a, b); //swap the baseline band pointers
404 311
405 - memset(b, (char)0, X * sizeof(T) ); //clear the high band 312 + memset(b, (char)0, X() * sizeof(T) ); //clear the high band
406 313
407 ai = bi; 314 ai = bi;
408 bi = w[B - 1]; 315 bi = w[B - 1];
@@ -412,7 +319,7 @@ public: @@ -412,7 +319,7 @@ public:
412 ci = w[cii]; 319 ci = w[cii];
413 320
414 //perform the baseline correction 321 //perform the baseline correction
415 - for(unsigned i=0; i < X; i++) 322 + for(unsigned i=0; i < X(); i++)
416 { 323 {
417 double r = (double) (ci - ai) / (double) (bi - ai); 324 double r = (double) (ci - ai) / (double) (bi - ai);
418 c[i * B + cii] =(T) ( c[i * B + cii] - (b[i] - a[i]) * r - a[i] ); 325 c[i * B + cii] =(T) ( c[i * B + cii] - (b[i] - a[i]) * r - a[i] );
@@ -439,11 +346,9 @@ public: @@ -439,11 +346,9 @@ public:
439 /// @param t is a threshold specified such that a spectrum with a value at w less than t is set to zero. Setting this threshold allows the user to limit division by extremely small numbers. 346 /// @param t is a threshold specified such that a spectrum with a value at w less than t is set to zero. Setting this threshold allows the user to limit division by extremely small numbers.
440 bool normalize(std::string outname, double w, double t = 0.0) 347 bool normalize(std::string outname, double w, double t = 0.0)
441 { 348 {
442 - unsigned int B = R[2]; //calculate the number of bands  
443 - unsigned int Y = R[1];  
444 - unsigned int X = R[0];  
445 - unsigned int ZX = R[2] * R[0];  
446 - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band 349 + unsigned int B = Z(); //calculate the number of bands
  350 + unsigned int ZX = Z() * X();
  351 + unsigned int XY = X() * Y(); //calculate the number of pixels in a band
447 unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band 352 unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band
448 unsigned int L = ZX * sizeof(T); 353 unsigned int L = ZX * sizeof(T);
449 354
@@ -458,11 +363,11 @@ public: @@ -458,11 +363,11 @@ public:
458 363
459 band(b, w); //get the certain band into memory 364 band(b, w); //get the certain band into memory
460 365
461 - for(unsigned j = 0; j < Y; j++) 366 + for(unsigned j = 0; j < Y(); j++)
462 { 367 {
463 - getY(c, j);  
464 - unsigned jX = j * X; //to avoid calculating it many times  
465 - for(unsigned i = 0; i < X; i++) 368 + read_plane_y(c, j);
  369 + unsigned jX = j * X(); //to avoid calculating it many times
  370 + for(unsigned i = 0; i < X(); i++)
466 { 371 {
467 unsigned iB = i * B; 372 unsigned iB = i * B;
468 for(unsigned m = 0; m < B; m++) 373 for(unsigned m = 0; m < B; m++)
@@ -494,7 +399,7 @@ public: @@ -494,7 +399,7 @@ public:
494 bil(temp); 399 bil(temp);
495 400
496 stim::bil<T> n; 401 stim::bil<T> n;
497 - if(n.open(temp, R[0], R[1], R[2], offset, w)==false){ //open infile 402 + if(n.open(temp, X(), Y(), Z(), offset, w)==false){ //open infile
498 std::cout<<"ERROR: unable to open input file"<<std::endl; 403 std::cout<<"ERROR: unable to open input file"<<std::endl;
499 exit(1); 404 exit(1);
500 } 405 }
@@ -511,7 +416,7 @@ public: @@ -511,7 +416,7 @@ public:
511 /// @param outname is the name of the output BIL file to be saved to disk. 416 /// @param outname is the name of the output BIL file to be saved to disk.
512 bool bil(std::string outname) 417 bool bil(std::string outname)
513 { 418 {
514 - unsigned int S = R[0] * R[2] * sizeof(T); //calculate the number of bytes in a ZX slice 419 + unsigned int S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice
515 420
516 std::ofstream target(outname.c_str(), std::ios::binary); 421 std::ofstream target(outname.c_str(), std::ios::binary);
517 std::string headername = outname + ".hdr"; 422 std::string headername = outname + ".hdr";
@@ -521,14 +426,14 @@ public: @@ -521,14 +426,14 @@ public:
521 T * q; //pointer to the current XZ slice for bil file 426 T * q; //pointer to the current XZ slice for bil file
522 q = (T*)malloc(S); 427 q = (T*)malloc(S);
523 428
524 - for ( unsigned i = 0; i < R[1]; i++) 429 + for ( unsigned i = 0; i < Y(); i++)
525 { 430 {
526 - getY(p, i);  
527 - for ( unsigned k = 0; k < R[2]; k++) 431 + read_plane_y(p, i);
  432 + for ( unsigned k = 0; k < Z(); k++)
528 { 433 {
529 - unsigned ks = k * R[0];  
530 - for ( unsigned j = 0; j < R[0]; j++)  
531 - q[ks + j] = p[k + j * R[2]]; 434 + unsigned ks = k * X();
  435 + for ( unsigned j = 0; j < X(); j++)
  436 + q[ks + j] = p[k + j * Z()];
532 } 437 }
533 target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file 438 target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file
534 } 439 }
@@ -550,7 +455,7 @@ public: @@ -550,7 +455,7 @@ public:
550 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. 455 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size.
551 bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){ 456 bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){
552 457
553 - unsigned XY = R[0] * R[1]; 458 + unsigned XY = X() * Y();
554 band(result, wavelength); //get band 459 band(result, wavelength); //get band
555 460
556 //perform the baseline correction 461 //perform the baseline correction
@@ -571,7 +476,7 @@ public: @@ -571,7 +476,7 @@ public:
571 476
572 T* lp; 477 T* lp;
573 T* rp; 478 T* rp;
574 - unsigned XY = R[0] * R[1]; 479 + unsigned XY = X() * Y();
575 unsigned S = XY * sizeof(T); 480 unsigned S = XY * sizeof(T);
576 lp = (T*) malloc(S); //memory allocation 481 lp = (T*) malloc(S); //memory allocation
577 rp = (T*) malloc(S); 482 rp = (T*) malloc(S);
@@ -601,7 +506,7 @@ public: @@ -601,7 +506,7 @@ public:
601 T* cur; //current band 1 506 T* cur; //current band 1
602 T* cur2; //current band 2 507 T* cur2; //current band 2
603 508
604 - unsigned XY = R[0] * R[1]; 509 + unsigned XY = X() * Y();
605 unsigned S = XY * sizeof(T); 510 unsigned S = XY * sizeof(T);
606 511
607 lp = (T*) malloc(S); //memory allocation 512 lp = (T*) malloc(S); //memory allocation
@@ -682,14 +587,14 @@ public: @@ -682,14 +587,14 @@ public:
682 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 587 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
683 bool ph_to_ph(double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, T * result){ 588 bool ph_to_ph(double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, T * result){
684 589
685 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
686 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 590 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  591 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
687 592
688 //get the two peak band 593 //get the two peak band
689 height(lb1, rb1, pos1, p1); 594 height(lb1, rb1, pos1, p1);
690 height(lb2, rb2, pos2, p2); 595 height(lb2, rb2, pos2, p2);
691 //calculate the ratio in result 596 //calculate the ratio in result
692 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 597 + for(unsigned i = 0; i < X() * Y(); i++){
693 if(p1[i] == 0 && p2[i] ==0) 598 if(p1[i] == 0 && p2[i] ==0)
694 result[i] = 1; 599 result[i] = 1;
695 else 600 else
@@ -713,14 +618,14 @@ public: @@ -713,14 +618,14 @@ public:
713 bool pa_to_ph(double lb1, double rb1, double lab1, double rab1, 618 bool pa_to_ph(double lb1, double rb1, double lab1, double rab1,
714 double lb2, double rb2, double pos, T* result){ 619 double lb2, double rb2, double pos, T* result){
715 620
716 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
717 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 621 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  622 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
718 623
719 //get the area and the peak band 624 //get the area and the peak band
720 area(lb1, rb1, lab1, rab1, p1); 625 area(lb1, rb1, lab1, rab1, p1);
721 height(lb2, rb2, pos, p2); 626 height(lb2, rb2, pos, p2);
722 //calculate the ratio in result 627 //calculate the ratio in result
723 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 628 + for(unsigned i = 0; i < X() * Y(); i++){
724 if(p1[i] == 0 && p2[i] ==0) 629 if(p1[i] == 0 && p2[i] ==0)
725 result[i] = 1; 630 result[i] = 1;
726 else 631 else
@@ -746,14 +651,14 @@ public: @@ -746,14 +651,14 @@ public:
746 bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, 651 bool pa_to_pa(double lb1, double rb1, double lab1, double rab1,
747 double lb2, double rb2, double lab2, double rab2, T* result){ 652 double lb2, double rb2, double lab2, double rab2, T* result){
748 653
749 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
750 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 654 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  655 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
751 656
752 //get the area and the peak band 657 //get the area and the peak band
753 area(lb1, rb1, lab1, rab1, p1); 658 area(lb1, rb1, lab1, rab1, p1);
754 area(lb2, rb2, lab2, rab2, p2); 659 area(lb2, rb2, lab2, rab2, p2);
755 //calculate the ratio in result 660 //calculate the ratio in result
756 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 661 + for(unsigned i = 0; i < X() * Y(); i++){
757 if(p1[i] == 0 && p2[i] ==0) 662 if(p1[i] == 0 && p2[i] ==0)
758 result[i] = 1; 663 result[i] = 1;
759 else 664 else
@@ -778,7 +683,7 @@ public: @@ -778,7 +683,7 @@ public:
778 T* cur; //current band 1 683 T* cur; //current band 1
779 T* cur2; //current band 2 684 T* cur2; //current band 2
780 685
781 - unsigned XY = R[0] * R[1]; 686 + unsigned XY = X() * Y();
782 unsigned S = XY * sizeof(T); 687 unsigned S = XY * sizeof(T);
783 688
784 lp = (T*) malloc(S); //memory allocation 689 lp = (T*) malloc(S); //memory allocation
@@ -854,14 +759,14 @@ public: @@ -854,14 +759,14 @@ public:
854 /// @param rab is the label for the end of the peak 759 /// @param rab is the label for the end of the peak
855 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 760 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
856 bool cpoint(double lb, double rb, double lab, double rab, T* result){ 761 bool cpoint(double lb, double rb, double lab, double rab, T* result){
857 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
858 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 762 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  763 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
859 764
860 //get the area and the peak band 765 //get the area and the peak band
861 x_area(lb, rb, lab, rab, p1); 766 x_area(lb, rb, lab, rab, p1);
862 area(lb, rb, lab, rab, p2); 767 area(lb, rb, lab, rab, p2);
863 //calculate the ratio in result 768 //calculate the ratio in result
864 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 769 + for(unsigned i = 0; i < X() * Y(); i++){
865 if(p1[i] == 0 && p2[i] ==0) 770 if(p1[i] == 0 && p2[i] ==0)
866 result[i] = 1; 771 result[i] = 1;
867 else 772 else
@@ -882,10 +787,10 @@ public: @@ -882,10 +787,10 @@ public:
882 /// @param p is a pointer to a pre-allocated array at least X * Y in size 787 /// @param p is a pointer to a pre-allocated array at least X * Y in size
883 bool build_mask(double mask_band, double threshold, unsigned char* p){ 788 bool build_mask(double mask_band, double threshold, unsigned char* p){
884 789
885 - T* temp = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate memory for the certain band 790 + T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band
886 band(temp, mask_band); 791 band(temp, mask_band);
887 792
888 - for (unsigned int i = 0; i < R[0] * R[1];i++) { 793 + for (unsigned int i = 0; i < X() * Y();i++) {
889 if (temp[i] < threshold) 794 if (temp[i] < threshold)
890 p[i] = 0; 795 p[i] = 0;
891 else 796 else
@@ -905,20 +810,20 @@ public: @@ -905,20 +810,20 @@ public:
905 810
906 std::ofstream target(outfile.c_str(), std::ios::binary); 811 std::ofstream target(outfile.c_str(), std::ios::binary);
907 812
908 - unsigned ZX = R[2] * R[0]; //calculate the number of values in a page (XZ in BIP) 813 + unsigned ZX = Z() * X(); //calculate the number of values in a page (XZ in BIP)
909 unsigned L = ZX * sizeof(T); //calculate the number of bytes in a page 814 unsigned L = ZX * sizeof(T); //calculate the number of bytes in a page
910 815
911 T * temp = (T*)malloc(L); //allocate space for that page 816 T * temp = (T*)malloc(L); //allocate space for that page
912 817
913 - for (unsigned i = 0; i < R[1]; i++) //for each page (Y in BIP) 818 + for (unsigned i = 0; i < Y(); i++) //for each page (Y in BIP)
914 { 819 {
915 - getY(temp, i); //load that page (it's pointed to by temp)  
916 - for ( unsigned j = 0; j < R[0]; j++) //for each X value 820 + read_plane_y(temp, i); //load that page (it's pointed to by temp)
  821 + for ( unsigned j = 0; j < X(); j++) //for each X value
917 { 822 {
918 - for (unsigned k = 0; k < R[2]; k++) //for each B value (band) 823 + for (unsigned k = 0; k < Z(); k++) //for each B value (band)
919 { 824 {
920 - if (p[i * R[0] + j] == 0) //if the mask value is zero  
921 - temp[j * R[2] + k] = 0; //set the pixel value to zero 825 + if (p[i * X() + j] == 0) //if the mask value is zero
  826 + temp[j * Z() + k] = 0; //set the pixel value to zero
922 else //otherwise just continue 827 else //otherwise just continue
923 continue; 828 continue;
924 } 829 }
@@ -933,30 +838,29 @@ public: @@ -933,30 +838,29 @@ public:
933 838
934 ///Saves to disk only those spectra corresponding to mask values != 0 839 ///Saves to disk only those spectra corresponding to mask values != 0
935 bool sift_mask(std::string outfile, unsigned char* p){ 840 bool sift_mask(std::string outfile, unsigned char* p){
936 - // Assume R[0] = X, R[1] = Y, R[2] = Z. 841 + // Assume X() = X, Y() = Y, Z() = Z.
937 std::ofstream target(outfile.c_str(), std::ios::binary); 842 std::ofstream target(outfile.c_str(), std::ios::binary);
938 843
939 //for loading pages: 844 //for loading pages:
940 - unsigned ZX = R[0] * R[2]; //calculate the number of values in an XZ page on disk 845 + unsigned ZX = X() * Z(); //calculate the number of values in an XZ page on disk
941 unsigned L = ZX * sizeof(T); //calculate the size of the page (in bytes) 846 unsigned L = ZX * sizeof(T); //calculate the size of the page (in bytes)
942 T * temp = (T*)malloc(L); //allocate memory for a temporary page 847 T * temp = (T*)malloc(L); //allocate memory for a temporary page
943 848
944 //for saving spectra: 849 //for saving spectra:
945 - unsigned Z = R[2]; //calculate the number of values in a spectrum  
946 - unsigned LZ = Z * sizeof(T); //calculate the size of the spectrum (in bytes) 850 + unsigned LZ = Z() * sizeof(T); //calculate the size of the spectrum (in bytes)
947 T * tempZ = (T*)malloc(LZ); //allocate memory for a temporary spectrum 851 T * tempZ = (T*)malloc(LZ); //allocate memory for a temporary spectrum
948 - spectrum(tempZ, R[0] - 1, R[1] - 1); //creates a dummy spectrum by taking the last spectrum in the image 852 + spectrum(tempZ, X() - 1, Y() - 1); //creates a dummy spectrum by taking the last spectrum in the image
949 853
950 - for (unsigned i = 0; i < R[1]; i++) //Select a page by choosing Y coordinate, R[1] 854 + for (unsigned i = 0; i < Y(); i++) //Select a page by choosing Y coordinate, Y()
951 { 855 {
952 - getY(temp, i); //retrieve an ZX page, store in "temp"  
953 - for (unsigned j = 0; j < R[0]; j++) //Select a pixel by choosing X coordinate in the page, R[0] 856 + read_plane_y(temp, i); //retrieve an ZX page, store in "temp"
  857 + for (unsigned j = 0; j < X(); j++) //Select a pixel by choosing X coordinate in the page, X()
954 { 858 {
955 - if (p[j * R[0] + i] != 0) //if the mask != 0 at that XY pixel 859 + if (p[j * X() + i] != 0) //if the mask != 0 at that XY pixel
956 { 860 {
957 - for (unsigned k = 0; k < R[2]; k++) //Select a voxel by choosing Z coordinate at the pixel 861 + for (unsigned k = 0; k < Z(); k++) //Select a voxel by choosing Z coordinate at the pixel
958 { 862 {
959 - tempZ[k] = temp[i*R[2] + k]; //Pass the correct spectral value from XZ page into the spectrum to be saved. 863 + tempZ[k] = temp[i*Z() + k]; //Pass the correct spectral value from XZ page into the spectrum to be saved.
960 } 864 }
961 target.write(reinterpret_cast<const char*>(tempZ), LZ); //write that spectrum to disk. Size is L2. 865 target.write(reinterpret_cast<const char*>(tempZ), LZ); //write that spectrum to disk. Size is L2.
962 } 866 }
@@ -973,16 +877,16 @@ public: @@ -973,16 +877,16 @@ public:
973 877
974 /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages. 878 /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages.
975 bool band_avg(T* p){ 879 bool band_avg(T* p){
976 - unsigned long long XY = R[0] * R[1]; 880 + unsigned long long XY = X() * Y();
977 //get every pixel and calculate average value 881 //get every pixel and calculate average value
978 - T* temp = (T*)malloc(sizeof(T) * R[2]); 882 + T* temp = (T*)malloc(sizeof(T) * Z());
979 T sum; 883 T sum;
980 for (unsigned i = 0; i < XY; i++){ 884 for (unsigned i = 0; i < XY; i++){
981 pixel(temp, i); 885 pixel(temp, i);
982 //calculate the sum value of every value 886 //calculate the sum value of every value
983 sum = 0; //initialize sum value 887 sum = 0; //initialize sum value
984 - for (unsigned j = 0; j < R[2]; j++){  
985 - sum += temp[j]/(T)R[2]; 888 + for (unsigned j = 0; j < Z(); j++){
  889 + sum += temp[j]/(T)Z();
986 } 890 }
987 p[i] = sum; 891 p[i] = sum;
988 } 892 }
@@ -995,10 +899,10 @@ public: @@ -995,10 +899,10 @@ public:
995 /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum 899 /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum
996 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location 900 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
997 bool avg_band(T*p, unsigned char* mask){ 901 bool avg_band(T*p, unsigned char* mask){
998 - unsigned long long XY = R[0] * R[1];  
999 - T* temp = (T*)malloc(sizeof(T) * R[2]); 902 + unsigned long long XY = X() * Y();
  903 + T* temp = (T*)malloc(sizeof(T) * Z());
1000 //Iinitialize 904 //Iinitialize
1001 - for (unsigned j = 0; j < R[2]; j++){ 905 + for (unsigned j = 0; j < Z(); j++){
1002 p[j] = 0; 906 p[j] = 0;
1003 } 907 }
1004 //calculate vaild number in a band 908 //calculate vaild number in a band
@@ -1012,7 +916,7 @@ public: @@ -1012,7 +916,7 @@ public:
1012 for (unsigned i = 0; i < XY; i++){ 916 for (unsigned i = 0; i < XY; i++){
1013 if (mask[i] != 0){ 917 if (mask[i] != 0){
1014 pixel(temp, i); 918 pixel(temp, i);
1015 - for (unsigned j = 0; j < R[2]; j++){ 919 + for (unsigned j = 0; j < Z(); j++){
1016 p[j] += temp[j] / (T)count; 920 p[j] += temp[j] / (T)count;
1017 } 921 }
1018 } 922 }
@@ -1028,8 +932,8 @@ public: @@ -1028,8 +932,8 @@ public:
1028 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location 932 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
1029 bool co_matrix(T* co, T* avg, unsigned char *mask){ 933 bool co_matrix(T* co, T* avg, unsigned char *mask){
1030 //memory allocation 934 //memory allocation
1031 - unsigned long long xy = R[0] * R[1];  
1032 - unsigned int B = R[2]; 935 + unsigned long long xy = X() * Y();
  936 + unsigned int B = Z();
1033 T* temp = (T*)malloc(sizeof(T) * B); 937 T* temp = (T*)malloc(sizeof(T) * B);
1034 //count vaild pixels in a band 938 //count vaild pixels in a band
1035 unsigned count = 0; 939 unsigned count = 0;
@@ -1079,17 +983,17 @@ public: @@ -1079,17 +983,17 @@ public:
1079 //calculate the new number of samples and lines 983 //calculate the new number of samples and lines
1080 unsigned long long sam = x1 - x0; //samples 984 unsigned long long sam = x1 - x0; //samples
1081 unsigned long long lin = y1 - y0; //lines 985 unsigned long long lin = y1 - y0; //lines
1082 - unsigned long long L = R[2] * sizeof(T); 986 + unsigned long long L = Z() * sizeof(T);
1083 //get specified band and save 987 //get specified band and save
1084 T* temp = (T*)malloc(L); 988 T* temp = (T*)malloc(L);
1085 std::ofstream out(outfile.c_str(), std::ios::binary); 989 std::ofstream out(outfile.c_str(), std::ios::binary);
1086 //get start 990 //get start
1087 - unsigned long long sp = y0 * R[0] + x0; //start pixel 991 + unsigned long long sp = y0 * X() + x0; //start pixel
1088 for (unsigned i = 0; i < lin; i++) 992 for (unsigned i = 0; i < lin; i++)
1089 { 993 {
1090 for (unsigned j = 0; j < sam; j++) 994 for (unsigned j = 0; j < sam; j++)
1091 { 995 {
1092 - pixel(temp, sp + j + i * R[0]); 996 + pixel(temp, sp + j + i * X());
1093 out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file 997 out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file
1094 } 998 }
1095 } 999 }
@@ -30,12 +30,26 @@ protected: @@ -30,12 +30,26 @@ protected:
30 std::vector<double> w; //band wavelengths 30 std::vector<double> w; //band wavelengths
31 unsigned int offset; 31 unsigned int offset;
32 32
  33 + using binary<T>::R;
  34 +
  35 + unsigned long X(){
  36 + return R[0];
  37 + }
  38 + unsigned long Y(){
  39 + return R[1];
  40 + }
  41 + unsigned long Z(){
  42 + return R[2];
  43 + }
  44 +
33 public: 45 public:
34 46
35 using binary<T>::open; 47 using binary<T>::open;
36 using binary<T>::file; 48 using binary<T>::file;
  49 + using binary<T>::read_line_01;
  50 + using binary<T>::read_plane_2;
37 //using binary<T>::getSlice; 51 //using binary<T>::getSlice;
38 - using binary<T>::R; 52 +
39 53
40 /// Open a data file for reading using the class interface. 54 /// Open a data file for reading using the class interface.
41 55
@@ -61,17 +75,7 @@ public: @@ -61,17 +75,7 @@ public:
61 /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. 75 /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size.
62 /// @param page <= B is the integer number of the band to be copied. 76 /// @param page <= B is the integer number of the band to be copied.
63 bool band_index( T * p, unsigned int page){ 77 bool band_index( T * p, unsigned int page){
64 -  
65 - if (page >= R[2]){ //make sure the bank number is right  
66 - std::cout<<"ERROR: page out of range"<<std::endl;  
67 - return false;  
68 - }  
69 - //move the file pointer to the start of the page on disk  
70 - file.seekg(R[0] * R[1] * page * sizeof(T));  
71 - //read the page from disk  
72 - file.read((char *)p, sizeof(T) * R[0] * R[1]);  
73 -  
74 - return true; 78 + return read_plane_2(p, page);
75 } 79 }
76 80
77 /// Retrieve a single band (by numerical label) and stores it in pre-allocated memory. 81 /// Retrieve a single band (by numerical label) and stores it in pre-allocated memory.
@@ -84,7 +88,7 @@ public: @@ -84,7 +88,7 @@ public:
84 if(w.size() == 0) 88 if(w.size() == 0)
85 return band_index(p, (unsigned int)wavelength); 89 return band_index(p, (unsigned int)wavelength);
86 90
87 - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band 91 + unsigned int XY = X() * Y(); //calculate the number of pixels in a band
88 unsigned page = 0; 92 unsigned page = 0;
89 93
90 94
@@ -101,8 +105,8 @@ public: @@ -101,8 +105,8 @@ public:
101 page++; 105 page++;
102 //if wavelength is larger than the last wavelength in the header file 106 //if wavelength is larger than the last wavelength in the header file
103 // (the wavelength is out of bounds) 107 // (the wavelength is out of bounds)
104 - if (page == R[2]) {  
105 - band_index(p, R[2]-1); //return the last band 108 + if (page == Z()) {
  109 + band_index(p, Z()-1); //return the last band
106 return true; 110 return true;
107 } 111 }
108 } 112 }
@@ -131,28 +135,13 @@ public: @@ -131,28 +135,13 @@ public:
131 return true; 135 return true;
132 } 136 }
133 137
134 - /// Retrieve a single spectrum (B-axis line) at a given (x, y) location and stores it in pre-allocated memory. 138 + /// Retrieve a single spectrum (Z-axis line) at a given (x, y) location and stores it in pre-allocated memory.
135 139
136 /// @param p is a pointer to pre-allocated memory at least B * sizeof(T) in size. 140 /// @param p is a pointer to pre-allocated memory at least B * sizeof(T) in size.
137 /// @param x is the x-coordinate (dimension 1) of the spectrum. 141 /// @param x is the x-coordinate (dimension 1) of the spectrum.
138 /// @param y is the y-coordinate (dimension 2) of the spectrum. 142 /// @param y is the y-coordinate (dimension 2) of the spectrum.
139 bool spectrum(T * p, unsigned x, unsigned y){ 143 bool spectrum(T * p, unsigned x, unsigned y){
140 -  
141 - unsigned int i;  
142 -  
143 - if ( x >= R[0] || y >= R[1]){ //make sure the sample and line number is right  
144 - std::cout<<"ERROR: sample or line out of range"<<std::endl;  
145 - return false;  
146 - }  
147 -  
148 - file.seekg((x + y * R[0]) * sizeof(T), std::ios::beg); //point to the certain sample and line  
149 - for (i = 0; i < R[2]; i++)  
150 - {  
151 - file.read((char *)(p + i), sizeof(T));  
152 - file.seekg((R[1] * R[0] - 1) * sizeof(T), std::ios::cur); //go to the next band  
153 - }  
154 -  
155 - return true; 144 + return read_line_01(p, x, y);
156 } 145 }
157 146
158 /// Retrieve a single pixel and stores it in pre-allocated memory. 147 /// Retrieve a single pixel and stores it in pre-allocated memory.
@@ -161,14 +150,14 @@ public: @@ -161,14 +150,14 @@ public:
161 /// @param n is an integer index to the pixel using linear array indexing. 150 /// @param n is an integer index to the pixel using linear array indexing.
162 bool pixel(T * p, unsigned n){ 151 bool pixel(T * p, unsigned n){
163 152
164 - unsigned bandnum = R[0] * R[1]; //calculate numbers in one band 153 + unsigned bandnum = X() * Y(); //calculate numbers in one band
165 if ( n >= bandnum){ //make sure the pixel number is right 154 if ( n >= bandnum){ //make sure the pixel number is right
166 std::cout<<"ERROR: sample or line out of range"<<std::endl; 155 std::cout<<"ERROR: sample or line out of range"<<std::endl;
167 return false; 156 return false;
168 } 157 }
169 158
170 file.seekg(n * sizeof(T), std::ios::beg); //point to the certain pixel 159 file.seekg(n * sizeof(T), std::ios::beg); //point to the certain pixel
171 - for (unsigned i = 0; i < R[2]; i++) 160 + for (unsigned i = 0; i < Z(); i++)
172 { 161 {
173 file.read((char *)(p + i), sizeof(T)); 162 file.read((char *)(p + i), sizeof(T));
174 file.seekg((bandnum - 1) * sizeof(T), std::ios::cur); //go to the next band 163 file.seekg((bandnum - 1) * sizeof(T), std::ios::cur); //go to the next band
@@ -189,8 +178,8 @@ public: @@ -189,8 +178,8 @@ public:
189 std::string headername = outname + ".hdr"; //the header file name 178 std::string headername = outname + ".hdr"; //the header file name
190 179
191 //simplify image resolution 180 //simplify image resolution
192 - unsigned int B = R[2]; //calculate the number of bands  
193 - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band 181 + unsigned int B = Z(); //calculate the number of bands
  182 + unsigned int XY = X() * Y(); //calculate the number of pixels in a band
194 unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band 183 unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band
195 184
196 double ai, bi; //stores the two baseline points wavelength surrounding the current band 185 double ai, bi; //stores the two baseline points wavelength surrounding the current band
@@ -289,8 +278,8 @@ public: @@ -289,8 +278,8 @@ public:
289 /// @param t is a threshold specified such that a spectrum with a value at w less than t is set to zero. Setting this threshold allows the user to limit division by extremely small numbers. 278 /// @param t is a threshold specified such that a spectrum with a value at w less than t is set to zero. Setting this threshold allows the user to limit division by extremely small numbers.
290 bool normalize(std::string outname, double w, double t = 0.0) 279 bool normalize(std::string outname, double w, double t = 0.0)
291 { 280 {
292 - unsigned int B = R[2]; //calculate the number of bands  
293 - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band 281 + unsigned int B = Z(); //calculate the number of bands
  282 + unsigned int XY = X() * Y(); //calculate the number of pixels in a band
294 unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band 283 unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band
295 284
296 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file 285 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file
@@ -336,7 +325,7 @@ public: @@ -336,7 +325,7 @@ public:
336 bil(temp); 325 bil(temp);
337 326
338 stim::bil<T> n; 327 stim::bil<T> n;
339 - if(n.open(temp, R[0], R[1], R[2], offset, w)==false){ //open infile 328 + if(n.open(temp, X(), Y(), Z(), offset, w)==false){ //open infile
340 std::cout<<"ERROR: unable to open input file"<<std::endl; 329 std::cout<<"ERROR: unable to open input file"<<std::endl;
341 exit(1); 330 exit(1);
342 } 331 }
@@ -354,8 +343,8 @@ public: @@ -354,8 +343,8 @@ public:
354 bool bil(std::string outname) 343 bool bil(std::string outname)
355 { 344 {
356 //simplify image resolution 345 //simplify image resolution
357 - unsigned int L = R[0] * R[2] * sizeof(T); //calculate the number of bytes of a ZX slice  
358 - unsigned int jump = (R[1] - 1) * R[0] * sizeof(T); 346 + unsigned int L = X() * Z() * sizeof(T); //calculate the number of bytes of a ZX slice
  347 + unsigned int jump = (Y() - 1) * X() * sizeof(T);
359 348
360 std::ofstream target(outname.c_str(), std::ios::binary); 349 std::ofstream target(outname.c_str(), std::ios::binary);
361 std::string headername = outname + ".hdr"; 350 std::string headername = outname + ".hdr";
@@ -363,12 +352,12 @@ public: @@ -363,12 +352,12 @@ public:
363 T * p; //pointer to the current spectrum 352 T * p; //pointer to the current spectrum
364 p = (T*)malloc(L); 353 p = (T*)malloc(L);
365 354
366 - for ( unsigned i = 0; i < R[1]; i++) 355 + for ( unsigned i = 0; i < Y(); i++)
367 { 356 {
368 - file.seekg(R[0] * i * sizeof(T), std::ios::beg);  
369 - for ( unsigned j = 0; j < R[2]; j++ ) 357 + file.seekg(X() * i * sizeof(T), std::ios::beg);
  358 + for ( unsigned j = 0; j < Z(); j++ )
370 { 359 {
371 - file.read((char *)(p + j * R[0]), sizeof(T) * R[0]); 360 + file.read((char *)(p + j * X()), sizeof(T) * X());
372 file.seekg(jump, std::ios::cur); //go to the next band 361 file.seekg(jump, std::ios::cur); //go to the next band
373 } 362 }
374 target.write(reinterpret_cast<const char*>(p), L); //write XZ slice data into target file 363 target.write(reinterpret_cast<const char*>(p), L); //write XZ slice data into target file
@@ -391,7 +380,7 @@ public: @@ -391,7 +380,7 @@ public:
391 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. 380 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size.
392 bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){ 381 bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){
393 382
394 - unsigned XY = R[0] * R[1]; 383 + unsigned XY = X() * Y();
395 band(result, wavelength); //get band 384 band(result, wavelength); //get band
396 385
397 //perform the baseline correction 386 //perform the baseline correction
@@ -412,7 +401,7 @@ public: @@ -412,7 +401,7 @@ public:
412 401
413 T* lp; 402 T* lp;
414 T* rp; 403 T* rp;
415 - unsigned XY = R[0] * R[1]; 404 + unsigned XY = X() * Y();
416 unsigned S = XY * sizeof(T); 405 unsigned S = XY * sizeof(T);
417 lp = (T*) malloc(S); //memory allocation 406 lp = (T*) malloc(S); //memory allocation
418 rp = (T*) malloc(S); 407 rp = (T*) malloc(S);
@@ -442,7 +431,7 @@ public: @@ -442,7 +431,7 @@ public:
442 T* cur; //current band 1 431 T* cur; //current band 1
443 T* cur2; //current band 2 432 T* cur2; //current band 2
444 433
445 - unsigned XY = R[0] * R[1]; 434 + unsigned XY = X() * Y();
446 unsigned S = XY * sizeof(T); 435 unsigned S = XY * sizeof(T);
447 436
448 lp = (T*) malloc(S); //memory allocation 437 lp = (T*) malloc(S); //memory allocation
@@ -523,14 +512,14 @@ public: @@ -523,14 +512,14 @@ public:
523 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 512 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
524 bool ph_to_ph(double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, T * result){ 513 bool ph_to_ph(double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, T * result){
525 514
526 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
527 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 515 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  516 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
528 517
529 //get the two peak band 518 //get the two peak band
530 height(lb1, rb1, pos1, p1); 519 height(lb1, rb1, pos1, p1);
531 height(lb2, rb2, pos2, p2); 520 height(lb2, rb2, pos2, p2);
532 //calculate the ratio in result 521 //calculate the ratio in result
533 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 522 + for(unsigned i = 0; i < X() * Y(); i++){
534 if(p1[i] == 0 && p2[i] ==0) 523 if(p1[i] == 0 && p2[i] ==0)
535 result[i] = 1; 524 result[i] = 1;
536 else 525 else
@@ -554,14 +543,14 @@ public: @@ -554,14 +543,14 @@ public:
554 bool pa_to_ph(double lb1, double rb1, double lab1, double rab1, 543 bool pa_to_ph(double lb1, double rb1, double lab1, double rab1,
555 double lb2, double rb2, double pos, T* result){ 544 double lb2, double rb2, double pos, T* result){
556 545
557 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
558 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 546 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  547 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
559 548
560 //get the area and the peak band 549 //get the area and the peak band
561 area(lb1, rb1, lab1, rab1, p1); 550 area(lb1, rb1, lab1, rab1, p1);
562 height(lb2, rb2, pos, p2); 551 height(lb2, rb2, pos, p2);
563 //calculate the ratio in result 552 //calculate the ratio in result
564 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 553 + for(unsigned i = 0; i < X() * Y(); i++){
565 if(p1[i] == 0 && p2[i] ==0) 554 if(p1[i] == 0 && p2[i] ==0)
566 result[i] = 1; 555 result[i] = 1;
567 else 556 else
@@ -587,14 +576,14 @@ public: @@ -587,14 +576,14 @@ public:
587 bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, 576 bool pa_to_pa(double lb1, double rb1, double lab1, double rab1,
588 double lb2, double rb2, double lab2, double rab2, T* result){ 577 double lb2, double rb2, double lab2, double rab2, T* result){
589 578
590 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
591 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 579 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  580 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
592 581
593 //get the area and the peak band 582 //get the area and the peak band
594 area(lb1, rb1, lab1, rab1, p1); 583 area(lb1, rb1, lab1, rab1, p1);
595 area(lb2, rb2, lab2, rab2, p2); 584 area(lb2, rb2, lab2, rab2, p2);
596 //calculate the ratio in result 585 //calculate the ratio in result
597 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 586 + for(unsigned i = 0; i < X() * Y(); i++){
598 if(p1[i] == 0 && p2[i] ==0) 587 if(p1[i] == 0 && p2[i] ==0)
599 result[i] = 1; 588 result[i] = 1;
600 else 589 else
@@ -619,7 +608,7 @@ public: @@ -619,7 +608,7 @@ public:
619 T* cur; //current band 1 608 T* cur; //current band 1
620 T* cur2; //current band 2 609 T* cur2; //current band 2
621 610
622 - unsigned XY = R[0] * R[1]; 611 + unsigned XY = X() * Y();
623 unsigned S = XY * sizeof(T); 612 unsigned S = XY * sizeof(T);
624 613
625 lp = (T*) malloc(S); //memory allocation 614 lp = (T*) malloc(S); //memory allocation
@@ -695,14 +684,14 @@ public: @@ -695,14 +684,14 @@ public:
695 /// @param rab is the label for the end of the peak 684 /// @param rab is the label for the end of the peak
696 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size 685 /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
697 bool cpoint(double lb, double rb, double lab, double rab, T* result){ 686 bool cpoint(double lb, double rb, double lab, double rab, T* result){
698 - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T));  
699 - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); 687 + T* p1 = (T*)malloc(X() * Y() * sizeof(T));
  688 + T* p2 = (T*)malloc(X() * Y() * sizeof(T));
700 689
701 //get the area and the peak band 690 //get the area and the peak band
702 x_area(lb, rb, lab, rab, p1); 691 x_area(lb, rb, lab, rab, p1);
703 area(lb, rb, lab, rab, p2); 692 area(lb, rb, lab, rab, p2);
704 //calculate the ratio in result 693 //calculate the ratio in result
705 - for(unsigned i = 0; i < R[0] * R[1]; i++){ 694 + for(unsigned i = 0; i < X() * Y(); i++){
706 if(p1[i] == 0 && p2[i] ==0) 695 if(p1[i] == 0 && p2[i] ==0)
707 result[i] = 1; 696 result[i] = 1;
708 else 697 else
@@ -723,10 +712,10 @@ public: @@ -723,10 +712,10 @@ public:
723 /// @param p is a pointer to a pre-allocated array at least X * Y in size 712 /// @param p is a pointer to a pre-allocated array at least X * Y in size
724 bool build_mask(double mask_band, double threshold, unsigned char* p = NULL){ 713 bool build_mask(double mask_band, double threshold, unsigned char* p = NULL){
725 714
726 - T* temp = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate memory for the certain band 715 + T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band
727 band(temp, mask_band); 716 band(temp, mask_band);
728 717
729 - for (unsigned int i = 0; i < R[0] * R[1]; i++) { 718 + for (unsigned int i = 0; i < X() * Y(); i++) {
730 if (temp[i] < threshold) 719 if (temp[i] < threshold)
731 p[i] = 0; 720 p[i] = 0;
732 else 721 else
@@ -746,12 +735,12 @@ public: @@ -746,12 +735,12 @@ public:
746 735
747 std::ofstream target(outfile.c_str(), std::ios::binary); 736 std::ofstream target(outfile.c_str(), std::ios::binary);
748 737
749 - unsigned XY = R[0] * R[1]; //calculate number of a band 738 + unsigned XY = X() * Y(); //calculate number of a band
750 unsigned L = XY * sizeof(T); 739 unsigned L = XY * sizeof(T);
751 740
752 T * temp = (T*)malloc(L); 741 T * temp = (T*)malloc(L);
753 742
754 - for (unsigned i = 0; i < R[2]; i++) //for each spectral bin 743 + for (unsigned i = 0; i < Z(); i++) //for each spectral bin
755 { 744 {
756 band_index(temp, i); //get the specified band (by index) 745 band_index(temp, i); //get the specified band (by index)
757 for ( unsigned j = 0; j < XY; j++) // for each pixel 746 for ( unsigned j = 0; j < XY; j++) // for each pixel
@@ -774,13 +763,13 @@ public: @@ -774,13 +763,13 @@ public:
774 bool sift_mask(std::string outfile, unsigned char* p){ 763 bool sift_mask(std::string outfile, unsigned char* p){
775 std::ofstream target(outfile.c_str(), std::ios::binary); 764 std::ofstream target(outfile.c_str(), std::ios::binary);
776 // open a band (XY plane) 765 // open a band (XY plane)
777 - unsigned XY = R[0] * R[1]; //Number of XY pixels 766 + unsigned XY = X() * Y(); //Number of XY pixels
778 unsigned L = XY * sizeof(T); //size of XY pixels 767 unsigned L = XY * sizeof(T); //size of XY pixels
779 768
780 T * temp = (T*)malloc(L); //allocate memory for a band 769 T * temp = (T*)malloc(L); //allocate memory for a band
781 T * temp_vox = (T*)malloc(sizeof(T)); //allocate memory for one voxel 770 T * temp_vox = (T*)malloc(sizeof(T)); //allocate memory for one voxel
782 771
783 - for (unsigned i = 0; i < R[2]; i++) //for each spectral bin 772 + for (unsigned i = 0; i < Z(); i++) //for each spectral bin
784 { 773 {
785 band_index(temp, i); //get the specified band (XY sheet by index) 774 band_index(temp, i); //get the specified band (XY sheet by index)
786 for (unsigned j = 0; j < XY; j++) // for each pixel 775 for (unsigned j = 0; j < XY; j++) // for each pixel
@@ -804,18 +793,18 @@ public: @@ -804,18 +793,18 @@ public:
804 793
805 /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages. 794 /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages.
806 bool band_avg(T* p){ 795 bool band_avg(T* p){
807 - unsigned long long XY = R[0] * R[1]; 796 + unsigned long long XY = X() * Y();
808 T* temp = (T*)malloc(sizeof(T) * XY); 797 T* temp = (T*)malloc(sizeof(T) * XY);
809 //initialize p 798 //initialize p
810 band_index(p, 0); 799 band_index(p, 0);
811 for (unsigned j = 0; j < XY; j++){ 800 for (unsigned j = 0; j < XY; j++){
812 - p[j] /= (T)R[2]; 801 + p[j] /= (T)Z();
813 } 802 }
814 //get every band and add them all 803 //get every band and add them all
815 - for (unsigned i = 1; i < R[2]; i++){ 804 + for (unsigned i = 1; i < Z(); i++){
816 band_index(temp, i); 805 band_index(temp, i);
817 for (unsigned j = 0; j < XY; j++){ 806 for (unsigned j = 0; j < XY; j++){
818 - p[j] += temp[j]/(T)R[2]; 807 + p[j] += temp[j]/(T)Z();
819 } 808 }
820 } 809 }
821 free(temp); 810 free(temp);
@@ -827,7 +816,7 @@ public: @@ -827,7 +816,7 @@ public:
827 /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum 816 /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum
828 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location 817 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
829 bool avg_band(T*p, unsigned char* mask){ 818 bool avg_band(T*p, unsigned char* mask){
830 - unsigned long long XY = R[0] * R[1]; 819 + unsigned long long XY = X() * Y();
831 unsigned count = 0; //count will store the number of masked pixels 820 unsigned count = 0; //count will store the number of masked pixels
832 T* temp = (T*)malloc(sizeof(T) * XY); 821 T* temp = (T*)malloc(sizeof(T) * XY);
833 //calculate this loop counts the number of true pixels in the mask 822 //calculate this loop counts the number of true pixels in the mask
@@ -836,9 +825,9 @@ public: @@ -836,9 +825,9 @@ public:
836 count++; 825 count++;
837 } 826 }
838 } 827 }
839 - //this loops goes through each band in B (R[2]) 828 + //this loops goes through each band in B (Z())
840 // masked (or valid) pixels from that band are averaged and the average is stored in p 829 // masked (or valid) pixels from that band are averaged and the average is stored in p
841 - for (unsigned i = 0; i < R[2]; i++){ 830 + for (unsigned i = 0; i < Z(); i++){
842 p[i] = 0; 831 p[i] = 0;
843 band_index(temp, i); //get the band image and store it in temp 832 band_index(temp, i); //get the band image and store it in temp
844 for (unsigned j = 0; j < XY; j++){ //loop through temp, averaging valid pixels 833 for (unsigned j = 0; j < XY; j++){ //loop through temp, averaging valid pixels
@@ -858,8 +847,8 @@ public: @@ -858,8 +847,8 @@ public:
858 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location 847 /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
859 bool co_matrix(T* co, T* avg, unsigned char *mask){ 848 bool co_matrix(T* co, T* avg, unsigned char *mask){
860 //memory allocation 849 //memory allocation
861 - unsigned long long xy = R[0] * R[1];  
862 - unsigned int B = R[2]; 850 + unsigned long long xy = X() * Y();
  851 + unsigned int B = Z();
863 T* bandi = (T*)malloc(sizeof(T) * xy); 852 T* bandi = (T*)malloc(sizeof(T) * xy);
864 T* bandj = (T*)malloc(sizeof(T) * xy); 853 T* bandj = (T*)malloc(sizeof(T) * xy);
865 854
@@ -909,11 +898,11 @@ public: @@ -909,11 +898,11 @@ public:
909 //get specified band and save 898 //get specified band and save
910 T* temp = (T*)malloc(L); 899 T* temp = (T*)malloc(L);
911 std::ofstream out(outfile.c_str(), std::ios::binary); 900 std::ofstream out(outfile.c_str(), std::ios::binary);
912 - unsigned long long jumpb = R[0] * (R[1] - lin) * sizeof(T); //jump pointer to the next band  
913 - unsigned long long jumpl = (R[0] - sam) * sizeof(T); //jump pointer to the next line 901 + unsigned long long jumpb = X() * (Y() - lin) * sizeof(T); //jump pointer to the next band
  902 + unsigned long long jumpl = (X() - sam) * sizeof(T); //jump pointer to the next line
914 //get start 903 //get start
915 - file.seekg((y0 * R[0] + x0) * sizeof(T), std::ios::beg);  
916 - for (unsigned i = 0; i < R[2]; i++) 904 + file.seekg((y0 * X() + x0) * sizeof(T), std::ios::beg);
  905 + for (unsigned i = 0; i < Z(); i++)
917 { 906 {
918 for (unsigned j = 0; j < lin; j++) 907 for (unsigned j = 0; j < lin; j++)
919 { 908 {
@@ -710,6 +710,41 @@ public: @@ -710,6 +710,41 @@ public:
710 return false; 710 return false;
711 } 711 }
712 712
  713 + bool spectrum(void* ptr, unsigned int x, unsigned int y){
  714 +
  715 + if(header.interleave == envi_header::BSQ){ //if the infile is bsq file
  716 + if(header.data_type ==envi_header::float32)
  717 + return ((bsq<float>*)file)->spectrum((float*)ptr, x, y);
  718 + else if (header.data_type == envi_header::float64)
  719 + return ((bsq<double>*)file)->spectrum((double*)ptr, x, y);
  720 + else{
  721 + std::cout << "ERROR: unidentified data type" << std::endl;
  722 + exit(1);
  723 + }
  724 + }
  725 + else if (header.interleave == envi_header::BIL){
  726 + if (header.data_type == envi_header::float32)
  727 + return ((bil<float>*)file)->spectrum((float*)ptr, x, y);
  728 + else if (header.data_type == envi_header::float64)
  729 + return ((bil<double>*)file)->spectrum((double*)ptr, x, y);
  730 + else{
  731 + std::cout << "ERROR: unidentified data type" << std::endl;
  732 + exit(1);
  733 + }
  734 + }
  735 + else if (header.interleave == envi_header::BIP){
  736 + if (header.data_type == envi_header::float32)
  737 + return ((bip<float>*)file)->spectrum((float*)ptr, x, y);
  738 + else if (header.data_type == envi_header::float64)
  739 + return ((bip<double>*)file)->spectrum((double*)ptr, x, y);
  740 + else{
  741 + std::cout << "ERROR: unidentified data type" << std::endl;
  742 + exit(1);
  743 + }
  744 + }
  745 + return false;
  746 + }
  747 +
713 /// Retrieve a single band (based on index) and stores it in pre-allocated memory. 748 /// Retrieve a single band (based on index) and stores it in pre-allocated memory.
714 749
715 /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. 750 /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size.
@@ -762,50 +797,6 @@ public: @@ -762,50 +797,6 @@ public:
762 return true; 797 return true;
763 } 798 }
764 799
765 - //p:start positon; N: number of pixels saved in X;  
766 - bool feature_matrix(void * X, unsigned char * mask, unsigned start, unsigned N)  
767 - {  
768 - //save pixels in X as floating numbers: float * p  
769 - float * p = (float*)malloc(header.bands * sizeof(float)); //save pixel information  
770 - unsigned pixels = header.samples * header.lines; //calculate pixel numbers in a band  
771 - unsigned count = 0; //for counting use  
772 - unsigned j = 0; //memory the pointer location in X  
773 -  
774 - //create two indices into the mask (mask index and pixel index)  
775 - unsigned mi = 0; //valid pixel index  
776 - unsigned pi = 0; //actual pixel in the mask  
777 -  
778 - //find the actual pixel index for the mask index "start"  
779 - while(mi < start){  
780 - if(mask[pi])  
781 - mi++;  
782 -  
783 - pi++;  
784 - }  
785 -  
786 - for(unsigned i = pi; i < pixels; i++){  
787 - if(mask[i] != 0){  
788 - pixel(p, i);  
789 - //copy p to X  
790 - for(unsigned k = 0; k < header.bands; k++){  
791 - ((float *)X)[j] = p[k];  
792 - j++;  
793 - }  
794 - count++;  
795 - if(count == N)  
796 - break;  
797 - }  
798 - else  
799 - continue;  
800 - }  
801 - if(count < N){  
802 - std::cout << "number of valid pixels in the mask : " << count <<"is less than N: "<< N;  
803 - exit(1);  
804 - }  
805 - free(p);  
806 - return true;  
807 - }  
808 -  
809 /// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum 800 /// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum
810 801
811 /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum 802 /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum