11 #include <sys/types.h>
17 filename(_filename), data(nullptr), fd(-1),
18 width(0), height(0), numFrames(0), bytesPerPixel(0) {
40 return ((
char*)data)+(width*height*bytesPerPixel*frame);
55 munmap(data, width*height*numFrames*bytesPerPixel);
63 unsigned int i, j, avg;
64 unsigned int dim = width*height;
66 uint16_t *ret =
new uint16_t[dim]();
69 for(i=0; i<dim; i++) {
71 for(j=0; j<numFrames; j++)
72 avg+=((uint16_t *)data)[j*dim+i];
73 ret[i] = avg/numFrames;
77 std::cerr <<
"Interleave type not supported!" << std::endl;
84 void EnviFile::parseHeader() {
85 std::ifstream hdrStream;
86 hdrStream.exceptions(std::ifstream::failbit);
89 hdrStream.open(filename);
91 std::getline(hdrStream, line);
92 if(line.compare(0, 4,
"ENVI") !=0)
93 std::cerr << filename <<
" is not an ENVI header file!" << std::endl;
97 while(std::getline(hdrStream,line) && ctr < 5) {
98 if(line.compare(0, 7,
"samples") == 0) {
99 auto pos = line.find_last_of(
" ");
100 width = atoi(line.substr(pos+1).c_str());
102 }
else if(line.compare(0, 5,
"lines") == 0) {
103 auto pos = line.find_last_of(
" ");
104 numFrames = atoi(line.substr(pos+1).c_str());
106 }
else if(line.compare(0, 5,
"bands") == 0) {
107 auto pos = line.find_last_of(
" ");
108 height = atoi(line.substr(pos+1).c_str());
110 }
else if(line.compare(0, 4,
"tint") == 0) {
111 auto pos = line.find_last_of(
"=");
112 tint = strtod(line.substr(pos+1).c_str(),
nullptr);
114 }
else if(line.compare(0, 3,
"fps") == 0) {
115 auto pos = line.find_last_of(
"=");
116 fps = strtod(line.substr(pos+1).c_str(),
nullptr);
124 catch(std::ifstream::failure e) {
125 std::cerr <<
"File " << filename <<
" not found!" << std::endl;
126 std::cerr <<
"Error: " << e.what() << std::endl;
130 std::cout <<
"Width: " << width << std::endl;
131 std::cout <<
"Height: " << height << std::endl;
132 std::cout <<
"numFrames: " << numFrames << std::endl;
138 void EnviFile::loadImage() {
144 auto dot = filename.find_last_of(
".");
145 std::string imgFilename = filename.replace(dot+1, 3,
"raw");
149 fd = open(imgFilename.c_str(), O_RDONLY);
154 if(fstat(fd, &st) == -1) {
158 if((
unsigned long long)(st.st_size) != (width*height*numFrames*bytesPerPixel)) {
159 std::cerr <<
"Expected filesize: " << width*height*numFrames*bytesPerPixel <<
"\nActual filesize: " << st.st_size << std::endl;
161 data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
unsigned long long getNumFrames() const
void * getDataByFrame(unsigned int frame) const
void openFile(const std::string &_filename)
unsigned long long getHeight() const
unsigned long long getWidth() const
unsigned int getBytesPerPixel() const