Commit 3033b88677f3d9c402a43cbaac5b6c19b0db4565

Authored by David Mayerich
1 parent 177c7863

implemented spectral cropping in BIL and BIP files

Showing 2 changed files with 72 additions and 25 deletions   Show diff stats
stim/envi/bil.h
... ... @@ -1012,27 +1012,48 @@ public:
1012 1012 unsigned long long b0,
1013 1013 unsigned long long b1){
1014 1014  
1015   - //calculate the new number of samples and lines
1016   - unsigned long long sam = x1 - x0; //samples
1017   - unsigned long long lin = y1 - y0; //lines
1018   - unsigned long long L = sam * Z() * sizeof(T);
1019   - //get specified band and save
1020   - T* temp = (T*)malloc(L);
  1015 + //calculate the new image parameters
  1016 + unsigned long long samples = x1 - x0;
  1017 + unsigned long long lines = y1 - y0;
  1018 + unsigned long long bands = b1 - b0;
  1019 +
  1020 + //calculate the size of a line
  1021 + unsigned long long L = samples * sizeof(T);
  1022 +
  1023 +
  1024 + //allocate space for a line
  1025 + T* temp = (T*)malloc(bands * L);
  1026 +
  1027 + //create an output stream to store the cropped file
1021 1028 std::ofstream out(outfile.c_str(), std::ios::binary);
1022   - unsigned long long jumpb = (X() - sam) * sizeof(T); //jump pointer to the next band
1023   - //get start
1024   - file.seekg((y0 * X() * Z() + x0) * sizeof(T), std::ios::beg);
1025   - for (unsigned i = 0; i < lin; i++)
  1029 +
  1030 + //calculate the distance between bands
  1031 + unsigned long long jumpb = (X() - samples) * sizeof(T);
  1032 +
  1033 + //distance needed to jump from the previous line of the last band to the next line of the first band
  1034 + unsigned long long longjump = ((Z() - b1) * X() + b0 * X()) * sizeof(T);
  1035 +
  1036 + //set the start position for the cropped region
  1037 + file.seekg((y0 * X() * Z() + b0 * X() + x0) * sizeof(T), std::ios::beg);
  1038 +
  1039 + for (unsigned x = 0; x < lines; x++)
1026 1040 {
1027   - for (unsigned j = 0; j < Z(); j++)
  1041 + for (unsigned z = b0; z < b1; z++)
1028 1042 {
1029   - file.read((char *)(temp + j * sam), sizeof(T) * sam);
  1043 + file.read((char *)(temp + z * samples), sizeof(T) * samples);
1030 1044 file.seekg(jumpb, std::ios::cur); //go to the next band
1031 1045  
1032   - thread_data = (double)(i * Z() + j) / (lin * Z()) * 100;
  1046 + thread_data = (double)(x * Z() + z) / (lines * Z()) * 100;
1033 1047 }
1034   - out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file
  1048 +
  1049 + //write slice data into target file
  1050 + out.write(reinterpret_cast<const char*>(temp), bands * L);
  1051 +
  1052 + //seek to the beginning of the next X-Z slice
  1053 + file.seekg(longjump, std::ios::cur);
1035 1054 }
  1055 +
  1056 + //free the temporary frame
1036 1057 free(temp);
1037 1058  
1038 1059 thread_data = 100;
... ...
stim/envi/bip.h
... ... @@ -1068,24 +1068,50 @@ public:
1068 1068 unsigned long long b0,
1069 1069 unsigned long long b1){
1070 1070  
1071   - //calculate the new number of samples and lines
1072   - unsigned long long sam = x1 - x0; //samples
1073   - unsigned long long lin = y1 - y0; //lines
1074   - unsigned long long L = Z() * sizeof(T);
1075   - //get specified band and save
  1071 + //calculate the new number of samples, lines, and bands
  1072 + unsigned long long samples = x1 - x0;
  1073 + unsigned long long lines = y1 - y0;
  1074 + unsigned long long bands = b1 - b0;
  1075 +
  1076 + //calculate the length of one cropped spectrum
  1077 + unsigned long long L = bands * sizeof(T);
  1078 +
  1079 + //unsigned long long L = Z() * sizeof(T);
  1080 +
  1081 + //allocate space for the spectrum
1076 1082 T* temp = (T*)malloc(L);
  1083 +
  1084 + //open an output file for binary writing
1077 1085 std::ofstream out(outfile.c_str(), std::ios::binary);
1078   - //get start
1079   - unsigned long long sp = y0 * X() + x0; //start pixel
1080   - for (unsigned i = 0; i < lin; i++)
  1086 +
  1087 + //seek to the first pixel in the cropped image
  1088 + file.seekg( (y0 * X() * Z() + x0 * Z() + b0) * sizeof(T), std::ios::beg);
  1089 +
  1090 + //distance between sample spectra in the same line
  1091 + unsigned long long jump_sample = ( (Z() - b1) + b0 ) * sizeof(T);
  1092 +
  1093 + //distance between sample spectra in adjacent lines
  1094 + unsigned long long jump_line = (X() - x1) * Z() * sizeof(T);
  1095 +
  1096 +
  1097 + //unsigned long long sp = y0 * X() + x0; //start pixel
  1098 +
  1099 + //for each pixel in the image
  1100 + for (unsigned y = 0; y < lines; y++)
1081 1101 {
1082   - for (unsigned j = 0; j < sam; j++)
  1102 + for (unsigned x = 0; x < samples; x++)
1083 1103 {
1084   - pixel(temp, sp + j + i * X());
  1104 + //read the cropped spectral region
  1105 + file.read( (char*) temp, L );
  1106 + //pixel(temp, sp + x + y * X());
1085 1107 out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file
1086 1108  
1087   - thread_data = (double)(i * sam + j) / (lin * sam) * 100;
  1109 + file.seekg(jump_sample, std::ios::cur);
  1110 +
  1111 + thread_data = (double)(y * samples + x) / (lines * samples) * 100;
1088 1112 }
  1113 +
  1114 + file.seekg(jump_line, std::ios::cur);
1089 1115 }
1090 1116 free(temp);
1091 1117  
... ...