Commit f78ceb6484bea2dab62e8cabc231f9219b9fdf62

Authored by David Mayerich
1 parent dd5aab2f

fixed BIP cropping bug

Showing 2 changed files with 22 additions and 17 deletions   Show diff stats
@@ -1479,9 +1479,9 @@ public: @@ -1479,9 +1479,9 @@ public:
1479 bool PROGRESS = false){ 1479 bool PROGRESS = false){
1480 1480
1481 //calculate the new number of samples, lines, and bands 1481 //calculate the new number of samples, lines, and bands
1482 - unsigned long long samples = x1 - x0;  
1483 - unsigned long long lines = y1 - y0;  
1484 - unsigned long long bands = b1 - b0; 1482 + unsigned long long samples = x1 - x0 + 1;
  1483 + unsigned long long lines = y1 - y0 + 1;
  1484 + unsigned long long bands = b1 - b0 + 1;
1485 1485
1486 //calculate the length of one cropped spectrum 1486 //calculate the length of one cropped spectrum
1487 unsigned long long L = bands * sizeof(T); 1487 unsigned long long L = bands * sizeof(T);
@@ -1489,32 +1489,36 @@ public: @@ -1489,32 +1489,36 @@ public:
1489 //unsigned long long L = Z() * sizeof(T); 1489 //unsigned long long L = Z() * sizeof(T);
1490 1490
1491 //allocate space for the spectrum 1491 //allocate space for the spectrum
1492 - T* temp = (T*)malloc(L); 1492 + char* temp = (char*)malloc(L);
1493 1493
1494 //open an output file for binary writing 1494 //open an output file for binary writing
1495 std::ofstream out(outfile.c_str(), std::ios::binary); 1495 std::ofstream out(outfile.c_str(), std::ios::binary);
1496 1496
1497 //seek to the first pixel in the cropped image 1497 //seek to the first pixel in the cropped image
1498 - file.seekg( (y0 * X() * Z() + x0 * Z() + b0) * sizeof(T), std::ios::beg); 1498 + size_t startx = x0 * Z();
  1499 + size_t starty = y0 * X() * Z();
  1500 + size_t startb = b0;
  1501 + file.seekg( (starty + startx + startb) * sizeof(T), std::ios::beg);
1499 1502
1500 //distance between sample spectra in the same line 1503 //distance between sample spectra in the same line
1501 - unsigned long long jump_sample = ( (Z() - b1) + b0 ) * sizeof(T); 1504 + size_t dist_between_samples = Z() - bands;
  1505 + size_t jump_sample = dist_between_samples * sizeof(T);
1502 1506
1503 //distance between sample spectra in adjacent lines 1507 //distance between sample spectra in adjacent lines
1504 - unsigned long long jump_line = ( X() - x1 + x0 ) * Z() * sizeof(T); 1508 + //unsigned long long jump_line = ( X() - x1 + x0 ) * Z() * sizeof(T);
  1509 + size_t dist_between_lines = X() - samples;
  1510 + size_t jump_line = dist_between_lines * Z() * sizeof(T);
1505 1511
1506 1512
1507 //unsigned long long sp = y0 * X() + x0; //start pixel 1513 //unsigned long long sp = y0 * X() + x0; //start pixel
1508 1514
1509 //for each pixel in the image 1515 //for each pixel in the image
1510 - for (unsigned y = 0; y < lines; y++)  
1511 - {  
1512 - for (unsigned x = 0; x < samples; x++)  
1513 - { 1516 + for (unsigned y = 0; y < lines; y++) {
  1517 + for (unsigned x = 0; x < samples; x++) {
1514 //read the cropped spectral region 1518 //read the cropped spectral region
1515 - file.read( (char*) temp, L ); 1519 + file.read(temp, L );
1516 //pixel(temp, sp + x + y * X()); 1520 //pixel(temp, sp + x + y * X());
1517 - out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file 1521 + out.write(temp, L); //write slice data into target file
1518 1522
1519 file.seekg(jump_sample, std::ios::cur); 1523 file.seekg(jump_sample, std::ios::cur);
1520 1524
@@ -1524,6 +1528,7 @@ public: @@ -1524,6 +1528,7 @@ public:
1524 file.seekg(jump_line, std::ios::cur); 1528 file.seekg(jump_line, std::ios::cur);
1525 } 1529 }
1526 free(temp); 1530 free(temp);
  1531 + out.close();
1527 1532
1528 return true; 1533 return true;
1529 } 1534 }
@@ -1545,11 +1545,11 @@ public: @@ -1545,11 +1545,11 @@ public:
1545 1545
1546 //save the header for the cropped file 1546 //save the header for the cropped file
1547 stim::envi_header new_header = header; 1547 stim::envi_header new_header = header;
1548 - new_header.samples = x1 - x0;  
1549 - new_header.lines = y1 - y0;  
1550 - new_header.bands = b1 - b0; 1548 + new_header.samples = x1 - x0 + 1;
  1549 + new_header.lines = y1 - y0 + 1;
  1550 + new_header.bands = b1 - b0 + 1;
1551 std::vector<double>::const_iterator first = new_header.wavelength.begin() + b0; 1551 std::vector<double>::const_iterator first = new_header.wavelength.begin() + b0;
1552 - std::vector<double>::const_iterator last = new_header.wavelength.begin() + b1; 1552 + std::vector<double>::const_iterator last = new_header.wavelength.begin() + b1 + 1;
1553 new_header.wavelength = std::vector<double>(first, last); 1553 new_header.wavelength = std::vector<double>(first, last);
1554 new_header.save(outfile + ".hdr"); 1554 new_header.save(outfile + ".hdr");
1555 1555