Commit 724ec3470fd1d9a8570e991a2bfa6e5a97e57487
1 parent
5f81932b
simplified the ENVI classes, moved more stuff into binary.h
Showing
5 changed files
with
495 additions
and
493 deletions
Show diff stats
stim/envi/bil.h
... | ... | @@ -25,6 +25,16 @@ protected: |
25 | 25 | |
26 | 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 | 38 | public: |
29 | 39 | |
30 | 40 | using binary<T>::open; |
... | ... | @@ -43,7 +53,7 @@ public: |
43 | 53 | |
44 | 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 | 62 | /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. |
53 | 63 | /// @param page <= B is the integer number of the band to be copied. |
54 | 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 | 71 | std::cout<<"ERROR: page out of range"<<std::endl; |
61 | 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 | 79 | file.seekg( jump, std::ios::cur); |
69 | 80 | } |
70 | 81 | |
... | ... | @@ -81,7 +92,7 @@ public: |
81 | 92 | if(w.size() == 0) |
82 | 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 | 96 | unsigned int S = XY * sizeof(T); //calculate the number of bytes of a band |
86 | 97 | |
87 | 98 | unsigned page=0; //bands around the wavelength |
... | ... | @@ -99,8 +110,8 @@ public: |
99 | 110 | { |
100 | 111 | page++; |
101 | 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 | 115 | return true; |
105 | 116 | } |
106 | 117 | } |
... | ... | @@ -127,12 +138,15 @@ public: |
127 | 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 | 151 | unsigned page=0; //samples around the wavelength |
138 | 152 | T * p1; |
... | ... | @@ -151,7 +165,7 @@ public: |
151 | 165 | page++; |
152 | 166 | //if wavelength is larger than the last wavelength in header file |
153 | 167 | if (page == B) { |
154 | - memcpy(p, c + (B - 1) * X, L); | |
168 | + memcpy(p, c + (B - 1) * X(), L); | |
155 | 169 | return true; |
156 | 170 | } |
157 | 171 | } |
... | ... | @@ -160,16 +174,16 @@ public: |
160 | 174 | p1=(T*)malloc( L ); //memory allocation |
161 | 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 | 181 | double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); |
168 | 182 | p[i] = (p2[i] - p1[i]) * r + p1[i]; |
169 | 183 | } |
170 | 184 | } |
171 | 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 | 188 | return true; |
175 | 189 | } |
... | ... | @@ -180,24 +194,7 @@ public: |
180 | 194 | /// @param x is the x-coordinate (dimension 1) of the spectrum. |
181 | 195 | /// @param y is the y-coordinate (dimension 2) of the spectrum. |
182 | 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 | 200 | /// Retrieve a single pixel and stores it in pre-allocated memory. |
... | ... | @@ -207,26 +204,16 @@ public: |
207 | 204 | bool pixel(T * p, unsigned n){ |
208 | 205 | |
209 | 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 | 210 | //get the pixel |
214 | 211 | return spectrum(p, x, y); |
215 | 212 | } |
216 | 213 | |
217 | 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 | 229 | std::string headername = outname + ".hdr"; //the header file name |
243 | 230 | |
244 | 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 | 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 | 236 | T* c; //pointer to the current Y slice |
251 | 237 | c = (T*)malloc(L); //memory allocation |
252 | 238 | |
253 | 239 | T* a; //pointer to the two YZ lines surrounding the current YZ line |
254 | 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 | 246 | double ai, bi; //stores the two baseline points wavelength surrounding the current band |
... | ... | @@ -267,10 +253,10 @@ public: |
267 | 253 | } |
268 | 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 | 258 | //get the current y slice |
273 | - getY(c, k); | |
259 | + read_plane_y(c, k); | |
274 | 260 | |
275 | 261 | //initialize lownum, highnum, low, high |
276 | 262 | ai = w[0]; |
... | ... | @@ -279,16 +265,16 @@ public: |
279 | 265 | //set the baseline point at band 0 to 0 |
280 | 266 | if(wls[0] != w[0]){ |
281 | 267 | bi = wls[control]; |
282 | - memset(a, (char)0, X * sizeof(T) ); | |
268 | + memset(a, (char)0, X() * sizeof(T) ); | |
283 | 269 | } |
284 | 270 | //else get the low band |
285 | 271 | else{ |
286 | 272 | control++; |
287 | - getYZ(a, c, ai); | |
273 | + read_x_from_xz(a, c, ai); | |
288 | 274 | bi = wls[control]; |
289 | 275 | } |
290 | 276 | //get the high band |
291 | - getYZ(b, c, bi); | |
277 | + read_x_from_xz(b, c, bi); | |
292 | 278 | |
293 | 279 | //correct every YZ line |
294 | 280 | |
... | ... | @@ -305,7 +291,7 @@ public: |
305 | 291 | |
306 | 292 | ai = bi; |
307 | 293 | bi = wls[control]; |
308 | - getYZ(b, c, bi); | |
294 | + read_x_from_xz(b, c, bi); | |
309 | 295 | |
310 | 296 | } |
311 | 297 | //if the last BL point on the last band of the file? |
... | ... | @@ -313,7 +299,7 @@ public: |
313 | 299 | |
314 | 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 | 304 | ai = bi; |
319 | 305 | bi = w[B - 1]; |
... | ... | @@ -322,9 +308,9 @@ public: |
322 | 308 | |
323 | 309 | ci = w[cii]; |
324 | 310 | |
325 | - unsigned jump = cii * X; | |
311 | + unsigned jump = cii * X(); | |
326 | 312 | //perform the baseline correction |
327 | - for(unsigned i=0; i < X; i++) | |
313 | + for(unsigned i=0; i < X(); i++) | |
328 | 314 | { |
329 | 315 | double r = (double) (ci - ai) / (double) (bi - ai); |
330 | 316 | c[i + jump] =(T) ( c[i + jump] - (b[i] - a[i]) * r - a[i] ); |
... | ... | @@ -349,11 +335,9 @@ public: |
349 | 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 | 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 | 341 | unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band |
358 | 342 | unsigned int L = ZX * sizeof(T); |
359 | 343 | |
... | ... | @@ -368,17 +352,17 @@ public: |
368 | 352 | |
369 | 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 | 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 | 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 | 368 | target.write(reinterpret_cast<const char*>(c), L); //write normalized data into destination |
... | ... | @@ -395,7 +379,7 @@ public: |
395 | 379 | /// @param outname is the name of the output BSQ file to be saved to disk. |
396 | 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 | 384 | std::ofstream target(outname.c_str(), std::ios::binary); |
401 | 385 | std::string headername = outname + ".hdr"; |
... | ... | @@ -403,7 +387,7 @@ public: |
403 | 387 | T * p; //pointer to the current band |
404 | 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 | 392 | band_index(p, i); |
409 | 393 | target.write(reinterpret_cast<const char*>(p), S); //write a band data into target file |
... | ... | @@ -419,7 +403,7 @@ public: |
419 | 403 | /// @param outname is the name of the output BIP file to be saved to disk. |
420 | 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 | 408 | std::ofstream target(outname.c_str(), std::ios::binary); |
425 | 409 | std::string headername = outname + ".hdr"; |
... | ... | @@ -429,14 +413,14 @@ public: |
429 | 413 | T * q; //pointer to the current ZX slice for bip file |
430 | 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 | 426 | target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file |
... | ... | @@ -460,7 +444,7 @@ public: |
460 | 444 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. |
461 | 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 | 448 | band(result, wavelength); //get band |
465 | 449 | |
466 | 450 | //perform the baseline correction |
... | ... | @@ -481,7 +465,7 @@ public: |
481 | 465 | |
482 | 466 | T* lp; |
483 | 467 | T* rp; |
484 | - unsigned XY = R[0] * R[1]; | |
468 | + unsigned XY = X() * Y(); | |
485 | 469 | unsigned S = XY * sizeof(T); |
486 | 470 | lp = (T*) malloc(S); //memory allocation |
487 | 471 | rp = (T*) malloc(S); |
... | ... | @@ -512,7 +496,7 @@ public: |
512 | 496 | T* cur; //current band 1 |
513 | 497 | T* cur2; //current band 2 |
514 | 498 | |
515 | - unsigned XY = R[0] * R[1]; | |
499 | + unsigned XY = X() * Y(); | |
516 | 500 | unsigned S = XY * sizeof(T); |
517 | 501 | |
518 | 502 | lp = (T*) malloc(S); //memory allocation |
... | ... | @@ -593,14 +577,14 @@ public: |
593 | 577 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size |
594 | 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 | 583 | //get the two peak band |
600 | 584 | height(lb1, rb1, pos1, p1); |
601 | 585 | height(lb2, rb2, pos2, p2); |
602 | 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 | 588 | if(p1[i] == 0 && p2[i] ==0) |
605 | 589 | result[i] = 1; |
606 | 590 | else |
... | ... | @@ -624,14 +608,14 @@ public: |
624 | 608 | bool pa_to_ph(double lb1, double rb1, double lab1, double rab1, |
625 | 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 | 614 | //get the area and the peak band |
631 | 615 | area(lb1, rb1, lab1, rab1, p1); |
632 | 616 | height(lb2, rb2, pos, p2); |
633 | 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 | 619 | if(p1[i] == 0 && p2[i] ==0) |
636 | 620 | result[i] = 1; |
637 | 621 | else |
... | ... | @@ -657,14 +641,14 @@ public: |
657 | 641 | bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, |
658 | 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 | 647 | //get the area and the peak band |
664 | 648 | area(lb1, rb1, lab1, rab1, p1); |
665 | 649 | area(lb2, rb2, lab2, rab2, p2); |
666 | 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 | 652 | if(p1[i] == 0 && p2[i] ==0) |
669 | 653 | result[i] = 1; |
670 | 654 | else |
... | ... | @@ -689,7 +673,7 @@ public: |
689 | 673 | T* cur; //current band 1 |
690 | 674 | T* cur2; //current band 2 |
691 | 675 | |
692 | - unsigned XY = R[0] * R[1]; | |
676 | + unsigned XY = X() * Y(); | |
693 | 677 | unsigned S = XY * sizeof(T); |
694 | 678 | |
695 | 679 | lp = (T*) malloc(S); //memory allocation |
... | ... | @@ -765,14 +749,14 @@ public: |
765 | 749 | /// @param rab is the label for the end of the peak |
766 | 750 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size |
767 | 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 | 755 | //get the area and the peak band |
772 | 756 | x_area(lb, rb, lab, rab, p1); |
773 | 757 | area(lb, rb, lab, rab, p2); |
774 | 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 | 760 | if(p1[i] == 0 && p2[i] ==0) |
777 | 761 | result[i] = 1; |
778 | 762 | else |
... | ... | @@ -793,10 +777,10 @@ public: |
793 | 777 | /// @param p is a pointer to a pre-allocated array at least X * Y in size |
794 | 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 | 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 | 784 | if (temp[i] < threshold) |
801 | 785 | p[i] = 0; |
802 | 786 | else |
... | ... | @@ -816,20 +800,20 @@ public: |
816 | 800 | std::ofstream target(outfile.c_str(), std::ios::binary); |
817 | 801 | |
818 | 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 | 804 | unsigned L = XZ * sizeof(T); //calculate the size of the page (in bytes) |
821 | 805 | |
822 | 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 | 817 | else |
834 | 818 | continue; |
835 | 819 | } |
... | ... | @@ -843,30 +827,29 @@ public: |
843 | 827 | |
844 | 828 | ///Saves to disk only those spectra corresponding to mask values != 0 |
845 | 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 | 831 | std::ofstream target(outfile.c_str(), std::ios::binary); |
848 | 832 | |
849 | 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 | 835 | unsigned L = XZ * sizeof(T); //calculate the size of the page (in bytes) |
852 | 836 | T * temp = (T*)malloc(L); //allocate memory for a temporary page |
853 | 837 | |
854 | 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 | 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 | 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 | 866 | |
884 | 867 | /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages. |
885 | 868 | bool band_avg(T* p){ |
886 | - unsigned long long XZ = R[0] * R[2]; | |
869 | + unsigned long long XZ = X() * Z(); | |
887 | 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 | 874 | getY(temp, i); |
892 | 875 | //initialize x-line |
893 | - for (unsigned j = 0; j < R[0]; j++){ | |
876 | + for (unsigned j = 0; j < X(); j++){ | |
894 | 877 | line[j] = 0; |
895 | 878 | } |
896 | 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 | 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 | 890 | free(temp); |
... | ... | @@ -913,10 +896,10 @@ public: |
913 | 896 | /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum |
914 | 897 | /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location |
915 | 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 | 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 | 903 | p[j] = 0; |
921 | 904 | } |
922 | 905 | //calculate vaild number in a band |
... | ... | @@ -926,13 +909,13 @@ public: |
926 | 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 | 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 | 931 | /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location |
949 | 932 | bool co_matrix(T* co, T* avg, unsigned char *mask){ |
950 | 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 | 936 | T* temp = (T*)malloc(sizeof(T) * B); |
954 | 937 | //count vaild pixels in a band |
955 | 938 | unsigned count = 0; |
... | ... | @@ -999,16 +982,16 @@ public: |
999 | 982 | //calculate the new number of samples and lines |
1000 | 983 | unsigned long long sam = x1 - x0; //samples |
1001 | 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 | 986 | //get specified band and save |
1004 | 987 | T* temp = (T*)malloc(L); |
1005 | 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 | 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 | 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 | 996 | file.read((char *)(temp + j * sam), sizeof(T) * sam); |
1014 | 997 | file.seekg(jumpb, std::ios::cur); //go to the next band | ... | ... |
stim/envi/binary.h
... | ... | @@ -81,6 +81,8 @@ protected: |
81 | 81 | return false; |
82 | 82 | } |
83 | 83 | |
84 | + | |
85 | + | |
84 | 86 | public: |
85 | 87 | |
86 | 88 | /// Open a binary file for streaming. |
... | ... | @@ -159,8 +161,141 @@ public: |
159 | 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 | 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 | 300 | //reset the file pointer back to the beginning of the file |
166 | 301 | file.seekg(0, std::ios::beg); |
... | ... | @@ -184,10 +319,10 @@ public: |
184 | 319 | |
185 | 320 | return true; |
186 | 321 | |
187 | - } | |
322 | + }*/ | |
188 | 323 | |
189 | 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 | 327 | unsigned int i; |
193 | 328 | |
... | ... | @@ -204,7 +339,7 @@ public: |
204 | 339 | } |
205 | 340 | |
206 | 341 | return true; |
207 | - } | |
342 | + }*/ | |
208 | 343 | |
209 | 344 | |
210 | 345 | }; | ... | ... |
stim/envi/bip.h
... | ... | @@ -27,11 +27,22 @@ protected: |
27 | 27 | std::vector<double> w; //band wavelength |
28 | 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 | 40 | public: |
31 | 41 | |
32 | 42 | using binary<T>::open; |
33 | 43 | using binary<T>::file; |
34 | 44 | using binary<T>::R; |
45 | + using binary<T>::read_line_12; | |
35 | 46 | |
36 | 47 | /// Open a data file for reading using the class interface. |
37 | 48 | |
... | ... | @@ -48,7 +59,7 @@ public: |
48 | 59 | //copy the offset to the structure |
49 | 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 | 68 | /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. |
58 | 69 | /// @param page <= B is the integer number of the band to be copied. |
59 | 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 | 74 | /// Retrieve a single band (by numerical label) and stores it in pre-allocated memory. |
... | ... | @@ -84,7 +81,7 @@ public: |
84 | 81 | if(w.size() == 0) |
85 | 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 | 86 | unsigned page=0; //bands around the wavelength |
90 | 87 | |
... | ... | @@ -101,8 +98,8 @@ public: |
101 | 98 | { |
102 | 99 | page++; |
103 | 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 | 103 | return true; |
107 | 104 | } |
108 | 105 | } |
... | ... | @@ -128,11 +125,24 @@ public: |
128 | 125 | |
129 | 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 | 147 | unsigned page=0; //samples around the wavelength |
138 | 148 | |
... | ... | @@ -141,7 +151,7 @@ public: |
141 | 151 | |
142 | 152 | //if wavelength is smaller than the first one in header file |
143 | 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 | 156 | p[j] = c[j * B]; |
147 | 157 | } |
... | ... | @@ -153,7 +163,7 @@ public: |
153 | 163 | page++; |
154 | 164 | //if wavelength is larger than the last wavelength in header file |
155 | 165 | if (page == B) { |
156 | - for(unsigned j = 0; j < R[0]; j++) | |
166 | + for(unsigned j = 0; j < X(); j++) | |
157 | 167 | { |
158 | 168 | p[j] = c[(j + 1) * B - 1]; |
159 | 169 | } |
... | ... | @@ -164,20 +174,20 @@ public: |
164 | 174 | { |
165 | 175 | T * p1; |
166 | 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 | 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 | 182 | p1[j] = c[j * B + page - 1]; |
173 | 183 | } |
174 | 184 | //band_index(p2, page ); |
175 | - for(unsigned j = 0; j < X; j++) | |
185 | + for(unsigned j = 0; j < X(); j++) | |
176 | 186 | { |
177 | 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 | 191 | double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); |
182 | 192 | p[i] = (p2[i] - p1[i]) * r + p1[i]; |
183 | 193 | } |
... | ... | @@ -187,7 +197,7 @@ public: |
187 | 197 | else //if the wavelength is equal to a wavelength in header file |
188 | 198 | { |
189 | 199 | //band_index(p, page); |
190 | - for(unsigned j = 0; j < X; j++) | |
200 | + for(unsigned j = 0; j < X(); j++) | |
191 | 201 | { |
192 | 202 | p[j] = c[j * B + page]; |
193 | 203 | } |
... | ... | @@ -195,98 +205,6 @@ public: |
195 | 205 | |
196 | 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 | 209 | /// Retrieve a single pixel and stores it in pre-allocated memory. |
292 | 210 | |
... | ... | @@ -294,30 +212,20 @@ public: |
294 | 212 | /// @param n is an integer index to the pixel using linear array indexing. |
295 | 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 | 216 | if ( n >= bandnum){ //make sure the pixel number is right |
299 | 217 | std::cout<<"ERROR: sample or line out of range"<<std::endl; |
300 | 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 | 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 | 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 | 240 | std::string headername = outname + ".hdr"; //the header file name |
333 | 241 | |
334 | 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 | 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 | 247 | T* c; //pointer to the current Y slice |
341 | 248 | c = (T*)malloc(L); //memory allocation |
... | ... | @@ -343,8 +250,8 @@ public: |
343 | 250 | T* a; //pointer to the two YZ lines surrounding the current YZ line |
344 | 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 | 257 | double ai, bi; //stores the two baseline points wavelength surrounding the current band |
... | ... | @@ -357,10 +264,10 @@ public: |
357 | 264 | } |
358 | 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 | 269 | //get the current y slice |
363 | - getY(c, k); | |
270 | + read_plane_y(c, k); | |
364 | 271 | |
365 | 272 | //initialize lownum, highnum, low, high |
366 | 273 | control=0; |
... | ... | @@ -369,16 +276,16 @@ public: |
369 | 276 | //set the baseline point at band 0 to 0 |
370 | 277 | if(wls[0] != w[0]){ |
371 | 278 | bi = wls[control]; |
372 | - memset(a, (char)0, X * sizeof(T) ); | |
279 | + memset(a, (char)0, X() * sizeof(T) ); | |
373 | 280 | } |
374 | 281 | //else get the low band |
375 | 282 | else{ |
376 | 283 | control++; |
377 | - getYZ(a, c, ai); | |
284 | + read_x_from_xz(a, c, ai); | |
378 | 285 | bi = wls[control]; |
379 | 286 | } |
380 | 287 | //get the high band |
381 | - getYZ(b, c, bi); | |
288 | + read_x_from_xz(b, c, bi); | |
382 | 289 | |
383 | 290 | //correct every YZ line |
384 | 291 | |
... | ... | @@ -394,7 +301,7 @@ public: |
394 | 301 | |
395 | 302 | ai = bi; |
396 | 303 | bi = wls[control]; |
397 | - getYZ(b, c, bi); | |
304 | + read_x_from_xz(b, c, bi); | |
398 | 305 | |
399 | 306 | } |
400 | 307 | //if the last BL point on the last band of the file? |
... | ... | @@ -402,7 +309,7 @@ public: |
402 | 309 | |
403 | 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 | 314 | ai = bi; |
408 | 315 | bi = w[B - 1]; |
... | ... | @@ -412,7 +319,7 @@ public: |
412 | 319 | ci = w[cii]; |
413 | 320 | |
414 | 321 | //perform the baseline correction |
415 | - for(unsigned i=0; i < X; i++) | |
322 | + for(unsigned i=0; i < X(); i++) | |
416 | 323 | { |
417 | 324 | double r = (double) (ci - ai) / (double) (bi - ai); |
418 | 325 | c[i * B + cii] =(T) ( c[i * B + cii] - (b[i] - a[i]) * r - a[i] ); |
... | ... | @@ -439,11 +346,9 @@ public: |
439 | 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 | 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 | 352 | unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band |
448 | 353 | unsigned int L = ZX * sizeof(T); |
449 | 354 | |
... | ... | @@ -458,11 +363,11 @@ public: |
458 | 363 | |
459 | 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 | 372 | unsigned iB = i * B; |
468 | 373 | for(unsigned m = 0; m < B; m++) |
... | ... | @@ -494,7 +399,7 @@ public: |
494 | 399 | bil(temp); |
495 | 400 | |
496 | 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 | 403 | std::cout<<"ERROR: unable to open input file"<<std::endl; |
499 | 404 | exit(1); |
500 | 405 | } |
... | ... | @@ -511,7 +416,7 @@ public: |
511 | 416 | /// @param outname is the name of the output BIL file to be saved to disk. |
512 | 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 | 421 | std::ofstream target(outname.c_str(), std::ios::binary); |
517 | 422 | std::string headername = outname + ".hdr"; |
... | ... | @@ -521,14 +426,14 @@ public: |
521 | 426 | T * q; //pointer to the current XZ slice for bil file |
522 | 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 | 438 | target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file |
534 | 439 | } |
... | ... | @@ -550,7 +455,7 @@ public: |
550 | 455 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. |
551 | 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 | 459 | band(result, wavelength); //get band |
555 | 460 | |
556 | 461 | //perform the baseline correction |
... | ... | @@ -571,7 +476,7 @@ public: |
571 | 476 | |
572 | 477 | T* lp; |
573 | 478 | T* rp; |
574 | - unsigned XY = R[0] * R[1]; | |
479 | + unsigned XY = X() * Y(); | |
575 | 480 | unsigned S = XY * sizeof(T); |
576 | 481 | lp = (T*) malloc(S); //memory allocation |
577 | 482 | rp = (T*) malloc(S); |
... | ... | @@ -601,7 +506,7 @@ public: |
601 | 506 | T* cur; //current band 1 |
602 | 507 | T* cur2; //current band 2 |
603 | 508 | |
604 | - unsigned XY = R[0] * R[1]; | |
509 | + unsigned XY = X() * Y(); | |
605 | 510 | unsigned S = XY * sizeof(T); |
606 | 511 | |
607 | 512 | lp = (T*) malloc(S); //memory allocation |
... | ... | @@ -682,14 +587,14 @@ public: |
682 | 587 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size |
683 | 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 | 593 | //get the two peak band |
689 | 594 | height(lb1, rb1, pos1, p1); |
690 | 595 | height(lb2, rb2, pos2, p2); |
691 | 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 | 598 | if(p1[i] == 0 && p2[i] ==0) |
694 | 599 | result[i] = 1; |
695 | 600 | else |
... | ... | @@ -713,14 +618,14 @@ public: |
713 | 618 | bool pa_to_ph(double lb1, double rb1, double lab1, double rab1, |
714 | 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 | 624 | //get the area and the peak band |
720 | 625 | area(lb1, rb1, lab1, rab1, p1); |
721 | 626 | height(lb2, rb2, pos, p2); |
722 | 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 | 629 | if(p1[i] == 0 && p2[i] ==0) |
725 | 630 | result[i] = 1; |
726 | 631 | else |
... | ... | @@ -746,14 +651,14 @@ public: |
746 | 651 | bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, |
747 | 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 | 657 | //get the area and the peak band |
753 | 658 | area(lb1, rb1, lab1, rab1, p1); |
754 | 659 | area(lb2, rb2, lab2, rab2, p2); |
755 | 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 | 662 | if(p1[i] == 0 && p2[i] ==0) |
758 | 663 | result[i] = 1; |
759 | 664 | else |
... | ... | @@ -778,7 +683,7 @@ public: |
778 | 683 | T* cur; //current band 1 |
779 | 684 | T* cur2; //current band 2 |
780 | 685 | |
781 | - unsigned XY = R[0] * R[1]; | |
686 | + unsigned XY = X() * Y(); | |
782 | 687 | unsigned S = XY * sizeof(T); |
783 | 688 | |
784 | 689 | lp = (T*) malloc(S); //memory allocation |
... | ... | @@ -854,14 +759,14 @@ public: |
854 | 759 | /// @param rab is the label for the end of the peak |
855 | 760 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size |
856 | 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 | 765 | //get the area and the peak band |
861 | 766 | x_area(lb, rb, lab, rab, p1); |
862 | 767 | area(lb, rb, lab, rab, p2); |
863 | 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 | 770 | if(p1[i] == 0 && p2[i] ==0) |
866 | 771 | result[i] = 1; |
867 | 772 | else |
... | ... | @@ -882,10 +787,10 @@ public: |
882 | 787 | /// @param p is a pointer to a pre-allocated array at least X * Y in size |
883 | 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 | 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 | 794 | if (temp[i] < threshold) |
890 | 795 | p[i] = 0; |
891 | 796 | else |
... | ... | @@ -905,20 +810,20 @@ public: |
905 | 810 | |
906 | 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 | 814 | unsigned L = ZX * sizeof(T); //calculate the number of bytes in a page |
910 | 815 | |
911 | 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 | 827 | else //otherwise just continue |
923 | 828 | continue; |
924 | 829 | } |
... | ... | @@ -933,30 +838,29 @@ public: |
933 | 838 | |
934 | 839 | ///Saves to disk only those spectra corresponding to mask values != 0 |
935 | 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 | 842 | std::ofstream target(outfile.c_str(), std::ios::binary); |
938 | 843 | |
939 | 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 | 846 | unsigned L = ZX * sizeof(T); //calculate the size of the page (in bytes) |
942 | 847 | T * temp = (T*)malloc(L); //allocate memory for a temporary page |
943 | 848 | |
944 | 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 | 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 | 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 | 877 | |
974 | 878 | /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages. |
975 | 879 | bool band_avg(T* p){ |
976 | - unsigned long long XY = R[0] * R[1]; | |
880 | + unsigned long long XY = X() * Y(); | |
977 | 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 | 883 | T sum; |
980 | 884 | for (unsigned i = 0; i < XY; i++){ |
981 | 885 | pixel(temp, i); |
982 | 886 | //calculate the sum value of every value |
983 | 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 | 891 | p[i] = sum; |
988 | 892 | } |
... | ... | @@ -995,10 +899,10 @@ public: |
995 | 899 | /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum |
996 | 900 | /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location |
997 | 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 | 904 | //Iinitialize |
1001 | - for (unsigned j = 0; j < R[2]; j++){ | |
905 | + for (unsigned j = 0; j < Z(); j++){ | |
1002 | 906 | p[j] = 0; |
1003 | 907 | } |
1004 | 908 | //calculate vaild number in a band |
... | ... | @@ -1012,7 +916,7 @@ public: |
1012 | 916 | for (unsigned i = 0; i < XY; i++){ |
1013 | 917 | if (mask[i] != 0){ |
1014 | 918 | pixel(temp, i); |
1015 | - for (unsigned j = 0; j < R[2]; j++){ | |
919 | + for (unsigned j = 0; j < Z(); j++){ | |
1016 | 920 | p[j] += temp[j] / (T)count; |
1017 | 921 | } |
1018 | 922 | } |
... | ... | @@ -1028,8 +932,8 @@ public: |
1028 | 932 | /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location |
1029 | 933 | bool co_matrix(T* co, T* avg, unsigned char *mask){ |
1030 | 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 | 937 | T* temp = (T*)malloc(sizeof(T) * B); |
1034 | 938 | //count vaild pixels in a band |
1035 | 939 | unsigned count = 0; |
... | ... | @@ -1079,17 +983,17 @@ public: |
1079 | 983 | //calculate the new number of samples and lines |
1080 | 984 | unsigned long long sam = x1 - x0; //samples |
1081 | 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 | 987 | //get specified band and save |
1084 | 988 | T* temp = (T*)malloc(L); |
1085 | 989 | std::ofstream out(outfile.c_str(), std::ios::binary); |
1086 | 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 | 992 | for (unsigned i = 0; i < lin; i++) |
1089 | 993 | { |
1090 | 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 | 997 | out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file |
1094 | 998 | } |
1095 | 999 | } | ... | ... |
stim/envi/bsq.h
... | ... | @@ -30,12 +30,26 @@ protected: |
30 | 30 | std::vector<double> w; //band wavelengths |
31 | 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 | 45 | public: |
34 | 46 | |
35 | 47 | using binary<T>::open; |
36 | 48 | using binary<T>::file; |
49 | + using binary<T>::read_line_01; | |
50 | + using binary<T>::read_plane_2; | |
37 | 51 | //using binary<T>::getSlice; |
38 | - using binary<T>::R; | |
52 | + | |
39 | 53 | |
40 | 54 | /// Open a data file for reading using the class interface. |
41 | 55 | |
... | ... | @@ -61,17 +75,7 @@ public: |
61 | 75 | /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. |
62 | 76 | /// @param page <= B is the integer number of the band to be copied. |
63 | 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 | 81 | /// Retrieve a single band (by numerical label) and stores it in pre-allocated memory. |
... | ... | @@ -84,7 +88,7 @@ public: |
84 | 88 | if(w.size() == 0) |
85 | 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 | 92 | unsigned page = 0; |
89 | 93 | |
90 | 94 | |
... | ... | @@ -101,8 +105,8 @@ public: |
101 | 105 | page++; |
102 | 106 | //if wavelength is larger than the last wavelength in the header file |
103 | 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 | 110 | return true; |
107 | 111 | } |
108 | 112 | } |
... | ... | @@ -131,28 +135,13 @@ public: |
131 | 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 | 140 | /// @param p is a pointer to pre-allocated memory at least B * sizeof(T) in size. |
137 | 141 | /// @param x is the x-coordinate (dimension 1) of the spectrum. |
138 | 142 | /// @param y is the y-coordinate (dimension 2) of the spectrum. |
139 | 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 | 147 | /// Retrieve a single pixel and stores it in pre-allocated memory. |
... | ... | @@ -161,14 +150,14 @@ public: |
161 | 150 | /// @param n is an integer index to the pixel using linear array indexing. |
162 | 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 | 154 | if ( n >= bandnum){ //make sure the pixel number is right |
166 | 155 | std::cout<<"ERROR: sample or line out of range"<<std::endl; |
167 | 156 | return false; |
168 | 157 | } |
169 | 158 | |
170 | 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 | 162 | file.read((char *)(p + i), sizeof(T)); |
174 | 163 | file.seekg((bandnum - 1) * sizeof(T), std::ios::cur); //go to the next band |
... | ... | @@ -189,8 +178,8 @@ public: |
189 | 178 | std::string headername = outname + ".hdr"; //the header file name |
190 | 179 | |
191 | 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 | 183 | unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band |
195 | 184 | |
196 | 185 | double ai, bi; //stores the two baseline points wavelength surrounding the current band |
... | ... | @@ -289,8 +278,8 @@ public: |
289 | 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 | 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 | 283 | unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band |
295 | 284 | |
296 | 285 | std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file |
... | ... | @@ -336,7 +325,7 @@ public: |
336 | 325 | bil(temp); |
337 | 326 | |
338 | 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 | 329 | std::cout<<"ERROR: unable to open input file"<<std::endl; |
341 | 330 | exit(1); |
342 | 331 | } |
... | ... | @@ -354,8 +343,8 @@ public: |
354 | 343 | bool bil(std::string outname) |
355 | 344 | { |
356 | 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 | 349 | std::ofstream target(outname.c_str(), std::ios::binary); |
361 | 350 | std::string headername = outname + ".hdr"; |
... | ... | @@ -363,12 +352,12 @@ public: |
363 | 352 | T * p; //pointer to the current spectrum |
364 | 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 | 361 | file.seekg(jump, std::ios::cur); //go to the next band |
373 | 362 | } |
374 | 363 | target.write(reinterpret_cast<const char*>(p), L); //write XZ slice data into target file |
... | ... | @@ -391,7 +380,7 @@ public: |
391 | 380 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. |
392 | 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 | 384 | band(result, wavelength); //get band |
396 | 385 | |
397 | 386 | //perform the baseline correction |
... | ... | @@ -412,7 +401,7 @@ public: |
412 | 401 | |
413 | 402 | T* lp; |
414 | 403 | T* rp; |
415 | - unsigned XY = R[0] * R[1]; | |
404 | + unsigned XY = X() * Y(); | |
416 | 405 | unsigned S = XY * sizeof(T); |
417 | 406 | lp = (T*) malloc(S); //memory allocation |
418 | 407 | rp = (T*) malloc(S); |
... | ... | @@ -442,7 +431,7 @@ public: |
442 | 431 | T* cur; //current band 1 |
443 | 432 | T* cur2; //current band 2 |
444 | 433 | |
445 | - unsigned XY = R[0] * R[1]; | |
434 | + unsigned XY = X() * Y(); | |
446 | 435 | unsigned S = XY * sizeof(T); |
447 | 436 | |
448 | 437 | lp = (T*) malloc(S); //memory allocation |
... | ... | @@ -523,14 +512,14 @@ public: |
523 | 512 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size |
524 | 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 | 518 | //get the two peak band |
530 | 519 | height(lb1, rb1, pos1, p1); |
531 | 520 | height(lb2, rb2, pos2, p2); |
532 | 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 | 523 | if(p1[i] == 0 && p2[i] ==0) |
535 | 524 | result[i] = 1; |
536 | 525 | else |
... | ... | @@ -554,14 +543,14 @@ public: |
554 | 543 | bool pa_to_ph(double lb1, double rb1, double lab1, double rab1, |
555 | 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 | 549 | //get the area and the peak band |
561 | 550 | area(lb1, rb1, lab1, rab1, p1); |
562 | 551 | height(lb2, rb2, pos, p2); |
563 | 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 | 554 | if(p1[i] == 0 && p2[i] ==0) |
566 | 555 | result[i] = 1; |
567 | 556 | else |
... | ... | @@ -587,14 +576,14 @@ public: |
587 | 576 | bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, |
588 | 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 | 582 | //get the area and the peak band |
594 | 583 | area(lb1, rb1, lab1, rab1, p1); |
595 | 584 | area(lb2, rb2, lab2, rab2, p2); |
596 | 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 | 587 | if(p1[i] == 0 && p2[i] ==0) |
599 | 588 | result[i] = 1; |
600 | 589 | else |
... | ... | @@ -619,7 +608,7 @@ public: |
619 | 608 | T* cur; //current band 1 |
620 | 609 | T* cur2; //current band 2 |
621 | 610 | |
622 | - unsigned XY = R[0] * R[1]; | |
611 | + unsigned XY = X() * Y(); | |
623 | 612 | unsigned S = XY * sizeof(T); |
624 | 613 | |
625 | 614 | lp = (T*) malloc(S); //memory allocation |
... | ... | @@ -695,14 +684,14 @@ public: |
695 | 684 | /// @param rab is the label for the end of the peak |
696 | 685 | /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size |
697 | 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 | 690 | //get the area and the peak band |
702 | 691 | x_area(lb, rb, lab, rab, p1); |
703 | 692 | area(lb, rb, lab, rab, p2); |
704 | 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 | 695 | if(p1[i] == 0 && p2[i] ==0) |
707 | 696 | result[i] = 1; |
708 | 697 | else |
... | ... | @@ -723,10 +712,10 @@ public: |
723 | 712 | /// @param p is a pointer to a pre-allocated array at least X * Y in size |
724 | 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 | 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 | 719 | if (temp[i] < threshold) |
731 | 720 | p[i] = 0; |
732 | 721 | else |
... | ... | @@ -746,12 +735,12 @@ public: |
746 | 735 | |
747 | 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 | 739 | unsigned L = XY * sizeof(T); |
751 | 740 | |
752 | 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 | 745 | band_index(temp, i); //get the specified band (by index) |
757 | 746 | for ( unsigned j = 0; j < XY; j++) // for each pixel |
... | ... | @@ -774,13 +763,13 @@ public: |
774 | 763 | bool sift_mask(std::string outfile, unsigned char* p){ |
775 | 764 | std::ofstream target(outfile.c_str(), std::ios::binary); |
776 | 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 | 767 | unsigned L = XY * sizeof(T); //size of XY pixels |
779 | 768 | |
780 | 769 | T * temp = (T*)malloc(L); //allocate memory for a band |
781 | 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 | 774 | band_index(temp, i); //get the specified band (XY sheet by index) |
786 | 775 | for (unsigned j = 0; j < XY; j++) // for each pixel |
... | ... | @@ -804,18 +793,18 @@ public: |
804 | 793 | |
805 | 794 | /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages. |
806 | 795 | bool band_avg(T* p){ |
807 | - unsigned long long XY = R[0] * R[1]; | |
796 | + unsigned long long XY = X() * Y(); | |
808 | 797 | T* temp = (T*)malloc(sizeof(T) * XY); |
809 | 798 | //initialize p |
810 | 799 | band_index(p, 0); |
811 | 800 | for (unsigned j = 0; j < XY; j++){ |
812 | - p[j] /= (T)R[2]; | |
801 | + p[j] /= (T)Z(); | |
813 | 802 | } |
814 | 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 | 805 | band_index(temp, i); |
817 | 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 | 810 | free(temp); |
... | ... | @@ -827,7 +816,7 @@ public: |
827 | 816 | /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum |
828 | 817 | /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location |
829 | 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 | 820 | unsigned count = 0; //count will store the number of masked pixels |
832 | 821 | T* temp = (T*)malloc(sizeof(T) * XY); |
833 | 822 | //calculate this loop counts the number of true pixels in the mask |
... | ... | @@ -836,9 +825,9 @@ public: |
836 | 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 | 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 | 831 | p[i] = 0; |
843 | 832 | band_index(temp, i); //get the band image and store it in temp |
844 | 833 | for (unsigned j = 0; j < XY; j++){ //loop through temp, averaging valid pixels |
... | ... | @@ -858,8 +847,8 @@ public: |
858 | 847 | /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location |
859 | 848 | bool co_matrix(T* co, T* avg, unsigned char *mask){ |
860 | 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 | 852 | T* bandi = (T*)malloc(sizeof(T) * xy); |
864 | 853 | T* bandj = (T*)malloc(sizeof(T) * xy); |
865 | 854 | |
... | ... | @@ -909,11 +898,11 @@ public: |
909 | 898 | //get specified band and save |
910 | 899 | T* temp = (T*)malloc(L); |
911 | 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 | 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 | 907 | for (unsigned j = 0; j < lin; j++) |
919 | 908 | { | ... | ... |
stim/envi/envi.h
... | ... | @@ -710,6 +710,41 @@ public: |
710 | 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 | 748 | /// Retrieve a single band (based on index) and stores it in pre-allocated memory. |
714 | 749 | |
715 | 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 | 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 | 800 | /// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum |
810 | 801 | |
811 | 802 | /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum | ... | ... |