Commit 3c25631c4a63746bf06d7e845ff6f09a32bcf834

Authored by David Mayerich
1 parent 3c714475

added a larger buffer to BSQ->BIL conversion

Showing 2 changed files with 52 additions and 5 deletions   Show diff stats
stim/envi/binary.h
... ... @@ -30,6 +30,8 @@ protected:
30 30  
31 31 double progress; //stores the progress on the current operation (accessible using a thread)
32 32  
  33 + static const size_t bufferSize = 1000000000;
  34 +
33 35  
34 36 /// Private initialization function used to set default parameters in the data structure.
35 37 void init(){
... ...
stim/envi/bsq.h
... ... @@ -377,18 +377,63 @@ public:
377 377 }
378 378  
379 379 /// Convert the current BSQ file to a BIL file with the specified file name.
  380 + bool bil(std::string outname, bool PROGRESS = false){
  381 + size_t XY = X() * Y(); //number of elements in a band
  382 + size_t XYbytes = XY * sizeof(T); //number of bytes in a band
  383 + size_t batchB = binary<T>::bufferSize / (XYbytes); //calculate the number of slices that can fit in memory
  384 + size_t batchXB = X() * batchB; //number of elements in a batch
380 385  
  386 + T* ptrBatch = (T*) malloc(batchB * XYbytes); //allocate a large buffer storing the read data
  387 + T* ptrSlice = (T*) malloc(sizeof(T) * batchB * X()); //allocate space for storing an output buffer
  388 +
  389 + size_t jump = (Z() - batchB) * X() * sizeof(T); //jump between writes in the output file
  390 +
  391 + std::ofstream target(outname.c_str(), std::ios::binary);
  392 + std::string headername = outname + ".hdr";
  393 +
  394 + size_t batches = ceil((double)(Z()) / (double)batchB); //calculate the number of batches
  395 + T* ptrDst;
  396 + T* ptrSrc;
  397 + for(size_t c = 0; c < batches; c++){
  398 + target.seekp(c * batchB * sizeof(T) * X(), std::ios::beg); //seek to the start of the current batch in the output file
  399 + file.read((char*)ptrBatch, sizeof(T) * X() * Y() * batchB); //read a batch
  400 + if(c == (batches - 1)){
  401 + batchB = Z() - (batches - 1) * batchB; //if this is the last batch, calculate the remaining # of bands
  402 + jump = (Z() - batchB) * X() * sizeof(T);
  403 + }
  404 + for(size_t y = 0; y < Y(); y++){ //for each line, store an XB slice in ptrDest
  405 + ptrDst = ptrSlice; //initialize ptrDst to the start of the XB output slice
  406 + ptrSrc = ptrBatch + (y * X());
  407 + for(size_t b = 0; b < batchB; b++){ //for each band in the current line
  408 + memcpy(ptrDst, ptrSrc, X() * sizeof(T)); //copy the band line from the source to the destination
  409 + ptrDst += X();
  410 + ptrSrc += X() * Y();
  411 + }
  412 + target.write((char*)ptrSlice, sizeof(T) * X() * batchB); //write the XB slice to disk
  413 + target.seekp(jump, std::ios::cur); //seek to the beginning of the next XB slice in the batch
  414 + }
  415 + if(PROGRESS) progress = (double)(c+1)/(double)(batches) * 100;
  416 + }
  417 +
  418 + //if(PROGRESS) progress = 100;
  419 +
  420 + free(ptrBatch);
  421 + free(ptrSlice);
  422 + target.close();
  423 +
  424 + return true;
  425 + }
381 426 /// @param outname is the name of the output BIL file to be saved to disk.
382   - bool bil(std::string outname, bool PROGRESS = false)
383   - {
  427 + /*bool bil(std::string outname, bool PROGRESS = false){
  428 +
384 429 //simplify image resolution
385 430 unsigned long long jump = (Y() - 1) * X() * sizeof(T);
386 431  
387 432 std::ofstream target(outname.c_str(), std::ios::binary);
388 433 std::string headername = outname + ".hdr";
389 434  
390   - unsigned long long L = X();
391   - T* line = (T*)malloc(sizeof(T) * L);
  435 + unsigned long long L = X(); //calculate the number of pixels in a line
  436 + T* line = (T*)malloc(sizeof(T) * L); //allocate space for a line
392 437  
393 438 for ( unsigned long long y = 0; y < Y(); y++) //for each y position
394 439 {
... ... @@ -406,7 +451,7 @@ public:
406 451 target.close();
407 452  
408 453 return true;
409   - }
  454 + }*/
410 455  
411 456 /// Return a baseline corrected band given two adjacent baseline points and their bands. The result is stored in a pre-allocated array.
412 457  
... ...