Commit c1078e19cba3f7205f60e9d86e66759585d550b0
1 parent
63fc1df8
HSIproc bug fixes and speed enhancements
Showing
4 changed files
with
25 additions
and
52 deletions
Show diff stats
stim/envi/bil.h
... | ... | @@ -419,7 +419,7 @@ public: |
419 | 419 | /// @param outname is the name of the output BIP file to be saved to disk. |
420 | 420 | bool bip(std::string outname, bool PROGRESS = false) |
421 | 421 | { |
422 | - unsigned int S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice | |
422 | + unsigned long long S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice | |
423 | 423 | |
424 | 424 | std::ofstream target(outname.c_str(), std::ios::binary); |
425 | 425 | std::string headername = outname + ".hdr"; |
... | ... | @@ -844,7 +844,7 @@ public: |
844 | 844 | |
845 | 845 | /// Saves to disk only those spectra corresponding to mask values != 0 |
846 | 846 | /// Unlike the BIP and BSQ versions of this function, this version saves a different format: the BIL file is saved as a BIP |
847 | - bool sift(std::string outfile, unsigned char* p){ | |
847 | + bool sift(std::string outfile, unsigned char* p, bool PROGRESS = false){ | |
848 | 848 | // Assume X() = X, Y() = Y, Z() = Z. |
849 | 849 | std::ofstream target(outfile.c_str(), std::ios::binary); |
850 | 850 | |
... | ... | @@ -878,14 +878,13 @@ public: |
878 | 878 | target.write((char*)spec, B * sizeof(T)); //write that spectrum to disk. Size is L2. |
879 | 879 | } |
880 | 880 | |
881 | - progress = (double) (y * X() + x) / (Y() * X()) * 100; | |
881 | + if(PROGRESS) progress = (double) ((y+1) * X() + x+1) / (Y() * X()) * 100; | |
882 | 882 | } |
883 | 883 | } |
884 | 884 | target.close(); |
885 | 885 | free(slice); |
886 | 886 | free(spec); |
887 | 887 | |
888 | - progress = 100; | |
889 | 888 | return true; |
890 | 889 | } |
891 | 890 | ... | ... |
stim/envi/bip.h
... | ... | @@ -836,7 +836,7 @@ public: |
836 | 836 | |
837 | 837 | |
838 | 838 | /// Saves to disk only those spectra corresponding to mask values != 0 |
839 | - bool sift(std::string outfile, unsigned char* mask){ | |
839 | + bool sift(std::string outfile, unsigned char* mask, bool PROGRESS = false){ | |
840 | 840 | |
841 | 841 | //reset the file pointer to the beginning of the file |
842 | 842 | file.seekg(0, std::ios::beg); |
... | ... | @@ -875,7 +875,7 @@ public: |
875 | 875 | //write this pixel out |
876 | 876 | target.write((char *)spectrum, B * sizeof(T)); |
877 | 877 | |
878 | - progress = (double) x / XY * 100; | |
878 | + if(PROGRESS) progress = (double) (x+1) / XY * 100; | |
879 | 879 | } |
880 | 880 | |
881 | 881 | } |
... | ... | @@ -884,8 +884,6 @@ public: |
884 | 884 | target.close(); |
885 | 885 | free(spectrum); |
886 | 886 | |
887 | - progress = 100; | |
888 | - | |
889 | 887 | return true; |
890 | 888 | } |
891 | 889 | ... | ... |
stim/envi/bsq.h
... | ... | @@ -324,59 +324,35 @@ public: |
324 | 324 | return true; |
325 | 325 | } |
326 | 326 | |
327 | - /// Convert the current BSQ file to a BIP file with the specified file name. | |
328 | - | |
329 | - /// @param outname is the name of the output BIP file to be saved to disk. | |
330 | - /*bool bip(std::string outname) | |
331 | - { | |
332 | - std::string temp = outname + "_temp"; | |
333 | - std::string headtemp = temp + ".hdr"; | |
334 | - //first creat a temporary bil file and convert bsq file to bil file | |
335 | - bil(temp); | |
336 | - | |
337 | - stim::bil<T> n; | |
338 | - if(n.open(temp, X(), Y(), Z(), offset, w)==false){ //open infile | |
339 | - std::cout<<"ERROR: unable to open input file"<<std::endl; | |
340 | - exit(1); | |
341 | - } | |
342 | - //then convert bil file to bip file | |
343 | - progress = 0; | |
344 | - n.bip(outname); | |
345 | - n.close(); | |
346 | - remove(temp.c_str()); | |
347 | - remove(headtemp.c_str()); | |
348 | - return true; | |
349 | - }*/ | |
350 | - | |
351 | 327 | /// Convert the current BSQ file to a BIL file with the specified file name. |
352 | 328 | |
353 | 329 | /// @param outname is the name of the output BIL file to be saved to disk. |
354 | 330 | bool bil(std::string outname, bool PROGRESS = false) |
355 | 331 | { |
356 | 332 | //simplify image resolution |
357 | - unsigned int L = X() * Z() * sizeof(T); //calculate the number of bytes of a ZX slice | |
333 | + //unsigned int L = X() * Z() * sizeof(T); //calculate the number of bytes of a ZX slice | |
358 | 334 | unsigned int jump = (Y() - 1) * X() * sizeof(T); |
359 | 335 | |
360 | 336 | std::ofstream target(outname.c_str(), std::ios::binary); |
361 | 337 | std::string headername = outname + ".hdr"; |
362 | 338 | |
363 | - unsigned int xz_bytes = X() * Z() * sizeof(T); | |
364 | - T * xz_slice; //pointer to the current spectrum | |
365 | - xz_slice = (T*)malloc(xz_bytes); | |
339 | + unsigned int L = X(); | |
340 | + T* line = (T*)malloc(sizeof(T) * L); | |
366 | 341 | |
367 | 342 | for ( unsigned y = 0; y < Y(); y++) //for each y position |
368 | 343 | { |
369 | 344 | file.seekg(y * X() * sizeof(T), std::ios::beg); //seek to the beginning of the xz slice |
370 | 345 | for ( unsigned z = 0; z < Z(); z++ ) //for each band |
371 | 346 | { |
372 | - file.read((char *)(xz_slice + z * X()), sizeof(T) * X()); //read a line | |
347 | + file.read((char *)line, sizeof(T) * X()); //read a line | |
348 | + target.write((char*)line, sizeof(T) * X()); //write the line to the output file | |
373 | 349 | file.seekg(jump, std::ios::cur); //seek to the next band |
374 | - if(PROGRESS) progress = (double)(y * Z() + z + 1) / (Z() * Y()) * 100; //update the progress counter | |
350 | + if(PROGRESS) progress = (double)((y+1) * Z() + z + 1) / (Z() * Y()) * 100; //update the progress counter | |
375 | 351 | } |
376 | - target.write(reinterpret_cast<const char*>(xz_slice), xz_bytes); //write the generated XZ slice to the target file | |
352 | + //std::cout<<progress<<": "<<y<<std::endl; | |
377 | 353 | } |
378 | 354 | |
379 | - free(xz_slice); | |
355 | + free(line); | |
380 | 356 | target.close(); |
381 | 357 | |
382 | 358 | return true; |
... | ... | @@ -842,16 +818,17 @@ public: |
842 | 818 | /// Saves to disk only those spectra corresponding to mask values != 0 |
843 | 819 | /// @param outfile is the name of the sifted ENVI file to be written to disk |
844 | 820 | /// @param unsigned char* p is the mask file used for sifting |
845 | - bool sift(std::string outfile, unsigned char* p){ | |
821 | + bool sift(std::string outfile, unsigned char* p, bool PROGRESS = false){ | |
846 | 822 | std::ofstream target(outfile.c_str(), std::ios::binary); |
847 | 823 | // open a band (XY plane) |
848 | 824 | unsigned long XY = X() * Y(); //Number of XY pixels |
849 | 825 | unsigned long L = XY * sizeof(T); //size of XY pixels |
826 | + unsigned long long B = Z(); | |
850 | 827 | |
851 | 828 | T * temp = (T*)malloc(L); //allocate memory for a band |
852 | 829 | T * temp_vox = (T*)malloc(sizeof(T)); //allocate memory for one voxel |
853 | 830 | |
854 | - for (unsigned long i = 0; i < Z(); i++) //for each spectral bin | |
831 | + for (unsigned long i = 0; i < B; i++) //for each spectral bin | |
855 | 832 | { |
856 | 833 | band_index(temp, i); //get the specified band (XY sheet by index) |
857 | 834 | for (unsigned long j = 0; j < XY; j++) // for each pixel |
... | ... | @@ -864,8 +841,9 @@ public: |
864 | 841 | continue; |
865 | 842 | } |
866 | 843 | |
867 | - progress = (double)(i * XY + j) / (XY * Z()) * 100; | |
844 | + | |
868 | 845 | } |
846 | + if(PROGRESS) progress = (double)(i+1)/ B * 100; | |
869 | 847 | } |
870 | 848 | target.close(); |
871 | 849 | free(temp); | ... | ... |
stim/envi/envi.h
... | ... | @@ -573,7 +573,7 @@ public: |
573 | 573 | /// Saves in an array only those spectra corresponding to nonzero values of the mask. |
574 | 574 | /// @param outfile is the name of the sifted output file |
575 | 575 | /// @param p is the mask |
576 | - bool sift(std::string outfile, unsigned char* p) | |
576 | + bool sift(std::string outfile, unsigned char* p, bool PROGRESS = false) | |
577 | 577 | { |
578 | 578 | |
579 | 579 | //calculate the number of non-zero values in the mask |
... | ... | @@ -594,31 +594,29 @@ public: |
594 | 594 | new_header.samples = nnz; |
595 | 595 | new_header.save(outfile + ".hdr"); |
596 | 596 | |
597 | - std::cout<<"Saved: "<<outfile+".hdr"<<std::endl; | |
598 | - | |
599 | 597 | if (header.interleave == envi_header::BSQ){ //if the infile is bsq file |
600 | 598 | if (header.data_type == envi_header::float32) |
601 | - return ((bsq<float>*)file)->sift(outfile, p); | |
599 | + return ((bsq<float>*)file)->sift(outfile, p, PROGRESS); | |
602 | 600 | else if (header.data_type == envi_header::float64) |
603 | - return ((bsq<double>*)file)->sift(outfile, p); | |
601 | + return ((bsq<double>*)file)->sift(outfile, p, PROGRESS); | |
604 | 602 | else |
605 | 603 | std::cout << "ERROR: unidentified data type" << std::endl; |
606 | 604 | } |
607 | 605 | |
608 | 606 | else if (header.interleave == envi_header::BIL){ //if the infile is bil file |
609 | 607 | if (header.data_type == envi_header::float32) |
610 | - return ((bil<float>*)file)->sift(outfile, p); | |
608 | + return ((bil<float>*)file)->sift(outfile, p, PROGRESS); | |
611 | 609 | else if (header.data_type == envi_header::float64) |
612 | - return ((bil<double>*)file)->sift(outfile, p); | |
610 | + return ((bil<double>*)file)->sift(outfile, p, PROGRESS); | |
613 | 611 | else |
614 | 612 | std::cout << "ERROR: unidentified data type" << std::endl; |
615 | 613 | } |
616 | 614 | |
617 | 615 | else if (header.interleave == envi_header::BIP){ //if the infile is bip file |
618 | 616 | if (header.data_type == envi_header::float32) |
619 | - return ((bip<float>*)file)->sift(outfile, p); | |
617 | + return ((bip<float>*)file)->sift(outfile, p, PROGRESS); | |
620 | 618 | else if (header.data_type == envi_header::float64) |
621 | - return ((bip<double>*)file)->sift(outfile, p); | |
619 | + return ((bip<double>*)file)->sift(outfile, p, PROGRESS); | |
622 | 620 | else |
623 | 621 | std::cout << "ERROR: unidentified data type" << std::endl; |
624 | 622 | } | ... | ... |