bsq.h 1.59 KB
#include "../envi/envi.h"
#include "../envi/binary.h"

namespace rts{

template <typename T>

class bsq: public binary<T> {

protected:
	
	envi header;

public:

	//open a file, given the file and its header's names
	bool open(std::string filename, std::string headername){

		if (header.load(headername)==false){
			std::cout<<"ERROR: unable to load head file"<<std::endl;
			return false;
		}

		binary::open(filename, (header.samples, header.lines, header.bands), header.header_offset);
		return true;
		
	}

	//save one band of the file into the memory, and return the pointer
	bool saveBand( T * p, unsigned int page){

		if (page >= header.bands){										//make sure the bank number is right
			std::cout<<"ERROR: page out of range"<<std::endl;
			return false;
		}

		file.seekg(header.lines * header.samples * page * sizeof(T), ios::beg);   //write into memory from the binary file
		file.read((char *)p, header.lines * header.samples * sizeof(T));

		return true;
	}

	//save one pixel of the file into the memory, and return the pointer
	bool saveSpectrum(T * p, unsigned x, unsigned y){

		unsigned int i;

		if ( x >= header.samples || y >= header.lines){							//make sure the sample and line number is right
			std::cout<<"ERROR: sample or line out of range"<<std::endl;
			return false;
		}

		file.seekg((x + y * header.samples) * sizeof(T), ios::beg);           //point to the certain sample and line
		for (i = 0; i < header.bands; i++)
		{
			file.read((char *)(p + i), sizeof(T));
			file.seekg((header.lines * header.samples - 1) * sizeof(T), ios::cur);    //go to the next band
		}

		return true;	
	}

	};
}